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
コードや知識を組み込む / Incorporating Codes and Knowledge
ks91
PRO
0
46
シリアスゲームとしての RPG / RPGs as Serious Games
ks91
PRO
0
27
"September 12th" ゲームのプロンプトの構造 / "September 12th" Game Prompt Structure
ks91
PRO
0
22
ワールドカフェI /チューターを改良する / World Café I and Improving the Tutors
ks91
PRO
0
130
自然言語の扱いと翻訳のためのプロンプト / Natural Language Handling and Prompts for Translation
ks91
PRO
0
31
研究って何だっけ / What is Research?
ks91
PRO
0
29
ブロックチェーンと分散ファイナンス概論 / Introduction to Blockchain and Decentralized Finance
ks91
PRO
0
16
大規模言語モデルの原理と使いこなしの原則 / Principles of Large Language Models and Their Use
ks91
PRO
0
30
講師研究紹介 / Research Introduction of the Lecturer
ks91
PRO
0
41
Other Decks in Technology
See All in Technology
QA/SDETの現在と、これからの挑戦
imtnd
0
140
AI Agentを「期待通り」に動かすために:設計アプローチの模索と現在地
kworkdev
PRO
2
470
LiteXとオレオレCPUで作る自作SoC奮闘記
msyksphinz
0
740
今日からはじめるプラットフォームエンジニアリング
jacopen
7
1.4k
「経験の点」の位置を意識したキャリア形成 / Career development with an awareness of the “point of experience” position
pauli
4
110
Running JavaScript within Ruby
hmsk
3
350
Porting PicoRuby to Another Microcontroller: ESP32
yuuu
4
450
白金鉱業Meetup_Vol.18_AIエージェント時代のUI/UX設計
brainpadpr
1
180
サーバレス、コンテナ、データベース特化型機能をご紹介。CloudWatch をもっと使いこなそう!
o11yfes2023
0
180
4/17/25 - CIJUG - Java Meets AI: Build LLM-Powered Apps with LangChain4j (part 2)
edeandrea
PRO
0
120
アセスメントで紐解く、10Xのデータマネジメントの軌跡
10xinc
1
450
Bazel for Ruby (RubyKaigi 2025)
p0deje
0
110
Featured
See All Featured
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
26k
GitHub's CSS Performance
jonrohan
1030
460k
The Language of Interfaces
destraynor
157
25k
Adopting Sorbet at Scale
ufuk
76
9.3k
Bootstrapping a Software Product
garrettdimon
PRO
307
110k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
34
2.2k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
135
33k
RailsConf 2023
tenderlove
30
1.1k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.5k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
30
2k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.4k
A designer walks into a library…
pauljervisheath
205
24k
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