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

スマートコントラクトプログラミング / Smart Contract Programming

スマートコントラクトプログラミング / Smart Contract Programming

2022年10月31日(月)、慶應義塾大学大学院メディアデザイン研究科「サービスデザインプロジェクト B」2022秋で使用したスライドです。

Kenji Saito

October 31, 2022
Tweet

More Decks by Kenji Saito

Other Decks in Technology

Transcript

  1. Service Design, generated by Stable Diffusion B 2022 3 (

    ) B 2022 — 3 — 2022-10-31 – p.1/93
  2. https://speakerdeck.com/ks91 ( ) ( ) Discord Discord ( / /

    ) Zoom URL ( ) B 2022 — 3 — 2022-10-31 – p.2/93
  3. ( ) 1 10 17 • 2 10 24 •

    3 10 31 • 4 11 7 5 11 14 1 6 11 21 2 7 11 28 8 12 5 Discord B 2022 — 3 — 2022-10-31 – p.3/93
  4. – : : Ethereum ( ) Ethereum (1)(2)(3) ( ←

    ) B 2022 — 3 — 2022-10-31 – p.4/93
  5. ެ։୆ா ެ։Ͱͳ͚Ε͹ͳΒͳ͍ ʜ ຊਓ ɾૹۚه࿥ ɾίʔυ ɾσʔλͷมԽ ɾ࣮ߦϩά ݕӾͰ͖Δୈࡾऀ ͜Εɺ

    ه࿥͍ͨ͠Μ͚ͩͲ ͋ͳͨ͸ ॻ͍ͨΒμϝ ͜ͷ಺༰͸ ॻ͍ͨΒμϝ ॻ͍ͪΌͬͨͷʁ ͜͏ॻ͖׵͑·͠ΐ͏ ॻ͍ͯͳ͍ͷʁ ͜͏ॻ͔Εͨ͜ͱʹ ͠·͠ΐ͏ ॻ͍ͪΌͬͨͷʁ ফ͠·͠ΐ͏ %P4 ߈ܸͱ͔Ͱ ๦֐ͯ͠ࢭΊ·͠ΐ͏ B 2022 — 3 — 2022-10-31 – p.6/93
  6. ( ) ( ) ( ) ↓ ( ( ))

    (BP : Blockchain Property ) BP-1 : ( ) BP-2 : ( ( ) ) BP-3 : ( ) ( ) ( ) B 2022 — 3 — 2022-10-31 – p.7/93
  7. ( ) (LIFO : Last In, First Out) ( )

    B 2022 — 3 — 2022-10-31 – p.8/93
  8. ( ) ( ) ( ) ( ) ( 16

    ) SHA (Secure Hash Algorithm) ( ) 1bit B 2022 — 3 — 2022-10-31 – p.9/93
  9. ( ) ( ) < , > ( ) ←

    : < , > : : < , , > : OK ( 1 ) NG ( ) B 2022 — 3 — 2022-10-31 – p.10/93
  10. BP-1 ( ), BP-2 ( ) BBc-1 f : →

    BP-3 ( ) ( ) ( ) ← BP-2 ← BP-3 ( ) (BP-2 ) ( ) ← BP-3 (?) + ← BP-{1,2} B 2022 — 3 — 2022-10-31 – p.11/93
  11. [Lamport 1984][Schneider 1990] (1) (2) (3) (4) ( ) ←

    ↑ B 2022 — 3 — 2022-10-31 – p.12/93
  12. TX : transaction ( 4 TX ) SMRP (State Machine

    Replication Problem) SMRP-1 ( ) SMRP-2 : B 2022 — 3 — 2022-10-31 – p.13/93
  13. ( ) ( ) ϒϩοΫ൪߸O ϒϩοΫ൪߸O  ϒϩοΫ൪߸O  ϒϩοΫ൪߸O

     ϒϩοΫ൪߸O  ϒϩοΫ൪߸O  ϒϩοΫ൪߸O  ϒϩοΫ൪߸O  ͍ͪ͹Μه࿥ɾॻ͖׵͑ͷίετ͕ߴ͍ྺ࢙ ࠷΋վ͟Μ͠ʹ͍͘ྺ࢙ ͕༗ޮ ͻͱͭखલͷϒϩοΫͷ ʮμΠδΣετʯ σδλϧॺ໊͞Εͨ ʮτϥϯβΫγϣϯ औҾ ʯ ϒϩοΫΛ࡞ΔͨΊʹ͸ɺͦͷʮμΠδΣετʯ͕͋Δ਺ҎԼͱ͍͏৚݅Λຬͨͨ͠Γ 1SPPGPG8PSL Ծ૝௨՟ͷ࣋ͪ෼ʹԠͨ͡ݖརΛ࢖ͬͨ౤ථͰউͬͨΓ 1SPPGPG4UBLF ͢Δඞཁ͕͋Δ ίετ͕͔͔Δ ϒϩοΫΛ࡞Δਓ͸ͦͷதʹࣗ෼ʹѼͯͨ Ծ૝௨՟ͰͷใुΛॻ͖ࠐΉ͜ͱ͕Ͱ͖ɺ ͦͷϒϩοΫ͕༗ޮͳྺ࢙ͷதʹ͋Δ৔߹ʹ ݶΓใु΋༗ޮͱͳΔ τϥϯβΫγϣϯͷଘࡏΛ֬ೝ͢Δ ͨΊͷޮ཰తͳํ๏͕ఏڙ͞ΕΔ ਖ਼౰ੑ ଘࡏੑ །Ұੑ Proof of Work ( ) . . . → B 2022 — 3 — 2022-10-31 – p.14/93
  14. (BP-3 ) ( : ) (BP-3 ) ( : )

    ← NFT BP-{1,2,3} BP-{1, 2} ⇒ B 2022 — 3 — 2022-10-31 – p.15/93
  15. 2009 ↑ 13 ( 5 ) B 2022 — 3

    — 2022-10-31 – p.17/93
  16. ( ) ( ) ( ) B 2022 — 3

    — 2022-10-31 – p.19/93
  17. ( ) ( ) ( ) B 2022 — 3

    — 2022-10-31 – p.21/93
  18. / ( ) ( ) ( ) ( ) B

    2022 — 3 — 2022-10-31 – p.23/93
  19. ( ) ( ) ( ) B 2022 — 3

    — 2022-10-31 – p.24/93
  20. ( ) ( ) ( ) : https://doi.org/10.1016/j.icte.2021.08.012 ( )

    ( ) ( ) : https://ieeexplore.ieee.org/document/9686409 B 2022 — 3 — 2022-10-31 – p.31/93
  21. — τʔΫϯ ୅ସ՟ฎ εϚʔτίϯτϥΫτ ݕূՄೳͳঢ়ଶϚγϯ ϓϩϏφϯε དྷྺূ໌  ୅ସՄೳ ঈؐෆೳ

    ୅ସՄೳ ঈؐՄೳ ୅ସෆೳ ঈؐՄೳ ୅ସෆೳ ঈؐෆೳ ূ໌͢Δ ಉఆ͢Δ ࢧ෷͍౳ ࠷ॳͷԠ༻ *%Χʔυ౳ ηΩϡϦςΟ τʔΫϯ ূ݊ ౳ ηϯαʔ౳ ෺ྲྀɾ Ҩݴॻ౳ อݥਃ੥౳ ௥੻͢Δ ײ஌͢Δ தԝۜߦ σδλϧ௨՟౳ ҭ੒ήʔϜ౳ ൿີ伴Λ࢖͑Δ͔Βຊਓʁ ෛ࠴  Ξηοτͷ ਺஋දݱΛҠసͰ͖Δʁ ొ࿥͞Εͨίʔυͱ ͦͷ࣮ߦϩάͱ݁Ռͷ ਅਖ਼ੑΛ୲อͰ͖Δʁ ݖҖ͕  Կ͔ͷ಺༰Λূ໌͢Δʁ ࣋ଓ͢Δଘࡏʹ͍ͭͯ ه࿥ΛΞοϓσʔτՄʁ ओମ͕͍ͳ͘ͳͬͯ΋ աڈͷσʔλ͸༗ޮʁ /'5 Ұൠతͳ ೝࣝ ػೳͷ ෼ੳ݁Ռ B 2022 — 3 — 2022-10-31 – p.33/93
  22. Ethereum macOS (Monterey 12.6) + Homebrew Linux (Ubuntu 22.04) (x86/AMD64

    and ARM) Windows 10/11 + Windows Subsystem for Linux (Ubuntu ) Ethereum solidity brownie B 2022 — 3 — 2022-10-31 – p.34/93
  23. Solidity Ethereum ( ) http://solidity.readthedocs.io/en/latest/installing-solidity.html Linux (on Windows) “sudo apt”,

    macOS “brew” Linux ARM git clone dependencies solc 0.8.16 (9/6 ) $ solc --version brownie py-solc-x solc (dependencies) ( solc py-solc-x ) B 2022 — 3 — 2022-10-31 – p.35/93
  24. Python3 macOS $ brew install python3 Python 3.8 Homebrew $

    brew uninstall --ignore-dependencies [email protected] $ brew install [email protected] /usr/local/opt/[email protected]/bin/python3 Linux $ sudo apt update $ sudo apt install python3.8 python3.8-dev python3.8-venv python3.8-tk 16.10 python3(.8) apt (PPA ) python3 python3-dev python3-venv python3-tk B 2022 — 3 — 2022-10-31 – p.37/93
  25. macOS : . . . macOS wget brew dyld: Library

    not loaded: /usr/local/opt/gettext/lib/libintl.8.dylib $ brew uninstall --force gettext $ brew install gettext B 2022 — 3 — 2022-10-31 – p.38/93
  26. Python (venv) macOS (Linux python3 ) ‘python3.8’ ‘python3’ $ python3.8

    -m venv bbc1env $ source bbc1env/bin/activate (bbc1env) $ pip install -U pip (bbc1env) $ pip install wheel bbc1env ( BBc-1 ) ( ) (bbc1env) $ deactivate B 2022 — 3 — 2022-10-31 – p.39/93
  27. $ brownie init ( ERC-20 ) $ brownie bake token

    $ cd token B 2022 — 3 — 2022-10-31 – p.41/93
  28. contracts (macOS Linux (x86/AMD64) ) $ brownie compile Linux (ARM)

    (AMD64 ) $ python # >>> import solcx >>> solcx.compile_solc(’0.6.12’) # >>> quit() /tmp ∼/.solcx/solc-v0.6.12 ( ) $ pytest tests B 2022 — 3 — 2022-10-31 – p.42/93
  29. Brownie $ brownie console Python >>> len(accounts) 10 >>> accounts[0].balance()

    100000000000000000000 >>> quit() B 2022 — 3 — 2022-10-31 – p.43/93
  30. infura.io Brownie https://infura.io SIGN UP PROJECT ID infura Ethereum API

    infura . . . infura.io PROJECT ID WEB3_INFURA_PROJECT_ID PROJECT ID (∼/.bash_profile ) $ export WEB3_INFURA_PROJECT_ID= PROJECT ID B 2022 — 3 — 2022-10-31 – p.45/93
  31. G¨ orli ETH brownie console Görli >>> network.disconnect() >>> network.connect(’goerli’)

    >>> len(accounts) 0 >>> accounts.add() <LocalAccount object ’ 16 ’> >>> accounts[0].private_key 16 >>> quit() Görli Testnet ETH Faucet ETH https://goerlifaucet.com ( ) ( ) https://metamask.io/ B 2022 — 3 — 2022-10-31 – p.46/93
  32. G¨ orli ETH brownie console Görli ETH >>> network.disconnect() >>>

    network.connect(’goerli’) >>> len(accounts) 0 >>> accounts.add( 16 ) <LocalAccount object ’ 16 ’> >>> accounts[0].balace() ETH >>> quit() ETH >>> accounts[0].transfer(accounts[1], "0.01 ether") B 2022 — 3 — 2022-10-31 – p.47/93
  33. Ethereum Vitalik Buterin, “Ethereum White Paper: A NEXT GENERATION SMART

    CONTRACT & DECENTRALIZED APPLICATION PLATFORM” 12 (= ) = ⇒ → Dapps ( / / ) B 2022 — 3 — 2022-10-31 – p.49/93
  34. Ether Ethereum EOA : Externally-Owned Account ( ) Ether EVM

    EVM B 2022 — 3 — 2022-10-31 – p.54/93
  35. EVM : Ethereum Virtual Machine Gas ( EVM = (

    burn)) . . . B 2022 — 3 — 2022-10-31 – p.55/93
  36. EVM : : ← : Solidity — JavaScript LLL —

    Lisp Vyper — Python Fe — Vyper Rust ← NEW! Solidity Vyper Python B 2022 — 3 — 2022-10-31 – p.56/93
  37. Solidity ( ) pragma solidityˆ0.7.0; contract IndivisibleAsset { /* */

    string public _name; string public _symbol; uint256 public _quantity; address public _owner; constructor(string memory name, string memory symbol, uint256 quantity) public { _name = name; _symbol = symbol; _quantity = quantity; _owner = msg.sender; } function transfer(address to) public returns (bool) { require (_owner == msg.sender); _owner = to; return true; } } B 2022 — 3 — 2022-10-31 – p.57/93
  38. ( , ) (constructor) (deploy) = ( ) ( )

    ETH Trusted Actor Model B 2022 — 3 — 2022-10-31 – p.58/93
  39. (1) : ERC20 $ brownie bake token B 2022 —

    3 — 2022-10-31 – p.60/93
  40. Solidity JavaScript ( , ) (constructor) ( ) ( )

    Ether Ethereum B 2022 — 3 — 2022-10-31 – p.61/93
  41. pragma solidity ˆ0.6.0; /* 0.8.x */ contract Token { (

    ) : (EVM ) : constructor (...) public { /* */ : } function balanceOf(...) { /* ( ) */ : } : } constructor C (/* */ // ) B 2022 — 3 — 2022-10-31 – p.62/93
  42. ERC20 ERC (Ethereum Request for Comment) 20 https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md contract ERC20

    { function totalSupply() constant returns (uint totalSupply); function balanceOf(address _owner) constant returns (uint balance); function transfer(address _to, uint _value) returns (bool success); function transferFrom(address _from, address _to, uint _value) returns (bool success); function approve(address _spender, uint _value) returns (bool success); function allowance(address _owner, address _spender) constant returns (uint remaining); event Transfer(address indexed _from, address indexed _to, uint _value); event Approval(address indexed _owner, address indexed _spender, uint _value); } name/ , symbol/ , decimals/ approve allowance ERC-777 ( ) B 2022 — 3 — 2022-10-31 – p.63/93
  43. ERC20 ERC20 ( ) ⇒ ERC20 ( ERC20 ) ERC20

    ⇒ B 2022 — 3 — 2022-10-31 – p.64/93
  44. (fungible) ERC-20 → ERC-223 (draft) or ERC-777 ( )( )

    (non-fungible) ERC-721 ( ) ERC-1155 ( ) ERC-3525 (SLOT ) B 2022 — 3 — 2022-10-31 – p.65/93
  45. Token ( ) string public symbol; string public name; uint256

    public decimals; uint256 public totalSupply; mapping(address => uint256) balances; . . . name, symbol decimals : 2 100 1.00 mapping balances B 2022 — 3 — 2022-10-31 – p.66/93
  46. Token ( ) event Transfer(address from, address to, uint256 value);

    Token function emit Transfer() ( ) B 2022 — 3 — 2022-10-31 – p.67/93
  47. Token ( ) constructor( string memory _symbol, string memory _name,

    uint256 _decimals, uint256 _totalSupply ) public { symbol = _symbol; name = _name; decimals = _decimals; totalSupply = _totalSupply; balances[msg.sender] = _totalSupply; } msg.sender _totalSupply B 2022 — 3 — 2022-10-31 – p.68/93
  48. Token balanceOf() function balanceOf(address _owner) public view returns (uint256) {

    return balances[_owner]; } B 2022 — 3 — 2022-10-31 – p.69/93
  49. Token transfer() function transfer(address _to, uint256 _value) public returns (bool)

    { balances[msg.sender] = balances[msg.sender].sub(_value); balances[_to] = balances[_to].add(_value); emit Transfer(msg.sender, _to, _value); return true; } ( ↑ ) OpenZeppelin https://openzeppelin.org B 2022 — 3 — 2022-10-31 – p.70/93
  50. GitHub git clone $ git clone https://github.com/ks91/sample-smart-contracts.git sample-smart-contracts contracts, scripts

    tests token Token Solidity 0.5 0.6/0.7 (Token ) Solidity B 2022 — 3 — 2022-10-31 – p.72/93
  51. IndivisibleAsset string public _name_; string public _symbol_; uint256 public _quantity_;

    address public _owner_; _name_ ( ) _symbol_ _quantity_ (m2 ) _owner_ ‘_’ Solidity . . . B 2022 — 3 — 2022-10-31 – p.74/93
  52. IndivisibleAsset constructor (string name, string symbol, uint256 quantity) public {

    _name_ = name; _symbol_ = symbol; _quantity_ = quantity; _owner_ = msg.sender; } B 2022 — 3 — 2022-10-31 – p.76/93
  53. IndivisibleAsset transfer() function transfer(address to) public returns (bool) { require(_owner_

    == msg.sender); _owner_ = to; emit Transfer(msg.sender, to); return true; } require (function ) ( ) $ brownie compile B 2022 — 3 — 2022-10-31 – p.78/93
  54. scripts/indivisible asset.py from brownie import * def main(): accounts[0].deploy(IndivisibleAsset, "5322

    Endo, Fujisawa", "mˆ2", 300) SFC (300m2 ) ( ) B 2022 — 3 — 2022-10-31 – p.79/93
  55. from brownie import * import pytest def test_owner_and_transfer(IndivisibleAsset): asset =

    accounts[0].deploy(IndivisibleAsset, "5322 Endo", "mˆ2", 300) assert asset.getOwner() == accounts[0] asset.transfer(accounts[1], {’from’: accounts[0]}) assert asset.getOwner() == accounts[1] try: asset.transfer(accounts[0], {’from’: accounts[0]}) done = 1 except: done = 0 assert done == 0 accounts[0] accounts[1] B 2022 — 3 — 2022-10-31 – p.80/93
  56. 1. ( ) 2. ( ) 3. ( ) B

    2022 — 3 — 2022-10-31 – p.84/93
  57. OneTimeEscrow settle() function settle() public returns (bool) { require(_token_.balanceOf(address(this)) >=

    _price_); /* this */ require(_asset_.getOwner() == address(this)); _token_.transfer(_seller_ , _price_); _asset_.transfer(_buyer_); emit Settled(); /* */ return true; } settle() transfer ( / ) $ brownie compile B 2022 — 3 — 2022-10-31 – p.85/93
  58. (1) from brownie import * import pytest def test_deploy_and_settle(Token, IndivisibleAsset,

    OneTimeEscrow): asset = accounts[0].deploy(IndivisibleAsset, "5322 Endo", "mˆ2", 300) token = accounts[0].deploy(Token, "Test Token", "TEST", 18, "1000 ether") B 2022 — 3 — 2022-10-31 – p.86/93
  59. (2) token.transfer(accounts[1], 300, {’from’: accounts[0]}) escrow = accounts[0].deploy(OneTimeEscrow, token, accounts[1],

    asset, accounts[0], 300) accounts[0] accounts[1] 300 300 TX accounts[1] accounts[0] 300 bake Token 300 ETH wei B 2022 — 3 — 2022-10-31 – p.87/93
  60. (3) token.transfer(escrow, 300, {’from’: accounts[1]}) asset.transfer(escrow, {’from’: accounts[0]}) assert token.balanceOf(accounts[0])

    == 999999999999999999700 assert token.balanceOf(accounts[1]) == 0 assert token.balanceOf(escrow) == 300 assert asset.getOwner() == escrow accounts[1] ( ) 300 accounts[0] ( ) B 2022 — 3 — 2022-10-31 – p.88/93
  61. (4) escrow.settle({’from’: accounts[0]}) assert token.balanceOf(accounts[0]) == 1000000000000000000000 assert token.balanceOf(accounts[1]) ==

    0 assert token.balanceOf(escrow) == 0 assert asset.getOwner() == accounts[1] settle() accounts[0] OK B 2022 — 3 — 2022-10-31 – p.89/93
  62. 1. (1) (2) Ethereum 2022 11 5 ( ) 23:59

    JST B 2022 — 3 — 2022-10-31 – p.92/93