@exodus/balances 11.2.2 → 12.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,25 @@
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
+ ## [12.0.0](https://github.com/ExodusMovement/exodus-hydra/compare/@exodus/balances@11.2.2...@exodus/balances@12.0.0) (2023-11-23)
7
+
8
+ ### ⚠ BREAKING CHANGES
9
+
10
+ - use `assetsAtom` instead of legacy event (#4838)
11
+
12
+ ### Features
13
+
14
+ - has balance stored ([#4856](https://github.com/ExodusMovement/exodus-hydra/issues/4856)) ([510ffd5](https://github.com/ExodusMovement/exodus-hydra/commit/510ffd517c22bbbcb16441275f1ab71ffcc7de90))
15
+
16
+ ### Bug Fixes
17
+
18
+ - cleanup subscriptions on stop ([#4814](https://github.com/ExodusMovement/exodus-hydra/issues/4814)) ([d053582](https://github.com/ExodusMovement/exodus-hydra/commit/d0535826c2023dd4d3273b367bbcc5cca6e4bb95))
19
+ - zero balance after send is ignore ([#4725](https://github.com/ExodusMovement/exodus-hydra/issues/4725)) ([56c5d35](https://github.com/ExodusMovement/exodus-hydra/commit/56c5d35ca1643e869c112d0166df036ec988088f))
20
+
21
+ ### Code Refactoring
22
+
23
+ - use `assetsAtom` instead of legacy event ([#4838](https://github.com/ExodusMovement/exodus-hydra/issues/4838)) ([33260c3](https://github.com/ExodusMovement/exodus-hydra/commit/33260c3fd48286d00a940a4e5ec4956326f8c3c1))
24
+
6
25
  ## [11.2.2](https://github.com/ExodusMovement/exodus-hydra/compare/@exodus/balances@11.2.1...@exodus/balances@11.2.2) (2023-11-06)
7
26
 
8
27
  ### Bug Fixes
@@ -1,9 +1,9 @@
1
- import { compute } from '@exodus/atoms'
1
+ import { compute, createStorageAtomFactory } from '@exodus/atoms'
2
2
  import { flatMap, map } from 'lodash'
3
3
 
4
4
  // balancesAtom schema: { balances: { [walletAccount]: { [assetName]: { balance: NumberUnit } } } } }
5
5
 
6
- const createHasBalanceAtom = ({ balancesAtom }) => {
6
+ const createComputedAtom = ({ balancesAtom }) => {
7
7
  const selector = ({ balances } = {}) => {
8
8
  const numberUnits = flatMap(balances, (value) => map(value, 'balance'))
9
9
  return numberUnits.some((numberUnit) => (numberUnit ? !numberUnit.isZero : false))
@@ -12,4 +12,17 @@ const createHasBalanceAtom = ({ balancesAtom }) => {
12
12
  return compute({ atom: balancesAtom, selector })
13
13
  }
14
14
 
15
+ const createHasBalanceAtom = ({ balancesAtom, storage }) => {
16
+ const storageAtom = createStorageAtomFactory({ storage })({
17
+ key: 'hasBalance',
18
+ defaultValue: false,
19
+ isSoleWriter: true,
20
+ })
21
+ const computedAtom = createComputedAtom({ balancesAtom })
22
+ // eslint-disable-next-line @exodus/hydra/no-eternal-subscription
23
+ computedAtom.observe((value) => storageAtom.set(value))
24
+
25
+ return storageAtom
26
+ }
27
+
15
28
  export default createHasBalanceAtom
package/atoms/index.js CHANGED
@@ -12,5 +12,5 @@ export const hasBalanceAtomDefinition = {
12
12
  id: 'hasBalanceAtom',
13
13
  type: 'atom',
14
14
  factory: createHasBalanceAtom,
15
- dependencies: ['balancesAtom'],
15
+ dependencies: ['balancesAtom', 'storage'],
16
16
  }
package/index.js CHANGED
@@ -22,9 +22,13 @@ const balances = (config) => {
22
22
  config,
23
23
  },
24
24
  { definition: balancesAtomDefinition },
25
- { definition: hasBalanceAtomDefinition },
25
+ {
26
+ definition: hasBalanceAtomDefinition,
27
+ storage: { namespace: 'balances' },
28
+ aliases: [{ implementationId: 'unsafeStorage', interfaceId: 'storage' }],
29
+ },
26
30
  { definition: balancesReportDefinition },
27
- { definition: balancesPluginDefinition },
31
+ { definition: balancesPluginDefinition, writesAtoms: ['hasBalanceAtom'] },
28
32
  ],
29
33
  }
30
34
  }
package/module/index.js CHANGED
@@ -21,6 +21,7 @@ export const MODULE_ID = 'balances'
21
21
 
22
22
  class Balances extends ExodusModule {
23
23
  #assetsModule
24
+ #assetsAtom
24
25
  #balanceFields
25
26
  #walletAccounts = [] // string[]
26
27
  #ready = false
@@ -32,9 +33,11 @@ class Balances extends ExodusModule {
32
33
  #txLogsAtom
33
34
  #enabledWalletAccountsAtom
34
35
  #loaded = false
36
+ #subscriptions = []
35
37
 
36
38
  constructor({
37
39
  assetsModule,
40
+ assetsAtom,
38
41
  enabledWalletAccountsAtom,
39
42
  config: { balanceFields },
40
43
  logger,
@@ -45,6 +48,7 @@ class Balances extends ExodusModule {
45
48
  super({ name: MODULE_ID, logger })
46
49
 
47
50
  this.#assetsModule = assetsModule
51
+ this.#assetsAtom = assetsAtom
48
52
  this.#balanceFields = balanceFields
49
53
  this.#balancesAtom = balancesAtom
50
54
  this.#accountStatesAtom = accountStatesAtom
@@ -92,23 +96,27 @@ class Balances extends ExodusModule {
92
96
  load = () => {
93
97
  if (this.#loaded) return
94
98
  this.#loaded = true
95
- this.#enabledWalletAccountsAtom.observe((payload) => {
96
- this.#setWalletAccounts(Object.keys(payload))
97
- })
98
-
99
- this.#accountStatesAtom.observe(({ changes }) =>
100
- flattenToPaths(changes).forEach(([walletAccount, assetName, accountState]) =>
101
- this.#handleAccountStateUpdate({ walletAccount, assetName, accountState })
102
- )
103
- )
104
99
 
105
- this.#txLogsAtom.observe(({ changes }) =>
106
- flattenToPaths(changes).forEach(([walletAccount, assetName, txLog]) =>
107
- this.#handleTxLogUpdate({ walletAccount, assetName, txLog })
108
- )
100
+ this.#subscriptions.push(
101
+ this.#enabledWalletAccountsAtom.observe((payload) => {
102
+ this.#setWalletAccounts(Object.keys(payload))
103
+ }),
104
+ this.#accountStatesAtom.observe(({ changes }) =>
105
+ flattenToPaths(changes).forEach(([walletAccount, assetName, accountState]) =>
106
+ this.#handleAccountStateUpdate({ walletAccount, assetName, accountState })
107
+ )
108
+ ),
109
+ this.#txLogsAtom.observe(({ changes }) =>
110
+ flattenToPaths(changes).forEach(([walletAccount, assetName, txLog]) =>
111
+ this.#handleTxLogUpdate({ walletAccount, assetName, txLog })
112
+ )
113
+ ),
114
+ this.#assetsAtom.observe(({ added }) => {
115
+ if (added.length > 0) {
116
+ this.#onAssetsChanged(added)
117
+ }
118
+ })
109
119
  )
110
-
111
- this.#assetsModule.on('assets-add', this.#onAssetsChanged)
112
120
  }
113
121
 
114
122
  #getAsset = (assetName) => this.#assetsModule.getAsset(assetName)
@@ -160,7 +168,8 @@ class Balances extends ExodusModule {
160
168
  const asset = this.#getAsset(assetName)
161
169
  const txLog = txLogsByAssetName[assetName]
162
170
  const balances = asset.api.getBalances({ asset, accountState, txLog })
163
- if (!balances || isEmpty(balances) || !balances.balance) return null
171
+ if (!balances || isEmpty(balances) || !balances.balance)
172
+ return Object.fromEntries(this.#balanceFields.map((field) => [field, asset.currency.ZERO]))
164
173
  if (!balances.balance.unitType.equals(asset.currency)) {
165
174
  this.emit('error', {
166
175
  fn: 'getAssetBalances',
@@ -380,6 +389,11 @@ class Balances extends ExodusModule {
380
389
 
381
390
  this.#oldBalances = balances
382
391
  }
392
+
393
+ stop = () => {
394
+ this.#subscriptions.forEach((unsubscribe) => unsubscribe())
395
+ this.#subscriptions = []
396
+ }
383
397
  }
384
398
 
385
399
  const createBalances = (opts) => new Balances(opts)
@@ -396,6 +410,7 @@ const balancesDefinition = {
396
410
  'balancesAtom',
397
411
  'txLogsAtom',
398
412
  'accountStatesAtom',
413
+ 'assetsAtom',
399
414
  ],
400
415
  }
401
416
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exodus/balances",
3
- "version": "11.2.2",
3
+ "version": "12.0.0",
4
4
  "description": "Tracks crypto balances across enabled wallet accounts.",
5
5
  "author": "Exodus Movement Inc.",
6
6
  "license": "UNLICENSED",
@@ -45,18 +45,18 @@
45
45
  "devDependencies": {
46
46
  "@exodus/assets": "^8.0.94",
47
47
  "@exodus/assets-base": "^8.1.10",
48
- "@exodus/assets-feature": "^3.7.0",
48
+ "@exodus/assets-feature": "^4.0.0",
49
49
  "@exodus/bitcoin-meta": "^1.0.1",
50
50
  "@exodus/blockchain-metadata": "^9.0.1",
51
51
  "@exodus/ethereum-meta": "^1.0.30",
52
52
  "@exodus/models": "^10.1.0",
53
53
  "@exodus/redux-dependency-injection": "^3.0.0",
54
54
  "@exodus/storage-memory": "*",
55
- "@exodus/wallet-accounts": "^14.2.0",
55
+ "@exodus/wallet-accounts": "^14.3.0",
56
56
  "eslint": "^8.44.0",
57
57
  "events": "^3.3.0",
58
58
  "jest": "^29.1.2",
59
59
  "redux": "^4.2.1"
60
60
  },
61
- "gitHead": "1c23ddca93850c0a40da742a5cefd2e42b554356"
61
+ "gitHead": "0c943e6a82e29239305544665711c1b1b4a98fce"
62
62
  }
package/plugins/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { createAtomObserver } from '@exodus/atoms'
2
2
 
3
- const createBalancesPlugin = ({ balancesAtom, port, balances }) => {
3
+ const createBalancesPlugin = ({ balancesAtom, port, balances, hasBalanceAtom }) => {
4
4
  const balancesAtomObserver = createAtomObserver({
5
5
  port,
6
6
  atom: balancesAtom,
@@ -8,7 +8,15 @@ const createBalancesPlugin = ({ balancesAtom, port, balances }) => {
8
8
  })
9
9
  balancesAtomObserver.register()
10
10
 
11
+ const hasBalanceAtomObserver = createAtomObserver({
12
+ port,
13
+ atom: hasBalanceAtom,
14
+ event: 'hasBalance',
15
+ })
16
+ hasBalanceAtomObserver.register()
17
+
11
18
  const onLoad = () => {
19
+ hasBalanceAtomObserver.start()
12
20
  balancesAtomObserver.start()
13
21
 
14
22
  balances.load()
@@ -16,11 +24,18 @@ const createBalancesPlugin = ({ balancesAtom, port, balances }) => {
16
24
 
17
25
  const onStop = () => {
18
26
  balancesAtomObserver.unregister()
27
+ hasBalanceAtomObserver.unregister()
28
+ balances.stop()
29
+ }
30
+
31
+ const onClear = async () => {
32
+ await hasBalanceAtom.set(undefined)
19
33
  }
20
34
 
21
35
  return {
22
36
  onLoad,
23
37
  onStop,
38
+ onClear,
24
39
  }
25
40
  }
26
41
 
@@ -28,7 +43,7 @@ const balancesPluginDefinition = {
28
43
  id: 'balancesPlugin',
29
44
  type: 'plugin',
30
45
  factory: createBalancesPlugin,
31
- dependencies: ['balancesAtom', 'port', 'balances'],
46
+ dependencies: ['balancesAtom', 'port', 'balances', 'hasBalanceAtom'],
32
47
  }
33
48
 
34
49
  export default balancesPluginDefinition
package/redux/index.js CHANGED
@@ -43,8 +43,15 @@ const balancesReduxDefinition = {
43
43
  return mergeChanges(state, payload.changes)
44
44
  }
45
45
 
46
- return setAccounts(state, payload.balances)
46
+ return {
47
+ hasBalance: state.hasBalance,
48
+ ...setAccounts(state, payload.balances),
49
+ }
47
50
  },
51
+ hasBalance: (state, payload) => ({
52
+ ...state,
53
+ hasBalance: payload,
54
+ }),
48
55
  },
49
56
  selectorDefinitions: [...helper.selectorDefinitions, ...selectorDefinitions],
50
57
  }
@@ -1,5 +1,8 @@
1
1
  import helper from './multi-account-helper'
2
2
 
3
- const initialState = helper.createInitialState()
3
+ const initialState = {
4
+ hasBalance: false,
5
+ ...helper.createInitialState(),
6
+ }
4
7
 
5
8
  export default initialState