ethereum contracts - coinfest 2015

18
Ethereum Contracts Coinfest 2015 - Decentral Vancouver Rob Myers - https://robmyers.org

Upload: rob-myers

Post on 15-Jul-2015

101 views

Category:

Internet


1 download

TRANSCRIPT

Page 1: Ethereum Contracts - Coinfest 2015

Ethereum ContractsCoinfest 2015 - Decentral Vancouver

Rob Myers - https://robmyers.org

Page 2: Ethereum Contracts - Coinfest 2015

ÐApps

Distributed Applications● Ethereum - Smart contracts● Whisper - Ephemeral communication● Swarm - DHT storage

Page 3: Ethereum Contracts - Coinfest 2015

How Will We Use ÐApps?

Page 4: Ethereum Contracts - Coinfest 2015

How Will We Develop ÐApps?

Page 5: Ethereum Contracts - Coinfest 2015

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.

Page 6: Ethereum Contracts - Coinfest 2015

If You Know MVC...

Page 7: Ethereum Contracts - Coinfest 2015

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.

Page 8: Ethereum Contracts - Coinfest 2015

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.

Page 9: Ethereum Contracts - Coinfest 2015

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.

Page 10: Ethereum Contracts - Coinfest 2015

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! :-)

Page 11: Ethereum Contracts - Coinfest 2015

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;

Page 12: Ethereum Contracts - Coinfest 2015

Compiling The Contract

$ solc --binary file --json-abi file Coin.sol

Page 13: Ethereum Contracts - Coinfest 2015

The Compiled Contract60056013565b6101e0806100216000396000f35b336000819060000155505b56006000357c010000

00000000000000000000000000000000000000000000000000009004806337f42841146100455780

6340c10f191461005a578063d0679d341461006e57005b6100506004356101a8565b806000526020

6000f35b610068600435602435610082565b60006000f35b61007c6004356024356100fd565b6000

6000f35b60005473ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffff

ffffffffffffffffffffff1614156100bd576100c2565b6100f9565b80600160008473ffffffffff

ffffffffffffffffffffffffffffff16815260200190815260200160002090815401908190600001

55505b5050565b80600160003373ffffffffffffffffffffffffffffffffffffffff168152602001

908152602001600020541061013257610137565b6101a4565b80600160003373ffffffffffffffff

ffffffffffffffffffffffff16815260200190815260200160002090815403908190600001555080

600160008473ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000

209081540190819060000155505b5050565b6000600160008373ffffffffffffffffffffffffffff

ffffffffffff1681526020019081526020016000205490506101db565b91905056

Page 14: Ethereum Contracts - Coinfest 2015

The Contract's ABI[

{

"constant" : true,

"inputs" : [

{

"name" : "addr",

"type" : "address"

}

],

"name" : "queryBalance",

"outputs" : [

{

"name" : "balance",

"type" : "uint256”,

Page 15: Ethereum Contracts - Coinfest 2015

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);

Page 16: Ethereum Contracts - Coinfest 2015

Try It Yourself

Online compiler:https://chriseth.github.io/cpp-ethereum/

Example ÐApps:https://github.com/ethereum/dapp-bin

Page 17: Ethereum Contracts - Coinfest 2015

Where To Learn More

The Wiki:https://github.com/ethereum/wiki/wiki

The Forum:https://forum.ethereum.org/

Page 18: Ethereum Contracts - Coinfest 2015

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