Database (Shared Ledger)
Node Application
Consensus Algorithm
Public (Permissionless)
Private (Central)
Consortium (Central Group)
Blockchain based distributed computing platform.
Personal Accounts
Contract Accounts
Tokens (ICOs)... ERC20
Asset titles... ERC721
Digital Identity
Enter
Applications are built, distributed and ran as a contract on the blockchain
We write these applications using the Solidity programming language
// Our version pragma
pragma solidity ^0.4.6;
// Define our contract.
contract HelloWorld {
// Single simple method.
function displayMessage() constant returns (string m) {
return "Hello from a smart contract";
}
}
Compile, using the solc compiler
Deploy, by sending a transaction to the network
Execute, by calling the contract with from a client program
Solc
Web3.js
Ganache (TestRPC)
We will call our lotto
Yolanda.sol
pragma solidity ^0.4.6;
contract Yolanda {
// All our contracts code lives here...
}
Lets add some variables to keep track of the lotto.
mapping(address => uint) usersBet;
mapping(uint => address) users;
uint nbUsers = 0;
uint totalBets = 0;
address owner;
Next we will define our contructor
function Yolanda() public {
// Register the owner of the contract to who created it.
owner = msg.sender;
}
We are cooking
We will define 3 methods:
bet() - Which will allow us to send ether to it
getJackpot() - Which will return the total value of jackpot
endLottery() - Which will only be callable from the owner and will select a winner.
bet()
function bet() public payable {
if (msg.value > 0) {
if (usersBet[msg.sender] == 0) {
users[nbUsers] = msg.sender;
nbUsers += 1;
}
usersBet[msg.sender] += msg.value;
totalBets += msg.value;
}
}
getJackpot()
function getJackpot() public view returns (uint t) {
return this.balance;
}
endLottery()
function endLottery() public {
if (msg.sender == owner) {
uint sum = 0;
uint winningNumber = uint(block.blockhash(block.number-1)) % totalBets + 1;
for (uint i = 0; i < nbUsers; i++) {
sum += usersBet[users[i]];
if (sum >= winningNumber) {
selfdestruct(users[i]);
return;
}
}
}
}
Some interesting contract addresses on the MainNet:
Thank you!
Twitter: @direct