diff --git a/src/balancer/OceanPool.ts b/src/balancer/OceanPool.ts index 3153c0a1..bbf05c8d 100644 --- a/src/balancer/OceanPool.ts +++ b/src/balancer/OceanPool.ts @@ -4,7 +4,6 @@ import { TransactionReceipt } from 'web3-core' import { Pool } from './Pool' import { EventData, Filter } from 'web3-eth-contract' import BigNumber from 'bignumber.js' -import Decimal from 'decimal.js' declare type PoolTransactionType = 'swap' | 'join' | 'exit' @@ -13,6 +12,11 @@ export interface PoolDetails { tokens: string[] } +export interface TokensReceived { + dtAmount: string + oceanAmount: string +} + export interface PoolTransaction { poolAddress: string dtAddress: string @@ -385,6 +389,34 @@ export class OceanPool extends Pool { return this.calcSingleOutGivenPoolIn(poolAddress, this.oceanAddress, poolShares) } + /** + * Returns Datatoken & Ocean amounts received after spending poolShares + * @param {String} poolAddress + * @param {String} poolShares + * @return {TokensReceived} + */ + public async getTokensRemovedforPoolShares( + poolAddress: string, + poolShares: string + ): Promise { + try { + const totalPoolTokens = await this.getPoolSharesTotalSupply(poolAddress) + const dtReserve = await this.getDTReserve(poolAddress) + const oceanReserve = await this.getOceanReserve(poolAddress) + + const dtAmount = `${ + (Number(poolShares) / Number(totalPoolTokens)) * Number(dtReserve) + }` + const oceanAmount = `${ + (Number(poolShares) / Number(totalPoolTokens)) * Number(oceanReserve) + }` + + return { dtAmount, oceanAmount } + } catch (e) { + console.error(`ERROR: Unable to get token info. ${e.message}`) + } + } + /** * Returns max DT amount that you can add to the pool * @param poolAddress diff --git a/test/unit/balancer/Balancer.test.ts b/test/unit/balancer/Balancer.test.ts index d574ac80..e164dc17 100644 --- a/test/unit/balancer/Balancer.test.ts +++ b/test/unit/balancer/Balancer.test.ts @@ -388,7 +388,12 @@ describe('Balancer flow', () => { 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) + assert(parseFloat(amounts.dtAmount) > 0) + assert(parseFloat(amounts.oceanAmount) > 0) + }) it('ALice should remove all liquidity', async () => { const aliceShares = await Pool.sharesBalance(alice, greatPool) const aliceDtBalance = await datatoken.balance(tokenAddress, alice)