Skip to content

Commit

Permalink
Merge pull request #78 from atomex-protocol/feat/atm-326-calculation-…
Browse files Browse the repository at this point in the history
…of-estimated-fees-ethereum

Implement fees calculation for ethereum operations
  • Loading branch information
skubarenko authored Aug 19, 2022
2 parents 5ea807c + f68546d commit 872dadb
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 16 deletions.
12 changes: 6 additions & 6 deletions src/ethereum/atomexProtocol/erc20EthereumWeb3AtomexProtocolV1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ export class ERC20EthereumWeb3AtomexProtocolV1 extends Web3AtomexProtocolV1 {
throw new Error('Method not implemented.');
}

getInitiateFees(_params: Partial<AtomexProtocolV1InitiateParameters>): Promise<FeesInfo> {
throw new Error('Method not implemented.');
getInitiateFees(params: Partial<AtomexProtocolV1InitiateParameters>): Promise<FeesInfo> {
return super.getInitiateFees(params);
}

redeem(_params: AtomexProtocolV1RedeemParameters): Promise<Transaction> {
Expand All @@ -40,15 +40,15 @@ export class ERC20EthereumWeb3AtomexProtocolV1 extends Web3AtomexProtocolV1 {
throw new Error('Method not implemented.');
}

getRedeemFees(_params: Partial<AtomexProtocolV1InitiateParameters>): Promise<FeesInfo> {
throw new Error('Method not implemented.');
getRedeemFees(params: Partial<AtomexProtocolV1InitiateParameters>): Promise<FeesInfo> {
return super.getRedeemFees(params);
}

refund(_params: AtomexProtocolV1RefundParameters): Promise<Transaction> {
throw new Error('Method not implemented.');
}

getRefundFees(_params: Partial<AtomexProtocolV1InitiateParameters>): Promise<FeesInfo> {
throw new Error('Method not implemented.');
getRefundFees(params: Partial<AtomexProtocolV1InitiateParameters>): Promise<FeesInfo> {
return super.getRefundFees(params);
}
}
12 changes: 6 additions & 6 deletions src/ethereum/atomexProtocol/ethereumWeb3AtomexProtocolV1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ export class EthereumWeb3AtomexProtocolV1 extends Web3AtomexProtocolV1 {
throw new Error('Method not implemented.');
}

async getInitiateFees(_params: Partial<AtomexProtocolV1InitiateParameters>): Promise<FeesInfo> {
throw new Error('Method not implemented.');
getInitiateFees(params: Partial<AtomexProtocolV1InitiateParameters>): Promise<FeesInfo> {
return super.getInitiateFees(params);
}

redeem(_params: AtomexProtocolV1RedeemParameters): Promise<Transaction> {
Expand All @@ -36,15 +36,15 @@ export class EthereumWeb3AtomexProtocolV1 extends Web3AtomexProtocolV1 {
throw new Error('Method not implemented.');
}

getRedeemFees(_params: Partial<AtomexProtocolV1InitiateParameters>): Promise<FeesInfo> {
throw new Error('Method not implemented.');
getRedeemFees(params: Partial<AtomexProtocolV1InitiateParameters>): Promise<FeesInfo> {
return super.getRedeemFees(params);
}

refund(_params: AtomexProtocolV1RefundParameters): Promise<Transaction> {
throw new Error('Method not implemented.');
}

getRefundFees(_params: Partial<AtomexProtocolV1InitiateParameters>): Promise<FeesInfo> {
throw new Error('Method not implemented.');
getRefundFees(params: Partial<AtomexProtocolV1InitiateParameters>): Promise<FeesInfo> {
return super.getRefundFees(params);
}
}
42 changes: 38 additions & 4 deletions src/evm/atomexProtocol/web3AtomexProtocolV1.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type BigNumber from 'bignumber.js';
import BigNumber from 'bignumber.js';
import type Web3 from 'web3';

import type {
Expand All @@ -8,9 +8,11 @@ import type {
} from '../../blockchain/index';
import type { AtomexNetwork } from '../../common/index';
import type { DeepReadonly } from '../../core/index';
import { web3Helper } from '../helpers';
import type { Web3AtomexProtocolV1Options } from '../models/index';

export abstract class Web3AtomexProtocolV1 implements AtomexProtocolV1 {
protected static maxNetworkFeeMultiplier = new BigNumber(1.2);
readonly version = 1;

constructor(
Expand All @@ -28,17 +30,49 @@ export abstract class Web3AtomexProtocolV1 implements AtomexProtocolV1 {

abstract initiate(_params: AtomexProtocolV1InitiateParameters): Promise<Transaction>;

abstract getInitiateFees(_params: Partial<AtomexProtocolV1InitiateParameters>): Promise<FeesInfo>;
async getInitiateFees(params: Partial<AtomexProtocolV1InitiateParameters>): Promise<FeesInfo> {
const toolkit = await this.getReadonlyWeb3();
const gasPriceInWei = await web3Helper.getGasPriceInWei(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(Web3AtomexProtocolV1.maxNetworkFeeMultiplier);
const estimated = web3Helper.convertFromWei(toolkit, estimatedWei, 'ether');
const result: FeesInfo = { estimated, max: estimated };

return Promise.resolve(result);
}

abstract redeem(_params: AtomexProtocolV1RedeemParameters): Promise<Transaction>;

abstract getRedeemReward(_nativeTokenPriceInUsd: number, _nativeTokenPriceInCurrency: number): Promise<BigNumber>;

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

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

return Promise.resolve(result);
}

abstract refund(_params: AtomexProtocolV1RefundParameters): Promise<Transaction>;

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

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

return Promise.resolve(result);
}

protected async getReadonlyWeb3(): Promise<Web3> {
const toolkit = await this.atomexBlockchainProvider.getReadonlyToolkit<Web3>('web3', this.blockchain);
Expand Down
1 change: 1 addition & 0 deletions src/evm/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * as web3Helper from './web3Helper';
16 changes: 16 additions & 0 deletions src/evm/helpers/web3Helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import BigNumber from 'bignumber.js';
import type Web3 from 'web3';
import type { Unit } from 'web3-utils';

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

return new BigNumber(gasPrice);
};

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);

return new BigNumber(result);
};

0 comments on commit 872dadb

Please sign in to comment.