@exodus/ethereum-api 6.3.29 → 6.3.30
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/get-balances.js +27 -13
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@exodus/ethereum-api",
|
|
3
|
-
"version": "6.3.
|
|
3
|
+
"version": "6.3.30",
|
|
4
4
|
"description": "Ethereum Api",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"files": [
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"dependencies": {
|
|
17
17
|
"@exodus/asset-lib": "^3.7.1",
|
|
18
18
|
"@exodus/crypto": "^1.0.0-rc.0",
|
|
19
|
-
"@exodus/ethereum-lib": "^
|
|
19
|
+
"@exodus/ethereum-lib": "^4.0.0",
|
|
20
20
|
"@exodus/ethereumjs-util": "^7.1.0-exodus.6",
|
|
21
21
|
"@exodus/fetch": "^1.3.0-beta.4",
|
|
22
22
|
"@exodus/simple-retry": "^0.0.6",
|
|
@@ -34,5 +34,5 @@
|
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"@exodus/models": "^8.10.4"
|
|
36
36
|
},
|
|
37
|
-
"gitHead": "
|
|
37
|
+
"gitHead": "44bf590d6ecef38d828b466687ad579c7528ea84"
|
|
38
38
|
}
|
package/src/get-balances.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { isRpcBalanceAsset } from '@exodus/ethereum-lib'
|
|
2
|
+
import { get } from 'lodash'
|
|
2
3
|
|
|
3
4
|
const fixBalance = ({ txLog, balance }) => {
|
|
4
5
|
for (const tx of txLog) {
|
|
@@ -14,25 +15,38 @@ const fixBalance = ({ txLog, balance }) => {
|
|
|
14
15
|
return balance
|
|
15
16
|
}
|
|
16
17
|
|
|
18
|
+
const getBalanceFromAccountState = ({ asset, accountState }) => {
|
|
19
|
+
const isBase = asset.name === asset.baseAsset.name
|
|
20
|
+
return get(
|
|
21
|
+
accountState,
|
|
22
|
+
isBase ? ['balance'] : ['tokenBalances', asset.name],
|
|
23
|
+
asset.currency.ZERO
|
|
24
|
+
)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const getBalanceFromTxLog = ({ txLog, asset }) => {
|
|
28
|
+
return txLog.size > 0 ? txLog.getMutations().slice(-1)[0].balance : asset.currency.ZERO
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const getBalance = ({ asset, accountState, txLog }) => {
|
|
32
|
+
const balance = isRpcBalanceAsset(asset)
|
|
33
|
+
? getBalanceFromAccountState({ asset, accountState })
|
|
34
|
+
: getBalanceFromTxLog({ txLog, asset })
|
|
35
|
+
|
|
36
|
+
const shouldFixBalance = isRpcBalanceAsset(asset)
|
|
37
|
+
|
|
38
|
+
return shouldFixBalance ? fixBalance({ txLog, balance }) : balance
|
|
39
|
+
}
|
|
40
|
+
|
|
17
41
|
/**
|
|
18
42
|
* Api method to return the balance based on either account state balances or tx history.
|
|
19
43
|
*
|
|
20
44
|
* @param asset the asset to get the balances
|
|
21
45
|
* @param txLog the txLog when the balance is transaction based
|
|
22
46
|
* @param accountState the account state when the balance is loaded from RPC
|
|
23
|
-
* @returns {{balance}|null} an object with the balance or null if the balance is unknown
|
|
47
|
+
* @returns {{balance}|null} an object with the balance or null if the balance is unknown
|
|
24
48
|
*/
|
|
25
49
|
export const getBalances = ({ asset, txLog, accountState }) => {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
asset.baseAsset.name === asset.name
|
|
29
|
-
? accountState?.balance
|
|
30
|
-
: accountState?.tokenBalances?.[asset.name]
|
|
31
|
-
return balance && !balance.isZero ? { balance: fixBalance({ txLog, balance }) } : null
|
|
32
|
-
}
|
|
33
|
-
return txLog.size
|
|
34
|
-
? {
|
|
35
|
-
balance: txLog.getMutations().slice(-1)[0].balance,
|
|
36
|
-
}
|
|
37
|
-
: null
|
|
50
|
+
const balance = getBalance({ asset, accountState, txLog })
|
|
51
|
+
return { balance }
|
|
38
52
|
}
|