1
0
mirror of https://github.com/oceanprotocol/ocean.js.git synced 2024-11-26 20:39:05 +01:00

add getTokensRemovedforPoolShares

This commit is contained in:
alexcos20 2020-10-14 02:57:45 -07:00
parent 7f279350a0
commit 457b2a108a
2 changed files with 35 additions and 11 deletions

View File

@ -5,6 +5,7 @@ import { Pool } from './Pool'
import { EventData, Filter } from 'web3-eth-contract' import { EventData, Filter } from 'web3-eth-contract'
import BigNumber from 'bignumber.js' import BigNumber from 'bignumber.js'
import Decimal from 'decimal.js' import Decimal from 'decimal.js'
import { createDiffieHellman } from 'crypto'
declare type PoolTransactionType = 'swap' | 'join' | 'exit' declare type PoolTransactionType = 'swap' | 'join' | 'exit'
@ -13,6 +14,11 @@ export interface PoolDetails {
tokens: string[] tokens: string[]
} }
export interface TokensReceived {
dtAmount: string
oceanAmount: string
}
export interface PoolTransaction { export interface PoolTransaction {
poolAddress: string poolAddress: string
dtAddress: string dtAddress: string
@ -385,6 +391,31 @@ export class OceanPool extends Pool {
return this.calcSingleOutGivenPoolIn(poolAddress, this.oceanAddress, poolShares) return this.calcSingleOutGivenPoolIn(poolAddress, this.oceanAddress, poolShares)
} }
/**
* Returns datatoken & Ocean amounts received after spending poolShares
* @param poolAddress
* @param poolShares
* @return {TokensReceived}
*/
public async getTokensRemovedforPoolShares(
poolAddress: string,
poolShares: string
): Promise<TokensReceived> {
const amounts = { dtAmount: '0', oceanAmount: '0' }
try {
const totalPoolTokens = await this.getPoolSharesTotalSupply(poolAddress)
amounts.dtAmount = String(
(Number(poolShares) / Number(totalPoolTokens)) *
Number(await this.getDTReserve(poolAddress))
)
amounts.oceanAmount = String(
(Number(poolShares) / Number(totalPoolTokens)) *
Number(await this.getOceanReserve(poolAddress))
)
} catch (e) {}
return amounts
}
/** /**
* Returns max DT amount that you can add to the pool * Returns max DT amount that you can add to the pool
* @param poolAddress * @param poolAddress

View File

@ -388,18 +388,11 @@ describe('Balancer flow', () => {
assert(parseFloat(bobDtBalance) < parseFloat(newbobDtBalance)) assert(parseFloat(bobDtBalance) < parseFloat(newbobDtBalance))
assert(parseFloat(poolShares) > parseFloat(newpoolShares)) assert(parseFloat(poolShares) > parseFloat(newpoolShares))
}) })
it('ALice should know how many tokens she will get for removing all liquidity', async () => {
it('ALice should remove all liquidity', async () => {
const aliceShares = await Pool.sharesBalance(alice, greatPool) const aliceShares = await Pool.sharesBalance(alice, greatPool)
const aliceDtBalance = await datatoken.balance(tokenAddress, alice) const amounts = await Pool.getTokensRemovedforPoolShares(greatPool, aliceShares)
const aliceOceanBalance = await datatoken.balance(oceanTokenAddress, alice) assert(parseFloat(amounts.dtAmount) > 0)
await Pool.removePoolLiquidity(alice, greatPool, aliceShares) assert(parseFloat(amounts.oceanAmount) > 0)
const newAliceDtBalance = await datatoken.balance(tokenAddress, alice)
const newAliceOceanBalance = await datatoken.balance(oceanTokenAddress, alice)
const newAliceShares = await Pool.sharesBalance(alice, greatPool)
assert(parseFloat(aliceDtBalance) < parseFloat(newAliceDtBalance))
assert(parseFloat(aliceOceanBalance) < parseFloat(newAliceOceanBalance))
assert(parseFloat(aliceShares) > parseFloat(newAliceShares))
}) })
it('ALice should get all the pools that she created', async () => { it('ALice should get all the pools that she created', async () => {
const alicePools = await Pool.getPoolsbyCreator(alice) const alicePools = await Pool.getPoolsbyCreator(alice)