Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix/remove liquidity #425

Merged
merged 3 commits into from
Oct 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions src/balancer/OceanPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -691,13 +691,15 @@ export class OceanPool extends Pool {
this.logger.error('ERROR: Not enough poolShares')
return null
}
if (
parseFloat(maximumPoolShares) <
parseFloat(await this.getPoolSharesRequiredToRemoveDT(poolAddress, amount))
) {
const sharesRequired = await this.getPoolSharesRequiredToRemoveDT(poolAddress, amount)
if (parseFloat(maximumPoolShares) < parseFloat(sharesRequired)) {
this.logger.error('ERROR: Not enough poolShares')
return null
}
// Balancer bug fix
if (parseFloat(maximumPoolShares) < parseFloat(sharesRequired))
maximumPoolShares = String(parseFloat(maximumPoolShares) * 0.9999)
// Balance bug fix
return this.exitswapExternAmountOut(
account,
poolAddress,
Expand Down Expand Up @@ -775,13 +777,18 @@ export class OceanPool extends Pool {
this.logger.error('ERROR: Not enough poolShares')
return null
}
if (
parseFloat(maximumPoolShares) <
parseFloat(await this.getPoolSharesRequiredToRemoveOcean(poolAddress, amount))
) {
const sharesRequired = await this.getPoolSharesRequiredToRemoveOcean(
poolAddress,
amount
)
if (parseFloat(maximumPoolShares) < parseFloat(sharesRequired)) {
this.logger.error('ERROR: Not enough poolShares')
return null
}
// Balancer bug fix
if (parseFloat(maximumPoolShares) < parseFloat(sharesRequired))
maximumPoolShares = String(parseFloat(maximumPoolShares) * 0.9999)
// Balance bug fix
return super.exitswapExternAmountOut(
account,
poolAddress,
Expand Down Expand Up @@ -812,7 +819,10 @@ export class OceanPool extends Pool {
this.logger.error('ERROR: Not enough poolShares')
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])
}

Expand Down
102 changes: 61 additions & 41 deletions src/balancer/Pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,12 @@ export class Pool extends PoolFactory {
from: account
})
let result = null

const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas
try {
estGas = await token.methods
.approve(spender, amount)
.estimateGas(function (err, estGas) {
if (err) {
// console.error('ERROR: OnChainMetadataCacheEstimateGas: ' + err)
return this.GASLIMIT_DEFAULT
}
return estGas
})
.estimateGas((err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = this.GASLIMIT_DEFAULT
}
Expand Down Expand Up @@ -511,6 +505,7 @@ export class Pool extends PoolFactory {
from: account
})
let result = null
const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas
try {
estGas = await pool.methods
Expand All @@ -521,13 +516,7 @@ export class Pool extends PoolFactory {
this.web3.utils.toWei(minAmountOut),
maxPrice ? this.web3.utils.toWei(maxPrice) : MaxUint256
)
.estimateGas(function (err, estGas) {
if (err) {
// console.error('ERROR: OnChainMetadataCacheEstimateGas: ' + err)
return this.GASLIMIT_DEFAULT
}
return estGas
})
.estimateGas((err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = this.GASLIMIT_DEFAULT
}
Expand Down Expand Up @@ -571,6 +560,7 @@ export class Pool extends PoolFactory {
from: account
})
let result = null
const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas
try {
estGas = await pool.methods
Expand All @@ -581,13 +571,7 @@ export class Pool extends PoolFactory {
this.web3.utils.toWei(minAmountOut),
maxPrice ? this.web3.utils.toWei(maxPrice) : MaxUint256
)
.estimateGas(function (err, estGas) {
if (err) {
// console.error('ERROR: OnChainMetadataCacheEstimateGas: ' + err)
return this.GASLIMIT_DEFAULT
}
return estGas
})
.estimateGas((err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = this.GASLIMIT_DEFAULT
}
Expand Down Expand Up @@ -633,11 +617,19 @@ export class Pool extends PoolFactory {
}

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 {
result = await pool.methods
.joinPool(this.web3.utils.toWei(poolAmountOut), weiMaxAmountsIn)
.send({ from: account, gas: this.GASLIMIT_DEFAULT })
.send({ from: account, gas: estGas + 1 })
} catch (e) {
this.logger.error(`ERROR: Failed to join pool: ${e.message}`)
}
Expand Down Expand Up @@ -668,10 +660,19 @@ export class Pool extends PoolFactory {
weiMinAmountsOut.push(this.web3.utils.toWei(amount))
}
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 {
result = await pool.methods
.exitPool(this.web3.utils.toWei(poolAmountIn), weiMinAmountsOut)
.send({ from: account, gas: this.GASLIMIT_DEFAULT })
.send({ from: account, gas: estGas })
} catch (e) {
this.logger.error(`ERROR: Failed to exit pool: ${e.message}`)
}
Expand All @@ -698,6 +699,7 @@ export class Pool extends PoolFactory {
from: account
})
let result = null
const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas
try {
estGas = await await pool.methods
Expand All @@ -706,13 +708,7 @@ export class Pool extends PoolFactory {
this.web3.utils.toWei(tokenAmountIn),
this.web3.utils.toWei(minPoolAmountOut)
)
.estimateGas(function (err, estGas) {
if (err) {
// console.error('ERROR: OnChainMetadataCacheEstimateGas: ' + err)
return this.GASLIMIT_DEFAULT
}
return estGas
})
.estimateGas((err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = this.GASLIMIT_DEFAULT
}
Expand Down Expand Up @@ -751,14 +747,27 @@ export class Pool extends PoolFactory {
from: account
})
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 {
result = await pool.methods
.joinswapPoolAmountOut(
tokenIn,
this.web3.utils.toWei(poolAmountOut),
this.web3.utils.toWei(maxAmountIn)
)
.send({ from: account, gas: this.GASLIMIT_DEFAULT })
.send({ from: account, gas: estGas + 1 })
} catch (e) {
this.logger.error('ERROR: Failed to join swap pool amount out')
}
Expand All @@ -785,6 +794,19 @@ export class Pool extends PoolFactory {
from: account
})
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 {
result = await pool.methods
.exitswapPoolAmountIn(
Expand Down Expand Up @@ -815,28 +837,26 @@ export class Pool extends PoolFactory {
tokenAmountOut: string,
maxPoolAmountIn: string
): Promise<TransactionReceipt> {
const gasLimitDefault = this.GASLIMIT_DEFAULT

const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, {
from: account
})
let result = null
let estGas

try {
estGas = await pool.methods
.exitswapExternAmountOut(
tokenOut,
this.web3.utils.toWei(tokenAmountOut),
this.web3.utils.toWei(maxPoolAmountIn)
)
.estimateGas(function (err, estGas) {
if (err) {
// console.error('ERROR: OnChainMetadataCacheEstimateGas: ' + err)
return this.GASLIMIT_DEFAULT
}
return estGas
})
.estimateGas((err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = this.GASLIMIT_DEFAULT
estGas = gasLimitDefault
}

try {
result = await pool.methods
.exitswapExternAmountOut(
Expand Down