Skip to content

switchChain

Action for switching to a different Starknet chain.

Import

import { switchChain } from "starkweb/core";

Usage

index.ts
import { switchChain } from "starkweb/core";
import { mainnet } from "starkweb/chains"; 
import { config } from "./config";
 
await switchChain(config, {
  chainId: mainnet.chain_id, 
});

Parameters

import { type SwitchChainParameters } from "starkweb/core";

chainId

Hex

The ID of the chain to switch to.

index.ts
import { switchChain } from "starkweb/core";
import { mainnet } from "starkweb/chains"; 
import { config } from "./config";
 
await switchChain(config, {
  chainId: mainnet.chain_id, 
});

connector (optional)

Connector | undefined

The connector to use for switching chains. If not provided, uses the current connector.

index.ts
import { switchChain } from "starkweb/core";
import { injected } from "starkweb/connectors"; 
import { config } from "./config";
 
const connector = injected();
await switchChain(config, {
  chainId: mainnet.chain_id,
  connector, 
});

addStarknetChainParameter (optional)

Partial<AddStarknetChainParameters> | undefined

Optional parameters for adding a new chain to the wallet.

Return Type

import { type SwitchChainReturnType } from "starkweb/core";

Returns the chain object that was switched to, containing:

chain_id

Hex

The ID of the chain that was switched to.

name

string

The name of the chain.

rpcUrls

RpcUrls

The RPC URLs for the chain.

Example

example.ts
import { switchChain } from "starkweb/core";
import { mainnet, testnet } from "starkweb/chains"; 
import { injected } from "starkweb/connectors"; 
import { config } from "./config";
 
// Switch to mainnet
const result = await switchChain(config, {
  chainId: mainnet.chain_id, 
});
console.log("Switched to chain:", result.name);
 
// Switch using specific connector
const connector = injected();
await switchChain(config, {
  chainId: testnet.chain_id, 
  connector, 
});

Error

import { type SwitchChainErrorType } from "starkweb/core";

TanStack Query

import {
  type SwitchChainData,
  type SwitchChainVariables,
  type SwitchChainMutate,
  type SwitchChainMutateAsync,
  switchChainMutationOptions,
} from "starkweb/query";