@ledgerhq/live-common 34.50.0-nightly.6 → 34.51.0-nightly.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.
Files changed (179) hide show
  1. package/lib/bridge/generic-alpaca/prepareTransaction.d.ts.map +1 -1
  2. package/lib/bridge/generic-alpaca/prepareTransaction.js +3 -6
  3. package/lib/bridge/generic-alpaca/prepareTransaction.js.map +1 -1
  4. package/lib/bridge/generic-alpaca/tests/prepareTransaction.test.js +60 -0
  5. package/lib/bridge/generic-alpaca/tests/prepareTransaction.test.js.map +1 -1
  6. package/lib/bridge/generic-alpaca/types.d.ts +0 -1
  7. package/lib/bridge/generic-alpaca/types.d.ts.map +1 -1
  8. package/lib/exchange/swap/postSwapState.d.ts.map +1 -1
  9. package/lib/exchange/swap/postSwapState.js +19 -23
  10. package/lib/exchange/swap/postSwapState.js.map +1 -1
  11. package/lib/exchange/swap/setBroadcastTransaction.d.ts +4 -4
  12. package/lib/exchange/swap/setBroadcastTransaction.js +7 -7
  13. package/lib/exchange/swap/setBroadcastTransaction.js.map +1 -1
  14. package/lib/exchange/swap/types.d.ts +3 -3
  15. package/lib/exchange/swap/types.d.ts.map +1 -1
  16. package/lib/families/tezos/config.js +2 -2
  17. package/lib/modularDrawer/hooks/modules/useRightMarketTrendModule.js +5 -2
  18. package/lib/modularDrawer/hooks/modules/useRightMarketTrendModule.js.map +1 -1
  19. package/lib/modularDrawer/hooks/useRightBalanceAsset.d.ts +5 -8
  20. package/lib/modularDrawer/hooks/useRightBalanceAsset.d.ts.map +1 -1
  21. package/lib/modularDrawer/hooks/useRightBalanceAsset.js +15 -24
  22. package/lib/modularDrawer/hooks/useRightBalanceAsset.js.map +1 -1
  23. package/lib/modularDrawer/hooks/useRightBalanceNetwork.d.ts +6 -25
  24. package/lib/modularDrawer/hooks/useRightBalanceNetwork.d.ts.map +1 -1
  25. package/lib/modularDrawer/hooks/useRightBalanceNetwork.js +4 -3
  26. package/lib/modularDrawer/hooks/useRightBalanceNetwork.js.map +1 -1
  27. package/lib/modularDrawer/modules/createNetworkConfiguration.d.ts.map +1 -1
  28. package/lib/modularDrawer/modules/createNetworkConfiguration.js +8 -4
  29. package/lib/modularDrawer/modules/createNetworkConfiguration.js.map +1 -1
  30. package/lib/modularDrawer/utils/__tests__/calculateProviderTotal.test.d.ts +2 -0
  31. package/lib/modularDrawer/utils/__tests__/calculateProviderTotal.test.d.ts.map +1 -0
  32. package/lib/modularDrawer/utils/__tests__/calculateProviderTotal.test.js +212 -0
  33. package/lib/modularDrawer/utils/__tests__/calculateProviderTotal.test.js.map +1 -0
  34. package/lib/modularDrawer/utils/__tests__/groupAccountsByAsset.test.d.ts +2 -0
  35. package/lib/modularDrawer/utils/__tests__/groupAccountsByAsset.test.d.ts.map +1 -0
  36. package/lib/modularDrawer/utils/__tests__/groupAccountsByAsset.test.js +202 -0
  37. package/lib/modularDrawer/utils/__tests__/groupAccountsByAsset.test.js.map +1 -0
  38. package/lib/modularDrawer/utils/calculateProviderTotal.d.ts +1 -0
  39. package/lib/modularDrawer/utils/calculateProviderTotal.d.ts.map +1 -1
  40. package/lib/modularDrawer/utils/calculateProviderTotal.js +10 -2
  41. package/lib/modularDrawer/utils/calculateProviderTotal.js.map +1 -1
  42. package/lib/modularDrawer/utils/getBalanceAndFiatValueByAssets.d.ts +6 -4
  43. package/lib/modularDrawer/utils/getBalanceAndFiatValueByAssets.d.ts.map +1 -1
  44. package/lib/modularDrawer/utils/getBalanceAndFiatValueByAssets.js +9 -22
  45. package/lib/modularDrawer/utils/getBalanceAndFiatValueByAssets.js.map +1 -1
  46. package/lib/modularDrawer/utils/groupAccountsByAsset.d.ts +4 -3
  47. package/lib/modularDrawer/utils/groupAccountsByAsset.d.ts.map +1 -1
  48. package/lib/modularDrawer/utils/groupAccountsByAsset.js +27 -11
  49. package/lib/modularDrawer/utils/groupAccountsByAsset.js.map +1 -1
  50. package/lib/modularDrawer/utils/index.d.ts +0 -1
  51. package/lib/modularDrawer/utils/index.d.ts.map +1 -1
  52. package/lib/modularDrawer/utils/index.js +1 -3
  53. package/lib/modularDrawer/utils/index.js.map +1 -1
  54. package/lib/modularDrawer/utils/sortByBalance.d.ts +1 -3
  55. package/lib/modularDrawer/utils/sortByBalance.d.ts.map +1 -1
  56. package/lib/modularDrawer/utils/sortByBalance.js +6 -18
  57. package/lib/modularDrawer/utils/sortByBalance.js.map +1 -1
  58. package/lib/modularDrawer/utils/type.d.ts +9 -8
  59. package/lib/modularDrawer/utils/type.d.ts.map +1 -1
  60. package/lib/wallet-api/Exchange/server.js +6 -6
  61. package/lib/wallet-api/Exchange/server.js.map +1 -1
  62. package/lib-es/bridge/generic-alpaca/prepareTransaction.d.ts.map +1 -1
  63. package/lib-es/bridge/generic-alpaca/prepareTransaction.js +3 -6
  64. package/lib-es/bridge/generic-alpaca/prepareTransaction.js.map +1 -1
  65. package/lib-es/bridge/generic-alpaca/tests/prepareTransaction.test.js +60 -0
  66. package/lib-es/bridge/generic-alpaca/tests/prepareTransaction.test.js.map +1 -1
  67. package/lib-es/bridge/generic-alpaca/types.d.ts +0 -1
  68. package/lib-es/bridge/generic-alpaca/types.d.ts.map +1 -1
  69. package/lib-es/exchange/swap/postSwapState.d.ts.map +1 -1
  70. package/lib-es/exchange/swap/postSwapState.js +19 -23
  71. package/lib-es/exchange/swap/postSwapState.js.map +1 -1
  72. package/lib-es/exchange/swap/setBroadcastTransaction.d.ts +4 -4
  73. package/lib-es/exchange/swap/setBroadcastTransaction.js +7 -7
  74. package/lib-es/exchange/swap/setBroadcastTransaction.js.map +1 -1
  75. package/lib-es/exchange/swap/types.d.ts +3 -3
  76. package/lib-es/exchange/swap/types.d.ts.map +1 -1
  77. package/lib-es/families/tezos/config.js +2 -2
  78. package/lib-es/modularDrawer/hooks/modules/useRightMarketTrendModule.js +1 -1
  79. package/lib-es/modularDrawer/hooks/modules/useRightMarketTrendModule.js.map +1 -1
  80. package/lib-es/modularDrawer/hooks/useRightBalanceAsset.d.ts +5 -8
  81. package/lib-es/modularDrawer/hooks/useRightBalanceAsset.d.ts.map +1 -1
  82. package/lib-es/modularDrawer/hooks/useRightBalanceAsset.js +15 -24
  83. package/lib-es/modularDrawer/hooks/useRightBalanceAsset.js.map +1 -1
  84. package/lib-es/modularDrawer/hooks/useRightBalanceNetwork.d.ts +6 -25
  85. package/lib-es/modularDrawer/hooks/useRightBalanceNetwork.d.ts.map +1 -1
  86. package/lib-es/modularDrawer/hooks/useRightBalanceNetwork.js +4 -3
  87. package/lib-es/modularDrawer/hooks/useRightBalanceNetwork.js.map +1 -1
  88. package/lib-es/modularDrawer/modules/createNetworkConfiguration.d.ts.map +1 -1
  89. package/lib-es/modularDrawer/modules/createNetworkConfiguration.js +8 -4
  90. package/lib-es/modularDrawer/modules/createNetworkConfiguration.js.map +1 -1
  91. package/lib-es/modularDrawer/utils/__tests__/calculateProviderTotal.test.d.ts +2 -0
  92. package/lib-es/modularDrawer/utils/__tests__/calculateProviderTotal.test.d.ts.map +1 -0
  93. package/lib-es/modularDrawer/utils/__tests__/calculateProviderTotal.test.js +207 -0
  94. package/lib-es/modularDrawer/utils/__tests__/calculateProviderTotal.test.js.map +1 -0
  95. package/lib-es/modularDrawer/utils/__tests__/groupAccountsByAsset.test.d.ts +2 -0
  96. package/lib-es/modularDrawer/utils/__tests__/groupAccountsByAsset.test.d.ts.map +1 -0
  97. package/lib-es/modularDrawer/utils/__tests__/groupAccountsByAsset.test.js +197 -0
  98. package/lib-es/modularDrawer/utils/__tests__/groupAccountsByAsset.test.js.map +1 -0
  99. package/lib-es/modularDrawer/utils/calculateProviderTotal.d.ts +1 -0
  100. package/lib-es/modularDrawer/utils/calculateProviderTotal.d.ts.map +1 -1
  101. package/lib-es/modularDrawer/utils/calculateProviderTotal.js +10 -2
  102. package/lib-es/modularDrawer/utils/calculateProviderTotal.js.map +1 -1
  103. package/lib-es/modularDrawer/utils/getBalanceAndFiatValueByAssets.d.ts +6 -4
  104. package/lib-es/modularDrawer/utils/getBalanceAndFiatValueByAssets.d.ts.map +1 -1
  105. package/lib-es/modularDrawer/utils/getBalanceAndFiatValueByAssets.js +9 -22
  106. package/lib-es/modularDrawer/utils/getBalanceAndFiatValueByAssets.js.map +1 -1
  107. package/lib-es/modularDrawer/utils/groupAccountsByAsset.d.ts +4 -3
  108. package/lib-es/modularDrawer/utils/groupAccountsByAsset.d.ts.map +1 -1
  109. package/lib-es/modularDrawer/utils/groupAccountsByAsset.js +27 -11
  110. package/lib-es/modularDrawer/utils/groupAccountsByAsset.js.map +1 -1
  111. package/lib-es/modularDrawer/utils/index.d.ts +0 -1
  112. package/lib-es/modularDrawer/utils/index.d.ts.map +1 -1
  113. package/lib-es/modularDrawer/utils/index.js +0 -1
  114. package/lib-es/modularDrawer/utils/index.js.map +1 -1
  115. package/lib-es/modularDrawer/utils/sortByBalance.d.ts +1 -3
  116. package/lib-es/modularDrawer/utils/sortByBalance.d.ts.map +1 -1
  117. package/lib-es/modularDrawer/utils/sortByBalance.js +5 -15
  118. package/lib-es/modularDrawer/utils/sortByBalance.js.map +1 -1
  119. package/lib-es/modularDrawer/utils/type.d.ts +9 -8
  120. package/lib-es/modularDrawer/utils/type.d.ts.map +1 -1
  121. package/lib-es/wallet-api/Exchange/server.js +6 -6
  122. package/lib-es/wallet-api/Exchange/server.js.map +1 -1
  123. package/package.json +70 -70
  124. package/src/bridge/generic-alpaca/prepareTransaction.ts +3 -7
  125. package/src/bridge/generic-alpaca/tests/prepareTransaction.test.ts +78 -0
  126. package/src/bridge/generic-alpaca/types.ts +1 -1
  127. package/src/exchange/swap/postSwapState.ts +28 -32
  128. package/src/exchange/swap/setBroadcastTransaction.ts +12 -12
  129. package/src/exchange/swap/types.ts +3 -3
  130. package/src/families/tezos/config.ts +2 -2
  131. package/src/modularDrawer/hooks/modules/useRightMarketTrendModule.tsx +1 -1
  132. package/src/modularDrawer/hooks/useRightBalanceAsset.tsx +22 -38
  133. package/src/modularDrawer/hooks/useRightBalanceNetwork.tsx +5 -7
  134. package/src/modularDrawer/modules/createNetworkConfiguration.ts +10 -5
  135. package/src/modularDrawer/utils/__tests__/calculateProviderTotal.test.ts +237 -0
  136. package/src/modularDrawer/utils/__tests__/groupAccountsByAsset.test.ts +228 -0
  137. package/src/modularDrawer/utils/calculateProviderTotal.ts +13 -2
  138. package/src/modularDrawer/utils/getBalanceAndFiatValueByAssets.ts +13 -37
  139. package/src/modularDrawer/utils/groupAccountsByAsset.ts +38 -19
  140. package/src/modularDrawer/utils/index.ts +0 -2
  141. package/src/modularDrawer/utils/sortByBalance.tsx +5 -18
  142. package/src/modularDrawer/utils/type.ts +13 -4
  143. package/src/wallet-api/Exchange/server.ts +6 -6
  144. package/lib/modularDrawer/utils/__tests__/counterValueFormatter.test.d.ts +0 -2
  145. package/lib/modularDrawer/utils/__tests__/counterValueFormatter.test.d.ts.map +0 -1
  146. package/lib/modularDrawer/utils/__tests__/counterValueFormatter.test.js +0 -496
  147. package/lib/modularDrawer/utils/__tests__/counterValueFormatter.test.js.map +0 -1
  148. package/lib/modularDrawer/utils/counterValueFormatter.d.ts +0 -14
  149. package/lib/modularDrawer/utils/counterValueFormatter.d.ts.map +0 -1
  150. package/lib/modularDrawer/utils/counterValueFormatter.js +0 -107
  151. package/lib/modularDrawer/utils/counterValueFormatter.js.map +0 -1
  152. package/lib/modularDrawer/utils/getBalanceAndFiatValue.d.ts +0 -20
  153. package/lib/modularDrawer/utils/getBalanceAndFiatValue.d.ts.map +0 -1
  154. package/lib/modularDrawer/utils/getBalanceAndFiatValue.js +0 -39
  155. package/lib/modularDrawer/utils/getBalanceAndFiatValue.js.map +0 -1
  156. package/lib/modularDrawer/utils/parseToBigNumber.d.ts +0 -9
  157. package/lib/modularDrawer/utils/parseToBigNumber.d.ts.map +0 -1
  158. package/lib/modularDrawer/utils/parseToBigNumber.js +0 -24
  159. package/lib/modularDrawer/utils/parseToBigNumber.js.map +0 -1
  160. package/lib-es/modularDrawer/utils/__tests__/counterValueFormatter.test.d.ts +0 -2
  161. package/lib-es/modularDrawer/utils/__tests__/counterValueFormatter.test.d.ts.map +0 -1
  162. package/lib-es/modularDrawer/utils/__tests__/counterValueFormatter.test.js +0 -494
  163. package/lib-es/modularDrawer/utils/__tests__/counterValueFormatter.test.js.map +0 -1
  164. package/lib-es/modularDrawer/utils/counterValueFormatter.d.ts +0 -14
  165. package/lib-es/modularDrawer/utils/counterValueFormatter.d.ts.map +0 -1
  166. package/lib-es/modularDrawer/utils/counterValueFormatter.js +0 -103
  167. package/lib-es/modularDrawer/utils/counterValueFormatter.js.map +0 -1
  168. package/lib-es/modularDrawer/utils/getBalanceAndFiatValue.d.ts +0 -20
  169. package/lib-es/modularDrawer/utils/getBalanceAndFiatValue.d.ts.map +0 -1
  170. package/lib-es/modularDrawer/utils/getBalanceAndFiatValue.js +0 -32
  171. package/lib-es/modularDrawer/utils/getBalanceAndFiatValue.js.map +0 -1
  172. package/lib-es/modularDrawer/utils/parseToBigNumber.d.ts +0 -9
  173. package/lib-es/modularDrawer/utils/parseToBigNumber.d.ts.map +0 -1
  174. package/lib-es/modularDrawer/utils/parseToBigNumber.js +0 -17
  175. package/lib-es/modularDrawer/utils/parseToBigNumber.js.map +0 -1
  176. package/src/modularDrawer/utils/__tests__/counterValueFormatter.test.ts +0 -549
  177. package/src/modularDrawer/utils/counterValueFormatter.ts +0 -165
  178. package/src/modularDrawer/utils/getBalanceAndFiatValue.ts +0 -53
  179. package/src/modularDrawer/utils/parseToBigNumber.ts +0 -17
