mirror of
https://github.com/oceanprotocol/ocean.js.git
synced 2024-11-26 20:39:05 +01:00
Merge pull request #361 from oceanprotocol/feature/add_pool_getTokensRemovedforPoolShares
add getTokensRemovedforPoolShares
This commit is contained in:
commit
71314a83f8
@ -4,7 +4,6 @@ import { TransactionReceipt } from 'web3-core'
|
|||||||
import { Pool } from './Pool'
|
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'
|
|
||||||
|
|
||||||
declare type PoolTransactionType = 'swap' | 'join' | 'exit'
|
declare type PoolTransactionType = 'swap' | 'join' | 'exit'
|
||||||
|
|
||||||
@ -13,6 +12,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 +389,34 @@ 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 {String} poolAddress
|
||||||
|
* @param {String} poolShares
|
||||||
|
* @return {TokensReceived}
|
||||||
|
*/
|
||||||
|
public async getTokensRemovedforPoolShares(
|
||||||
|
poolAddress: string,
|
||||||
|
poolShares: string
|
||||||
|
): Promise<TokensReceived> {
|
||||||
|
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
|
* Returns max DT amount that you can add to the pool
|
||||||
* @param poolAddress
|
* @param poolAddress
|
||||||
|
@ -388,7 +388,12 @@ 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 () => {
|
||||||
|
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 () => {
|
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 aliceDtBalance = await datatoken.balance(tokenAddress, alice)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user