@exodus/balances 1.1.0 → 1.1.2

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 +27 -8
  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
@@ -43,12 +43,10 @@ class Balances extends ExodusModule {
43
43
  this.#updateAssetSource = shareQueue(this.#updateAssetSource)
44
44
  this.#recompute = shareQueue(this.#recompute)
45
45
 
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)
46
+ blockchainMetadata.on('loaded', () => {
47
+ this.#recompute()
48
+ blockchainMetadata.on('account-state', this.#handleAccountStateUpdate)
49
+ blockchainMetadata.on('tx-log', this.#handleTxLogUpdate)
52
50
  })
53
51
 
54
52
  walletAccountsAtom.observe((payload) =>
@@ -62,6 +60,26 @@ class Balances extends ExodusModule {
62
60
 
63
61
  #getAsset = (assetName) => this.#assetsModule.getAsset(assetName)
64
62
 
63
+ #handleAccountStateUpdate = async (opts) => {
64
+ if (!this.#ready) {
65
+ this._logger.log(`ignore update from account-state event.`, {
66
+ isReady: this.#ready,
67
+ })
68
+ return
69
+ }
70
+ return this.#updateAssetSource(opts)
71
+ }
72
+
73
+ #handleTxLogUpdate = (opts) => {
74
+ if (!this.#ready) {
75
+ this._logger.log(`ignore update from tx-log event.`, {
76
+ isReady: this.#ready,
77
+ })
78
+ return
79
+ }
80
+ if (this.#isHistoryBasedBalanceAsset(opts.assetName)) return this.#updateAssetSource(opts)
81
+ }
82
+
65
83
  #pushIntoSharedQueue = makeConcurrent((fn) => fn(), { concurrency: 1 })
66
84
 
67
85
  #reset = ({ walletAccounts = this.#walletAccounts } = {}) => {
@@ -219,13 +237,14 @@ class Balances extends ExodusModule {
219
237
  this._logger.log('balances updated')
220
238
 
221
239
  const oldBalances = this.#oldBalances
240
+ const balances = cloneBalances(this.#balances)
222
241
  this.emit('balances', {
223
- balances: this.#balances,
242
+ balances,
224
243
  oldBalances,
225
244
  changes,
226
245
  })
227
246
 
228
- this.#oldBalances = cloneBalances(this.#balances)
247
+ this.#oldBalances = balances
229
248
  }
230
249
  }
231
250
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exodus/balances",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
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": "8dc3595d4bd5750bdf649fafef7fd5d9dd345824"
34
+ "gitHead": "d23a92b5962eedd5874f0362a6983b7c4b8a3ce4"
35
35
  }