浏览器安装多个 web3 钱包,前端连接钱包指定用 MetaMask 的方式
先通过 ethereum?.providerMap 尝试找 MetaMask,如果找不到,返回默认的 ethereum
export default function getEthereum() {
let ethereum = (window as any).ethereum?.providerMap?.get('MetaMask') || (window as any).ethereum?.providerMap?.get('metamask') || (window as any).ethereum?.providerMap?.get('Metamask')
if (!ethereum) {
ethereum = (window as any).ethereum
}
return ethereum
}
使用方法:
const ethereum = getEthereum()