@exodus/ethereum-api 0.2.6 → 0.2.7

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.7",
4
4
  "description": "Ethereum Api",
5
5
  "main": "src/index.js",
6
6
  "author": "Exodus Movement, Inc.",
@@ -19,5 +19,5 @@
19
19
  "url-join": "4.0.0",
20
20
  "ws": "6.1.0"
21
21
  },
22
- "gitHead": "cb562d91bae2c526403fb66ab8b05127a8f1d673"
22
+ "gitHead": "7b2e88a73e28ff83bafb6ba4695f3c24ecd20d21"
23
23
  }
@@ -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, contractAddress = 'value') => {
52
+ const balances = await ethServer.getBalance(address)
53
+ return get(balances, ['confirmed', contractAddress], '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
  }