Skip to content

Commit

Permalink
Merge pull request #550 from Adamant-im/fix/eth-transfer-doesnt-after…
Browse files Browse the repository at this point in the history
…-web3-eth-upgrade

fix(ETH, ERC20): transfer funds doesn't work after `web3-eth` upgrade
  • Loading branch information
bludnic authored Nov 11, 2023
2 parents 488bd25 + 400727d commit 2d07d9e
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 36 deletions.
4 changes: 2 additions & 2 deletions src/lib/constants/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
6 changes: 3 additions & 3 deletions src/lib/constants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ 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 = 2
export const INCREASE_FEE_MULTIPLIER = 1.5

export { Cryptos, CryptosInfo, CryptosOrder }

Expand Down
27 changes: 17 additions & 10 deletions src/store/modules/erc20/erc20-actions.js
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -14,26 +18,29 @@ 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 gasLimit = await api
.estimateGas(transaction)
.catch(() => BigInt(DEFAULT_ERC20_TRANSFER_GAS_LIMIT))
transaction.gasLimit = increaseFee ? gasLimit * BigInt(INCREASE_FEE_MULTIPLIER) : gasLimit

return transaction
}

const parseTransaction = (context, tx) => {
Expand Down
4 changes: 2 additions & 2 deletions src/store/modules/erc20/erc20-getters.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
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 {
gasPrice(state, getters, rootState, rootGetters) {
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
}
7 changes: 5 additions & 2 deletions src/store/modules/eth-base/eth-base-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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',
Expand Down
36 changes: 19 additions & 17 deletions src/store/modules/eth/actions.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import BigNumber from 'bignumber.js'
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 */
Expand All @@ -18,24 +21,24 @@ 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)),
gasPrice,
nonce
}

return api.estimateGas(transaction).then((gasLimit) => {
gasLimit = increaseFee
? BigNumber(gasLimit).times(INCREASE_FEE_MULTIPLIER).toNumber()
: gasLimit
const gasLimit = await api
.estimateGas(transaction)
.catch(() => BigInt(DEFAULT_ETH_TRANSFER_GAS_LIMIT))
transaction.gasLimit = increaseFee ? gasLimit * BigInt(INCREASE_FEE_MULTIPLIER) : gasLimit

transaction.gas = gasLimit
return transaction
})
return transaction
}

const parseTransaction = (context, tx) => {
Expand Down Expand Up @@ -87,10 +90,9 @@ const createSpecificActions = (api) => ({

// Current gas price
void api.getGasPrice().then((price) => {
// It is OK with London hardfork
context.commit('gasPrice', {
gasPrice: 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_LIMIT, price)).toFixed(8)
})
})

Expand Down

0 comments on commit 2d07d9e

Please sign in to comment.