diff --git a/src/balancer/OceanPool.ts b/src/balancer/OceanPool.ts index ce9d03d4..144738b4 100644 --- a/src/balancer/OceanPool.ts +++ b/src/balancer/OceanPool.ts @@ -749,6 +749,7 @@ export class OceanPool extends Pool { * @param {String} account * @param {String} poolAddress * @param {String} amount datatoken amount + * @param {String} maximumPoolShares * @return {TransactionReceipt} */ public async removeDTLiquidity( @@ -828,10 +829,44 @@ export class OceanPool extends Pool { } /** - * Remove Ocean Token amount from pool liquidity + * Remove Ocean Token amount from pool liquidity based on the minimum allowed of Ocean Tokens received + * @param {String} account + * @param {String} poolAddress + * @param {String} poolShares pool shares + * @param {String} minOcean minimum amount of OCEAN received + * @return {TransactionReceipt} + */ + public async removeOceanLiquidityWithMinimum( + account: string, + poolAddress: string, + poolShares: string, + minOcean: string + ): Promise { + if (this.oceanAddress == null) { + this.logger.error('ERROR: oceanAddress is not defined') + return null + } + const usershares = await this.sharesBalance(account, poolAddress) + if (parseFloat(usershares) < parseFloat(poolShares)) { + this.logger.error('ERROR: Not enough poolShares') + return null + } + + return super.exitswapPoolAmountIn( + account, + poolAddress, + this.oceanAddress, + poolShares, + minOcean + ) + } + + /** + * Remove Ocean Token amount from pool liquidity based on the maximum pool shares allowed to be spent * @param {String} account * @param {String} poolAddress * @param {String} amount Ocean Token amount in OCEAN + * @param {String} maximumPoolShares maximum pool shares allowed to be spent * @return {TransactionReceipt} */ public async removeOceanLiquidity( diff --git a/test/unit/balancer/Balancer.test.ts b/test/unit/balancer/Balancer.test.ts index 1957b75c..38ce7ad7 100644 --- a/test/unit/balancer/Balancer.test.ts +++ b/test/unit/balancer/Balancer.test.ts @@ -34,7 +34,7 @@ describe('Balancer flow', () => { let contracts: TestContractHandler let datatoken: DataTokens let tokenAddress: string - let consoleDebug: true + const consoleDebug = false let greatPool: string const tokenAmount = '1000' const transferAmount = '200' @@ -405,6 +405,7 @@ describe('Balancer flow', () => { if (consoleDebug) console.log('poolShares:' + poolShares) assert(parseFloat(poolShares) > 0) }) + it('Bob should remove Ocean liquidity from pool ', async () => { const currentDtReserve = await Pool.getOceanReserve(greatPool) const bobDtBalance = await datatoken.balance(oceanTokenAddress, bob) @@ -427,6 +428,42 @@ describe('Balancer flow', () => { assert(parseFloat(bobDtBalance) < parseFloat(newbobDtBalance)) assert(parseFloat(poolShares) > parseFloat(newpoolShares)) }) + + it('Bob should remove Ocean liquidity from pool by specifying slippage ', async () => { + const currentDtReserve = await Pool.getOceanReserve(greatPool) + const bobDtBalance = await datatoken.balance(oceanTokenAddress, bob) + + const poolShares = await Pool.sharesBalance(bob, greatPool) + const poolSharesAmount = '0.75' + + if (consoleDebug) console.log('currentDtReserve:' + currentDtReserve) + if (consoleDebug) console.log('bobDtBalance:' + bobDtBalance) + if (consoleDebug) console.log('poolShares:' + poolShares) + + const oceanAmount = await Pool.getOceanRemovedforPoolShares( + greatPool, + poolSharesAmount + ) + // 10% slippage + const oceanAmountWithSlippage = parseFloat(oceanAmount) * 0.9 + await Pool.removeOceanLiquidityWithMinimum( + bob, + greatPool, + poolSharesAmount, + oceanAmountWithSlippage.toString() + ) + + const newDtReserve = await Pool.getOceanReserve(greatPool) + const newbobDtBalance = await datatoken.balance(oceanTokenAddress, bob) + const newpoolShares = await Pool.sharesBalance(bob, greatPool) + + if (consoleDebug) console.log('newDtReserve:' + newDtReserve) + if (consoleDebug) console.log('newbobDtBalance:' + newbobDtBalance) + if (consoleDebug) console.log('newpoolShares:' + newpoolShares) + assert(parseFloat(newDtReserve) < parseFloat(currentDtReserve)) + assert(parseFloat(bobDtBalance) < parseFloat(newbobDtBalance)) + assert(parseFloat(poolShares) > parseFloat(newpoolShares)) + }) it('Alice should know how many tokens she will get for removing all liquidity', async () => { const aliceShares = await Pool.sharesBalance(alice, greatPool) const amounts = await Pool.getTokensRemovedforPoolShares(greatPool, aliceShares)