@@ -0,0 +1,228 @@
1
+ import { groupAccountsByAsset } from "../groupAccountsByAsset";
2
+ import { createFixtureCryptoCurrency } from "../../../mock/fixtures/cryptoCurrencies";
3
+ import BigNumber from "bignumber.js";
4
+ import type { Account, TokenAccount } from "@ledgerhq/types-live";
5
+ import type { CounterValuesState } from "@ledgerhq/live-countervalues/types";
6
+
7
+ // Mock calculate function
8
+ jest.mock("@ledgerhq/live-countervalues/logic", () => ({
9
+ calculate: jest.fn((state, { value }) => {
10
+ // Mock: return the balance directly as fiat value for simple testing
11
+ return value;
12
+ }),
13
+ }));
14
+
15
+ describe("groupAccountsByAsset", () => {
16
+ const mockCurrency = createFixtureCryptoCurrency("bitcoin");
17
+ const mockEthCurrency = createFixtureCryptoCurrency("ethereum");
18
+
19
+ const mockCounterValuesState = {} as CounterValuesState;
20
+ const mockTargetCurrency = createFixtureCryptoCurrency("usd");
21
+
22
+ const mockUSDCToken = {
23
+ type: "TokenCurrency" as const,
24
+ id: "ethereum/erc20/usdc",
25
+ contractAddress: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
26
+ parentCurrency: mockEthCurrency,
27
+ tokenType: "erc20" as const,
28
+ name: "USD Coin",
29
+ ticker: "USDC",
30
+ units: [{ name: "USD Coin", code: "USDC", magnitude: 6 }],
31
+ };
32
+
33
+ const mockBTCAccount: Account = {
34
+ type: "Account",
35
+ id: "btc-account-1",
36
+ seedIdentifier: "seed-id",
37
+ derivationMode: "",
38
+ currency: mockCurrency,
39
+ balance: new BigNumber(100000000), // 1 BTC
40
+ spendableBalance: new BigNumber(100000000),
41
+ blockHeight: 0,
42
+ operations: [],
43
+ operationsCount: 0,
44
+ pendingOperations: [],
45
+ index: 0,
46
+ freshAddress: "btc-address",
47
+ freshAddressPath: "44'/0'/0'/0/0",
48
+ used: true,
49
+ swapHistory: [],
50
+ nfts: [],
51
+ lastSyncDate: new Date(),
52
+ creationDate: new Date(),
53
+ balanceHistoryCache: {
54
+ HOUR: { balances: [], latestDate: null },
55
+ DAY: { balances: [], latestDate: null },
56
+ WEEK: { balances: [], latestDate: null },
57
+ },
58
+ };
59
+
60
+ const mockUSDCAccount: TokenAccount = {
61
+ type: "TokenAccount",
62
+ id: "usdc-account-1",
63
+ token: mockUSDCToken,
64
+ parentId: "eth-account-1",
65
+ balance: new BigNumber(1000000), // 1 USDC
66
+ spendableBalance: new BigNumber(1000000),
67
+ operations: [],
68
+ operationsCount: 0,
69
+ pendingOperations: [],
70
+ swapHistory: [],
71
+ creationDate: new Date(),
72
+ balanceHistoryCache: {
73
+ HOUR: { balances: [], latestDate: null },
74
+ DAY: { balances: [], latestDate: null },
75
+ WEEK: { balances: [], latestDate: null },
76
+ },
77
+ };
78
+
79
+ it("should group accounts by asset ID and aggregate balances", () => {
80
+ const accounts = [mockBTCAccount, mockUSDCAccount];
81
+ const result = groupAccountsByAsset(accounts, mockCounterValuesState, mockTargetCurrency);
82
+
83
+ expect(result).toEqual({
84
+ [mockCurrency.id]: {
85
+ totalBalance: new BigNumber(100000000), // 1 BTC in satoshis
86
+ totalFiatValue: new BigNumber(100000000), // Mock returns balance as fiat value
87
+ accounts: [mockBTCAccount],
88
+ referenceCurrency: mockCurrency,
89
+ },
90
+ [mockUSDCToken.id]: {
91
+ totalBalance: new BigNumber(1000000), // 1 USDC in micro-USDC
92
+ totalFiatValue: new BigNumber(1000000), // Mock returns balance as fiat value
93
+ accounts: [mockUSDCAccount],
94
+ referenceCurrency: mockUSDCToken,
95
+ },
96
+ });
97
+ });
98
+
99
+ it("should aggregate multiple accounts with the same asset", () => {
100
+ const secondBTCAccount: Account = {
101
+ ...mockBTCAccount,
102
+ id: "btc-account-2",
103
+ balance: new BigNumber(50000000), // 0.5 BTC
104
+ };
105
+
106
+ const accounts = [mockBTCAccount, secondBTCAccount];
107
+ const result = groupAccountsByAsset(accounts, mockCounterValuesState, mockTargetCurrency);
108
+
109
+ expect(result[mockCurrency.id]).toEqual({
110
+ totalBalance: new BigNumber(150000000), // 1.5 BTC total
111
+ totalFiatValue: new BigNumber(150000000), // Mock returns balance as fiat value
112
+ accounts: [mockBTCAccount, secondBTCAccount],
113
+ referenceCurrency: mockCurrency,
114
+ });
115
+ });
116
+
117
+ it("should handle empty accounts array", () => {
118
+ const result = groupAccountsByAsset([], mockCounterValuesState, mockTargetCurrency);
119
+ expect(result).toEqual({});
120
+ });
121
+
122
+ it("should handle accounts with zero balance", () => {
123
+ const zeroBalanceAccount: Account = {
124
+ ...mockBTCAccount,
125
+ balance: new BigNumber(0),
126
+ };
127
+
128
+ const accounts = [zeroBalanceAccount];
129
+ const result = groupAccountsByAsset(accounts, mockCounterValuesState, mockTargetCurrency);
130
+
131
+ expect(result[mockCurrency.id]).toEqual({
132
+ totalBalance: new BigNumber(0),
133
+ totalFiatValue: new BigNumber(0), // Mock returns balance (0) as fiat value
134
+ accounts: [zeroBalanceAccount],
135
+ referenceCurrency: mockCurrency,
136
+ });
137
+ });
138
+
139
+ it("should set referenceCurrency to the first currency encountered for each asset group", () => {
140
+ const accounts = [mockBTCAccount, mockUSDCAccount];
141
+ const result = groupAccountsByAsset(accounts, mockCounterValuesState, mockTargetCurrency);
142
+
143
+ expect(result[mockCurrency.id].referenceCurrency).toEqual(mockCurrency);
144
+ expect(result[mockUSDCToken.id].referenceCurrency).toEqual(mockUSDCToken);
145
+ });
146
+
147
+ it("should normalize balances when tokens have different magnitudes", () => {
148
+ // Create BSC USDC with different magnitude (18 decimals instead of 6)
149
+ const mockBscUSDCToken = {
150
+ ...mockUSDCToken,
151
+ id: "bsc/bep20/usdc",
152
+ name: "USD Coin (BSC)",
153
+ ticker: "USDC",
154
+ units: [{ name: "USD Coin", code: "USDC", magnitude: 18 }], // 18 decimals
155
+ };
156
+
157
+ const mockEthUSDCAccount: TokenAccount = {
158
+ ...mockUSDCAccount,
159
+ id: "eth-usdc-account",
160
+ token: mockUSDCToken, // 6 decimals
161
+ balance: new BigNumber(1000000), // 1 USDC with 6 decimals
162
+ };
163
+
164
+ const mockBscUSDCAccount: TokenAccount = {
165
+ ...mockUSDCAccount,
166
+ id: "bsc-usdc-account",
167
+ token: mockBscUSDCToken, // 18 decimals
168
+ balance: new BigNumber("1000000000000000000"), // 1 USDC with 18 decimals
169
+ };
170
+
171
+ // Both should be grouped under the same token ID for this test
172
+ // First account (ETH USDC) becomes reference currency with 6 decimals
173
+ const accounts = [mockEthUSDCAccount, mockBscUSDCAccount];
174
+ const result = groupAccountsByAsset(accounts, mockCounterValuesState, mockTargetCurrency);
175
+
176
+ const ethGroup = result[mockUSDCToken.id];
177
+ expect(ethGroup.referenceCurrency).toEqual(mockUSDCToken); // First currency is reference
178
+ expect(ethGroup.accounts).toHaveLength(1);
179
+ expect(ethGroup.totalBalance).toEqual(new BigNumber(1000000)); // 1 USDC normalized to 6 decimals
180
+
181
+ const bscGroup = result[mockBscUSDCToken.id];
182
+ expect(bscGroup.referenceCurrency).toEqual(mockBscUSDCToken);
183
+ expect(bscGroup.accounts).toHaveLength(1);
184
+ expect(bscGroup.totalBalance).toEqual(new BigNumber("1000000000000000000")); // 1 USDC with 18 decimals
185
+ });
186
+
187
+ it("should normalize balances correctly when adding accounts with different magnitudes to same asset group", () => {
188
+ // Create two USDC tokens with same ID but different magnitudes
189
+ const ethUSDCToken = {
190
+ ...mockUSDCToken,
191
+ units: [{ name: "USD Coin", code: "USDC", magnitude: 6 }], // 6 decimals
192
+ };
193
+
194
+ const bscUSDCToken = {
195
+ ...mockUSDCToken,
196
+ units: [{ name: "USD Coin", code: "USDC", magnitude: 18 }], // 18 decimals
197
+ };
198
+
199
+ const ethAccount: TokenAccount = {
200
+ ...mockUSDCAccount,
201
+ id: "eth-usdc",
202
+ token: ethUSDCToken,
203
+ balance: new BigNumber(1000000), // 1 USDC with 6 decimals
204
+ };
205
+
206
+ const bscAccount: TokenAccount = {
207
+ ...mockUSDCAccount,
208
+ id: "bsc-usdc",
209
+ token: bscUSDCToken,
210
+ balance: new BigNumber("1000000000000000000"), // 1 USDC with 18 decimals
211
+ };
212
+
213
+ // Process ETH account first to make it the reference currency
214
+ const accounts = [ethAccount, bscAccount];
215
+ const result = groupAccountsByAsset(accounts, mockCounterValuesState, mockTargetCurrency);
216
+
217
+ const group = result[mockUSDCToken.id];
218
+ expect(group.referenceCurrency).toEqual(ethUSDCToken); // First currency is reference (6 decimals)
219
+ expect(group.accounts).toHaveLength(2);
220
+
221
+ // BSC balance should be normalized from 18 decimals to 6 decimals
222
+ // 1000000000000000000 (18 decimals) -> 1000000 (6 decimals)
223
+ // magnitudeDiff = 6 - 18 = -12
224
+ // normalized = 1000000000000000000 * 10^(-12) = 1000000
225
+ const expectedTotal = new BigNumber(1000000).plus(new BigNumber(1000000)); // 2 USDC total
226
+ expect(group.totalBalance).toEqual(expectedTotal);
227
+ });
228
+ });
@@ -9,15 +9,26 @@ export const calculateProviderTotals = (
9
9
  let totalBalance = new BigNumber(0);
10
10
  let totalFiatValue = new BigNumber(0);
11
11
  let hasAccounts = false;
12
+ let referenceCurrency: CryptoOrTokenCurrency | null = null;
12
13
 
13
14
  for (const currency of currencies) {
14
15
  const assetGroup = groupedAccountsByAsset[currency.id];
15
16
  if (assetGroup?.accounts.length > 0) {
16
- totalBalance = totalBalance.plus(assetGroup.totalBalance);
17
+ const refCurrency = referenceCurrency ?? assetGroup.referenceCurrency;
18
+ const magnitudeDiff =
19
+ refCurrency.units[0].magnitude - assetGroup.referenceCurrency.units[0].magnitude;
20
+ const normalizedBalance = assetGroup.totalBalance.shiftedBy(magnitudeDiff);
21
+
22
+ totalBalance = totalBalance.plus(normalizedBalance);
17
23
  totalFiatValue = totalFiatValue.plus(assetGroup.totalFiatValue);
18
24
  hasAccounts = true;
25
+
26
+ // Use the referenceCurrency from the first asset group with accounts
27
+ if (!referenceCurrency) {
28
+ referenceCurrency = assetGroup.referenceCurrency;
29
+ }
19
30
  }
20
31
  }
21
32
 
22
- return { totalBalance, totalFiatValue, hasAccounts };
33
+ return { totalBalance, totalFiatValue, hasAccounts, referenceCurrency };
23
34
  };
