1

In solidity, how do I know the return result if I call from another contract? From contract B, call contract A's method methodA, if contract B, can I write:

Contract B{
     bool callResult=contractA.methodA(param);
}

or something similiar to this in solidity, any example?

  • 1
    If contractA.methodsA returns a boolean value, then yes. – Henk Aug 27 '18 at 11:31
2

Here's a little gist.

pragma solidity 0.4.24;

contract A {

    function methodA(string param) public pure returns(string) {
        // ignoring param but leaving it in the pattern to show how it would fit
        return "Behold";
    }

}

contract B {

    A contractA;

    constructor() public {
        contractA = new A();
    }

    function callA(string param) public view returns(string) {
        string memory callResult=contractA.methodA(param);
        return callResult;
    }

}

To see it work in Remix,

  1. deploy B then
  2. call B.callA("anything").

contractA is cast as type contract A and then instantiated by the constructor that just creates a new one and keeps an eye on where it is. This sidesteps some real use-case deployment concerns.

Another non-obvious aspect of this is that when B is compiled, the compiler can "see" the code for A. This familiarizes B with the interface to A.

Hope it helps.

  • in my case it is different, the contract A has been deployed already so if I deploy B right now, it will say the declaration A contractA; is wrong, since the contract does not know A. I use remix to compile B only – Louis Aug 28 '18 at 5:01
  • I also try resultCall=contractAdd.call(bytes4(sha3("removeToken(address, uint256)")),contractAdd,amount);, but it looks like the return is error, not string? or bool? – Louis Aug 28 '18 at 5:07
  • That's a separate issue. Deployment. You need to add a function or a constructor argument to tell it where the deployed contract is. – Rob Hitchens - B9lab Aug 28 '18 at 5:18
  • any example? Thanks, or I need something like interface? – Louis Aug 28 '18 at 5:22

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.