Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add getSupportedChainIdsFromWalletConnectSession #855

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
CoreProtocolConfig
} from "@bosonprotocol/core-sdk";
import { SvgImage } from "../../ui/SvgImage";
import { getSupportedChainIdsFromWalletConnectSession } from "./getSupportedChainIdsFromWalletConnectSession";

const IconAndChevron = styled.div<{
$isOpen: boolean;
Expand Down Expand Up @@ -71,8 +72,6 @@ function useWalletSupportedChains({

switch (connectionType) {
case ConnectionType.WALLET_CONNECT_V2:
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return getSupportedChainIdsFromWalletConnectSession(
(connector as WalletConnectV2).provider?.session
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import type { SessionTypes } from "@walletconnect/types";
import { ChainId } from "@uniswap/sdk-core";

// Helper function to extract chainId from string in format 'eip155:{chainId}'
function getChainIdFromFormattedString(item: string): number | null {
const splitItem = item.startsWith("eip155:") ? item.split(":") : [];
return splitItem.length > 1 && !isNaN(Number(splitItem[1]))
? Number(splitItem[1])
: null;
}

export function getSupportedChainIdsFromWalletConnectSession(
session?: SessionTypes.Struct
): ChainId[] {
if (!session?.namespaces) return [];

const eip155Keys = Object.keys(session.namespaces);
const namespaces = Object.values(session.namespaces);

// Collect all arrays into one for unified processing
const allItems = [
...eip155Keys,
...namespaces.flatMap((namespace) => namespace.chains),
...namespaces.flatMap((namespace) => namespace.accounts)
];

// Process all items to extract chainIds
const allChainIds = allItems
.map((item) => {
if (typeof item === "string") {
return getChainIdFromFormattedString(item);
}
// Check if the item is a number
return isNaN(Number(item)) ? null : Number(item);
})
.filter((item) => item !== null); // Filter out any null values

return Array.from(new Set(allChainIds)) as ChainId[];
}
Loading