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
100
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
ゼミ紹介 : 公共の利益のためのデジタルトランスフォーメーション / Zemi Introduction : Digital Transformation for Public Good
ks91
PRO
0
26
強者の側にいたら『テクノロジーで不自由なく』ではない / If you side with the powerful, then it is not like people can live without any inconvenience because of the technology.
ks91
PRO
0
54
アカデミーキャンプ 2024冬「都の西北でAI(アイ) を叫ぶ(準備編)」DAY 2 / Academy Camp 2024 Winter "Shouting AI in the Northwest of the Capital" DAY 2
ks91
PRO
0
45
アカデミーキャンプ 2024冬「都の西北でAI(アイ) を叫ぶ(準備編)」DAY 1 / Academy Camp 2024 Winter "Shouting AI in the Northwest of the Capital" DAY 1
ks91
PRO
0
40
続・衣食住をテクノロジーで不自由なく / Technology Ensures Food, Clothing, and Housing
ks91
PRO
0
43
ブロックチェーン技術の来し方行く末 / The Coming and Going of Blockchain Technology
ks91
PRO
0
44
言うことを聞かないこどもたちになってもらうためのAI活用論 / Using AI to Prevent Children from Listening to Others
ks91
PRO
0
75
LaTeX と Overleaf によるショートペーパー作成 / Short Paper Writing with LaTeX and Overleaf
ks91
PRO
0
36
GPT-4 を用いたデータ分析 / Data Analysis Using GPT-4
ks91
PRO
0
51
Other Decks in Technology
See All in Technology
OCI Data Integration技術情報 / ocidi_technical_jp
oracle4engineer
PRO
1
1.5k
Elementaryを用いたデータ品質の可視化とデータ基盤の運用改善
10xinc
6
1.6k
やっていきテスト
k6s4i53rx
0
160
10分でわかるfreeeのQA
freee
0
260
OpenTelemetry実践 はじめの一歩
taxin
0
330
Building a RAG app to chat with your data (on Azure)
pamelafox
0
120
KubeCon EU: Unlocking new Platform Experiences with Open Interfaces
salaboy
1
380
社内共通ルールを値オブジェクトにして社内ライブラリとして運用してみた話
leveragestech
2
1.2k
暗黙知を集積するプラットフォーム : 「健常者エミュレータ事例集」の取り組み
sora32127
1
160
Autify Company Deck
autifyhq
1
30k
LLM + RAG を使った SORACOM Support Bot の裏側の歴史
soracom
PRO
1
640
TypeScript Quiz (Encraft #12 Frontend Quiz Night)
uhyo
6
720
Featured
See All Featured
GitHub's CSS Performance
jonrohan
1023
450k
Build The Right Thing And Hit Your Dates
maggiecrowley
23
1.9k
No one is an island. Learnings from fostering a developers community.
thoeni
14
2k
GraphQLとの向き合い方2022年版
quramy
28
12k
Imperfection Machines: The Place of Print at Facebook
scottboms
257
12k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
111
35k
What’s in a name? Adding method to the madness
productmarketing
PRO
14
2.6k
Keith and Marios Guide to Fast Websites
keithpitt
407
22k
Building an army of robots
kneath
300
41k
The Brand Is Dead. Long Live the Brand.
mthomps
48
22k
Ruby is Unlike a Banana
tanoku
95
10k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
11
1.4k
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