smart-contract-intro / contract-payments

Contract Payments

Now the next couple of things we need to do is add the ability accept payments to our vending machine, store it and put the item in the buyers inventory.

Let's start with the payment. To do this we can introduce a mapping. A Mapping in Solidity acts like a hash table or dictionary in any other language. These are used to store the data in the form of key-value pairs. Most commonly it is used to assign unique addresses with certain value types. It is like a phone book, we look up the name and it has a phone number assigned to it.

We can define a mapping using the mapping keyword, as such

// if we want to assign different strings an address (0xaddress)
mapping (string => address) nameToAddress;
// if we want to assign names to different numbers
mapping (uint => string) phoneNumberToName;

In our instance we want to be able to load up the vending machine with money and associate the amount loaded into the machine with the user who paid. Since we are using Ethereum we know that the user will have a unique address so we can store the balances according to their address.

This would be an example of a mapping that maps addresses to integers.

Instructions

Add a public mapping called balances to the contract. Then add the following function to the contract.
function addMoney (uint _amount) public payable {
}

Now note this function is payable. Any function in Solidity with the modifier payable ensures that the function can send and receive Ether. It can process transactions with non-zero Ether values and rejects any transactions with a zero Ether value.

smart-contract-intro / contract-payments

Contract Payments

Now the next couple of things we need to do is add the ability accept payments to our vending machine, store it and put the item in the buyers inventory.

Let's start with the payment. To do this we can introduce a mapping. A Mapping in Solidity acts like a hash table or dictionary in any other language. These are used to store the data in the form of key-value pairs. Most commonly it is used to assign unique addresses with certain value types. It is like a phone book, we look up the name and it has a phone number assigned to it.

We can define a mapping using the mapping keyword, as such

// if we want to assign different strings an address (0xaddress)
mapping (string => address) nameToAddress;
// if we want to assign names to different numbers
mapping (uint => string) phoneNumberToName;

In our instance we want to be able to load up the vending machine with money and associate the amount loaded into the machine with the user who paid. Since we are using Ethereum we know that the user will have a unique address so we can store the balances according to their address.

This would be an example of a mapping that maps addresses to integers.

Instructions

Add a public mapping called balances to the contract. Then add the following function to the contract.
function addMoney (uint _amount) public payable {
}

Now note this function is payable. Any function in Solidity with the modifier payable ensures that the function can send and receive Ether. It can process transactions with non-zero Ether values and rejects any transactions with a zero Ether value.

Loading...