
- what we will build in this course ?
- a fully decentralize voting app which :
- smart contract is deployed to ethereum test network : ropsten, rinkeby
- webui is deployed to distributed file system called : ipfs
- why voting app is perfect to demo decentralize app ?
- ethereum blockchain make sure that no one could change data after it is made
- we do not want any people could change vote result once it is made
- and we do not want any people change the logic behind back end
- structure of decentralize application:
- dapp like other web application, it contains:
- webui
- html layout : be updated dynamically by App.js javascript code
- App.js will communicate with blockchain and get back data when ever transaction happen
- backend is not any web server but is smart contract which is deployed to blockchain
- install node --> https://nodejs.org/en/
- install truffle framewrok --> https://truffleframework.com/truffle
- install ganache --> https://truffleframework.com/ganache
- install metamask --> https://metamask.io/
- install visual studio code --> https://code.visualstudio.com/
- install plugin for coding with solidity
- install live server
- install terminal
- start project
- truffle doc --> https://truffleframework.com/docs
- create a empty folder --> mkdir C:\election
- truffle prepare for us some template to speed up development process
- we will use pet-shop template --> truffle unbox pet-shop
- explain about what petshop have
- src : contain webui code html,css,js
- contracts and migrations folder
- test folder
- truffle.js file contain blockchain information for deploy smart contract
- if you on windows --> create truffle-config.js then copy
all content from truffle.js, then delete truffle.js file
- if you on mac --> just use truffle.js file
- this file describe how we will connect to blockchain
- start ganache
- create new contract name Election.sol
with very simple content
contract Election {
string public candidate;
constructor() public {
candidate = "candidate 1";
}
}
- create a new migration file call 2_deploy_election.js
- deploy start contract to local blockchain --> truffle migrate
- try to interact with smart contract
--> truffle console
--> Election.deployed().then(function(i){instance=i})
--> instance.candidate()
- design contract component
- data for contract
- need a struct to model a candidate
- need to manage multiple candidates
- need to manage multiple voters and state of vote
- function for contract
- add candidate function
- constructor function
- vote function
- event for communicate between contract and webui
interact with smart contract from truffle console
- get contract instance --> Election.deployed().then(function(i){instance=i})
- show contract address --> instance.address
- get candidate count --> instance.candidatesCount().then(function(count){vcount=count})
- get candidate name --> instance.candidates(1).then(function(candidate){name=candidate[1]})
- do vote using account 0 --> instance.vote(1, {from:web3.eth.accounts[0]})
- get voter state --> instance.voters(web3.eth.accounts[0])
- get vote count --> instance.candidates(1).then(function(candidate){voteCount=candidate[2]})
- vote with other voter --> instance.vote(1, {from:web3.eth.accounts[2]})
- event --> instance.vote(1, {from:web3.eth.accounts[8]}).then(function(result){console.log(result.logs[0].args._candidateId)})
/**
* automation test structure
* - check init number of candidate
*
* - check init value for each candidate
*
* - check vote function
* - check require candidate id condition for vote function
* - check require voter not yet vote condition
* - check voters change after vote
* - check vote count after vote
* - check event happen
*/
- download geth clien from --> https://geth.ethereum.org/downloads/
- install geth and binary will go to --> C:\Program Files\Geth
- start node and syncing use rinkeby network ---> geth --rinkeby
- attach to console javascript ---> geth attach ipc:\\.\pipe\geth.ipc
- check syncing status (need to wait until all sync complete) --> eth.syncing (should return false)
- create new account --> personal.newAccount()
- list account --> eth.accounts
- get some free eth from faucet --> https://faucet.rinkeby.io/
- check account balance with console --> eth.getBalance(your account address)
- check account balance and convert to ether --> web3.fromWei(your account address,"ether")
- start geth again with command --> geth --rinkeby --rpc --rpcapi db,eth,net,web3,personal
- add define for rinkeby network config to truffle-config.js (use truffle.js on MAC)
rinkeby: {
host: "127.0.0.1",
port: 8545,
network_id: 4,
from: "your account address"
}
- unlock account before deploy to test network --> personal.unlockAccount(your account address)
- deploy to rinkeby network after unlock accont ----> truffle migrate --reset --network rinkeby
- switch to rinkeby network on metamask
- improt rinkeby account to metamask ----> C:\Users\TAN\AppData\Roaming\Ethereum\rinkeby\keystore
- scan information after deploy and do vote ----> https://rinkeby.etherscan.io
- register user on infura --> https://infura.io
- go to dashboard and copy the api url with key and use in App.js
- change app.js to use infura host
- truffle hdwallet link --> https://github.com/trufflesuite/truffle-hdwallet-provider
- install windows build tool --> npm install --production windows-build-tools
- install hdwallet --> npm install truffle-hdwallet-provider
- get 12 words for mnenomic from ---> https://iancoleman.io/bip39/
- create test_hdwallet.js to show up address from mnenomic
- modify app.js to use truffle hdwallet provider
ropsten: {
provider: () => new HDWalletProvider(mnenomic, "https://ropsten.infura.io/v3/abbe691d210c470bbb9e2956e2f82c49"),
network_id: 3,
gas: 3000000,
gasPrice: 21
},
get some free ether from ropsten faucet -->https://faucet.ropsten.be/
- deploy to ropsten network --> truffle migrate --reset --network ropsten
- modify app.js to use ropsten infura url api
- use metamask to restore account from seed phrase
- install ipfs --> https://dist.ipfs.io/#go-ipfs
- init a ipfs node --> ipfs init
- run as long service --> ipfs daemon
- check the peer --> ipfs swarm peers
- add all dist folder --> ipfs add -r dist/
- publish the hash--> ipfs name publish "hash of dist"
- access dapp from internet --> gateway.ipfs.io/ipfs/"hash of dist"
What is Etherum ?
Ethereum is an open source, globally decentralized computing infrastructure that executes programs called smart contracts. It uses a blockchain to synchronize and store the system’s state changes, along with a cryptocurrency called ether to meter and constrain execution resource costs.
The Ethereum platform enables developers to build powerful decentralized applications with built-in economic functions. While providing high availability, auditability, transparency and neutrality, it also reduces or eliminates censorship, and reduces certain counterparty risks.
What is a smart contract ?
The term smart contract has been used over the years to describe a wide variety of different things. In the 1990s, cryptographer Nick Szabo coined the term and defined it as “a set of promises, specified in digital form, including protocols within which the parties perform on the other promises”. Since then, the concept of smart contracts has evolved, especially after the introduction of decentralized blockchain platforms with the invention of Bitcoin in 2009. In the context of Ethereum, the term is actually a bit of a misnomer, given that Ethereum smart contracts are neither smart nor legal contracts, but the term has stuck. In this book, we use the term “smart contract” to refer to immutable computer programs that run deterministically in the context of an Ethereum Virtual Machine as part of the Ethereum network protocol, i.e. on the decentralized Ethereum world computer.
What is a DApp?
A Decentralized Application, or DApp, is an application which is mostly or entirely decentralized.
Consider all the possible aspects of an application that may be decentralized:
Front-end software
Back-end software (logic)
Data storage
Name resolution
Message communications
Tools will be used in this course
In this course you will learn all tools which use for develop decentralize application : truffle frame work, ganache, metamask, visual studio code, geth, infura, ipfs, javascript, jquery. This course is design to practically guide you real life skill on development of Ethereum Dapp. Following topics are covered.
Understand the big picture of decentralize application
Install development tool : nodejs, truffle framework, ganache, metamask, visual studio code, and plugins
Start project with truffle unbox
Solidiy smart contract coding
Interact with smart contract use truffle console
Develop automation test for smart contract
Layout html
Javascript code to communicate with smart contract and update webui
Deploy smart contract to rinkeby test network use geth node
Deploy smart contract to ropsten test network use hdwallet and infura
Deploy webui to distributed file system with ipfs
Make the app fully decentralized
Blockchain and Ethereum dapp right now are very hot technical trend. Developer with good skill of blockchain and Dapp very easy to get jobs with 6 figures. Are you interesting joining the huge innovation of Dapp ? Joint my course and I will show you.