-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.ts
85 lines (76 loc) · 2.75 KB
/
client.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { MemoryClient } from 'tevm';
import { Chain } from '@/lib/types/providers';
const alchemyApiKey = process.env.ALCHEMY_API_KEY || '';
/* ---------------------------------- TYPES --------------------------------- */
/**
* @type {Function} CreateClient
* @param {number} chainId The id of the chain to create the client for
* @param {string} forkUrl The url of the fork to use
* @returns {Promise<MemoryClient>}
*/
type CreateClient = (chainId: number, forkUrl: string) => Promise<MemoryClient>;
/**
* @type {Function} ResetClient
* @param {MemoryClient} client The client to reset
* @returns {Promise<{ success: boolean, error?: string }>}
*/
type ResetClient = (client: MemoryClient) => Promise<{
success: boolean;
error?: string;
}>;
/**
* @type {Function} InitializeClient
* @param {Chain} chain The chain to initialize a client for (using the custom fork url)
* @returns {Promise<MemoryClient>}
*/
type InitializeClient = (chain: Chain) => Promise<MemoryClient>;
/* -------------------------------- FUNCTIONS ------------------------------- */
/**
* @notice Create a Tevm client for a given chain
* @dev This will create a memory client with a sync storage persister, meaning that
* the state of the client will be synced with local storage every second (default throttle).
* @dev If there is a state saved in local storage, it will be loaded.
* @see https://tevm.sh/learn/clients/#state-persistence
*/
// TODO TEMP async/await because of lazy import
const createClient: CreateClient = async (chainId, forkUrl) => {
const { createMemoryClient } = await import('tevm');
const { createSyncStoragePersister } = await import(
'tevm/sync-storage-persister'
);
const needsAlchemyApiKey = forkUrl.endsWith('g.alchemy.com/v2/');
return createMemoryClient({
persister: createSyncStoragePersister({
storage: localStorage,
key: `TEVM_CLIENT_${chainId.toString()}`,
}),
fork: {
url: needsAlchemyApiKey ? `${forkUrl}${alchemyApiKey}` : forkUrl,
},
});
};
/**
* @notice Reset the state of a provided Tevm client
* @dev This will clear the underlying cache of the client, effectively resetting its state.
*/
export const resetClient: ResetClient = async (client) => {
try {
(await client.getVm()).stateManager.clearCaches();
return { success: true };
} catch (err) {
console.error(err);
return {
success: false,
error: err instanceof Error ? err.message : 'Unknown error',
};
}
};
/**
* @notice Initialize the client for a given chain
* @dev This will create or load a client for this chain on first mount.
*/
export const initializeClient: InitializeClient = async (chain) => {
const client = await createClient(chain.id, chain.custom.config.rpcUrl);
await client.ready();
return client;
};