I have a contract named Call
:
contract Call {
...
function () payable {
// TODO: Call the call function in the main contract
// and forward all funds (msg.value) sent to this contract
// and passing in the following data: msg.sender
}
}
Here is the Main
contract:
contract Main {
...
function call(address senderAddress) public {
// Make the code in here run when someone sends ethers to the Call contract
}
}
So, I want to have it so that whenever someone sends ethers the Call
contract, it forwards all the ethers to the Main
contract while running the call
function in the Main
contract with the necessary arguments.
Can this be done?
Best Answer
Update per @Girish comment, in Solidity 0.6+ the syntax has changed to:
address.function{value:msg.value}(arg1, arg2, arg3)
Original
The general syntax for calling a function in another contract with arguments and sending funds is:
address.func.value(amount)(arg1, arg2, arg3)
func
needs to have thepayable
modifier (for Solidity 0.4+).Completing @Edmund's answer with these important details, here's an example that compiles:
Also emphasizing what he wrote: