0

I am trying to call another contracts function. When I test this in remix and inspect the transaction I don't see that the event is executed. No error is display either.

I execute check function in caller.sol passing the callee.sol contract address but the event never runs

caller.sol

contract Caller {        
    function check(address callbackAddress) returns (string) {
           callbackAddress.call(bytes4(sha3("callMe")));
           return 'something';  
    }
}

callee.sol

contract Callee {  
    event EventTest(string value1);

    function callMe(string value) returns (string) {
            EventTest('testing')
    }
}
1

This line:

callbackAddress.call(bytes4(sha3("callMe")));

uses the wrong function signature. It should be this:

callbackAddress.call(bytes4(sha3("callMe(string)")));

Always make sure to check the return value of call(). I assume it's failing here, so the issue isn't that the event isn't being logged; it's that the call fails (because there's no matching function selector).

EDIT

Working code I tested after silencing all the compiler warnings and adding a require to make sure the call was succeeding:

pragma solidity ^0.4.20;

contract Caller {
    function check(address callbackAddress) public {
        require(callbackAddress.call(bytes4(keccak256("callMe(string)"))));
    }
}

contract Callee {  
    event EventTest(string value1);

    function callMe(string) public {
        EventTest('testing');
    }
}

I tested in Remix with the JavaScript VM by first deploying Callee and then deploying Caller and invoking check with the address of the deployed Callee. The event was logged as expected.

Changes I made are below. Only the first two are necessary to make the code work:

  • Use "callMe(string)" to get the correct function selector.
  • Add a semicolon at the end of EventTest('testing').
  • Specify a compiler version.
  • Get rid of the return type on check, since it's going to be invoked by a transaction.
  • Explicitly mark check as public.
  • Use keccak256 instead of (the deprecated) sha3.
  • Drop the return type from callMe, since it's going to be invoked via call and wasn't returning anything anyway.
  • Drop the name of callMe's parameter, since it's unused.
  • Explicitly mark callMe as public.
  • Thanks for the reply. I tried that but still no luck – Decrypter Feb 21 '18 at 6:09
  • I edited my post to include the working code. – smarx Feb 21 '18 at 6:15
  • Thanks. I am using my local testrpc and expecting to see the event in the decoded output section in remix. Should it be displayed there in this scenario? – Decrypter Feb 21 '18 at 6:22
  • Yes, it should. – smarx Feb 21 '18 at 6:26
  • Sorry, no, it should be in the "logs" section in Remix. I just tested with ganache-cli, and it works there too. – smarx Feb 21 '18 at 6:27

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.