Building Blockchain Application 👍
Building a blockchain application can be complex due to the various technologies involved. This guide will give you a basic understanding of the steps involved in building a blockchain application. Here we will focus on building a simple blockchain application using Ethereum and Solidity, with a basic front-end using web3.js library for interaction.
Prerequisite:
Basics understanding of Ethereum Public Blockchain
Javascript and node.js
Since we are using Ethereum -Solidity language
Tools: Metamask, Infura
Step 1: Installation of Tools
Node.js and npm (https://nodejs.org/en/download/)
Truffle Suite: Truffle is a popular development framework for Ethereum.
Install it using npm: npm install -g truffle
Metamask Wallet: Metamask is a browser extension that allows you to interact with the Ethereum blockchain. Download and install it from the [official Metamask website](https://metamask.io/download.html).
Ganache: Ganache is a personal blockchain for Ethereum development. download from https://www.trufflesuite.com/ganache
Step 2 :Project setup
create new directory
Use truffle to initialize a new project: truffle init
Note: This will create a basic project structure with contracts, migrations and test directories.
Step 3: write smart contract in solidity
we can use remix ide environment ,it has in built Ethereum Virtual Machine: use oneline ide https://remix.ethereum.org/#lang=en&optimize=false&runs=200&evmVersion=null&version=soljson-v0.8.18+commit.87f61d96.js
Contract directory we can create Contract.sol
smart contract looks like this,
This contract simply allows anyone to write a string of data to the blockchain, which can be read by anyone.
Step 4: Compiling and Migrating the contract
Compile the contract using Truffle:
cmd : truffle compile
In the migrations directory, create a new file called 2_deploy_contracts.js and write the following:
javascript.js
var MyContract = artifacts.require(“MyContract”);
module.exports = function(deployer) {
deployer.deploy(MyContract);
};
Note: Migrate the contract to your local blockchain Ganache ,we need to set up ganache and add metamask local network 8585 and use metamask private keys with ganache (client) to add tokens for deployment.
cmd: truffle migrate
Step 5 : Contract Testing
1. In the `test` directory, create a new file called `myContract.js`. You can write tests for your contract using JavaScript.
2. A basic test for the above contract might look like this:
3. Run : truffle test
Step 6: Creation of Front end with web3.js
1. In the root directory of your project, create a new `src` directory for your front end files.
2. Inside `src`, create `index.html` and `app.js`.
3. In `app.js`, use web3.js to interact with your contract:
4. In `index.html`, you can create a simple interface to interact with your contract:
5. Now its ready. you can check at ganache cli how the block is getting created.