ethereum contracts - coinfest 2015
TRANSCRIPT
Ethereum ContractsCoinfest 2015 - Decentral Vancouver
Rob Myers - https://robmyers.org
ÐApps
Distributed Applications● Ethereum - Smart contracts● Whisper - Ephemeral communication● Swarm - DHT storage
How Will We Use ÐApps?
How Will We Develop ÐApps?
What Is A Contract?
● A small piece of code.● A self-contained piece of code.● A small amount of data.● A public body of data.● A small amount of Ether.
If You Know MVC...
What Is A Transaction?
● How we communicate with contracts.● Data and Ether can be sent via transactions.● Sent from the JS API or other contracts.● Transactions cost "gas", the sender pays.● JavaScript API can use accessors instead.
Writing Contracts
● Write contracts in the Solidity language.● Create the UI for contracts in HTML/JS.● AlethZero is a good place to start.● solc / eth / Chrome is the cutting edge.● Mix IDE & Mist browser are the future.
Bad Ideas For Contracts
● Doom on the EVM (too much processing).● Video on the blockchain (too much storage).● Ethereum web server (no network access).● Blockchain DRM (blockchain is public).● Anything that uses too much processor time,
storage, or secrecy.
Good Ideas For Contracts
● Altcoins, tokens, assets.● Crowdfunding, fan incentive schemes.● Voting systems, prediction markets, lotteries.● Access control - sites, games, doors, cars.● DAOs - Organizations on the blockchain.● Use your imagination! :-)
A Solidity Contractcontract Coin {
address minter;
mapping (address => uint) balances;
function Coin() {
minter = msg.sender;
}
function mint(address owner, uint amount) {
if (msg.sender != minter) return;
balances[owner] += amount;
}
function send(address receiver, uint amount) {
if (balances[msg.sender] < amount) return;
balances[msg.sender] -= amount;
balances[receiver] += amount;
Compiling The Contract
$ solc --binary file --json-abi file Coin.sol
The Compiled Contract60056013565b6101e0806100216000396000f35b336000819060000155505b56006000357c010000
00000000000000000000000000000000000000000000000000009004806337f42841146100455780
6340c10f191461005a578063d0679d341461006e57005b6100506004356101a8565b806000526020
6000f35b610068600435602435610082565b60006000f35b61007c6004356024356100fd565b6000
6000f35b60005473ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffff
ffffffffffffffffffffff1614156100bd576100c2565b6100f9565b80600160008473ffffffffff
ffffffffffffffffffffffffffffff16815260200190815260200160002090815401908190600001
55505b5050565b80600160003373ffffffffffffffffffffffffffffffffffffffff168152602001
908152602001600020541061013257610137565b6101a4565b80600160003373ffffffffffffffff
ffffffffffffffffffffffff16815260200190815260200160002090815403908190600001555080
600160008473ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000
209081540190819060000155505b5050565b6000600160008373ffffffffffffffffffffffffffff
ffffffffffff1681526020019081526020016000205490506101db565b91905056
The Contract's ABI[
{
"constant" : true,
"inputs" : [
{
"name" : "addr",
"type" : "address"
}
],
"name" : "queryBalance",
"outputs" : [
{
"name" : "balance",
"type" : "uint256”,
Call The Contract From JavaScript<script type="text/javascript" src="../ext/bignumber.min.js"></script><script type="text/javascript" src="../ext/ethereum.js/dist/ethereum.js"></script>...var web3 = require('web3');web3.setProvider(new web3.providers.HttpSyncProvider('http://localhost:8545'));var contract_address = "0xaf1206fcb32fbdb42878c429e61a49d5143e6f32";var contract_abi = ... // see previous slidevar contract = web3.eth.contract(contract_address, contract_abi);...var accountBalance = parseInt(contract.call().queryBalance(queryAddress));contract.call().send("0xaf1206fcb32fbdb42878c429e61a49d5143e6f32", accountBalance / 100);
Try It Yourself
Online compiler:https://chriseth.github.io/cpp-ethereum/
Example ÐApps:https://github.com/ethereum/dapp-bin
Where To Learn More
The Wiki:https://github.com/ethereum/wiki/wiki
The Forum:https://forum.ethereum.org/
Install Party!
AlethZero / solc / eth / mix:https://github.com/ethereum/cpp-ethereum/wiki
Mist:https://github.com/ethereum/go-ethereum/wiki/Building-Ethereum%28Go%29