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

Merge pull request #425 from oceanprotocol/fix/remove-liquidity

fix/remove liquidity
This commit is contained in:
Alex Coseru 2020-10-28 14:14:13 +02:00 committed by GitHub
commit 32c0ef91ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 80 additions and 50 deletions

View File

@ -691,13 +691,15 @@ export class OceanPool extends Pool {
this.logger.error('ERROR: Not enough poolShares') this.logger.error('ERROR: Not enough poolShares')
return null return null
} }
if ( const sharesRequired = await this.getPoolSharesRequiredToRemoveDT(poolAddress, amount)
parseFloat(maximumPoolShares) < if (parseFloat(maximumPoolShares) < parseFloat(sharesRequired)) {
parseFloat(await this.getPoolSharesRequiredToRemoveDT(poolAddress, amount))
) {
this.logger.error('ERROR: Not enough poolShares') this.logger.error('ERROR: Not enough poolShares')
return null return null
} }
// Balancer bug fix
if (parseFloat(maximumPoolShares) < parseFloat(sharesRequired))
maximumPoolShares = String(parseFloat(maximumPoolShares) * 0.9999)
// Balance bug fix
return this.exitswapExternAmountOut( return this.exitswapExternAmountOut(
account, account,
poolAddress, poolAddress,
@ -775,13 +777,18 @@ export class OceanPool extends Pool {
this.logger.error('ERROR: Not enough poolShares') this.logger.error('ERROR: Not enough poolShares')
return null return null
} }
if ( const sharesRequired = await this.getPoolSharesRequiredToRemoveOcean(
parseFloat(maximumPoolShares) < poolAddress,
parseFloat(await this.getPoolSharesRequiredToRemoveOcean(poolAddress, amount)) amount
) { )
if (parseFloat(maximumPoolShares) < parseFloat(sharesRequired)) {
this.logger.error('ERROR: Not enough poolShares') this.logger.error('ERROR: Not enough poolShares')
return null return null
} }
// Balancer bug fix
if (parseFloat(maximumPoolShares) < parseFloat(sharesRequired))
maximumPoolShares = String(parseFloat(maximumPoolShares) * 0.9999)
// Balance bug fix
return super.exitswapExternAmountOut( return super.exitswapExternAmountOut(
account, account,
poolAddress, poolAddress,
@ -812,7 +819,10 @@ export class OceanPool extends Pool {
this.logger.error('ERROR: Not enough poolShares') this.logger.error('ERROR: Not enough poolShares')
return null return null
} }
// Balancer bug fix
if (parseFloat(usershares) === parseFloat(poolShares))
poolShares = String(parseFloat(poolShares) * 0.9999)
// Balance bug fix
return this.exitPool(account, poolAddress, poolShares, [minDT, minOcean]) return this.exitPool(account, poolAddress, poolShares, [minDT, minOcean])
} }

View File

