@exodus/fiat-balances 4.0.0 → 6.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.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,28 @@
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.0.0](https://github.com/ExodusMovement/exodus-hydra/compare/@exodus/fiat-balances@5.0.0...@exodus/fiat-balances@6.0.0) (2023-04-28)
7
+
8
+ ### ⚠ BREAKING CHANGES
9
+
10
+ - fiat-balances load method (#1383)
11
+
12
+ ### Features
13
+
14
+ - fiat-balances load method ([#1383](https://github.com/ExodusMovement/exodus-hydra/issues/1383)) ([1adf3b7](https://github.com/ExodusMovement/exodus-hydra/commit/1adf3b78cc7865fa6a8da7eb87bdd7d1795a45ef))
15
+
16
+ ## [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)
17
+
18
+ ### ⚠ BREAKING CHANGES
19
+
20
+ - use atoms in fiat balances (#1355)
21
+ - replace emit with atom in balances module (#1349)
22
+
23
+ ### Features
24
+
25
+ - replace emit with atom in balances module ([#1349](https://github.com/ExodusMovement/exodus-hydra/issues/1349)) ([f9110f8](https://github.com/ExodusMovement/exodus-hydra/commit/f9110f8e9e76b8b199bc4d40461cb1bed3a5be1e))
26
+ - use atoms in fiat balances ([#1355](https://github.com/ExodusMovement/exodus-hydra/issues/1355)) ([3bbdb4d](https://github.com/ExodusMovement/exodus-hydra/commit/3bbdb4d0a956151d9e4895d2445cf8deee19dd09))
27
+
6
28
  ## [4.0.0](https://github.com/ExodusMovement/exodus-hydra/compare/@exodus/fiat-balances@3.0.0...@exodus/fiat-balances@4.0.0) (2023-02-02)
7
29
 
8
30
  ### ⚠ BREAKING CHANGES
@@ -0,0 +1,5 @@
1
+ import { createInMemoryAtom } from '@exodus/atoms'
2
+
3
+ const createFiatBalancesAtom = () => createInMemoryAtom()
4
+
5
+ export default createFiatBalancesAtom
package/atoms/index.js ADDED
@@ -0,0 +1,8 @@
1
+ import createFiatBalancesAtom from './fiat-balances'
2
+
3
+ export const fiatBalancesAtomDefinition = {
4
+ id: 'fiatBalancesAtom',
5
+ type: 'atom',
6
+ factory: createFiatBalancesAtom,
7
+ dependencies: [],
8
+ }
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())
@@ -16,6 +17,9 @@ class FiatBalances extends ExodusModule {
16
17
  #latestAssetBalances = null
17
18
  #enabledAssetsData = Object.create(null)
18
19
  #balances = Object.create(null)
20
+ #fiatBalancesAtom
21
+ #inputAtom
22
+ #balancesAtom
19
23
  // #balances.totals: Object,
20
24
  // #balances.byWalletAccount: Object,
21
25
  // #balances.byAssetSource: Object,
@@ -24,31 +28,34 @@ class FiatBalances extends ExodusModule {
24
28
  constructor({
25
29
  assetsModule,
26
30
  logger,
27
- balances,
31
+ balancesAtom,
28
32
  config: { balanceFields },
29
- ratesMonitor,
30
33
  enabledAssetsAtom,
34
+ fiatBalancesAtom,
35
+ ratesAtom,
36
+ currencyAtom,
31
37
  }) {
32
38
  super({ name: MODULE_ID, logger })
33
39
 
34
40
  this.#assetsModule = assetsModule
35
41
  this.#balanceFields = balanceFields
36
-
37
- balances.on('balances', this.#setBalances)
38
-
39
- ratesMonitor.on('rates', ({ rates, currency }) => {
40
- this._logger.log('recomputing based on new rates and/or fiat currency')
41
- this.#fiatCurrency = currencyFiat[currency]
42
- this.#cachedRates = rates
43
- this.#maybeRecompute()
42
+ this.#fiatBalancesAtom = fiatBalancesAtom
43
+ this.#balancesAtom = balancesAtom
44
+ this.#inputAtom = combine({
45
+ rates: ratesAtom,
46
+ enabledAssets: enabledAssetsAtom,
47
+ currency: currencyAtom,
44
48
  })
49
+ }
45
50
 
46
- // TODO: Move atom subscription to load method
47
- // eslint-disable-next-line hydra/module-side-effects
48
- enabledAssetsAtom.observe((enabledAssets) => {
51
+ load = () => {
52
+ this.#inputAtom.observe(({ currency, enabledAssets, rates }) => {
53
+ this.#fiatCurrency = currency
54
+ this.#cachedRates = rates
49
55
  this.#enabledAssetsData = enabledAssets
50
56
  this.#maybeRecompute()
51
57
  })
58
+ this.#balancesAtom.observe(this.#setBalances)
52
59
  }
53
60
 
54
61
  #isReady = () => {
@@ -56,20 +63,22 @@ class FiatBalances extends ExodusModule {
56
63
  this.#cachedRates &&
57
64
  this.#fiatCurrency &&
58
65
  this.#latestAssetBalances &&
59
- Object.keys(this.#enabledAssetsData).length > 0
66
+ Object.keys(this.#enabledAssetsData).length > 0 &&
67
+ this.#cachedRates[this.#fiatCurrency]
60
68
  )
61
69
  }
62
70
 
63
71
  // seems cheap, worth memoizing?
64
72
  #getCreateConversion = ({ assetName }) => {
65
- const fiatCurrency = this.#fiatCurrency
73
+ const fiatCurrency = fiatCurrencies[this.#fiatCurrency]
74
+ const rates = this.#cachedRates[this.#fiatCurrency]
66
75
  const asset = this.#assetsModule.getAsset(assetName)
67
76
  let fiatRate
68
77
  // HACK: Super hacky, terrible, the rates state object should have the prices split up by sub-objects per fiat
69
78
  if (fiatCurrency.defaultUnit.unitName === 'USD') {
70
- fiatRate = this.#cachedRates?.[asset.ticker]?.priceUSD || 0
79
+ fiatRate = rates?.[asset.ticker]?.priceUSD || 0
71
80
  } else {
72
- fiatRate = this.#cachedRates?.[asset.ticker]?.price || 0
81
+ fiatRate = rates?.[asset.ticker]?.price || 0
73
82
  }
74
83
 
75
84
  return conversionByRate(asset.currency, fiatCurrency, fiatRate)
@@ -89,10 +98,12 @@ class FiatBalances extends ExodusModule {
89
98
  const balances = this.#latestAssetBalances
90
99
  const { byAssetSource, byBaseAssetSource, byWalletAccount } = this.#balances
91
100
  const enabledAssets = this.#enabledAssetsData
101
+ const fiatZero = fiatCurrencies[this.#fiatCurrency].ZERO
102
+
92
103
  Object.keys(balances).forEach((walletAccount) => {
93
104
  const walletAccountBalances = balances[walletAccount]
94
105
  this.#balanceFields.forEach((field) => {
95
- let fieldTotal = this.#fiatCurrency.ZERO
106
+ let fieldTotal = fiatZero
96
107
  const entries = Object.entries(walletAccountBalances).filter(
97
108
  ([assetName, balances]) => enabledAssets[assetName] && balances
98
109
  )
@@ -102,12 +113,12 @@ class FiatBalances extends ExodusModule {
102
113
  if (!balance) return
103
114
 
104
115
  if (!byAssetSource?.[walletAccount]?.[assetName]?.[field]) {
105
- set(byAssetSource, [walletAccount, assetName, field], this.#fiatCurrency.ZERO)
116
+ set(byAssetSource, [walletAccount, assetName, field], fiatZero)
106
117
  }
107
118
 
108
119
  const baseAssetName = this.#assetsModule.getAsset(assetName).baseAsset.name
109
120
  if (!byBaseAssetSource?.[walletAccount]?.[baseAssetName]?.[field]) {
110
- set(byBaseAssetSource, [walletAccount, baseAssetName, field], this.#fiatCurrency.ZERO)
121
+ set(byBaseAssetSource, [walletAccount, baseAssetName, field], fiatZero)
111
122
  }
112
123
 
113
124
  const fiatValue = this.#convertToFiat({ assetName, value: balance })
@@ -144,6 +155,9 @@ class FiatBalances extends ExodusModule {
144
155
 
145
156
  this._logger.log('recomputing based on new balances')
146
157
 
158
+ const fiatZero = fiatCurrencies[this.#fiatCurrency].ZERO
159
+
160
+ // TODO set changes to fiatBalancesAtom as well
147
161
  const { byWalletAccount, byAssetSource, byBaseAssetSource } = this.#balances
148
162
  Object.entries(changes).forEach(([walletAccount, walletAccountBalancesChanges]) => {
149
163
  const walletAccountBalances = byWalletAccount[walletAccount]
@@ -155,7 +169,7 @@ class FiatBalances extends ExodusModule {
155
169
  if (!assetSourceBalancesChanges[field]) return
156
170
 
157
171
  const { from, to } = assetSourceBalancesChanges[field]
158
- walletAccountBalances[field] = walletAccountBalances[field] || this.#fiatCurrency.ZERO
172
+ walletAccountBalances[field] = walletAccountBalances[field] || fiatZero
159
173
 
160
174
  const fromFiatValue = this.#convertToFiat({ assetName, value: from })
161
175
  const toFiatValue = this.#convertToFiat({ assetName, value: to })
@@ -175,7 +189,7 @@ class FiatBalances extends ExodusModule {
175
189
  },
176
190
  ].forEach(({ byAssetSource, assetName }) => {
177
191
  if (!byAssetSource[walletAccount]?.[assetName]?.[field]) {
178
- set(byAssetSource, [walletAccount, assetName, field], this.#fiatCurrency.ZERO)
192
+ set(byAssetSource, [walletAccount, assetName, field], fiatZero)
179
193
  }
180
194
 
181
195
  // eslint-disable-next-line @exodus/mutable/no-param-reassign-prop-only
@@ -198,11 +212,11 @@ class FiatBalances extends ExodusModule {
198
212
  this.#balances.totals[field] = Object.entries(this.#balances.byWalletAccount).reduce(
199
213
  (total, [walletAccount, walletAccountBalances]) =>
200
214
  walletAccountBalances[field] ? total.add(walletAccountBalances[field]) : total,
201
- this.#fiatCurrency.ZERO
215
+ fiatCurrencies[this.#fiatCurrency].ZERO
202
216
  )
203
217
  })
204
218
 
205
- this.sync()
219
+ this.#flush()
206
220
  }
207
221
 
208
222
  #reset = () => {
@@ -218,9 +232,9 @@ class FiatBalances extends ExodusModule {
218
232
  })
219
233
  }
220
234
 
221
- sync = () => {
235
+ #flush = () => {
222
236
  this._logger.log('fiat balances updated')
223
- this.emit('fiat-balances', { balances: this.#balances })
237
+ this.#fiatBalancesAtom.set({ balances: this.#balances })
224
238
  }
225
239
  }
226
240
 
@@ -233,9 +247,11 @@ export default {
233
247
  dependencies: [
234
248
  'assetsModule',
235
249
  'logger',
236
- 'balances',
250
+ 'balancesAtom',
251
+ 'fiatBalancesAtom',
237
252
  'config',
238
- 'ratesMonitor',
239
253
  'enabledAssetsAtom',
254
+ 'ratesAtom',
255
+ 'currencyAtom',
240
256
  ],
241
257
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exodus/fiat-balances",
3
- "version": "4.0.0",
3
+ "version": "6.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",
@@ -31,11 +31,11 @@
31
31
  "lodash": "^4.17.21"
32
32
  },
33
33
  "devDependencies": {
34
- "@exodus/atoms": "^2.10.0",
35
- "@exodus/balances": "^4.0.0",
36
- "@exodus/basic-utils": "^1.0.0",
34
+ "@exodus/atoms": "^3.5.1",
35
+ "@exodus/balances": "^6.0.0",
36
+ "@exodus/basic-utils": "^2.0.0",
37
37
  "@exodus/blockchain-metadata": "*",
38
- "@exodus/enabled-assets": "^5.0.0",
38
+ "@exodus/enabled-assets": "^5.0.4",
39
39
  "@exodus/models": "^8.9.0",
40
40
  "@exodus/storage-memory": "^1.0.0",
41
41
  "@exodus/wallet-accounts": "*",
@@ -43,5 +43,5 @@
43
43
  "events": "^3.3.0",
44
44
  "jest": "^29.1.2"
45
45
  },
46
- "gitHead": "a72d79cc75c6c02876d745a498bf28b256d7f99e"
46
+ "gitHead": "961a45f96e3269a2389435e86ab00df5fe4ca695"
47
47
  }