Skip to content

Commit 9e0558c

Browse files
committed
Merge branch 'greg/viem-ox' of https://github.com/thirdweb-dev/js into greg/viem-ox
2 parents 580f42e + 505a026 commit 9e0558c

File tree

6 files changed

+98
-17
lines changed

6 files changed

+98
-17
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

apps/dashboard/src/@3rdweb-sdk/react/components/connect-wallet/index.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,14 @@ export const CustomConnectWallet = (props: {
9393

9494
const chainSections: NetworkSelectorProps["sections"] = useMemo(() => {
9595
return [
96-
{
97-
label: "Favorites",
98-
chains: favoriteChainsWithMetadata,
99-
},
10096
{
10197
label: "Recent",
10298
chains: recentlyUsedChainsWithMetadata,
10399
},
100+
{
101+
label: "Favorites",
102+
chains: favoriteChainsWithMetadata,
103+
},
104104
{
105105
label: "Popular",
106106
chains: popularChainsWithMetadata,

apps/dashboard/src/components/configure-networks/ConfigureNetworkForm.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,12 @@ export const ConfigureNetworkForm: React.FC<NetworkConfigFormProps> = ({
6262
values: {
6363
name: editingChain?.name || prefillSlug || "",
6464
rpcUrl:
65-
editingChain && editingChain?.status !== "deprecated"
66-
? getDashboardChainRpc(editingChain.chainId, editingChain)
67-
: "",
65+
(!editingChain || editingChain.status === "deprecated"
66+
? ""
67+
: // if chain is custom or modified, show the rpc as is
68+
editingChain.isCustom || editingChain.isModified
69+
? editingChain.rpc[0]
70+
: getDashboardChainRpc(editingChain.chainId, editingChain)) || "",
6871
chainId: editingChain?.chainId
6972
? `${editingChain?.chainId}`
7073
: prefillChainId || "",

apps/dashboard/src/components/configure-networks/Form/NetworkIdInput.tsx

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ export const NetworkIDInput: React.FC<{
88
form: UseFormReturn<NetworkConfigFormData>;
99
disabled?: boolean;
1010
}> = ({ form, disabled }) => {
11-
const slug = form.watch("slug");
1211
const { slugToChain } = useAllChainsData();
13-
const existingChain = slugToChain.get(slug);
1412

1513
return (
1614
<FormFieldSetup
@@ -29,12 +27,7 @@ export const NetworkIDInput: React.FC<{
2927
}
3028
errorMessage={
3129
form.formState.errors.slug?.type === "taken" ? (
32-
<>
33-
Can not use {`"${slug}"`}.{" "}
34-
{slug &&
35-
existingChain &&
36-
`It is being used by "${existingChain.name}"`}
37-
</>
30+
<>Slug is taken by other network</>
3831
) : undefined
3932
}
4033
>
@@ -59,7 +52,13 @@ export const NetworkIDInput: React.FC<{
5952
return true;
6053
}
6154

62-
return !slugToChain.has(_slug);
55+
const chainForSlug = slugToChain.get(_slug);
56+
57+
if (chainForSlug && !chainForSlug.isCustom) {
58+
return false;
59+
}
60+
61+
return true;
6362
},
6463
},
6564
})}

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)