Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Smart Contract programming 101 with Solidity #PizzaHackathon

1,945 views

Published on

Smart Contract programming 101 with Solidity training used in Pizza Hackathon #0 Bangkok.

Published in: Software

Smart Contract programming 101 with Solidity #PizzaHackathon

  1. 1. Training nuuneoi
  2. 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
  3. 3. Introduction to Blockchain nuuneoi
  4. 4. What is Blockchain? (Speedy Version) “The Decentralized Database”
  5. 5. What is Blockchain? (Speedy Version) “The Decentralized Database”
  6. 6. What is Blockchain? (Speedy Version) “The Decentralized Database”
  7. 7. What is Blockchain? (Speedy Version) “The Decentralized Database”
  8. 8. What is Blockchain? (Speedy Version) “The Decentralized Database”
  9. 9. What is Blockchain? (Speedy Version) “The Decentralized Database”
  10. 10. Introduction to Smart Contract Platform nuuneoi
  11. 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. 12. What is Smart Contract Platform? • Everyone hold the same source code in the decentralized way.
  13. 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. 14. What else Smart Contract could do? • Store the variables (States) • Transaction 1: a = 10 • Transaction 2: a = 20
  15. 15. Ethereum nuuneoi
  16. 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.
  17. 17. Creating a Wallet • Install MetaMask • Chrome plugin
  18. 18. Faucet • Get Ethereum for FREE! … (Sort of) • https://faucet.kovan.network/ (Github account required)
  19. 19. Creating another Wallet • Do it in MetaMask
  20. 20. Make the first transaction • Let’s transfer some Ethereum from first account to the second one. • Learn more a bit about Etherscan.
  21. 21. Smart Contract Programming with Solidity nuuneoi
  22. 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; } }
  23. 23. IDE: Remix • https://remix.ethereum.org/
  24. 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. 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
  26. 26. Deploying • Live Demo: Do it on remix
  27. 27. Working with Smart Contract through web3 • ABI • BYTECODE
  28. 28. Working with Smart Contract through web3 • ABI [ { "constant": false, "inputs": [ { "name": "newBalance", "type": "uint256" } ], "name": "setBalance", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "inputs": [], "payable": false, "stateMutability": "nonpayable", "type": "constructor" }, { "constant": true, "inputs": [], "name": "getBalance", "outputs": [ { "name": "", "type": "uint256" } ], "payable": false, "stateMutability": "view", "type": "function" } ] • BYTECODE 608060405234801561001057600080fd5b506103e860005560bf806100256000396000f3006 0806040526004361060485763ffffffff7c0100000000000000000000000000000000000000 00000000000000000060003504166312065fe08114604d578063fb1669ca146071575b60008 0fd5b348015605857600080fd5b50605f6088565b60408051918252519081900360200190f3 5b348015607c57600080fd5b506086600435608e565b005b60005490565b6000555600a1656 27a7a72305820cf77c0639acc98ef9acefd6386fe697d2d71d7478f3b9d459d8778557e3061 4a0029
  29. 29. Working with Smart Contract through web3 ABI
  30. 30. Working with Smart Contract through web3 • Live Coding: HTML + Javascript + jquery + web3.js + MetaMask • Boilerplate: https://goo.gl/jychkY
  31. 31. Testing $ npm install –g http-server $ http-server .
  32. 32. Understand Gas Price • See it on etherscan
  33. 33. Coding: Types • Live Coding: • bool • int, uint, int8, uint8, int16, uint16, …, int256, uint256 • address • fixed, ufixed • byte, bytes • string • struct • mapping • enum • []
  34. 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; } }
  35. 35. Coding: pure function function func(uint x, uint y) public pure returns (uint) { return x * (y + 42); }
  36. 36. Coding: view function uint a; function func() public view returns (uint) { return a; }
  37. 37. Coding: pure & view function through read fn uint a; function func() public view returns (uint) { return a; }
  38. 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. 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. 40. Coding: require function setBalance(uint newBalance) public { // Check if newBalance is in [0, 10000] require(newBalance <= 10000); // Set new balance balance = newBalance; }
  41. 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. 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. 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. 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. 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 });
  46. 46. Coding: payable function register() public payable { registeredAddresses[msg.sender] = true; }
  47. 47. Redeploying • Live Demo: See the difference [new contract address]. • Test the event.
  48. 48. Extras nuuneoi
  49. 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.
  50. 50. Truffle: Solidity Framework • Highly recommended • Testable code • Incremental deployment • etc.
  51. 51. Store file on IPFS • https://ipfs.io/ • Upload through Infura
  52. 52. Alternative Public Blockchain • EOSIO: Smart Contract • TomoChain: Smart Contract (Ethereum-Forked) • NEM: Smart Contract • Stellar: Payment (Non Turing Completed)
  53. 53. Create your own Smart Contract blockchain • Hyperledger Fabric • Use Golang to write Smart Contract

×