锐单电子商城 , 一站式电子元器件采购平台!
  • 电话:400-990-0325

1.MetaMask-注意事项

时间:2023-12-03 13:37:02 bpkgv2a1m传感器

浏览器检测

MetaMask检测

用户状态

连接MetaMask

chainid network

浏览器检测

验证浏览器是否正在运行MetAmask

if (typeof window.ethereum !== 'undefined') { console.log('MetaMask is installed!'); }

MetaMask检测

与其他浏览器兼容,检测是否使用metaMask

ethereum.isMetaMask

用户状态

当前网络

ethereum.networkVersion

当前账户

ethereum.selectedAddress

连接MetaMask

官方建议不要在页面加载时要求,尽量用按钮点击连接,挂起时禁止按钮。

点击按钮连接dapp

ethereum.request({ method: 'eth_requestAccounts' });

示例

 

Account:

const ethereumButton = document.querySelector('.enableEthereumButton'); const showAccount = document.querySelector('.showAccount'); ethereumButton.addEventListener('click', () => { getAccount(); }); async function getAccount() { const accounts = await ethereum.request({ method: 'eth_requestAccounts' }); const account = accounts[0]; showAccount.innerHTML = account; }

当钱包多账户切换时,需要使用监控事件

ethereum.on('accountsChanged', (accounts) => { // Handle the new accounts, or lack thereof. // "accounts" will always be an array, but it can be empty. console.log(accounts,"accountsChanged") });

网络也需要监控,需要保证dapp正确使用链条

ethereum.on('chainChanged', (chainId) => { // Handle the new chain. // Correctly handling chain changes can be complicated. // We recommend reloading the page unless you have good reason not to. window.location.reload(); });

chainid network

Hex

Decimal

Network

0x1

1

Ethereum Main Network (Mainnet)

0x3

3

Ropsten Test Network

0x4

4

Rinkeby Test Network

0x5

5

Goerli Test Network

0x2a

42

Kovan Test Network

0x80

128

heco mainnet network

0x100

256

heco testnet network

锐单商城拥有海量元器件数据手册IC替代型号,打造电子元器件IC百科大全!

相关文章