From 0eb89bc872a6c6f700ad1272196ce96d8f0ca027 Mon Sep 17 00:00:00 2001 From: bludnic Date: Sat, 11 Nov 2023 15:54:01 +0000 Subject: [PATCH] fix(ETH/ERC20): round `gasLimit` before converting to BigInt --- src/lib/__tests__/eth-utils.spec.js | 16 +++++++++++++++- src/lib/eth-utils.js | 12 ++++++++++++ src/store/modules/erc20/erc20-actions.js | 2 +- src/store/modules/eth/actions.js | 10 +++------- 4 files changed, 31 insertions(+), 9 deletions(-) diff --git a/src/lib/__tests__/eth-utils.spec.js b/src/lib/__tests__/eth-utils.spec.js index 42e259bb9..8994ecd89 100644 --- a/src/lib/__tests__/eth-utils.spec.js +++ b/src/lib/__tests__/eth-utils.spec.js @@ -4,7 +4,7 @@ import { describe, it, expect } from 'vitest' import Web3Eth from 'web3-eth' -import { toEther, toWei, getAccountFromPassphrase } from '@/lib/eth-utils' +import { toEther, toWei, getAccountFromPassphrase, increaseFee } from '@/lib/eth-utils' describe('eth-utils', () => { describe('toEther', () => { @@ -52,4 +52,18 @@ describe('eth-utils', () => { }) }) }) + + describe('increaseFee', () => { + it('should multiply `gasLimit` as a number', () => { + expect(increaseFee(21000, 2)).toBe(BigInt(42000)) + }) + + it('should multiply `gasLimit` as a bigint', () => { + expect(increaseFee(BigInt(21000), 2)).toBe(BigInt(42000)) + }) + + it('should round the result before converting to bigint', () => { + expect(increaseFee(BigInt(21001), 1.5)).toBe(BigInt(31502)) + }) + }) }) diff --git a/src/lib/eth-utils.js b/src/lib/eth-utils.js index ff4933cae..01e65edcf 100644 --- a/src/lib/eth-utils.js +++ b/src/lib/eth-utils.js @@ -3,6 +3,7 @@ import * as web3Utils from 'web3-utils' import { privateKeyToAccount } from 'web3-eth-accounts' import BigNumber from 'bignumber.js' import cache from '@/store/cache.js' +import { INCREASE_FEE_MULTIPLIER } from '@/lib/constants' const HD_KEY_PATH = "m/44'/60'/3'/1/0" @@ -106,3 +107,14 @@ export function toFraction(amount, decimals, separator = '.') { return whole + (fraction ? separator + fraction : '') } + +/** + * @param {bigint | number} gasLimit + * @param {number} multiplier + * @returns {bigint} Increased fee + */ +export function increaseFee(gasLimit, multiplier = INCREASE_FEE_MULTIPLIER) { + const increasedGasLimit = BigNumber(Number(gasLimit)).multipliedBy(multiplier).toNumber() + + return BigInt(Math.round(increasedGasLimit)) +} diff --git a/src/store/modules/erc20/erc20-actions.js b/src/store/modules/erc20/erc20-actions.js index ec10b17cc..fbc41b559 100644 --- a/src/store/modules/erc20/erc20-actions.js +++ b/src/store/modules/erc20/erc20-actions.js @@ -38,7 +38,7 @@ const initTransaction = async (api, context, ethAddress, amount, increaseFee) => const gasLimit = await api .estimateGas(transaction) .catch(() => BigInt(DEFAULT_ERC20_TRANSFER_GAS_LIMIT)) - transaction.gasLimit = increaseFee ? gasLimit * BigInt(INCREASE_FEE_MULTIPLIER) : gasLimit + transaction.gasLimit = increaseFee ? ethUtils.increaseFee(gasLimit) : gasLimit return transaction } diff --git a/src/store/modules/eth/actions.js b/src/store/modules/eth/actions.js index 27284180e..7e7c1ebad 100644 --- a/src/store/modules/eth/actions.js +++ b/src/store/modules/eth/actions.js @@ -1,11 +1,7 @@ -import * as utils from '../../../lib/eth-utils' +import * as utils from '@/lib/eth-utils' import createActions from '../eth-base/eth-base-actions' -import { - DEFAULT_ETH_TRANSFER_GAS_LIMIT, - FetchStatus, - INCREASE_FEE_MULTIPLIER -} from '@/lib/constants' +import { DEFAULT_ETH_TRANSFER_GAS_LIMIT, FetchStatus } from '@/lib/constants' import { storeCryptoAddress } from '@/lib/store-crypto-address' /** Timestamp of the most recent status update */ @@ -36,7 +32,7 @@ const initTransaction = async (api, context, ethAddress, amount, increaseFee) => const gasLimit = await api .estimateGas(transaction) .catch(() => BigInt(DEFAULT_ETH_TRANSFER_GAS_LIMIT)) - transaction.gasLimit = increaseFee ? gasLimit * BigInt(INCREASE_FEE_MULTIPLIER) : gasLimit + transaction.gasLimit = increaseFee ? utils.increaseFee(gasLimit) : gasLimit return transaction }