222
12 Cryptocurrencies
In order to illustrate the use of smart contracts let us consider a pub that accepts
ether as payment for drinks. The publican simply displays a QR-code to the customer
who has an Ethereum wallet on his smartphone. The QR-code shows the price of
the drink and the address of a smart contract to which the payment is directed. This
publican is very law abiding and maintains a smart contract that splits the incoming
payments into a tax account and a business account. The source code of this smart
contract, written in the solidity programming language, is the following.
pragma solidity ˆ0.5.17;
contract TaxCollector {
uint16 drinks;
constructor() public {
drinks=0x0;
}
function drinks_total() view public returns (uint16) {
return drinks;
}
function () external payable {
address payable business=0x3a..;
address payable tax=0xF2..;
uint256 t=2*msg.value/10;
tax.transfer(t);
business.transfer(msg.value-t);
drinks += 1;
}
}
The pragma directive specifies which version of the compiler to use and inside
contract definition we first define a global variable drinks, that will count the
total number of drinks ever sold. As a global variable it is stored on the blockchain
and updates every time the contract is executed. The constructor is executed
when the contract is stored on the blockchain and initiates the variable drinks
to zero. The function drinks_total allows us to query how many drinks have
been sold so far. The last function has no name; it is called the fallback function.
Moreover, it has the attribute payable which allows it to receive funds from the
customer who bought the drink and initiated the transaction. The received amount is
accessible in the variable msg.value. Inside the function the addresses of the tax
and business accounts are hard-coded. Then the tax is calculated and stored in the
variable t, before it is sent to the tax account and the remainder sent to the business
account. Note that this contract is only illustrative, but can serve as a starting point
for further explorations using the test environment from Appendix B.12. On the other
hand, it should not be deployed on the main Ethereum blockchain.
Once the publican has written the smart contract, she compiles it to bytecode
using a solidity compiler, signs the bytecode with her private key, and the sends
it to a reserved address from where it is scheduled for inclusion on the blockchain.
Henceforth, her smart contract will be accessible under an address that is given to the
12 Cryptocurrencies
In order to illustrate the use of smart contracts let us consider a pub that accepts
ether as payment for drinks. The publican simply displays a QR-code to the customer
who has an Ethereum wallet on his smartphone. The QR-code shows the price of
the drink and the address of a smart contract to which the payment is directed. This
publican is very law abiding and maintains a smart contract that splits the incoming
payments into a tax account and a business account. The source code of this smart
contract, written in the solidity programming language, is the following.
pragma solidity ˆ0.5.17;
contract TaxCollector {
uint16 drinks;
constructor() public {
drinks=0x0;
}
function drinks_total() view public returns (uint16) {
return drinks;
}
function () external payable {
address payable business=0x3a..;
address payable tax=0xF2..;
uint256 t=2*msg.value/10;
tax.transfer(t);
business.transfer(msg.value-t);
drinks += 1;
}
}
The pragma directive specifies which version of the compiler to use and inside
contract definition we first define a global variable drinks, that will count the
total number of drinks ever sold. As a global variable it is stored on the blockchain
and updates every time the contract is executed. The constructor is executed
when the contract is stored on the blockchain and initiates the variable drinks
to zero. The function drinks_total allows us to query how many drinks have
been sold so far. The last function has no name; it is called the fallback function.
Moreover, it has the attribute payable which allows it to receive funds from the
customer who bought the drink and initiated the transaction. The received amount is
accessible in the variable msg.value. Inside the function the addresses of the tax
and business accounts are hard-coded. Then the tax is calculated and stored in the
variable t, before it is sent to the tax account and the remainder sent to the business
account. Note that this contract is only illustrative, but can serve as a starting point
for further explorations using the test environment from Appendix B.12. On the other
hand, it should not be deployed on the main Ethereum blockchain.
Once the publican has written the smart contract, she compiles it to bytecode
using a solidity compiler, signs the bytecode with her private key, and the sends
it to a reserved address from where it is scheduled for inclusion on the blockchain.
Henceforth, her smart contract will be accessible under an address that is given to the
