Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to Tezos Smart Contracts

arvidj
January 19, 2021

Introduction to Tezos Smart Contracts

In this video we will demonstrate the smart contract programming capabilities of the Tezos blockchain. We introduce Michelson, the programming language embedded in Tezos, and illustrate it using a smart contract implementing a vote on the block chain.

arvidj

January 19, 2021
Tweet

Other Decks in Programming

Transcript

  1. Introduction to Tezos Smart Contracts Vote contract Conclusion Introduction to

    Tezos Smart Contracts Tezos EPFL Webinars Arvid Jakobsson ([email protected]) Nomadic Labs training May 15, 2020 1 / 11
  2. Introduction to Tezos Smart Contracts Vote contract Conclusion Introduction to

    Tezos Smart Contracts Tezos blockchain: list of blocks containing operations Operations: smart contract originations, transactions Block1 op1 op2 · · · opn Block2 op1 op2 · · · opn Context Adr1 → ꜩ4 Adr2 → ꜩ1 Adr3 → ꜩ3 · · · 2 / 11
  3. Introduction to Tezos Smart Contracts Vote contract Conclusion Introduction to

    Tezos Smart Contracts Tezos blockchain: list of blocks containing operations Operations: smart contract originations, transactions Block1 op1 op2 · · · opn Block2 originate contract Adr4 <initial_balance> <initial_storage> <script> · · · Context Adr1 → ꜩ4 Adr2 → ꜩ1 Adr3 → ꜩ3 Adr4 → balance: <initial_balance> storage: <initial_storage> script: <script> 2 / 11
  4. Introduction to Tezos Smart Contracts Vote contract Conclusion Introduction to

    Tezos Smart Contracts Tezos blockchain: list of blocks containing operations Operations: smart contract originations, transactions Block2 originate contract Adr4 <initial_balance> <initial_storage> <script> · · · Block3 transaction from Adr1 to Adr4 amount 3ꜩ parameter Unit <new_ops> · · · <new_ops>, <new_storage> Context Adr1 → ꜩ4 - ꜩ3 Adr2 → ꜩ1 Adr3 → ꜩ3 Adr4 → balance: <initial_balance> + ꜩ3 storage: <new_storage> script: <script> 2 / 11
  5. Introduction to Tezos Smart Contracts Vote contract Conclusion Structure of

    a Michelson contract A Michelson contract script: parameter <parameter_ty>; storage <storage_ty>; code { <contract_code> }; 3 / 11
  6. Introduction to Tezos Smart Contracts Vote contract Conclusion Structure of

    a Michelson contract A Michelson contract script: parameter <parameter_ty>; storage <storage_ty>; code { <contract_code> }; The signature of <contract_code>: pair <parameter_ty> <storage_ty> : [] -> pair (list operation) <storage_ty> : [] 3 / 11
  7. Introduction to Tezos Smart Contracts Vote contract Conclusion Michelson Types

    Base types: unit : Unit bool : True, False nat : 0, 1, 2, ... int : ..., -2, -1, 0, 1, ... string : "Apples", "Bananas", … … Compound types: option 'a : Some v, None list 'a : {}, {x1; ...; xn} map 'a 'b : {}, {Elt k1 v1 ; ... ; Elt kn vn} Domain-specific types: mutez : 0, 1, … operation No literal values 4 / 11
  8. Introduction to Tezos Smart Contracts Vote contract Conclusion Michelson Stack-based

    Execution Michelson is a stack machine: Instructions consume/produce a fixed number (0, 1, …) of elements from/to the stack. Extends to sequences of instructions. 5 / 11
  9. Introduction to Tezos Smart Contracts Vote contract Conclusion Michelson Stack-based

    Execution Michelson is a stack machine: Instructions consume/produce a fixed number (0, 1, …) of elements from/to the stack. Extends to sequences of instructions. PUSH nat 1; PUSH nat 2; PUSH nat 3; ADD; ADD 5 / 11
  10. Introduction to Tezos Smart Contracts Vote contract Conclusion Michelson Stack-based

    Execution Michelson is a stack machine: Instructions consume/produce a fixed number (0, 1, …) of elements from/to the stack. Extends to sequences of instructions. ⇓ PUSH nat 1; PUSH nat 2; PUSH nat 3; ADD; ADD ⇒ 1 : nat 5 / 11
  11. Introduction to Tezos Smart Contracts Vote contract Conclusion Michelson Stack-based

    Execution Michelson is a stack machine: Instructions consume/produce a fixed number (0, 1, …) of elements from/to the stack. Extends to sequences of instructions. ⇓ PUSH nat 1; PUSH nat 2; PUSH nat 3; ADD; ADD ⇒ 2 : nat 1 : nat 1 : nat 5 / 11
  12. Introduction to Tezos Smart Contracts Vote contract Conclusion Michelson Stack-based

    Execution Michelson is a stack machine: Instructions consume/produce a fixed number (0, 1, …) of elements from/to the stack. Extends to sequences of instructions. ⇓ PUSH nat 1; PUSH nat 2; PUSH nat 3; ADD; ADD 3 : nat 2 : nat ⇒ 2 : nat 1 : nat 1 : nat 5 / 11
  13. Introduction to Tezos Smart Contracts Vote contract Conclusion Michelson Stack-based

    Execution Michelson is a stack machine: Instructions consume/produce a fixed number (0, 1, …) of elements from/to the stack. Extends to sequences of instructions. ⇓ PUSH nat 1; PUSH nat 2; PUSH nat 3; ADD; ADD 3 : nat 2 : nat ⇒ 5 : nat 1 : nat 1 : nat 5 / 11
  14. Introduction to Tezos Smart Contracts Vote contract Conclusion Michelson Stack-based

    Execution Michelson is a stack machine: Instructions consume/produce a fixed number (0, 1, …) of elements from/to the stack. Extends to sequences of instructions. ⇓ PUSH nat 1; PUSH nat 2; PUSH nat 3; ADD; ADD 5 : nat ⇒ 1 : nat 6 : nat 5 / 11
  15. Introduction to Tezos Smart Contracts Vote contract Conclusion Example Contract:

    vote vote lets users vote for a specific candidate for ꜩ5 Votes are stored in a mapping: string → nat Voting fees are collected 6 / 11
  16. Introduction to Tezos Smart Contracts Vote contract Conclusion Example Contract:

    vote vote lets users vote for a specific candidate for ꜩ5 Votes are stored in a mapping: string → nat Voting fees are collected Block2 originate contract vote ꜩ0 { Elt "Apples" 0 ; Elt "Bananas" 0 } <vote script> · · · Block3 transaction from Adr1 to vote amount 5ꜩ parameter "Bananas" transaction from vote to tz1... amount 5ꜩ parameter Unit · · · [transfer 5 to tz1 ... ], { Elt "Apples" 0 ; Elt "Bananas" 1} Context Adr1 → ꜩ6 - ꜩ5 Adr2 → ꜩ1 Adr3 → ꜩ3 Adr4 → balance: ꜩ0 + ꜩ5 storage: { Elt "Apples" 0 ; Elt "Bananas" 1 } script: <script> 6 / 11
  17. Introduction to Tezos Smart Contracts Vote contract Conclusion Example Contract:

    vote vote lets users vote for a specific candidate for ꜩ5 Votes are stored in a mapping: string → nat Voting fees are collected Block2 originate contract vote ꜩ0 { Elt "Apples" 0 ; Elt "Bananas" 0 } <vote script> · · · Block3 transaction from Adr1 to vote amount 3ꜩ parameter "Bananas" · · · Error: Insufficient funds Context Adr1 → ꜩ6 - ꜩ3 Adr2 → ꜩ1 Adr3 → ꜩ3 Adr4 → balance: ꜩ0 + ꜩ3 storage: { Elt "Apples" 0 ; Elt "Bananas" 0 } script: <script> 6 / 11
  18. Introduction to Tezos Smart Contracts Vote contract Conclusion Example Contract:

    vote vote lets users vote for a specific candidate for ꜩ5 Votes are stored in a mapping: string → nat Voting fees are collected Block2 originate contract vote ꜩ0 { Elt "Apples" 0 ; Elt "Bananas" 0 } <vote script> · · · Block3 transaction from Adr1 to vote amount 5ꜩ parameter "Pears" · · · Error: Ineligable candidate Context Adr1 → ꜩ6 - ꜩ5 Adr2 → ꜩ1 Adr3 → ꜩ3 Adr4 → balance: ꜩ0 + ꜩ5 storage: { Elt "Apples" 0 ; Elt "Bananas" 0 } script: <script> 6 / 11
  19. Introduction to Tezos Smart Contracts Vote contract Conclusion Necessary Michelson

    instructions Stack manipulation: PUSH, DUP, DIP {code} Data manipulation: CAR, CDR, GET, UPDATE, SOME, ADD, UNIT, NIL, CONS Paywall: AMOUNT, COMPARE, LE Domain specific: BALANCE, IMPLICIT_ACCOUNT, TRANSFER_TOKENS Control flow: IF { bt } { bf } IF_NONE { bt } { bf } FAILWITH 7 / 11
  20. Introduction to Tezos Smart Contracts Vote contract Conclusion Full Contract

    Code of vote storage (map string nat); parameter string; code { # Paywall AMOUNT; PUSH mutez 5000000; COMPARE; LE; IF { } {FAILWITH}; # Verify and update vote DUP; DIP {CDR; DUP}; CAR; DUP; DIP { GET; IF_NONE { PUSH string "Ineligible candidate"; FAILWITH} {}; PUSH nat 1; ADD; SOME}; UPDATE; # Collect fees and setup stack for exit NIL operation; PUSH key_hash "tz1..."; IMPLICIT_ACCOUNT; BALANCE; UNIT; TRANSFER_TOKENS; CONS; PAIR } 8 / 11
  21. Introduction to Tezos Smart Contracts Vote contract Conclusion Deploying and

    Testing the Contract Deploying the contract: $ tezos-client originate contract vote ... Reading the contract’s storage: $ tezos-client get contract storage for vote Making a transaction to the contract: $ tezos-client transfer 4 from adr1 to vote \ --arg '"Bananas"' 9 / 11
  22. Introduction to Tezos Smart Contracts Vote contract Conclusion Going Further

    Michelson editor plugins & tools VS Code: https://gitlab.com/kinokasai/vscode-michelson IntelliJ: https://www.plugin-dev.com/plugins/tezos-michelson Vim: https://github.com/rnestler/michelson.vim Emacs: https://gitlab.com/tezos/tezos/tree/master/emacs Try Michelson: https://try-michelson.com Documentation Michelson whitepaper: https://tezos.gitlab.io/whitedoc/michelson.html Interactive language reference: https://michelson.nomadic-labs.com Michelson tutorial: https://gitlab.com/camlcase-dev/michelson-tutorial High-level languages: SmartPy, LIGO, Archetype, SCaml… 10 / 11
  23. Introduction to Tezos Smart Contracts Vote contract Conclusion Extra slide:

    Commands Deploying the contract: $ tezos-client originate contract vote \ transferring 0 from bootstrap1 \ running vote.tz \ --init '{ Elt "Apples" 0 ; Elt "Bananas" 1}' \ --burn-cap 0.6 Baking in sandbox $ tezos-client bake for bootstrap1 Reading the contract’s storage: $ tezos-client get contract storage for vote Making a transaction to the contract: $ tezos-client transfer 4 from adr1 to vote \ --arg '"Bananas"' 11 / 11