@rango-dev/widget-embedded 0.30.1-next.3 → 0.30.1-next.5

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 (40) hide show
  1. package/dist/components/Quote/Quote.styles.d.ts +1 -1
  2. package/dist/components/RefreshModal/RefreshModal.styles.d.ts +1 -1
  3. package/dist/containers/Wallets/Wallets.d.ts.map +1 -1
  4. package/dist/hooks/usePrepareBlockchainList/usePrepareBlockchainList.helpers.d.ts +2 -2
  5. package/dist/index.js +2 -2
  6. package/dist/index.js.map +4 -4
  7. package/dist/services/cacheService.d.ts +21 -0
  8. package/dist/services/cacheService.d.ts.map +1 -0
  9. package/dist/store/notification.d.ts.map +1 -1
  10. package/dist/store/slices/config.d.ts +2 -1
  11. package/dist/store/slices/config.d.ts.map +1 -1
  12. package/dist/store/slices/data.d.ts +2 -1
  13. package/dist/store/slices/data.d.ts.map +1 -1
  14. package/dist/store/utils/data.d.ts +20 -0
  15. package/dist/store/utils/data.d.ts.map +1 -0
  16. package/dist/store/utils/data.test.d.ts +2 -0
  17. package/dist/store/utils/data.test.d.ts.map +1 -0
  18. package/dist/store/utils/index.d.ts +2 -0
  19. package/dist/store/utils/index.d.ts.map +1 -0
  20. package/dist/test-utils/fixtures.d.ts +1 -0
  21. package/dist/test-utils/fixtures.d.ts.map +1 -1
  22. package/dist/types/notification.d.ts +1 -0
  23. package/dist/types/notification.d.ts.map +1 -1
  24. package/dist/types/wallets.d.ts +1 -1
  25. package/dist/types/wallets.d.ts.map +1 -1
  26. package/dist/utils/settings.d.ts +1 -1
  27. package/dist/widget-embedded.build.json +1 -1
  28. package/package.json +2 -2
  29. package/src/containers/Wallets/Wallets.tsx +7 -2
  30. package/src/services/cacheService.ts +35 -0
  31. package/src/store/notification.ts +13 -1
  32. package/src/store/slices/config.ts +39 -1
  33. package/src/store/slices/data.test.ts +84 -0
  34. package/src/store/slices/data.ts +44 -24
  35. package/src/store/utils/data.test.ts +175 -0
  36. package/src/store/utils/data.ts +117 -0
  37. package/src/store/utils/index.ts +1 -0
  38. package/src/test-utils/fixtures.ts +14 -7
  39. package/src/types/notification.ts +1 -0
  40. package/src/types/wallets.ts +1 -1
