// 安裝該依賴 並引用
import { useMetaMask } from 'metamask-react';
// 使用該鉤子 獲取小狐狸錢包資訊 鏈id是十六進制 例如以太坊鏈是0x1
const { status, connect, account, chainId: hexChainId, ethereum } = useMetaMask();
// 判斷插件與錢包連線狀態
const metaMaskStatusMap = {
initializing: 'initializing', // 初始化
unavailable: 'unavailable', // 沒裝插件
notConnected: 'notConnected', // 裝了 沒連上
connecting: 'connecting', // 裝了 連接中
connected: 'connected', // 裝了 連上
};
// 點擊按鈕 連上小狐狸錢包
const handleConnectMetaMask = useCallback(async () => {
console.log('handleConnectMetaMask');
// 沒裝MetaMask插件
if (status === metaMaskStatusMap.unavailable) {
toast.error(t('Install the MetaMask plugin before using this feature'), {
hideProgressBar: true,
});
return;
}
try {
await connect(); // 連上 改變錢包狀態status
} catch (error) {
console.error('Failed to connect MetaMask wallet:', error);
}
}, [status, account]);
以太坊虛擬機(Ethereum Virtual Machine,縮寫為 EVM)是以太坊區塊鏈平台的運行環境。用於執行以太坊智能合約的程式碼。
The Ethereum Virtual Machine (EVM) serves as the runtime environment for the Ethereum blockchain platform. It is responsible for executing the code of Ethereum smart contracts.
確認代號 https://chainlist.org/zh?search=[幣種縮寫] 某些幣種可能在evm上沒有id,因為該幣種不是在evm上的合約
Web3 Wallet Transaction Flow
1. Connect to the Ethereum Network: