@exodus/fiat-balances 5.0.0 → 6.1.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.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,22 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [6.1.0](https://github.com/ExodusMovement/exodus-hydra/compare/@exodus/fiat-balances@6.0.0...@exodus/fiat-balances@6.1.0) (2023-05-01)
7
+
8
+ ### Features
9
+
10
+ - add optimisticFiatBalancesAtom definition ([#1448](https://github.com/ExodusMovement/exodus-hydra/issues/1448)) ([10f936e](https://github.com/ExodusMovement/exodus-hydra/commit/10f936e1e35cc88f3481691a5187a309f120201f))
11
+
12
+ ## [6.0.0](https://github.com/ExodusMovement/exodus-hydra/compare/@exodus/fiat-balances@5.0.0...@exodus/fiat-balances@6.0.0) (2023-04-28)
13
+
14
+ ### ⚠ BREAKING CHANGES
15
+
16
+ - fiat-balances load method (#1383)
17
+
18
+ ### Features
19
+
20
+ - fiat-balances load method ([#1383](https://github.com/ExodusMovement/exodus-hydra/issues/1383)) ([1adf3b7](https://github.com/ExodusMovement/exodus-hydra/commit/1adf3b78cc7865fa6a8da7eb87bdd7d1795a45ef))
21
+
6
22
  ## [5.0.0](https://github.com/ExodusMovement/exodus-hydra/compare/@exodus/fiat-balances@4.0.0...@exodus/fiat-balances@5.0.0) (2023-04-25)
7
23
 
8
24
  ### ⚠ BREAKING CHANGES
package/atoms/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import createFiatBalancesAtom from './fiat-balances'
2
+ import createOptimisticFiatBalancesAtom from './optimistic-fiat-balances'
2
3
 
3
4
  export const fiatBalancesAtomDefinition = {
4
5
  id: 'fiatBalancesAtom',
@@ -6,3 +7,10 @@ export const fiatBalancesAtomDefinition = {
6
7
  factory: createFiatBalancesAtom,
7
8
  dependencies: [],
8
9
  }
10
+
11
+ export const optimisticFiatBalancesAtomDefinition = {
12
+ id: 'optimisticFiatBalancesAtom',
13
+ type: 'atom',
14
+ factory: createOptimisticFiatBalancesAtom,
15
+ dependencies: [],
16
+ }
@@ -0,0 +1,5 @@
1
+ import { createInMemoryAtom } from '@exodus/atoms'
2
+
3
+ const createOptimisticFiatBalancesAtom = () => createInMemoryAtom()
4
+
5
+ export default createOptimisticFiatBalancesAtom
package/module/index.js CHANGED
@@ -1,7 +1,8 @@
1
1
  import { conversionByRate } from '@exodus/currency'
2
- import currencyFiat from '@exodus/fiat-currencies'
3
2
  import ExodusModule from '@exodus/module'
4
3
  import { isEqual, set } from 'lodash'
4
+ import fiatCurrencies from '@exodus/fiat-currencies'
5
+ import { combine } from '@exodus/atoms'
5
6
 
6
7
  // not super efficient, so we're assuming small objects, like { [walletAccount]: xyz }
7
8
  const areKeysSame = (a, b) => isEqual(Object.keys(a).sort(), Object.keys(b).sort())
@@ -17,6 +18,8 @@ class FiatBalances extends ExodusModule {
17
18
  #enabledAssetsData = Object.create(null)
18
19
  #balances = Object.create(null)
19
20
  #fiatBalancesAtom
21
+ #inputAtom
22
+ #balancesAtom
20
23
  // #balances.totals: Object,
21
24
  // #balances.byWalletAccount: Object,
22
25
  // #balances.byAssetSource: Object,
@@ -27,32 +30,32 @@ class FiatBalances extends ExodusModule {
27
30
  logger,
28
31
  balancesAtom,
29
32
  config: { balanceFields },
30
- ratesMonitor,
31
33
  enabledAssetsAtom,
32
34
  fiatBalancesAtom,
35
+ ratesAtom,
36
+ currencyAtom,
33
37
  }) {
34
38
  super({ name: MODULE_ID, logger })
35
39
 
36
40
  this.#assetsModule = assetsModule
37
41
  this.#balanceFields = balanceFields
38
42
  this.#fiatBalancesAtom = fiatBalancesAtom
39
-
40
- // eslint-disable-next-line hydra/module-side-effects
41
- balancesAtom.observe(this.#setBalances)
42
-
43
- ratesMonitor.on('rates', ({ rates, currency }) => {
44
- this._logger.log('recomputing based on new rates and/or fiat currency')
45
- this.#fiatCurrency = currencyFiat[currency]
46
- this.#cachedRates = rates
47
- this.#maybeRecompute()
43
+ this.#balancesAtom = balancesAtom
44
+ this.#inputAtom = combine({
45
+ rates: ratesAtom,
46
+ enabledAssets: enabledAssetsAtom,
47
+ currency: currencyAtom,
48
48
  })
49
+ }
49
50
 
50
- // TODO: Move atom subscription to load method
51
- // eslint-disable-next-line hydra/module-side-effects
52
- enabledAssetsAtom.observe((enabledAssets) => {
51
+ load = () => {
52
+ this.#inputAtom.observe(({ currency, enabledAssets, rates }) => {
53
+ this.#fiatCurrency = currency
54
+ this.#cachedRates = rates
53
55
  this.#enabledAssetsData = enabledAssets
54
56
  this.#maybeRecompute()
55
57
  })
58
+ this.#balancesAtom.observe(this.#setBalances)
56
59
  }
57
60
 
58
61
  #isReady = () => {
@@ -60,20 +63,22 @@ class FiatBalances extends ExodusModule {
60
63
  this.#cachedRates &&
61
64
  this.#fiatCurrency &&
62
65
  this.#latestAssetBalances &&
63
- Object.keys(this.#enabledAssetsData).length > 0
66
+ Object.keys(this.#enabledAssetsData).length > 0 &&
67
+ this.#cachedRates[this.#fiatCurrency]
64
68
  )
65
69
  }
66
70
 
67
71
  // seems cheap, worth memoizing?
68
72
  #getCreateConversion = ({ assetName }) => {
69
- const fiatCurrency = this.#fiatCurrency
73
+ const fiatCurrency = fiatCurrencies[this.#fiatCurrency]
74
+ const rates = this.#cachedRates[this.#fiatCurrency]
70
75
  const asset = this.#assetsModule.getAsset(assetName)
71
76
  let fiatRate
72
77
  // HACK: Super hacky, terrible, the rates state object should have the prices split up by sub-objects per fiat
73
78
  if (fiatCurrency.defaultUnit.unitName === 'USD') {
74
- fiatRate = this.#cachedRates?.[asset.ticker]?.priceUSD || 0
79
+ fiatRate = rates?.[asset.ticker]?.priceUSD || 0
75
80
  } else {
76
- fiatRate = this.#cachedRates?.[asset.ticker]?.price || 0
81
+ fiatRate = rates?.[asset.ticker]?.price || 0
77
82
  }
78
83
 
79
84
  return conversionByRate(asset.currency, fiatCurrency, fiatRate)
@@ -93,10 +98,12 @@ class FiatBalances extends ExodusModule {
93
98
  const balances = this.#latestAssetBalances
94
99
  const { byAssetSource, byBaseAssetSource, byWalletAccount } = this.#balances
95
100
  const enabledAssets = this.#enabledAssetsData
101
+ const fiatZero = fiatCurrencies[this.#fiatCurrency].ZERO
102
+
96
103
  Object.keys(balances).forEach((walletAccount) => {
97
104
  const walletAccountBalances = balances[walletAccount]
98
105
  this.#balanceFields.forEach((field) => {
99
- let fieldTotal = this.#fiatCurrency.ZERO
106
+ let fieldTotal = fiatZero
100
107
  const entries = Object.entries(walletAccountBalances).filter(
101
108
  ([assetName, balances]) => enabledAssets[assetName] && balances
102
109
  )
@@ -106,12 +113,12 @@ class FiatBalances extends ExodusModule {
106
113
  if (!balance) return
107
114
 
108
115
  if (!byAssetSource?.[walletAccount]?.[assetName]?.[field]) {
109
- set(byAssetSource, [walletAccount, assetName, field], this.#fiatCurrency.ZERO)
116
+ set(byAssetSource, [walletAccount, assetName, field], fiatZero)
110
117
  }
111
118
 
112
119
  const baseAssetName = this.#assetsModule.getAsset(assetName).baseAsset.name
113
120
  if (!byBaseAssetSource?.[walletAccount]?.[baseAssetName]?.[field]) {
114
- set(byBaseAssetSource, [walletAccount, baseAssetName, field], this.#fiatCurrency.ZERO)
121
+ set(byBaseAssetSource, [walletAccount, baseAssetName, field], fiatZero)
115
122
  }
116
123
 
117
124
  const fiatValue = this.#convertToFiat({ assetName, value: balance })
@@ -148,6 +155,8 @@ class FiatBalances extends ExodusModule {
148
155
 
149
156
  this._logger.log('recomputing based on new balances')
150
157
 
158
+ const fiatZero = fiatCurrencies[this.#fiatCurrency].ZERO
159
+
151
160
  // TODO set changes to fiatBalancesAtom as well
152
161
  const { byWalletAccount, byAssetSource, byBaseAssetSource } = this.#balances
153
162
  Object.entries(changes).forEach(([walletAccount, walletAccountBalancesChanges]) => {
@@ -160,7 +169,7 @@ class FiatBalances extends ExodusModule {
160
169
  if (!assetSourceBalancesChanges[field]) return
161
170
 
162
171
  const { from, to } = assetSourceBalancesChanges[field]
163
- walletAccountBalances[field] = walletAccountBalances[field] || this.#fiatCurrency.ZERO
172
+ walletAccountBalances[field] = walletAccountBalances[field] || fiatZero
164
173
 
165
174
  const fromFiatValue = this.#convertToFiat({ assetName, value: from })
166
175
  const toFiatValue = this.#convertToFiat({ assetName, value: to })
@@ -180,7 +189,7 @@ class FiatBalances extends ExodusModule {
180
189
  },
181
190
  ].forEach(({ byAssetSource, assetName }) => {
182
191
  if (!byAssetSource[walletAccount]?.[assetName]?.[field]) {
183
- set(byAssetSource, [walletAccount, assetName, field], this.#fiatCurrency.ZERO)
192
+ set(byAssetSource, [walletAccount, assetName, field], fiatZero)
184
193
  }
185
194
 
186
195
  // eslint-disable-next-line @exodus/mutable/no-param-reassign-prop-only
@@ -203,7 +212,7 @@ class FiatBalances extends ExodusModule {
203
212
  this.#balances.totals[field] = Object.entries(this.#balances.byWalletAccount).reduce(
204
213
  (total, [walletAccount, walletAccountBalances]) =>
205
214
  walletAccountBalances[field] ? total.add(walletAccountBalances[field]) : total,
206
- this.#fiatCurrency.ZERO
215
+ fiatCurrencies[this.#fiatCurrency].ZERO
207
216
  )
208
217
  })
209
218
 
@@ -241,7 +250,8 @@ export default {
241
250
  'balancesAtom',
242
251
  'fiatBalancesAtom',
243
252
  'config',
244
- 'ratesMonitor',
245
253
  'enabledAssetsAtom',
254
+ 'ratesAtom',
255
+ 'currencyAtom',
246
256
  ],
247
257
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exodus/fiat-balances",
3
- "version": "5.0.0",
3
+ "version": "6.1.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",
@@ -31,8 +31,8 @@
31
31
  "lodash": "^4.17.21"
32
32
  },
33
33
  "devDependencies": {
34
- "@exodus/atoms": "^3.5.0",
35
- "@exodus/balances": "^5.0.0",
34
+ "@exodus/atoms": "^3.5.1",
35
+ "@exodus/balances": "^6.0.0",
36
36
  "@exodus/basic-utils": "^2.0.0",
37
37
  "@exodus/blockchain-metadata": "*",
38
38
  "@exodus/enabled-assets": "^5.0.4",
@@ -43,5 +43,5 @@
43
43
  "events": "^3.3.0",
44
44
  "jest": "^29.1.2"
45
45
  },
46
- "gitHead": "990b8c6a59d2145d55fd43bd622bd8770c710ab2"
46
+ "gitHead": "f89f1e233b114cbc947887e818a0ad23a1f28dc7"
47
47
  }