Skip to content

Commit

Permalink
Merge pull request #556 from Adamant-im/fix/eth-erc20-increase-fee
Browse files Browse the repository at this point in the history
fix(ETH/ERC20): round `gasLimit` before converting to BigInt
  • Loading branch information
bludnic authored Nov 13, 2023
2 parents 28d1ec7 + 0eb89bc commit cd87d16
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
16 changes: 15 additions & 1 deletion src/lib/__tests__/eth-utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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))
})
})
})
12 changes: 12 additions & 0 deletions src/lib/eth-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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))
}
2 changes: 1 addition & 1 deletion src/store/modules/erc20/erc20-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const initTransaction = async (api, context, ethAddress, amount, increaseFee) =>
.getClient()
.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
}
Expand Down
10 changes: 3 additions & 7 deletions src/store/modules/eth/actions.js
Original file line number Diff line number Diff line change
@@ -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 */
Expand Down Expand Up @@ -37,7 +33,7 @@ const initTransaction = async (api, context, ethAddress, amount, increaseFee) =>
.getClient()
.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
}
Expand Down

0 comments on commit cd87d16

Please sign in to comment.