Skip to content

Commit 6e05ec8

Browse files
committed
feat: provider url constructors
1 parent 0910afb commit 6e05ec8

File tree

2 files changed

+209
-0
lines changed

2 files changed

+209
-0
lines changed

src/dev/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export * from "./EthCallSpy.js";
99
export * from "./ltUtils.js";
1010
export * from "./migrateFaucet.js";
1111
export * from "./mint/index.js";
12+
export * from "./providers.js";
1213
export * from "./RevolverTransport.js";
1314
export * from "./replaceStorage.js";
1415
export * from "./types.js";

src/dev/providers.ts

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
import { chains, getChain, type NetworkType } from "../sdk/index.js";
2+
3+
export const SUPPORTED_RPC_PROVIDERS = [
4+
"alchemy",
5+
"drpc",
6+
"thirdweb",
7+
"ankr",
8+
] as const;
9+
10+
export type RpcProvider = (typeof SUPPORTED_RPC_PROVIDERS)[number];
11+
12+
export function getRpcProviderUrl(
13+
provider: RpcProvider,
14+
network: NetworkType,
15+
apiKey: string,
16+
protocol: "http" | "ws" = "http",
17+
): string | undefined {
18+
switch (provider) {
19+
case "alchemy":
20+
return getAlchemyUrl(network, apiKey, protocol);
21+
case "drpc":
22+
return getDrpcUrl(network, apiKey, protocol);
23+
case "thirdweb":
24+
return getThirdWebUrl(network, apiKey, protocol);
25+
case "ankr":
26+
return getAnkrUrl(network, apiKey, protocol);
27+
default:
28+
return undefined;
29+
}
30+
}
31+
32+
export function getAlchemyUrl(
33+
network: NetworkType,
34+
apiKey: string,
35+
protocol: "http" | "ws" = "http",
36+
): string | undefined {
37+
const alchemyDomain = ALCHEMY_DOMAINS[network];
38+
if (!alchemyDomain) {
39+
return undefined;
40+
}
41+
return `${protocol}s://${alchemyDomain}.g.alchemy.com/v2/${apiKey}`;
42+
}
43+
44+
const DRPC_NETS: Record<NetworkType, string | null> = {
45+
Arbitrum: "arbitrum",
46+
Base: "base",
47+
BNB: "bsc",
48+
Mainnet: "ethereum",
49+
Optimism: "optimism",
50+
Sonic: "sonic",
51+
WorldChain: "worldchain",
52+
Berachain: "berachain",
53+
Avalanche: "avalanche",
54+
Monad: "monad-mainnet",
55+
Hemi: "hemi",
56+
Lisk: "lisk",
57+
MegaETH: null,
58+
Etherlink: null,
59+
Plasma: "plasma",
60+
Somnia: "somnia",
61+
};
62+
63+
const ALCHEMY_DOMAINS: Record<NetworkType, string | null> = {
64+
Mainnet: "eth-mainnet",
65+
Arbitrum: "arb-mainnet",
66+
Optimism: "opt-mainnet",
67+
Base: "base-mainnet",
68+
Sonic: "sonic-mainnet",
69+
Monad: "monad-mainnet",
70+
Berachain: "berachain-mainnet",
71+
Avalanche: "avax-mainnet",
72+
BNB: "bnb-mainnet",
73+
WorldChain: "worldchain-mainnet",
74+
MegaETH: null,
75+
Etherlink: null,
76+
Hemi: null,
77+
Lisk: null,
78+
Plasma: "plasma-mainnet",
79+
Somnia: null,
80+
};
81+
82+
export function getDrpcUrl(
83+
network: NetworkType,
84+
apiKey: string,
85+
protocol: "http" | "ws" = "http",
86+
): string | undefined {
87+
const net = DRPC_NETS[network];
88+
return net ? `${protocol}s://lb.drpc.live/${net}/${apiKey}` : undefined;
89+
}
90+
91+
const ANKR_DOMAINS: Record<NetworkType, string | null> = {
92+
Arbitrum: "arbitrum",
93+
Avalanche: "avalanche",
94+
Base: "base",
95+
Berachain: null,
96+
BNB: "bsc",
97+
Etherlink: "etherlink_mainnet",
98+
Hemi: null,
99+
Lisk: null,
100+
Mainnet: "eth",
101+
MegaETH: null,
102+
Monad: "monad_mainnet",
103+
Optimism: "optimism",
104+
Plasma: null,
105+
Sonic: "sonic_mainnet",
106+
WorldChain: null,
107+
Somnia: "somnia_mainnet",
108+
};
109+
110+
export function getAnkrUrl(
111+
network: NetworkType,
112+
apiKey: string,
113+
protocol: "http" | "ws" = "http",
114+
): string | undefined {
115+
const net = ANKR_DOMAINS[network];
116+
const sep = protocol === "ws" ? "/ws/" : "/";
117+
return net ? `${protocol}s://rpc.ankr.com/${net}${sep}${apiKey}` : undefined;
118+
}
119+
120+
const THIRDWEB_DOMAINS: Record<NetworkType, string | null> = {
121+
Arbitrum: chains.Arbitrum.id.toString(),
122+
Avalanche: chains.Avalanche.id.toString(),
123+
Base: chains.Base.id.toString(),
124+
Berachain: chains.Berachain.id.toString(),
125+
BNB: chains.BNB.id.toString(),
126+
Etherlink: chains.Etherlink.id.toString(),
127+
Hemi: chains.Hemi.id.toString(),
128+
Lisk: chains.Lisk.id.toString(),
129+
Mainnet: chains.Mainnet.id.toString(),
130+
MegaETH: chains.MegaETH.id.toString(),
131+
Monad: chains.Monad.id.toString(),
132+
Optimism: chains.Optimism.id.toString(),
133+
Plasma: chains.Plasma.id.toString(),
134+
Sonic: chains.Sonic.id.toString(),
135+
WorldChain: chains.WorldChain.id.toString(),
136+
Somnia: chains.Somnia.id.toString(),
137+
};
138+
139+
export function getThirdWebUrl(
140+
network: NetworkType,
141+
apiKey: string,
142+
protocol: "http" | "ws" = "http",
143+
): string | undefined {
144+
if (protocol === "ws") {
145+
return undefined;
146+
}
147+
const net = THIRDWEB_DOMAINS[network];
148+
return `https://${net}.rpc.thirdweb.com/${apiKey}`;
149+
}
150+
151+
export function getErpcKey(
152+
network: NetworkType,
153+
projectId?: string,
154+
urlBase?: string,
155+
): string | undefined {
156+
if (!projectId || !urlBase) {
157+
return undefined;
158+
}
159+
const chain = getChain(network);
160+
return `${urlBase}/${projectId}/evm/${chain.id}`;
161+
}
162+
163+
/**
164+
Non-existent event:
165+
┌────────────┬─────────┬──────┬──────────┬──────┐
166+
│ Network │ alchemy │ drpc │ thirdweb │ ankr │
167+
├────────────┼─────────┼──────┼──────────┼──────┤
168+
│ Mainnet │ ∞ │ 10M │ 1K │ 10K │
169+
│ Arbitrum │ ∞ │ ∞ │ 1K │ 10K │
170+
│ Optimism │ ∞ │ 1M │ 1K │ 10K │
171+
│ Base │ ∞ │ ∞ │ 1K │ 10K │
172+
│ Sonic │ 10K │ ∞ │ 1K │ 10K │
173+
│ MegaETH │ ✗ │ ✗ │ ✗ │ ✗ │
174+
│ Monad │ 1K │ ✗ │ 1K │ 1K │
175+
│ Berachain │ 10K │ 300K │ 1K │ ✗ │
176+
│ Avalanche │ 10K │ 5M │ 1K │ 10K │
177+
│ BNB │ 10K │ 1M │ 1K │ 10K │
178+
│ WorldChain │ ∞ │ 10M │ 1K │ ✗ │
179+
│ Etherlink │ ✗ │ ✗ │ ✗ │ ✗ │
180+
│ Hemi │ ✗ │ ∞ │ 1K │ ✗ │
181+
│ Lisk │ ✗ │ 5M │ 1K │ ✗ │
182+
│ Plasma │ 10K │ 100K │ 1K │ ✗ │
183+
│ Somnia │ ✗ │ ✗ │ 1K │ 10K │
184+
└────────────┴─────────┴──────┴──────────┴──────┘
185+
186+
Rare event (InstanceManager.OwnershipTransferred):
187+
188+
┌────────────┬─────────┬──────┬──────────┬──────┐
189+
│ Network │ alchemy │ drpc │ thirdweb │ ankr │
190+
├────────────┼─────────┼──────┼──────────┼──────┤
191+
│ Mainnet │ ∞ │ 10M │ 1K │ 10K │
192+
│ Arbitrum │ ∞ │ ∞ │ 1K │ 10K │
193+
│ Optimism │ ∞ │ ∞ │ 1K │ 10K │
194+
│ Base │ ∞ │ ∞ │ 1K │ 10K │
195+
│ Sonic │ 10K │ ∞ │ 1K │ 10K │
196+
│ MegaETH │ ✗ │ ✗ │ ✗ │ ✗ │
197+
│ Monad │ 1K │ ✗ │ 1K │ 1K │
198+
│ Berachain │ 10K │ ∞ │ 1K │ ✗ │
199+
│ Avalanche │ 10K │ 5M │ 1K │ 10K │
200+
│ BNB │ 10K │ ∞ │ 1K │ 10K │
201+
│ WorldChain │ ∞ │ 1M │ 1K │ ✗ │
202+
│ Etherlink │ ✗ │ ✗ │ ✗ │ ✗ │
203+
│ Hemi │ ✗ │ ∞ │ 1K │ ✗ │
204+
│ Lisk │ ✗ │ 10M │ 1K │ ✗ │
205+
│ Plasma │ 10K │ 100K │ 1K │ ✗ │
206+
│ Somnia │ ✗ │ ✗ │ 1K │ 10K │
207+
└────────────┴─────────┴──────┴──────────┴──────┘
208+
*/

0 commit comments

Comments
 (0)