2

I'm trying to create a contract which receives value to a function called contribute.

Then I shall call another contract function with value. I wonder how this is done, it's not very clear.

This is the code I got so far (all other functions and variables are left out for simplicity):

contract Hub {
    function contribute(address _address) payable {
       Project project = Project(_address);
       project.fund.value(msg.value)();
    }
}

contract Project {
    function fund(address sender) payable {
       amount += msg.value;
       contributors[sender] = msg.value;
    }
}

This gives me invalid opcode for some reason. What might be wrong? Am I trying to do it wrong?

Opcode stack is here: https://gist.github.com/vongohren/160b61e5fd2b6c0c85fb8829aed9b3b2

2 Answers 2

2

fund requires a parameter, so try passing an argument to it:

project.fund.value(msg.value)(msg.sender)


By not passing an argument, Solidity probably tries to look for a function fund that takes no parameters, and it won't find and thus will invoke the fallback function, which in turn will cause an internal exception via invalid opcode if it isn't payable.

2
  • That was not it, I did try with and without parameters, but it still provides invalid opcode :(
    – vonGohren
    Commented May 22, 2017 at 5:21
  • You might need to comment out more code, or show more. Gist was tested in Remix and it worked.
    – eth
    Commented May 22, 2017 at 9:16
1

The problem for me was that I did not send enough gas. I figured this out with different ways, but the main problem was that since I call another contracts method, which costed more gas then sent. It threw a invalid op code, and not out of gas.

So it might be smart to specifiy gas usage aswell in your contract that will call other contracts, so that the user will get a proper feedback. This way if, not sending enough gas to the main contract, that will be the proper error! Not an bogus invalid opcode error.

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.