ingredients for creating dapps

27
Ingredients for creating Dapps by stefaan ponnet API Craftsmanship Meetup aug 11 2016

Upload: stefaan-ponnet

Post on 21-Jan-2017

135 views

Category:

Internet


0 download

TRANSCRIPT

Page 1: Ingredients for creating dapps

Ingredients for creating Dapps

by stefaan ponnetAPI Craftsmanship Meetup aug 11 2016

Page 2: Ingredients for creating dapps

The basics: Ethereum as a smart contract platform

Blockchain

Nodes and mining

Transactions

EVM

Smart Contracts

Page 3: Ingredients for creating dapps

Ethereum accounts : EOA and Contract accounts

Regular Accounts : ( EOA : Externally Owned Accounts )

- consist of a public / private keypair- controlled by a human - or whoever holds the private key

Contract account:

- contains the EVM code + the storage ( = smart contract )- smart contracts are executed by an EOA ( an EOA starts a transaction )

Page 4: Ingredients for creating dapps

The Ethereum Virtual Machine ( EVM )- Place where Smart Contracts run on the Ethereum blockchain- Ethereum is “Turing complete”. -> You can run any sort of application on

Ethereum - the “distributed world computer” - Dapp: Decentralized Application

Page 5: Ingredients for creating dapps

EVM and the Ethereum nodes - every Ethereum node runs instances of the EVM - maintain state and transaction consensus across the blockchain- contract code gets executed on every node

It is not efficient in terms of computing power , but gives:

- extreme levels of fault tolerance- ensures zero downtime- makes data stored on the blockchain forever unchangeable and

censorship-resistant.

Page 6: Ingredients for creating dapps

3 types of instructions in the EVM1. Create new Smart Contracts ( programs on the blockchain )2. Execute functions of a specific smart contract3. Transfer Ether

Page 7: Ingredients for creating dapps

EVM opcodes are specific for Ethereum

etc...

Page 8: Ingredients for creating dapps

Preventing spam

ProblemPublic blockchain : if anyone can deploy and run contracts - it gets overloaded

SolutionUse a anti-spam payment token : Gas

Page 9: Ingredients for creating dapps

GasEvery instruction in the EVM uses up gas

Page 10: Ingredients for creating dapps

- Gas is bought using Ether by the caller to fuel the EVM.- There is a (dynamic) gasprice ( ETH / gas )- You set a gas limit per transaction to avoid draining the caller’s account- When the execution of the contract ends within the gas limit , the new

state is saved on the blockchain and the leftover gas is refunded.

Gas

Page 11: Ingredients for creating dapps

GasWhen the EVM instance runs out of gas , the execution stops and no state changes are saved on the blockchain.

Page 12: Ingredients for creating dapps

What do we need ?- A running Ethereum node to connect to the blockchain ( JSON/RPC interface)- A client library (Web3) for talking to the JSON/RPC interface- A wallet (lightwallet) to sign transactions- A smart contract ( logic + storage )- A frontend ( Dapp ) for interacting with the smart contract.

Page 13: Ingredients for creating dapps

Ethereum nodeThere are several options :

Test on a real blockchain

- Ethereum has a public testnet to test your Dapp (‘morden’)- run a local testnet node on your development machine ( geth ) or in the cloud

Simulating

- Use a transaction+API simulator e.g. ethereumjs-testrpchttps://github.com/ethereumjs/testrpc

Page 14: Ingredients for creating dapps

Web3 client libraryhttps://github.com/ethereum/web3.js/

Page 15: Ingredients for creating dapps

Lightwallethttps://github.com/ConsenSys/eth-lightwallet

Allows signing of transactions on the client / in the browser

Page 16: Ingredients for creating dapps

A Smart contractTo create the EVM bytecode - we use a special programming language called Solidity

and we need a compiler that converts out solidity files into EVM bytecode for us.

Page 17: Ingredients for creating dapps

Compile solidity files in an online editorhttps://ethereum.github.io/browser-solidity

Page 18: Ingredients for creating dapps

Compile solidity files using Trufflehttps://truffle.readthedocs.io/en/latest/

Page 19: Ingredients for creating dapps

Compile your files using a Gulp-task var solc = require('solc');

Page 20: Ingredients for creating dapps

Example : Macaulay’s allowanceAs a parent you want to give a weekly allowance to your kid - which your kid can claim once every week.

We will create a contract that :

- Holds the allowance funds- Allows the parent to define the kid’s wallet account (recepient of the funds)- Allows the kid to periodically claim funds out of the allowance contract.

Page 21: Ingredients for creating dapps

Create the contract→ See example...

Page 22: Ingredients for creating dapps

Creating the DappWe will use Polymer to create a quick app

we want to be able to

- As a parent : deploy a new contract and set the rules for our kid’s allowance- As a kid : be able to get the allowance

Page 23: Ingredients for creating dapps

Create a few Polymer components- yo polymer → Scaffold out the base app- bower install web3-wallet → add web3-wallet component- yo polymer:el allowance-wallet → visualize your wallet & show balance- yo polymer:el allowance-create → create a new allowance contract- yo polymer:el allowance-claim → extract funds from the contract

Page 24: Ingredients for creating dapps

Deploy and test the appOnly deploy the HTML/js files

There is no central backend server

Hosting can also be decentral ( IPFS : Interplanetary File System )

→ Demo time !

Page 25: Ingredients for creating dapps

Thats it!Get the code here : https://github.com/sponnet/allowance

Page 26: Ingredients for creating dapps

DAO initiativesIf you like this & want to learn more and put it into practice :

We are rolling out several DAO’s ( https://www.ethereum.org/dao ) in which you can participate immediately as

- developer- curator- investor- marketing ...

e.g. decentralized video platform ( blocktu.be ) / a product DAO ( KimRadio ) / and a blockchain expert group DAO

Page 27: Ingredients for creating dapps

Next meetups Starting in september we will host 1 meetup per month like this one

And every week we’ll be organizing free open-table discussions here at BIA.

Join our group to recieve updates: http://www.meetup.com/API-Craftsmanship/

Hope to see you soon !@sponnet - [email protected] - https://www.linkedin.com/in/sponnet