Getting Started With Dapp Developments For Totally Beginners.
In this tutorial, you will learn how to develop your first Dapp using Truffle Framework. Truffle is a development environment used for Etherum Blockchain. It has all tools necessary to develop, test and deploy your dapp.
This Tutorial will be covering:
1-Dapp development tools
2- Installing framework
3- How to write a smart contract
4- How to test
5- How to migrate
6- How to call contract function from front end using web3js.
Tools You Need To Install:
1- Install Metamask extension: for interacting with the Ethereum network and to upload your Etherum wallet to deposit and send ETH. Learn more how on to use it from here.
2- Install Nodejs (It’s a prerequisite for installing Truffle)
3- Install Truffle ( solidity framework)
npm install -g truffle
Create your first Dapp hello App:
First, create a directory in your development folder of choice.
Then you need to use a truffle box(Truffle box is a simple boilerplate or starter app template to start your project)
truffle unbox yehia67/helloworldbox
Now you need to start your development environment.
This command will start the truffle console and generate 10 fake wallet addresses for testing purpose. We will use this console later for compiling and migration.
Go to MetaMask and import any private key generated from the truffle.
(Main Etherum network for production.. You should import http://127.0.0.1:9545/ network your Metamask wallet)/
Congratulation... You have your Dapp and Developments environment ready to work.
Explore the project structure:
got your project directory and select contracts folder then select Simple.sol (contracts/Simple.sol)
You should see something like this. A simple smart contract that get and set
value. You could read more about writing solidity using the link below
After writing your code you need to compile.
Open truffle console
Now that we’ve successfully compiled our contracts, it’s time to migrate them
to the blockchain!
A migration is a deployment script meant to alter the state of your
application’s contracts, moving it from one state to the next. For the first
migration, you might just be deploying new code, but over time, other
migrations might move data around or replace a contract with a new one.
Go To migrations/2_deploy_contracts.js file
Any Smart Contract you need to deploy you must include it on
2_deploy_contracts.js to migrate it.You can see in the first line the .json file that includes all contract details generated after compilation on build/directory. It’s known as artifacts. While in line four you deploy contract and to start migration Back in our terminal, migrate the contract to the blockchain.
Testing The Smart Contract
Go to test/Simple.test.js
this how we write test case for each function.
To check your test cases back to your terminal
Now it’s time to call your functions from front end using web3js library
Web3js is already install and ready to use in helloworldbox all you need to do is
Go to src/app.js
Initializing your contract:
Calling methods from contract using await keyword and the contract loaded
I hop you find this tutorial useful if you have any question please comment below.Thanks!