Subscribe to get access to this video

and the whole library of videos, sample code, and tutorials.

Contracts calling other contracts

Our bank has the following withdraw function:

  // Bank.sol

  // This is wrong and an anti-example. Don't ever do this
  function worstWithdraw() {
    msg.sender.call.value(balances[msg.sender])();
    balances[msg.sender] = 0;
  }

In the first line of this withdraw function, there are a lot of implicit Ethereum runtime features being used. Let’s unpack them.

Remember that msg.sender is the address of the account calling this transaction. Also balances is our Bank‘s internal accounting that keeps track of the amount of deposits credited to each address. So far, so good.

But what about .call.value and the extra parenthesis at the end? What’s going on here?

Similar to .send and .transfer

In one sense, this is similar to calling .send or .transfer.

Remember from the video sending funds from a contract, that we can send funds from a contract to another address by using someAddress.send() like this:

if(!someAddress.send(amount)) {
  // handle the failure here
  // by reverting necessary changes
}

And recall that .transfer is just a high-level version of .send that will revert changes if .send fails. That is, .transfer is equivalent to:

if (!x.send(y)) revert();

We often think of there being a human at the receiving end of a send, but in Ethereum this doesn’t have to be the case! That is, contracts can send funds to other contracts and contracts can call out to invoke other contracts.

 

Subscribe to get the full text of this lesson

and the entire library of videos, sample code, and tutorials.