@@ -1,14 +1,14 @@
1
- import { formatCurrencyUnit } from "@ledgerhq/coin-framework/lib-es/currencies/formatCurrencyUnit";
2
- import type { CounterValuesState } from "@ledgerhq/live-countervalues/types";
3
1
  import type { CryptoOrTokenCurrency, Currency } from "@ledgerhq/types-cryptoassets";
4
2
  import { groupAccountsByAsset, type GroupedAccount } from "./groupAccountsByAsset";
5
3
  import { AssetType } from "./type";
6
4
  import { AccountLike } from "@ledgerhq/types-live";
7
- import { counterValueFormatter } from "./counterValueFormatter";
5
+ import BigNumber from "bignumber.js";
6
+ import type { CounterValuesState } from "@ledgerhq/live-countervalues/types";
8
7
 
9
8
  interface ExtendedAssetType extends AssetType {
10
- balance?: string;
11
- fiatValue?: string;
9
+ currency?: CryptoOrTokenCurrency;
10
+ balance?: BigNumber;
11
+ fiatValue?: number;
12
12
  }
13
13
 
14
14
  export const getBalanceAndFiatValueByAssets = (
@@ -16,21 +16,14 @@ export const getBalanceAndFiatValueByAssets = (
16
16
  assets: CryptoOrTokenCurrency[],
17
17
  counterValuesState: CounterValuesState,
18
18
  targetCurrency: Currency,
19
- isDiscreetMode: boolean = false,
20
- userLocale: string,
21
19
  ): ExtendedAssetType[] => {
22
- const groupedAccountsByAsset = groupAccountsByAsset(
23
- accounts,
24
- counterValuesState,
25
- targetCurrency,
26
- isDiscreetMode,
27
- );
20
+ const groupedAccountsByAsset = groupAccountsByAsset(accounts, counterValuesState, targetCurrency);
28
21
 
29
22
  return assets.map(asset => {
30
23
  const assetGroup = groupedAccountsByAsset[asset.id];
31
24
 
32
25
  if (assetGroup) {
33
- return formatAssetDetails(assetGroup, asset, targetCurrency, isDiscreetMode, userLocale);
26
+ return yieldAssetDetails(assetGroup, asset);
34
27
  }
35
28
 
36
29
  return {
@@ -41,36 +34,19 @@ export const getBalanceAndFiatValueByAssets = (
41
34
  });
42
35
  };
43
36
 
44
- const formatAssetDetails = (
37
+ const yieldAssetDetails = (
45
38
  assetGroup: GroupedAccount,
46
39
  asset: CryptoOrTokenCurrency,
47
- targetCurrency: Currency,
48
- isDiscreetMode: boolean,
49
- userLocale: string,
50
40
  ): ExtendedAssetType => {
51
- const assetDetails =
52
- assetGroup.accounts[0].type === "Account"
53
- ? assetGroup.accounts[0].currency
54
- : assetGroup.accounts[0].token;
55
-
56
- const formattedBalance = formatCurrencyUnit(assetDetails.units[0], assetGroup.totalBalance, {
57
- showCode: true,
58
- discreet: isDiscreetMode,
59
- });
60
-
61
- const formattedFiatValue = counterValueFormatter({
62
- currency: targetCurrency.ticker,
63
- value: assetGroup.totalFiatValue.toNumber(),
64
- locale: userLocale,
65
- allowZeroValue: true,
66
- discreetMode: isDiscreetMode,
67
- });
41
+ // Use the reference currency for consistent magnitude
42
+ const assetDetails = assetGroup.referenceCurrency;
68
43
 
69
44
  return {
70
45
  id: asset.id,
71
46
  name: asset.name,
72
47
  ticker: asset.ticker,
73
- balance: formattedBalance,
74
- fiatValue: formattedFiatValue,
48
+ currency: assetDetails,
49
+ balance: assetGroup.totalBalance,
50
+ fiatValue: assetGroup.totalFiatValue.toNumber(),
75
51
  };
76
52
  };
@@ -1,51 +1,70 @@
1
- import { getBalanceAndFiatValue } from "./getBalanceAndFiatValue";
2
- import type { CounterValuesState } from "@ledgerhq/live-countervalues/types";
3
- import { Currency } from "@ledgerhq/types-cryptoassets";
4
1
  import { AccountLike } from "@ledgerhq/types-live";
5
2
  import BigNumber from "bignumber.js";
6
- import { parseToBigNumber } from "./parseToBigNumber";
3
+ import type { CounterValuesState } from "@ledgerhq/live-countervalues/types";
4
+ import { CryptoOrTokenCurrency, Currency } from "@ledgerhq/types-cryptoassets";
5
+ import { calculate } from "@ledgerhq/live-countervalues/logic";
7
6
 
8
7
  export type GroupedAccount = {
9
8
  totalBalance: BigNumber;
10
9
  totalFiatValue: BigNumber;
11
10
  accounts: AccountLike[];
11
+ referenceCurrency: CryptoOrTokenCurrency; // Reference currency for magnitude normalization
12
12
  };
13
13
 
14
14
  export const groupAccountsByAsset = (
15
15
  accounts: AccountLike[],
16
16
  counterValuesState: CounterValuesState,
17
17
  targetCurrency: Currency,
18
- isDiscreetMode: boolean,
19
18
  ): Record<string, GroupedAccount> => {
20
19
  const initialGroupedAccounts: Record<string, GroupedAccount> = {};
21
20
 
22
21
  return accounts.reduce((groupedAccounts, account) => {
23
22
  const assetId = account.type === "Account" ? account.currency.id : account.token.id;
24
23
 
25
- if (!groupedAccounts[assetId]) {
26
- groupedAccounts[assetId] = {
24
+ let assetGroup = groupedAccounts[assetId];
25
+ if (!assetGroup) {
26
+ const currency = account.type === "Account" ? account.currency : account.token;
27
+ assetGroup = groupedAccounts[assetId] = {
27
28
  totalBalance: new BigNumber(0),
28
29
  totalFiatValue: new BigNumber(0),
29
30
  accounts: [],
31
+ referenceCurrency: currency, // First currency becomes the reference
30
32
  };
31
33
  }
32
34
 
33
- const { fiatValue } = getBalanceAndFiatValue(
34
- account,
35
- counterValuesState,
36
- targetCurrency,
37
- isDiscreetMode,
38
- false,
39
- );
35
+ const currency = account.type === "Account" ? account.currency : account.token;
40
36
  const balance = account.balance;
41
37
 
42
- const parsedFiatValue = parseToBigNumber(fiatValue ?? "0");
38
+ // Convert balance to reference currency magnitude before summing
39
+ const referenceMagnitude = assetGroup.referenceCurrency.units[0].magnitude || 0;
40
+ const currentMagnitude = currency.units[0].magnitude || 0;
41
+ const magnitudeDiff = referenceMagnitude - currentMagnitude;
42
+ const normalizedBalance = balance.shiftedBy(magnitudeDiff);
43
43
 
44
- groupedAccounts[assetId].totalBalance = groupedAccounts[assetId].totalBalance.plus(balance);
45
- groupedAccounts[assetId].totalFiatValue =
46
- groupedAccounts[assetId].totalFiatValue.plus(parsedFiatValue);
47
- groupedAccounts[assetId].accounts.push(account);
44
+ const fiatValue = calculateFiatValue(account, counterValuesState, targetCurrency);
45
+
46
+ assetGroup.totalBalance = assetGroup.totalBalance.plus(normalizedBalance);
47
+ assetGroup.totalFiatValue = assetGroup.totalFiatValue.plus(fiatValue || 0);
48
+ assetGroup.accounts.push(account);
48
49
 
49
50
  return groupedAccounts;
50
51
  }, initialGroupedAccounts);
51
52
  };
53
+
54
+ function calculateFiatValue(
55
+ account: AccountLike,
56
+ state: CounterValuesState,
57
+ toCurrency: Currency,
58
+ ): number {
59
+ const currency = account.type === "Account" ? account.currency : account.token;
60
+
61
+ const balanceNumber = account.balance.toNumber();
62
+
63
+ const fiatValue = calculate(state, {
64
+ from: currency,
65
+ to: toCurrency,
66
+ value: balanceNumber,
67
+ });
68
+
69
+ return fiatValue || 0;
70
+ }
@@ -1,6 +1,4 @@
1
1
  import { CryptoOrTokenCurrency } from "@ledgerhq/types-cryptoassets";
2
-
3
- export { getBalanceAndFiatValue } from "./getBalanceAndFiatValue";
4
2
  export { groupCurrenciesByProvider } from "./groupCurrenciesByProvider";
5
3
 
6
4
  function isCorrespondingCurrency(
@@ -1,32 +1,19 @@
1
1
  import { BalanceUI } from "./type";
2
2
 
3
- export const parseFiatValue = (value?: string, discreet?: boolean) => {
4
- if (!value || discreet) return -1;
5
- return parseFloat(value.replace(/[^0-9.-]+/g, "")) || -1;
6
- };
7
-
8
- export function toFiatString(x?: BalanceUI): string | undefined {
9
- return x && "fiatValue" in x ? String(x.fiatValue) : undefined;
10
- }
11
-
12
3
  export function hasBalance(balanceData?: BalanceUI): boolean {
13
- return (
14
- !!balanceData &&
15
- (("fiatValue" in balanceData && !!balanceData.fiatValue) ||
16
- ("balance" in balanceData && !!balanceData.balance))
17
- );
4
+ return !!balanceData && balanceData.balance !== undefined && balanceData.balance.gt(0);
18
5
  }
19
6
 
20
7
  export function compareByBalanceThenFiat(
21
8
  a: BalanceUI | undefined,
22
9
  b: BalanceUI | undefined,
23
- discreet: boolean,
24
10
  ): number {
25
11
  const ah = hasBalance(a);
26
12
  const bh = hasBalance(b);
27
13
  if (ah && !bh) return -1;
28
14
  if (!ah && bh) return 1;
29
- const af = parseFiatValue(toFiatString(a), discreet);
30
- const bf = parseFiatValue(toFiatString(b), discreet);
31
- return bf - af;
15
+
16
+ const aFiat = a?.fiatValue ?? 0;
17
+ const bFiat = b?.fiatValue ?? 0;
18
+ return bFiat - aFiat;
32
19
  }
@@ -1,5 +1,5 @@
1
1
  import { CounterValuesState } from "@ledgerhq/live-countervalues/lib/types";
2
- import { CryptoOrTokenCurrency, Currency } from "@ledgerhq/types-cryptoassets";
2
+ import { CryptoOrTokenCurrency, Currency, Unit } from "@ledgerhq/types-cryptoassets";
3
3
  import { AccountLike } from "@ledgerhq/types-live";
4
4
  import { ReactNode } from "react";
5
5
  import { Observable } from "rxjs";
@@ -7,6 +7,7 @@ import { WalletAPIAccount } from "../../wallet-api/types";
7
7
  import { EnhancedModularDrawerConfiguration } from "../../wallet-api/ModularDrawer/types";
8
8
  import { InterestRate } from "../../dada-client/entities";
9
9
  import { MarketItemResponse } from "../../market/utils/types";
10
+ import BigNumber from "bignumber.js";
10
11
  import { ApyType } from "../../dada-client/types/trend";
11
12
 
12
13
  export type AssetType = {
@@ -34,13 +35,21 @@ export type AssetWithBalance = AssetType & {
34
35
 
35
36
  export type UseBalanceDeps = () => {
36
37
  flattenedAccounts: AccountLike[];
37
- discreet: boolean;
38
38
  state: CounterValuesState;
39
39
  counterValueCurrency: Currency;
40
40
  locale: string;
41
41
  };
42
42
 
43
- export type BalanceUI = { balance?: string; fiatValue?: string };
43
+ export type BalanceUI = {
44
+ // Raw values
45
+ balance?: BigNumber;
46
+ fiatValue?: number;
47
+ fiatUnit?: Unit;
48
+ currency?: CryptoOrTokenCurrency;
49
+ // Formatting parameters
50
+ locale?: string;
51
+ discreet?: boolean;
52
+ };
44
53
  export type CreateBalanceItem = (x: BalanceUI) => React.ReactNode;
45
54
 
46
55
  export type CreateAccountsCountAndApy = (args: {
@@ -111,7 +120,7 @@ export type AssetConfigurationDeps = {
111
120
  MarketPercentIndicator: (args: { percent: number }) => ReactNode;
112
121
  MarketPriceIndicator: (args: { price: string; percent: number }) => ReactNode;
113
122
  useBalanceDeps: UseBalanceDeps;
114
- balanceItem: (asset: { fiatValue?: string; balance?: string }) => ReactNode;
123
+ balanceItem: CreateBalanceItem;
115
124
  assetsMap: Map<
116
125
  string,
117
126
  { mainCurrency: CryptoOrTokenCurrency; currencies: CryptoOrTokenCurrency[] }
@@ -567,9 +567,9 @@ export const handlers = ({
567
567
  targetCurrencyId: toCurrency?.id,
568
568
  hardwareWalletType: deviceInfo?.modelId as DeviceModelId,
569
569
  swapAppVersion,
570
- fromAccountId: fromAccount.id,
571
- toAccountId: toAccount?.id,
572
- amount: amountExpectedTo.toString(),
570
+ fromAccountAddress,
571
+ toAccountAddress,
572
+ fromAmount,
573
573
  });
574
574
 
575
575
  resolve({ operationHash, swapId });
@@ -586,11 +586,11 @@ export const handlers = ({
586
586
  hardwareWalletType: deviceInfo?.modelId as DeviceModelId,
587
587
  swapType: quoteId ? "fixed" : "float",
588
588
  swapAppVersion,
589
- fromAccountId: fromAccount.id,
590
- toAccountId: toAccount?.id,
589
+ fromAccountAddress,
590
+ toAccountAddress,
591
591
  refundAddress,
592
592
  payoutAddress,
593
- amount: amountExpectedTo.toString(),
593
+ fromAmount,
594
594
  seedIdFrom: mainFromAccount.seedIdentifier,
595
595
  seedIdTo: toParentAccount?.seedIdentifier || (toAccount as Account)?.seedIdentifier,
596
596
  });
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=counterValueFormatter.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"counterValueFormatter.test.d.ts","sourceRoot":"","sources":["../../../../src/modularDrawer/utils/__tests__/counterValueFormatter.test.ts"],"names":[],"mappings":""}