There are a few things that are a good reminder and can help minimize debugging. Here's an example and answer to the question.
contract Relay {
function bal() constant returns (uint) {
return this.balance;
}
function setRelay() payable {
//...
}
function() payable {
}
}
contract Bar {
uint public x = 0;
function foo(Relay contractRelay) {
x = 2;
contractRelay.setRelay.value(100)(); // this is how to send a fee along with setRelay
}
}
Reminder to add payable
modifier to setRelay
if you want it to get a fee.
contractRelay.setRelay.value(100)()
is the syntax for sending a fee as part of setRelay
. I changed the example so that setRelay
doesn't have arguments, to highlight that the last ()
is important to avoid debugging headaches.
If Bar
has less than 100 wei, than setRelay
will cause an exception and the transaction will be reverted (x
will remain 0).
Here are reminders if instead foo
just uses contractRelay.send(100)
:
The fallback function should have the payable
modifier.
If Bar
has less than 100 wei, then contractRelay.send(100)
will not cause an exception: x
will be set to 2. A general recommendation is to always check the return value of send
, and in this case
the following is probably desired:
if (!contractRelay.send(100)) {
throw;
}