11import type { Address , Chain , PublicClient } from "viem" ;
2- import { createPublicClient , defineChain , http } from "viem" ;
2+ import { createPublicClient , defineChain , fallback , http } from "viem" ;
33
44import { AddressLabeller } from "../base/AddressLabeller" ;
55import type { IAddressLabeller } from "../base/IAddressLabeller" ;
@@ -12,13 +12,17 @@ export interface ProviderOptions {
1212 */
1313 account ?: Address ;
1414 /**
15- * RPC URL to use
15+ * RPC URL (and fallbacks) to use.
1616 */
17- rpcURL : string ;
17+ rpcURLs : string [ ] ;
1818 /**
1919 * RPC client timeout in milliseconds
2020 */
2121 timeout ?: number ;
22+ /**
23+ * Retry count for RPC
24+ */
25+ retryCount ?: number ;
2226 /**
2327 * Chain Id needs to be set, because we sometimemes use forked testnets with different chain ids
2428 */
@@ -38,7 +42,14 @@ export class Provider {
3842 public readonly addressLabels : IAddressLabeller ;
3943
4044 constructor ( opts : ProviderOptions ) {
41- const { account, chainId, networkType, rpcURL, timeout = 120_000 } = opts ;
45+ const {
46+ account,
47+ chainId,
48+ networkType,
49+ rpcURLs,
50+ timeout = 120_000 ,
51+ retryCount,
52+ } = opts ;
4253 this . account = account ;
4354 this . chainId = chainId ;
4455 this . networkType = networkType ;
@@ -49,9 +60,12 @@ export class Provider {
4960 id : chainId ,
5061 } ) ;
5162
63+ const rpcs = rpcURLs . map ( url => http ( url , { timeout, retryCount } ) ) ;
64+ const transport = rpcs . length ? fallback ( rpcs ) : rpcs [ 0 ] ;
65+
5266 this . publicClient = createPublicClient ( {
5367 chain : this . chain ,
54- transport : http ( rpcURL , { timeout } ) , // for SDK they could be multiple RPCs
68+ transport,
5569 } ) ;
5670
5771 this . addressLabels = new AddressLabeller ( ) ;
0 commit comments