Skip to content

Commit df23351

Browse files
committed
fix: support multiple rpc urls
1 parent 8e94d90 commit df23351

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

src/GearboxSDK.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ export interface SDKAttachOptions {
3333
*/
3434
account?: Address;
3535
/**
36-
* RPC URL to use
36+
* RPC URL (and fallbacks) to use
3737
*/
38-
rpcURL: string;
38+
rpcURLs: string[];
3939
/**
4040
* RPC client timeout in milliseconds
4141
*/
@@ -107,10 +107,13 @@ export class GearboxSDK extends EventEmitter<SDKEventsMap> {
107107
public readonly contracts = new AddressMap<BaseContract<any>>();
108108

109109
public static async attach(options: SDKAttachOptions): Promise<GearboxSDK> {
110-
const { rpcURL, timeout, logger, riskCurators } = options;
110+
const { rpcURLs, timeout, logger, riskCurators } = options;
111111
let { networkType, addressProvider, chainId } = options;
112+
if (rpcURLs.length === 0) {
113+
throw new Error("please specify at least one rpc url");
114+
}
112115
const attachClient = createAnvilClient({
113-
transport: http(rpcURL, { timeout }),
116+
transport: http(rpcURLs[0], { timeout }),
114117
});
115118
if (!networkType) {
116119
networkType = await attachClient.detectNetwork();
@@ -125,7 +128,7 @@ export class GearboxSDK extends EventEmitter<SDKEventsMap> {
125128
const provider = new Provider({
126129
chainId,
127130
networkType,
128-
rpcURL,
131+
rpcURLs,
129132
timeout,
130133
});
131134
logger?.debug(

src/chain/Provider.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Address, Chain, PublicClient } from "viem";
2-
import { createPublicClient, defineChain, http } from "viem";
2+
import { createPublicClient, defineChain, fallback, http } from "viem";
33

44
import { AddressLabeller } from "../base/AddressLabeller";
55
import 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

Comments
 (0)