@exodus/ethereum-api 2.0.0 → 2.0.1
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 +3 -3
- package/src/gas-estimation.js +30 -18
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@exodus/ethereum-api",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"description": "Ethereum Api",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"author": "Exodus Movement, Inc.",
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"access": "restricted"
|
|
11
11
|
},
|
|
12
12
|
"dependencies": {
|
|
13
|
-
"@exodus/ethereum-lib": "^2.0.
|
|
13
|
+
"@exodus/ethereum-lib": "^2.0.1",
|
|
14
14
|
"@exodus/simple-retry": "^0.0.6",
|
|
15
15
|
"fetchival": "0.3.3",
|
|
16
16
|
"lodash": "^4.17.11",
|
|
@@ -20,5 +20,5 @@
|
|
|
20
20
|
"url-join": "4.0.0",
|
|
21
21
|
"ws": "6.1.0"
|
|
22
22
|
},
|
|
23
|
-
"gitHead": "
|
|
23
|
+
"gitHead": "7708ea7f4a1a441c910143dece98fe712048670f"
|
|
24
24
|
}
|
package/src/gas-estimation.js
CHANGED
|
@@ -5,34 +5,28 @@ import { estimateGas, isContract as _isContract } from './eth-like-util'
|
|
|
5
5
|
|
|
6
6
|
const EXTRA_PERCENTAGE = 20
|
|
7
7
|
|
|
8
|
+
// Starting with geth v1.9.14, if gasPrice is set for eth_estimateGas call, the call allowance will
|
|
9
|
+
// be calculated with account's balance divided by gasPrice. If user's balance is too low,
|
|
10
|
+
// the gasEstimation will fail. If gasPrice is set to '0x0', the account's balance is not
|
|
11
|
+
// used to estimate gas.
|
|
8
12
|
export async function estimateGasLimit(
|
|
9
13
|
asset: Object,
|
|
10
14
|
fromAddress: string,
|
|
11
15
|
toAddress: string,
|
|
12
16
|
amount: Buffer | Object,
|
|
13
17
|
data: Buffer | string,
|
|
14
|
-
gasPrice
|
|
15
|
-
extraPercentage
|
|
18
|
+
gasPrice?: string = '0x',
|
|
19
|
+
extraPercentage?: number = EXTRA_PERCENTAGE
|
|
16
20
|
): number {
|
|
17
|
-
|
|
18
|
-
amount = currency2buffer(amount)
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
amount = ethUtil.bufferToHex(amount)
|
|
22
|
-
while (amount[2] === '0') amount = '0x' + amount.slice(3)
|
|
23
|
-
if (amount === '0x') amount = '0x0'
|
|
24
|
-
|
|
25
|
-
while (gasPrice[2] === '0') gasPrice = '0x' + gasPrice.slice(3)
|
|
26
|
-
if (gasPrice === '0x') gasPrice = '0x0'
|
|
27
|
-
|
|
28
|
-
const estimatedGas = await estimateGas({
|
|
29
|
-
asset,
|
|
21
|
+
const opts = {
|
|
30
22
|
from: fromAddress,
|
|
31
23
|
to: toAddress,
|
|
32
|
-
value: amount,
|
|
24
|
+
value: normalizeAmount(amount),
|
|
33
25
|
data: Buffer.isBuffer(data) ? ethUtil.bufferToHex(data) : data,
|
|
34
|
-
gasPrice,
|
|
35
|
-
}
|
|
26
|
+
gasPrice: normalizeGasPrice(gasPrice),
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const estimatedGas = await estimateGas({ asset, ...opts })
|
|
36
30
|
return new BN(estimatedGas.slice(2), 16)
|
|
37
31
|
.imuln(100 + extraPercentage)
|
|
38
32
|
.idivn(100)
|
|
@@ -97,3 +91,21 @@ export async function fetchGasLimit({
|
|
|
97
91
|
|
|
98
92
|
return isToken ? asset.gasLimit : asset.contractGasLimit
|
|
99
93
|
}
|
|
94
|
+
|
|
95
|
+
function normalizeAmount(amount) {
|
|
96
|
+
if (!Buffer.isBuffer(amount)) {
|
|
97
|
+
amount = currency2buffer(amount)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
amount = ethUtil.bufferToHex(amount)
|
|
101
|
+
while (amount[2] === '0') amount = '0x' + amount.slice(3)
|
|
102
|
+
if (amount === '0x') amount = '0x0'
|
|
103
|
+
|
|
104
|
+
return amount
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function normalizeGasPrice(gasPrice) {
|
|
108
|
+
while (gasPrice[2] === '0') gasPrice = '0x' + gasPrice.slice(3)
|
|
109
|
+
if (gasPrice === '0x') gasPrice = '0x0'
|
|
110
|
+
return gasPrice
|
|
111
|
+
}
|