Skip to main content

wallet_addEthereumChain

Request UKey Wallet to add a new EVM network. After the user confirms, the network will be saved to the wallet; if the network already exists, the wallet may switch directly to the chain.


Inputs

Pass an array whose first item is the chain configuration object:

FieldVariantNeedRemarks
chainIdstringyesChain ID in 0x-prefixed hex
chainNamestringyesNetwork name shown to the user
nativeCurrencyobjectyesNative asset metadata
nativeCurrency.namestringyesNative asset name
nativeCurrency.symbolstringyesNative asset ticker, usually 2-6 chars
nativeCurrency.decimalsnumberyesNative asset decimals, usually 18
rpcUrlsstring[]yesRPC endpoints; provide at least one
blockExplorerUrlsstring[]noExplorer URLs for the Network
iconUrlsstring[]noIcon URLs for wallet display

return value

Returns null on success.


Demo

await window.$ukey.ethereum.request({
method: "wallet_addEthereumChain",
requestParams: [
{
chainId: "0x89",
chainName: "Polygon Mainnet",
nativeCurrency: {
name: "MATIC",
symbol: "MATIC",
decimals: 18,
},
rpcUrls: ["https://polygon-rpc.com"],
blockExplorerUrls: ["https://polygonscan.com"],
},
],
});

Add Base

await window.$ukey.ethereum.request({
method: "wallet_addEthereumChain",
requestParams: [
{
chainId: "0x2105",
chainName: "Base",
nativeCurrency: {
name: "Ethereum",
symbol: "ETH",
decimals: 18,
},
rpcUrls: ["https://mainnet.base.org"],
blockExplorerUrls: ["https://basescan.org"],
},
],
});

Add Arbitrum

await window.$ukey.ethereum.request({
method: "wallet_addEthereumChain",
requestParams: [
{
chainId: "0xa4b1",
chainName: "Arbitrum One",
nativeCurrency: {
name: "Ethereum",
symbol: "ETH",
decimals: 18,
},
rpcUrls: ["https://arb1.arbitrum.io/rpc"],
blockExplorerUrls: ["https://arbiscan.io"],
},
],
});

error code

CodeMessageDetails
4001User rejected the requestNetwork addition was rejected by the user
-32602Parameters failed validationNetwork configuration is invalid

illustrate

  • Wallets usually only use the first available address of rpcUrls and blockExplorerUrls.
  • Please confirm that the source is trustworthy before incoming RPC to avoid directing users to connect to malicious nodes.
  • The wallet may verify whether the RPC endpoint can respond normally to the corresponding Chain ID.
  • Some wallets have limited support for native tokens with non-18-bit precision.
  • Follows EIP-3085 specification.

Combination addition and switching

async function switchOrAddChain(chainConfig) {
try {
await window.$ukey.ethereum.request({
method: "wallet_switchEthereumChain",
requestParams: [{ chainId: chainConfig.chainId }],
});
} catch (error) {
if (error.code === 4902) {
await window.$ukey.ethereum.request({
method: "wallet_addEthereumChain",
requestParams: [chainConfig],
});
} else {
throw error;
}
}
}