@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.
- package/dist/components/Quote/Quote.styles.d.ts +1 -1
- package/dist/components/RefreshModal/RefreshModal.styles.d.ts +1 -1
- package/dist/containers/Wallets/Wallets.d.ts.map +1 -1
- package/dist/hooks/usePrepareBlockchainList/usePrepareBlockchainList.helpers.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/index.js.map +4 -4
- package/dist/services/cacheService.d.ts +21 -0
- package/dist/services/cacheService.d.ts.map +1 -0
- package/dist/store/notification.d.ts.map +1 -1
- package/dist/store/slices/config.d.ts +2 -1
- package/dist/store/slices/config.d.ts.map +1 -1
- package/dist/store/slices/data.d.ts +2 -1
- package/dist/store/slices/data.d.ts.map +1 -1
- package/dist/store/utils/data.d.ts +20 -0
- package/dist/store/utils/data.d.ts.map +1 -0
- package/dist/store/utils/data.test.d.ts +2 -0
- package/dist/store/utils/data.test.d.ts.map +1 -0
- package/dist/store/utils/index.d.ts +2 -0
- package/dist/store/utils/index.d.ts.map +1 -0
- package/dist/test-utils/fixtures.d.ts +1 -0
- package/dist/test-utils/fixtures.d.ts.map +1 -1
- package/dist/types/notification.d.ts +1 -0
- package/dist/types/notification.d.ts.map +1 -1
- package/dist/types/wallets.d.ts +1 -1
- package/dist/types/wallets.d.ts.map +1 -1
- package/dist/utils/settings.d.ts +1 -1
- package/dist/widget-embedded.build.json +1 -1
- package/package.json +2 -2
- package/src/containers/Wallets/Wallets.tsx +7 -2
- package/src/services/cacheService.ts +35 -0
- package/src/store/notification.ts +13 -1
- package/src/store/slices/config.ts +39 -1
- package/src/store/slices/data.test.ts +84 -0
- package/src/store/slices/data.ts +44 -24
- package/src/store/utils/data.test.ts +175 -0
- package/src/store/utils/data.ts +117 -0
- package/src/store/utils/index.ts +1 -0
- package/src/test-utils/fixtures.ts +14 -7
- package/src/types/notification.ts +1 -0
- 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:
|
|
236
|
-
|
|
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
|
),
|