From e2a4c32a65c47c07412d4faf28b1c63a0e62fa4c Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Tue, 5 Apr 2022 23:37:54 +0200 Subject: [PATCH] use estimateGas() function in FixedRateExchange --- src/pools/fixedRate/FixedRateExchange.ts | 295 ++++++++++------------- 1 file changed, 128 insertions(+), 167 deletions(-) diff --git a/src/pools/fixedRate/FixedRateExchange.ts b/src/pools/fixedRate/FixedRateExchange.ts index 5104789c..04048a00 100644 --- a/src/pools/fixedRate/FixedRateExchange.ts +++ b/src/pools/fixedRate/FixedRateExchange.ts @@ -9,7 +9,8 @@ import { configHelperNetworks, setContractDefaults, amountToUnits, - unitsToAmount + unitsToAmount, + estimateGas } from '../../utils' import { Config } from '../../models/index.js' import { PriceAndFees } from '../..' @@ -131,22 +132,16 @@ export class FixedRateExchange { contractInstance?: Contract ): Promise { const fixedRate = contractInstance || this.fixedRateContract - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await fixedRate.methods - .buyDT( - datatokenAddress, - dtAmount, - maxBaseTokenAmount, - consumeMarketAddress, - consumeMarketFee - ) - .estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + + return estimateGas( + account, + fixedRate.methods.buyDT, + datatokenAddress, + dtAmount, + maxBaseTokenAmount, + consumeMarketAddress, + consumeMarketFee + ) } /** @@ -178,8 +173,9 @@ export class FixedRateExchange { maxBaseTokenAmount ) - const estGas = await this.estBuyDT( + const estGas = await estimateGas( address, + this.contract.methods.buyDT, exchangeId, dtAmountFormatted, maxBtFormatted, @@ -227,22 +223,16 @@ export class FixedRateExchange { contractInstance?: Contract ): Promise { const fixedRate = contractInstance || this.fixedRateContract - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await fixedRate.methods - .sellDT( - datatokenAddress, - dtAmount, - maxBaseTokenAmount, - consumeMarketAddress, - consumeMarketFee - ) - .estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + + return estimateGas( + account, + fixedRate.methods.sellDT, + datatokenAddress, + dtAmount, + maxBaseTokenAmount, + consumeMarketAddress, + consumeMarketFee + ) } /** @@ -273,8 +263,9 @@ export class FixedRateExchange { exchange.baseToken, minBaseTokenAmount ) - const estGas = await this.estBuyDT( + const estGas = await estimateGas( address, + this.contract.methods.sellDT, exchangeId, dtAmountFormatted, minBtFormatted, @@ -328,16 +319,13 @@ export class FixedRateExchange { contractInstance?: Contract ): Promise { const fixedRate = contractInstance || this.fixedRateContract - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await fixedRate.methods - .setRate(exchangeId, await this.web3.utils.toWei(newRate)) - .estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + + return estimateGas( + account, + fixedRate.methods.setRate, + exchangeId, + await this.web3.utils.toWei(newRate) + ) } /** @@ -352,7 +340,12 @@ export class FixedRateExchange { exchangeId: string, newRate: string ): Promise { - const estGas = await this.estSetRate(address, exchangeId, newRate) + const estGas = await estimateGas( + address, + this.contract.methods.setRate, + exchangeId, + this.web3.utils.toWei(newRate) + ) const trxReceipt = await this.contract.methods .setRate(exchangeId, this.web3.utils.toWei(newRate)) .send({ @@ -378,16 +371,8 @@ export class FixedRateExchange { contractInstance?: Contract ): Promise { const fixedRate = contractInstance || this.fixedRateContract - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await fixedRate.methods - .setRate(exchangeId, newAllowedSwapper) - .estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + + return estimateGas(account, fixedRate.methods.setRate, exchangeId, newAllowedSwapper) } /** @@ -402,7 +387,12 @@ export class FixedRateExchange { exchangeId: string, newAllowedSwapper: string ): Promise { - const estGas = await this.estSetAllowedSwapper(address, exchangeId, newAllowedSwapper) + const estGas = await estimateGas( + address, + this.contract.methods.setAllowedSwapper, + exchangeId, + newAllowedSwapper + ) const trxReceipt = await this.contract.methods .setAllowedSwapper(exchangeId, newAllowedSwapper) .send({ @@ -426,16 +416,8 @@ export class FixedRateExchange { contractInstance?: Contract ): Promise { const fixedRate = contractInstance || this.fixedRateContract - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await fixedRate.methods - .toggleExchangeState(exchangeId) - .estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + + return estimateGas(account, fixedRate.methods.toggleExchangeState, exchangeId) } /** @@ -452,7 +434,11 @@ export class FixedRateExchange { if (!exchange) return null if (exchange.active === true) return null - const estGas = await this.estActivate(address, exchangeId) + const estGas = await estimateGas( + address, + this.contract.methods.toggleExchangeState, + exchangeId + ) const trxReceipt = await this.contract.methods.toggleExchangeState(exchangeId).send({ from: address, gas: estGas + 1, @@ -474,16 +460,8 @@ export class FixedRateExchange { contractInstance?: Contract ): Promise { const fixedRate = contractInstance || this.fixedRateContract - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await fixedRate.methods - .toggleExchangeState(exchangeId) - .estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + + return estimateGas(account, fixedRate.methods.toggleExchangeState, exchangeId) } /** @@ -500,7 +478,11 @@ export class FixedRateExchange { if (!exchange) return null if (exchange.active === false) return null - const estGas = await this.estDeactivate(address, exchangeId) + const estGas = await estimateGas( + address, + this.contract.methods.toggleExchangeState, + exchangeId + ) const trxReceipt = await this.contract.methods.toggleExchangeState(exchangeId).send({ from: address, @@ -711,16 +693,8 @@ export class FixedRateExchange { contractInstance?: Contract ): Promise { const fixedRate = contractInstance || this.fixedRateContract - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await fixedRate.methods - .toggleMintState(exchangeId, true) - .estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + + return estimateGas(account, fixedRate.methods.toggleMintState, exchangeId, true) } /** @@ -737,7 +711,12 @@ export class FixedRateExchange { if (!exchange) return null if (exchange.withMint === true) return null - const estGas = await this.estActivateMint(address, exchangeId) + const estGas = await estimateGas( + address, + this.contract.methods.toggleMintState, + exchangeId, + true + ) const trxReceipt = await this.contract.methods .toggleMintState(exchangeId, true) .send({ @@ -761,16 +740,8 @@ export class FixedRateExchange { contractInstance?: Contract ): Promise { const fixedRate = contractInstance || this.fixedRateContract - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await fixedRate.methods - .toggleMintState(exchangeId) - .estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + + return estimateGas(account, fixedRate.methods.toggleMintState, exchangeId) } /** @@ -787,7 +758,12 @@ export class FixedRateExchange { if (!exchange) return null if (exchange.withMint === false) return null - const estGas = await this.estDeactivate(address, exchangeId) + const estGas = await estimateGas( + address, + this.contract.methods.toggleMintState, + exchangeId, + false + ) const trxReceipt = await this.contract.methods .toggleMintState(exchangeId, false) @@ -815,20 +791,11 @@ export class FixedRateExchange { contractInstance?: Contract ): Promise { const fixedRate = contractInstance || this.fixedRateContract - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas const fixedrate: FixedPriceExchange = await this.contract.methods .getExchange(exchangeId) .call() const amountWei = await this.amountToUnits(fixedrate.baseToken, amount) - try { - estGas = await fixedRate.methods - .collectBT(exchangeId, amountWei) - .estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + return estimateGas(account, fixedRate.methods.collectBT, exchangeId, amountWei) } /** @@ -846,11 +813,18 @@ export class FixedRateExchange { const exchange = await this.getExchange(exchangeId) if (!exchange) return null - const estGas = await this.estCollectBT(address, exchangeId, amount) const fixedrate: FixedPriceExchange = await this.contract.methods .getExchange(exchangeId) .call() const amountWei = await this.amountToUnits(fixedrate.baseToken, amount) + + const estGas = await estimateGas( + address, + this.contract.methods.collectBT, + exchangeId, + amountWei + ) + const trxReceipt = await this.contract.methods.collectBT(exchangeId, amountWei).send({ from: address, gas: estGas + 1, @@ -874,20 +848,12 @@ export class FixedRateExchange { contractInstance?: Contract ): Promise { const fixedRate = contractInstance || this.fixedRateContract - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas const fixedrate: FixedPriceExchange = await this.contract.methods .getExchange(exchangeId) .call() + const amountWei = await this.amountToUnits(fixedrate.datatoken, amount) - try { - estGas = await fixedRate.methods - .collectDT(exchangeId, amountWei) - .estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + return estimateGas(account, fixedRate.methods.collectDT, exchangeId, amountWei) } /** @@ -905,11 +871,18 @@ export class FixedRateExchange { const exchange = await this.getExchange(exchangeId) if (!exchange) return null - const estGas = await this.estCollectDT(address, exchangeId, amount) const fixedrate: FixedPriceExchange = await this.contract.methods .getExchange(exchangeId) .call() const amountWei = await this.amountToUnits(fixedrate.datatoken, amount) + + const estGas = await estimateGas( + address, + this.contract.methods.collectDT, + exchangeId, + amountWei + ) + const trxReceipt = await this.contract.methods.collectDT(exchangeId, amountWei).send({ from: address, gas: estGas + 1, @@ -931,16 +904,8 @@ export class FixedRateExchange { contractInstance?: Contract ): Promise { const fixedRate = contractInstance || this.fixedRateContract - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await fixedRate.methods - .collectMarketFee(exchangeId) - .estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + + return estimateGas(account, fixedRate.methods.collectMarketFee, exchangeId) } /** @@ -956,7 +921,11 @@ export class FixedRateExchange { const exchange = await this.getExchange(exchangeId) if (!exchange) return null - const estGas = await this.estCollectMarketFee(address, exchangeId) + const estGas = await estimateGas( + address, + this.contract.methods.collectMarketFee, + exchangeId + ) const trxReceipt = await this.contract.methods.collectMarketFee(exchangeId).send({ from: address, gas: estGas + 1, @@ -978,16 +947,8 @@ export class FixedRateExchange { contractInstance?: Contract ): Promise { const fixedRate = contractInstance || this.fixedRateContract - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await fixedRate.methods - .collectMarketFee(exchangeId) - .estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + + return estimateGas(account, fixedRate.methods.collectMarketFee, exchangeId) } /** @@ -1003,7 +964,11 @@ export class FixedRateExchange { const exchange = await this.getExchange(exchangeId) if (!exchange) return null - const estGas = await this.estCollectOceanFee(address, exchangeId) + const estGas = await estimateGas( + address, + this.contract.methods.collectOceanFee, + exchangeId + ) const trxReceipt = await this.contract.methods.collectOceanFee(exchangeId).send({ from: address, gas: estGas + 1, @@ -1070,16 +1035,13 @@ export class FixedRateExchange { contractInstance?: Contract ): Promise { const fixedRate = contractInstance || this.fixedRateContract - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await fixedRate.methods - .updateMarketFee(exchangeId, newMarketFee) - .estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + + return estimateGas( + account, + fixedRate.methods.updateMarketFee, + exchangeId, + newMarketFee + ) } /** @@ -1094,8 +1056,9 @@ export class FixedRateExchange { exchangeId: string, newMarketFee: string ): Promise { - const estGas = await this.estSetRate( + const estGas = await estimateGas( address, + this.contract.methods.updateMarketFee, exchangeId, this.web3.utils.toWei(newMarketFee) ) @@ -1124,16 +1087,13 @@ export class FixedRateExchange { contractInstance?: Contract ): Promise { const fixedRate = contractInstance || this.fixedRateContract - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await fixedRate.methods - .updateMarketFeeCollector(exchangeId, newMarketFeeCollector) - .estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + + return estimateGas( + account, + fixedRate.methods.updateMarketFeeCollector, + exchangeId, + newMarketFeeCollector + ) } /** @@ -1148,8 +1108,9 @@ export class FixedRateExchange { exchangeId: string, newMarketFeeCollector: string ): Promise { - const estGas = await this.estUpdateMarketFeeCollector( + const estGas = await estimateGas( address, + this.contract.methods.updateMarketFeeCollector, exchangeId, newMarketFeeCollector )