$30 off During Our Annual Pro Sale. View Details »

Solidity in 5 Minutes

U-Zyn Chua
November 26, 2016

Solidity in 5 Minutes

Creating Decentralized Apps on Ethereum Blockchain

Presented at JSConf.Asia 2016

Code snippets: https://bit.ly/SGDT-sol

U-Zyn Chua

November 26, 2016
Tweet

More Decks by U-Zyn Chua

Other Decks in Technology

Transcript

  1. Solidity in 5 Minutes
    Creating Decentralized Apps on Ethereum Blockchain
    U-Zyn Chua 

    @uzyn
    JSConf.Asia 2016

    View Slide

  2. @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

    View Slide

  3. @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.

    View Slide

  4. @uzyn
    Smart contract is 

    application logic

    with trustless execution

    View Slide

  5. @uzyn
    a single globally highly available 

    virtual machine that

    executes instructions.

    View Slide

  6. @uzyn
    is thus
    slow, inefficient and expensive

    View Slide

  7. @uzyn
    a price to pay for
    trustless decentralization

    View Slide

  8. @uzyn
    open execution
    open source

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  14. @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.

    View Slide

  15. @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.

    View Slide

  16. @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

    View Slide

  17. @uzyn
    Ready?

    Let’s try it out!

    View Slide

  18. @uzyn
    What shall we create?

    View Slide

  19. @uzyn
    How about...
    we print some cash

    View Slide

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

    View Slide

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

    View Slide

  22. @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

    View Slide

  23. @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

    View Slide

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

    View Slide

  25. @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

    View Slide

  26. @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

    View Slide

  27. @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

    View Slide

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

    View Slide

  29. @uzyn
    web3.js
    Ethereum JavaScript API
    Because Web 2.0 is so year 2000.
    Allows interaction with JS 

    (Node.JS and browser)

    View Slide

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

    View Slide

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

    View Slide

  32. @uzyn
    http://ethereum.github.io/browser-solidity
    Browser-solidity

    web-based solidity compiler
    Solidity Compiler
    Make sure
    it’s not
    HTTPS

    View Slide

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

    View Slide

  34. @uzyn
    Say hi!
    Twitter · GitHub

    uzyn

    View Slide