Skip to content

Commit

Permalink
Fix: duplicated chain ids (#554)
Browse files Browse the repository at this point in the history
Fixes #551
  • Loading branch information
fracek authored Jan 13, 2025
2 parents fb34a0c + 4dcb325 commit 4c677b0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "core: fail if there is duplicated chain ids",
"packageName": "@starknet-react/core",
"email": "[email protected]",
"dependentChangeType": "patch"
}
17 changes: 16 additions & 1 deletion packages/core/src/context/starknet.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { accounts, defaultConnector } from "../../test/devnet";
import { act, renderHook } from "../../test/react";
import type { MockConnector } from "../connectors";

import { useStarknet } from "./starknet";
import { jsonRpcProvider } from "../providers";
import { StarknetProvider, useStarknet } from "./starknet";

describe("StarknetProvider", () => {
it("defaults to the first chain", async () => {
Expand Down Expand Up @@ -82,4 +83,18 @@ describe("StarknetProvider", () => {
expect(await result.current.connector?.chainId()).toEqual(mainnet.id);
expect(result.current.chain.id).toEqual(mainnet.id);
});

it("fails if there is duplicated chain ids", async () => {
const chains = [devnet, devnet];
function rpc() {
return {
nodeUrl: devnet.rpcUrls.public.http[0],
};
}
const provider = jsonRpcProvider({ rpc });

expect(() => StarknetProvider({ provider, chains })).toThrowError(
"Duplicated chain id found",
);
});
});
10 changes: 10 additions & 0 deletions packages/core/src/context/starknet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,16 @@ function useStarknetManager({
throw new Error("Must provide at least one chain.");
}

// check for duplicated ids in the chains list
const seen = new Set<bigint>();

for (const chain of chains) {
if (seen.has(chain.id)) {
throw new Error(`Duplicated chain id found: ${chain.id}`);
}
seen.add(chain.id);
}

const { chain: _, provider: defaultProvider } = providerForChain(
defaultChain,
provider,
Expand Down

0 comments on commit 4c677b0

Please sign in to comment.