Skip to content

Commit

Permalink
fix: gas estimation (#10515)
Browse files Browse the repository at this point in the history
* refactor: AccountTransaction

* refactor: use memoize

* refactor: add comment

* refactor: typo
  • Loading branch information
guanbinrui committed Aug 20, 2023
1 parent cdc8e42 commit 520058a
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions packages/web3-shared/evm/src/libs/AccountTransaction.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import { identity, pickBy } from 'lodash-es'
import { BigNumber } from 'bignumber.js'
import { identity, pickBy, memoize } from 'lodash-es'
import { toHex } from 'web3-utils'
import { ZERO_ADDRESS } from '../constants/index.js'
import { isEmptyHex } from '../helpers/address.js'
import { ChainId, type Transaction } from '../types/index.js'

const normalizeHex = memoize((value: string | number) => {
// fix an abnormal hex value like: 0x02c68af0bb140000
if (typeof value === 'string' && value.length > 3 && value.startsWith('0x0'))
return toHex(new BigNumber(value).toFixed())
return toHex(value)
})

export class AccountTransaction {
constructor(private transaction?: Transaction) {}

Expand Down Expand Up @@ -47,12 +55,12 @@ export class AccountTransaction {
from,
to,
data,
value: value ? toHex(value) : undefined,
chainId: chainId && chainId !== ChainId.Astar ? toHex(chainId) : undefined,
gas: gas ? toHex(gas) : undefined,
gasPrice: gasPrice ? toHex(gasPrice) : undefined,
maxPriorityFeePerGas: maxPriorityFeePerGas ? toHex(maxPriorityFeePerGas) : undefined,
maxFeePerGas: maxFeePerGas ? toHex(maxFeePerGas) : undefined,
value: value ? normalizeHex(value) : undefined,
chainId: chainId && chainId !== ChainId.Astar ? normalizeHex(chainId) : undefined,
gas: gas ? normalizeHex(gas) : undefined,
gasPrice: gasPrice ? normalizeHex(gasPrice) : undefined,
maxPriorityFeePerGas: maxPriorityFeePerGas ? normalizeHex(maxPriorityFeePerGas) : undefined,
maxFeePerGas: maxFeePerGas ? normalizeHex(maxFeePerGas) : undefined,
nonce,
},
identity,
Expand Down

0 comments on commit 520058a

Please sign in to comment.