0

I am using Ganache and Truffle console. I compile and migrate the following contract:

// SPDX-License-Identifier: MIT
pragma solidity ^0.6.10;

contract EthReceiver {
    address payable private owner;

    constructor() public {
        owner = msg.sender;
    }

    receive() external payable {
        require(msg.value > 0, "No ETH sent.");
    }

    function returnBalance() public view returns(uint) {
        return address(this).balance;
    }
}

I want the contract to be able to receive Ether and to be able to return its Ether balance through the returnBalance() method.

I run the following code in the Truffle console:

EthReceiver.deployed().then(instance => {return instance.returnBalance()}).then(value => console.log(value))

This is code I looked up here: https://stackoverflow.com/questions/46496845/how-to-use-truffle-console

My console output is:

{ Error: Returned error: VM Exception while processing transaction: invalid opcode
    at EthReceiver.deployed.then.instance (evalmachine.<anonymous>:0:58)
    at process._tickCallback (internal/process/next_tick.js:68:7)
  hijackedStack:
   'Error: Returned error: VM Exception while processing transaction: invalid opcode\n    at Object.ErrorResponse (/Users/cex/.npm-packages/lib/node_modules/truffle/build/webpack:/node_modules/web3-core-helpers/src/errors.js:29:1)\n    at /Users/cex/.npm-packages/lib/node_modules/truffle/build/webpack:/node_modules/web3-core-requestmanager/src/index.js:140:1\n    at /Users/cex/.npm-packages/lib/node_modules/truffle/build/webpack:/packages/provider/wrapper.js:112:1\n    at XMLHttpRequest.request.onreadystatechange (/Users/cex/.npm-packages/lib/node_modules/truffle/build/webpack:/node_modules/web3-providers-http/src/index.js:96:1)\n    at XMLHttpRequestEventTarget.dispatchEvent (/Users/cex/.npm-packages/lib/node_modules/truffle/build/webpack:/node_modules/xhr2-cookies/dist/xml-http-request-event-target.js:34:1)\n    at XMLHttpRequest._setReadyState (/Users/cex/.npm-packages/lib/node_modules/truffle/build/webpack:/node_modules/xhr2-cookies/dist/xml-http-request.js:208:1)\n    at XMLHttpRequest._onHttpResponseEnd (/Users/cex/.npm-packages/lib/node_modules/truffle/build/webpack:/node_modules/xhr2-cookies/dist/xml-http-request.js:318:1)\n    at IncomingMessage.<anonymous> (/Users/cex/.npm-packages/lib/node_modules/truffle/build/webpack:/node_modules/xhr2-cookies/dist/xml-http-request.js:289:47)\n    at IncomingMessage.emit (events.js:194:15)\n    at IncomingMessage.EventEmitter.emit (domain.js:459:23)\n    at endReadableNT (_stream_readable.js:1125:12)\n    at process._tickCallback (internal/process/next_tick.js:63:19)' }

My exact versions are:

Truffle v5.1.32 (core: 5.1.32)
Solidity - 0.6.10 (solc-js)

Why am I getting this result? What should I do in order to fix this issue?

0

After digging into the question I think that the root of the problem is Ganache. The code in the post has no issues with it, compiles and migrates properly even with Ganache; and when deployed in Remix IDE or Truffle Develop has fully functional methods.

While I don't have the knowledge to assess what part of Ganache or interaction between it and Truffle Console is causing the problem, the way I'm going about solving it is to simply stop using Ganache and using Truffle Develop instead.

On a side note:

function returnBalance() public view returns(uint) {
    return address(this).balance;
}

Is an unnecessary function because

instance = await EthReceiver.deployed()
balance = web3.eth.getBalance(instance.address)

Can be used for any contract anyway.

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.