import { useWeb3React } from "@web3-react/core";
import { InjectedConnector } from "@web3-react/injected-connector";
const injected = new InjectedConnector({
supportedChainIds : [1,2,3,4]
})
function App() {
const {active , account , library ,connector , activate , deactivate} = useWeb3React()
async function connect (){
try{
await activate (injected)
}catch (err){
console.log(err)
}}
async function disconnect(){
try{
await deactivate()
}catch(err){
console.log(err)
}
}
async function getBalance(wallet){
try{
library.eth.getBalance(wallet).then(walletBalance => console.log(walletBalance))
}catch(err){
console.log(err);
}
}
return (
<>
{console.log({active , account , library , connector})}
<button onClick={connect}>connect to wallet</button>
<p>{activate ? account :'disconnected'}</p>
<button onClick={disconnect}>disconnected</button>
{activate && <button onClick={()=>getBalance(account)}>get Balance</button>}
</>
);
}错误:在没有回调参数的情况下,MetaMask提供程序不支持像eth_getBalance这样的同步方法。
发布于 2022-09-12 13:35:39
尝试使用回调来调用library.eth.getBalance函数,而不是.then:
library.eth.getBalance(wallet, walletBalance => console.log(walletBalance));https://ethereum.stackexchange.com/questions/135439
复制相似问题