2. Agenda
• Introduction to Blockchain
• Introduction to Smart Contract Platform
• Smart Contract 101
• Coding with Solidity
• Deploy to Ethereum Network
• Work with Ethereum’s Smart Contract though web3
• Extras
11. What is Smart Contract Platform?
• Since Blockchain is the decentralized database. How about we let it keep the
“executable code” inside instead than just a simple transaction?
12. What is Smart Contract Platform?
• Everyone hold the same source code in the decentralized way.
13. How the code is executed?
• Same as the way transaction is made in Blockchain 1.0 we make a
transaction to call the code instead of making a money transferring tx.
• We call it “Blockchain 2.0”
14. What else Smart Contract could do?
• Store the variables (States)
• Transaction 1: a = 10
• Transaction 2: a = 20
16. Ethereum Networks
Main Net
Network ID: 1
Consensus: PoW
Ropsten
Network ID: 3
Consensus: PoW
Client: Cross-Client
Rinkeby
Network ID: 4
Consensus: PoA
Client: Geth
Kovan
Network ID: 42
Consensus: PoA
Client: Parity
• Since the Ethereum is open sourced. There could be unlimited number of
networks out there and each of them are run separately.
22. Ethereum Smart Contract
pragma solidity ^0.4.18;
contract SimpleContract {
uint balance;
constructor() public {
// Set initial balance as 1000
balance = 1000;
}
function setBalance(uint newBalance) public {
// Cap balance to be [0, 10000]
require(newBalance <= 10000);
// Set new balance
balance = newBalance;
}
function getBalance() public view returns(uint) {
return balance;
}
}
24. Solidity Helloworld
pragma solidity ^0.4.18;
contract SimpleContract {
uint balance;
constructor() public {
// Set initial balance as 1000
balance = 1000;
}
function setBalance(uint newBalance) public {
// Set new balance
balance = newBalance;
}
function getBalance() public view returns(uint) {
return balance;
}
}
25. Coding: Constructor
pragma solidity ^0.4.18;
contract SimpleContract {
uint balance;
constructor() public {
// Set initial balance as 1000
balance = 1000;
}
function setBalance(uint newBalance) public {
// Set new balance
balance = newBalance;
}
function getBalance() public view returns(uint) {
return balance;
}
}
constructor will be called
only once when deployed
34. Coding: Inheritance
• “is” contract A {
uint a;
function getA() public view returns(uint) {
return a;
}
}
contract B is A {
uint b;
function getBsumA() public view returns(uint) {
return b + a;
}
}
37. Coding: pure & view function through read fn
uint a;
function func() public view returns (uint) {
return a;
}
38. Coding: pure & view function through read fn
uint a;
function func() public view returns (uint) {
return a;
}
function func2() public view returns (uint) {
return func();
}
39. Coding: pure & view function through write fn
uint a;
uint b;
function func() public view returns (uint) {
return a;
}
function func3(uint someVal) public returns (uint) {
b = someVal;
return func() + b;
}
40. Coding: require
function setBalance(uint newBalance) public {
// Check if newBalance is in [0, 10000]
require(newBalance <= 10000);
// Set new balance
balance = newBalance;
}
41. Coding: Visibility
• public – Can be either called internally or via messages.
• private – Only visible for the contract they are defined and
not in the derived contract.
• internal – Can only be accessed internally (i.e. from within
the current contract or contracts deriving from it)
• external - Can be called from other contracts and via
transactions. Cannot be called internally.
42. Coding: modifier
contract Ownable {
address public owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
function Ownable() public {
owner = msg.sender;
}
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
function transferOwnership(address newOwner) public onlyOwner {
require(newOwner != address(0));
emit OwnershipTransferred(owner, newOwner);
owner = newOwner;
}
}
https://goo.gl/ZLThph
43. Coding: modifier
pragma solidity ^0.4.18;
contract SimpleContract is Ownable {
uint balance;
constructor() public {
// Set initial balance as 1000
balance = 1000;
}
function setBalance(uint newBalance) public onlyOwner {
// Set new balance
balance = newBalance;
}
function getBalance() public view returns(uint) {
return balance;
}
}
contract Ownable {
address public owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
function Ownable() public {
owner = msg.sender;
}
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
function transferOwnership(address newOwner) public onlyOwner {
require(newOwner != address(0));
emit OwnershipTransferred(owner, newOwner);
owner = newOwner;
}
}
https://goo.gl/ZLThph
44. Coding: Events
pragma solidity ^0.4.18;
contract SimpleContract is Ownable {
event BalancedUpdated(uint balance);
uint balance;
constructor() public {
// Set initial balance as 1000
balance = 1000;
}
function setBalance(uint newBalance) public onlyOwner {
// Set new balance
balance = newBalance;
// Fire Event
emit BalanceUpdated(newBalance);
}
function getBalance() public view returns(uint) {
return balance;
}
}
45. Coding: Events
pragma solidity ^0.4.18;
contract SimpleContract is Ownable {
event BalancedUpdated(uint balance);
uint balance;
constructor() public {
// Set initial balance as 1000
balance = 1000;
}
function setBalance(uint newBalance) public onlyOwner {
// Set new balance
balance = newBalance;
// Fire Event
emit BalanceUpdated(newBalance);
}
function getBalance() public view returns(uint) {
return balance;
}
}
var event = SimpleContract.BalanceUpdated();
event.watch(function(error, result) {
// Will be called once there is new event emittted
});
49. What is ERC-20?
// https://github.com/ethereum/EIPs/issues/20
interface ERC20Interface {
function totalSupply() public view returns (uint supply);
function balanceOf(address _owner) public view returns (uint balance);
function transfer(address _to, uint _value) public returns (bool success);
function transferFrom(address _from, address _to, uint _value) public returns (bool success);
function approve(address _spender, uint _value) public returns (bool success);
function allowance(address _owner, address _spender) public view returns (uint remaining);
function decimals() public view returns(uint digits);
event Approval(address indexed _owner, address indexed _spender, uint _value);
}
• ERC-20 is just a contract implemented the interface defined by ERC-20
standard.