mirror of
https://github.com/oceanprotocol/ocean.js.git
synced 2024-11-26 20:39:05 +01:00
Add remove ocean liquidity function (#685)
Add remove ocean liquidity function Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro>
This commit is contained in:
parent
57b1e77197
commit
79ff76e026
@ -749,6 +749,7 @@ export class OceanPool extends Pool {
|
|||||||
* @param {String} account
|
* @param {String} account
|
||||||
* @param {String} poolAddress
|
* @param {String} poolAddress
|
||||||
* @param {String} amount datatoken amount
|
* @param {String} amount datatoken amount
|
||||||
|
* @param {String} maximumPoolShares
|
||||||
* @return {TransactionReceipt}
|
* @return {TransactionReceipt}
|
||||||
*/
|
*/
|
||||||
public async removeDTLiquidity(
|
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<TransactionReceipt> {
|
||||||
|
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} account
|
||||||
* @param {String} poolAddress
|
* @param {String} poolAddress
|
||||||
* @param {String} amount Ocean Token amount in OCEAN
|
* @param {String} amount Ocean Token amount in OCEAN
|
||||||
|
* @param {String} maximumPoolShares maximum pool shares allowed to be spent
|
||||||
* @return {TransactionReceipt}
|
* @return {TransactionReceipt}
|
||||||
*/
|
*/
|
||||||
public async removeOceanLiquidity(
|
public async removeOceanLiquidity(
|
||||||
|
@ -34,7 +34,7 @@ describe('Balancer flow', () => {
|
|||||||
let contracts: TestContractHandler
|
let contracts: TestContractHandler
|
||||||
let datatoken: DataTokens
|
let datatoken: DataTokens
|
||||||
let tokenAddress: string
|
let tokenAddress: string
|
||||||
let consoleDebug: true
|
const consoleDebug = false
|
||||||
let greatPool: string
|
let greatPool: string
|
||||||
const tokenAmount = '1000'
|
const tokenAmount = '1000'
|
||||||
const transferAmount = '200'
|
const transferAmount = '200'
|
||||||
@ -405,6 +405,7 @@ describe('Balancer flow', () => {
|
|||||||
if (consoleDebug) console.log('poolShares:' + poolShares)
|
if (consoleDebug) console.log('poolShares:' + poolShares)
|
||||||
assert(parseFloat(poolShares) > 0)
|
assert(parseFloat(poolShares) > 0)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Bob should remove Ocean liquidity from pool ', async () => {
|
it('Bob should remove Ocean liquidity from pool ', async () => {
|
||||||
const currentDtReserve = await Pool.getOceanReserve(greatPool)
|
const currentDtReserve = await Pool.getOceanReserve(greatPool)
|
||||||
const bobDtBalance = await datatoken.balance(oceanTokenAddress, bob)
|
const bobDtBalance = await datatoken.balance(oceanTokenAddress, bob)
|
||||||
@ -427,6 +428,42 @@ describe('Balancer flow', () => {
|
|||||||
assert(parseFloat(bobDtBalance) < parseFloat(newbobDtBalance))
|
assert(parseFloat(bobDtBalance) < parseFloat(newbobDtBalance))
|
||||||
assert(parseFloat(poolShares) > parseFloat(newpoolShares))
|
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 () => {
|
it('Alice should know how many tokens she will get for removing all liquidity', async () => {
|
||||||
const aliceShares = await Pool.sharesBalance(alice, greatPool)
|
const aliceShares = await Pool.sharesBalance(alice, greatPool)
|
||||||
const amounts = await Pool.getTokensRemovedforPoolShares(greatPool, aliceShares)
|
const amounts = await Pool.getTokensRemovedforPoolShares(greatPool, aliceShares)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user