@exodus/balances 12.0.1 → 12.2.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,19 @@
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.2.0](https://github.com/ExodusMovement/exodus-hydra/compare/@exodus/balances@12.1.0...@exodus/balances@12.2.0) (2024-01-03)
7
+
8
+ ### Features
9
+
10
+ - **balances:** new unconfirmed balance selector to improve performance ([#5234](https://github.com/ExodusMovement/exodus-hydra/issues/5234)) ([9c97b66](https://github.com/ExodusMovement/exodus-hydra/commit/9c97b6618c0fa494763d76431aa4bbf915495091))
11
+
12
+ ## [12.1.0](https://github.com/ExodusMovement/exodus-hydra/compare/@exodus/balances@12.0.1...@exodus/balances@12.1.0) (2023-12-14)
13
+
14
+ ### Features
15
+
16
+ - **balances:** funded wallet accounts atom ([#5074](https://github.com/ExodusMovement/exodus-hydra/issues/5074)) ([dc98436](https://github.com/ExodusMovement/exodus-hydra/commit/dc98436d5f8bbade4ceab85af60455ac3b4efa49))
17
+ - remove storing oldBalances in balances atoms ([#5073](https://github.com/ExodusMovement/exodus-hydra/issues/5073)) ([414db86](https://github.com/ExodusMovement/exodus-hydra/commit/414db866a86aaf3242e68698596af187da6927c3))
18
+
6
19
  ## [12.0.1](https://github.com/ExodusMovement/exodus-hydra/compare/@exodus/balances@12.0.0...@exodus/balances@12.0.1) (2023-12-12)
7
20
 
8
21
  ### Bug Fixes
package/README.md CHANGED
@@ -30,8 +30,8 @@ const balances = createBalances({
30
30
  logger,
31
31
  })
32
32
 
33
- balances.on('balances', ({ balances, oldBalances, changes }) => {
34
- // `balances`, `oldBalances`
33
+ balances.on('balances', ({ balances, changes }) => {
34
+ // `balances`
35
35
  // {
36
36
  // [walletAccount]: {
37
37
  // [assetName]: {
@@ -0,0 +1,23 @@
1
+ import { combine, compute } from '@exodus/atoms'
2
+ import { mapValues } from '@exodus/basic-utils'
3
+
4
+ const computeFundedWalletAccounts = ({ walletAccounts, balances }) => {
5
+ return mapValues(walletAccounts, (_, key) => {
6
+ const walletAccountBalaces = balances.balances[key] || {}
7
+ return Object.values(walletAccountBalaces).some(({ balance }) => !balance.isZero)
8
+ })
9
+ }
10
+
11
+ const createFundedWalletAccountsAtom = ({ balancesAtom, enabledWalletAccountsAtom }) => {
12
+ const walletAccountsBalanceAtom = combine({
13
+ walletAccounts: enabledWalletAccountsAtom,
14
+ balances: balancesAtom,
15
+ })
16
+
17
+ return compute({
18
+ atom: walletAccountsBalanceAtom,
19
+ selector: computeFundedWalletAccounts,
20
+ })
21
+ }
22
+
23
+ export default createFundedWalletAccountsAtom
package/atoms/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import createBalancesAtom from './balances'
2
+ import createFundedWalletAccountsAtom from './funded-wallet-accounts'
2
3
  import createHasBalanceAtom from './has-balance'
3
4
 
4
5
  export const balancesAtomDefinition = {
@@ -14,3 +15,10 @@ export const hasBalanceAtomDefinition = {
14
15
  factory: createHasBalanceAtom,
15
16
  dependencies: ['balancesAtom', 'storage'],
16
17
  }
18
+
19
+ export const fundedWalletAccountsAtomDefinition = {
20
+ id: 'fundedWalletAccountsAtom',
21
+ type: 'atom',
22
+ factory: createFundedWalletAccountsAtom,
23
+ dependencies: ['balancesAtom', 'enabledWalletAccountsAtom'],
24
+ }
package/index.js CHANGED
@@ -1,5 +1,9 @@
1
1
  import balancesDefinition from './module'
2
- import { balancesAtomDefinition, hasBalanceAtomDefinition } from './atoms'
2
+ import {
3
+ balancesAtomDefinition,
4
+ hasBalanceAtomDefinition,
5
+ fundedWalletAccountsAtomDefinition,
6
+ } from './atoms'
3
7
  import balancesReportDefinition from './report'
4
8
  import balancesPluginDefinition from './plugins'
5
9
  import typeforce from '@exodus/typeforce'
@@ -18,7 +22,6 @@ const balances = (config) => {
18
22
  definitions: [
19
23
  {
20
24
  definition: balancesDefinition,
21
- writesAtoms: ['balancesAtom'],
22
25
  config,
23
26
  },
24
27
  { definition: balancesAtomDefinition },
@@ -27,8 +30,9 @@ const balances = (config) => {
27
30
  storage: { namespace: 'balances' },
28
31
  aliases: [{ implementationId: 'unsafeStorage', interfaceId: 'storage' }],
29
32
  },
33
+ { definition: fundedWalletAccountsAtomDefinition },
30
34
  { definition: balancesReportDefinition },
31
- { definition: balancesPluginDefinition, writesAtoms: ['hasBalanceAtom'] },
35
+ { definition: balancesPluginDefinition },
32
36
  ],
33
37
  }
34
38
  }
package/module/index.js CHANGED
@@ -26,7 +26,6 @@ class Balances extends ExodusModule {
26
26
  #walletAccounts = [] // string[]
27
27
  #ready = false
28
28
 
29
- #oldBalances = Object.create(null)
30
29
  #balances = Object.create(null)
31
30
  #balancesAtom
32
31
  #accountStatesAtom
@@ -375,19 +374,12 @@ class Balances extends ExodusModule {
375
374
  }
376
375
 
377
376
  #flush = ({ changes } = {}) => {
378
- const oldBalances = this.#oldBalances
379
377
  const balances = cloneBalances(this.#balances)
380
378
 
381
379
  if (Object.keys(balances).length === 0) return // wait till `load` finished
382
380
 
383
381
  this._logger.log('balances updated')
384
- this.#balancesAtom.set({
385
- balances,
386
- oldBalances,
387
- changes,
388
- })
389
-
390
- this.#oldBalances = balances
382
+ this.#balancesAtom.set({ balances, changes })
391
383
  }
392
384
 
393
385
  stop = () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exodus/balances",
3
- "version": "12.0.1",
3
+ "version": "12.2.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": "^4.0.0",
48
+ "@exodus/assets-feature": "^4.0.1",
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": "^15.0.0",
55
+ "@exodus/wallet-accounts": "^15.1.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": "be0d353e5d42bd166526ac72d256b2b4c7de36a9"
61
+ "gitHead": "a158fff895557150c79a1cdec05a29d4587f5577"
62
62
  }
@@ -1,5 +1,6 @@
1
1
  import { memoize } from 'lodash'
2
2
  import { createSelector } from 'reselect'
3
+ import { getUnconfirmedBalance } from './get-unconfirmed-balance'
3
4
 
4
5
  const selectorFactory = (assetsSelector, createAssetSourceSelector) =>
5
6
  memoize(
@@ -7,21 +8,7 @@ const selectorFactory = (assetsSelector, createAssetSourceSelector) =>
7
8
  createSelector(
8
9
  assetsSelector,
9
10
  createAssetSourceSelector({ assetName, walletAccount }),
10
- (assets, txs) => {
11
- const asset = assets[assetName]
12
- let result = asset.currency.ZERO
13
-
14
- for (const tx of txs) {
15
- const isIncommingTX = tx.coinAmount.isPositive
16
- const isUnconfirmed = !tx.failed && tx.pending
17
-
18
- if (isUnconfirmed) {
19
- result = isIncommingTX ? result.add(tx.coinAmount) : result.sub(tx.coinAmount)
20
- }
21
- }
22
-
23
- return result
24
- }
11
+ (assets, txs) => getUnconfirmedBalance({ asset: assets[assetName], txs })
25
12
  ),
26
13
  ({ assetName, walletAccount }) => [assetName, walletAccount].join('-')
27
14
  )
@@ -0,0 +1,40 @@
1
+ import { memoize } from 'lodash'
2
+ import { createSelector } from 'reselect'
3
+
4
+ export const getUnconfirmedBalance = ({ asset, txs }) => {
5
+ let result = asset.currency.ZERO
6
+
7
+ for (const tx of txs) {
8
+ const isIncommingTX = tx.coinAmount.isPositive
9
+ const isUnconfirmed = !tx.failed && tx.pending
10
+
11
+ if (isUnconfirmed) {
12
+ result = isIncommingTX ? result.add(tx.coinAmount) : result.sub(tx.coinAmount)
13
+ }
14
+ }
15
+
16
+ return result
17
+ }
18
+
19
+ const selectorFactory = (assetsSelector, createAssetSourceSelector) =>
20
+ createSelector(assetsSelector, createAssetSourceSelector, (assets, getTxs) =>
21
+ memoize(
22
+ ({ assetName, walletAccount }) =>
23
+ getUnconfirmedBalance({
24
+ asset: assets[assetName],
25
+ txs: getTxs({ assetName, walletAccount }),
26
+ }),
27
+ ({ assetName, walletAccount }) => [assetName, walletAccount].join('-')
28
+ )
29
+ )
30
+
31
+ const getUnconfirmedBalanceSelectorDefinition = {
32
+ id: 'getUnconfirmedBalance',
33
+ selectorFactory,
34
+ dependencies: [
35
+ { module: 'assets', selector: 'all' },
36
+ { module: 'txLog', selector: 'createAssetSourceSelectorOld' },
37
+ ],
38
+ }
39
+
40
+ export default getUnconfirmedBalanceSelectorDefinition
@@ -1,3 +1,4 @@
1
1
  import createUnconfirmedBalanceSelectorDefinition from './create-unconfirmed-balance'
2
+ import getUnconfirmedBalanceSelectorDefinition from './get-unconfirmed-balance'
2
3
 
3
- export default [createUnconfirmedBalanceSelectorDefinition]
4
+ export default [createUnconfirmedBalanceSelectorDefinition, getUnconfirmedBalanceSelectorDefinition]