UPDATE: The short description is this function works the contract is deployed on Mumbai Polygon, but fails in Hardhat locally.


Here is my simple function which has no issue when testing, but breaks when trying to execute from the front end

function getContractBalance() public view returns (uint256) {
    return address(this).balance;

My front end code is such:

const localNodeProvider = ethers.providers.getDefaultProvider('')
const c = new ethers.Contract("0x5FbDB2315678afecb367f032d93F642f64180aa3", MyContract.abi, localNodeProvider)
const balance = await c.getContractBalance()

I get this exception thrown below.


When I use a Web3Provider, getContractBalance also fails. However, other functions that are not view but are payable seem to work fine.

const connection = await web3Modal.connect()
                const provider = new ethers.providers.Web3Provider(connection)
                const signer = provider.getSigner()
                const contract = new ethers.Contract("0x5FbDB2315678afecb367f032d93F642f64180aa3", PictosPointsBank.abi, signer)
                await contract.getContractBalance()

1 Answer 1


Hii even I ran out in the same issue...The problem with me was My metamask was connected to another network. I switched to hardhat network in metamask it worked.

Your Answer

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

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