0

I'm trying to call a interface function for testing, but it can't work.

Here is my test contract:

pragma solidity ^0.4.24;

interface InterfaceTest{
    function testInterface() external pure returns(uint);
}

contract TestContract {   

    InterfaceTest _inter;  

    function test2(address _addr) public returns(uint) {
        _inter = InterfaceTest(_addr);
        return 2;
    }

    function test4() public view returns(uint) {
        uint t = _inter.testInterface();
        return t;
    }
}

And here is the implement:

interface InterfaceTest{
    function testInterface() external pure returns(uint);
}

contract InterfaceContract is InterfaceTest {
    function testInterface() external pure returns(uint) {
        return 9;
    }
}

I deployed them with truffle(4.1.14) and Ganache, the test2() function work well and return 2, but when I tried to call test4(), it said VM Exception while processing transaction: revert, is there any Error in my code?


There is an interesting error I found:

function test2(address _addr) public {
    _interAddr = _addr;
}

function test22() public view returns(address) {
    return _interAddr;
}

function test222(address _addr) public view returns(bool) {
    if (_addr == _interAddr){
        return true;
    }
    return false;
}

First I set the _interAddr, and then I get it, but the TWO address are different, what looks like set 0x1d77078832a0f88c6e064cc5e4df88b83d3dd8ca and get 0x1d77078832a0f8eca804a4989f812d2e80000000. in test222, the last numbers have no effect to the result, my contract always return true.

Does anyone have the same error?


Finally I resolved. If you want to set an address, you must enclose address in "", what a stupid mistake. This problem has been solved, thank you guys.

0

1 Answer 1

0

I hope this will help you

contract TestContract{   

    InterfaceTest _inter;
    InterfaceContract ts = new InterfaceContract();

    function test2(address _addr) public returns(uint) {
        _inter = InterfaceTest(_addr);
        return 2;
    }


    function test4()public view returns(uint)  {
        uint t = ts.testInterface();
        return t;

    }
}
3
  • Thank you first. The two contracts are in different .sol file, so just declare a 'InterfaceContract' instance will cause an error.
    – user48469
    Commented Nov 27, 2018 at 13:55
  • If both .sol files are different then you can import 'InterfaceContract' sol file in your 'TestContract' sol file and then create 'InterfaceContract' instance will work fine. Commented Nov 28, 2018 at 4:35
  • Yes it works, thank you, well, actually that seems not an 'interface' solution. If I want to interact with a deployed contract or extend it, it's impossible to import a new contract. Thank you anyway.
    – user48469
    Commented Nov 28, 2018 at 5:22

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.