-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add getSupportedChainIdsFromWalletConnectSession (#855)
- Loading branch information
1 parent
eac5be2
commit 6863857
Showing
2 changed files
with
40 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
...react-kit/src/components/wallet2/selector/getSupportedChainIdsFromWalletConnectSession.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[]; | ||
} |