Blockchain Coding Dojo - BlockchainHub Graz

29
Blockchain Coding Dojo Ethereum Smart Contract with Solidity & web3.JS

Transcript of Blockchain Coding Dojo - BlockchainHub Graz

Page 1: Blockchain Coding Dojo - BlockchainHub Graz

Blockchain Coding Dojo

Ethereum Smart Contract with Solidity & web3.JS

Page 2: Blockchain Coding Dojo - BlockchainHub Graz

First Up:

● The Who / The Why / The How / The What● Short Presentation

○ Example we’re going to build○ Setup & Tools○ Solidity crash course○ Web3.js crash course

● Q & A

Welcome To The Dojo!

Page 3: Blockchain Coding Dojo - BlockchainHub Graz

● Didi

● Mario

The Who

Page 4: Blockchain Coding Dojo - BlockchainHub Graz

● Get Programmers more involved in Blockchain technology○ Get a feeling for what creating DApps is all about○ Get to know the tools & ecosystem○ Share experiences & learn

The Why

Page 5: Blockchain Coding Dojo - BlockchainHub Graz

● Coding Dojo!○ Split up in N % (3|4) groups

■ (groups of 3 or 4)○ Driver/Navigator Pair Programming (switch every ~8 Minutes - don’t

hug the keyboard!)■ Rest of the group contributes via discussion

○ Didi & Mario are available for help (as far as we can (not far! :D))○ Start Coding!

■ An example including a smart contract and a UI for using it○ Victory!

■ Deploy the contract to the testchain■ (If you’re done and still motivated, feel free to experiment with

/ expand the example!)

The How

Page 6: Blockchain Coding Dojo - BlockchainHub Graz

The What

Winner-Takes-All Crowdfunding on the Ethereum Blockchain!

Page 7: Blockchain Coding Dojo - BlockchainHub Graz

Example: Winner-Takes-All CrowdfundingBasically:

A Crowdfunding System, where people can submit their Projects until a Deadline, with an entry fee to avoid spam.

After this Deadline, people can “vote” for projects using Ether until a second Deadline.

After the second Deadline, the Contract can be finished and the project with the most “votes” (i.e. Ether) wins the WHOLE pot (all entry fees + all pledged Ether).

Page 8: Blockchain Coding Dojo - BlockchainHub Graz

Winner-Takes-All Crowdfunding - Tasks● Create contract with proposal date, campaign date in the future and

minimum entry fee ● People can enter projects (with an initial money transfer of X ether, so

there's no spam)○ until proposal date

● Now, after proposal date, other people can "vote" with ether for the projects

● When the target date is here and the contract gets finished, ALL the money goes to the project with the highest votes (most money contributed)

○ and contract is closed thereafter

Page 9: Blockchain Coding Dojo - BlockchainHub Graz

Winner-Takes-All Crowdfunding - “Layout”

Page 10: Blockchain Coding Dojo - BlockchainHub Graz

Setup & Tools● Repository:

○ https://github.com/zupzup/blockchainhub-solidity-dojo

● Includes:○ app.js - the UI JavaScript source○ contract.sol - the smart contract source○ contract.js - contract in one-line-form for web3.js○ index.html - the HTML for the UI○ js/ - static js○ css/ - static css○ LICENSE - the license (duh!)○ README.md - setup & task list

Page 11: Blockchain Coding Dojo - BlockchainHub Graz

Setup & Tools● Docker

○ https://www.docker.com/

● Solidity (Solc)○ https://solidity.readthedocs.io/en/develop/#○ https://ethereum.github.io/browser-solidity

● testRPC○ https://github.com/ethereumjs/testrpc

● Web3.JS○ https://github.com/ethereum/web3.js

○ There are some high-level frameworks as well■ https://truffle.readthedocs.io/en/latest/ ■ https://github.com/iurimatias/embark-framework■ http://populus.readthedocs.io/en/latest/ ■ ...

Page 12: Blockchain Coding Dojo - BlockchainHub Graz

Setup & Tools● Bothersome to set up locally, so we’ll use Docker

● TestRPC (our local blockchain, which auto-mines)○ docker pull harshjv/testrpc○ docker run -d -p 8545:8545 harshjv/testrpc

● Building (compile and make ready to use with web3.js)○ docker pull mzupzup/soliditybuilder○ docker run -v /path/to/this/folder:/sol mzupzup/soliditybuilder○ OR (on Linux + macOS) with file-watcher

■ docker pull mzupzup/soliditywatcher■ docker run -v /path/to/this/folder:/sol mzupzup/soliditywatcher

Page 13: Blockchain Coding Dojo - BlockchainHub Graz

Setup & Tools● Once testRPC and the build-container are running

○ Open index.html and see what’s what○ Explore contract.sol and app.js○ Get to work! :)

