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 { 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<TransactionReceipt> {
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<TransactionReceipt> {
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
}