blockchain programming
TRANSCRIPT
Blockchain ProgrammingSoftware Development With Ethereum
Rob Myers - https://robmyers.org/
Implementations Of Ethereum
● go-ethereum● cpp-ethereum● pyethereum● ethereumj (java)● node-ethereum (client)All implement the same VM / network.Docker images are available.
Ðapps
Distributed Applications● Ethereum - Smart contracts● Whisper - Ephemeral communication● Swarm - DHT storage
How Will We Use Ðapps?
How Will We Develop Ðapps?
How Do We Write Ðapps Now?
How Do We Deploy And Test Ð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...
So How Do We Get External State?
● Direct user input● Oracles● Transactions / feeds from trusted parties● SchellingCoins● By hash...
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.
Transaction Costs
“usually, a computational step costs 1 gas, but some operations cost higher amounts of gas because they are more computationally expensive, or increase the amount of data that must be stored as part of the state. There is also a fee of 5 gas for every byte in the transaction data.” - The Whitepaper.
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 needs 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! :-)
Writing Contracts
● Write contracts in the Solidity language.● Create the UI for contracts in HTML/JS.● AlethZero is a good development system.● solc / eth / Chrome is the cutting edge.● Test on a private chain.● Mix IDE & Mist browser are the future.
Solidity
● C-ish syntax() {;}● Statically typed● Types for hashes, addresses, memory cells● Structs, mappings, events● Support for invariants & other conditions● Provably correct subset
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
(Or paste it into AlethZero...)
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/
The official tutorial:https://dappsforbeginners.wordpress.com/
Any Questions?
http://robmyers.org/notes/ethereum/