Sending funds from a contract
We want a contract to send funds to another address.
Before we get started, I want to point out that we're entering a danger zone. What's the worst thing that can happen to your smart contracts? The worst thing is for it to have a bug that causes people to lose money in unintended ways.
Because sending funds is such a major part of smart contracts, Solidity and Ethereum have a number of ways to send value around. The problem is this flexibility means that bugs can be introduced in subtle ways.
Since this is the first time we've talked about contracts sending funds, I want to focus on the high-level ideas. But do not release code in production that handles real funds until we've had time to talk through security pitfalls. For example, we haven't talked about re-entrancy yet, nor have we discussed fallback functions or contracts calling other contracts.
We will. But I want to make the point that this video teaches you enough Solidity to be dangerous and we need to talk more about the implications of contracts sending funds before you release a production app.
Okay, with that out of the way, let's go back to our bank example.
In this contract, we accepted deposits, but we didn't keep track of them, and the only way to get the money back out was to destroy the contract and send the money to the contract-owner.
Let's fix this.
We'll start by adding a new property to the bank called
balances which will be a mapping that keeps track of how much ether was sent by which address.
Think of it as keeping track of different bank accounts.