From ead3d212a2eabe0882f5581f6ca37bd2453399e3 Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Fri, 30 Oct 2020 06:02:06 -0700 Subject: [PATCH] optimise gas costs --- src/exchange/FixedRateExchange.ts | 83 +++++++++++-------------------- 1 file changed, 29 insertions(+), 54 deletions(-) diff --git a/src/exchange/FixedRateExchange.ts b/src/exchange/FixedRateExchange.ts index df2c5f96..af7c614f 100644 --- a/src/exchange/FixedRateExchange.ts +++ b/src/exchange/FixedRateExchange.ts @@ -4,7 +4,7 @@ import { TransactionReceipt } from 'web3-core' import { Contract, EventData } from 'web3-eth-contract' import { AbiItem } from 'web3-utils/types' import Web3 from 'web3' -import { SubscribablePromise, Logger } from '../utils' +import { SubscribablePromise, Logger, getFairGasPrice } from '../utils' import { DataTokens } from '../datatokens/Datatokens' export interface FixedPriceExchange { @@ -29,9 +29,8 @@ export enum FixedRateCreateProgressStep { ApprovingDatatoken } -const DEFAULT_GAS_LIMIT = 1000000 - export class OceanFixedRateExchange { + public GASLIMIT_DEFAULT = 1000000 /** Ocean related functions */ public oceanAddress: string = null public fixedRateExchangeAddress: string @@ -87,20 +86,13 @@ export class OceanFixedRateExchange { return new SubscribablePromise(async (observer) => { observer.next(FixedRateCreateProgressStep.CreatingExchange) let estGas + const gasLimitDefault = this.GASLIMIT_DEFAULT try { - /* estGas = await this.contract.methods - .create(this.oceanAddress, dataToken, this.web3.utils.toWei(rate)) - .estimateGas(function (err, g) { - if (err) { - return DEFAULT_GAS_LIMIT - } else { - return g - } - }) - */ - estGas = DEFAULT_GAS_LIMIT + estGas = await this.contract.methods + .create(this.oceanAddress, dataToken, this.web3.utils.toWei(rate)) + .estimateGas((err, estGas) => (err ? gasLimitDefault : estGas)) } catch (e) { - estGas = DEFAULT_GAS_LIMIT + estGas = gasLimitDefault } let exchangeId = null let trxReceipt = null @@ -109,7 +101,8 @@ export class OceanFixedRateExchange { .create(this.oceanAddress, dataToken, this.web3.utils.toWei(rate)) .send({ from: address, - gas: estGas + 1 + gas: estGas + 1, + gasPrice: await getFairGasPrice(this.web3) }) exchangeId = trxReceipt.events.ExchangeCreated.returnValues[0] } catch (e) { @@ -148,26 +141,22 @@ export class OceanFixedRateExchange { dataTokenAmount: string, address: string ): Promise { + const gasLimitDefault = this.GASLIMIT_DEFAULT let estGas try { estGas = await this.contract.methods .swap(exchangeId, this.web3.utils.toWei(String(dataTokenAmount))) - .estimateGas(function (err, g) { - if (err) { - return DEFAULT_GAS_LIMIT - } else { - return g - } - }) + .estimateGas((err, estGas) => (err ? gasLimitDefault : estGas)) } catch (e) { - estGas = DEFAULT_GAS_LIMIT + estGas = gasLimitDefault } try { const trxReceipt = await this.contract.methods .swap(exchangeId, this.web3.utils.toWei(String(dataTokenAmount))) .send({ from: address, - gas: estGas + 1 + gas: estGas + 1, + gasPrice: await getFairGasPrice(this.web3) }) return trxReceipt } catch (e) { @@ -199,25 +188,21 @@ export class OceanFixedRateExchange { newRate: number, address: string ): Promise { + const gasLimitDefault = this.GASLIMIT_DEFAULT let estGas try { estGas = await this.contract.methods .setRate(exchangeId, this.web3.utils.toWei(String(newRate))) - .estimateGas(function (err, estGas) { - if (err) { - console.error(`ERROR: FixedPriceExchange: ${err.message}`) - return DEFAULT_GAS_LIMIT - } - return estGas - }) + .estimateGas((err, estGas) => (err ? gasLimitDefault : estGas)) } catch (e) { - estGas = DEFAULT_GAS_LIMIT + estGas = gasLimitDefault } const trxReceipt = await this.contract.methods .setRate(exchangeId, this.web3.utils.toWei(String(newRate))) .send({ from: address, - gas: estGas + 1 + gas: estGas + 1, + gasPrice: await getFairGasPrice(this.web3) }) return trxReceipt } @@ -235,24 +220,19 @@ export class OceanFixedRateExchange { const exchange = await this.getExchange(exchangeId) if (!exchange) return null if (exchange.active === true) return null + const gasLimitDefault = this.GASLIMIT_DEFAULT let estGas try { estGas = await this.contract.methods .toggleExchangeState(exchangeId) - .estimateGas(function (err, estGas) { - if (err) { - console.error(`ERROR: FixedPriceExchange: ${err.message}`) - estGas = DEFAULT_GAS_LIMIT - } - return estGas - }) + .estimateGas((err, estGas) => (err ? gasLimitDefault : estGas)) } catch (e) { - this.logger.error(`ERROR: FixedPriceExchange: ${e.message}`) - estGas = DEFAULT_GAS_LIMIT + estGas = gasLimitDefault } const trxReceipt = await this.contract.methods.toggleExchangeState(exchangeId).send({ from: address, - gas: estGas + 1 + gas: estGas + 1, + gasPrice: await getFairGasPrice(this.web3) }) return trxReceipt } @@ -270,24 +250,19 @@ export class OceanFixedRateExchange { const exchange = await this.getExchange(exchangeId) if (!exchange) return null if (exchange.active === false) return null + const gasLimitDefault = this.GASLIMIT_DEFAULT let estGas try { estGas = await this.contract.methods .toggleExchangeState(exchangeId) - .estimateGas(function (err, estGas) { - if (err) { - console.error(`ERROR: FixedPriceExchange: ${err.message}`) - estGas = DEFAULT_GAS_LIMIT - } - return estGas - }) + .estimateGas((err, estGas) => (err ? gasLimitDefault : estGas)) } catch (e) { - this.logger.error(`ERROR: FixedPriceExchange: ${e.message}`) - estGas = DEFAULT_GAS_LIMIT + estGas = gasLimitDefault } const trxReceipt = await this.contract.methods.toggleExchangeState(exchangeId).send({ from: address, - gas: estGas + 1 + gas: estGas + 1, + gasPrice: await getFairGasPrice(this.web3) }) return trxReceipt }