0
eth_chainId
eth_accounts
eth_call
  WARNING: Calling an account which is not a contract
  From: 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266
  To:   0x5fbdb2315678afecb367f032d93f642f64180aa3

this is what logs on server.js:

Error: Error: could not decode result data (value="0x", info={ "method": "getBalance", "signature": "getBalance()" }, code=BAD_DATA, version=6.12.0)
    at makeError (C:\Users\Thato\OneDrive\Documents\Programming\DApps\DCrowdFunding\server\node_modules\ethers\lib.commonjs\utils\errors.js:129:21)
    at assert (C:\Users\Thato\OneDrive\Documents\Programming\DApps\DCrowdFunding\server\node_modules\ethers\lib.commonjs\utils\errors.js:149:15)
    at Interface.decodeFunctionResult (C:\Users\Thato\OneDrive\Documents\Programming\DApps\DCrowdFunding\server\node_modules\ethers\lib.commonjs\abi\interface.js:780:31)
    at staticCallResult (C:\Users\Thato\OneDrive\Documents\Programming\DApps\DCrowdFunding\server\node_modules\ethers\lib.commonjs\contract\contract.js:254:35)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async staticCall (C:\Users\Thato\OneDrive\Documents\Programming\DApps\DCrowdFunding\server\node_modules\ethers\lib.commonjs\contract\contract.js:219:24)
    at async Proxy.getBalance (C:\Users\Thato\OneDrive\Documents\Programming\DApps\DCrowdFunding\server\node_modules\ethers\lib.commonjs\contract\contract.js:259:20)
    at async getContractBalance (C:\Users\Thato\OneDrive\Documents\Programming\DApps\DCrowdFunding\server\server.js:23:25) {    
  code: 'BAD_DATA',
  value: '0x',
  info: { method: 'getBalance', signature: 'getBalance()' },
  shortMessage: 'could not decode result data'

...this is my code:

async function getContractBalance() {
    try {
       // const result = await contract.getBalance();
        //console.log("Contract Balance:", result);
        const balance = await contract.getBalance();
        console.log("Contract Balance:", balance); 



    } catch (error) {
        console.error("Error:", error);
    }
}

..and this is the getBalance function in the contract:

function getBalance() public view returns(uint){
    return address(this).balance;
}
4
  • 1
    Please add the line where you’re creating contract instance. Commented Apr 22 at 7:34
  • // We get the contract to deploy const Contract = await hre.ethers.getContractFactory("Crowdfunding"); const contract = await Contract.deploy(10000,43567892,9); const [deployer] = await hre.ethers.getSigners(); Commented Apr 22 at 13:12
  • Is your contract getting deployed successfully? Can you share the minimum reproducible code of your contract? Commented Apr 22 at 13:36
  • Instead of adding in comments, please add the same by editing in your question, and respond in the comment when you edit the question. Commented Apr 22 at 13:36

1 Answer 1

0

address().balance returns uint256 type.
so you need your solidity code to be:

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

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.