Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Ethereum Exercise I
Search
Kenji Saito
PRO
August 30, 2023
Technology
0
91
Ethereum Exercise I
2023年8月30日(水)、ブロックチェーンハブ主催のブロックチェーンアカデミーにて使用したスライドです。
Kenji Saito
PRO
August 30, 2023
Tweet
Share
More Decks by Kenji Saito
See All by Kenji Saito
コバヤシマルテスト ─ 勝ち目のないゲームに抗いながら旅する心の旅路 / Kobayashi Maru Test - a mental journey of defiance against a game that cannot be won
ks91
PRO
0
3
セキュリティ戦略・発表と総括 / Security Strategies, Presentation and Conclusions
ks91
PRO
1
31
暗号資産の経済学・イントロダクション / Economics of Cryptocurrencies (Introduction)
ks91
PRO
0
20
AI とデジタルトランスフォーメーション / AI and Digital Transformation
ks91
PRO
0
10
インシデントレスポンス演習 II / Incident Response Exercise II
ks91
PRO
0
36
A思考の自動化とX 思考の行方 / Automation of A Thinking and the Future of X Thinking
ks91
PRO
0
23
サイバー戦の現在と未来 / Present and Future of Cyber Warfare
ks91
PRO
0
69
サイバー攻撃・防御とサイバー法 / Cyber Attacks, Defenses and Cyber Law
ks91
PRO
0
50
インシデントレスポンス演習I / Incident Response Exercise I
ks91
PRO
0
63
Other Decks in Technology
See All in Technology
VPoEの視点から見た、ヘンリーがサーバーサイドKotlinを使う理由 / Why Server-side Kotlin 2024
cho0o0
1
420
CEL(Common Expression Language)で書いた条件にマッチしたIAM Policyを見つける / iam-policy-finder
fujiwara3
0
710
Azure Pipelinesを使用したCICDベースラインアーキテクチャ実践
yuriemori
0
190
20240717_イケコパ代表Copilot_in_Teams会社でこう使ってます
ponponmikankan
2
430
目標設定は好きですか? アジャイルとともに目標と向き合い続ける方法 / Do you like target Management?
kakehashi
10
3k
楽しくGoを学び合う、LayerXの勉強会文化 / LayerX's study culture of having fun and learning Go together
ar_tama
2
350
年間一億円削減した時系列データベースのアーキテクチャ改善~不確実性の高いプロジェクトへの挑戦~
lycorptech_jp
PRO
3
2.9k
データ分析基盤を作ってみよう~設計編~
nrinetcom
PRO
1
110
たくさん本を読んだけど 1年後には綺麗サッパリ!を乗り越えて 学習の鬼になるぞ👹
yum3
0
160
開発生産性をむしろ向上させる セキュリティパートナーの作り方 / Dev Productivity Con 2024
flatt_security
0
360
Classmethod Odyssey 登壇資料
yamahiro
0
390
20240724_cm_odyssey_hibiyatech
hiashisan
0
110
Featured
See All Featured
Keith and Marios Guide to Fast Websites
keithpitt
408
22k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
17
1.5k
[RailsConf 2023] Rails as a piece of cake
palkan
35
4.4k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
353
29k
Git: the NoSQL Database
bkeepers
PRO
423
64k
How to Ace a Technical Interview
jacobian
274
23k
The Illustrated Children's Guide to Kubernetes
chrisshort
39
47k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
26
1.6k
The Cult of Friendly URLs
andyhume
75
5.9k
Optimizing for Happiness
mojombo
373
69k
Web development in the modern age
philhawksworth
203
10k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
13
430
Transcript
2 Ethereum I CSO /
[email protected]
— 2 Ethereum I
— 2023-08-30 – p.1/37
https://speakerdeck.com/ks91 — 2 Ethereum I — 2023-08-30 – p.2/37
( ) SFC ( ) CSO (Chief Science Officer) 1993
( ) 2006 ( ) SFC 22 P2P (Peer-to-Peer) 2011 ( ) 2018 2019 VR 2021.9 & VR 2022.3 2023 AI VR&RPG 2023.5 “Don’t Be So Serious” VOXEL 2023.7 DAZE 2023 → ( ) — 2 Ethereum I — 2023-08-30 – p.3/37
ERC-20 2 ERC-20 — 2 Ethereum I — 2023-08-30 –
p.4/37
1. : ERC-20 2. : 3. : — 2 Ethereum
I — 2023-08-30 – p.5/37
1. : ERC-20 $ brownie bake token — 2 Ethereum
I — 2023-08-30 – p.6/37
Solidity JavaScript ( , ) (constructor) ( ) ( )
Ether Ethereum — 2 Ethereum I — 2023-08-30 – p.7/37
pragma solidity ˆ0.6.0; /* 0.8.x */ contract Token { (
) : (EVM ) : constructor (...) public { /* */ : } function balanceOf(...) { /* ( ) */ : } : } constructor C (/* */ // ) — 2 Ethereum I — 2023-08-30 – p.8/37
ERC-20 ERC (Ethereum Request for Comments) EIP (Ethereum Improvement Proposals)
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 — 2 Ethereum I — 2023-08-30 – p.9/37
ERC-20 ERC-20 ( ) ⇒ ERC-20 ( ERC-20 ) ERC-20
⇒ — 2 Ethereum I — 2023-08-30 – p.10/37
(fungible) ERC-20 → ERC-223 (draft) or ERC-777 ( )( )
(non-fungible) ERC-721 ( ) ERC-1155 ( ) ERC-3525 (SLOT ) — 2 Ethereum I — 2023-08-30 – p.11/37
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 — 2 Ethereum I — 2023-08-30 – p.12/37
Token ( ) event Transfer(address from, address to, uint256 value);
Token function emit Transfer() ( ) — 2 Ethereum I — 2023-08-30 – p.13/37
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 — 2 Ethereum I — 2023-08-30 – p.14/37
Token balanceOf() function balanceOf(address _owner) public view returns (uint256) {
return balances[_owner]; } — 2 Ethereum I — 2023-08-30 – p.15/37
Token transfer() function transfer(address _to, uint256 _value) public returns (bool)
{ require(balances[msg.sender] >= _value, "Insufficient balance"); balances[msg.sender] = balances[msg.sender].sub(_value); balances[_to] = balances[_to].add(_value); emit Transfer(msg.sender, _to, _value); return true; } require (function ) ( ↑ ) OpenZeppelin https://openzeppelin.org — 2 Ethereum I — 2023-08-30 – p.16/37
2. — 2 Ethereum I — 2023-08-30 – p.17/37
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 ) Token Solidity 0.5 0.6 Solidity — 2 Ethereum I — 2023-08-30 – p.18/37
(m2 ) 1 1 — 2 Ethereum I — 2023-08-30
– p.19/37
IndivisibleAsset string public _name_; string public _symbol_; uint256 public _quantity_;
address public _owner_; _name_ ( ) _symbol_ _quantity_ (m2 ) _owner_ ‘_’ Solidity . . . — 2 Ethereum I — 2023-08-30 – p.20/37
IndivisibleAsset event Transfer(address indexed from, address indexed to); from to
indexed — 2 Ethereum I — 2023-08-30 – p.21/37
IndivisibleAsset constructor (string name, string symbol, uint256 quantity) public {
_name_ = name; _symbol_ = symbol; _quantity_ = quantity; _owner_ = msg.sender; } — 2 Ethereum I — 2023-08-30 – p.22/37
IndivisibleAsset getOwner() function getOwner() public view returns (address) { return
(_owner_); } — 2 Ethereum I — 2023-08-30 – p.23/37
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 — 2 Ethereum I — 2023-08-30 – p.24/37
scripts/indivisible asset.py from brownie import * def main(): accounts[0].deploy(IndivisibleAsset, "5322
Endo, Fujisawa", "mˆ2", 300) SFC (300m2 ) ( ) — 2 Ethereum I — 2023-08-30 – p.25/37
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] — 2 Ethereum I — 2023-08-30 – p.26/37
$ pytest tests/test_indivisible_asset.py — 2 Ethereum I — 2023-08-30 –
p.27/37
3. — 2 Ethereum I — 2023-08-30 – p.28/37
transfer settle, retrieve asset, retrieve token 3 — 2 Ethereum
I — 2023-08-30 – p.29/37
1. ( ) 2. ( ) 3. ( ) —
2 Ethereum I — 2023-08-30 – p.30/37
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 — 2 Ethereum I — 2023-08-30 – p.31/37
(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") — 2 Ethereum I — 2023-08-30 – p.32/37
(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 — 2 Ethereum I — 2023-08-30 – p.33/37
(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] ( ) — 2 Ethereum I — 2023-08-30 – p.34/37
(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 — 2 Ethereum I — 2023-08-30 – p.35/37
$ pytest tests/test_one_time_escrow.py : settle() — 2 Ethereum I —
2023-08-30 – p.36/37
3 Ethereum II : — 2 Ethereum I — 2023-08-30
– p.37/37