1

I was going through this code and I decided to test it on RemixIDE... Creating a function that calls another contract

I kept getting "VM Exception while processing transaction: revert" when I ran a function from the caller contract. I googled the error, tried all suggestions of getting rid of the problem but none has worked.

I had earlier tried running the code from this tutorial (still about smart contract interaction) https://www.youtube.com/watch?v=zjUbLC21avw&t=425s got errors, then after research noticed the code was outdated syntax, thus the errors.

So my question is... Is it that the code from Creating a function that calls another contract , outdated, or is it a bug in Remix/Solidity... If it's a bug, how do you get rid of it? Is there a currently working example of contract interaction for version 0.4.24?

The code I am running on remix IDE is...

pragma solidity ^0.4.24;

contract Caller {

    Called public called_address;

    function set_address(address _addy) public {
        called_address = Called(_addy);
    }

    function set(uint256 _var) public {
        called_address.set(_var);
    } 

    function set_call(address _called, uint256 _var) public {
        require(_called.call(bytes4(keccak256("set(uint256)")), _var));
    }
}

interface Called{
    function set(uint) external;
    function get() external view returns (uint);
}

Code for the called contract

pragma solidity ^0.4.24;

contract Called{
    uint public myuint;

    function myuint() public view returns(uint _myuint) {
        _myuint = myuint;
    }

    function set(uint _var) public {
        myuint = _var;
    }

    function get() public view returns (uint){
        return myuint;
    }
}

Also to note is that I had to define a getter function myself as the IDE was giving me an error saying some functions are not defined/used... Thank you for your help.

I am using web3 provider with an local instance of the blockchain via Genache

  • Your code seems to work fine for me. You may want to describe your testing in more detail. It's a little worrisome that you used the same name for your interface and contract. It might make it easier to make sure you're deploying the right thing if you renamed one or the other. – smarx Aug 28 '18 at 16:17
  • I just tested it again with Javascript VM and it worked. But once I try it with Web3 provider and a local blockchain using Genache... still brings the error. Tried restarting Genache and chrome, (Because I read that the error is sometimes caused by genache or metamask) , the error still appears. All I wanted to do (mostly) is just to make sure the code works. Will try and dig in deeper for a solution then will post it. @smarx – YulePale Aug 29 '18 at 6:19
1

Current online version of remix (0.6.4) has some bugs related to intercontract communication which are present using external web3 provider or facility. Try to use current alpha version (http://remix-alpha.ethereum.org) and, if all is then ok, simply switch to it. The current alpha version shall be the current online version (as 0.7.0 version) in short.

0

Finally... I have code that works...

Caller code:

pragma solidity ^0.4.24;

contract Caller {

    address public called_address;

    function set_address(address _addy) public {
        called_address = address(_addy);
    }
    function getCalleeValue() public view returns (uint){
        Called c = Called(called_address);
        return c.get();
    }
    function setCalleeValue(uint newValue) public returns (uint){
        Called c = Called(called_address);
        c.set(newValue);

}

interface Called{

    function set(uint) external;
    function get() external view returns (uint);
}

Called Contract code:

pragma solidity ^0.4.24;

contract Called{
    uint public myuint;

    function myuint() public view returns(uint _myuint) {
        _myuint = myuint;
    }

    function set(uint _var) public {
        myuint = _var;
    }

    function get() public view returns (uint){
        return myuint;
    }
}

It seems like there was an issue with how the first code stored the addresses. Now that I have changed the code to store the address explicitly as an address type, there is no issue in Remix IDE using Web3 provider and Genache as the local blockchain.

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.