@exodus/ethereum-api 0.2.6 → 0.2.8

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.6",
3
+ "version": "0.2.8",
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.8",
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": "cb562d91bae2c526403fb66ab8b05127a8f1d673"
22
+ "gitHead": "fc53854a8e60f021526fa8b6f4ac7fb5ccb0a5a4"
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 } from './with-fallback'
5
+
6
+ const EXTRA_PERCENTAGE = 20
7
+
8
+ 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 asset.address.isContract(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 }
@@ -1,5 +1,4 @@
1
1
  import { get } from 'lodash'
2
- import { _isEthereumToken } from '@exodus/ethereum-lib'
3
2
  import { eth as ethServer, etc as etcServer } from './exodus-eth-server'
4
3
  import * as etherscan from './etherscan'
5
4
 
@@ -19,9 +18,12 @@ export function withFallback(fn, fn2) {
19
18
  }
20
19
 
21
20
  export async function isContract(assetName, address) {
22
- const server =
23
- assetName === 'ethereumclassic' ? etcServer : withFallback(ethServer.getCode, etherscan.getCode)
24
- const code = await server.getCode(address)
21
+ const _getCode =
22
+ assetName === 'ethereumclassic'
23
+ ? withFallback(etcServer.getCode, etcServer.getCode)
24
+ : withFallback(ethServer.getCode, etherscan.getCode)
25
+
26
+ const code = await _getCode(address)
25
27
  return code.length > 2
26
28
  }
27
29
 
@@ -29,48 +31,57 @@ export async function getNonce({ asset, address }) {
29
31
  if (!['ethereum', 'ethereumclassic'].includes(asset.name)) return
30
32
 
31
33
  const _getNonce =
32
- asset.name === 'ethereum'
33
- ? withFallback(ethServer.getTransactionCount, etherscan.getTransactionCount)
34
- : etcServer.getTransactionCount
34
+ asset.name === 'ethereumclassic'
35
+ ? withFallback(etcServer.getTransactionCount, etcServer.getTransactionCount)
36
+ : withFallback(ethServer.getTransactionCount, etherscan.getTransactionCount)
35
37
 
36
38
  const nonce = await _getNonce(address)
37
39
  return parseInt(nonce, 16)
38
40
  }
39
41
 
40
42
  export async function estimateGas({ asset, ...args }) {
41
- const useEthServer: boolean = asset.name === 'ethereum' || _isEthereumToken(asset)
42
- return (useEthServer
43
- ? withFallback(ethServer.estimateGas, etherscan.estimateGas)
44
- : etcServer.estimateGas)(args)
43
+ const _estimateGas =
44
+ asset.name === 'ethereumclassic'
45
+ ? withFallback(etcServer.estimateGas, etcServer.estimateGas)
46
+ : withFallback(ethServer.estimateGas, etherscan.estimateGas)
47
+
48
+ return _estimateGas(args)
45
49
  }
46
50
 
47
- export async function getBalance({ asset, address }) {
48
- const fetchEthereumBalance = withFallback(async (address) => {
49
- const balances = await ethServer.getBalance(address)
50
- return get(balances, 'confirmed.value', '0')
51
- }, etherscan.fetchBalance)
51
+ const fetchEthereumBalance = async (address, balanceField = 'value') => {
52
+ const balances = await ethServer.getBalance(address)
53
+ return get(balances, ['confirmed', balanceField], '0')
54
+ }
52
55
 
53
- const fetchEthereumClassicBalance = async (address) => {
54
- const balances = await etcServer.getBalance(address)
55
- return get(balances, 'confirmed.value', '0')
56
- }
56
+ const fetchEthereumClassicBalance = async (address) => {
57
+ const balances = await etcServer.getBalance(address)
58
+ return get(balances, 'confirmed.value', '0')
59
+ }
60
+
61
+ // Only Ethereum and Ethereumclassic, not ERC20
62
+ export async function getBalance({ asset, address }) {
63
+ const _getBalance =
64
+ asset.name === 'ethereumclassic'
65
+ ? withFallback(fetchEthereumClassicBalance, fetchEthereumClassicBalance)
66
+ : withFallback(fetchEthereumBalance, etherscan.fetchBalance)
57
67
 
58
- return (asset.name === 'ethereum' ? fetchEthereumBalance : fetchEthereumClassicBalance)(address)
68
+ return _getBalance(address)
59
69
  }
60
70
 
61
71
  // Only Ethereum ERC20, not Ethereumclassic
62
72
  export async function getTokenBalance({ asset, address }) {
63
- const contractAddress = asset.contract.addresses.current.toLowerCase()
73
+ const _getTokenBalance = withFallback(
74
+ async (contractAddress, address) => fetchEthereumBalance(address, contractAddress),
75
+ etherscan.tokenBalance
76
+ )
64
77
 
65
- return withFallback(async (contractAddress, address) => {
66
- const balances = await ethServer.getBalance(address)
67
- return get(balances, ['confirmed', contractAddress], '0')
68
- }, etherscan.tokenBalance)(contractAddress, address)
78
+ const contractAddress = asset.contract.addresses.current.toLowerCase()
79
+ return _getTokenBalance(contractAddress, address)
69
80
  }
70
81
 
71
82
  // Returns function for supplied asset
72
83
  export function sendRawTransaction(asset) {
73
- return asset.name === 'ethereum' || _isEthereumToken(asset)
74
- ? withFallback(ethServer.sendRawTransaction, etherscan.sendRawTransaction)
75
- : etcServer.sendRawTransaction
84
+ return asset.name === 'ethereumclassic'
85
+ ? withFallback(etcServer.sendRawTransaction, etcServer.sendRawTransaction)
86
+ : withFallback(ethServer.sendRawTransaction, etherscan.sendRawTransaction)
76
87
  }