1.MetaMask-注意事项

浏览器检测

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' });

示例

<button class="enableEthereumButton">Enable Ethereum</button> <h2>Account: <span class="showAccount"></span></h2>

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

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

盒曰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值