0

async function onInit() { await window.ethereum.enable(); const accounts = await window.ethereum.request({ method: 'eth_requestAccounts' }); const account = accounts[0]; console.log(account) window.ethereum.on('accountsChanged', function (accounts) { // Time to reload your interface with accounts[0]! console.log(accounts[0]) }); }

onInit();

const getBalance=async ()=>{ const address=await onInit(); const balance=await web3.eth.getBalance(address); console.log(balance); } getBalance(); This shows the error; Error: Provided address [object Promise] is invalid, the capitalization checksum test failed, or it's an indirect IBAN address which can't be converted.

1
  • 1
    Can you format/indent the code? Surrounding it by ``` before and after the source will mark it as code.
    – Ismael
    Sep 21, 2021 at 5:17

1 Answer 1

1

I was having a similar issue, I would consider using ethers (https://docs.ethers.io/v5/) instead of web3 as it's less buggy and the documentation is better. It was hard to see from your formatting but my solution for getting the balance is below:

import { ethers } from "ethers";
const provider = new ethers.providers.Web3Provider(window.ethereum)

const ethbalance = await provider.getBalance(address)

hope that helps, Henry

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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