主网及代币管理
跟踪用户的网络链 ID 非常重要,因为所有 RPC 请求都会提交到当前连接的网络。 使用 eth_chainId RPC 方法检测用户当前网络的链 ID。
以 ETH 链为例,侦听 chainChanged 提供程序事件以检测用户何时更改网络:
js
const chainId = await provider.request({ method: "eth_chainId" });
provider.on("chainChanged", handleChainChanged);
function handleChainChanged(chainId) {
// We recommend reloading the page, unless you must do otherwise.
window.location.reload();
}
const chainId = await provider.request({ method: "eth_chainId" });
provider.on("chainChanged", handleChainChanged);
function handleChainChanged(chainId) {
// We recommend reloading the page, unless you must do otherwise.
window.location.reload();
}
主网支持判断的通常做法
- 调用钱包方法切换至目标链
- 切换失败标识不存在
以 ETH 为例代码如下:
js
try {
await provider.request({
method: "wallet_switchEthereumChain",
params: [{ chainId: "0xf00" }],
});
} catch (switchError) {
// This error code indicates that the chain has not been added to MetaMask.
if (switchError.code === 4902) {
try {
await provider.request({
method: "wallet_addEthereumChain",
params: [
{
chainId: "0xf00",
chainName: "...",
rpcUrls: ["https://..."] /* ... */,
},
],
});
} catch (addError) {
// Handle "add" error.
}
}
// Handle other "switch" errors.
}
try {
await provider.request({
method: "wallet_switchEthereumChain",
params: [{ chainId: "0xf00" }],
});
} catch (switchError) {
// This error code indicates that the chain has not been added to MetaMask.
if (switchError.code === 4902) {
try {
await provider.request({
method: "wallet_addEthereumChain",
params: [
{
chainId: "0xf00",
chainName: "...",
rpcUrls: ["https://..."] /* ... */,
},
],
});
} catch (addError) {
// Handle "add" error.
}
}
// Handle other "switch" errors.
}
主网添加
使用钱包添加主网功能:如何添加/切换主链?
代码模式添加
js
// 要添加的主网信息
let chainInfo = {
chainId: "0xaa36a7", // A 0x-prefixed hexadecimal string
chainName: "sepolia",
nativeCurrency: "ETH",
iconUrls: "xxx",
rpcUrls: ["https://sepolia.drpc.org"],
blockExplorerUrls: ["https://sepolia.drpc.org"],
nativeCurrency: {
name: "ETH",
symbol: "ETH",
decimals: 18,
},
};
await provider.request({
method: "wallet_addEthereumChain",
params: [chainInfo],
});
// 要添加的主网信息
let chainInfo = {
chainId: "0xaa36a7", // A 0x-prefixed hexadecimal string
chainName: "sepolia",
nativeCurrency: "ETH",
iconUrls: "xxx",
rpcUrls: ["https://sepolia.drpc.org"],
blockExplorerUrls: ["https://sepolia.drpc.org"],
nativeCurrency: {
name: "ETH",
symbol: "ETH",
decimals: 18,
},
};
await provider.request({
method: "wallet_addEthereumChain",
params: [chainInfo],
});
什么是代币
代币是智能合约平台(如以太坊)的特殊费用载体,用户可以在平台上创建、发行并管理代币(主区块链的衍生品)。
代币添加
- 使用钱包添加代币功能:如何添加代币/NFT?
- 代币余额查询及交易参考 智能合约
js
const customTokenInfo = {
type: "ERC20",
options: {
address: "0xd00981105....fe7e037d935b513",
symbol: "BWB",
decimals: 18,
image: "http://...",
},
};
const added = await provider.request({
method: "wallet_watchAsset",
params: customTokenInfo,
});
const customTokenInfo = {
type: "ERC20",
options: {
address: "0xd00981105....fe7e037d935b513",
symbol: "BWB",
decimals: 18,
image: "http://...",
},
};
const added = await provider.request({
method: "wallet_watchAsset",
params: customTokenInfo,
});