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

optimise gas costs

This commit is contained in:
alexcos20 2020-10-30 06:02:06 -07:00
parent 17a07b4897
commit ead3d212a2

View File

@ -4,7 +4,7 @@ import { TransactionReceipt } from 'web3-core'
import { Contract, EventData } from 'web3-eth-contract' import { Contract, EventData } from 'web3-eth-contract'
import { AbiItem } from 'web3-utils/types' import { AbiItem } from 'web3-utils/types'
import Web3 from 'web3' import Web3 from 'web3'
import { SubscribablePromise, Logger } from '../utils' import { SubscribablePromise, Logger, getFairGasPrice } from '../utils'
import { DataTokens } from '../datatokens/Datatokens' import { DataTokens } from '../datatokens/Datatokens'
export interface FixedPriceExchange { export interface FixedPriceExchange {
@ -29,9 +29,8 @@ export enum FixedRateCreateProgressStep {
ApprovingDatatoken ApprovingDatatoken
} }
const DEFAULT_GAS_LIMIT = 1000000
export class OceanFixedRateExchange { export class OceanFixedRateExchange {
public GASLIMIT_DEFAULT = 1000000
/** Ocean related functions */ /** Ocean related functions */
public oceanAddress: string = null public oceanAddress: string = null
public fixedRateExchangeAddress: string public fixedRateExchangeAddress: string
@ -87,20 +86,13 @@ export class OceanFixedRateExchange {
return new SubscribablePromise(async (observer) => { return new SubscribablePromise(async (observer) => {
observer.next(FixedRateCreateProgressStep.CreatingExchange) observer.next(FixedRateCreateProgressStep.CreatingExchange)
let estGas let estGas
const gasLimitDefault = this.GASLIMIT_DEFAULT
try { try {
/* estGas = await this.contract.methods estGas = await this.contract.methods
.create(this.oceanAddress, dataToken, this.web3.utils.toWei(rate)) .create(this.oceanAddress, dataToken, this.web3.utils.toWei(rate))
.estimateGas(function (err, g) { .estimateGas((err, estGas) => (err ? gasLimitDefault : estGas))
if (err) {
return DEFAULT_GAS_LIMIT
} else {
return g
}
})
*/
estGas = DEFAULT_GAS_LIMIT
} catch (e) { } catch (e) {
estGas = DEFAULT_GAS_LIMIT estGas = gasLimitDefault
} }
let exchangeId = null let exchangeId = null
let trxReceipt = null let trxReceipt = null
@ -109,7 +101,8 @@ export class OceanFixedRateExchange {
.create(this.oceanAddress, dataToken, this.web3.utils.toWei(rate)) .create(this.oceanAddress, dataToken, this.web3.utils.toWei(rate))
.send({ .send({
from: address, from: address,
gas: estGas + 1 gas: estGas + 1,
gasPrice: await getFairGasPrice(this.web3)
}) })
exchangeId = trxReceipt.events.ExchangeCreated.returnValues[0] exchangeId = trxReceipt.events.ExchangeCreated.returnValues[0]
} catch (e) { } catch (e) {
@ -148,26 +141,22 @@ export class OceanFixedRateExchange {
dataTokenAmount: string, dataTokenAmount: string,
address: string address: string
): Promise<TransactionReceipt> { ): Promise<TransactionReceipt> {
const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas let estGas
try { try {
estGas = await this.contract.methods estGas = await this.contract.methods
.swap(exchangeId, this.web3.utils.toWei(String(dataTokenAmount))) .swap(exchangeId, this.web3.utils.toWei(String(dataTokenAmount)))
.estimateGas(function (err, g) { .estimateGas((err, estGas) => (err ? gasLimitDefault : estGas))
if (err) {
return DEFAULT_GAS_LIMIT
} else {
return g
}
})
} catch (e) { } catch (e) {
estGas = DEFAULT_GAS_LIMIT estGas = gasLimitDefault
} }
try { try {
const trxReceipt = await this.contract.methods const trxReceipt = await this.contract.methods
.swap(exchangeId, this.web3.utils.toWei(String(dataTokenAmount))) .swap(exchangeId, this.web3.utils.toWei(String(dataTokenAmount)))
.send({ .send({
from: address, from: address,
gas: estGas + 1 gas: estGas + 1,
gasPrice: await getFairGasPrice(this.web3)
}) })
return trxReceipt return trxReceipt
} catch (e) { } catch (e) {
@ -199,25 +188,21 @@ export class OceanFixedRateExchange {
newRate: number, newRate: number,
address: string address: string
): Promise<TransactionReceipt> { ): Promise<TransactionReceipt> {
const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas let estGas
try { try {
estGas = await this.contract.methods estGas = await this.contract.methods
.setRate(exchangeId, this.web3.utils.toWei(String(newRate))) .setRate(exchangeId, this.web3.utils.toWei(String(newRate)))
.estimateGas(function (err, estGas) { .estimateGas((err, estGas) => (err ? gasLimitDefault : estGas))
if (err) {
console.error(`ERROR: FixedPriceExchange: ${err.message}`)
return DEFAULT_GAS_LIMIT
}
return estGas
})
} catch (e) { } catch (e) {
estGas = DEFAULT_GAS_LIMIT estGas = gasLimitDefault
} }
const trxReceipt = await this.contract.methods const trxReceipt = await this.contract.methods
.setRate(exchangeId, this.web3.utils.toWei(String(newRate))) .setRate(exchangeId, this.web3.utils.toWei(String(newRate)))
.send({ .send({
from: address, from: address,
gas: estGas + 1 gas: estGas + 1,
gasPrice: await getFairGasPrice(this.web3)
}) })
return trxReceipt return trxReceipt
} }
@ -235,24 +220,19 @@ export class OceanFixedRateExchange {
const exchange = await this.getExchange(exchangeId) const exchange = await this.getExchange(exchangeId)
if (!exchange) return null if (!exchange) return null
if (exchange.active === true) return null if (exchange.active === true) return null
const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas let estGas
try { try {
estGas = await this.contract.methods estGas = await this.contract.methods
.toggleExchangeState(exchangeId) .toggleExchangeState(exchangeId)
.estimateGas(function (err, estGas) { .estimateGas((err, estGas) => (err ? gasLimitDefault : estGas))
if (err) {
console.error(`ERROR: FixedPriceExchange: ${err.message}`)
estGas = DEFAULT_GAS_LIMIT
}
return estGas
})
} catch (e) { } catch (e) {
this.logger.error(`ERROR: FixedPriceExchange: ${e.message}`) estGas = gasLimitDefault
estGas = DEFAULT_GAS_LIMIT
} }
const trxReceipt = await this.contract.methods.toggleExchangeState(exchangeId).send({ const trxReceipt = await this.contract.methods.toggleExchangeState(exchangeId).send({
from: address, from: address,
gas: estGas + 1 gas: estGas + 1,
gasPrice: await getFairGasPrice(this.web3)
}) })
return trxReceipt return trxReceipt
} }
@ -270,24 +250,19 @@ export class OceanFixedRateExchange {
const exchange = await this.getExchange(exchangeId) const exchange = await this.getExchange(exchangeId)
if (!exchange) return null if (!exchange) return null
if (exchange.active === false) return null if (exchange.active === false) return null
const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas let estGas
try { try {
estGas = await this.contract.methods estGas = await this.contract.methods
.toggleExchangeState(exchangeId) .toggleExchangeState(exchangeId)
.estimateGas(function (err, estGas) { .estimateGas((err, estGas) => (err ? gasLimitDefault : estGas))
if (err) {
console.error(`ERROR: FixedPriceExchange: ${err.message}`)
estGas = DEFAULT_GAS_LIMIT
}
return estGas
})
} catch (e) { } catch (e) {
this.logger.error(`ERROR: FixedPriceExchange: ${e.message}`) estGas = gasLimitDefault
estGas = DEFAULT_GAS_LIMIT
} }
const trxReceipt = await this.contract.methods.toggleExchangeState(exchangeId).send({ const trxReceipt = await this.contract.methods.toggleExchangeState(exchangeId).send({
from: address, from: address,
gas: estGas + 1 gas: estGas + 1,
gasPrice: await getFairGasPrice(this.web3)
}) })
return trxReceipt return trxReceipt
} }