Handling Networks
The following guide will show you how to handle networks with Openfort's smart wallets using the EIP-1193 provider.
Get the current network
To get the current network, you can use the eth_chainId
request (with no params
)
getCurrentNetwork.ts
import openfort from "./openfortConfig"
// This example assumes you have already checked that Openfort 'embeddedState' is
// `ready` and the user is `authenticated`
const provider = await openfort.embeddedWallet.getEthereumProvider();
const chainId = await provider.request({
method: "eth_chainId",
params: [],
});
Switch networks
To switch the smart wallet to a different network, send a wallet_switchEthereumChain
JSON-RPC request to the wallet's EIP-1193 provider. In the request's params
, specify your target chainId
as a hexadecimal string.
switchNetwork.ts
import openfort from "./openfortConfig"
// This example assumes you have already checked that Openfort 'embeddedState' is
// `ready` and the user is `authenticated`
const provider = await openfort.embeddedWallet.getEthereumProvider();
await provider.request({
method: 'wallet_switchEthereumChain',
// Replace '0x5' with the chainId of your target network
params: [{chainId: '0x5'}],
});