1. Smart contracts on Ethereum
Getting started
DappsMedia Tomoaki Sato
June 2015
Main source: https://github.com/ethereum/wiki/wiki/Ethereum-Development-Tutorial
~From blockchain installation to make tokenContract ~
2. What are smart contracts on Ethereum blockchain?
1Source: https://github.com/ethereum/wiki/wiki/White-Paper
Original idea derived from Nick Szabo’s paper in1997.
http://szabo.best.vwh.net/smart_contracts_idea.html
and going to be live by Stateful blockchain with Ethereum virtual machine
3. What kind of application needs smart contracts ?
2Source: Source
1.Vending machine
2.Asset automated transfer system
• Token Systems
• Financial derivatives
• Identity and Reputation Systems
• Decentralized File Storage
• Decentralized Autonomous
Organizations
• See also
https://docs.google.com/spreads
heets/u/1/d/1VdRMFENPzjL2V-
vZhcc_aa5-
ysf243t5vXlxC2b054g/edit
After blockchain age
4. Smart contract 4 purposes
3
Smart contract
1. Maintain a data
store contract.
2. Forwarding
contract which
has access policy,
and some
conditions to
send messages.
3. Ongoing
contract such as
escrow,
crowdfunding.
4.Library type
contract which
provides
functions to other
contracts.
Process objectives
Ethereum contracts objectives
Source: https://github.com/ethereum/wiki/wiki/Ethereum-Development-Tutorial
5. 1.Maintain a data store contract
4
Most simple type can be used by Embark (more about the later)
Source: http://jorisbontje.github.io/sleth/#/
# app/contracts/simple_storage.sol
contract SimpleStorage {
uint storedData;
function set(uint x) {
storedData = x * x * x;
}
function get() constant returns (uint retVal)
{
return storedData;
}
}
6. 2.Forwarding contract
5
Sleth is the decentralized slot machine application using forwarding type smart contract.
Currently I can not find the forwarding type contract which can be run on testnet. If you know please comment on.
Source: https://github.com/ethereum/wiki/wiki/Ethereum-Development-Tutorial
When Bob wants to finalize the bet, the following
steps happen:
1. A transaction is sent, triggering a message
from Bob's EOA to Bob's forwarding contract.
2. Bob's forwarding contract sends the hash of
the message and the Lamport signature to a
contract which functions as a Lamport
signature verification library.
3. The Lamport signature verification library sees
that Bob wants a SHA256-based Lamport sig,
so it calls the SHA256 library many times as
needed to verify the signature.
4. Once the Lamport signature verification library
returns 1, signifying that the signature has
been verified, it sends a message to the
contract representing the bet.
5. The bet contract checks the contract providing
the San Francisco temperature to see what the
temperature is.
7. 3.Ongoing contract
6Source: https://github.com/WeiFund/WeiFund/blob/master/WeiFund.sol
Most smart contracts are ongoing type contracts, such as crowdfunding is ongoing contract.
// WeiFund System
// Start, donate, payout and refund crowdfunding campaigns
// @authors:
// Nick Dodson <thenickdodson@gmail.com>
// If goal is not reached and campaign is expired, contributers can get the
refunded individually
// If goal is reached by alloted time, contributions can still be made
contract WeiFundConfig
{
function onContribute(uint cid, address addr, uint amount){}
function onRefund(uint cid, address addr, uint amount){}
function onPayout(uint cid, uint amount){}
}
contract WeiFund
{
struct User
{
uint numCampaigns;
mapping(uint => uint) campaigns;
}
struct Funder
{
address addr;
uint amount;
}
...
8. 4. Library contract
7Source: https://github.com/ethereum/wiki/wiki/Ethereum-Development-Tutorial
Smart contract which provides utility functions.
In the case below, Lamport sig verifier contract is Library type contracts.
The contract will provide a set of function
9. Structuring every type of smart contract
8
You can make structure by combining different contracts
Source:https://github.com/ethereum/wiki/wiki/Ethereum-Development-Tutorial
11. Our goal today
10
Download blockchain, writing smart contracts, publish contract on blockchain, send message to
contracts.
Source: http://jorisbontje.github.io/sleth/#/ http://ethereum.gitbooks.io/frontier-guide/content/contract_coin.html
http://meteor-dapp-cosmo.meteor.com/
Cool!
1. Install Ethereum blockchain 2.writing smart contract
3.Publish contract on blockchain 4.Send message to contracts &
check the result
tokenInstance.getBalance.call(eth.accounts[1])
tokenInstance.sendToken.sendTransaction(et
h.accounts[1], 100, {from: eth.accounts[0]})
12. 1. Install Ethereum blockchain
11
3 ways to install...
Source: http://jorisbontje.github.io/sleth/#/
1. Live Testnet chain https://stats.ethdev.com/
2. Private chain
3. Private chain with framework (Embark framework)
https://github.com/iurimatias/embark-framework
Cool!
13. 1. Install Ethereum blockchain
12Source: http://jorisbontje.github.io/sleth/#/
1. Live Testnet chain we will install this chain. https://stats.ethdev.com/
Cool!
CPP
O △ ☓
14. 1. Install Ethereum blockchain
13Source:
1.Live Testnet chain we will install this chain. https://stats.ethdev.com/
June, 2015
Go-Ethereum = Geth !
1. installation of geth
https://github.com/ethereum/go-ethereum/releases/tag/v0.9.20
2. geth account new
http://ethereum.gitbooks.io/frontier-guide/content/creating_accounts.html
3. Do mining & should wait until the Blocknumber on stats (roughly 4 ~6 hours )
https://stats.ethdev.com/
15. 1. Install Ethereum blockchain
14
2.Private chain is private blockchain just for you. No network, your own blockchain.
Source:https://github.com/ethereum/go-ethereum/wiki/Setting-up-private-network-or-local-cluster
Currently Ethereum using blockchain,
You can run multiple chains locally to do this,
1. Each instance has separate data dir
2. Each instance runs on a different port ( both eth and rpc. )
3. The instances know about each other
You can run multiple chains locally
1st chain
// create dir for blockchain
$ mkdir /tmp/eth/60/01
// run private chain
$ geth –datadir=“/tmp/eth/60/01” –
verbosity 6 –port 30301 –rpcport 8101
console 2>> /tmp/eth/60/01.log
// mining start
$ admin.miner.start()
You can run multiple chains locally
2nd chain but for my env doesn’t work.
// create dir for blockchain
$ mkdir /tmp/eth/61/01
// run private chain
$ geth –datadir=“/tmp/eth/61/01” –
verbosity 6 –port 30302 –rpcport 8102
console 2>> /tmp/eth/61/01.log
// mining start
$ admin.miner.start()
16. 1. Install Ethereum blockchain
15
3. Embark Framework for Ethereum DApps. https://iurimatias.github.io/embark-framework/
Source: https://iurimatias.github.io/embark-framework/
I feel
1. Easy to upload contracts you write to private blockchain
2. If you don’t know about How to use
3. Fast demo – Simple storage application using private chain is buildin demo.
$ npm install -g embark-framework grunt-cli
$ embark demo
$ cd embark_demo
$ embark blockchain
$ embark run
17. 2. Write smart contract after $ geth console
16
1. Write Token contract you can publish your own token. After $ geth console
### coin contract from console
var tokenSource = 'contract token { mapping (address => uint) balances; function token() { balances[msg.sender]
= 10000; } function sendToken(address receiver, uint amount) returns(bool sufficient) { if
(balances[msg.sender] < amount) return false; balances[msg.sender] -= amount; balances[receiver] +=
amount; return true; } function getBalance(address account) returns(uint balance){ return
balances[account]; } }'
var tokenCompiled = eth.compile.solidity(tokenSource).token
var tokenAddress = eth.sendTransaction({data: tokenCompiled.code, from: eth.accounts[0], gas:1000000});
admin.miner.start()
admin.miner.stop()
eth.getCode(tokenAddress)
tokenContract = eth.contract(tokenCompiled.info.abiDefinition)
tokenInstance = tokenContract.at(tokenAddress);
> tokenInstance
{
address: '0xf95ff51f532bd6821b98f312e876e1e2213f3e36',
sendToken: [Function],
getBalance: [Function]
}
tokenInstance.getBalance.call(eth.accounts[0])
tokenInstance.sendToken.sendTransaction(eth.accounts[1], 100, {from: eth.accounts[0]})
admin.miner.start()
admin.miner.stop()
tokenInstance.getBalance.call(eth.accounts[0])
> tokenInstance.getBalance.call(eth.accounts[0])
'9900'
18. 3.Publish contract
17
This phrase is uploading the contract onto blockchain.
### coin contract from console
var tokenSource = 'contract token { mapping (address => uint) balances; function token() { balances[msg.sender]
= 10000; } function sendToken(address receiver, uint amount) returns(bool sufficient) { if
(balances[msg.sender] < amount) return false; balances[msg.sender] -= amount; balances[receiver] +=
amount; return true; } function getBalance(address account) returns(uint balance){ return
balances[account]; } }'
var tokenCompiled = eth.compile.solidity(tokenSource).token
var tokenAddress = eth.sendTransaction({data: tokenCompiled.code, from:
eth.accounts[0], gas:1000000});
admin.miner.start()
admin.miner.stop()
eth.getCode(tokenAddress)
tokenContract = eth.contract(tokenCompiled.info.abiDefinition)
tokenInstance = tokenContract.at(tokenAddress);
> tokenInstance
{
address: '0xf95ff51f532bd6821b98f312e876e1e2213f3e36',
sendToken: [Function],
getBalance: [Function]
}
tokenInstance.getBalance.call(eth.accounts[0])
tokenInstance.sendToken.sendTransaction(eth.accounts[1], 100, {from: eth.accounts[0]})
admin.miner.start()
admin.miner.stop()
tokenInstance.getBalance.call(eth.accounts[0])
> tokenInstance.getBalance.call(eth.accounts[0])
'9900'
19. 4.Send message to the contract and check the result
18
This phrase is sending message to the contract on blockchain.
### coin contract from console
var tokenSource = 'contract token { mapping (address => uint) balances; function token() { balances[msg.sender]
= 10000; } function sendToken(address receiver, uint amount) returns(bool sufficient) { if
(balances[msg.sender] < amount) return false; balances[msg.sender] -= amount; balances[receiver] +=
amount; return true; } function getBalance(address account) returns(uint balance){ return
balances[account]; } }'
var tokenCompiled = eth.compile.solidity(tokenSource).token
var tokenAddress = eth.sendTransaction({data: tokenCompiled.code, from: eth.accounts[0], gas:1000000});
admin.miner.start()
admin.miner.stop()
eth.getCode(tokenAddress)
tokenContract = eth.contract(tokenCompiled.info.abiDefinition)
tokenInstance = tokenContract.at(tokenAddress);
> tokenInstance
{
address: '0xf95ff51f532bd6821b98f312e876e1e2213f3e36',
sendToken: [Function],
getBalance: [Function]
}
tokenInstance.getBalance.call(eth.accounts[0])
tokenInstance.sendToken.sendTransaction(eth.accounts[1], 100, {from:
eth.accounts[0]})
admin.miner.start()
admin.miner.stop()
tokenInstance.getBalance.call(eth.accounts[0])
> tokenInstance.getBalance.call(eth.accounts[0])
'9900'
20. Miscellaneous
19
Ethereum frontier guide
Solidty online compiler
https://chriseth.github.io/cpp-ethereum/
Geth(go-ethereum)
https://github.com/ethereum/go-ethereum/releases/tag/v0.9.20
State of the DApps spreadsheet
https://docs.google.com/spreadsheets/d/1VdRMFENPzjL2V-vZhcc_aa5-
ysf243t5vXlxC2b054g/edit#gid=0
Ethereum forum
https://forum.ethereum.org/categories/services-and-decentralized-applications
Solidity presentation
https://www.youtube.com/watch?v=DIqGDNPO5YM
http://ethereum.gitbooks.io/frontier-guide/content/creating_accounts.html
21. 20
Decentralization is just beginning.
We hope you start to be involved in!
Do you have interested in the DAppsMedia also ?
If you have, contract us from here!