@exodus/fiat-balances 2.0.1 → 3.0.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 (2) hide show
  1. package/index.js +14 -10
  2. package/package.json +4 -4
package/index.js CHANGED
@@ -1,7 +1,10 @@
1
1
  import { conversionByRate } from '@exodus/currency'
2
2
  import currencyFiat from '@exodus/fiat-currencies'
3
3
  import ExodusModule from '@exodus/module'
4
- import { difference, set } from 'lodash'
4
+ import { isEqual, set } from 'lodash'
5
+
6
+ // not super efficient, so we're assuming small objects, like { [walletAccount]: xyz }
7
+ const areKeysSame = (a, b) => isEqual(Object.keys(a).sort(), Object.keys(b).sort())
5
8
 
6
9
  class FiatBalances extends ExodusModule {
7
10
  #assetsModule
@@ -114,21 +117,19 @@ class FiatBalances extends ExodusModule {
114
117
  }
115
118
 
116
119
  #setBalances = ({ balances, oldBalances, changes }) => {
117
- const needsRecompute = !changes || this.#latestAssetBalances !== oldBalances
120
+ const needsRecompute =
121
+ !changes ||
122
+ this.#latestAssetBalances !== oldBalances ||
123
+ // adding/removing walletAccounts is fairly rare, recompute to reduce code complexity
124
+ !oldBalances ||
125
+ !areKeysSame(oldBalances, balances)
126
+
118
127
  this.#latestAssetBalances = balances
119
128
  if (!this.#isReady()) return
120
129
 
121
130
  if (needsRecompute) return this.#recompute()
122
131
 
123
132
  this._logger.log('recomputing based on new balances')
124
- const walletAccountsRemoved = difference(
125
- Object.keys(balances),
126
- Object.keys(oldBalances || balances)
127
- )
128
-
129
- walletAccountsRemoved.forEach((walletAccount) => {
130
- delete this.#balances.byWalletAccount[walletAccount]
131
- })
132
133
 
133
134
  const { byWalletAccount, byAssetSource, byBaseAssetSource } = this.#balances
134
135
  Object.entries(changes).forEach(([walletAccount, walletAccountBalancesChanges]) => {
@@ -137,6 +138,9 @@ class FiatBalances extends ExodusModule {
137
138
  ([assetName, assetSourceBalancesChanges]) => {
138
139
  const baseAssetName = this.#assetsModule.getAsset(assetName).baseAsset.name
139
140
  this.#balanceFields.forEach((field) => {
141
+ // we may not be interested in all fields from `balances`
142
+ if (!assetSourceBalancesChanges[field]) return
143
+
140
144
  const { from, to } = assetSourceBalancesChanges[field]
141
145
  walletAccountBalances[field] = walletAccountBalances[field] || this.#fiatCurrency.ZERO
142
146
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exodus/fiat-balances",
3
- "version": "2.0.1",
3
+ "version": "3.0.0",
4
4
  "description": "module to track fiat balances based on changes in crypto balances and fiat currency",
5
5
  "author": "Exodus Movement Inc",
6
6
  "license": "UNLICENSED",
@@ -28,12 +28,12 @@
28
28
  },
29
29
  "devDependencies": {
30
30
  "@exodus/atoms": "^2.3.0",
31
- "@exodus/balances": "^2.0.0",
31
+ "@exodus/balances": "^3.0.0",
32
32
  "@exodus/basic-utils": "^1.0.0",
33
- "@exodus/blockchain-metadata": "^3.0.0",
33
+ "@exodus/blockchain-metadata": "^4.0.0",
34
34
  "@exodus/enabled-assets": "^4.0.0",
35
35
  "@exodus/models": "^8.9.0",
36
36
  "@exodus/storage-memory": "^1.0.0"
37
37
  },
38
- "gitHead": "d03a2b5e5e85e7ffdd3720b22eee00e49f591824"
38
+ "gitHead": "47c8cb54d8cbbdfe978221fd421a6c269fbd9547"
39
39
  }