From 03665f263940834198f6143ad5af4cdc81d0fb6a Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Mon, 26 Oct 2020 03:49:22 -0700 Subject: [PATCH] gas optimisations --- src/balancer/PoolFactory.ts | 2 +- src/datatokens/Datatokens.ts | 20 ++++++++++---- src/exchange/FixedRateExchange.ts | 2 +- src/metadatacache/OnChainMetaDataCache.ts | 33 ++++++++++++++++++----- 4 files changed, 44 insertions(+), 13 deletions(-) diff --git a/src/balancer/PoolFactory.ts b/src/balancer/PoolFactory.ts index 886bd5964..71d9a6cac 100644 --- a/src/balancer/PoolFactory.ts +++ b/src/balancer/PoolFactory.ts @@ -5,7 +5,7 @@ import jsonFactoryABI from '@oceanprotocol/contracts/artifacts/BFactory.json' import { TransactionReceipt } from 'web3-core' export class PoolFactory { - public GASLIMIT_DEFAULT = 5000000 + public GASLIMIT_DEFAULT = 8000000 public web3: Web3 = null public factoryABI: AbiItem | AbiItem[] public factoryAddress: string diff --git a/src/datatokens/Datatokens.ts b/src/datatokens/Datatokens.ts index 648aa8a98..5056d4ac2 100644 --- a/src/datatokens/Datatokens.ts +++ b/src/datatokens/Datatokens.ts @@ -102,8 +102,7 @@ export class DataTokens { .createToken(metadataCacheUri, name, symbol, this.web3.utils.toWei(cap)) .send({ from: address, - gas: estGas + 1, - gasPrice: '3000000000' + gas: estGas + 1 }) let tokenAddress = null @@ -167,8 +166,7 @@ export class DataTokens { .mint(destAddress, this.web3.utils.toWei(amount)) .send({ from: address, - gas: estGas + 1, - gasPrice: '3000000000' + gas: estGas + 1 }) return trxReceipt @@ -365,6 +363,18 @@ export class DataTokens { }) if (!mpFeeAddress) mpFeeAddress = '0x0000000000000000000000000000000000000000' try { + const estGas = await datatoken.methods + .startOrder( + consumer, + this.web3.utils.toWei(amount), + String(serviceId), + mpFeeAddress + ) + .estimateGas(function (err, estGas) { + if (err) console.error(`ERROR: Datatokens : ${err}`) + return estGas + }) + const trxReceipt = await datatoken.methods .startOrder( consumer, @@ -372,7 +382,7 @@ export class DataTokens { String(serviceId), mpFeeAddress ) - .send({ from: address, gas: 600000 }) + .send({ from: address, gas: estGas + 1 }) return trxReceipt } catch (e) { this.logger.error(`ERROR: Failed to start order : ${e.message}`) diff --git a/src/exchange/FixedRateExchange.ts b/src/exchange/FixedRateExchange.ts index 6e2ebda38..2ab15e838 100644 --- a/src/exchange/FixedRateExchange.ts +++ b/src/exchange/FixedRateExchange.ts @@ -29,7 +29,7 @@ export enum FixedRateCreateProgressStep { ApprovingDatatoken } -const DEFAULT_GAS_LIMIT = 300000 +const DEFAULT_GAS_LIMIT = 1000000 export class OceanFixedRateExchange { /** Ocean related functions */ diff --git a/src/metadatacache/OnChainMetaDataCache.ts b/src/metadatacache/OnChainMetaDataCache.ts index 9c4211030..c012b36e9 100644 --- a/src/metadatacache/OnChainMetaDataCache.ts +++ b/src/metadatacache/OnChainMetaDataCache.ts @@ -8,7 +8,7 @@ import { didZeroX, Logger } from '../utils' // Using limited, compress-only version // See https://github.com/LZMA-JS/LZMA-JS#but-i-dont-want-to-use-web-workers import { LZMA } from 'lzma/src/lzma-c' - +const DEFAULT_GAS_LIMIT = 1000000 /** * Provides an interface with Metadata Cache. * Metadata Cache provides an off-chain database store for metadata about data assets. @@ -105,17 +105,24 @@ export class OnChainMetadataCache { this.logger.error('ERROR: Missing DDOContract') return null } + let estGas try { - /* const estGas = await this.DDOContract.methods + estGas = await this.DDOContract.methods .create(didZeroX(did), flags, data) .estimateGas(function (err, estGas) { - if (err) console.error('ERROR: OnChainMetadataCacheEstimateGas: ' + err) + if (err) { + // console.error('ERROR: OnChainMetadataCacheEstimateGas: ' + err) + return DEFAULT_GAS_LIMIT + } return estGas }) - */ + } catch (e) { + estGas = DEFAULT_GAS_LIMIT + } + try { const trxReceipt = await this.DDOContract.methods .create(didZeroX(did), flags, data) - .send({ from: consumerAccount }) + .send({ from: consumerAccount, gas: estGas + 1 }) return trxReceipt } catch (e) { this.logger.error(`ERROR: Failed to publish raw DDO : ${e.message}`) @@ -141,10 +148,24 @@ export class OnChainMetadataCache { this.logger.error('ERROR: Missing DDOContract') return null } + let estGas + try { + estGas = await this.DDOContract.methods + .update(didZeroX(did), flags, data) + .estimateGas(function (err, estGas) { + if (err) { + // console.error('ERROR: OnChainMetadataCacheEstimateGas: ' + err) + return DEFAULT_GAS_LIMIT + } + return estGas + }) + } catch (e) { + estGas = DEFAULT_GAS_LIMIT + } try { const trxReceipt = await this.DDOContract.methods .update(didZeroX(did), flags, data) - .send({ from: consumerAccount }) + .send({ from: consumerAccount, gas: estGas + 1 }) return trxReceipt } catch (e) { this.logger.error(`ERROR: Failed to update raw DDO : ${e.message}`)