Skip to content

Commit

Permalink
update estimate gas eth
Browse files Browse the repository at this point in the history
  • Loading branch information
Sotatek-MinhVu committed Jan 5, 2024
1 parent 9b2e947 commit 7c881bc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
20 changes: 15 additions & 5 deletions src/shared/modules/web3/web3.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import {
import { sleep } from '@shared/utils/promise';

import ETHBridgeAbi from './abis/eth-bridge-contract.json';
import MinaBridgeAbi from './abis/mina-bridge.json';
import { IRpcService } from './web3.module';
import BigNumber from 'bignumber.js';

export class DefaultContract {
private contract: Contract;
Expand Down Expand Up @@ -81,9 +81,6 @@ export class DefaultContract {
public call(method: string, param: Array<any>) {
return this.wrapper(() => this.contract.methods[method](...param).call(), true);
}
// public async estimateGas() {
// return await this.rpcService.web3.eth.getGasPrice();
// }

public async estimateGas(
method: string,
Expand Down Expand Up @@ -199,6 +196,17 @@ export class DefaultContract {
public async getBlockTimeByBlockNumber(blockNumber: number) {
return this.rpcService.web3.eth.getBlock(blockNumber);
}

public async convertGasPriceToEther(amount : number) {
try {
const gasPrice = await this.rpcService.web3.eth.getGasPrice();
console.log('Current gas price:', gasPrice, 'wei'); // Gas price is returned in wei
const estimateGasToWei = BigNumber(amount).multipliedBy(BigNumber(gasPrice)).toString()
return this.rpcService.web3.utils.fromWei(estimateGasToWei, 'ether');
} catch (error) {
console.error('Error getting gas price:', error);
}
}
}

@Injectable()
Expand All @@ -223,7 +231,9 @@ export class ETHBridgeContract extends DefaultContract {
return this.write('unlock', [tokenFromAddress, amount, receiveAddress, txHashLock, Number(fee)])
}
public async getEstimateGas(tokenFromAddress, amount, txHashLock, receiveAddress, fee?) {
return await this.estimateGas('unlock', [tokenFromAddress, amount, receiveAddress, txHashLock, Number(fee)])
const estimateGas = await this.estimateGas('unlock', [tokenFromAddress, amount, receiveAddress, txHashLock, Number(fee)])
const getGasPrice = await this.convertGasPriceToEther(estimateGas);
return getGasPrice
}
public async getTokenURI(tokenId: number) {
return this.call('tokenURI', [tokenId]);
Expand Down
2 changes: 1 addition & 1 deletion src/shared/utils/bignumber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ export const addDecimal = (value: string | number, decimal: number) => {
export const calculateFee = (amount: string, gasFee: string | number, tipPercent: number) => {
const tip = BigNumber(amount).minus(BigNumber(gasFee)).multipliedBy(tipPercent * 100).dividedBy(100);

return BigNumber(gasFee).plus(tip).toFixed(0, BigNumber.ROUND_DOWN);
return BigNumber(gasFee).plus(tip).toFixed(0, BigNumber.ROUND_UP);
}

0 comments on commit 7c881bc

Please sign in to comment.