diff --git a/packages/thirdweb/src/chain/index.ts b/packages/thirdweb/src/chain/index.ts index ebb61cb38b2..f8611da994a 100644 --- a/packages/thirdweb/src/chain/index.ts +++ b/packages/thirdweb/src/chain/index.ts @@ -4,7 +4,7 @@ import type { ApiChain } from "./types.js"; export type Chain = | { - id: bigint | number; + id: number; rpc: string; nativeCurrency?: { name?: string; @@ -14,7 +14,7 @@ export type Chain = } // TODO: add all possible chainIds somehow for autocompletion // eslint-disable-next-line @typescript-eslint/ban-types - | ((number | bigint) & {}); + | (number & {}); /** * Defines a chain based on the provided options. @@ -49,7 +49,7 @@ type GetRpcUrlForChainOptions = { */ export function getRpcUrlForChain(options: GetRpcUrlForChainOptions): string { // if the chain is just the chainId use the thirdweb rpc - if (typeof options.chain === "bigint" || typeof options.chain === "number") { + if (typeof options.chain === "number") { return `https://${options.chain.toString()}.rpc.thirdweb.com/${ options.client.clientId }`; @@ -70,11 +70,11 @@ export function getRpcUrlForChain(options: GetRpcUrlForChainOptions): string { * @returns The chain ID. * @internal */ -export function getChainIdFromChain(chain: Chain): bigint { - if (typeof chain === "bigint" || typeof chain === "number") { - return BigInt(chain); +export function getChainIdFromChain(chain: Chain): number { + if (typeof chain === "number") { + return chain; } - return BigInt(chain.id); + return chain.id; } /** @@ -84,11 +84,7 @@ export function getChainIdFromChain(chain: Chain): bigint { * @internal */ export async function getChainSymbol(chain: Chain): Promise { - if ( - typeof chain === "bigint" || - typeof chain === "number" || - !chain.nativeCurrency?.symbol - ) { + if (typeof chain === "number" || !chain.nativeCurrency?.symbol) { const chainId = getChainIdFromChain(chain); return getChainDataForChainId(chainId) .then((data) => data.nativeCurrency.symbol) @@ -109,11 +105,7 @@ export async function getChainSymbol(chain: Chain): Promise { * @internal */ export async function getChainDecimals(chain: Chain): Promise { - if ( - typeof chain === "bigint" || - typeof chain === "number" || - !chain.nativeCurrency?.decimals - ) { + if (typeof chain === "number" || !chain.nativeCurrency?.decimals) { const chainId = getChainIdFromChain(chain); return getChainDataForChainId(chainId) .then((data) => data.nativeCurrency.decimals) @@ -137,11 +129,7 @@ export async function getChainDecimals(chain: Chain): Promise { export async function getChainNativeCurrencyName( chain: Chain, ): Promise { - if ( - typeof chain === "bigint" || - typeof chain === "number" || - !chain.nativeCurrency?.name - ) { + if (typeof chain === "number" || !chain.nativeCurrency?.name) { const chainId = getChainIdFromChain(chain); return getChainDataForChainId(chainId) .then((data) => data.nativeCurrency.name) @@ -167,7 +155,7 @@ type FetchChainResponse = /** * @internal */ -export function getChainDataForChainId(chainId: bigint): Promise { +export function getChainDataForChainId(chainId: number): Promise { return withCache( async () => { const res = await fetch(`https://api.thirdweb.com/v1/chains/${chainId}`); diff --git a/packages/thirdweb/src/gas/fee-data.ts b/packages/thirdweb/src/gas/fee-data.ts index abf1417f953..a38118da533 100644 --- a/packages/thirdweb/src/gas/fee-data.ts +++ b/packages/thirdweb/src/gas/fee-data.ts @@ -105,12 +105,12 @@ async function getDynamicFeeData( const chainId = getChainIdFromChain(chain); // flag chain testnet & flag chain - if (chainId === 220n || chainId === 1220n) { + if (chainId === 220 || chainId === 1220) { // these does not support eip-1559, for some reason even though `eth_maxPriorityFeePerGas` is available?!? // return null because otherwise TX break return { maxFeePerGas: null, maxPriorityFeePerGas: null }; // mumbai & polygon - } else if (chainId === 80001n || chainId === 137n) { + } else if (chainId === 80001 || chainId === 137) { // for polygon, get fee data from gas station maxPriorityFeePerGas_ = await getPolygonGasPriorityFee(chainId); } else if (maxPriorityFeePerGas) { @@ -130,7 +130,7 @@ async function getDynamicFeeData( maxFeePerGas = baseBlockFee * 2n + maxPriorityFeePerGas_; // special cased for Celo gas fees - if (chainId === 42220n || chainId === 44787n || chainId === 62320n) { + if (chainId === 42220 || chainId === 44787 || chainId === 62320) { maxPriorityFeePerGas_ = maxFeePerGas; } @@ -184,11 +184,11 @@ async function getGasPrice( /** * @internal */ -function getGasStationUrl(chainId: 137n | 80001n): string { +function getGasStationUrl(chainId: 137 | 80001): string { switch (chainId) { - case 137n: + case 137: return "https://gasstation.polygon.technology/v2"; - case 80001n: + case 80001: return "https://gasstation-testnet.polygon.technology/v2"; } } @@ -200,11 +200,11 @@ const MIN_MUMBAI_GAS_PRICE = 1n; // 1 gwei /** * @internal */ -function getDefaultGasFee(chainId: 137n | 80001n): bigint { +function getDefaultGasFee(chainId: 137 | 80001): bigint { switch (chainId) { - case 137n: + case 137: return MIN_POLYGON_GAS_PRICE; - case 80001n: + case 80001: return MIN_MUMBAI_GAS_PRICE; } } @@ -214,9 +214,7 @@ function getDefaultGasFee(chainId: 137n | 80001n): bigint { * @returns The gas price * @internal */ -async function getPolygonGasPriorityFee( - chainId: 137n | 80001n, -): Promise { +async function getPolygonGasPriorityFee(chainId: 137 | 80001): Promise { const gasStationUrl = getGasStationUrl(chainId); try { const data = await (await fetch(gasStationUrl)).json(); diff --git a/packages/thirdweb/src/react/hooks/others/useChainQuery.ts b/packages/thirdweb/src/react/hooks/others/useChainQuery.ts index 1f02b3efd2b..e54e5578aa1 100644 --- a/packages/thirdweb/src/react/hooks/others/useChainQuery.ts +++ b/packages/thirdweb/src/react/hooks/others/useChainQuery.ts @@ -4,7 +4,7 @@ import { getChainDataForChainId } from "../../../chain/index.js"; /** * @internal */ -function getChainQuery(chainId?: bigint) { +function getChainQuery(chainId?: number) { // TODO make this aware of local overrides (developer passed into provider or something) return queryOptions({ queryKey: ["chain", `${chainId}`] as const, @@ -22,14 +22,14 @@ function getChainQuery(chainId?: bigint) { /** * @internal */ -export function useChainQuery(chainId?: bigint) { +export function useChainQuery(chainId?: number) { return useQuery(getChainQuery(chainId)); } /** * @internal */ -export function useChainsQuery(chainIds: bigint[]) { +export function useChainsQuery(chainIds: number[]) { // this way the underlying queries end up shared with the single query! return useQueries({ queries: chainIds.map(getChainQuery), diff --git a/packages/thirdweb/src/react/providers/wallet-ui-states-provider.tsx b/packages/thirdweb/src/react/providers/wallet-ui-states-provider.tsx index b77d3a9258f..93e66bc3b63 100644 --- a/packages/thirdweb/src/react/providers/wallet-ui-states-provider.tsx +++ b/packages/thirdweb/src/react/providers/wallet-ui-states-provider.tsx @@ -23,8 +23,8 @@ type ModalConfig = { }; isEmbed?: boolean; onConnect?: (wallet: Wallet) => void; - chainId?: bigint; - chains?: bigint[]; + chainId?: number; + chains?: number[]; showThirdwebBranding?: boolean; }; @@ -63,8 +63,8 @@ export const WalletUIStatesProvider = ( onLogout?: () => void; }; onConnect?: (wallet: Wallet) => void; - chainId?: bigint; - chains?: bigint[]; + chainId?: number; + chains?: number[]; showThirdwebBranding?: boolean; }>, ) => { @@ -243,8 +243,8 @@ type ModalConfigOptions = { */ onConnect?: () => void; - chainId?: bigint; - chains?: bigint[]; + chainId?: number; + chains?: number[]; /** * By default the ConnectWallet Modal shows "powered by thirdweb" branding at the bottom of the modal. diff --git a/packages/thirdweb/src/react/types/wallets.ts b/packages/thirdweb/src/react/types/wallets.ts index 6770716c7c9..ff928d7fc59 100644 --- a/packages/thirdweb/src/react/types/wallets.ts +++ b/packages/thirdweb/src/react/types/wallets.ts @@ -119,12 +119,12 @@ export type ConnectUIProps = { /** * Chain Id to connect the wallet to */ - chainId?: bigint; + chainId?: number; /** * List of all chains supported by the app */ - chains?: bigint[]; + chains?: number[]; /** * Create a wallet instance diff --git a/packages/thirdweb/src/react/ui/ConnectWallet/ConnectWallet.tsx b/packages/thirdweb/src/react/ui/ConnectWallet/ConnectWallet.tsx index 5e4430924fa..0feb13c2849 100644 --- a/packages/thirdweb/src/react/ui/ConnectWallet/ConnectWallet.tsx +++ b/packages/thirdweb/src/react/ui/ConnectWallet/ConnectWallet.tsx @@ -69,7 +69,7 @@ export function ConnectWallet(props: ConnectWalletProps) { const isNetworkMismatch = activeWalletChainId !== undefined && props.chainId && - activeWalletChainId !== BigInt(props.chainId); + activeWalletChainId !== props.chainId; // const [showSignatureModal, setShowSignatureModal] = useState(false); // const address = useActiveWalletAddress(); @@ -170,8 +170,8 @@ export function ConnectWallet(props: ConnectWalletProps) { titleIconUrl: props.connectModal?.titleIcon, // auth: props.auth, onConnect: props.onConnect, - chainId: props.chainId ? BigInt(props.chainId) : undefined, - chains: props.chains?.map(BigInt), + chainId: props.chainId ? props.chainId : undefined, + chains: props.chains, showThirdwebBranding: props.connectModal?.showThirdwebBranding, }); @@ -195,7 +195,7 @@ export function ConnectWallet(props: ConnectWalletProps) { style={props.switchButton?.style} className={props.switchButton?.className} switchNetworkBtnTitle={props.switchButton?.label} - targetChainId={BigInt(props.chainId)} + targetChainId={props.chainId} /> ); } @@ -241,7 +241,7 @@ export function ConnectWallet(props: ConnectWalletProps) { // props?.auth?.onLogout?.(); // } }} - chains={props?.chains?.map(BigInt) || []} + chains={props?.chains || []} /> ); })()} @@ -256,7 +256,7 @@ function SwitchNetworkButton(props: { style?: React.CSSProperties; className?: string; switchNetworkBtnTitle?: string; - targetChainId: bigint; + targetChainId: number; }) { const switchChain = useSwitchActiveWalletChain(); const [switching, setSwitching] = useState(false); diff --git a/packages/thirdweb/src/react/ui/ConnectWallet/ConnectWalletProps.ts b/packages/thirdweb/src/react/ui/ConnectWallet/ConnectWalletProps.ts index 21ddb68fe73..9a1fa115b97 100644 --- a/packages/thirdweb/src/react/ui/ConnectWallet/ConnectWalletProps.ts +++ b/packages/thirdweb/src/react/ui/ConnectWallet/ConnectWalletProps.ts @@ -268,7 +268,7 @@ export type ConnectWalletProps = { * * ``` */ - chainId?: bigint | number; + chainId?: number; /** * Array of chain ids that your app supports. @@ -283,7 +283,7 @@ export type ConnectWalletProps = { * * ``` */ - chains?: (bigint | number)[]; + chains?: number[]; /** * Set the theme for the button and modal. By default it is set to `"dark"` diff --git a/packages/thirdweb/src/react/ui/ConnectWallet/Details.tsx b/packages/thirdweb/src/react/ui/ConnectWallet/Details.tsx index d79630dfc41..2ca4ad08ce1 100644 --- a/packages/thirdweb/src/react/ui/ConnectWallet/Details.tsx +++ b/packages/thirdweb/src/react/ui/ConnectWallet/Details.tsx @@ -80,7 +80,7 @@ export const ConnectedWalletDetails: React.FC<{ detailsModal?: ConnectWallet_DetailsModalOptions; theme: "light" | "dark" | Theme; supportedTokens: SupportedTokens; - chains: bigint[]; + chains: number[]; }> = (props) => { const locale = useTWLocale().connectWallet; const activeWallet = useActiveWallet(); diff --git a/packages/thirdweb/src/react/ui/ConnectWallet/NetworkSelector.tsx b/packages/thirdweb/src/react/ui/ConnectWallet/NetworkSelector.tsx index ffd528fc2da..b69371a386d 100644 --- a/packages/thirdweb/src/react/ui/ConnectWallet/NetworkSelector.tsx +++ b/packages/thirdweb/src/react/ui/ConnectWallet/NetworkSelector.tsx @@ -96,16 +96,16 @@ export type NetworkSelectorProps = { */ open: boolean; - chains?: (number | bigint)[]; + chains?: number[]; /** * Array of chains to be displayed under "Popular" section */ - popularChains?: (number | bigint)[]; + popularChains?: number[]; /** * Array of chains to be displayed under "Recent" section */ - recentChains?: (number | bigint)[]; + recentChains?: number[]; /** * Override how the chain button is rendered in the Modal */ @@ -249,13 +249,11 @@ export function NetworkSelectorContent( onBack?: () => void; }, ) { - const allChainIds = new Set( - [ - ...(props.chains || []), - ...(props.popularChains || []), - ...(props.recentChains || []), - ].map((c) => BigInt(c)), - ); + const allChainIds = new Set([ + ...(props.chains || []), + ...(props.popularChains || []), + ...(props.recentChains || []), + ]); // TODO: @manan - instead of doing this here and then passing the chains down we can consider the following approach: // 1. use the useChainsQuery hook wherever we need the search index @@ -680,10 +678,8 @@ const NetworkList = /* @__PURE__ */ memo(function NetworkList( ) { const switchChain = useSwitchActiveWalletChain(); const activeChainId = useActiveWalletChainId(); - const [switchingChainId, setSwitchingChainId] = useState(BigInt(-1)); - const [errorSwitchingChainId, setErrorSwitchingChainId] = useState( - BigInt(-1), - ); + const [switchingChainId, setSwitchingChainId] = useState(-1); + const [errorSwitchingChainId, setErrorSwitchingChainId] = useState(-1); const twLocale = useTWLocale(); const locale = twLocale.connectWallet.networkSelector; @@ -691,7 +687,7 @@ const NetworkList = /* @__PURE__ */ memo(function NetworkList( useEffect(() => { // if switching and switched successfully - close modal - if (switchingChainId !== BigInt(-1) && activeChainId === switchingChainId) { + if (switchingChainId !== -1 && activeChainId === switchingChainId) { if (close) { close(); } @@ -699,17 +695,17 @@ const NetworkList = /* @__PURE__ */ memo(function NetworkList( }, [switchingChainId, close, activeChainId]); const handleSwitch = async (chain: ApiChain) => { - setErrorSwitchingChainId(BigInt(-1)); - setSwitchingChainId(BigInt(chain.chainId)); + setErrorSwitchingChainId(-1); + setSwitchingChainId(chain.chainId); try { - await switchChain(BigInt(chain.chainId)); + await switchChain(chain.chainId); props.onSwitch(chain); } catch (e: any) { - setErrorSwitchingChainId(BigInt(chain.chainId)); + setErrorSwitchingChainId(chain.chainId); console.error(e); } finally { - setSwitchingChainId(BigInt(-1)); + setSwitchingChainId(-1); } }; const RenderChain = props.renderChain; @@ -741,8 +737,8 @@ const NetworkList = /* @__PURE__ */ memo(function NetworkList( return ( {props.chains.map((chain) => { - const confirming = switchingChainId === BigInt(chain.chainId); - const switchingFailed = errorSwitchingChainId === BigInt(chain.chainId); + const confirming = switchingChainId === chain.chainId; + const switchingFailed = errorSwitchingChainId === chain.chainId; const chainName = {chain.name} ; @@ -754,8 +750,8 @@ const NetworkList = /* @__PURE__ */ memo(function NetworkList( handleSwitch(chain); }} chain={chain} - switching={switchingChainId === BigInt(chain.chainId)} - switchFailed={errorSwitchingChainId === BigInt(chain.chainId)} + switching={switchingChainId === chain.chainId} + switchFailed={errorSwitchingChainId === chain.chainId} close={props.close} /> @@ -765,7 +761,7 @@ const NetworkList = /* @__PURE__ */ memo(function NetworkList( return (
  • { handleSwitch(chain); }} @@ -773,7 +769,7 @@ const NetworkList = /* @__PURE__ */ memo(function NetworkList( diff --git a/packages/thirdweb/src/react/wallets/smartWallet/SmartWalletConnectUI.tsx b/packages/thirdweb/src/react/wallets/smartWallet/SmartWalletConnectUI.tsx index 7af1e93692a..8bc5b0acffd 100644 --- a/packages/thirdweb/src/react/wallets/smartWallet/SmartWalletConnectUI.tsx +++ b/packages/thirdweb/src/react/wallets/smartWallet/SmartWalletConnectUI.tsx @@ -11,6 +11,7 @@ import { Container, ModalHeader } from "../../ui/components/basic.js"; import { Button } from "../../ui/components/buttons.js"; import { iconSize, spacing, fontSize } from "../../ui/design-system/index.js"; import { Text } from "../../ui/components/text.js"; +import { normalizeChainId } from "../../../wallets/utils/normalizeChainId.js"; /** * @internal @@ -18,7 +19,7 @@ import { Text } from "../../ui/components/text.js"; export const SmartConnectUI = (props: { connectUIProps: ConnectUIProps; personalWalletConfig: WalletConfig; - smartWalletChainId: bigint; + smartWalletChainId: number; }) => { const [personalWallet, setPersonalWallet] = useState(null); const { personalWalletConfig } = props; @@ -61,7 +62,7 @@ const SmartWalletConnecting = (props: { connectUIProps: ConnectUIProps; personalWallet: Wallet; personalWalletConfig: WalletConfig; - smartWalletChainId: bigint; + smartWalletChainId: number; }) => { const locale = useTWLocale().wallets.smartWallet; const createSmartWalletInstance = props.connectUIProps.createInstance; @@ -70,12 +71,12 @@ const SmartWalletConnecting = (props: { const modalSize = props.connectUIProps.screenConfig.size; const [personalWalletChainId, setPersonalWalletChainId] = useState< - bigint | undefined + number | undefined >(personalWallet.getChainId()); useEffect(() => { function handleChainChanged(chain: string) { - setPersonalWalletChainId(BigInt(chain)); + setPersonalWalletChainId(normalizeChainId(chain)); } personalWallet.events?.addListener("chainChanged", handleChainChanged); diff --git a/packages/thirdweb/src/react/wallets/smartWallet/smartWalletConfig.tsx b/packages/thirdweb/src/react/wallets/smartWallet/smartWalletConfig.tsx index f674d38ffe3..f725c6ea9ff 100644 --- a/packages/thirdweb/src/react/wallets/smartWallet/smartWalletConfig.tsx +++ b/packages/thirdweb/src/react/wallets/smartWallet/smartWalletConfig.tsx @@ -1,3 +1,4 @@ +import { getChainIdFromChain } from "../../../chain/index.js"; import { smartWallet, type SmartWalletOptions, @@ -49,12 +50,7 @@ export const smartWalletConfig = ( }, connectUI(props) { const chain = options.chain; - const chainId = - typeof chain === "bigint" - ? chain - : typeof chain === "number" - ? BigInt(chain) - : BigInt(chain.id); + const chainId = getChainIdFromChain(chain); return ( >(); diff --git a/packages/thirdweb/src/utils/any-evm/create-2-factory.ts b/packages/thirdweb/src/utils/any-evm/create-2-factory.ts index 4ab935fde09..64374c760e0 100644 --- a/packages/thirdweb/src/utils/any-evm/create-2-factory.ts +++ b/packages/thirdweb/src/utils/any-evm/create-2-factory.ts @@ -52,7 +52,7 @@ export async function getCreate2FactoryAddress( const enforceEip155 = await isEIP155Enforced(options); const chainId = getChainIdFromChain(options.chain); const custom = CUSTOM_GAS_FOR_CHAIN[chainId.toString()]; - const eipChain = enforceEip155 ? chainId : 0n; + const eipChain = enforceEip155 ? chainId : 0; const deploymentInfo = custom ? getCreate2FactoryDeploymentInfo(eipChain, { @@ -72,7 +72,7 @@ export async function getCreate2FactoryAddress( * @internal */ export async function getCreate2FactoryDeploymentInfo( - chainId: bigint, + chainId: number, gasOptions: { gasPrice?: bigint; gasLimit?: bigint }, ) { const deploymentTransaction = await getKeylessTransaction({ diff --git a/packages/thirdweb/src/utils/any-evm/is-eip155-enforced.ts b/packages/thirdweb/src/utils/any-evm/is-eip155-enforced.ts index ec54818857a..06403f060c1 100644 --- a/packages/thirdweb/src/utils/any-evm/is-eip155-enforced.ts +++ b/packages/thirdweb/src/utils/any-evm/is-eip155-enforced.ts @@ -6,7 +6,7 @@ import { getRpcClient } from "../../rpc/rpc.js"; // it's OK to cache this forever because: // 1. the results can't change // 2. the total size can be max * boolean -const EIP_ENFORCED_CACHE = new Map(); +const EIP_ENFORCED_CACHE = new Map(); type IsEIP155EnforcedOptions = { chain: Chain; diff --git a/packages/thirdweb/src/wallets/injected/index.ts b/packages/thirdweb/src/wallets/injected/index.ts index 799a7f53b00..a5d2c6df25d 100644 --- a/packages/thirdweb/src/wallets/injected/index.ts +++ b/packages/thirdweb/src/wallets/injected/index.ts @@ -60,7 +60,7 @@ export function injectedWallet(options?: InjectedWalletOptions) { */ export class InjectedWallet implements Wallet { metadata: Wallet["metadata"]; - private chainId: bigint | undefined; + private chainId: number | undefined; private account?: Account | undefined; events: Wallet["events"]; @@ -106,7 +106,7 @@ export class InjectedWallet implements Wallet { * const chainId = wallet.getChainId(); * ``` */ - getChainId(): bigint | undefined { + getChainId(): number | undefined { return this.chainId; } @@ -171,7 +171,7 @@ export class InjectedWallet implements Wallet { return this.onConnect({ provider, addresses, - targetChainId: options?.chainId ? BigInt(options.chainId) : undefined, + targetChainId: options?.chainId ? options.chainId : undefined, }); } @@ -194,7 +194,7 @@ export class InjectedWallet implements Wallet { * await wallet.switchChain(1) * ``` */ - async switchChain(chainId: bigint | number) { + async switchChain(chainId: number) { if (!this.provider) { throw new Error("no provider available"); } @@ -207,7 +207,7 @@ export class InjectedWallet implements Wallet { } catch (e: any) { // if chain does not exist, add the chain if (e?.code === 4902 || e?.data?.originalError?.code === 4902) { - const chain = await getChainDataForChainId(BigInt(chainId)); + const chain = await getChainDataForChainId(chainId); await this.provider.request({ method: "wallet_addEthereumChain", params: [ @@ -233,7 +233,7 @@ export class InjectedWallet implements Wallet { * @internal */ private async onConnect(data: { - targetChainId?: bigint; + targetChainId?: number; provider: Ethereum; addresses: string[]; }): Promise { diff --git a/packages/thirdweb/src/wallets/injected/types.ts b/packages/thirdweb/src/wallets/injected/types.ts index 53369e0f55b..f4faa90b730 100644 --- a/packages/thirdweb/src/wallets/injected/types.ts +++ b/packages/thirdweb/src/wallets/injected/types.ts @@ -30,4 +30,4 @@ export type SpecificInjectedWalletOptions = Omit< "walletId" >; -export type InjectedWalletConnectOptions = { chainId?: bigint | number }; +export type InjectedWalletConnectOptions = { chainId?: number }; diff --git a/packages/thirdweb/src/wallets/interfaces/wallet.ts b/packages/thirdweb/src/wallets/interfaces/wallet.ts index c807418bf49..8889ae8c3f0 100644 --- a/packages/thirdweb/src/wallets/interfaces/wallet.ts +++ b/packages/thirdweb/src/wallets/interfaces/wallet.ts @@ -22,7 +22,7 @@ export type Wallet = { autoConnect: (options?: any) => Promise; disconnect: () => Promise; getAccount(): Account | undefined; - getChainId(): bigint | undefined; + getChainId(): number | undefined; // OPTIONAL events?: { @@ -30,7 +30,7 @@ export type Wallet = { removeListener: WalletEventListener; }; - switchChain?: (newChainId: bigint | number) => Promise; + switchChain?: (newChainId: number) => Promise; }; export interface WalletWithPersonalWallet extends Wallet { diff --git a/packages/thirdweb/src/wallets/manager/index.ts b/packages/thirdweb/src/wallets/manager/index.ts index cfd1a169ba0..cb5db27f925 100644 --- a/packages/thirdweb/src/wallets/manager/index.ts +++ b/packages/thirdweb/src/wallets/manager/index.ts @@ -2,6 +2,7 @@ import { computedStore } from "../../reactive/computedStore.js"; import { effect } from "../../reactive/effect.js"; import { createStore } from "../../reactive/store.js"; import type { Account, Wallet } from "../interfaces/wallet.js"; +import { normalizeChainId } from "../utils/normalizeChainId.js"; import { walletStorage } from "./storage.js"; type WalletIdToConnectedWalletMap = Map; @@ -28,7 +29,7 @@ export function createConnectionManager() { // active wallet/account const activeWallet = createStore(undefined); const activeAccount = createStore(undefined); - const activeWalletChainId = createStore(undefined); + const activeWalletChainId = createStore(undefined); const activeWalletConnectionStatus = createStore("unknown"); // other connected accounts @@ -115,7 +116,7 @@ export function createConnectionManager() { }; const onChainChanged = (chainId: string) => { - activeWalletChainId.setValue(BigInt(chainId)); + activeWalletChainId.setValue(normalizeChainId(chainId)); }; const handleDisconnect = () => { @@ -165,7 +166,7 @@ export function createConnectionManager() { false, ); - const switchActiveWalletChain = async (chainId: number | bigint) => { + const switchActiveWalletChain = async (chainId: number) => { const wallet = activeWallet.getValue(); if (!wallet) { throw new Error("no wallet found"); diff --git a/packages/thirdweb/src/wallets/smart/index.ts b/packages/thirdweb/src/wallets/smart/index.ts index fc29817692e..5e0bd7d72dc 100644 --- a/packages/thirdweb/src/wallets/smart/index.ts +++ b/packages/thirdweb/src/wallets/smart/index.ts @@ -60,7 +60,7 @@ export const personalWalletToSmartAccountMap = new WeakMap(); */ export class SmartWallet implements WalletWithPersonalWallet { private options: SmartWalletOptions; - private chainId?: bigint | undefined; + private chainId?: number | undefined; private account?: Account | undefined; personalWallet: Wallet | undefined; @@ -90,7 +90,7 @@ export class SmartWallet implements WalletWithPersonalWallet { * const chainId = wallet.getChainId(); * ``` */ - getChainId(): bigint | undefined { + getChainId(): number | undefined { return this.chainId; } diff --git a/packages/thirdweb/src/wallets/smart/lib/userop.ts b/packages/thirdweb/src/wallets/smart/lib/userop.ts index ce2254d560a..01ecf59878f 100644 --- a/packages/thirdweb/src/wallets/smart/lib/userop.ts +++ b/packages/thirdweb/src/wallets/smart/lib/userop.ts @@ -179,7 +179,7 @@ async function getAccountInitCode(args: { function getUserOpHash(args: { userOp: UserOperation; entryPoint: string; - chainId: bigint; + chainId: number; }): Hex { const { userOp, entryPoint, chainId } = args; const hashedInitCode = keccak256(userOp.initCode); @@ -214,7 +214,7 @@ function getUserOpHash(args: { ); const encoded = encodeAbiParameters( [{ type: "bytes32" }, { type: "address" }, { type: "uint256" }], - [keccak256(packedUserOp), entryPoint, chainId], + [keccak256(packedUserOp), entryPoint, BigInt(chainId)], ); return keccak256(encoded); } diff --git a/packages/thirdweb/src/wallets/utils/normalizeChainId.ts b/packages/thirdweb/src/wallets/utils/normalizeChainId.ts index 7fa73229b67..a4fadd5b820 100644 --- a/packages/thirdweb/src/wallets/utils/normalizeChainId.ts +++ b/packages/thirdweb/src/wallets/utils/normalizeChainId.ts @@ -1,10 +1,14 @@ +import { hexToNumber, isHex } from "viem"; + /** * @internal */ -export function normalizeChainId(chainId: string | number | bigint) { - // always want a bigint in the end and it already handles - // hex - // integer - // bigint - return BigInt(chainId); +export function normalizeChainId(chainId: string | number): number { + if (typeof chainId === "number") { + return chainId; + } + if (isHex(chainId)) { + return hexToNumber(chainId); + } + return parseInt(chainId, 10); } diff --git a/packages/thirdweb/src/wallets/wallet-connect/index.ts b/packages/thirdweb/src/wallets/wallet-connect/index.ts index ca55f5664a5..cd7d0c1c426 100644 --- a/packages/thirdweb/src/wallets/wallet-connect/index.ts +++ b/packages/thirdweb/src/wallets/wallet-connect/index.ts @@ -54,11 +54,11 @@ const storageKeys = { const isNewChainsStale = true; const defaultShowQrModal = true; -const defaultChainId = /* @__PURE__ */ BigInt(1); +const defaultChainId = 1; type SavedConnectParams = { - optionalChains?: string[]; - chainId: string; + optionalChains?: number[]; + chainId: number; pairingTopic?: string; }; @@ -89,7 +89,7 @@ export function walletConnect(options: WalletConnectCreationOptions) { export class WalletConnect implements Wallet { private options: WalletConnectCreationOptions; private provider: InstanceType | undefined; - private chainId: bigint | undefined; + private chainId: number | undefined; private account?: Account | undefined; events: Wallet["events"]; @@ -119,7 +119,7 @@ export class WalletConnect implements Wallet { * const chainId = wallet.getChainId(); * ``` */ - getChainId(): bigint | undefined { + getChainId(): number | undefined { return this.chainId; } @@ -151,9 +151,9 @@ export class WalletConnect implements Wallet { true, savedConnectParams ? { - chainId: BigInt(savedConnectParams.chainId), + chainId: savedConnectParams.chainId, pairingTopic: savedConnectParams.pairingTopic, - optionalChains: savedConnectParams.optionalChains?.map(BigInt), + optionalChains: savedConnectParams.optionalChains, } : undefined, ); @@ -254,11 +254,12 @@ export class WalletConnect implements Wallet { async connect(options?: WalletConnectConnectionOptions): Promise { const provider = await this.initProvider(false, options); - const isChainsState = await this.isChainsStale( - [provider.chainId, ...(options?.optionalChains || [])].map(BigInt), - ); + const isChainsState = await this.isChainsStale([ + provider.chainId, + ...(options?.optionalChains || []), + ]); - const targetChainId = BigInt(options?.chainId || defaultChainId); + const targetChainId = options?.chainId || defaultChainId; const rpc = getRpcUrlForChain({ chain: targetChainId, @@ -307,8 +308,8 @@ export class WalletConnect implements Wallet { if (options) { const savedParams: SavedConnectParams = { - optionalChains: options.optionalChains?.map(String), - chainId: String(options.chainId), + optionalChains: options.optionalChains, + chainId: this.chainId, pairingTopic: options.pairingTopic, }; @@ -346,16 +347,16 @@ export class WalletConnect implements Wallet { * await wallet.switchChain(1); * ``` */ - async switchChain(chainId: number | bigint) { + async switchChain(chainId: number) { const provider = this.assertProvider(); - const chainIdBigInt = BigInt(chainId); + try { - const namespaceChains = this.getNamespaceChainsIds().map(BigInt); + const namespaceChains = this.getNamespaceChainsIds(); const namespaceMethods = this.getNamespaceMethods(); - const isChainApproved = namespaceChains.includes(chainIdBigInt); + const isChainApproved = namespaceChains.includes(chainId); if (!isChainApproved && namespaceMethods.includes(ADD_ETH_CHAIN_METHOD)) { - const chain = await getChainDataForChainId(BigInt(chainId)); + const chain = await getChainDataForChainId(chainId); const firstExplorer = chain.explorers && chain.explorers[0]; const blockExplorerUrls = firstExplorer ? { blockExplorerUrls: [firstExplorer.url] } @@ -372,10 +373,8 @@ export class WalletConnect implements Wallet { }, ], }); - const requestedChains = (await this.getRequestedChainsIds()).map( - BigInt, - ); - requestedChains.push(chainIdBigInt); + const requestedChains = await this.getRequestedChainsIds(); + requestedChains.push(chainId); this.setRequestedChainsIds(requestedChains); } await provider.request({ @@ -408,7 +407,7 @@ export class WalletConnect implements Wallet { const { EthereumProvider, OPTIONAL_EVENTS, OPTIONAL_METHODS } = await import("@walletconnect/ethereum-provider"); - const targetChainId = BigInt(connectionOptions?.chainId || defaultChainId); + const targetChainId = connectionOptions?.chainId || defaultChainId; const rpc = getRpcUrlForChain({ chain: targetChainId, @@ -448,7 +447,7 @@ export class WalletConnect implements Wallet { const chains = [ targetChainId, ...(connectionOptions?.optionalChains || []), - ].map(BigInt); + ]; const isStale = await this.isChainsStale(chains); if (isStale && provider.session) { @@ -514,9 +513,9 @@ export class WalletConnect implements Wallet { * Get the last requested chains from the storage. * @internal */ - private async getRequestedChainsIds(): Promise { + private async getRequestedChainsIds(): Promise { const data = await walletStorage.get(storageKeys.requestedChains); - return (data ? JSON.parse(data) : []).map(BigInt); + return data ? JSON.parse(data) : []; } /** @@ -540,7 +539,7 @@ export class WalletConnect implements Wallet { * @param connectToChainId * @internal */ - private async isChainsStale(chains: bigint[]) { + private async isChainsStale(chains: number[]) { const namespaceMethods = this.getNamespaceMethods(); // if chain adding method is available, then chains are not stale @@ -554,7 +553,7 @@ export class WalletConnect implements Wallet { } const requestedChains = await this.getRequestedChainsIds(); - const namespaceChains = this.getNamespaceChainsIds().map(BigInt); + const namespaceChains = this.getNamespaceChainsIds(); // if any of the requested chains are not in the namespace chains, then they are stale if ( @@ -572,11 +571,8 @@ export class WalletConnect implements Wallet { * Set the requested chains to the storage. * @internal */ - private setRequestedChainsIds(chains: bigint[]) { - walletStorage.set( - storageKeys.requestedChains, - JSON.stringify(chains.map(Number)), - ); + private setRequestedChainsIds(chains: number[]) { + walletStorage.set(storageKeys.requestedChains, JSON.stringify(chains)); } /** diff --git a/packages/thirdweb/src/wallets/wallet-connect/types.ts b/packages/thirdweb/src/wallets/wallet-connect/types.ts index a3944108281..d6224f760ff 100644 --- a/packages/thirdweb/src/wallets/wallet-connect/types.ts +++ b/packages/thirdweb/src/wallets/wallet-connect/types.ts @@ -26,8 +26,8 @@ export type WalletConnectCreationOptions = { }; export type WalletConnectConnectionOptions = { - chainId?: number | bigint; - optionalChains?: (number | bigint)[]; + chainId?: number; + optionalChains?: number[]; showQrModal?: boolean; pairingTopic?: string; qrModalOptions?: WalletConnectQRCodeModalOptions;