-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathStorage.ts
71 lines (59 loc) · 2.75 KB
/
Storage.ts
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
import { LazyTevm, type SupportedNetwork } from './LazyTevm';
const alchemyApiKey = 'beaEwjczm1iCOAcSco_F8QbtqnwnginU';
const alchemyUrls = {
mainnet: `https://eth-mainnet.g.alchemy.com/v2/${alchemyApiKey}`,
sepolia: `https://eth-sepolia.g.alchemy.com/v2/${alchemyApiKey}`,
optimism: `https://opt-mainnet.g.alchemy.com/v2/${alchemyApiKey}`,
optimismSepolia: `https://opt-sepolia.g.alchemy.com/v2/${alchemyApiKey}`,
polygon: `https://polygon-mainnet.g.alchemy.com/v2/${alchemyApiKey}`,
arbitrum: `https://arb-mainnet.g.alchemy.com/v2/${alchemyApiKey}`,
arbitrumSepolia: `https://arb-sepolia.g.alchemy.com/v2/${alchemyApiKey}`,
zora: `https://zora-mainnet.g.alchemy.com/v2/${alchemyApiKey}`,
zoraSepolia: `https://zora-sepolia.g.alchemy.com/v2/${alchemyApiKey}`,
base: `https://base-mainnet.g.alchemy.com/v2/${alchemyApiKey}`,
baseSepolia: `https://base-sepolia.g.alchemy.com/v2/${alchemyApiKey}`,
zksync: `https://zksync-era.g.alchemy.com/v2/${alchemyApiKey}`,
zksyncSepolia: `https://zksync-sepolia.g.alchemy.com/v2/${alchemyApiKey}`,
};
export class Storage {
migrateLocalStorage() {
const mainnetUrl = localStorage.getItem('rpcUrl_mainnet');
const optimismUrl = localStorage.getItem('rpcUrl_optimism');
const baseUrl = localStorage.getItem('rpcUrl_base');
if (mainnetUrl && mainnetUrl.includes('cloudflare')) {
localStorage.setItem('rpcUrl_mainnet', alchemyUrls.mainnet);
}
if (optimismUrl && optimismUrl.includes('mainnet.optimism.io')) {
localStorage.setItem('rpcUrl_optimism', alchemyUrls.optimism);
}
if (baseUrl && baseUrl.includes('https://mainnet.base.org')) {
localStorage.setItem('rpcUrl_base', alchemyUrls.base);
}
}
public async getStoredUrl(network: SupportedNetwork): Promise<string> {
const storedUrl = localStorage.getItem(`rpcUrl_${network}`);
if (storedUrl) return storedUrl;
const out = alchemyUrls[network] ?? await this.getUrlFromCommon(network);
return out ?? '';
}
private async getUrlFromCommon(network: SupportedNetwork): Promise<string> {
const common = await LazyTevm.getCommon(network);
return common.rpcUrls.default.http[0] ?? '';
}
async setStoredUrl(network: string, url: string): Promise<void> {
localStorage.setItem(`rpcUrl_${network}`, url);
}
getStoredHistory(): string[] {
const storedHistory = localStorage.getItem('commandHistory');
if (!storedHistory) {
const initialHistory = ['cast --help'];
localStorage.setItem('commandHistory', JSON.stringify(initialHistory));
return initialHistory;
}
return JSON.parse(storedHistory);
}
setStoredHistory(history: string[]) {
const limitedHistory = history.slice(-100);
localStorage.setItem('commandHistory', JSON.stringify(limitedHistory));
}
}