@exodus/balances 1.1.1 → 1.2.0

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.
Files changed (3) hide show
  1. package/README.md +57 -0
  2. package/index.js +33 -14
  3. package/package.json +2 -2
package/README.md ADDED
@@ -0,0 +1,57 @@
1
+ # @exodus/balances
2
+
3
+ This module tracks balances across assets and portfolios. It delegates to assets for any asset-specifics.
4
+
5
+ ## WARNING: no asset specifics!
6
+
7
+ If you introduce any asset specifics like `if (assetName === 'dogicorn') balance = balance.mul(2)`, you will be fired immediately. This is a safety measure to save you from @feri42's wrath. All asset-specifics belong in `asset.api.getBalances` or other APIs inside the asset libraries.
8
+
9
+ ## Install
10
+
11
+ ```sh
12
+ yarn add @exodus/balances
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ```js
18
+ import createBalances from '@exodus/balances'
19
+
20
+ const balances = createBalances({
21
+ // see #/assets-module in exodus-browser or tests for the API
22
+ assetsModule,
23
+ // pushes data of the form `{ [walletAccount]: WalletAccount }`
24
+ walletAccountsAtom,
25
+ // @exodus/blockchain-metadata instance
26
+ blockchainMetadata,
27
+ // the types of balances supported may change. For now it's just the total balance
28
+ balanceFields: ['balance'],
29
+ // see ../modules/module/src/logger.js for API
30
+ logger,
31
+ })
32
+
33
+ balances.on('balances', ({ balances, oldBalances, changes }) => {
34
+ // `balances`, `oldBalances`
35
+ // {
36
+ // [walletAccount]: {
37
+ // [assetName]: {
38
+ // [balanceField]: NumberUnit
39
+ // }
40
+ // }
41
+ // }
42
+ //
43
+ // `changes`
44
+ // {
45
+ // [walletAccount]: {
46
+ // [assetName]: {
47
+ // [balanceField]: {
48
+ // from: NumberUnit,
49
+ // to: NumberUnit,
50
+ // }
51
+ // }
52
+ // }
53
+ // }
54
+ })
55
+
56
+ balances.sync() // forces 'balances' event emission
57
+ ```
package/index.js CHANGED
@@ -41,27 +41,44 @@ class Balances extends ExodusModule {
41
41
 
42
42
  // these share a queue
43
43
  this.#updateAssetSource = shareQueue(this.#updateAssetSource)
44
+ this.#handleTxLogUpdate = shareQueue(this.#handleTxLogUpdate)
44
45
  this.#recompute = shareQueue(this.#recompute)
45
46
 
46
- blockchainMetadata.on('account-state', async (opts) => {
47
- return this.#updateAssetSource(opts)
48
- })
49
-
50
- blockchainMetadata.on('tx-log', async (opts) => {
51
- if (this.#isHistoryBasedBalanceAsset(opts.assetName)) return this.#updateAssetSource(opts)
52
- })
53
-
54
- walletAccountsAtom.observe((payload) =>
55
- this.#setWalletAccounts(
56
- Object.keys(payload).filter((walletAccount) => payload[walletAccount].enabled)
47
+ blockchainMetadata.on('loaded', () => {
48
+ walletAccountsAtom.observe((payload) =>
49
+ this.#setWalletAccounts(
50
+ Object.keys(payload).filter((walletAccount) => payload[walletAccount].enabled)
51
+ )
57
52
  )
58
- )
53
+ blockchainMetadata.on('account-state', this.#handleAccountStateUpdate)
54
+ blockchainMetadata.on('tx-log', this.#handleTxLogUpdate)
55
+ })
59
56
 
60
57
  assetsModule.on('assets-add', this.#onAssetsChanged)
61
58
  }
62
59
 
63
60
  #getAsset = (assetName) => this.#assetsModule.getAsset(assetName)
64
61
 
62
+ #handleAccountStateUpdate = async (opts) => {
63
+ if (!this.#ready) {
64
+ this._logger.log(`ignore update from account-state event.`, {
65
+ isReady: this.#ready,
66
+ })
67
+ return
68
+ }
69
+ return this.#updateAssetSource(opts)
70
+ }
71
+
72
+ #handleTxLogUpdate = (opts) => {
73
+ if (!this.#ready) {
74
+ this._logger.log(`ignore update from tx-log event.`, {
75
+ isReady: this.#ready,
76
+ })
77
+ return
78
+ }
79
+ if (this.#isHistoryBasedBalanceAsset(opts.assetName)) return this.#updateAssetSource(opts)
80
+ }
81
+
65
82
  #pushIntoSharedQueue = makeConcurrent((fn) => fn(), { concurrency: 1 })
66
83
 
67
84
  #reset = ({ walletAccounts = this.#walletAccounts } = {}) => {
@@ -216,10 +233,12 @@ class Balances extends ExodusModule {
216
233
  }
217
234
 
218
235
  sync = ({ changes } = {}) => {
219
- this._logger.log('balances updated')
220
-
221
236
  const oldBalances = this.#oldBalances
222
237
  const balances = cloneBalances(this.#balances)
238
+
239
+ if (Object.keys(balances).length === 0) return // wait till `load` finished
240
+
241
+ this._logger.log('balances updated')
223
242
  this.emit('balances', {
224
243
  balances,
225
244
  oldBalances,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exodus/balances",
3
- "version": "1.1.1",
3
+ "version": "1.2.0",
4
4
  "description": "balances",
5
5
  "author": "Exodus Movement Inc",
6
6
  "license": "UNLICENSED",
@@ -31,5 +31,5 @@
31
31
  "@exodus/basic-utils": "^1.0.0",
32
32
  "@exodus/models": "^8.9.0"
33
33
  },
34
- "gitHead": "cbca7f075af27af557d498dc6dc74f61909434ad"
34
+ "gitHead": "e99b34358ce40fe70ce2a9cb09c622582d0503c9"
35
35
  }