-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathstorage.tsx
225 lines (183 loc) · 7.82 KB
/
storage.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import { RegistryAsset } from "@/types/chainRegistry";
import { emptyChain } from "./helpers";
import { ChainInfo, ChainItems, ExplorerLinks } from "./types";
const registryShaStorageKey = "context-registry-sha";
export const getShaFromStorage = () => localStorage.getItem(registryShaStorageKey);
export const setShaInStorage = (sha: string) => localStorage.setItem(registryShaStorageKey, sha);
const chainsStorageKey = "context-chain-items";
export const getChainsFromStorage = () => {
const storedChains = localStorage.getItem(chainsStorageKey);
if (!storedChains) {
const chains: ChainItems = { mainnets: new Map(), testnets: new Map(), localnets: new Map() };
return chains;
}
const { mainnets, testnets, localnets } = JSON.parse(storedChains);
const chains: ChainItems = {
mainnets: new Map(mainnets),
testnets: new Map(testnets),
localnets: new Map(localnets),
};
return chains;
};
export const setChainsInStorage = (chains: ChainItems) => {
const arrayMainnets = Array.from(chains.mainnets.entries());
const arrayTestnets = Array.from(chains.testnets.entries());
const arrayLocalnets = Array.from(chains.localnets.entries());
const stringChains = JSON.stringify({
mainnets: arrayMainnets,
testnets: arrayTestnets,
localnets: arrayLocalnets,
});
localStorage.setItem(chainsStorageKey, stringChains);
};
export const addLocalChainInStorage = (chain: ChainInfo, chains: ChainItems) => {
chains.localnets.set(chain.registryName, chain);
setChainsInStorage(chains);
};
export const deleteLocalChainFromStorage = (chainName: string, chains: ChainItems) => {
chains.localnets.delete(chainName);
setChainsInStorage(chains);
};
const recentChainsStorageKey = "context-recent-chains";
export const getRecentChainNamesFromStorage = () => {
const storedNames = localStorage.getItem(recentChainsStorageKey);
if (!storedNames) return [];
const chainNames: readonly string[] = JSON.parse(storedNames);
return chainNames;
};
export const setRecentChainNamesInStorage = (chainNames: readonly string[]) => {
const stringChainNames = JSON.stringify(chainNames);
localStorage.setItem(recentChainsStorageKey, stringChainNames);
};
export const addRecentChainNameInStorage = (chainName: string) => {
const storedNames = getRecentChainNamesFromStorage();
const newChains = storedNames.filter((storedName) => storedName !== chainName);
setRecentChainNamesInStorage([chainName, ...newChains.slice(0, 3)]);
};
export const getRecentChainsFromStorage = (chains: ChainItems) => {
const recentChainNames = getRecentChainNamesFromStorage();
const recentChains = recentChainNames.map((chainName) => {
const chain =
chains.localnets.get(chainName) ??
chains.testnets.get(chainName) ??
chains.mainnets.get(chainName);
return chain ?? null;
});
const nonNullRecentChains: readonly ChainInfo[] = recentChains.filter(
(chain): chain is ChainInfo => chain !== null,
);
return nonNullRecentChains;
};
export const getRecentChainFromStorage = (chains: ChainItems): Partial<ChainInfo> => {
const recentChains = getRecentChainsFromStorage(chains);
const recentChain = recentChains?.[0] ?? {};
return recentChain;
};
export const getChainFromUrl = (chainName: string) => {
if (!chainName) {
return { registryName: chainName };
}
const params = new URLSearchParams(location.search);
const logo = params.get("logo");
const chainId = params.get("chainId");
const chainDisplayName = params.get("chainDisplayName");
const nodeAddresses = params.get("nodeAddresses");
const denom = params.get("denom");
const displayDenom = params.get("displayDenom");
const displayDenomExponent = params.get("displayDenomExponent");
const assets = params.get("assets");
const gasPrice = params.get("gasPrice");
const addressPrefix = params.get("addressPrefix");
const explorerLinks = params.get("explorerLinks");
const nodeAddressesValue: readonly string[] = JSON.parse(nodeAddresses || "[]");
const assetsValue: readonly RegistryAsset[] = JSON.parse(assets || "[]");
const explorerLinkValue: Partial<ExplorerLinks> = JSON.parse(explorerLinks || "{}");
const urlChain: Partial<ChainInfo> = {
registryName: chainName,
...(logo && { logo }),
...(chainId && { chainId }),
...(chainDisplayName && { chainDisplayName }),
...(nodeAddressesValue.length && { nodeAddress: "" }),
...(nodeAddressesValue.length && { nodeAddresses: nodeAddressesValue }),
...(denom && { denom }),
...(displayDenom && { displayDenom }),
...(displayDenomExponent && { displayDenomExponent: Number(displayDenomExponent) }),
...(assetsValue.length && { assets: assetsValue }),
...(gasPrice && { gasPrice }),
...(addressPrefix && { addressPrefix }),
...(explorerLinks && {
explorerLinks: { tx: explorerLinkValue.tx || "", account: explorerLinkValue.account || "" },
}),
};
return urlChain;
};
export const getChainFromEnvfile = (chainName: string) => {
const registryName = process.env.NEXT_PUBLIC_REGISTRY_NAME || "";
if (chainName && registryName !== chainName) {
return { registryName: chainName };
}
const logo = process.env.NEXT_PUBLIC_LOGO;
const chainId = process.env.NEXT_PUBLIC_CHAIN_ID;
const chainDisplayName = process.env.NEXT_PUBLIC_CHAIN_DISPLAY_NAME;
const nodeAddresses = process.env.NEXT_PUBLIC_NODE_ADDRESSES;
const denom = process.env.NEXT_PUBLIC_DENOM;
const displayDenom = process.env.NEXT_PUBLIC_DISPLAY_DENOM;
const displayDenomExponent = process.env.NEXT_PUBLIC_DISPLAY_DENOM_EXPONENT;
const assets = process.env.NEXT_PUBLIC_ASSETS;
const gasPrice = process.env.NEXT_PUBLIC_GAS_PRICE;
const addressPrefix = process.env.NEXT_PUBLIC_ADDRESS_PREFIX;
// An object containing link templates for txs and accounts
const explorerLinks = process.env.NEXT_PUBLIC_EXPLORER_LINKS;
const nodeAddressesValue: readonly string[] = JSON.parse(nodeAddresses || "[]");
const assetsValue: readonly RegistryAsset[] = JSON.parse(assets || "[]");
const explorerLinksValue: Partial<ExplorerLinks> = JSON.parse(explorerLinks || "{}");
const envfileChain: Partial<ChainInfo> = {
registryName: chainName,
...(logo && { logo }),
...(chainId && { chainId }),
...(chainDisplayName && { chainDisplayName }),
...(nodeAddressesValue.length && { nodeAddress: "" }),
...(nodeAddressesValue.length && { nodeAddresses: nodeAddressesValue }),
...(denom && { denom }),
...(displayDenom && { displayDenom }),
...(displayDenomExponent && { displayDenomExponent: Number(displayDenomExponent) }),
...(assetsValue.length && { assets: assetsValue }),
...(gasPrice && { gasPrice }),
...(addressPrefix && { addressPrefix }),
...(explorerLinksValue && {
explorerLinks: { tx: explorerLinksValue.tx || "", account: explorerLinksValue.account || "" },
}),
};
return envfileChain;
};
export const getChainFromStorage = (
chainName: string | null,
{ localnets, testnets, mainnets }: ChainItems,
) => {
if (!chainName) {
return emptyChain;
}
return (
localnets.get(chainName) ?? testnets.get(chainName) ?? mainnets.get(chainName) ?? emptyChain
);
};
export const setChainInUrl = (chain: ChainInfo, chains: ChainItems) => {
const newPathname = location.pathname.includes(chain.registryName)
? location.pathname
: `/${chain.registryName}`;
if (chains.mainnets.has(chain.registryName) || chains.testnets.has(chain.registryName)) {
window.history.replaceState({}, "", newPathname);
return;
}
// Set full url if chain is not on chain-registry repo
const params = new URLSearchParams();
for (const [key, value] of Object.entries(chain)) {
if (typeof value === "object") {
params.set(key, JSON.stringify(value));
} else {
params.set(key, value);
}
}
const newUrl = params.size ? `${newPathname}?${params}` : newPathname;
window.history.replaceState({}, "", newUrl);
};