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 実習 / Practicing Ethereum
Search
Kenji Saito
PRO
October 31, 2018
Technology
0
110
Ethereum 実習 / Practicing Ethereum
2018年10月31日(水)、ブロックチェーンハブ主催で開催されたブロックチェーンアカデミー「【ハンズオン】スマートコントラクトプログラミング(2) イーサリアム実習」にて使用したスライドです。
Kenji Saito
PRO
October 31, 2018
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
32
暗号資産の経済学・イントロダクション / 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
70
サイバー攻撃・防御とサイバー法 / Cyber Attacks, Defenses and Cyber Law
ks91
PRO
0
50
インシデントレスポンス演習I / Incident Response Exercise I
ks91
PRO
0
65
Other Decks in Technology
See All in Technology
Git 研修 Basic【MIXI 24新卒技術研修】
mixi_engineers
PRO
0
310
データ分析を支える技術 生成AI再入門
ishikawa_satoru
0
380
Flutter研修【MIXI 24新卒技術研修】
mixi_engineers
PRO
0
160
スタートアップにおける組織設計とスクラムの長期戦略 / Scrum Fest Kanazawa 2024
yoshikiiida
13
3.6k
[NIKKEI Tech Talk] KDDI/KAG Scrum & Community for Engineering Training
curanosuke
2
220
ここがすごいよ! AWS Systems Manager!
saichan11
0
1.8k
たくさん本を読んだけど 1年後には綺麗サッパリ!を乗り越えて 学習の鬼になるぞ👹
yum3
0
160
テストケースの自動生成に生成AIの導入を試みた話と生成AIによる今後の期待
shift_evolve
0
190
20240724_cm_odyssey_hibiyatech
hiashisan
0
110
Classmethod Odyssey 登壇資料
yamahiro
0
390
How to Think Like a Performance Engineer
csswizardry
4
590
初中級者用如何使用backlog -VALE TUDOEDITION-
in0u
0
140
Featured
See All Featured
4 Signs Your Business is Dying
shpigford
178
21k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
90
47k
The Art of Programming - Codeland 2020
erikaheidi
48
13k
ParisWeb 2013: Learning to Love: Crash Course in Emotional UX Design
dotmariusz
105
6.8k
5 minutes of I Can Smell Your CMS
philhawksworth
200
19k
Git: the NoSQL Database
bkeepers
PRO
423
64k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
277
13k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
226
52k
Unsuck your backbone
ammeep
666
57k
How GitHub (no longer) Works
holman
305
140k
What the flash - Photography Introduction
edds
65
11k
Music & Morning Musume
bryan
43
5.9k
Transcript
(2) CSO / SFC ( https://speakerdeck.com/ks91 )
[email protected]
— (2)
— 2018-10-31 – p.1/37
1. 2. 3. 4. 5. — (2) — 2018-10-31 –
p.2/37
1. : ERC20 2. : 3. : — (2) —
2018-10-31 – p.3/37
1. : ERC20 ERC20 — (2) — 2018-10-31 – p.4/37
BcH-smart-contract-programming.zip solidity 0.4.21 contracts tests ( ) — (2) —
2018-10-31 – p.5/37
Solidity JavaScript ( , ) (constructor) ( ) ( )
Ether — (2) — 2018-10-31 – p.6/37
pragma solidity ˆ0.4.24; contract MyToken { ( ) : (EVM
) : constructor (...) public { /* */ : } function MyToken(...) { /* ( ) */ : } : } constructor C (/* */ // ) — (2) — 2018-10-31 – p.7/37
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 ERC223 ( ), ERC721 (Non-Fungible) — (2) — 2018-10-31 – p.8/37
MyToken string public _name; string public _symbol; uint8 public _decimals;
mapping (address => uint256) public _balanceOf; uint256 _totalSupply; _name, _symbol _decimals : 2 100 1.00 mapping _balanceOf ‘_’ Solidity . . . — (2) — 2018-10-31 – p.9/37
MyToken event Transfer(address indexed from, address indexed to, uint256 value);
indexed (3 ) MyToken function Transfer() — (2) — 2018-10-31 – p.10/37
MyToken constructor (uint256 supply, string name, string symbol, uint8 decimals)
public { if (supply == 0) { supply = 1000000; /* supply 1,000,000 */ } _totalSupply = supply; _balanceOf[msg.sender] = supply; _name = name; _symbol = symbol; _decimals = decimals; } msg.sender supply — (2) — 2018-10-31 – p.11/37
MyToken balanceOf() function balanceOf(address tokenOwner) external view returns (uint256) {
return _balanceOf[tokenOwner]; } — (2) — 2018-10-31 – p.12/37
MyToken transfer() function transfer(address to, uint256 value) external returns (bool)
{ require(value <= _balanceOf[msg.sender]); /* */ require(_balanceOf[to] + value >= _balanceOf[to]); /* */ _balanceOf[msg.sender] -= value; _balanceOf[to] += value; Transfer(msg.sender, to, value); return true; } require (function ) OpenZeppelin https://openzeppelin.org — (2) — 2018-10-31 – p.13/37
(1) import pytest @pytest.fixture() def token_contract(chain): TokenFactory = chain.provider.get_contract_factory(’MyToken’) deploy_txid
= TokenFactory.deploy(args=[ 0, "BcH Coin", "BcH", 0, ]) contract_address = chain.wait.for_contract_address(deploy_txid) return TokenFactory(address=contract_address) populus — (2) — 2018-10-31 – p.14/37
(2) def test_my_token(token_contract, chain): account0 = chain.web3.eth.accounts[0] account1 = chain.web3.eth.accounts[1]
assert token_contract.call().balanceOf(account0) == 1000000 assert token_contract.call().balanceOf(account1) == 0 txid = token_contract.transact().transfer(account1, 10) chain.wait.for_receipt(txid) assert token_contract.call().balanceOf(account0) == 999990 assert token_contract.call().balanceOf(account1) == 10 account0 coinbase account1 account0 account1 10BcH — (2) — 2018-10-31 – p.15/37
$ py.test -k test_my_token.py — (2) — 2018-10-31 – p.16/37
2. — (2) — 2018-10-31 – p.17/37
(m2 ) 1 1 — (2) — 2018-10-31 – p.18/37
IndivisibleAsset string public _name; string public _symbol; uint256 public _quantity;
address public _owner; _name ( ) _symbol _quantity (m2 ) _owner — (2) — 2018-10-31 – p.19/37
IndivisibleAsset event Transfer(address indexed from, address indexed to); from to
— (2) — 2018-10-31 – p.20/37
IndivisibleAsset constructor (string name, string symbol, uint256 quantity) public {
_name = name; _symbol = symbol; _quantity = quantity; _owner = msg.sender; } — (2) — 2018-10-31 – p.21/37
IndivisibleAsset getOwner() function getOwner() external view returns (address) { return
(_owner); } — (2) — 2018-10-31 – p.22/37
IndivisibleAsset transfer() function transfer(address to) external returns (bool) { require(_owner
== msg.sender); _owner = to; Transfer(msg.sender, to); return true; } ( / ) $ populus compile — (2) — 2018-10-31 – p.23/37
(1) import pytest @pytest.fixture() def asset_contract(chain): AssetFactory = chain.provider.get_contract_factory(’IndivisibleAsset’) deploy_txid
= AssetFactory.deploy(args=[ "5322 Endo, Fujisawa", "mˆ2", 300, ]) contract_address = chain.wait.for_contract_address(deploy_txid) return AssetFactory(address=contract_address) SFC (300m2 ) — (2) — 2018-10-31 – p.24/37
(2) def test_indivisible_assset(asset_contract, chain): account0 = chain.web3.eth.accounts[0] account1 = chain.web3.eth.accounts[1]
assert asset_contract.call().getOwner() == account0 txid = asset_contract.transact().transfer(account1) chain.wait.for_receipt(txid) assert asset_contract.call().getOwner() == account1 account0 account1 — (2) — 2018-10-31 – p.25/37
$ py.test -k test_indivisible_asset.py — (2) — 2018-10-31 – p.26/37
3. — (2) — 2018-10-31 – p.27/37
transfer settle, retrieve asset, retrieve token 3 — (2) —
2018-10-31 – p.28/37
1. ( ) 2. ( ) 3. ( ) —
(2) — 2018-10-31 – p.29/37
OneTimeEscrow settle() function settle() public returns (bool) { require(_token.balanceOf(this) >=
_price); /* this */ require(_asset.getOwner() == address(this)); _token.transfer(_seller , _price); _asset.transfer(_buyer); Settled(); /* */ return true; } settle() transfer ( / ) $ populus compile solidity 0.4.21 — (2) — 2018-10-31 – p.30/37
(1) import pytest @pytest.fixture() def token_contract(chain): . . . @pytest.fixture()
def asset_contract(chain): . . . — (2) — 2018-10-31 – p.31/37
(2) def test_one_time_escrow(token_contract, asset_contract, chain): account0 = chain.web3.eth.accounts[0] account1 =
chain.web3.eth.accounts[1] txid = chain.web3.eth.sendTransaction({ ’from’: account0, ’to’: account1, ’value’: chain.web3.toWei(1, "ether") }) chain.wait.for_receipt(txid) txid = token_contract.transact().transfer(account1, 300) chain.wait.for_receipt(txid) account0 account1 1Ether 300BcH 300BcH TX — (2) — 2018-10-31 – p.32/37
(3) EscrowFactory = chain.provider.get_contract_factory(’OneTimeEscrow’) txid = EscrowFactory.deploy(args=[ token_contract.address, account1, asset_contract.address,
account0, 300, ]) contract_address = chain.wait.for_contract_address(txid) EscrowFactory.address = contract_address; account1 account0 300BcH — (2) — 2018-10-31 – p.33/37
(4) txid = token_contract.transact({ ’from’: account1 }).transfer(contract_address, 300) chain.wait.for_receipt(txid) txid
= asset_contract.transact().transfer(contract_address) chain.wait.for_receipt(txid) assert token_contract.call().balanceOf(account0) == 999700 assert token_contract.call().balanceOf(account1) == 0 assert token_contract.call().balanceOf(contract_address) == 300 assert asset_contract.call().getOwner() == contract_address account1 300BcH account0 — (2) — 2018-10-31 – p.34/37
(5) txid = EscrowFactory.transact().settle(); chain.wait.for_receipt(txid) assert token_contract.call().balanceOf(account0) == 1000000 assert
token_contract.call().balanceOf(account1) == 0 assert token_contract.call().balanceOf(contract_address) == 0 assert asset_contract.call().getOwner() == account1 settle() — (2) — 2018-10-31 – p.35/37
$ py.test -k test_one_time_escrow.py : settle() — (2) — 2018-10-31
– p.36/37
BBc-1 1. BBc-1 2. BBc-1 3. 4. Ethereum ( )
— (2) — 2018-10-31 – p.37/37