1
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>}
  </>
 );
}

ERROR: The MetaMask Ethereum provider does not support synchronous methods like eth_getBalance without a callback parameter.

1 Answer 1

1

Try to call the library.eth.getBalance function with a callback instead of a .then:

library.eth.getBalance(wallet, walletBalance => console.log(walletBalance));
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.