From d1be7d533891926976b677df40e461858b01f75a Mon Sep 17 00:00:00 2001 From: akayi07 Date: Mon, 15 May 2023 02:31:49 +0800 Subject: [PATCH 1/2] add getEstimatedGasFee --- clients/contracts/bridge.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/clients/contracts/bridge.ts b/clients/contracts/bridge.ts index 813cee7..ae908bc 100644 --- a/clients/contracts/bridge.ts +++ b/clients/contracts/bridge.ts @@ -58,8 +58,11 @@ function hexcmp(x: string, y: string) { } export class BridgeContract extends DelphinusContract { + private readonly web3: DelphinusWeb3; + constructor(web3: DelphinusWeb3, address: string, account?: string) { super(web3, BridgeContract.getJsonInterface(), address, account); + this.web3 = web3; } static getJsonInterface(): any { @@ -82,6 +85,27 @@ export class BridgeContract extends DelphinusContract { return this.getWeb3Contract().methods.addToken(tokenid).send(); } + async getEstimatedGasFee(calldata: number[], verifydata: BN[], vid: number, rid: BN) { + const gasPrice = await this.web3.web3Instance.eth.getGasPrice(); + console.log("The gas price is", gasPrice); + const tx = this.getWeb3Contract().methods.verify( + calldata, + verifydata, + vid, + rid + ); + return tx.estimateGas() + .then((estimatedGas: number) => { + console.log("The estimated gas is", estimatedGas); + const txPriceWei = estimatedGas * Number(gasPrice); + const txPriceEth = this.web3.web3Instance.utils.fromWei(txPriceWei.toString(), 'ether'); + return Number(txPriceEth) * estimatedGas; + }) + .catch((e: any) => { + console.log("%s", e); + }); + } + private _verify(calldata: number[], verifydata: BN[], vid: number, rid: BN) { const tx = this.getWeb3Contract().methods.verify( calldata, From e1e8e34464edfd62ba0697edd615827c520e0655 Mon Sep 17 00:00:00 2001 From: akayi07 Date: Sun, 28 May 2023 11:05:05 +0800 Subject: [PATCH 2/2] refine the way of calculating gas fee --- clients/contracts/bridge.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/clients/contracts/bridge.ts b/clients/contracts/bridge.ts index ae908bc..eeb6deb 100644 --- a/clients/contracts/bridge.ts +++ b/clients/contracts/bridge.ts @@ -87,7 +87,7 @@ export class BridgeContract extends DelphinusContract { async getEstimatedGasFee(calldata: number[], verifydata: BN[], vid: number, rid: BN) { const gasPrice = await this.web3.web3Instance.eth.getGasPrice(); - console.log("The gas price is", gasPrice); + console.log("The gas price is", gasPrice, "wei"); const tx = this.getWeb3Contract().methods.verify( calldata, verifydata, @@ -97,9 +97,7 @@ export class BridgeContract extends DelphinusContract { return tx.estimateGas() .then((estimatedGas: number) => { console.log("The estimated gas is", estimatedGas); - const txPriceWei = estimatedGas * Number(gasPrice); - const txPriceEth = this.web3.web3Instance.utils.fromWei(txPriceWei.toString(), 'ether'); - return Number(txPriceEth) * estimatedGas; + return estimatedGas * Number(gasPrice); }) .catch((e: any) => { console.log("%s", e);