Skip to content

Commit bfc4778

Browse files
authored
fix RPC pathname override behavior (#5915)
1 parent f323f7c commit bfc4778

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

.changeset/odd-hats-give.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": patch
3+
---
4+
5+
respect custom RPC urls passed directly

packages/thirdweb/src/chains/utils.test.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
getChainDecimals,
2424
getChainNativeCurrencyName,
2525
getChainSymbol,
26+
getRpcUrlForChain,
2627
} from "./utils.js";
2728

2829
const legacyChain: LegacyChain = {
@@ -306,4 +307,75 @@ describe("defineChain", () => {
306307
testnet: undefined,
307308
});
308309
});
310+
311+
describe("getRpcUrlForChain", () => {
312+
it("should construct RPC URL using chain ID and client ID when chain is a number", () => {
313+
const options = {
314+
client: { ...TEST_CLIENT, clientId: "test-client-id" },
315+
chain: 1,
316+
};
317+
const result = getRpcUrlForChain(options);
318+
expect(result).toBe("https://1.rpc.thirdweb.com/test-client-id");
319+
});
320+
321+
it("should return the custom RPC URL if provided", () => {
322+
const options = {
323+
client: { ...TEST_CLIENT, clientId: "test-client-id" },
324+
chain: {
325+
id: 1,
326+
rpc: "https://custom-rpc.com",
327+
},
328+
};
329+
const result = getRpcUrlForChain(options);
330+
expect(result).toBe("https://custom-rpc.com");
331+
});
332+
333+
it("should add client ID to thirdweb RPC URL", () => {
334+
const options = {
335+
client: { ...TEST_CLIENT, clientId: "test-client-id" },
336+
chain: {
337+
id: 1,
338+
rpc: "https://1.rpc.thirdweb.com",
339+
},
340+
};
341+
const result = getRpcUrlForChain(options);
342+
expect(result).toBe("https://1.rpc.thirdweb.com/test-client-id");
343+
});
344+
345+
it("should honor client ID passed directly in rpc field", () => {
346+
const options = {
347+
client: { ...TEST_CLIENT, clientId: "test-client-id" },
348+
chain: {
349+
id: 1,
350+
rpc: "https://1.rpc.thirdweb.com/abc",
351+
},
352+
};
353+
const result = getRpcUrlForChain(options);
354+
expect(result).toBe("https://1.rpc.thirdweb.com/abc");
355+
});
356+
357+
it("should replace template string in rpc url", () => {
358+
const options = {
359+
client: { ...TEST_CLIENT, clientId: "test-client-id" },
360+
chain: {
361+
id: 1,
362+
rpc: "https://1.rpc.thirdweb.com/${THIRDWEB_API_KEY}",
363+
},
364+
};
365+
const result = getRpcUrlForChain(options);
366+
expect(result).toBe("https://1.rpc.thirdweb.com/test-client-id");
367+
});
368+
369+
it("should return the RPC URL without modification if it's not a thirdweb URL", () => {
370+
const options = {
371+
client: { ...TEST_CLIENT, clientId: "test-client-id" },
372+
chain: {
373+
id: 1,
374+
rpc: "https://custom-rpc.com",
375+
},
376+
};
377+
const result = getRpcUrlForChain(options);
378+
expect(result).toBe("https://custom-rpc.com");
379+
});
380+
});
309381
});

packages/thirdweb/src/chains/utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,9 @@ export function getRpcUrlForChain(options: GetRpcUrlForChainOptions): string {
193193
const rpcUrl = new URL(
194194
options.chain.rpc.replace(DEFAULT_RPC_URL, baseRpcUrl),
195195
);
196-
rpcUrl.pathname = `/${options.client.clientId}`;
196+
if (rpcUrl.pathname === "/" || rpcUrl.pathname.startsWith("/$")) {
197+
rpcUrl.pathname = `/${options.client.clientId}`;
198+
}
197199
return rpcUrl.toString();
198200
}
199201
return rpc;

0 commit comments

Comments
 (0)