1
0
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:
Matthias Kretschmann 2020-10-16 10:40:03 +02:00 committed by GitHub
commit 09c83eeb51
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 2 deletions

View File

@ -8,6 +8,9 @@ import { MetadataCache } from '../metadatacache/MetadataCache'
import { didNoZeroX, didPrefixed } from '../utils'
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 {
poolAddress: string
tokens: string[]
@ -449,7 +452,7 @@ export class OceanPool extends Pool {
const balance = await super.getReserve(poolAddress, tokenAddress)
if (parseFloat(balance) > 0) {
const result = new BigNumber(this.web3.utils.toWei(balance))
.dividedBy(3)
.multipliedBy(POOL_MAX_AMOUNT_IN_LIMIT)
.integerValue(BigNumber.ROUND_DOWN)
.minus(1)
return this.web3.utils.fromWei(result.toString())
@ -468,7 +471,7 @@ export class OceanPool extends Pool {
const balance = await super.getReserve(poolAddress, tokenAddress)
if (parseFloat(balance) > 0) {
const result = new BigNumber(this.web3.utils.toWei(balance))
.dividedBy(4)
.multipliedBy(POOL_MAX_AMOUNT_OUT_LIMIT)
.integerValue(BigNumber.ROUND_DOWN)
.minus(1)
return this.web3.utils.fromWei(result.toString())

View File

@ -812,6 +812,7 @@ export class Pool extends PoolFactory {
): Promise<string> {
const pool = new this.web3.eth.Contract(this.poolABI, poolAddress)
let amount = null
if (parseFloat(tokenAmountOut) >= parseFloat(tokenBalanceOut)) return null
try {
const result = await pool.methods
.calcInGivenOut(

View File

@ -173,6 +173,13 @@ describe('Balancer flow', () => {
const totalSupply = await Pool.getPoolSharesTotalSupply(alicePoolAddress)
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 () => {
const requiredOcean = await Pool.getOceanNeeded(alicePoolAddress, '1')
assert(Number(requiredOcean) > 0)