2

Here is the code confusing me:

function createRelay () returns (bool success) {
    relay = new Relay()
    bool success = Relay(relay).setRelay(someInt, someAddress);

    if (success) {
        msg.sender.send(fee);
    }
}

I'm trying to send a predetermined fee to the new instance of a Relay contract.

Who is msg.sender? How do I point to the Relay I want to send the fee to?

eth
  • 85,679
  • 53
  • 285
  • 406
Michael O'Rourke
  • 324
  • 2
  • 13
  • Related: http://ethereum.stackexchange.com/questions/9705/how-can-you-call-a-payable-function-in-another-contract-with-arguments-and-send – eth Jan 20 '17 at 02:23

2 Answers2

2

msg.sender is the account (external or another contract) that called createRelay().

If you want to send to your Relay, you write: relay.send(fee) or if you want it to be part of the setRelay call you do:

bool success = relay.setRelay.value(fee)(someInt, someAddress);
2

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
    }
}
  1. Reminder to add payable modifier to setRelay if you want it to get a fee.

  2. 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.

  3. 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;
}
eth
  • 85,679
  • 53
  • 285
  • 406