@exodus/ethereum-api 2.25.1 → 2.25.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exodus/ethereum-api",
3
- "version": "2.25.1",
3
+ "version": "2.25.2",
4
4
  "description": "Ethereum Api",
5
5
  "main": "src/index.js",
6
6
  "files": [
@@ -34,5 +34,5 @@
34
34
  "devDependencies": {
35
35
  "@exodus/models": "^8.7.2"
36
36
  },
37
- "gitHead": "26582c94b07259d510c483a753fb0f696edf3957"
37
+ "gitHead": "0466bafed6bfbfb5198758df301a1a163266cb34"
38
38
  }
@@ -5,6 +5,11 @@ import { estimateGas, isContractAddress } from './eth-like-util'
5
5
 
6
6
  const EXTRA_PERCENTAGE = 20
7
7
 
8
+ // 16 gas per non-zero byte (4 gas per zero byte) of "transaction input data"
9
+ const GAS_PER_NON_ZERO_BYTE = 16
10
+
11
+ export const DEFAULT_CONTRACT_GAS_LIMIT = 1e6
12
+
8
13
  // Starting with geth v1.9.14, if gasPrice is set for eth_estimateGas call, the call allowance will
9
14
  // be calculated with account's balance divided by gasPrice. If user's balance is too low,
10
15
  // the gasEstimation will fail. If gasPrice is set to '0x0', the account's balance is not
@@ -17,7 +22,7 @@ export async function estimateGasLimit(
17
22
  data: Buffer | string,
18
23
  gasPrice?: string = '0x',
19
24
  extraPercentage?: number = EXTRA_PERCENTAGE
20
- ): number {
25
+ ): Promise<number> {
21
26
  const opts = {
22
27
  from: fromAddress,
23
28
  to: toAddress,
@@ -51,6 +56,9 @@ export async function fetchGasLimit({
51
56
  if (!amount) amount = asset.currency.ZERO
52
57
  if (!feeData.gasPrice) feeData.gasPrice = asset.baseAsset.currency.ZERO
53
58
 
59
+ const defaultGasLimit = () =>
60
+ asset.gasLimit + GAS_PER_NON_ZERO_BYTE * ethUtil.toBuffer(txInput).length
61
+
54
62
  const _isToken = isToken(asset)
55
63
  if (_isToken) {
56
64
  txInput = ethUtil.bufferToHex(
@@ -61,7 +69,7 @@ export async function fetchGasLimit({
61
69
  } else if (!isContract) {
62
70
  if (isContract === undefined)
63
71
  isContract = await isContractAddress({ asset, address: toAddress })
64
- if (!isContract) return asset.gasLimit
72
+ if (!isContract) return defaultGasLimit()
65
73
  }
66
74
 
67
75
  const gasPrice = ethUtil.bufferToHex(currency2buffer(feeData.gasPrice))
@@ -78,10 +86,14 @@ export async function fetchGasLimit({
78
86
  )
79
87
  } catch (err) {
80
88
  if (throwOnError) throw err
81
- console.log('fetchGasLimit error', err)
82
- }
89
+ console.error('fetchGasLimit error', err)
90
+
91
+ // fallback value for contract case
92
+ if (isContract) return asset.contractGasLimit || DEFAULT_CONTRACT_GAS_LIMIT
83
93
 
84
- return _isToken ? asset.gasLimit : asset.contractGasLimit
94
+ // fallback value for rest cases: token.
95
+ return defaultGasLimit()
96
+ }
85
97
  }
86
98
 
87
99
  function normalizeAmount(amount) {