@ -130,18 +130,12 @@ export class Pool extends PoolFactory {
from: account from: account
}) })
let result = null let result = null
const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas let estGas
try { try {
estGas = await token.methods estGas = await token.methods
.approve(spender, amount) .approve(spender, amount)
.estimateGas(function (err, estGas) { .estimateGas((err, estGas) => (err ? gasLimitDefault : estGas))
if (err) {
// console.error('ERROR: OnChainMetadataCacheEstimateGas: ' + err)
return this.GASLIMIT_DEFAULT
}
return estGas
})
} catch (e) { } catch (e) {
estGas = this.GASLIMIT_DEFAULT estGas = this.GASLIMIT_DEFAULT
} }
@ -511,6 +505,7 @@ export class Pool extends PoolFactory {
from: account from: account
}) })
let result = null let result = null
const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas let estGas
try { try {
estGas = await pool.methods estGas = await pool.methods
@ -521,13 +516,7 @@ export class Pool extends PoolFactory {
this.web3.utils.toWei(minAmountOut), this.web3.utils.toWei(minAmountOut),
maxPrice ? this.web3.utils.toWei(maxPrice) : MaxUint256 maxPrice ? this.web3.utils.toWei(maxPrice) : MaxUint256
) )
.estimateGas(function (err, estGas) { .estimateGas((err, estGas) => (err ? gasLimitDefault : estGas))
if (err) {
// console.error('ERROR: OnChainMetadataCacheEstimateGas: ' + err)
return this.GASLIMIT_DEFAULT
}
return estGas
})
} catch (e) { } catch (e) {
estGas = this.GASLIMIT_DEFAULT estGas = this.GASLIMIT_DEFAULT
} }
@ -571,6 +560,7 @@ export class Pool extends PoolFactory {
from: account from: account
}) })
let result = null let result = null
const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas let estGas
try { try {
estGas = await pool.methods estGas = await pool.methods
@ -581,13 +571,7 @@ export class Pool extends PoolFactory {
this.web3.utils.toWei(minAmountOut), this.web3.utils.toWei(minAmountOut),
maxPrice ? this.web3.utils.toWei(maxPrice) : MaxUint256 maxPrice ? this.web3.utils.toWei(maxPrice) : MaxUint256
) )
.estimateGas(function (err, estGas) { .estimateGas((err, estGas) => (err ? gasLimitDefault : estGas))
if (err) {
// console.error('ERROR: OnChainMetadataCacheEstimateGas: ' + err)
return this.GASLIMIT_DEFAULT
}
return estGas
})
} catch (e) { } catch (e) {
estGas = this.GASLIMIT_DEFAULT estGas = this.GASLIMIT_DEFAULT
} }
@ -633,11 +617,19 @@ export class Pool extends PoolFactory {
} }
let result = null let result = null
const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas
try {
estGas = await await pool.methods
.joinPool(this.web3.utils.toWei(poolAmountOut), weiMaxAmountsIn)
.estimateGas((err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = this.GASLIMIT_DEFAULT
}
try { try {
result = await pool.methods result = await pool.methods
.joinPool(this.web3.utils.toWei(poolAmountOut), weiMaxAmountsIn) .joinPool(this.web3.utils.toWei(poolAmountOut), weiMaxAmountsIn)
.send({ from: account, gas: this.GASLIMIT_DEFAULT }) .send({ from: account, gas: estGas + 1 })
} catch (e) { } catch (e) {
this.logger.error(`ERROR: Failed to join pool: ${e.message}`) this.logger.error(`ERROR: Failed to join pool: ${e.message}`)
} }
@ -668,10 +660,19 @@ export class Pool extends PoolFactory {
weiMinAmountsOut.push(this.web3.utils.toWei(amount)) weiMinAmountsOut.push(this.web3.utils.toWei(amount))
} }
let result = null let result = null
const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas
try {
estGas = await await pool.methods
.exitPool(this.web3.utils.toWei(poolAmountIn), weiMinAmountsOut)
.estimateGas((err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = this.GASLIMIT_DEFAULT
}
try { try {
result = await pool.methods result = await pool.methods
.exitPool(this.web3.utils.toWei(poolAmountIn), weiMinAmountsOut) .exitPool(this.web3.utils.toWei(poolAmountIn), weiMinAmountsOut)
.send({ from: account, gas: this.GASLIMIT_DEFAULT }) .send({ from: account, gas: estGas })
} catch (e) { } catch (e) {
this.logger.error(`ERROR: Failed to exit pool: ${e.message}`) this.logger.error(`ERROR: Failed to exit pool: ${e.message}`)
} }
@ -698,6 +699,7 @@ export class Pool extends PoolFactory {
from: account from: account
}) })
let result = null let result = null
const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas let estGas
try { try {
estGas = await await pool.methods estGas = await await pool.methods
@ -706,13 +708,7 @@ export class Pool extends PoolFactory {
this.web3.utils.toWei(tokenAmountIn), this.web3.utils.toWei(tokenAmountIn),
this.web3.utils.toWei(minPoolAmountOut) this.web3.utils.toWei(minPoolAmountOut)
) )
.estimateGas(function (err, estGas) { .estimateGas((err, estGas) => (err ? gasLimitDefault : estGas))
if (err) {
// console.error('ERROR: OnChainMetadataCacheEstimateGas: ' + err)
return this.GASLIMIT_DEFAULT
}
return estGas
})
} catch (e) { } catch (e) {
estGas = this.GASLIMIT_DEFAULT estGas = this.GASLIMIT_DEFAULT
} }
@ -751,6 +747,19 @@ export class Pool extends PoolFactory {
from: account from: account
}) })
let result = null let result = null
const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas
try {
estGas = await await pool.methods
.joinswapPoolAmountOut(
tokenIn,
this.web3.utils.toWei(poolAmountOut),
this.web3.utils.toWei(maxAmountIn)
)
.estimateGas((err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = this.GASLIMIT_DEFAULT
}
try { try {
result = await pool.methods result = await pool.methods
.joinswapPoolAmountOut( .joinswapPoolAmountOut(
@ -758,7 +767,7 @@ export class Pool extends PoolFactory {
this.web3.utils.toWei(poolAmountOut), this.web3.utils.toWei(poolAmountOut),
this.web3.utils.toWei(maxAmountIn) this.web3.utils.toWei(maxAmountIn)
) )
.send({ from: account, gas: this.GASLIMIT_DEFAULT }) .send({ from: account, gas: estGas + 1 })
} catch (e) { } catch (e) {
this.logger.error('ERROR: Failed to join swap pool amount out') this.logger.error('ERROR: Failed to join swap pool amount out')
} }
@ -785,6 +794,19 @@ export class Pool extends PoolFactory {
from: account from: account
}) })
let result = null let result = null
const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas
try {
estGas = await await pool.methods
.exitswapPoolAmountIn(
tokenOut,
this.web3.utils.toWei(poolAmountIn),
this.web3.utils.toWei(minTokenAmountOut)
)
.estimateGas((err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = this.GASLIMIT_DEFAULT
}
try { try {
result = await pool.methods result = await pool.methods
.exitswapPoolAmountIn( .exitswapPoolAmountIn(
@ -815,11 +837,14 @@ export class Pool extends PoolFactory {
tokenAmountOut: string, tokenAmountOut: string,
maxPoolAmountIn: string maxPoolAmountIn: string
): Promise<TransactionReceipt> { ): Promise<TransactionReceipt> {
const gasLimitDefault = this.GASLIMIT_DEFAULT
const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, { const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, {
from: account from: account
}) })
let result = null let result = null
let estGas let estGas
try { try {
estGas = await pool.methods estGas = await pool.methods
.exitswapExternAmountOut( .exitswapExternAmountOut(
@ -827,16 +852,11 @@ export class Pool extends PoolFactory {
this.web3.utils.toWei(tokenAmountOut), this.web3.utils.toWei(tokenAmountOut),
this.web3.utils.toWei(maxPoolAmountIn) this.web3.utils.toWei(maxPoolAmountIn)
) )
.estimateGas(function (err, estGas) { .estimateGas((err, estGas) => (err ? gasLimitDefault : estGas))
if (err) {
// console.error('ERROR: OnChainMetadataCacheEstimateGas: ' + err)
return this.GASLIMIT_DEFAULT
}
return estGas
})
} catch (e) { } catch (e) {
estGas = this.GASLIMIT_DEFAULT estGas = gasLimitDefault
} }
try { try {
result = await pool.methods result = await pool.methods
.exitswapExternAmountOut( .exitswapExternAmountOut(