From d3f6cfd05ccfe5b38fb817c2ab7e0d6712163aae Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Thu, 15 Oct 2020 04:11:34 -0700 Subject: [PATCH 1/2] fix bug when amountOut>=balanceOut --- src/balancer/OceanPool.ts | 7 +++++-- src/balancer/Pool.ts | 1 + 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/balancer/OceanPool.ts b/src/balancer/OceanPool.ts index 5faaa4f9..cb97dc12 100644 --- a/src/balancer/OceanPool.ts +++ b/src/balancer/OceanPool.ts @@ -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()) diff --git a/src/balancer/Pool.ts b/src/balancer/Pool.ts index 60ab4cda..2a517018 100644 --- a/src/balancer/Pool.ts +++ b/src/balancer/Pool.ts @@ -812,6 +812,7 @@ export class Pool extends PoolFactory { ): Promise { 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( From b34166732f4ddaacb4a7ba9ab9f13cb6e5961cfc Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Thu, 15 Oct 2020 23:04:04 -0700 Subject: [PATCH 2/2] add test --- test/unit/balancer/Balancer.test.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/unit/balancer/Balancer.test.ts b/test/unit/balancer/Balancer.test.ts index e164dc17..85aa330c 100644 --- a/test/unit/balancer/Balancer.test.ts +++ b/test/unit/balancer/Balancer.test.ts @@ -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)