Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Developing a decentralised application for Ethereum

Developing a decentralised application for Ethereum

The slides from my presentation at Devoxx Morocco 2019 in Agadir.
#devoxxma #chainskills

Said Eloudrhiri

November 12, 2019
Tweet

More Decks by Said Eloudrhiri

Other Decks in Programming

Transcript

  1. 2 • In IT since 1992 • Agile coach •

    Developer, Trainer, Speaker, Writer • Founder of Noratek • Co-founder of ChainSkills with Sébastien Arbogast (@sarbogast) @eloudsa
  2. 3 Disclaimer • We are not related to any bank

    or any trading company or marketplace. • Material used in this presentation remains the property of their owners.
  3. #chainskills Disclaimers (bis) 4 • This is completely original code

    • Bear with us: there are PoCs, it might not be the most efficiently coded • This is by no means usable in production • We will focus on smart contracts and show you a bit of web user interface at the end • Room for improvement? Please contribute on Github ;-) • Demo session could fail. Shit happens ;-)
  4. 8 0x1AB… 0X2CD… + TX gas fee OK OK Signed

    Tx 0x1AB sends 1 ETH to 0x2CD OK
  5. Bloc #0 Bloc #1 Bloc #2 Bloc #3 Genesis TX

    1 TX 2 TX 3 TX n TX 1 TX 2 TX 3 TX n TX 1 TX 2 TX 3 TX n 9 TX 3 Tx 3 0x1AB sent 1 ETH to 0x2CD
  6. Bloc #0 Bloc #1 Bloc #n Bloc #n+x Genesis TX

    1 TX 2 TX 3 TX n TX 1 TX 2 TX 3 TX n TX 1 TX 2 TX 3 TX n 11 … TX 1 TX 2 TX n TX 3 … Bloc #n-1 TX 3 fake …
  7. Smart contract 17 • Micro-service • Distributed across the Ethereum

    network • Uniquely identified through its unique address • The code is immutable, so not updatable • State variables are mutable • The exposed functions modify the state • Has a wallet and a balance in ether • Can receive/transfer ethers from/to a user or to another contract • Can be composed with other contracts • Can emit events or throw exceptions
  8. 18 Bloc 0 Bloc 1 Bloc 2 Bloc 3 Bloc

    n smart contract Ethereum Virtual Machine 0x123 ?
  9. 20 HTML, CSS, JS web3js Dapp/Frontend Bloc 0 Bloc 1

    Bloc 2 Bloc 3 Bloc n smart contract Dapp/Backend Ethereum Virtual Machine 0x123 wrapper Wallet
  10. Deploy a smart contract pragma solidity ^0.4.25; contract Greetings {

    string message; constructor() public { message = "I am ready!"; } function setGreetings(string _message) public { message = _message; } function getGreetings() public view returns (string) { return message; } } Solidity code Greetings.sol Bytecode ABI Address Code Balance Greetings.json Ethereum node compile deploy
  11. 22 0x608060405234801561001057600080fd5b5060408051908101604052806 00b81526020017f4920616d20726561647921000000000000000000000000 0000000000000000008152506000908051906020019061005c92919061006 2565b50610107565b82805460018160011615610100020316600290049060 0052602060002090601f016020900481019282601f106100a357805160ff19 168380011785556100d1565b828001600101855582156100d1579182015b8 28111156100d05782518255916020019190600101906100b5565b5b509050 6100de91906100e2565b5090565b61010491905b808211156101005760008 160009055506001016100e8565b5090565b90565b6102d780610116600039

    6000f30060806040526004361061004c576000357c0100000000000000000 000000000000000000000000000000000000000900463ffffffff16806349da5 de414610051578063ca4c3a41146100ba575b600080fd5b34801561005d576 00080fd5b506100b860048036038101908080359060200190820180359060 2001908080601f01602080910402602001604051908101604052809392919 0818152602001838380828437820191505050505050919291929050505061 014a565b005b3480156100c657600080fd5b506100cf610164565b60405180 8060200182810382528381815181526020019150805190602001908083836 0005b8381101561010f5780820151818401526020810190506100f4565b505 05050905090810190601f16801561013c5780820380516001836020036101 000a031916815260200191505b509250505060405180910390f35b8060009 080519060200190610160929190610206565b5050565b6060600080546001 81600116156101000203166002900480601f0160208091040260200160405 1908101604052809291908181526020018280546001816001161561010002 03166002900480156101fc5780601f106101d1576101008083540402835291 602001916101fc565b820191906000526020600020905b815481529060010 1906020018083116101df57829003601f168201915b5050505050905090565 b828054600181600116156101000203166002900490600052602060002090 601f016020900481019282601f1061024757805160ff191683800117855561 0275565b82800160010185558215610275579182015b82811115610274578 251825591602001919060010190610259565b5b5090506102829190610286 565b5090565b6102a891905b808211156102a457600081600090555060010 161028c565b5090565b905600a165627a7a723058201a235e9931eb6f7b0e5 f7612b43b88fa6773d29d40dce4582687be83c66fc9b00029 Disamssembly Bytecode Opcode
  12. Gas, Gas Price 25 Transaction Fee = Gas * Gas

    Price 0X123… 0x456… Gas: sum used to execute each opcode Gas Price: Price in Wei that the account is willing to pay per unit of gas
  13. Gas, Gas Price 27 Transaction Fee = Gas * Gas

    Price 0X123… 0x456… ? Gas Price: 20 Gwei Defined by you Gas: 21000 Opcode cost 1 ETH ? 1 ETH
  14. Gas, Gas Price 28 Transaction Fee = Gas * Gas

    Price 0X123… 0x456… Transaction Fee = 420000 Gwei Transaction Fee = 0.00042 ether Paid by Alice to the Miner 1 ETH 1 ETH + 0.00042 ETH
  15. Low Gas Price = Long Delay 29 Transaction Fee =

    Gas * Gas Price 0X123… 0x456… ? Gas: 21000 Gas Price: 1 Gwei Too low? You will have to wait for a miner
  16. Additional setup - Windows 56 On Windows, you will need

    to install these packages: > npm install --global --production windows-build-tools > npm install --global node-gyp LAB 0
  17. Addtional setup - Mac 57 On Mac, you will need

    to install Xcode command line tools: $ xcode-select —install LAB 0
  18. Solidity 63 https://solidity.readthedocs.io/en/latest/ “Solidity is an object-oriented, high-level language for

    implementing smart contracts. Smart contracts are programs which govern the behaviour of accounts within the Ethereum state.” LAB 1
  19. web3.js 64 “web3.js is a collection of libraries which allow

    you to interact with a local or remote ethereum node, using a HTTP or IPC connection.” https://web3js.readthedocs.io/en/v1.2.2/ LAB 1
  20. #chainskills Objectives 65 1.Create the project 2.Write the smart contract

    3.Prepare the deploy 4.Build and Deploy the contract to Ganache 5.Interact with the contract with web3.js 6.Inspect through Truffle console and Ganache Truffle console web3js Greetings LAB 1
  21. #chainskills Objectives 69 1.Write Unit test cases in JS (Mocha

    and Chai) 2.Run the tests through Truffle console Truffle console web3js Greetings Unit Tests 69 LAB 2
  22. #chainskills Objectives 73 1.Write event 2.Catch event on the console

    3.Listen event on Ganache 4.Improve unit tests Truffle console web3js Greetings Unit Tests LAB 3
  23. #chainskills Objectives 77 1.Write exception handlers 2.Catch exceptions on the

    console 3.Inspect impacts on Ganache 4.Improve unit tests Truffle console web3js Greetings Unit Tests LAB 4
  24. #chainskills Objectives 81 1.Write payable functions 2.Transfer earnings 3.Check impacts

    on Ganache 4.Improve unit tests LAB 5 Truffle console web3js Greetings Unit Tests
  25. #chainskills Objectives 85 1.Write kill function 2.Kill the contract from

    the console 3.Interact with the contract 4.Check impacts wallet Truffle console web3js Greetings Unit Tests LAB 6
  26. #chainskills Objectives 89 1.Write function modifiers and base contract 2.Check

    impacts on Ganache Truffle console web3js Greetings Unit Tests LAB 7
  27. #chainskills Objectives 93 1.Create react project 2.Install Drizzle and other

    dependencies 3.Configure Drizzle 4.Configure Metamask 5.Interact with the contract HTML, CSS, JS, web3js Greetings Dapp/Frontend Dapp/Backend LAB 8
  28. #chainskills Objectives 100 1.Get initial data 2.Change greetings message 3.Change

    service fee 4.Listen events 5.Transfer earnings 6.Enable/Disable contract HTML, CSS, JS, web3js Greetings Dapp/Frontend Dapp/Backend LAB 9
  29. #chainskills Objectives 104 1.Create an account in infura.io 2.Configure Truffle

    to deploy on Ropsten 3.Create accounts on Ropsten and get some fake ETH 4.Deploy backend to Infura 5.Configure frontend for Github Pages 6.Configure Github repository 7.Deploy frontend to Github Pages LAB 10
  30. ChainBizz 108 • An issuer creates and submits a project

    • A fulfiller offers his services • The issuer accepts the service and deposit the amount • The fulfiller delivers the project • The issuer accepts the delivery. The fulfiller receives the amount
  31. 109

  32. ChainSkills - Udemy 114 • Online training on Udemy •

    14000+ students • 145 countries • rating: 4,2 on 5 • training.chainskills.com • Target • Managers • Developers PROMO CODE: DEVOXXMA2019
  33. ChainSkills - eBook 115 • Lean-published eBook • Create a

    dApp from A to Z • https://leanpub.com/blockchain-developer • Target • Managers • Developers PROMO CODE: DEVOXXMA2019