From 260e78344de0be4971e25d80797d9bbb42a26289 Mon Sep 17 00:00:00 2001 From: bludnic Date: Fri, 3 Nov 2023 19:12:34 +0000 Subject: [PATCH 1/6] fix(ETH, ERC20): transfer funds doesn't work after `web3-eth` upgrade --- src/store/modules/erc20/erc20-actions.js | 20 ++++++++------ .../modules/eth-base/eth-base-actions.js | 7 +++-- src/store/modules/eth/actions.js | 27 ++++++++++--------- 3 files changed, 31 insertions(+), 23 deletions(-) diff --git a/src/store/modules/erc20/erc20-actions.js b/src/store/modules/erc20/erc20-actions.js index 4bf2a60e7..36996aa77 100644 --- a/src/store/modules/erc20/erc20-actions.js +++ b/src/store/modules/erc20/erc20-actions.js @@ -14,26 +14,30 @@ const STATUS_INTERVAL = 25000 // Setup decoder const abiDecoder = new AbiDecoder(Erc20) -const initTransaction = (api, context, ethAddress, amount, increaseFee) => { +const initTransaction = async (api, context, ethAddress, amount, increaseFee) => { const contract = new EthContract(Erc20, context.state.contractAddress) + const nonce = await api.getTransactionCount(context.state.address) + const gasPrice = await api.getGasPrice() + const transaction = { from: context.state.address, to: context.state.contractAddress, value: '0x0', // gasLimit: api.fromDecimal(DEFAULT_ERC20_TRANSFER_GAS), // Don't take default value, instead calculate with estimateGas(transactionObject) - // gasPrice: context.getters.gasPrice, // Set gas price to auto calc. Deprecated after London hardfork - // nonce // Let sendTransaction choose it + gasPrice, + nonce, data: contract.methods .transfer(ethAddress, ethUtils.toWhole(amount, context.state.decimals)) .encodeABI() } - return api.estimateGas(transaction).then((gasLimit) => { - gasLimit = increaseFee ? gasLimit * INCREASE_FEE_MULTIPLIER : gasLimit - transaction.gas = gasLimit - return transaction - }) + const defaultGasLimit = await api.estimateGas(transaction) + transaction.gasLimit = increaseFee + ? defaultGasLimit * BigInt(INCREASE_FEE_MULTIPLIER) + : defaultGasLimit + + return transaction } const parseTransaction = (context, tx) => { diff --git a/src/store/modules/eth-base/eth-base-actions.js b/src/store/modules/eth-base/eth-base-actions.js index 5f86306a0..26f98902c 100644 --- a/src/store/modules/eth-base/eth-base-actions.js +++ b/src/store/modules/eth-base/eth-base-actions.js @@ -6,6 +6,7 @@ import * as utils from '../../../lib/eth-utils' import { getTransactions } from '../../../lib/eth-index' import * as tf from '../../../lib/transactionsFetching' import { isStringEqualCI } from '@/lib/textHelpers' +import { signTransaction, TransactionFactory } from 'web3-eth-accounts' /** Interval between attempts to fetch the registered tx details */ const RETRY_TIMEOUT = 20 * 1000 @@ -65,7 +66,9 @@ export default function createActions(config) { return initTransaction(api, context, address, amount, increaseFee) .then((ethTx) => { - return api.accounts.signTransaction(ethTx, context.state.privateKey).then((signedTx) => { + const tx = TransactionFactory.fromTxData(ethTx) + + return signTransaction(tx, context.state.privateKey).then((signedTx) => { const txInfo = { signedTx, ethTx @@ -137,7 +140,7 @@ export default function createActions(config) { recipientId: address, amount, fee: utils.calculateFee( - sentTxInfo.txInfo.ethTx.gas, + sentTxInfo.txInfo.ethTx.gasLimit, sentTxInfo.txInfo.ethTx.gasPrice ), status: 'PENDING', diff --git a/src/store/modules/eth/actions.js b/src/store/modules/eth/actions.js index 1909f45bb..fff3c881c 100644 --- a/src/store/modules/eth/actions.js +++ b/src/store/modules/eth/actions.js @@ -18,24 +18,25 @@ function storeEthAddress(context) { storeCryptoAddress(context.state.crypto, context.state.address) } -const initTransaction = (api, context, ethAddress, amount, increaseFee) => { +const initTransaction = async (api, context, ethAddress, amount, increaseFee) => { + const nonce = await api.getTransactionCount(context.state.address) + const gasPrice = await api.getGasPrice() + const transaction = { from: context.state.address, to: ethAddress, - value: utils.toWei(amount) - // gas: api.fromDecimal(DEFAULT_ETH_TRANSFER_GAS), // Don't take default value, instead calculate with estimateGas(transactionObject) - // gasPrice: context.getters.gasPrice // Set gas price to auto calc. Deprecated after London hardfork - // nonce // Let sendTransaction choose it + value: BigInt(utils.toWei(amount)), + // gasLimit: api.fromDecimal(DEFAULT_ETH_TRANSFER_GAS), // Don't take default value, instead calculate with estimateGas(transactionObject) + gasPrice, + nonce } - return api.estimateGas(transaction).then((gasLimit) => { - gasLimit = increaseFee - ? BigNumber(gasLimit).times(INCREASE_FEE_MULTIPLIER).toNumber() - : gasLimit + const defaultGasLimit = await api.estimateGas(transaction) + transaction.gasLimit = increaseFee + ? defaultGasLimit * BigInt(INCREASE_FEE_MULTIPLIER) + : defaultGasLimit - transaction.gas = gasLimit - return transaction - }) + return transaction } const parseTransaction = (context, tx) => { @@ -89,7 +90,7 @@ const createSpecificActions = (api) => ({ void api.getGasPrice().then((price) => { // It is OK with London hardfork context.commit('gasPrice', { - gasPrice: price, // string type + gasPrice: Number(price), // string type fee: +(+utils.calculateFee(DEFAULT_ETH_TRANSFER_GAS, price)).toFixed(8) // number type, in ETH }) }) From 946b3fe104b58995df69d7f05e83ce6e4c09d59d Mon Sep 17 00:00:00 2001 From: bludnic Date: Mon, 6 Nov 2023 16:17:14 +0000 Subject: [PATCH 2/6] refactor(vuex, cryptos): remove comments --- src/store/modules/erc20/erc20-actions.js | 1 - src/store/modules/eth/actions.js | 6 ++---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/store/modules/erc20/erc20-actions.js b/src/store/modules/erc20/erc20-actions.js index 36996aa77..176b42389 100644 --- a/src/store/modules/erc20/erc20-actions.js +++ b/src/store/modules/erc20/erc20-actions.js @@ -24,7 +24,6 @@ const initTransaction = async (api, context, ethAddress, amount, increaseFee) => from: context.state.address, to: context.state.contractAddress, value: '0x0', - // gasLimit: api.fromDecimal(DEFAULT_ERC20_TRANSFER_GAS), // Don't take default value, instead calculate with estimateGas(transactionObject) gasPrice, nonce, data: contract.methods diff --git a/src/store/modules/eth/actions.js b/src/store/modules/eth/actions.js index fff3c881c..487c14fb1 100644 --- a/src/store/modules/eth/actions.js +++ b/src/store/modules/eth/actions.js @@ -26,7 +26,6 @@ const initTransaction = async (api, context, ethAddress, amount, increaseFee) => from: context.state.address, to: ethAddress, value: BigInt(utils.toWei(amount)), - // gasLimit: api.fromDecimal(DEFAULT_ETH_TRANSFER_GAS), // Don't take default value, instead calculate with estimateGas(transactionObject) gasPrice, nonce } @@ -88,10 +87,9 @@ const createSpecificActions = (api) => ({ // Current gas price void api.getGasPrice().then((price) => { - // It is OK with London hardfork context.commit('gasPrice', { - gasPrice: Number(price), // string type - fee: +(+utils.calculateFee(DEFAULT_ETH_TRANSFER_GAS, price)).toFixed(8) // number type, in ETH + gasPrice: Number(price), + fee: +(+utils.calculateFee(DEFAULT_ETH_TRANSFER_GAS, price)).toFixed(8) }) }) From 2f27b9c38bde4b873b79dc35e23d7590ad9416e7 Mon Sep 17 00:00:00 2001 From: bludnic Date: Mon, 6 Nov 2023 16:28:38 +0000 Subject: [PATCH 3/6] feat(constants): change `INCREASE_FEE_MULTIPLIER` to 1.5 --- src/lib/constants/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/constants/index.js b/src/lib/constants/index.js index 58111f676..d23e8025a 100644 --- a/src/lib/constants/index.js +++ b/src/lib/constants/index.js @@ -107,7 +107,7 @@ export const DEFAULT_ETH_TRANSFER_GAS = CryptosInfo['ETH'].defaultGasLimit export const DEFAULT_ERC20_TRANSFER_GAS = DEFAULT_ETH_TRANSFER_GAS * 2.4 /** Increase fee multiplier. Used as a checkbox on SendFundsForm */ -export const INCREASE_FEE_MULTIPLIER = 2 +export const INCREASE_FEE_MULTIPLIER = 1.5 export { Cryptos, CryptosInfo, CryptosOrder } From da9a8c186f9c9dadbeff924ef49350284e1a6ddb Mon Sep 17 00:00:00 2001 From: bludnic Date: Mon, 6 Nov 2023 16:39:08 +0000 Subject: [PATCH 4/6] refactor(eth/erc20 store): rename `gasLimit` const --- src/store/modules/erc20/erc20-actions.js | 6 ++---- src/store/modules/eth/actions.js | 6 ++---- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/store/modules/erc20/erc20-actions.js b/src/store/modules/erc20/erc20-actions.js index 176b42389..8a56f362b 100644 --- a/src/store/modules/erc20/erc20-actions.js +++ b/src/store/modules/erc20/erc20-actions.js @@ -31,10 +31,8 @@ const initTransaction = async (api, context, ethAddress, amount, increaseFee) => .encodeABI() } - const defaultGasLimit = await api.estimateGas(transaction) - transaction.gasLimit = increaseFee - ? defaultGasLimit * BigInt(INCREASE_FEE_MULTIPLIER) - : defaultGasLimit + const gasLimit = await api.estimateGas(transaction) + transaction.gasLimit = increaseFee ? gasLimit * BigInt(INCREASE_FEE_MULTIPLIER) : gasLimit return transaction } diff --git a/src/store/modules/eth/actions.js b/src/store/modules/eth/actions.js index 487c14fb1..2966cb918 100644 --- a/src/store/modules/eth/actions.js +++ b/src/store/modules/eth/actions.js @@ -30,10 +30,8 @@ const initTransaction = async (api, context, ethAddress, amount, increaseFee) => nonce } - const defaultGasLimit = await api.estimateGas(transaction) - transaction.gasLimit = increaseFee - ? defaultGasLimit * BigInt(INCREASE_FEE_MULTIPLIER) - : defaultGasLimit + const gasLimit = await api.estimateGas(transaction) + transaction.gasLimit = increaseFee ? gasLimit * BigInt(INCREASE_FEE_MULTIPLIER) : gasLimit return transaction } From 3b539158763a3fcdeaecfefd1de105f4e4fc8744 Mon Sep 17 00:00:00 2001 From: bludnic Date: Mon, 6 Nov 2023 16:48:22 +0000 Subject: [PATCH 5/6] refactor(eth, erc20): rename "gas" to "gasLimit" --- src/lib/constants/index.d.ts | 4 ++-- src/lib/constants/index.js | 4 ++-- src/store/modules/erc20/erc20-getters.js | 4 ++-- src/store/modules/eth/actions.js | 3 +-- 4 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/lib/constants/index.d.ts b/src/lib/constants/index.d.ts index c8388ee40..55dc588e7 100644 --- a/src/lib/constants/index.d.ts +++ b/src/lib/constants/index.d.ts @@ -70,8 +70,8 @@ export declare const isTextDataAllowed: (crypto: CryptoSymbol) => boolean export declare const RE_LSK_ADDRESS_LEGACY: RegExp -export declare const DEFAULT_ETH_TRANSFER_GAS: number -export declare const DEFAULT_ERC20_TRANSFER_GAS: number +export declare const DEFAULT_ETH_TRANSFER_GAS_LIMIT: number +export declare const DEFAULT_ERC20_TRANSFER_GAS_LIMIT: number export declare const INCREASE_FEE_MULTIPLIER: number export { Cryptos, CryptosInfo, CryptosOrder } diff --git a/src/lib/constants/index.js b/src/lib/constants/index.js index d23e8025a..8fdc6210d 100644 --- a/src/lib/constants/index.js +++ b/src/lib/constants/index.js @@ -102,9 +102,9 @@ export const RE_LSK_ADDRESS_LEGACY = /^[0-9]{2,21}L$/ */ /** Gas limit value for the ETH transfers */ -export const DEFAULT_ETH_TRANSFER_GAS = CryptosInfo['ETH'].defaultGasLimit +export const DEFAULT_ETH_TRANSFER_GAS_LIMIT = CryptosInfo['ETH'].defaultGasLimit /** Gas limit value for the ERC-20 transfers */ -export const DEFAULT_ERC20_TRANSFER_GAS = DEFAULT_ETH_TRANSFER_GAS * 2.4 +export const DEFAULT_ERC20_TRANSFER_GAS_LIMIT = DEFAULT_ETH_TRANSFER_GAS_LIMIT * 2.4 /** Increase fee multiplier. Used as a checkbox on SendFundsForm */ export const INCREASE_FEE_MULTIPLIER = 1.5 diff --git a/src/store/modules/erc20/erc20-getters.js b/src/store/modules/erc20/erc20-getters.js index 28a63e72e..f3403c726 100644 --- a/src/store/modules/erc20/erc20-getters.js +++ b/src/store/modules/erc20/erc20-getters.js @@ -1,5 +1,5 @@ import baseGetters from '../eth-base/eth-base-getters' -import { DEFAULT_ERC20_TRANSFER_GAS } from '../../../lib/constants' +import { DEFAULT_ERC20_TRANSFER_GAS_LIMIT } from '../../../lib/constants' import { calculateFee } from '../../../lib/eth-utils' export default { @@ -7,7 +7,7 @@ export default { return rootGetters['eth/gasPrice'] }, - fee: (state, getters) => (amount) => calculateFee(DEFAULT_ERC20_TRANSFER_GAS, getters.gasPrice), + fee: (state, getters) => (amount) => calculateFee(DEFAULT_ERC20_TRANSFER_GAS_LIMIT, getters.gasPrice), ...baseGetters } diff --git a/src/store/modules/eth/actions.js b/src/store/modules/eth/actions.js index 2966cb918..026206f76 100644 --- a/src/store/modules/eth/actions.js +++ b/src/store/modules/eth/actions.js @@ -1,4 +1,3 @@ -import BigNumber from 'bignumber.js' import * as utils from '../../../lib/eth-utils' import createActions from '../eth-base/eth-base-actions' @@ -87,7 +86,7 @@ const createSpecificActions = (api) => ({ void api.getGasPrice().then((price) => { context.commit('gasPrice', { gasPrice: Number(price), - fee: +(+utils.calculateFee(DEFAULT_ETH_TRANSFER_GAS, price)).toFixed(8) + fee: +(+utils.calculateFee(DEFAULT_ETH_TRANSFER_GAS_LIMIT, price)).toFixed(8) }) }) From 26bf437fea51c762b0fbb98bc043dfc4c2d5bac0 Mon Sep 17 00:00:00 2001 From: bludnic Date: Mon, 6 Nov 2023 16:48:58 +0000 Subject: [PATCH 6/6] feat(eth, erc20): use DEFAULT_GAS_LIMIT if `api.estimateGas()` failed --- src/store/modules/erc20/erc20-actions.js | 10 ++++++++-- src/store/modules/eth/actions.js | 10 ++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/store/modules/erc20/erc20-actions.js b/src/store/modules/erc20/erc20-actions.js index 8a56f362b..ec10b17cc 100644 --- a/src/store/modules/erc20/erc20-actions.js +++ b/src/store/modules/erc20/erc20-actions.js @@ -1,5 +1,9 @@ import * as ethUtils from '../../../lib/eth-utils' -import { FetchStatus, INCREASE_FEE_MULTIPLIER } from '@/lib/constants' +import { + FetchStatus, + INCREASE_FEE_MULTIPLIER, + DEFAULT_ERC20_TRANSFER_GAS_LIMIT +} from '@/lib/constants' import EthContract from 'web3-eth-contract' import Erc20 from './erc20.abi.json' import createActions from '../eth-base/eth-base-actions' @@ -31,7 +35,9 @@ const initTransaction = async (api, context, ethAddress, amount, increaseFee) => .encodeABI() } - const gasLimit = await api.estimateGas(transaction) + const gasLimit = await api + .estimateGas(transaction) + .catch(() => BigInt(DEFAULT_ERC20_TRANSFER_GAS_LIMIT)) transaction.gasLimit = increaseFee ? gasLimit * BigInt(INCREASE_FEE_MULTIPLIER) : gasLimit return transaction diff --git a/src/store/modules/eth/actions.js b/src/store/modules/eth/actions.js index 026206f76..27284180e 100644 --- a/src/store/modules/eth/actions.js +++ b/src/store/modules/eth/actions.js @@ -1,7 +1,11 @@ import * as utils from '../../../lib/eth-utils' import createActions from '../eth-base/eth-base-actions' -import { DEFAULT_ETH_TRANSFER_GAS, FetchStatus, INCREASE_FEE_MULTIPLIER } from '@/lib/constants' +import { + DEFAULT_ETH_TRANSFER_GAS_LIMIT, + FetchStatus, + INCREASE_FEE_MULTIPLIER +} from '@/lib/constants' import { storeCryptoAddress } from '@/lib/store-crypto-address' /** Timestamp of the most recent status update */ @@ -29,7 +33,9 @@ const initTransaction = async (api, context, ethAddress, amount, increaseFee) => nonce } - const gasLimit = await api.estimateGas(transaction) + const gasLimit = await api + .estimateGas(transaction) + .catch(() => BigInt(DEFAULT_ETH_TRANSFER_GAS_LIMIT)) transaction.gasLimit = increaseFee ? gasLimit * BigInt(INCREASE_FEE_MULTIPLIER) : gasLimit return transaction