Slide 1

Slide 1 text

Solidity in 5 Minutes Creating Decentralized Apps on Ethereum Blockchain U-Zyn Chua 
 @uzyn JSConf.Asia 2016

Slide 2

Slide 2 text

@uzyn Smart contracts are computer protocols that facilitate, verify, or enforce the negotiation or performance of a contract, or that make a contractual clause unnecessary. @uzyn

Slide 3

Slide 3 text

@uzyn Smart contracts are computer protocols that facilitate, verify, or enforce the negotiation or performance of a contract, or that make a contractual clause unnecessary.

Slide 4

Slide 4 text

@uzyn Smart contract is 
 application logic
 with trustless execution

Slide 5

Slide 5 text

@uzyn a single globally highly available 
 virtual machine that
 executes instructions.

Slide 6

Slide 6 text

@uzyn is thus slow, inefficient and expensive

Slide 7

Slide 7 text

@uzyn a price to pay for trustless decentralization

Slide 8

Slide 8 text

@uzyn open execution open source

Slide 9

Slide 9 text

@uzyn high level JavaScript-like language that compiles to Ethereum VM machine code Solidity

Slide 10

Slide 10 text

@uzyn Solidity actual contract code of Singapore-based DigixDAO ABI Bytecode

Slide 11

Slide 11 text

@uzyn Price to pay for carrying out operation and storage. Gas

Slide 12

Slide 12 text

@uzyn Solidity Basics based on http://solidity.readthedocs.io/

Slide 13

Slide 13 text

@uzyn are stored permanently on EVM. Data types Boolean bool Integer int / uint (unsigned) Ethereum address address Bytes and String bytes / string State variables

Slide 14

Slide 14 text

@uzyn mapping (address => uint) likes; function like(address _friend) public { likes[_friend]++; } Functions and Modifiers modifier onlyOwner { if (msg.sender != owner) throw; _; } function makeAdmin(_candidate) public onlyOwner { admin = _candidate; } The rest of the function goes here Looks pretty much like a JS function.

Slide 15

Slide 15 text

@uzyn Function Visibilities 1.public a. Visible externally and interally b. Automatically creates accessor. 2.private a. Only visible in current contract Remember, on blockchain, all data is publicly viewable! also internal and external.

Slide 16

Slide 16 text

@uzyn Arrays, Mappings & Structs // mapping - non-iterable (recommended) mapping (address => uint) likes; // array - iterable address[] public friends; // struct struct Person { string name; address wallet; uint lastSeen; } Hash Table

Slide 17

Slide 17 text

@uzyn Ready?
 Let’s try it out!

Slide 18

Slide 18 text

@uzyn What shall we create?

Slide 19

Slide 19 text

@uzyn How about... we print some cash

Slide 20

Slide 20 text

@uzyn Code is available at http://bit.ly/SGDT-sol

Slide 21

Slide 21 text

@uzyn SGD Token (SGDT) Shh… Don’t tell them 1 SGDT == 1 SGD, always.

Slide 22

Slide 22 text

@uzyn Roles 1. Issuer a. Able to issue SGDT, after putting up equivalent SGD collateral. b. SGDT <==> SGD exchanger & guarantor. 2. User a. Able to freely transact SGDT. 3.Authority a. Grant license to issuers. http://bit.ly/SGDT-sol

Slide 23

Slide 23 text

@uzyn pragma solidity ^0.4.2; contract SGDT { /* Public variables of the token */ uint8 public decimals = 2; uint public totalSupply = 0; address public authority; /* This creates an array with all balances */ mapping (address => uint) public balanceOf; mapping (address => uint) public issuedBy; mapping (address => bool) public isIssuer; } Define state variables http://bit.ly/SGDT-sol

Slide 24

Slide 24 text

@uzyn Modifiers modifier onlyIssuer { if (isIssuer[msg.sender] != true) { throw; } _; } modifier onlyAuthority { if (msg.sender != authority) { throw; } _; } http://bit.ly/SGDT-sol

Slide 25

Slide 25 text

@uzyn Events event Transfer(address from, address to, uint value); event TokenIssued(address by); http://bit.ly/SGDT-sol Notify your JS daemon 
 on contract updates

Slide 26

Slide 26 text

@uzyn Issuer functions function issue(uint _value) public onlyIssuer { issuedBy[msg.sender] += _value; balanceOf[msg.sender] += _value; TokenIssued(msg.sender, _value); // Event } Can you work on the redeem() function? http://bit.ly/SGDT-sol

Slide 27

Slide 27 text

@uzyn User functions // Transfer SGDT function transfer(address _to, uint _value) public { if (balanceOf[msg.sender] < _value) { throw; // Check if the sender has enough } balanceOf[msg.sender] -= _value; // Subtract from the sender balanceOf[_to] += _value; // Add the same to the recipient // Notify anyone listening that this transfer took place Transfer(msg.sender, _to, _value); } http://bit.ly/SGDT-sol

Slide 28

Slide 28 text

@uzyn Authority functions function addIssuer(address _newIssuer) public onlyAuthority { isIssuer[_newIssuer] = true; } Can you add removeIssuer() function? http://bit.ly/SGDT-sol

Slide 29

Slide 29 text

@uzyn web3.js Ethereum JavaScript API Because Web 2.0 is so year 2000. Allows interaction with JS 
 (Node.JS and browser)

Slide 30

Slide 30 text

@uzyn Interface with Smart Contract Ethereum node web3.js Frontend JS Node.JS

Slide 31

Slide 31 text

@uzyn Ethereum node web3.js Frontend JS Node.JS Dapp Dapp! Interface with Smart Contract

Slide 32

Slide 32 text

@uzyn http://ethereum.github.io/browser-solidity Browser-solidity
 web-based solidity compiler Solidity Compiler Make sure it’s not HTTPS

Slide 33

Slide 33 text

@uzyn Smart Contract & JS code more examples available at http://bit.ly/SGDT-sol

Slide 34

Slide 34 text

@uzyn Say hi! Twitter · GitHub
 uzyn