@exodus/balances 1.2.0 → 1.2.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.
Files changed (2) hide show
  1. package/index.js +52 -33
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { isNumberUnit } from '@exodus/currency'
2
+ import { TxSet } from '@exodus/models'
2
3
  import ExodusModule from '@exodus/module'
3
4
  import { cloneDeepWith, difference, isEmpty, merge, set, zipObject } from 'lodash'
4
5
  import makeConcurrent from 'make-concurrent'
@@ -58,6 +59,7 @@ class Balances extends ExodusModule {
58
59
  }
59
60
 
60
61
  #getAsset = (assetName) => this.#assetsModule.getAsset(assetName)
62
+ #getAssets = () => this.#assetsModule.getAssets()
61
63
 
62
64
  #handleAccountStateUpdate = async (opts) => {
63
65
  if (!this.#ready) {
@@ -76,7 +78,8 @@ class Balances extends ExodusModule {
76
78
  })
77
79
  return
78
80
  }
79
- if (this.#isHistoryBasedBalanceAsset(opts.assetName)) return this.#updateAssetSource(opts)
81
+
82
+ return this.#updateAssetSource(opts)
80
83
  }
81
84
 
82
85
  #pushIntoSharedQueue = makeConcurrent((fn) => fn(), { concurrency: 1 })
@@ -89,30 +92,21 @@ class Balances extends ExodusModule {
89
92
 
90
93
  #getBalancesForAssetSource = async ({ walletAccount, assetName }) => {
91
94
  const metadata = await this.#loadBlockchainMetadataForBalance({ walletAccount, assetName })
92
- return this.#getBalancesFromBlockchainMetadata({ assetName, ...metadata })
95
+ return this.#getBalancesFromBlockchainMetadata({
96
+ assetNames: this.#getRelatedAssetNames(assetName),
97
+ ...metadata,
98
+ })
93
99
  }
94
100
 
95
- #getBalancesFromBlockchainMetadata = ({ assetName, accountState, txLog }) => {
101
+ #getBalancesFromBlockchainMetadata = ({ assetNames, accountState, txLogsByAssetName }) => {
96
102
  // in the future accountState may have all the balances and we won't need to rely on txLog
97
103
  // for now, accountState-based balance has both the baseAsset and token balances while txLog is per asset/token
98
104
  const getAssetBalances = (assetName) => {
99
105
  const asset = this.#getAsset(assetName)
106
+ const txLog = txLogsByAssetName[assetName]
100
107
  return asset.api.getBalances({ asset, accountState, txLog })
101
108
  }
102
109
 
103
- if (this.#isHistoryBasedBalanceAsset(assetName)) {
104
- const balances = getAssetBalances(assetName)
105
- return balances
106
- ? {
107
- [assetName]: balances,
108
- }
109
- : {}
110
- }
111
-
112
- const assetNames = [assetName, ...this.#assetsModule.getTokenNames(assetName)].filter(
113
- this.#isSupportedAsset
114
- )
115
-
116
110
  const balances = assetNames.map(getAssetBalances)
117
111
  return zipNonNull(assetNames, balances)
118
112
  }
@@ -160,23 +154,41 @@ class Balances extends ExodusModule {
160
154
  }
161
155
 
162
156
  #isSupportedAsset = (assetName) => !this.#getAsset(assetName).isCombined
163
- #getSupportedAssetNames = () =>
164
- Object.keys(this.#assetsModule.getAssets()).filter(this.#isSupportedAsset)
157
+ #getSupportedAssetNames = () => Object.keys(this.#getAssets()).filter(this.#isSupportedAsset)
165
158
 
166
- #isHistoryBasedBalanceAsset = (assetName) => {
167
- const asset = this.#getAsset(assetName)
168
- return asset.api?.hasFeature('historyBasedBalance')
169
- }
159
+ #getRelatedAssetNames = (assetName) =>
160
+ [assetName, ...this.#assetsModule.getTokenNames(assetName)].filter(this.#isSupportedAsset)
161
+
162
+ #loadBlockchainMetadataForBalance = async ({
163
+ assetName,
164
+ walletAccount,
165
+ accountState: providedAccountState = null,
166
+ txLogsByAssetName: providedTxLogsByAssetName = {},
167
+ }) => {
168
+ const [txLogsByAssetSource, accountState] = await Promise.all([
169
+ this.#blockchainMetadata.getLoadedTxLogs(),
170
+ providedAccountState
171
+ ? Promise.resolve(providedAccountState)
172
+ : this.#blockchainMetadata.getAccountState({
173
+ assetName: this.#getAsset(assetName).baseAsset.name,
174
+ walletAccount,
175
+ }),
176
+ ])
177
+
178
+ const assetNames = this.#getRelatedAssetNames(assetName)
179
+ const txLogsByAssetName = zipObject(
180
+ assetNames,
181
+ assetNames.map(
182
+ (assetName) =>
183
+ providedTxLogsByAssetName[assetName] ||
184
+ txLogsByAssetSource[walletAccount]?.[assetName] ||
185
+ TxSet.EMPTY
186
+ )
187
+ )
170
188
 
171
- #loadBlockchainMetadataForBalance = async ({ assetName, walletAccount, accountState, txLog }) => {
172
189
  return {
173
- accountState:
174
- accountState ||
175
- (await this.#blockchainMetadata.getAccountState({
176
- assetName: this.#getAsset(assetName).baseAsset.name,
177
- walletAccount,
178
- })),
179
- txLog: txLog || (await this.#blockchainMetadata.getTxLog({ assetName, walletAccount })),
190
+ accountState,
191
+ txLogsByAssetName,
180
192
  }
181
193
  }
182
194
 
@@ -193,15 +205,22 @@ class Balances extends ExodusModule {
193
205
  return
194
206
  }
195
207
 
196
- const { accountState, txLog } = await this.#loadBlockchainMetadataForBalance({
208
+ const { accountState, txLogsByAssetName } = await this.#loadBlockchainMetadataForBalance({
197
209
  assetName,
198
210
  walletAccount,
199
211
  accountState: providedAccountState,
200
- txLog: providedTxLog,
212
+ txLogsByAssetName: {
213
+ [assetName]: providedTxLog,
214
+ },
201
215
  })
202
216
 
203
217
  const accountBalances = this.#balances[walletAccount]
204
- const newBalances = this.#getBalancesFromBlockchainMetadata({ assetName, accountState, txLog })
218
+ const newBalances = this.#getBalancesFromBlockchainMetadata({
219
+ assetNames: this.#getRelatedAssetNames(assetName),
220
+ accountState,
221
+ txLogsByAssetName,
222
+ })
223
+
205
224
  const changes = Object.create(null)
206
225
  for (let subAssetName in newBalances) {
207
226
  const { currency } = this.#getAsset(subAssetName)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exodus/balances",
3
- "version": "1.2.0",
3
+ "version": "1.2.1",
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": "e99b34358ce40fe70ce2a9cb09c622582d0503c9"
34
+ "gitHead": "03c583ee3f8e75c54871f0da68dd15dd336da09b"
35
35
  }