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

Introduction to Solidity

U-Zyn Chua
November 15, 2016

Introduction to Solidity

Introduction to programming smart contract on Ethereum with Solidity.

Presented at a sharing session at GovTech on 15 November 2016.

Companion code can be downloaded at http://bit.ly/SGDT-sol

U-Zyn Chua

November 15, 2016
Tweet

More Decks by U-Zyn Chua

Other Decks in Technology

Transcript

  1. Intro to Solidity
    Smart Contract on Blockchain
    U-Zyn Chua
    @uzyn

    View Slide

  2. Smart contract is computer protocol that
    facilitate, verify, or enforce the negotiation or
    performance of a contract, or that make a
    contractual clause unnecessary.

    View Slide

  3. View Slide

  4. Smart contract is
    application logic layer
    with trustless execution

    View Slide

  5. Further explanations will be done in the
    “Trust Me I’m an Engineer”
    fashion – for engineers from an engineer

    View Slide

  6. a single globally highly available
    virtual machine that
    executes instructions.

    View Slide

  7. is thus
    slow, inefficient and expensive

    View Slide

  8. a price to pay for
    trustless decentralization

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  14. 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. Function Visibilities
    1. public
    a. Visible externally and interally
    b. Automatically creates accessor.
    2. private
    a. Only visible in current contract
    3. internal
    a. Similar to private, but callable by child contracts.
    4. external
    a. Similar to public, different low level handling.
    Remember, on
    blockchain, all data is
    publicly viewable!

    View Slide

  16. 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;
    }

    View Slide

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

    View Slide

  18. Ready?
    Let’s try it out!

    View Slide

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

    View Slide

  20. What shall we create?

    View Slide

  21. How about...
    we print some cash

    View Slide

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

    View Slide

  23. Roles
    1. Issuer
    a. Able to issue SGDT.
    b. SGDT <==> SGD exchanger & guarantor.
    2. User
    a. Able to freely transact SGDT.
    3. Authority
    a. Grant license to issuers.

    View Slide

  24. pragma solidity ^0.4.2;
    contract SGDT {
    /* Public variables of the token */
    string public name = 'Singapore Dollar Token';
    string public symbol = 'SGDT';
    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

    View Slide

  25. Events and Modifiers
    event Transfer(address from, address to, uint value);
    event TokenIssued(address by);
    modifier onlyIssuer {
    if (isIssuer[msg.sender] != true) {
    throw;
    }
    _;
    }
    modifier onlyAuthority {
    if (msg.sender != authority) {
    throw;
    }
    _;
    }

    View Slide

  26. Issuer functions
    function issue(uint _value) public onlyIssuer {
    issuedBy[msg.sender] += _value;
    balanceOf[msg.sender] += _value;
    TokenIssued(msg.sender, _value);
    }
    Can you work on the redeem() function?

    View Slide

  27. User functions
    /* Send coins */
    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
    Transfer(msg.sender, _to, _value); // Notify anyone listening that this
    // transfer took place
    }

    View Slide

  28. Authority functions
    function addIssuer(address _newIssuer) public onlyAuthority {
    isIssuer[_newIssuer] = true;
    }
    Can you add removeIssuer() function?

    View Slide

  29. Contract is available
    at
    http://bit.ly/SGDT-sol

    View Slide

  30. Deploy and Test
    Quick test via NodeJS console
    1. mkdir sgdt
    2. cd sgdt
    3. yarn add web3
    or npm install web3
    4. node
    const Web3 = require('web3');
    const web3 = new Web3(
    new Web3.providers.HttpProvider(
    'http://localhost:8545 '
    )
    );
    > web3
    > web3.eth
    > web3.eth.accounts

    View Slide

  31. Deploy and Test
    Instantiate contract
    const address = 'ADDRESS';
    const abi = ABI_OBJECT;
    const SGDT = web3.eth.contract(abi).at(address);
    > SGDT;
    > SGDT.authority(); // Read some static values
    > SGDT.totalSupply(); // BigNumber.js Object
    > SGDT.totalSupply.toNumber();

    View Slide

  32. Deploy and Test
    const authority = SGDT.authority();
    SGDT.isIssuer(web3.eth.accounts[1]); // false
    SGDT.addIssuer(web3.eth.accounts[1], {
    from: authority,
    gas: 1000000
    });
    SGDT.isIssuer(web3.eth.accounts[1]); // true
    Let’s do some magic!

    View Slide

  33. SGDT.balanceOf(web3.eth.accounts[1]).toNumber(); // 0
    SGDT.issue(1000, {
    from: web3.eth.accounts[1],
    gas: 1000000
    });
    SGDT.balanceOf(web3.eth.accounts[1]).toNumber(); // 1000 ($10.00)
    SGDT.balanceOf(web3.eth.accounts[2]).toNumber(); // 0
    SGDT.transfer(web3.eth.accounts[2], 499, {
    from: web3.eth.accounts[1],
    gas: 1000000,
    });
    SGDT.balanceOf(web3.eth.accounts[2]).toNumber(); // 499 ($4.99)
    SGDT.balanceOf(web3.eth.accounts[1]).toNumber(); // 501 ($5.01)
    Print some cash and transact

    View Slide

  34. Feel free to play around with other commands.
    Questions?
    Thank you.

    View Slide