@@ -0,0 +1,117 @@
1
+ import type { BlockchainAndTokenConfig, TokenHash } from '../../types';
2
+ import type { DataSlice } from '../slices/data';
3
+ import type { Asset, Token } from 'rango-sdk';
4
+
5
+ import { createTokenHash } from '../../utils/meta';
6
+
7
+ /**
8
+ * This function retrieves tokens and blockchains from the widget configuration and combines them with token data from the meta response to determine which tokens should be displayed in the widget.
9
+ * To optimize performance, the function minimizes unnecessary iterations over the meta tokens.
10
+ * The result of this function should be cached and recalculated whenever the widget configuration changes.
11
+ */
12
+ export function matchTokensFromConfigWithMeta(params: {
13
+ type: 'source' | 'destination';
14
+ config: {
15
+ blockchains: BlockchainAndTokenConfig['blockchains'];
16
+ tokens: BlockchainAndTokenConfig['tokens'];
17
+ };
18
+ meta: {
19
+ tokensMapByTokenHash: DataSlice['_tokensMapByTokenHash'];
20
+ tokensMapByBlockchainName: DataSlice['_tokensMapByBlockchainName'];
21
+ };
22
+ }): Token[] {
23
+ const { config, meta } = params;
24
+ const result: Record<TokenHash, Token> = {};
25
+ const configTokens = config.tokens;
26
+
27
+ // Helper function to add tokens to result
28
+ const addTokens = (tokens: (Asset | TokenHash)[]) => {
29
+ tokens.forEach((item) => {
30
+ if (typeof item !== 'string') {
31
+ item = createTokenHash(item);
32
+ }
33
+ const tokenFromMeta = meta.tokensMapByTokenHash.get(item);
34
+ if (tokenFromMeta) {
35
+ result[item] = tokenFromMeta;
36
+ }
37
+ });
38
+ };
39
+
40
+ /**
41
+ * We support two types of configurations for passing tokens in the widget config.
42
+ * We check the type of configuration and calculate the result based on that.
43
+ */
44
+ if (Array.isArray(configTokens)) {
45
+ /**
46
+ * If "config.tokens" is an array,
47
+ * we iterate through it and include any token that exists in the meta data in the result.
48
+ */
49
+ if (configTokens.length > 0) {
50
+ addTokens(configTokens);
51
+ return Object.values(result);
52
+ }
53
+ return Array.from(meta.tokensMapByTokenHash.values());
54
+ }
55
+
56
+ /**
57
+ * From this point onward,
58
+ * the function handles the other type of widget configuration.
59
+ */
60
+ if (!configTokens) {
61
+ return Array.from(meta.tokensMapByTokenHash.values());
62
+ }
63
+
64
+ let configBlockchains: string[];
65
+
66
+ if (config.blockchains?.length) {
67
+ configBlockchains = config.blockchains;
68
+ } else {
69
+ /**
70
+ * If config.blockchains does not exist,
71
+ * we include all tokens from every blockchain in the meta data that are not specified in the tokens from the config.
72
+ */
73
+ configBlockchains = Object.keys(configTokens);
74
+ const configBlockchainsSet = new Set(configBlockchains);
75
+
76
+ Object.keys(meta.tokensMapByBlockchainName).forEach((blockchain) => {
77
+ if (!configBlockchainsSet.has(blockchain)) {
78
+ addTokens(meta.tokensMapByBlockchainName[blockchain]);
79
+ }
80
+ });
81
+ }
82
+ //We iterate over each blockchain and retrieve the related tokens for each one from the configuration.
83
+ configBlockchains.forEach((blockchain) => {
84
+ const targetTokensForBlockchain = configTokens[blockchain];
85
+ /**
86
+ * If no tokens exist for a blockchain,
87
+ * we include all tokens for that blockchain from the meta response.
88
+ */
89
+ if (
90
+ !targetTokensForBlockchain &&
91
+ meta.tokensMapByBlockchainName?.[blockchain]
92
+ ) {
93
+ addTokens(meta.tokensMapByBlockchainName[blockchain]);
94
+ return;
95
+ }
96
+
97
+ if (targetTokensForBlockchain) {
98
+ if (targetTokensForBlockchain.isExcluded) {
99
+ /**
100
+ * If tokens are excluded,
101
+ * we first include all tokens from the meta for that blockchain,
102
+ * then remove the excluded tokens from the result.
103
+ */
104
+ addTokens(meta.tokensMapByBlockchainName[blockchain]);
105
+ targetTokensForBlockchain.tokens.forEach((token) => {
106
+ const tokenHash = createTokenHash(token);
107
+ delete result[tokenHash];
108
+ });
109
+ } else {
110
+ //We retrieve the corresponding token from the meta and include it in the result.
111
+ addTokens(targetTokensForBlockchain.tokens);
112
+ }
113
+ }
114
+ });
115
+
116
+ return Object.values(result);
117
+ }
@@ -0,0 +1 @@
1
+ export * from './data';
@@ -1,5 +1,5 @@
1
1
  import type { AppStoreState } from '../store/app';
2
- import type { TokenHash, WidgetConfig } from '../types';
2
+ import type { Blockchain, TokenHash, WidgetConfig } from '../types';
3
3
  import type { BlockchainMeta, EvmBlockchainMeta, Token } from 'rango-sdk';
4
4
 
5
5
  import { faker } from '@faker-js/faker';
@@ -231,13 +231,20 @@ export function createInitialAppStore() {
231
231
  blockchains: blockchains.map((b) => b.name),
232
232
  });
233
233
 
234
+ const tokensMapByTokenHash: Map<TokenHash, Token> = new Map();
235
+ const tokensMapByBlockchainName: Record<Blockchain, TokenHash[]> = {};
236
+ tokens.forEach((token) => {
237
+ const tokenHash = createTokenHash(token);
238
+ if (!tokensMapByBlockchainName[token.blockchain]) {
239
+ tokensMapByBlockchainName[token.blockchain] = [];
240
+ }
241
+ tokensMapByTokenHash.set(tokenHash, token);
242
+ tokensMapByBlockchainName[token.blockchain].push(tokenHash);
243
+ });
244
+
234
245
  return {
235
- _tokensMapByTokenHash: new Map<TokenHash, Token>(
236
- tokens.map((token) => {
237
- const tokenHash = createTokenHash(token);
238
- return [tokenHash, token];
239
- })
240
- ),
246
+ _tokensMapByTokenHash: tokensMapByTokenHash,
247
+ _tokensMapByBlockchainName: tokensMapByBlockchainName,
241
248
  _blockchainsMapByName: new Map<string, BlockchainMeta>(
242
249
  blockchains.map((meta) => [meta.name, meta])
243
250
  ),
@@ -6,6 +6,7 @@ import {
6
6
  } from '@rango-dev/queue-manager-rango-preset';
7
7
 
8
8
  type NotificationRoute = {
9
+ creationTime: number;
9
10
  from: {
10
11
  blockchain: Step['fromBlockchain'];
11
12
  symbol: Step['fromSymbol'];
@@ -13,7 +13,7 @@ export type Balance = {
13
13
  usdValue: string;
14
14
  };
15
15
 
16
- type Blockchain = string;
16
+ export type Blockchain = string;
17
17
  type TokenSymbol = string;
18
18
  type Address = string;
19
19