Solidity 0.4.24 – Smart Contract Interaction Example for Remix

blockchain-interoperationcontract-developmentevmremix

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

Best Answer

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.

Related Topic