Skip to content

Commit

Permalink
Use the EIP 1559 for the Web3 atomex transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
skubarenko committed Sep 2, 2022
1 parent 940407a commit 1b74c7f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/evm/atomexProtocol/web3AtomexProtocolMultiChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { web3Helper } from '../helpers';
import type { Web3AtomexProtocolMultiChainOptions } from '../models/index';

export abstract class Web3AtomexProtocolMultiChain implements AtomexProtocolMultiChainBase {
protected static maxNetworkFeeMultiplier = new BigNumber(1.2);
protected static readonly defaultMaxNetworkFeeMultiplier = new BigNumber(1.2);

abstract readonly type: string;

Expand All @@ -41,14 +41,15 @@ export abstract class Web3AtomexProtocolMultiChain implements AtomexProtocolMult

async getInitiateFees(params: Partial<AtomexProtocolMultiChainInitiateParameters>): Promise<FeesInfo> {
const toolkit = await this.getReadonlyWeb3();
const gasPriceInWei = await web3Helper.getGasPriceInWei(toolkit);
const maxFeePerGasInWei = await web3Helper.getMaxFeePerGas(toolkit);
const gasLimitOptions = this.atomexProtocolOptions.initiateOperation.gasLimit;
const hasRewardForRedeem = params.rewardForRedeem?.isGreaterThan(0);
const gasLimit = new BigNumber(hasRewardForRedeem ? gasLimitOptions.withReward : gasLimitOptions.withoutReward);

const estimatedWei = gasPriceInWei.multipliedBy(gasLimit).multipliedBy(Web3AtomexProtocolMultiChain.maxNetworkFeeMultiplier);
const estimated = web3Helper.convertFromWei(toolkit, estimatedWei, 'ether');
const result: FeesInfo = { estimated, max: estimated };
const estimatedInWei = maxFeePerGasInWei.multipliedBy(gasLimit);
const estimated = web3Helper.convertFromWei(toolkit, estimatedInWei, 'ether');
const max = estimated.multipliedBy(Web3AtomexProtocolMultiChain.defaultMaxNetworkFeeMultiplier);
const result: FeesInfo = { estimated, max };

return Promise.resolve(result);
}
Expand All @@ -59,10 +60,10 @@ export abstract class Web3AtomexProtocolMultiChain implements AtomexProtocolMult

async getRedeemFees(_params: Partial<AtomexProtocolMultiChainInitiateParameters>): Promise<FeesInfo> {
const toolkit = await this.getReadonlyWeb3();
const gasPriceInWei = await web3Helper.getGasPriceInWei(toolkit);
const maxFeePerGasInWei = await web3Helper.getMaxFeePerGas(toolkit);
const gasLimit = this.atomexProtocolOptions.redeemOperation.gasLimit;

const estimatedWei = gasPriceInWei.multipliedBy(gasLimit).multipliedBy(Web3AtomexProtocolMultiChain.maxNetworkFeeMultiplier);
const estimatedWei = maxFeePerGasInWei.multipliedBy(gasLimit).multipliedBy(Web3AtomexProtocolMultiChain.defaultMaxNetworkFeeMultiplier);
const estimated = web3Helper.convertFromWei(toolkit, estimatedWei, 'ether');
const result: FeesInfo = { estimated, max: estimated };

Expand All @@ -73,10 +74,10 @@ export abstract class Web3AtomexProtocolMultiChain implements AtomexProtocolMult

async getRefundFees(_params: Partial<AtomexProtocolMultiChainInitiateParameters>): Promise<FeesInfo> {
const toolkit = await this.getReadonlyWeb3();
const gasPriceInWei = await web3Helper.getGasPriceInWei(toolkit);
const maxFeePerGasInWei = await web3Helper.getMaxFeePerGas(toolkit);
const gasLimit = this.atomexProtocolOptions.refundOperation.gasLimit;

const estimatedWei = gasPriceInWei.multipliedBy(gasLimit).multipliedBy(Web3AtomexProtocolMultiChain.maxNetworkFeeMultiplier);
const estimatedWei = maxFeePerGasInWei.multipliedBy(gasLimit).multipliedBy(Web3AtomexProtocolMultiChain.defaultMaxNetworkFeeMultiplier);
const estimated = web3Helper.convertFromWei(toolkit, estimatedWei, 'ether');
const result: FeesInfo = { estimated, max: estimated };

Expand Down
13 changes: 13 additions & 0 deletions src/evm/helpers/web3Helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,25 @@ import BigNumber from 'bignumber.js';
import type Web3 from 'web3';
import type { Unit } from 'web3-utils';

const defaultMaxPriorityFeePerGasInWei = new BigNumber(2_500_000_000);

export const getGasPriceInWei = async (toolkit: Web3): Promise<BigNumber> => {
const gasPrice = await toolkit.eth.getGasPrice();

return new BigNumber(gasPrice);
};

export const getMaxFeePerGas = async (toolkit: Web3, maxPriorityFeePerGasInWei = defaultMaxPriorityFeePerGasInWei): Promise<BigNumber> => {
const latestBlock = await toolkit.eth.getBlock('latest');
if (!latestBlock.baseFeePerGas)
return getGasPriceInWei(toolkit);

const latestBlockBaseFeePerGasInWei = new BigNumber(latestBlock.baseFeePerGas);
return latestBlockBaseFeePerGasInWei
.multipliedBy(2)
.plus(maxPriorityFeePerGasInWei);
};

export const convertFromWei = (toolkit: Web3, value: BigNumber | string, unit: Unit): BigNumber => {
const stringValue = typeof value === 'string' ? value : value.toString(10);
const result = toolkit.utils.fromWei(stringValue, unit);
Expand Down

0 comments on commit 1b74c7f

Please sign in to comment.