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
120
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
マニフェスト: 人類の知のフロンティアに向けた拡張的足場へ / Manifesto: Toward Expansive Scaffolding for Humanity's Knowledge Frontier
ks91
PRO
0
8
続・スマートコントラクトと分散ファイナンス / Smart Contracts and Decentralized Finance, Continued
ks91
PRO
0
42
スマートコントラクトと分散ファイナンス / Smart Contracts and Decentralized Finance
ks91
PRO
0
63
シン・ブロックチェーン / Truth of Blockchain
ks91
PRO
0
92
パスワード/パスフレーズと認証 / Password, Passphrase and Authentication
ks91
PRO
0
37
git と GitHub / git and GitHub
ks91
PRO
0
35
ソフトウェアの開発と保守 / Software Development and Maintenance
ks91
PRO
0
50
インターネットの特徴 / Features of the Internet
ks91
PRO
0
34
インターネットのガバナンス / Governance of the Internet
ks91
PRO
0
30
Other Decks in Technology
See All in Technology
Oracle Database@Google Cloud:サービス概要のご紹介
oracle4engineer
PRO
0
110
Delegating the chores of authenticating users to Keycloak
ahus1
0
140
React開発にStorybookとCopilotを導入して、爆速でUIを編集・確認する方法
yu_kod
1
270
20250707-AI活用の個人差を埋めるチームづくり
shnjtk
4
3.8k
Should Our Project Join the CNCF? (Japanese Recap)
whywaita
PRO
0
340
怖くない!はじめてのClaude Code
shinya337
0
390
成長し続けるアプリのためのテストと設計の関係、そして意思決定の記録。
sansantech
PRO
0
120
生成AI活用の組織格差を解消する 〜ビジネス職のCursor導入が開発効率に与えた好循環〜 / Closing the Organizational Gap in AI Adoption
upamune
7
5.2k
品質と速度の両立:生成AI時代の品質保証アプローチ
odasho
1
340
SEQUENCE object comparison - db tech showcase 2025 LT2
nori_shinoda
0
130
ネットワーク保護はどう変わるのか?re:Inforce 2025最新アップデート解説
tokushun
0
200
united airlines ™®️ USA Contact Numbers: Complete 2025 Support Guide
flyunitedhelp
1
300
Featured
See All Featured
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
6
300
Optimizing for Happiness
mojombo
379
70k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
Balancing Empowerment & Direction
lara
1
430
Designing for humans not robots
tammielis
253
25k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
Documentation Writing (for coders)
carmenintech
72
4.9k
YesSQL, Process and Tooling at Scale
rocio
173
14k
Done Done
chrislema
184
16k
The World Runs on Bad Software
bkeepers
PRO
69
11k
Art, The Web, and Tiny UX
lynnandtonic
299
21k
Scaling GitHub
holman
460
140k
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