@exodus/balances 13.4.0 → 13.5.1

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,18 @@
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
+ ## [13.5.1](https://github.com/ExodusMovement/exodus-hydra/compare/@exodus/balances@13.5.0...@exodus/balances@13.5.1) (2024-11-29)
7
+
8
+ ### Bug Fixes
9
+
10
+ - fix: balances map equality check (#10620)
11
+
12
+ ## [13.5.0](https://github.com/ExodusMovement/exodus-hydra/compare/@exodus/balances@13.4.0...@exodus/balances@13.5.0) (2024-11-29)
13
+
14
+ ### Features
15
+
16
+ - feat: add various crypto balance props (#10419)
17
+
6
18
  ## [13.4.0](https://github.com/ExodusMovement/exodus-hydra/compare/@exodus/balances@13.3.2...@exodus/balances@13.4.0) (2024-11-25)
7
19
 
8
20
  ### Features
@@ -0,0 +1,47 @@
1
+ import { compute, dedupe } from '@exodus/atoms'
2
+
3
+ const numberUnitMapsAreEqual = (mapA, mapB) => {
4
+ if (mapA.size !== mapB.size) {
5
+ return false
6
+ }
7
+
8
+ for (const [key, val] of mapA) {
9
+ const itemB = mapB.get(key)
10
+ if (!itemB || !itemB.equals(val)) {
11
+ return false
12
+ }
13
+ }
14
+
15
+ return true
16
+ }
17
+
18
+ const createAssetTotalWalletAmountsAtom = ({ balancesAtom }) => {
19
+ let previousValue = new Map()
20
+ const selector = ({ balances }) => {
21
+ const assetsWithBalances = new Map()
22
+
23
+ Object.values(balances).forEach((accountBalances) => {
24
+ Object.keys(accountBalances).forEach((assetName) => {
25
+ const accountAssetBalance = accountBalances[assetName] && accountBalances[assetName].balance
26
+ if (accountAssetBalance) {
27
+ if (assetsWithBalances.has(assetName)) {
28
+ const currentAssetBalance = assetsWithBalances.get(assetName)
29
+ assetsWithBalances.set(assetName, accountAssetBalance.add(currentAssetBalance))
30
+ } else {
31
+ assetsWithBalances.set(assetName, accountAssetBalance)
32
+ }
33
+ }
34
+ })
35
+ })
36
+
37
+ if (!numberUnitMapsAreEqual(assetsWithBalances, previousValue)) {
38
+ previousValue = assetsWithBalances
39
+ }
40
+
41
+ return previousValue
42
+ }
43
+
44
+ return dedupe(compute({ atom: balancesAtom, selector }))
45
+ }
46
+
47
+ export default createAssetTotalWalletAmountsAtom
package/atoms/index.js CHANGED
@@ -3,6 +3,7 @@ import createAssetsWithBalanceCountAtom from './assets-with-balance-count.js'
3
3
  import createBalancesAtom from './balances.js'
4
4
  import createFundedWalletAccountsAtom from './funded-wallet-accounts.js'
5
5
  import createHasBalanceAtom from './has-balance.js'
6
+ import createAssetsTotalWalletAmountsAtom from './asset-total-wallet-amounts.js'
6
7
 
7
8
  export const balancesAtomDefinition = {
8
9
  id: 'balancesAtom',
@@ -43,3 +44,11 @@ export const assetsWithBalanceCountAtomDefinition = {
43
44
  dependencies: ['assetNamesWithBalanceAtom'],
44
45
  public: true,
45
46
  }
47
+
48
+ export const assetsTotalWalletAmountsDefinition = {
49
+ id: 'assetsTotalWalletAmountsAtom',
50
+ type: 'atom',
51
+ factory: createAssetsTotalWalletAmountsAtom,
52
+ dependencies: ['balancesAtom'],
53
+ public: true,
54
+ }
package/default-config.js CHANGED
@@ -19,7 +19,8 @@ const config = {
19
19
  balanceFieldsConfig,
20
20
  balanceFields: balanceFieldsConfig
21
21
  .flatMap((fieldConfig) => [fieldConfig.name, fieldConfig.legacyName])
22
- .filter(Boolean), // wallets can reduce the number of exposed fields
22
+ .filter(Boolean), // wallets can reduce the number of exposed fields,
23
+ assetsToTrackForBalances: [],
23
24
  }
24
25
 
25
26
  export default config
package/index.js CHANGED
@@ -5,6 +5,7 @@ import {
5
5
  fundedWalletAccountsAtomDefinition,
6
6
  assetsWithBalanceCountAtomDefinition,
7
7
  assetNamesWithBalanceAtomDefinition,
8
+ assetsTotalWalletAmountsDefinition,
8
9
  } from './atoms/index.js'
9
10
  import balancesReportDefinition from './report/index.js'
10
11
  import {
@@ -18,11 +19,14 @@ const balances = (walletConfig) => {
18
19
  const config = {
19
20
  ...defaultConfig,
20
21
  balanceFields: walletConfig?.balanceFields || defaultConfig.balanceFields,
22
+ assetsToTrackForBalances:
23
+ walletConfig?.assetsToTrackForBalances || defaultConfig.assetsToTrackForBalances,
21
24
  }
22
25
  typeforce(
23
26
  {
24
27
  balanceFields: typeforce.arrayOf('String'),
25
28
  balanceFieldsConfig: typeforce.arrayOf('Object'),
29
+ assetsToTrackForBalances: typeforce.arrayOf('Object'),
26
30
  },
27
31
  config,
28
32
  true
@@ -46,9 +50,11 @@ const balances = (walletConfig) => {
46
50
  { definition: fundedWalletAccountsAtomDefinition },
47
51
  { definition: balancesReportDefinition },
48
52
  { definition: balancesLifecyclePluginDefinition },
53
+ { definition: assetsTotalWalletAmountsDefinition },
49
54
  {
50
55
  if: { registered: ['analytics'] },
51
56
  definition: balancesAnalyticsPluginDefinition,
57
+ config: { assetsToTrackForBalances: config.assetsToTrackForBalances },
52
58
  },
53
59
  ],
54
60
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exodus/balances",
3
- "version": "13.4.0",
3
+ "version": "13.5.1",
4
4
  "description": "Tracks crypto balances across enabled wallet accounts.",
5
5
  "author": "Exodus Movement, Inc.",
6
6
  "license": "MIT",
@@ -65,5 +65,5 @@
65
65
  "publishConfig": {
66
66
  "access": "public"
67
67
  },
68
- "gitHead": "901254d521ab3c43da0be4a4537ed80015e1c6b9"
68
+ "gitHead": "16255e10556dd9fdcd811351f7fea30c101a4fe7"
69
69
  }
@@ -2,9 +2,16 @@ const createBalancesAnalyticsPlugin = ({
2
2
  analytics,
3
3
  hasBalanceAtom,
4
4
  assetsWithBalanceCountAtom,
5
+ assetsTotalWalletAmountsAtom,
6
+ config: { assetsToTrackForBalances },
5
7
  }) => {
6
8
  let subscriptions = []
7
9
 
10
+ const getAssetAmountFromNumberUnit = (unit) => {
11
+ if (!unit) return 0
12
+ return unit.toDefaultNumber()
13
+ }
14
+
8
15
  const onStart = () => {
9
16
  analytics.requireDefaultEventProperties(['hasBalance', 'numberOfAssets'])
10
17
 
@@ -16,7 +23,19 @@ const createBalancesAnalyticsPlugin = ({
16
23
  analytics.setDefaultEventProperties({ numberOfAssets })
17
24
  )
18
25
 
19
- subscriptions = [unsubscribeBalanceAtom, unsubscribeBalanceCountAtom]
26
+ const unsubscribeWalletAmountsAtom = assetsTotalWalletAmountsAtom.observe(async (balances) => {
27
+ const assetBalances = assetsToTrackForBalances.reduce((acc, obj) => {
28
+ acc[obj.analyticsName] = getAssetAmountFromNumberUnit(balances.get(obj.assetName))
29
+ return acc
30
+ }, Object.create(null))
31
+ analytics.setDefaultEventProperties(assetBalances)
32
+ })
33
+
34
+ subscriptions = [
35
+ unsubscribeBalanceAtom,
36
+ unsubscribeBalanceCountAtom,
37
+ unsubscribeWalletAmountsAtom,
38
+ ]
20
39
  }
21
40
 
22
41
  const onStop = () => {
package/plugins/index.js CHANGED
@@ -13,6 +13,12 @@ export const balancesAnalyticsPluginDefinition = {
13
13
  id: 'balancesAnalyticsPlugin',
14
14
  type: 'plugin',
15
15
  factory: createBalancesAnalyticsPlugin,
16
- dependencies: ['analytics', 'hasBalanceAtom', 'assetsWithBalanceCountAtom'],
16
+ dependencies: [
17
+ 'analytics',
18
+ 'hasBalanceAtom',
19
+ 'assetsWithBalanceCountAtom',
20
+ 'assetsTotalWalletAmountsAtom',
21
+ 'config?',
22
+ ],
17
23
  public: true,
18
24
  }