2

In the Eris Industries tutorial, they present a banking and fund management contract system.

The FundManager controls the bank contract. I don't understand the following line from the deposit function used to check if the function call was successful:

bool success = Bank(bank).deposit.value(msg.value)(msg.sender);

Why Bank(bank) and what is it? The FundManager has an address bank attribute, instantiated by the constructor.

So, essentially, my question is:

Bank(bank): Get a reference object of type `Bank` at address `bank`
Bank(bank).deposit: call the `deposit` function on that object
Bank(bank).deposit.value(msg.value): What is happening here and what is returned?
Bank(bank).deposit.value(msg.value)(msg.sender): What is happening here?
4

The FundManager constructor creates a new Bank contract object...

bank = new Bank();

...and stores its address in this variable:

// This holds a reference to the current bank contract.
    address bank;

When the FundManager's deposit function is called, it calls the Bank contract using its address, as explained in this previous thread:

Bank(bank)...

In full, the call is:

bool success = Bank(bank).deposit.value(msg.value)(msg.sender);

Where the deposit function in the Bank contract is defined as:

// This will take the value of the transaction and add to the senders account.
function deposit(address customer) returns (bool res)

The customer address parameter equates to the msg.sender passed by the call in FundManager.

Edit:

Bank(bank).deposit.value(msg.value): What is happening here and what is returned?

I don't think this is a valid call by itself. It'd probably complain about a missing argument.

Bank(bank).deposit.value(msg.value)(msg.sender): What is happening here?

Bank(bank).deposit() is an external function call: it's calling a function in a different contract.

The parameter for the deposit function in the second contract is address customer, so we'd expect the call in the first contract to be:

Bank(bank).deposit(msg.sender)

...where msg.sender equates to address customer. So far so clear.

However, for external function calls, if we want the second contract to know the original value of msg.value from the first contract, then we must explicitly pass it. The reason being (from this page in the Solidity docs (part-way down)):

The values of all members of msg, including msg.sender and msg.value can change for every external function call. This includes calls to library functions.

To explicitly pass the value we need a value(msg.value) in there as well, which by convention goes before the actual parameter of the function we're calling:

          +------------------------------------+
          |                                    |
          |                                    V
Bank(bank).deposit    .value(msg.value)      (msg.sender)
                            ^
                            |
                  shove it in the middle
  • Helps, but I still dont understand the second half of the call. Adjusted the question. – TMOTTM Jul 15 '16 at 17:47
  • I updated the answer with more details. Let me know if it's still not clear. – Richard Horrocks Jul 18 '16 at 13:17
  • Sort of, but that's me.. Is there a reason not to just define the signature as deposit(address customer, uint value) and then to call it using Bank(bank).deposit(msg.sender, value_to_deposit)? – TMOTTM Jul 18 '16 at 16:41
  • msg.value is a special variable which you wouldn't normally need to pass around - you'd just call it from within the contract. When making an external call to a different contract, we need to pass it as a parameter. (The second contract may have a different value of the special variable.) They've perhaps implemented it this way to make the signatures for deposit() in both contracts match, or to show different features of the language. – Richard Horrocks Jul 18 '16 at 16:48
  • But no - I can't see any real reason for not implementing with two arguments. – Richard Horrocks Jul 18 '16 at 16:50

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.