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

use estimateGas() function in Pool

This commit is contained in:
Miquel A. Cabot 2022-04-05 18:08:21 +02:00
parent 09d7683986
commit 47e47863b3

View File

@ -8,7 +8,8 @@ import {
setContractDefaults, setContractDefaults,
unitsToAmount, unitsToAmount,
amountToUnits, amountToUnits,
LoggerInstance LoggerInstance,
estimateGas
} from '../../utils' } from '../../utils'
import BigNumber from 'bignumber.js' import BigNumber from 'bignumber.js'
import PoolTemplate from '@oceanprotocol/contracts/artifacts/contracts/pools/balancer/BPool.sol/BPool.json' import PoolTemplate from '@oceanprotocol/contracts/artifacts/contracts/pools/balancer/BPool.sol/BPool.json'
@ -91,16 +92,7 @@ export class Pool {
this.config this.config
) )
const gasLimitDefault = this.GASLIMIT_DEFAULT return estimateGas(account, poolContract.methods.setSwapFee, fee)
let estGas
try {
estGas = await poolContract.methods
.setSwapFee(fee)
.estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
return estGas
} }
/** /**
@ -121,7 +113,7 @@ export class Pool {
this.config this.config
) )
let result = null let result = null
const estGas = await this.estSetSwapFee(account, poolAddress, fee) const estGas = await estimateGas(account, pool.methods.setSwapFee, fee)
try { try {
result = await pool.methods.setSwapFee(this.web3.utils.toWei(fee)).send({ result = await pool.methods.setSwapFee(this.web3.utils.toWei(fee)).send({
@ -595,16 +587,7 @@ export class Pool {
this.config this.config
) )
const gasLimitDefault = this.GASLIMIT_DEFAULT return estimateGas(address, poolContract.methods.collectOPC)
let estGas
try {
estGas = await poolContract.methods
.collectOPC()
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
return estGas
} }
/** /**
@ -619,7 +602,7 @@ export class Pool {
this.config this.config
) )
let result = null let result = null
const estGas = await this.estCollectOPC(address, poolAddress) const estGas = await estimateGas(address, pool.methods.collectOPC)
try { try {
result = await pool.methods.collectOPC().send({ result = await pool.methods.collectOPC().send({
@ -653,16 +636,7 @@ export class Pool {
this.config this.config
) )
const gasLimitDefault = this.GASLIMIT_DEFAULT return estimateGas(address, poolContract.methods.collectMarketFee)
let estGas
try {
estGas = await poolContract.methods
.collectMarketFee()
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
return estGas
} }
/** /**
@ -684,7 +658,7 @@ export class Pool {
this.config this.config
) )
let result = null let result = null
const estGas = await this.estCollectMarketFee(address, poolAddress) const estGas = await estimateGas(address, pool.methods.collectMarketFee)
try { try {
result = await pool.methods.collectMarketFee().send({ result = await pool.methods.collectMarketFee().send({
@ -721,16 +695,12 @@ export class Pool {
this.config this.config
) )
const gasLimitDefault = this.GASLIMIT_DEFAULT return estimateGas(
let estGas address,
try { poolContract.methods.updatePublishMarketFee,
estGas = await poolContract.methods newPublishMarketAddress,
.updatePublishMarketFee(newPublishMarketAddress, newPublishMarketSwapFee) this.web3.utils.toWei(newPublishMarketSwapFee)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) )
} catch (e) {
estGas = gasLimitDefault
}
return estGas
} }
/** /**
@ -756,9 +726,9 @@ export class Pool {
) )
let result = null let result = null
const estGas = await this.estUpdatePublishMarketFee( const estGas = await estimateGas(
address, address,
poolAddress, pool.methods.updatePublishMarketFee,
newPublishMarketAddress, newPublishMarketAddress,
this.web3.utils.toWei(newPublishMarketSwapFee) this.web3.utils.toWei(newPublishMarketSwapFee)
) )
@ -822,28 +792,21 @@ export class Pool {
) )
: MaxUint256 : MaxUint256
const gasLimitDefault = this.GASLIMIT_DEFAULT return estimateGas(
let estGas address,
try { poolContract.methods.swapExactAmountIn,
estGas = await poolContract.methods [
.swapExactAmountIn( tokenInOutMarket.tokenIn,
[ tokenInOutMarket.tokenOut,
tokenInOutMarket.tokenIn, tokenInOutMarket.marketFeeAddress
tokenInOutMarket.tokenOut, ],
tokenInOutMarket.marketFeeAddress [
], tokenAmountIn,
[ minAmountOut,
tokenAmountIn, maxPrice,
minAmountOut, this.web3.utils.toWei(amountsInOutMaxFee.swapMarketFee)
maxPrice, ]
this.web3.utils.toWei(amountsInOutMaxFee.swapMarketFee) )
]
)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
return estGas
} }
/** /**
@ -875,13 +838,6 @@ export class Pool {
throw new Error(`tokenAmountIn is greater than ${maxSwap.toString()}`) throw new Error(`tokenAmountIn is greater than ${maxSwap.toString()}`)
} }
const estGas = await this.estSwapExactAmountIn(
address,
poolAddress,
tokenInOutMarket,
amountsInOutMaxFee
)
const tokenAmountIn = await amountToUnits( const tokenAmountIn = await amountToUnits(
this.web3, this.web3,
tokenInOutMarket.tokenIn, tokenInOutMarket.tokenIn,
@ -894,8 +850,6 @@ export class Pool {
amountsInOutMaxFee.minAmountOut amountsInOutMaxFee.minAmountOut
) )
let result = null
const maxPrice = amountsInOutMaxFee.maxPrice const maxPrice = amountsInOutMaxFee.maxPrice
? await amountToUnits( ? await amountToUnits(
this.web3, this.web3,
@ -904,6 +858,23 @@ export class Pool {
) )
: MaxUint256 : MaxUint256
const estGas = await estimateGas(
address,
pool.methods.swapExactAmountIn,
[
tokenInOutMarket.tokenIn,
tokenInOutMarket.tokenOut,
tokenInOutMarket.marketFeeAddress
],
[
tokenAmountIn,
minAmountOut,
maxPrice,
this.web3.utils.toWei(amountsInOutMaxFee.swapMarketFee)
]
)
let result = null
try { try {
result = await pool.methods result = await pool.methods
.swapExactAmountIn( .swapExactAmountIn(
@ -954,8 +925,6 @@ export class Pool {
this.config this.config
) )
const gasLimitDefault = this.GASLIMIT_DEFAULT
const maxAmountIn = await amountToUnits( const maxAmountIn = await amountToUnits(
this.web3, this.web3,
tokenInOutMarket.tokenIn, tokenInOutMarket.tokenIn,
@ -976,27 +945,21 @@ export class Pool {
) )
: MaxUint256 : MaxUint256
let estGas return estimateGas(
try { address,
estGas = await poolContract.methods poolContract.methods.swapExactAmountOut,
.swapExactAmountOut( [
[ tokenInOutMarket.tokenIn,
tokenInOutMarket.tokenIn, tokenInOutMarket.tokenOut,
tokenInOutMarket.tokenOut, tokenInOutMarket.marketFeeAddress
tokenInOutMarket.marketFeeAddress ],
], [
[ maxAmountIn,
maxAmountIn, tokenAmountOut,
tokenAmountOut, maxPrice,
maxPrice, this.web3.utils.toWei(amountsInOutMaxFee.swapMarketFee)
this.web3.utils.toWei(amountsInOutMaxFee.swapMarketFee) ]
] )
)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
return estGas
} }
/** /**
@ -1024,13 +987,6 @@ export class Pool {
throw new Error(`tokenAmountOut is greater than ${maxSwap.toString()}`) throw new Error(`tokenAmountOut is greater than ${maxSwap.toString()}`)
} }
const estGas = await this.estSwapExactAmountOut(
account,
poolAddress,
tokenInOutMarket,
amountsInOutMaxFee
)
const maxAmountIn = await amountToUnits( const maxAmountIn = await amountToUnits(
this.web3, this.web3,
tokenInOutMarket.tokenIn, tokenInOutMarket.tokenIn,
@ -1051,6 +1007,22 @@ export class Pool {
) )
: MaxUint256 : MaxUint256
const estGas = await estimateGas(
account,
pool.methods.swapExactAmountOut,
[
tokenInOutMarket.tokenIn,
tokenInOutMarket.tokenOut,
tokenInOutMarket.marketFeeAddress
],
[
maxAmountIn,
tokenAmountOut,
maxPrice,
this.web3.utils.toWei(amountsInOutMaxFee.swapMarketFee)
]
)
try { try {
result = await pool.methods result = await pool.methods
.swapExactAmountOut( .swapExactAmountOut(
@ -1100,16 +1072,12 @@ export class Pool {
this.config this.config
) )
const gasLimitDefault = this.GASLIMIT_DEFAULT return estimateGas(
let estGas address,
try { poolContract.methods.joinPool,
estGas = await poolContract.methods poolAmountOut,
.joinPool(poolAmountOut, maxAmountsIn) maxAmountsIn
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) )
} catch (e) {
estGas = gasLimitDefault
}
return estGas
} }
/** /**
@ -1143,9 +1111,9 @@ export class Pool {
let result = null let result = null
const estGas = await this.estJoinPool( const estGas = await estimateGas(
address, address,
poolAddress, pool.methods.joinPool,
this.web3.utils.toWei(poolAmountOut), this.web3.utils.toWei(poolAmountOut),
weiMaxAmountsIn weiMaxAmountsIn
) )
@ -1187,16 +1155,12 @@ export class Pool {
this.config this.config
) )
const gasLimitDefault = this.GASLIMIT_DEFAULT return estimateGas(
let estGas address,
try { poolContract.methods.exitPool,
estGas = await poolContract.methods poolAmountIn,
.exitPool(poolAmountIn, minAmountsOut) minAmountsOut
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) )
} catch (e) {
estGas = gasLimitDefault
}
return estGas
} }
/** /**
@ -1226,14 +1190,15 @@ export class Pool {
const amount = await amountToUnits(this.web3, tokens[i], minAmountsOut[i]) const amount = await amountToUnits(this.web3, tokens[i], minAmountsOut[i])
weiMinAmountsOut.push(amount) weiMinAmountsOut.push(amount)
} }
let result = null
const estGas = await this.estExitPool( const estGas = await estimateGas(
account, account,
poolAddress, pool.methods.exitPool,
this.web3.utils.toWei(poolAmountIn), this.web3.utils.toWei(poolAmountIn),
weiMinAmountsOut weiMinAmountsOut
) )
let result = null
try { try {
result = await pool.methods result = await pool.methods
.exitPool(this.web3.utils.toWei(poolAmountIn), weiMinAmountsOut) .exitPool(this.web3.utils.toWei(poolAmountIn), weiMinAmountsOut)
@ -1272,16 +1237,12 @@ export class Pool {
this.config this.config
) )
const gasLimitDefault = this.GASLIMIT_DEFAULT return estimateGas(
let estGas address,
try { poolContract.methods.joinswapExternAmountIn,
estGas = await poolContract.methods tokenAmountIn,
.joinswapExternAmountIn(tokenAmountIn, minPoolAmountOut) minPoolAmountOut
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) )
} catch (e) {
estGas = gasLimitDefault
}
return estGas
} }
/** /**
@ -1312,9 +1273,9 @@ export class Pool {
} }
const amountInFormatted = await amountToUnits(this.web3, tokenIn, tokenAmountIn) const amountInFormatted = await amountToUnits(this.web3, tokenIn, tokenAmountIn)
const estGas = await this.estJoinswapExternAmountIn( const estGas = await estimateGas(
account, account,
poolAddress, pool.methods.joinswapExternAmountIn,
amountInFormatted, amountInFormatted,
this.web3.utils.toWei(minPoolAmountOut) this.web3.utils.toWei(minPoolAmountOut)
) )
@ -1360,16 +1321,12 @@ export class Pool {
this.config this.config
) )
const gasLimitDefault = this.GASLIMIT_DEFAULT return estimateGas(
let estGas address,
try { poolContract.methods.exitswapPoolAmountIn,
estGas = await poolContract.methods poolAmountIn,
.exitswapPoolAmountIn(poolAmountIn, minTokenAmountOut) minTokenAmountOut
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) )
} catch (e) {
estGas = gasLimitDefault
}
return estGas
} }
/** /**
@ -1411,9 +1368,9 @@ export class Pool {
await this.getBaseToken(poolAddress), await this.getBaseToken(poolAddress),
minTokenAmountOut minTokenAmountOut
) )
const estGas = await this.estExitswapPoolAmountIn( const estGas = await estimateGas(
account, account,
poolAddress, pool.methods.exitswapPoolAmountIn,
this.web3.utils.toWei(poolAmountIn), this.web3.utils.toWei(poolAmountIn),
minTokenOutFormatted minTokenOutFormatted
) )