@exodus/ethereum-api 0.2.7 → 0.2.10

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": "0.2.7",
3
+ "version": "0.2.10",
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": "^0.2.4",
13
+ "@exodus/ethereum-lib": "^0.2.10",
14
14
  "fetchival": "0.3.3",
15
15
  "lodash": "^4.17.11",
16
16
  "make-concurrent": "4.0.0",
@@ -19,5 +19,5 @@
19
19
  "url-join": "4.0.0",
20
20
  "ws": "6.1.0"
21
21
  },
22
- "gitHead": "7b2e88a73e28ff83bafb6ba4695f3c24ecd20d21"
22
+ "gitHead": "13e2cc907ccda7585b96ae34d8745874406185bc"
23
23
  }
@@ -0,0 +1,99 @@
1
+ import BN from 'bn.js'
2
+ import * as ethUtil from 'ethereumjs-util'
3
+ import { currency2buffer, isEthereumToken, getBaseAsset } from '@exodus/ethereum-lib'
4
+ import { estimateGas, isContract as _isContract } from './with-fallback'
5
+
6
+ const EXTRA_PERCENTAGE = 20
7
+
8
+ export async function estimateGasLimit(
9
+ asset: Object,
10
+ fromAddress: string,
11
+ toAddress: string,
12
+ amount: Buffer | Object,
13
+ data: Buffer | string,
14
+ gasPrice: string,
15
+ extraPercentage: number = EXTRA_PERCENTAGE
16
+ ): number {
17
+ if (!Buffer.isBuffer(amount)) {
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,
30
+ from: fromAddress,
31
+ to: toAddress,
32
+ value: amount,
33
+ data: Buffer.isBuffer(data) ? ethUtil.bufferToHex(data) : data,
34
+ gasPrice,
35
+ })
36
+ return new BN(estimatedGas.slice(2), 16)
37
+ .imuln(100 + extraPercentage)
38
+ .idivn(100)
39
+ .toNumber()
40
+ }
41
+
42
+ export async function fetchGasLimit({
43
+ asset,
44
+ fromAddress,
45
+ toAddress,
46
+ txInput = '0x',
47
+ amount,
48
+ feeData,
49
+ bip70,
50
+ isContract,
51
+ throwOnError = true,
52
+ extraPercentage,
53
+ }) {
54
+ const isToken = isEthereumToken(asset)
55
+ if (isToken) {
56
+ txInput = ethUtil.bufferToHex(asset.contract.transfer.build(toAddress, currency2buffer(amount)))
57
+ amount = getBaseAsset(asset).currency.ZERO
58
+ toAddress = asset.contract.addresses.current
59
+ } else if (!isContract) {
60
+ if (isContract === undefined) isContract = await _isContract(asset.name, toAddress)
61
+ if (!isContract) return asset.gasLimit
62
+ }
63
+
64
+ if (!amount) {
65
+ amount = asset.currency.ZERO
66
+ }
67
+
68
+ let gasPrice = ethUtil.bufferToHex(currency2buffer(feeData.gasPrice))
69
+
70
+ if (bip70 && bip70.bitpay) {
71
+ if (bip70.bitpay.data) {
72
+ txInput = bip70.bitpay.data
73
+ }
74
+
75
+ if (bip70.bitpay.gasPrice) {
76
+ const bitPayGasPrice = ethUtil.bufferToHex(currency2buffer(bip70.bitpay.gasPrice))
77
+ if (new ethUtil.BN(bitPayGasPrice).gt(new ethUtil.BN(gasPrice))) {
78
+ gasPrice = bitPayGasPrice
79
+ }
80
+ }
81
+ }
82
+
83
+ try {
84
+ return await estimateGasLimit(
85
+ asset,
86
+ fromAddress,
87
+ toAddress,
88
+ amount,
89
+ txInput,
90
+ gasPrice,
91
+ extraPercentage
92
+ )
93
+ } catch (err) {
94
+ if (throwOnError) throw err
95
+ console.log('fetchGasLimit error', err)
96
+ }
97
+
98
+ return isToken ? asset.gasLimit : asset.contractGasLimit
99
+ }
package/src/index.js CHANGED
@@ -4,5 +4,6 @@ import * as etcWithFallback from './etc-with-fallback'
4
4
 
5
5
  export * from './with-fallback'
6
6
  export * from './fee-monitor'
7
+ export * from './gas-estimation'
7
8
  export * from './exodus-eth-server'
8
9
  export { etherscan, ethWithFallback, etcWithFallback }
@@ -48,9 +48,9 @@ export async function estimateGas({ asset, ...args }) {
48
48
  return _estimateGas(args)
49
49
  }
50
50
 
51
- const fetchEthereumBalance = async (address, contractAddress = 'value') => {
51
+ const fetchEthereumBalance = async (address, balanceField = 'value') => {
52
52
  const balances = await ethServer.getBalance(address)
53
- return get(balances, ['confirmed', contractAddress], '0')
53
+ return get(balances, ['confirmed', balanceField], '0')
54
54
  }
55
55
 
56
56
  const fetchEthereumClassicBalance = async (address) => {