Skip to content

Commit f537a93

Browse files
authored
Add config option for cache namespace (#3580)
* add config option for cache namespace * move wormhole-connect: prefix * deal with circular dep * clean up interface
1 parent 62f7655 commit f537a93

File tree

5 files changed

+45
-16
lines changed

5 files changed

+45
-16
lines changed

wormhole-connect/src/config/index.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,23 @@ export function buildConfig(
5151
customConfig.wrappedTokens,
5252
);
5353

54+
const cacheKey = (name: string) => {
55+
if (customConfig.cacheNamespace) {
56+
return `wormhole-connect:${customConfig.cacheNamespace}:${name}`;
57+
} else {
58+
return `wormhole-connect:${name}`;
59+
}
60+
};
61+
5462
const tokens = buildTokenCache(
55-
network,
5663
[
5764
...networkData.tokens,
5865
...(customConfig.tokensConfig
5966
? Object.values(customConfig.tokensConfig)
6067
: []),
6168
],
6269
wrappedTokens,
70+
cacheKey(`token-cache:${network}`),
6371
);
6472

6573
const sdkConfig = LEGACY_CONFIG[network.toUpperCase()];
@@ -147,6 +155,9 @@ export function buildConfig(
147155
// UI details
148156
ui: createUiConfig({ ...customConfig.ui }),
149157

158+
// Used to namespace localStorage caches
159+
cacheKey,
160+
150161
// Guardian Set
151162
guardianSet: networkData.guardianSet,
152163

wormhole-connect/src/config/tokens.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
TokenAddress,
88
toNative,
99
isNative,
10-
Network,
1110
chainToPlatform,
1211
UniversalAddress,
1312
} from '@wormhole-foundation/sdk';
@@ -507,12 +506,12 @@ export class TokenCache extends TokenMapping<Token> {
507506

508507
// Seed a new TokenCache using hard-coded tokens
509508
export function buildTokenCache(
510-
network: Network,
511509
tokens: TokenConfig[],
512510
wrappedTokens: WrappedTokenAddresses,
511+
cacheKey: string,
513512
tokenFilter?: string[],
514513
): TokenCache {
515-
const cache = TokenCache.load(`wormhole-connect:token-cache:${network}`);
514+
const cache = TokenCache.load(cacheKey);
516515

517516
for (const { tokenId, symbol, name, icon, decimals } of tokens) {
518517
const token = new Token(

wormhole-connect/src/config/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ export interface WormholeConnectConfig {
111111
// Custom tokens
112112
tokensConfig?: TokensConfig;
113113

114+
// Used to namespace localStorage caches
115+
cacheNamespace?: string;
116+
114117
// Wormhole-wrapped token addresses
115118
wrappedTokens?: WrappedTokenAddresses;
116119

@@ -174,6 +177,9 @@ export interface InternalConfig<N extends Network> {
174177
// UI configuration
175178
ui: UiConfig;
176179

180+
// Used to namespace localStorage caches
181+
cacheKey: (name: string) => string;
182+
177183
guardianSet: GuardianSetData;
178184

179185
transactionSettings: TransactionSettings;

wormhole-connect/src/utils/inProgressTxCache.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import config from 'config';
12
import { TransactionLocal } from 'config/types';
23
import { isEmptyObject } from 'utils';
34

4-
const LOCAL_STORAGE_KEY = 'wormhole-connect:transactions:inprogress';
5+
const LOCAL_STORAGE_KEY = 'transactions:inprogress';
56
const LOCAL_STORAGE_MAX = 3;
67

78
// Bigint types cannot be serialized to a string
@@ -112,7 +113,10 @@ export const getTxsFromLocalStorage = ():
112113
// Find the in-progress transactions list in localStorage
113114
for (let i = 0; i < ls.length; i++) {
114115
const itemKey = ls.key(i);
115-
if (itemKey?.toLowerCase() === LOCAL_STORAGE_KEY) {
116+
if (
117+
itemKey?.toLowerCase() ===
118+
config.cacheKey(LOCAL_STORAGE_KEY).toLowerCase()
119+
) {
116120
const item = ls.getItem(itemKey);
117121
if (item) {
118122
try {
@@ -124,7 +128,7 @@ export const getTxsFromLocalStorage = ():
124128
`Error while parsing localStorage item ${LOCAL_STORAGE_KEY}: Not an array of valid transactions`,
125129
);
126130
// Remove invalid transactions entry
127-
ls.removeItem(LOCAL_STORAGE_KEY);
131+
ls.removeItem(config.cacheKey(LOCAL_STORAGE_KEY));
128132
return;
129133
}
130134
} catch (e: any) {
@@ -135,7 +139,7 @@ export const getTxsFromLocalStorage = ():
135139
`Error while parsing localStorage item ${LOCAL_STORAGE_KEY}: ${e}`,
136140
);
137141
// Remove item
138-
ls.removeItem(LOCAL_STORAGE_KEY);
142+
ls.removeItem(config.cacheKey(LOCAL_STORAGE_KEY));
139143
return;
140144
}
141145
}
@@ -167,7 +171,10 @@ export const addTxToLocalStorage = (
167171

168172
// Update the list
169173
try {
170-
ls.setItem(LOCAL_STORAGE_KEY, JSON.stringify(newList, JSONReplacer));
174+
ls.setItem(
175+
config.cacheKey(LOCAL_STORAGE_KEY),
176+
JSON.stringify(newList, JSONReplacer),
177+
);
171178
} catch (e: any) {
172179
// We can get two different errors:
173180
// 1- TypeError from JSON.stringify
@@ -191,7 +198,10 @@ export const removeTxFromLocalStorage = (txHash: string) => {
191198
// remove the item and update localStorage
192199
items.splice(removeIndex, 1);
193200
try {
194-
ls.setItem(LOCAL_STORAGE_KEY, JSON.stringify(items, JSONReplacer));
201+
ls.setItem(
202+
config.cacheKey(LOCAL_STORAGE_KEY),
203+
JSON.stringify(items, JSONReplacer),
204+
);
195205
} catch (e: any) {
196206
// We can get two different errors:
197207
// 1- TypeError from JSON.stringify
@@ -221,7 +231,10 @@ export const updateTxInLocalStorage = (
221231
// Update item property and put back in local storage
222232
items[idx][key] = value;
223233
try {
224-
ls.setItem(LOCAL_STORAGE_KEY, JSON.stringify(items, JSONReplacer));
234+
ls.setItem(
235+
config.cacheKey(LOCAL_STORAGE_KEY),
236+
JSON.stringify(items, JSONReplacer),
237+
);
225238
} catch (e: any) {
226239
// We can get two different errors:
227240
// 1- TypeError from JSON.stringify

wormhole-connect/src/utils/wallet/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ export const connectWallet = async (
129129
setTimeout(() => {
130130
dispatch(clearWallet(type));
131131
}, 0);
132-
localStorage.removeItem(`wormhole-connect:wallet:${platform}`);
132+
localStorage.removeItem(config.cacheKey(`wallet:${platform}`));
133133
});
134134

135135
// when the user has multiple wallets connected and either changes
@@ -145,7 +145,7 @@ export const connectWallet = async (
145145
});
146146

147147
if (name !== ReadOnlyWallet.NAME) {
148-
localStorage.setItem(`wormhole-connect:wallet:${platform}`, name);
148+
localStorage.setItem(config.cacheKey(`wallet:${platform}`), name);
149149
}
150150

151151
return true;
@@ -159,9 +159,9 @@ export const connectLastUsedWallet = async (
159159
dispatch: Dispatch<any>,
160160
) => {
161161
const chainConfig = config.chains[chain!]!;
162-
const localStorageKey = `wormhole-connect:wallet:${chainToPlatform(
163-
chainConfig.sdkName,
164-
)}`;
162+
const localStorageKey = config.cacheKey(
163+
`wallet:${chainToPlatform(chainConfig.sdkName)}`,
164+
);
165165
const lastUsedWallet = localStorage.getItem(localStorageKey);
166166

167167
// if the last used wallet is not WalletConnect, try to connect to it

0 commit comments

Comments
 (0)