Skip to content

Commit 22328d7

Browse files
committed
feat: lazy load rpc
1 parent 858a5d9 commit 22328d7

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

src/config/getClient.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ import { Chain, createPublicClient, http, PublicClient } from "viem";
44
config();
55

66
export function getClient(chain: Chain): PublicClient {
7-
const rpcUrl = process.env[`RPC_URL_${chain.id}`];
8-
if (!rpcUrl) {
9-
throw new Error(`RPC_URL_${chain.id} is not set`);
10-
}
7+
// Use a lazy transport that only resolves the RPC URL when actually making requests
118
return createPublicClient({
129
chain,
13-
transport: http(rpcUrl),
10+
transport: http(() => {
11+
const rpcUrl = process.env[`RPC_URL_${chain.id}`];
12+
if (!rpcUrl) {
13+
throw new Error(`RPC_URL_${chain.id} is not set`);
14+
}
15+
return rpcUrl;
16+
}),
1417
});
1518
}

0 commit comments

Comments
 (0)