● Documentation:○ Solidity

■ http://solidity.readthedocs.io/en/develop/ ○ Web3.JS

■ https://github.com/ethereum/wiki/wiki/JavaScript-API ○ Async.js

■ http://caolan.github.io/async/docs.html ○ jQuery / HTML / CSS / JavaScript

■ https://duckduckgo.com/?q=jquery ;)

Page 14: Blockchain Coding Dojo - BlockchainHub Graz

Solidity● High-level language for smart contracts

○ Syntax similar to JavaScript○ Statically typed, Inheritance, Libraries can be imported

● https://ethereum.github.io/browser-solidity● Docs:http://solidity.readthedocs.io/en/develop/index.html ● We will use 0.4.6 (0.4.10 now, moves VERY fast)● Still very much in development, so some random things

just don’t work (yet)○ http://solidity.readthedocs.io/en/develop/frequently-asked-questions.

html

Page 15: Blockchain Coding Dojo - BlockchainHub Graz

Solidity - Smart Contracts Basics● Contract is basically your “backend application”

○ Running on the Blockchain

● Smart Contracts need Gas (i.e.: Money) to run○ No real money on our testRPC setup of course○ You can estimate the gas cost via web3 / browser solidity○ Only state-changing operations consume Gas

■ Just asking for a variable doesn’t○ Unused Gas is refunded

■ E.g.: you send a transaction and add 10000 Gas.● The transaction only needs 5000, so you get 5000 back

immediately.○ Incentive to leave a minimal footprint

Page 16: Blockchain Coding Dojo - BlockchainHub Graz

Solidity - Structure

Page 17: Blockchain Coding Dojo - BlockchainHub Graz

Solidity - Types● Integers

○ uint8 - uint256○ int8 - int256

● Strings● Address

○ .balance○ .send()

● Booleans● ...

Page 18: Blockchain Coding Dojo - BlockchainHub Graz

Solidity - Data Structures● Array (fixed & dynamic)

○ Mostly as one would expect (.length, array[0...N])

● Mapping (similar to hashmap)○ Can’t iterate over!

● Struct

Page 19: Blockchain Coding Dojo - BlockchainHub Graz

Solidity - Functions

Page 20: Blockchain Coding Dojo - BlockchainHub Graz

Solidity - Control Flow

Page 21: Blockchain Coding Dojo - BlockchainHub Graz

Solidity - EventsUsed for debugging only (logs to the blockchain)

Page 22: Blockchain Coding Dojo - BlockchainHub Graz

Solidity - Special Variables● Msg.value - value of the transaction● Msg.sender - address of the transaction’s sender

● http://solidity.readthedocs.io/en/develop/units-and-global-variables.html#block-and-transaction-properties

Page 23: Blockchain Coding Dojo - BlockchainHub Graz

Solidity - ThrowReverts transfer, returns “error”

Page 24: Blockchain Coding Dojo - BlockchainHub Graz

Solidity - Example Contractshttp://solidity.readthedocs.io/en/develop/solidity-by-example.html

Remember, maybe we can’t use ALL new features of 0.4.10, because we use the 0.4.6 compiler.

Page 25: Blockchain Coding Dojo - BlockchainHub Graz

Web3 - the Ethereum JavaScript API● https://github.com/ethereum/web3.js● Used for:

○ Connecting to the Blockchain○ Compiling contracts○ Instantiating Contracts○ Calling methods on contracts○ Retrieve public values from contracts○ Send Transactions○ Convert to/from Wei

● Basically a full interface for the Ethereum Blockchain○ Docs: https://github.com/ethereum/wiki/wiki/JavaScript-API

Page 26: Blockchain Coding Dojo - BlockchainHub Graz

● Addresses of available accounts:○ web3.eth.accounts

● Get Balance for an account in Ether:○ web3.fromWei(web3.toDecimal(web3.eth.getBalance(web3.eth.accounts[0])

), ‘ether’)

● Get a “public” attribute of the contract:○ contractInstance.{publicAttribute}(function(err, data) { ..callback

function.. })

● Sending a simple transaction:○ contractInstance.{payableFunction}(function params.., {

from: fromAddress,value: valueToSendInWei,gas: gasCostOfTransaction

}, function(err, data) { ..callback function.. })

Web3 - Examples (just to get a feeling for the API)

Page 27: Blockchain Coding Dojo - BlockchainHub Graz

● Debugging (very important!)○ Browser Dev Tools○ Solidity Events

■ Can listen to them in web3.js■ https://github.com/ethereum/wiki/wiki/JavaScript-API#contract-all

events ■ Logs all events within the contract

Web3

Page 28: Blockchain Coding Dojo - BlockchainHub Graz

Questions?