Skip to content

Commit

Permalink
Merge pull request #1394 from oceanprotocol/issue-1391-generic-estima…
Browse files Browse the repository at this point in the history
…tegas-function

Issue-#1391: Add generic estimateGas() function
  • Loading branch information
Miquel A. Cabot authored May 16, 2022
2 parents cac327c + 6f74add commit 6016c66
Show file tree
Hide file tree
Showing 10 changed files with 802 additions and 1,241 deletions.
297 changes: 138 additions & 159 deletions src/factories/NFTFactory.ts

Large diffs are not rendered by default.

225 changes: 77 additions & 148 deletions src/pools/Router.ts

Large diffs are not rendered by default.

225 changes: 94 additions & 131 deletions src/pools/balancer/Pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
unitsToAmount,
amountToUnits,
LoggerInstance,
estimateGas,
ConfigHelper
} from '../../utils'
import BigNumber from 'bignumber.js'
Expand Down Expand Up @@ -37,7 +38,6 @@ const MaxUint256 =
export class Pool {
public poolAbi: AbiItem | AbiItem[]
public web3: Web3
public GASLIMIT_DEFAULT = 1000000
private config: Config

constructor(
Expand Down Expand Up @@ -112,16 +112,7 @@ export class Pool {
this.config
)

const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas
try {
estGas = await poolContract.methods
.setSwapFee(fee)
.estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
return estGas
return estimateGas(account, poolContract.methods.setSwapFee, fee)
}

/**
Expand All @@ -142,7 +133,7 @@ export class Pool {
this.config
)
let result = null
const estGas = await this.estSetSwapFee(account, poolAddress, fee)
const estGas = await estimateGas(account, pool.methods.setSwapFee, fee)

try {
result = await pool.methods.setSwapFee(this.web3.utils.toWei(fee)).send({
Expand Down Expand Up @@ -631,16 +622,7 @@ export class Pool {
this.config
)

const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas
try {
estGas = await poolContract.methods
.collectOPC()
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
return estGas
return estimateGas(address, poolContract.methods.collectOPC)
}

/**
Expand All @@ -655,7 +637,7 @@ export class Pool {
this.config
)
let result = null
const estGas = await this.estCollectOPC(address, poolAddress)
const estGas = await estimateGas(address, pool.methods.collectOPC)

try {
result = await pool.methods.collectOPC().send({
Expand Down Expand Up @@ -689,16 +671,7 @@ export class Pool {
this.config
)

const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas
try {
estGas = await poolContract.methods
.collectMarketFee()
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
return estGas
return estimateGas(address, poolContract.methods.collectMarketFee)
}

/**
Expand All @@ -720,7 +693,7 @@ export class Pool {
this.config
)
let result = null
const estGas = await this.estCollectMarketFee(address, poolAddress)
const estGas = await estimateGas(address, pool.methods.collectMarketFee)

try {
result = await pool.methods.collectMarketFee().send({
Expand Down Expand Up @@ -757,16 +730,12 @@ export class Pool {
this.config
)

const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas
try {
estGas = await poolContract.methods
.updatePublishMarketFee(newPublishMarketAddress, newPublishMarketSwapFee)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
return estGas
return estimateGas(
address,
poolContract.methods.updatePublishMarketFee,
newPublishMarketAddress,
this.web3.utils.toWei(newPublishMarketSwapFee)
)
}

/**
Expand All @@ -792,9 +761,9 @@ export class Pool {
)
let result = null

const estGas = await this.estUpdatePublishMarketFee(
const estGas = await estimateGas(
address,
poolAddress,
pool.methods.updatePublishMarketFee,
newPublishMarketAddress,
this.web3.utils.toWei(newPublishMarketSwapFee)
)
Expand Down Expand Up @@ -857,28 +826,21 @@ export class Pool {
)
: MaxUint256

const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas
try {
estGas = await poolContract.methods
.swapExactAmountIn(
[
tokenInOutMarket.tokenIn,
tokenInOutMarket.tokenOut,
tokenInOutMarket.marketFeeAddress
],
[
tokenAmountIn,
minAmountOut,
maxPrice,
this.web3.utils.toWei(amountsInOutMaxFee.swapMarketFee)
]
)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
return estGas
return estimateGas(
address,
poolContract.methods.swapExactAmountIn,
[
tokenInOutMarket.tokenIn,
tokenInOutMarket.tokenOut,
tokenInOutMarket.marketFeeAddress
],
[
tokenAmountIn,
minAmountOut,
maxPrice,
this.web3.utils.toWei(amountsInOutMaxFee.swapMarketFee)
]
)
}

/**
Expand Down Expand Up @@ -910,13 +872,6 @@ export class Pool {
throw new Error(`tokenAmountIn is greater than ${maxSwap.toString()}`)
}

const estGas = await this.estSwapExactAmountIn(
address,
poolAddress,
tokenInOutMarket,
amountsInOutMaxFee
)

const tokenAmountIn = await this.amountToUnits(
tokenInOutMarket.tokenIn,
amountsInOutMaxFee.tokenAmountIn,
Expand All @@ -929,15 +884,30 @@ export class Pool {
tokenInOutMarket.tokenOutDecimals
)

let result = null

const maxPrice = amountsInOutMaxFee.maxPrice
? await this.amountToUnits(
await this.getBaseToken(poolAddress),
amountsInOutMaxFee.maxPrice
)
: 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 {
result = await pool.methods
.swapExactAmountIn(
Expand Down Expand Up @@ -988,8 +958,6 @@ export class Pool {
this.config
)

const gasLimitDefault = this.GASLIMIT_DEFAULT

const maxAmountIn = await this.amountToUnits(
tokenInOutMarket.tokenIn,
amountsInOutMaxFee.maxAmountIn,
Expand All @@ -1009,27 +977,21 @@ export class Pool {
)
: MaxUint256

let estGas
try {
estGas = await poolContract.methods
.swapExactAmountOut(
[
tokenInOutMarket.tokenIn,
tokenInOutMarket.tokenOut,
tokenInOutMarket.marketFeeAddress
],
[
maxAmountIn,
tokenAmountOut,
maxPrice,
this.web3.utils.toWei(amountsInOutMaxFee.swapMarketFee)
]
)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
return estGas
return estimateGas(
address,
poolContract.methods.swapExactAmountOut,
[
tokenInOutMarket.tokenIn,
tokenInOutMarket.tokenOut,
tokenInOutMarket.marketFeeAddress
],
[
maxAmountIn,
tokenAmountOut,
maxPrice,
this.web3.utils.toWei(amountsInOutMaxFee.swapMarketFee)
]
)
}

/**
Expand Down Expand Up @@ -1057,13 +1019,6 @@ export class Pool {
throw new Error(`tokenAmountOut is greater than ${maxSwap.toString()}`)
}

const estGas = await this.estSwapExactAmountOut(
account,
poolAddress,
tokenInOutMarket,
amountsInOutMaxFee
)

const maxAmountIn = await this.amountToUnits(
tokenInOutMarket.tokenIn,
amountsInOutMaxFee.maxAmountIn,
Expand All @@ -1083,6 +1038,22 @@ export class Pool {
)
: 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 {
result = await pool.methods
.swapExactAmountOut(
Expand Down Expand Up @@ -1133,16 +1104,12 @@ export class Pool {
this.config
)

const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas
try {
estGas = await poolContract.methods
.joinswapExternAmountIn(tokenAmountIn, minPoolAmountOut)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
return estGas
return estimateGas(
address,
poolContract.methods.joinswapExternAmountIn,
tokenAmountIn,
minPoolAmountOut
)
}

/**
Expand Down Expand Up @@ -1179,9 +1146,9 @@ export class Pool {
tokenAmountIn,
tokenInDecimals
)
const estGas = await this.estJoinswapExternAmountIn(
const estGas = await estimateGas(
account,
poolAddress,
pool.methods.joinswapExternAmountIn,
amountInFormatted,
this.web3.utils.toWei(minPoolAmountOut)
)
Expand Down Expand Up @@ -1227,16 +1194,12 @@ export class Pool {
this.config
)

const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas
try {
estGas = await poolContract.methods
.exitswapPoolAmountIn(poolAmountIn, minTokenAmountOut)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
return estGas
return estimateGas(
address,
poolContract.methods.exitswapPoolAmountIn,
poolAmountIn,
minTokenAmountOut
)
}

/**
Expand Down Expand Up @@ -1280,9 +1243,9 @@ export class Pool {
minTokenAmountOut,
poolDecimals
)
const estGas = await this.estExitswapPoolAmountIn(
const estGas = await estimateGas(
account,
poolAddress,
pool.methods.exitswapPoolAmountIn,
this.web3.utils.toWei(poolAmountIn),
minTokenOutFormatted
)
Expand Down
Loading

0 comments on commit 6016c66

Please sign in to comment.