A template for Ethereum-based DApp.
components-- common UI components of a web page such as header, footerethereum-- ethereum filesbuild-- compiled contracts<contract-name>.json-- compiled contract of<contract-name>.sol
contracts-- solidity contracts<contract-name>.sol-- solidity file of smart contract
<contract-name>.js--compile.js-- compiles<contract-name>.solfile and generates<contract-name>.jsonfiledeploy.js-- deploys the compiled contract onto Blockchain Network (e.g., Rinkeby)factory.js-- It tells web3 that a deployed copy of the contract exists.web3.js-- It configures web3 with a Blockchain network provider (in our case it is Rinkeby BC network) from metamask.
pages-- this directory contains react components that get turned into visitable webpagesindex.js-- home page of application
test-- test cases of smart contract<contract-name>.test.js-- test case of the<contract-name>
package.json-- project information and list require dependencies for the projectroutes.js-- list the routes of the projectserver.js-- boot upnextjsserver and tells to useroutes.jsREADME.md-- readme file of the projectnode_modules-- project dependencies
npm initto generatepackage.jsonfilenpm install --save ganache-cli mocha solc@0.4.17 fs-extra web3@1.0.0-beta.35to install dependenciesnpm install --save truffle-hdwallet-provider@0.0.3for deployment dependenciesnpm install --save next@4.1.4 react react-domfornext.jsdependenciesnpm install --save semantic-ui-reactfor UI componentsnpm install --save semantic-ui-cssfor semantic UI stylesheetnpm install --save next-routesfor routing
- Step 1: write smart contract (
.solfile) into Remix Editor - Step 2: copy paste the smart contract (
.solfile) into a file ofcontractsdirectory. - Step 3: compile the contract using
compile.js - Step 4: test the contract using a file of a
testdirectory. The contract is deployed on local networkganache - Step 5: deploy the contract on a test network (e.g., Rinkeby).
- Step 6: extract the address of the deployed contract from console.
- Step 7: Copy the address into
contract-address.txtfile for future use. - Step 8: Create React components into
pagesdirectory. - Step 9 : configures web3 with a provider from metamask in
web3.js - Step 10: update
factory.jsfile with a deployed contract address (specified incontract-address.txt) - Step 11: Use factory instance in React components (in
pagesdirectory) to interact with a deployed contract
Source of images: "Ethereum and Solidity: The Complete Developer's Guide" by Stephen Grider
This is an example of how factory pattern can be used to deploy different instances of a smart contract.
next.jswrapsreact.jswith some more functionalities such as routing, server-side rendering, hot module reload and so on.
this repository contains React components (as
.jsfiles) that get turned into visitable webpages. When installed.nextjs(from.nextfolder) starts, it see the react components intopagesdirectory and turn each into routes (according to its directory path), as shown belowed.
Configure
web3with a provider from a metamask.
next.js rendering at the server. It renders the pages and sends the HTML code to the browser first, then it sends javascript code to the browser.
It tells that a deployed copy of the contract on Rinkeby network exists
The nextjs server renders the HTML page and sends the HTML pages to the browser.
getInitialProps()function is executed at nextjs server.