@exodus/fiat-balances 9.1.2 → 10.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,29 @@
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
+ ## [10.1.0](https://github.com/ExodusMovement/exodus-hydra/compare/@exodus/fiat-balances@10.0.0...@exodus/fiat-balances@10.1.0) (2023-12-14)
7
+
8
+ ### Features
9
+
10
+ - remove storing oldBalances in balances atoms ([#5073](https://github.com/ExodusMovement/exodus-hydra/issues/5073)) ([414db86](https://github.com/ExodusMovement/exodus-hydra/commit/414db866a86aaf3242e68698596af187da6927c3))
11
+
12
+ ## [10.0.0](https://github.com/ExodusMovement/exodus-hydra/compare/@exodus/fiat-balances@9.1.2...@exodus/fiat-balances@10.0.0) (2023-12-12)
13
+
14
+ ### ⚠ BREAKING CHANGES
15
+
16
+ - mv `trackNonDustAssetsNamesPlugin` to fiat-balances (#4988)
17
+ - pass max wallet account amount to redux module (#4968)
18
+
19
+ ### Features
20
+
21
+ - **fiat-balances:** add by base asset source selector ([#4695](https://github.com/ExodusMovement/exodus-hydra/issues/4695)) ([47a10a0](https://github.com/ExodusMovement/exodus-hydra/commit/47a10a07c5d24e25e838d9c4f6a6063c7364886f))
22
+ - mv `trackNonDustAssetsNamesPlugin` to fiat-balances ([#4988](https://github.com/ExodusMovement/exodus-hydra/issues/4988)) ([270d109](https://github.com/ExodusMovement/exodus-hydra/commit/270d109b0198a5b705059b12a77dff726b3314cc))
23
+ - pass max wallet account amount to redux module ([#4968](https://github.com/ExodusMovement/exodus-hydra/issues/4968)) ([e6ab869](https://github.com/ExodusMovement/exodus-hydra/commit/e6ab8696da1ae56921ca4faef2b964096c005280))
24
+
25
+ ### Bug Fixes
26
+
27
+ - cleanup subscriptions on stop ([#4814](https://github.com/ExodusMovement/exodus-hydra/issues/4814)) ([d053582](https://github.com/ExodusMovement/exodus-hydra/commit/d0535826c2023dd4d3273b367bbcc5cca6e4bb95))
28
+
6
29
  ## [9.1.2](https://github.com/ExodusMovement/exodus-hydra/compare/@exodus/fiat-balances@9.1.1...@exodus/fiat-balances@9.1.2) (2023-10-20)
7
30
 
8
31
  ### Bug Fixes
@@ -1,5 +1,6 @@
1
1
  import { createInMemoryAtom } from '@exodus/atoms'
2
2
 
3
+ // eslint-disable-next-line @exodus/hydra/in-memory-atom-default-value
3
4
  const createFiatBalancesAtom = () => createInMemoryAtom()
4
5
 
5
6
  export default createFiatBalancesAtom
@@ -1,5 +1,6 @@
1
1
  import { createInMemoryAtom } from '@exodus/atoms'
2
2
 
3
+ // eslint-disable-next-line @exodus/hydra/in-memory-atom-default-value
3
4
  const createOptimisticFiatBalancesAtom = () => createInMemoryAtom()
4
5
 
5
6
  export default createOptimisticFiatBalancesAtom
package/index.js CHANGED
@@ -5,16 +5,25 @@ import {
5
5
  optimisticFiatBalancesAtomDefinition,
6
6
  } from './atoms'
7
7
  import fiatBalancesPluginDefinition from './plugin'
8
+ import trackNonDustAssetsNamesPluginDefinition from './plugin/track-non-dust-asset-names'
8
9
 
9
- const fiatBalances = ({ optimistic }) => ({
10
+ /**
11
+ * @param optimistic {boolean}
12
+ * @param nonDustAssets {object}
13
+ * @param nonDustAssets.balanceThresholdsUsd {object}
14
+ * @param nonDustAssets.balanceThresholdByChainUsd {object}
15
+ * @param nonDustAssets.defaultBalanceThresholdUsd {import('@exodus/fiat-currencies').Unit}
16
+ */
17
+
18
+ const fiatBalances = ({ optimistic, nonDustAssets }) => ({
10
19
  id: 'fiatBalances',
11
20
  definitions: [
12
21
  {
13
22
  definition: fiatBalancesDefinition,
14
- writesAtoms: ['fiatBalancesAtom'],
15
23
  },
16
24
  { definition: fiatBalancesAtomDefinition },
17
25
  { definition: fiatBalancesPluginDefinition },
26
+ { definition: trackNonDustAssetsNamesPluginDefinition, config: nonDustAssets },
18
27
  { definition: nonDustBalanceAssetNamesAtomDefinition },
19
28
  {
20
29
  if: optimistic,
@@ -36,7 +45,6 @@ const fiatBalances = ({ optimistic }) => ({
36
45
  interfaceId: 'fiatBalancesAtom',
37
46
  },
38
47
  ],
39
- writesAtoms: ['fiatBalancesAtom'],
40
48
  },
41
49
  ],
42
50
  })
@@ -28,6 +28,7 @@ class FiatBalances extends ExodusModule {
28
28
  // #balances.byWalletAccount: Object,
29
29
  // #balances.byAssetSource: Object,
30
30
  // #balances.byBaseAssetSource: Object,
31
+ #subscriptions = []
31
32
 
32
33
  constructor({
33
34
  assetsModule,
@@ -51,12 +52,14 @@ class FiatBalances extends ExodusModule {
51
52
  }
52
53
 
53
54
  load = () => {
54
- this.#inputAtom.observe(({ currency, rates }) => {
55
- this.#fiatCurrency = currency
56
- this.#cachedRates = rates
57
- this.#maybeRecompute()
58
- })
59
- this.#balancesAtom.observe(this.#setBalances)
55
+ this.#subscriptions.push(
56
+ this.#inputAtom.observe(({ currency, rates }) => {
57
+ this.#fiatCurrency = currency
58
+ this.#cachedRates = rates
59
+ this.#maybeRecompute()
60
+ }),
61
+ this.#balancesAtom.observe(this.#setBalances)
62
+ )
60
63
  }
61
64
 
62
65
  #isReady = () => {
@@ -137,13 +140,11 @@ class FiatBalances extends ExodusModule {
137
140
  this.#recomputeWalletTotals()
138
141
  }
139
142
 
140
- #setBalances = ({ balances, oldBalances, changes }) => {
143
+ #setBalances = ({ balances, changes }) => {
141
144
  const needsRecompute =
142
145
  !changes ||
143
- this.#latestAssetBalances !== oldBalances ||
144
146
  // adding/removing walletAccounts is fairly rare, recompute to reduce code complexity
145
- !oldBalances ||
146
- !areKeysSame(oldBalances, balances)
147
+ (this.#latestAssetBalances && !areKeysSame(this.#latestAssetBalances, balances))
147
148
 
148
149
  this.#latestAssetBalances = balances
149
150
  if (!this.#isReady()) return
@@ -240,6 +241,11 @@ class FiatBalances extends ExodusModule {
240
241
  const data = cloneBalances({ balances: this.#balances })
241
242
  this.#fiatBalancesAtom.set((current) => (isEqual(current, data) ? current : data))
242
243
  }
244
+
245
+ stop = () => {
246
+ this.#subscriptions.forEach((unsubscribe) => unsubscribe())
247
+ this.#subscriptions = []
248
+ }
243
249
  }
244
250
 
245
251
  const createFiatBalances = (...args) => new FiatBalances(...args)
package/module/index.js CHANGED
@@ -1 +1 @@
1
- export default from './fiat-balances'
1
+ export { default } from './fiat-balances'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exodus/fiat-balances",
3
- "version": "9.1.2",
3
+ "version": "10.1.0",
4
4
  "description": "Track asset and portfolio fiat balances based on changes in crypto balances and the user's preferred fiat currency",
5
5
  "author": "Exodus Movement Inc.",
6
6
  "license": "UNLICENSED",
@@ -31,17 +31,21 @@
31
31
  "@exodus/currency": "^2.1.2",
32
32
  "@exodus/fiat-currencies": "^1.0.0",
33
33
  "@exodus/module": "^1.1.0",
34
+ "@exodus/typeforce": "^1.18.1",
34
35
  "lodash": "^4.17.21",
36
+ "minimalistic-assert": "^1.0.1",
35
37
  "reselect": "^3.0.1"
36
38
  },
37
39
  "devDependencies": {
38
- "@exodus/atoms": "^5.7.3",
39
- "@exodus/balances": "^11.0.3",
40
+ "@exodus/assets": "^9.1.0",
41
+ "@exodus/atoms": "^7.0.0",
42
+ "@exodus/balances": "^12.1.0",
40
43
  "@exodus/basic-utils": "^2.0.0",
41
44
  "@exodus/blockchain-metadata": "*",
42
45
  "@exodus/enabled-assets": "^9.0.1",
43
46
  "@exodus/models": "^8.9.0",
44
47
  "@exodus/redux-dependency-injection": "^3.0.0",
48
+ "@exodus/solana-meta": "^1.0.4",
45
49
  "@exodus/storage-memory": "^2.1.1",
46
50
  "@exodus/wallet-accounts": "*",
47
51
  "eslint": "^8.44.0",
@@ -49,5 +53,5 @@
49
53
  "jest": "^29.1.2",
50
54
  "redux": "^4.2.1"
51
55
  },
52
- "gitHead": "18791c72d9be2a75aea6621a216c1aed819442ca"
56
+ "gitHead": "b2cc53c628d9cdb2e4a25557f72b88ec05c75e14"
53
57
  }
package/plugin/index.js CHANGED
@@ -33,6 +33,7 @@ const createFiatBalancesPlugin = ({
33
33
  onStop: () => {
34
34
  fiatBalancesAtomObserver.unregister()
35
35
  optimisticFiatBalancesAtomObserver?.unregister()
36
+ fiatBalances.stop()
36
37
  },
37
38
  }
38
39
  }
@@ -0,0 +1,98 @@
1
+ import { flattenToPaths, filterAsync } from '@exodus/basic-utils'
2
+ import fiatCurrencies from '@exodus/fiat-currencies'
3
+ import { isEqual } from 'lodash'
4
+
5
+ const { USD } = fiatCurrencies
6
+
7
+ const DEFAULT_BALANCES_THRESHOLD_USD = USD.defaultUnit(0.5)
8
+
9
+ const createTrackNonDustAssetNamesPlugin = ({
10
+ assetsModule,
11
+ fiatBalancesAtom,
12
+ fiatRateConverter,
13
+ nonDustBalanceAssetNamesAtom,
14
+ config: {
15
+ balanceThresholdsUsd = {},
16
+ balanceThresholdByChainUsd = {},
17
+ defaultBalanceThresholdUsd = DEFAULT_BALANCES_THRESHOLD_USD,
18
+ },
19
+ }) => {
20
+ const handleFiatBalancesChanged = async ({ balances }) => {
21
+ const { byAssetSource } = balances
22
+ if (!byAssetSource) return
23
+
24
+ const balancesByAssetSource = flattenToPaths(byAssetSource).map(
25
+ ([walletAccount, assetName, balanceField, balance]) => ({ walletAccount, assetName, balance })
26
+ )
27
+
28
+ const aboveThreshold = await filterAsync(
29
+ balancesByAssetSource,
30
+ async ({ assetName, balance }) => {
31
+ const usdBalance = await fiatRateConverter.toFiatCurrency({
32
+ currency: 'USD',
33
+ amount: balance,
34
+ })
35
+
36
+ return isAboveEnableThreshold({ assetName, usdBalance })
37
+ }
38
+ )
39
+
40
+ const nonDisableableAssetNamesSet = new Set(aboveThreshold.map(({ assetName }) => assetName))
41
+
42
+ const before = [...(await nonDustBalanceAssetNamesAtom.get())]
43
+ const after = [...nonDisableableAssetNamesSet]
44
+ if (!isEqual(before.sort(), after.sort())) await nonDustBalanceAssetNamesAtom.set(after)
45
+ }
46
+
47
+ let unobserveFiatBalancesAtom
48
+ const onLoad = () => {
49
+ unobserveFiatBalancesAtom = fiatBalancesAtom.observe((data) => {
50
+ // don't block processing
51
+ handleFiatBalancesChanged(data)
52
+ })
53
+ }
54
+
55
+ const onUnload = () => {
56
+ unobserveFiatBalancesAtom?.()
57
+ }
58
+
59
+ const resolveConfiguredThreshold = (asset) =>
60
+ balanceThresholdsUsd[asset.name] ||
61
+ balanceThresholdByChainUsd[asset.baseAsset.name] ||
62
+ defaultBalanceThresholdUsd
63
+
64
+ const isAboveEnableThreshold = async ({ assetName, usdBalance }) => {
65
+ const asset = assetsModule.getAsset(assetName)
66
+ const reserve = asset.accountReserve || asset.currency.ZERO
67
+ const configuredThreshold = resolveConfiguredThreshold(asset)
68
+ const reserveUsd = asset.accountReserve
69
+ ? await fiatRateConverter.toFiatCurrency({
70
+ currency: 'USD',
71
+ amount: reserve,
72
+ })
73
+ : USD.ZERO
74
+
75
+ const usdThreshold = reserveUsd.add(configuredThreshold)
76
+ return usdBalance.gte(usdThreshold)
77
+ }
78
+
79
+ return {
80
+ onLoad,
81
+ onUnload,
82
+ }
83
+ }
84
+
85
+ const trackNonDustAssetsNamesPluginDefinition = {
86
+ id: 'trackNonDustAssetsNamesPlugin',
87
+ type: 'plugin',
88
+ factory: createTrackNonDustAssetNamesPlugin,
89
+ dependencies: [
90
+ 'nonDustBalanceAssetNamesAtom',
91
+ 'fiatBalancesAtom',
92
+ 'fiatRateConverter',
93
+ 'assetsModule',
94
+ 'config?',
95
+ ],
96
+ }
97
+
98
+ export default trackNonDustAssetsNamesPluginDefinition
@@ -0,0 +1,16 @@
1
+ import { createSelector } from 'reselect'
2
+ import { toBalanceOnly } from './utils'
3
+
4
+ const selectorFactory = (byAssetSourceSelector) =>
5
+ createSelector(byAssetSourceSelector, toBalanceOnly)
6
+
7
+ const byBaseAssetSourceSelector = {
8
+ id: 'byBaseAssetSource',
9
+ selectorFactory,
10
+ dependencies: [
11
+ //
12
+ { selector: 'byBaseAssetSourceField' },
13
+ ],
14
+ }
15
+
16
+ export default byBaseAssetSourceSelector
@@ -0,0 +1,12 @@
1
+ const resultFunction = (data) => data.byBaseAssetSource
2
+
3
+ const byBaseAssetSourceFieldSelector = {
4
+ id: 'byBaseAssetSourceField',
5
+ resultFunction,
6
+ dependencies: [
7
+ //
8
+ { selector: 'data' },
9
+ ],
10
+ }
11
+
12
+ export default byBaseAssetSourceFieldSelector
@@ -3,6 +3,8 @@ import byWalletAccountSelector from './byWalletAccount'
3
3
  import byWalletAccountFieldSelector from './byWalletAccountField'
4
4
  import byAssetSourceFieldSelector from './byAssetSourceField'
5
5
  import byAssetInActiveAccountSelector from './byAssetInActiveAccount'
6
+ import byBaseAssetSourceSelector from './byBaseAssetSource'
7
+ import byBaseAssetSourceFieldSelector from './byBaseAssetSourceField'
6
8
  import optimisticByAssetInActiveAccountSelector from './optimisticByAssetInActiveAccount'
7
9
  import optimisticByAssetSourceSelector from './optimisticByAssetSource'
8
10
  import optimisticByAssetSourceFieldSelector from './optimisticByAssetSourceField'
@@ -16,6 +18,8 @@ export default [
16
18
  byAssetSourceSelector,
17
19
  byAssetSourceFieldSelector,
18
20
  byAssetInActiveAccountSelector,
21
+ byBaseAssetSourceSelector,
22
+ byBaseAssetSourceFieldSelector,
19
23
  byWalletAccountSelector,
20
24
  byWalletAccountFieldSelector,
21
25
  optimisticByAssetInActiveAccountSelector,