mirror of
https://github.com/oceanprotocol/ocean.js.git
synced 2024-11-26 20:39:05 +01:00
Merge pull request #365 from oceanprotocol/bug/fix_price_calculation
fix bug when amountOut>=balanceOut
This commit is contained in:
commit
09c83eeb51
@ -8,6 +8,9 @@ import { MetadataCache } from '../metadatacache/MetadataCache'
|
|||||||
import { didNoZeroX, didPrefixed } from '../utils'
|
import { didNoZeroX, didPrefixed } from '../utils'
|
||||||
declare type PoolTransactionType = 'swap' | 'join' | 'exit'
|
declare type PoolTransactionType = 'swap' | 'join' | 'exit'
|
||||||
|
|
||||||
|
const POOL_MAX_AMOUNT_IN_LIMIT = 0.25 // maximum 1/4 of the pool reserve
|
||||||
|
const POOL_MAX_AMOUNT_OUT_LIMIT = 0.25 // maximum 1/4 of the pool reserve
|
||||||
|
|
||||||
export interface PoolDetails {
|
export interface PoolDetails {
|
||||||
poolAddress: string
|
poolAddress: string
|
||||||
tokens: string[]
|
tokens: string[]
|
||||||
@ -449,7 +452,7 @@ export class OceanPool extends Pool {
|
|||||||
const balance = await super.getReserve(poolAddress, tokenAddress)
|
const balance = await super.getReserve(poolAddress, tokenAddress)
|
||||||
if (parseFloat(balance) > 0) {
|
if (parseFloat(balance) > 0) {
|
||||||
const result = new BigNumber(this.web3.utils.toWei(balance))
|
const result = new BigNumber(this.web3.utils.toWei(balance))
|
||||||
.dividedBy(3)
|
.multipliedBy(POOL_MAX_AMOUNT_IN_LIMIT)
|
||||||
.integerValue(BigNumber.ROUND_DOWN)
|
.integerValue(BigNumber.ROUND_DOWN)
|
||||||
.minus(1)
|
.minus(1)
|
||||||
return this.web3.utils.fromWei(result.toString())
|
return this.web3.utils.fromWei(result.toString())
|
||||||
@ -468,7 +471,7 @@ export class OceanPool extends Pool {
|
|||||||
const balance = await super.getReserve(poolAddress, tokenAddress)
|
const balance = await super.getReserve(poolAddress, tokenAddress)
|
||||||
if (parseFloat(balance) > 0) {
|
if (parseFloat(balance) > 0) {
|
||||||
const result = new BigNumber(this.web3.utils.toWei(balance))
|
const result = new BigNumber(this.web3.utils.toWei(balance))
|
||||||
.dividedBy(4)
|
.multipliedBy(POOL_MAX_AMOUNT_OUT_LIMIT)
|
||||||
.integerValue(BigNumber.ROUND_DOWN)
|
.integerValue(BigNumber.ROUND_DOWN)
|
||||||
.minus(1)
|
.minus(1)
|
||||||
return this.web3.utils.fromWei(result.toString())
|
return this.web3.utils.fromWei(result.toString())
|
||||||
|
@ -812,6 +812,7 @@ export class Pool extends PoolFactory {
|
|||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
const pool = new this.web3.eth.Contract(this.poolABI, poolAddress)
|
const pool = new this.web3.eth.Contract(this.poolABI, poolAddress)
|
||||||
let amount = null
|
let amount = null
|
||||||
|
if (parseFloat(tokenAmountOut) >= parseFloat(tokenBalanceOut)) return null
|
||||||
try {
|
try {
|
||||||
const result = await pool.methods
|
const result = await pool.methods
|
||||||
.calcInGivenOut(
|
.calcInGivenOut(
|
||||||
|
@ -173,6 +173,13 @@ describe('Balancer flow', () => {
|
|||||||
const totalSupply = await Pool.getPoolSharesTotalSupply(alicePoolAddress)
|
const totalSupply = await Pool.getPoolSharesTotalSupply(alicePoolAddress)
|
||||||
assert(Number(totalSupply) > 0)
|
assert(Number(totalSupply) > 0)
|
||||||
})
|
})
|
||||||
|
it('Should fail to get amount of Ocean needed to buy more dtTokens than reserve', async () => {
|
||||||
|
const requiredOcean = await Pool.getOceanNeeded(
|
||||||
|
alicePoolAddress,
|
||||||
|
await Pool.getDTReserve(alicePoolAddress)
|
||||||
|
)
|
||||||
|
assert(requiredOcean === null)
|
||||||
|
})
|
||||||
it('Get amount of Ocean needed to buy 1 dtToken', async () => {
|
it('Get amount of Ocean needed to buy 1 dtToken', async () => {
|
||||||
const requiredOcean = await Pool.getOceanNeeded(alicePoolAddress, '1')
|
const requiredOcean = await Pool.getOceanNeeded(alicePoolAddress, '1')
|
||||||
assert(Number(requiredOcean) > 0)
|
assert(Number(requiredOcean) > 0)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user