1

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

Details:

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('http://127.0.0.1:8545')
const c = new ethers.Contract("0x5FbDB2315678afecb367f032d93F642f64180aa3", MyContract.abi, localNodeProvider)
const balance = await c.getContractBalance()
console.log(balance.toNumber())

I get this exception thrown below.

{"reason":null,"code":"CALL_EXCEPTION","method":"getContractBalance()","data":"0x","errorArgs":null,"errorName":null,"errorSignature":null,"address":"0x5FbDB2315678afecb367f032d93F642f64180aa3","args":[],"transaction":{"data":"0x6f9fb98a","to":"0x5FbDB2315678afecb367f032d93F642f64180aa3","from":"0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"}}

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

0

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.