@exodus/balances 1.2.1 → 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.
@@ -0,0 +1,19 @@
1
+ import { TxSet } from '@exodus/models'
2
+
3
+ const isUnconfirmedTx = (tx) => tx.pending && !tx.error
4
+
5
+ const getTxSetBalance = (txSet) => txSet.getMutations().slice(-1)[0].balance
6
+
7
+ const getUnconfirmedTotal = ({ assets, asset, txLog }) => {
8
+ const unconfirmedTxs = [...txLog].filter(isUnconfirmedTx)
9
+ return unconfirmedTxs.length === 0
10
+ ? asset.currency.ZERO
11
+ : getTxSetBalance(TxSet.fromArray(unconfirmedTxs, { assets }))
12
+ }
13
+
14
+ const getConfirmedBalance = ({ assets, asset, txLog, balance }) => {
15
+ const unconfirmedTotal = getUnconfirmedTotal({ assets, asset, txLog })
16
+ return balance.sub(unconfirmedTotal)
17
+ }
18
+
19
+ export default getConfirmedBalance
package/index.js CHANGED
@@ -3,6 +3,7 @@ import { TxSet } from '@exodus/models'
3
3
  import ExodusModule from '@exodus/module'
4
4
  import { cloneDeepWith, difference, isEmpty, merge, set, zipObject } from 'lodash'
5
5
  import makeConcurrent from 'make-concurrent'
6
+ import getConfirmedBalance from './confirmed-balance'
6
7
 
7
8
  const cloneBalances = (balances) =>
8
9
  cloneDeepWith(balances, (val) => {
@@ -45,12 +46,13 @@ class Balances extends ExodusModule {
45
46
  this.#handleTxLogUpdate = shareQueue(this.#handleTxLogUpdate)
46
47
  this.#recompute = shareQueue(this.#recompute)
47
48
 
48
- blockchainMetadata.on('loaded', () => {
49
- walletAccountsAtom.observe((payload) =>
49
+ blockchainMetadata.once('load-wallet-accounts', () => {
50
+ walletAccountsAtom.observe((payload) => {
50
51
  this.#setWalletAccounts(
51
52
  Object.keys(payload).filter((walletAccount) => payload[walletAccount].enabled)
52
53
  )
53
- )
54
+ })
55
+
54
56
  blockchainMetadata.on('account-state', this.#handleAccountStateUpdate)
55
57
  blockchainMetadata.on('tx-log', this.#handleTxLogUpdate)
56
58
  })
@@ -104,7 +106,33 @@ class Balances extends ExodusModule {
104
106
  const getAssetBalances = (assetName) => {
105
107
  const asset = this.#getAsset(assetName)
106
108
  const txLog = txLogsByAssetName[assetName]
107
- return asset.api.getBalances({ asset, accountState, txLog })
109
+ const balances = asset.api.getBalances({ asset, accountState, txLog })
110
+ if (!balances || isEmpty(balances)) return null
111
+
112
+ if (balances.balance && this.#balanceFields.includes('confirmedBalance')) {
113
+ balances.confirmedBalance = getConfirmedBalance({
114
+ assets: this.#getAssets(),
115
+ asset,
116
+ txLog,
117
+ balance: balances.balance,
118
+ })
119
+ }
120
+
121
+ const negativeBalanceFields = Object.keys(balances).filter(
122
+ (field) => balances[field]?.isNegative
123
+ )
124
+
125
+ if (negativeBalanceFields.length > 0) {
126
+ this._logger.warn('unexpected negative balance', {
127
+ assetName,
128
+ accountState,
129
+ txLogsByAssetName,
130
+ balances,
131
+ negativeBalanceFields,
132
+ })
133
+ }
134
+
135
+ return balances
108
136
  }
109
137
 
110
138
  const balances = assetNames.map(getAssetBalances)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exodus/balances",
3
- "version": "1.2.1",
3
+ "version": "2.0.1",
4
4
  "description": "balances",
5
5
  "author": "Exodus Movement Inc",
6
6
  "license": "UNLICENSED",
@@ -10,7 +10,8 @@
10
10
  "lint:fix": "yarn lint --fix"
11
11
  },
12
12
  "files": [
13
- "index.js"
13
+ "index.js",
14
+ "confirmed-balance.js"
14
15
  ],
15
16
  "repository": {
16
17
  "type": "git",
@@ -26,10 +27,15 @@
26
27
  "lodash": "^4.17.21",
27
28
  "make-concurrent": "^4.0.0"
28
29
  },
30
+ "peerDependencies": {
31
+ "@exodus/blockchain-metadata": "^3.0.0"
32
+ },
29
33
  "devDependencies": {
30
- "@exodus/atoms": "^1.0.0",
34
+ "@exodus/atoms": "^2.3.0",
31
35
  "@exodus/basic-utils": "^1.0.0",
32
- "@exodus/models": "^8.9.0"
36
+ "@exodus/blockchain-metadata": "*",
37
+ "@exodus/models": "^8.9.0",
38
+ "@exodus/storage-memory": "*"
33
39
  },
34
- "gitHead": "03c583ee3f8e75c54871f0da68dd15dd336da09b"
40
+ "gitHead": "9ef26663a0f2415380d7bebc4c97acfbe8be35f0"
35
41
  }