{"id":39662,"date":"2024-09-03T05:30:11","date_gmt":"2024-09-03T11:00:11","guid":{"rendered":"https:\/\/www.solutionanalysts.com\/blog\/?p=39662"},"modified":"2024-09-23T07:00:38","modified_gmt":"2024-09-23T12:30:38","slug":"blockchain-application-using-node-js","status":"publish","type":"post","link":"https:\/\/www.solutionanalysts.com\/blog\/blockchain-application-using-node-js\/","title":{"rendered":"How to Create a Blockchain Application Using Node.js: A Detailed Guide"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">In recent years, blockchain technology has emerged as a revolutionary force across numerous industries. The centralized nature and the security it provides has given it the boost it required to go across the globe. Countries like El Salvador have adopted Bitcoin as a legal tender across the nation. <\/span><span style=\"font-weight: 400;\">Blockchain technology is transforming the way we handle data, ensuring transparency, security, and immutability. In this guide, we&#8217;ll walk you through building a simple blockchain application with Node.js. By the end of this tutorial, you&#8217;ll have a solid understanding of how blockchains work and how to implement one in your projects.\u00a0<\/span><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Prerequisites\"><\/span><b>Prerequisites\u00a0<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Before beginning, you must have the following things:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Basic knowledge of JavaScript and Node.js\u00a0<\/span><\/li>\n<\/ul>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Node.js installed on your system\u00a0<\/span><\/li>\n<\/ul>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">A code editor like Visual Studio Code\u00a0<\/span><\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-39663\" src=\"https:\/\/www.solutionanalysts.com\/blog\/wp-content\/uploads\/2024\/09\/Blog-Cover-02.jpg\" alt=\"Node.Js blockchain\" width=\"1920\" height=\"1080\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Step_1_Setting_Up_the_Project\"><\/span><b>Step 1: Setting Up the Project\u00a0<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Start by creating a new directory for your project and initializing it with npm:\u00a0<\/span><\/p>\n<div class=\"cards\">\n<div class=\"card-contents\">\n<i><span style=\"font-weight: 400;\">mkdir blockchain-nodejs <\/span><\/i><i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">cd blockchain-nodejs <\/span><\/i><i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">npm init -y\u00a0<\/span><\/i>\n<\/div>\n<\/div>\n<p><span style=\"font-weight: 400;\">Next, you need to install the necessary dependencies:\u00a0<\/span><\/p>\n<div class=\"cards\">\n<div class=\"card-contents\">\n<i><span style=\"font-weight: 400;\">npm install crypto-js\u00a0<\/span><\/i>\n<\/div>\n<\/div>\n<h2><span class=\"ez-toc-section\" id=\"Step_2_Creating_the_Blockchain_Class\"><\/span><b>Step 2: Creating the Blockchain Class\u00a0<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">In the root of your project, create a file named blockchain.js. This file will contain the logic for our blockchain.\u00a0<\/span><\/p>\n<div class=\"cards\">\n<div class=\"card-contents\">\n<i><span style=\"font-weight: 400;\">const SHA256 = require(&#8216;crypto-js\/sha256&#8217;);<\/span><\/i><span style=\"font-weight: 400;\">\u00a0<\/span><\/p>\n<p><i><span style=\"font-weight: 400;\">class Block {\u00a0<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">constructor(blockIndex, timestamp, blockData, previousBlockHash = &#8221;) {\u00a0<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><\/i> <i><span style=\"font-weight: 400;\">this.blockIndex = blockIndex;\u00a0<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><\/i> <i><span style=\"font-weight: 400;\">this.timestamp = timestamp;\u00a0<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><\/i> <i><span style=\"font-weight: 400;\">this.blockData = blockData;\u00a0<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><\/i> <i><span style=\"font-weight: 400;\">this.previousBlockHash = previousBlockHash;\u00a0<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><\/i> <i><span style=\"font-weight: 400;\">this.blockHash = this.calculateHash();\u00a0<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">}\u00a0<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">calculateHash() {\u00a0<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><\/i> <i><span style=\"font-weight: 400;\">return SHA256(\u00a0<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/i> <i><span style=\"font-weight: 400;\">this.blockIndex +\u00a0\u00a0<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/i> <i><span style=\"font-weight: 400;\">this.previousBlockHash +\u00a0\u00a0<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/i> <i><span style=\"font-weight: 400;\">this.timestamp +\u00a0\u00a0<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/i> <i><span style=\"font-weight: 400;\">JSON.stringify(this.blockData)\u00a0<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><\/i> <i><span style=\"font-weight: 400;\">).toString();\u00a0<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">}\u00a0<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">}\u00a0<\/span><\/i><\/p>\n<\/div>\n<\/div>\n<p><span style=\"font-weight: 400;\">This code defines a Block class that represents each block in the blockchain. It is important to know that besides index, timestamp, and data, each block contains its own hash and the previous block hash.\u00a0<\/span><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Step_3_Adding_the_Blockchain_Logic\"><\/span><b>Step 3: Adding the Blockchain Logic\u00a0<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Now, let&#8217;s create the blockchain structure by adding a Blockchain class:\u00a0<\/span><\/p>\n<div class=\"cards\">\n<div class=\"card-contents\">\n<i><span style=\"font-weight: 400;\">class Blockchain {\u00a0<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">constructor() {\u00a0<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><\/i> <i><span style=\"font-weight: 400;\">this.chain = [this.createGenesisBlock()];\u00a0<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">}\u00a0<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">createGenesisBlock() {\u00a0<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><\/i> <i><span style=\"font-weight: 400;\">return new Block(0, &#8220;08\/22\/2024&#8221;, &#8220;Genesis Block&#8221;, &#8220;0&#8221;);\u00a0<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">}\u00a0<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">\u00a0\u00a0<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">getLatestBlock() {\u00a0<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><\/i> <i><span style=\"font-weight: 400;\">return this.chain[this.chain.length &#8211; 1];\u00a0<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">}\u00a0<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">addBlock(newBlock) {\u00a0<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><\/i> <i><span style=\"font-weight: 400;\">newBlock.previousHash = this.getLatestBlock().hash;\u00a0<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><\/i> <i><span style=\"font-weight: 400;\">newBlock.hash = newBlock.calculateHash();\u00a0<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><\/i> <i><span style=\"font-weight: 400;\">this.chain.push(newBlock);\u00a0<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">}\u00a0<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">isChainValid() {\u00a0<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><\/i> <i><span style=\"font-weight: 400;\">return this.chain.slice(1).every((currentBlock, index) =&gt; {\u00a0<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/i> <i><span style=\"font-weight: 400;\">const previousBlock = this.chain[index];\u00a0<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/i> <i><span style=\"font-weight: 400;\">return currentBlock.hash === currentBlock.calculateHash() &amp;&amp;\u00a0<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/i> <i><span style=\"font-weight: 400;\">currentBlock.previousHash === previousBlock.hash;\u00a0<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><\/i> <i><span style=\"font-weight: 400;\">});\u00a0<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">} Preferences<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">}\u00a0<\/span><\/i>\n<\/div>\n<\/div>\n<p><span style=\"font-weight: 400;\">The Blockchain class initializes the chain with a genesis block, adds new blocks, and verifies the integrity of the chain.\u00a0<\/span><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Step_4_Testing_the_Blockchain\"><\/span><b>Step 4: Testing the Blockchain\u00a0<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Finally, let&#8217;s test our blockchain by creating some blocks and checking its validity.\u00a0<\/span><\/p>\n<p><i><span style=\"font-weight: 400;\">let myBlockchain = new Blockchain(); <\/span><\/i><i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">myBlockchain.addBlock(new Block(1, &#8220;12\/01\/2024&#8221;, { amount: 4 })); <\/span><\/i><i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">myBlockchain.addBlock(new Block(2, &#8220;12\/08\/2025&#8221;, { amount: 10 })); <\/span><\/i><i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i> <i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">console.log(&#8216;Blockchain valid? &#8216; + myBlockchain.isChainValid()); <\/span><\/i><i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">console.log(JSON.stringify(myBlockchain, null, 4));\u00a0<\/span><\/i><\/p>\n<p><span style=\"font-weight: 400;\">This script will add a few blocks to our blockchain and then print out the blockchain along with its validity.\u00a0<\/span><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Step_5_Implementing_a_Smart_Contract\"><\/span><b>Step 5: Implementing a Smart Contract\u00a0<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"Creating_the_Smart_Contract\"><\/span><b>Creating the Smart Contract\u00a0<\/b><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><span style=\"font-weight: 400;\">Smart contracts are stored and executed on a blockchain application with Node.js, they self-execute and their terms are based on coding. In this section, we&#8217;ll create a simple smart contract using Solidity and interact with it using Node.js.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">First, let&#8217;s create a simple Solidity smart contract. Create a new file called SimpleStorage.sol:\u00a0<\/span><\/p>\n<div class=\"cards\">\n<div class=\"card-contents\">\n<i><span style=\"font-weight: 400;\">\/\/ SPDX-License-Identifier: MIT <\/span><\/i><i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">pragma solidity ^0.8.0; <\/span><\/i><i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i> <i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">contract SimpleStorage { <\/span><\/i><i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i> <i><span style=\"font-weight: 400;\">uint256 public storedData; <\/span><\/i><i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i> <i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i> <i><span style=\"font-weight: 400;\">function set(uint256 x) public { <\/span><\/i><i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><\/i> <i><span style=\"font-weight: 400;\">storedData = x; <\/span><\/i><i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i> <i><span style=\"font-weight: 400;\">} <\/span><\/i><i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i> <i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i> <i><span style=\"font-weight: 400;\">function get() public view returns (uint256) { <\/span><\/i><i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><\/i> <i><span style=\"font-weight: 400;\">return storedData; <\/span><\/i><i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i> <i><span style=\"font-weight: 400;\">} <\/span><\/i><i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">}\u00a0<\/span><\/i>\n<\/div>\n<\/div>\n<p><span style=\"font-weight: 400;\">This contract allows you to store and retrieve a single uint256 value on the blockchain.\u00a0<\/span><\/p>\n<h3><span class=\"ez-toc-section\" id=\"Compiling_the_Smart_Contract\"><\/span><b>Compiling the Smart Contract\u00a0<\/b><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><span style=\"font-weight: 400;\">To compile the Solidity contract, you&#8217;ll need the Solidity compiler (solc).\u00a0<\/span><\/p>\n<div class=\"cards\">\n<div class=\"card-contents\">\n<i><span style=\"font-weight: 400;\">npm install -g solc\u00a0<\/span><\/i><\/p>\n<p><span style=\"font-weight: 400;\">Compile the contract using the following command:\u00a0<\/span><\/p>\n<p><i><span style=\"font-weight: 400;\">solc &#8211;abi &#8211;bin SimpleStorage.sol -o output\u00a0<\/span><\/i>\n<\/div>\n<\/div>\n<p><span style=\"font-weight: 400;\">This will generate the ABI (Application Binary Interface) and the binary code of the contract, which we can use to interact with it in our Node.js application.\u00a0<\/span><\/p>\n<p><a href=\"https:\/\/www.solutionanalysts.com\/contact-us\/\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-39667 size-full\" src=\"https:\/\/www.solutionanalysts.com\/blog\/wp-content\/uploads\/2024\/09\/Blog-Cover-03-3-1.jpg\" alt=\"blockchain using nodejs development\" width=\"1920\" height=\"628\" \/><\/a><\/p>\n<h3><span class=\"ez-toc-section\" id=\"Deploying_and_Interacting_with_the_Smart_Contract\"><\/span><b>Deploying and Interacting with the Smart Contract\u00a0<\/b><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><span style=\"font-weight: 400;\">Now, let&#8217;s create a script to deploy the contract to an Ethereum network and interact with it. We&#8217;ll use web3.js for this purpose. First, install the required dependencies:\u00a0<\/span><\/p>\n<div class=\"cards\">\n<div class=\"card-contents\">\n<i><span style=\"font-weight: 400;\">npm install web3\u00a0<\/span><\/i><\/p>\n<p><span style=\"font-weight: 400;\">Create a new file named deploy.js\u00a0<\/span><\/p>\n<p><i><span style=\"font-weight: 400;\">const Web3 = require(&#8216;web3&#8217;);<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">const fs = require(&#8216;fs&#8217;);<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">const path = require(&#8216;path&#8217;);<\/span><\/i><i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">\/\/ Connect to the local Ethereum node <\/span><\/i><i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">const web3 = new Web3(&#8216;http:\/\/localhost:8545&#8217;); <\/span><\/i><i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i> <i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">\/\/ Get the compiled contract&#8217;s ABI and bytecode <\/span><\/i><i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">const abi = JSON.parse(fs.readFileSync(path.resolve(__dirname, &#8216;output\/SimpleStorage.abi&#8217;), &#8216;utf-8&#8217;)); <\/span><\/i><i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">const bytecode = fs.readFileSync(path.resolve(__dirname, &#8216;output\/SimpleStorage.bin&#8217;), &#8216;utf-8&#8217;); <\/span><\/i><i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i> <i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">\/\/ Deploy the contract <\/span><\/i><i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">(async () =&gt; { <\/span><\/i><i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i> <i><span style=\"font-weight: 400;\">const accounts = await web3.eth.getAccounts(); <\/span><\/i><i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i> <i><span style=\"font-weight: 400;\">const simpleStorage = new web3.eth.Contract(abi); <\/span><\/i><i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i> <i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i> <i><span style=\"font-weight: 400;\">simpleStorage.deploy({ <\/span><\/i><i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><\/i> <i><span style=\"font-weight: 400;\">data: bytecode <\/span><\/i><i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i> <i><span style=\"font-weight: 400;\">}) <\/span><\/i><i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i> <i><span style=\"font-weight: 400;\">.send({ <\/span><\/i><i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><\/i> <i><span style=\"font-weight: 400;\">from: accounts[0], <\/span><\/i><i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><\/i> <i><span style=\"font-weight: 400;\">gas: 15000, <\/span><\/i><i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><\/i> <i><span style=\"font-weight: 400;\">gasPrice: &#8216;300000000000&#8217; <\/span><\/i><i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i> <i><span style=\"font-weight: 400;\">}) <\/span><\/i><i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i> <i><span style=\"font-weight: 400;\">.then(newContractInstance =&gt; { <\/span><\/i><i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><\/i> <i><span style=\"font-weight: 400;\">console.log(&#8216;Contract deployed at address:&#8217;, newContractInstance.options.address); <\/span><\/i><i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i> <i><span style=\"font-weight: 400;\">}); <\/span><\/i><i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">})();\u00a0<\/span><\/i>\n<\/div>\n<\/div>\n<p><span style=\"font-weight: 400;\">This script connects to a local Ethereum node, deploys the contract, and prints the contract&#8217;s address to the console.\u00a0<\/span><\/p>\n<h3><span class=\"ez-toc-section\" id=\"Interacting_with_the_Smart_Contract\"><\/span><b>Interacting with the Smart Contract\u00a0<\/b><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><span style=\"font-weight: 400;\">After deploying the contract, you can interact with it using the following code:\u00a0<\/span><\/p>\n<div class=\"cards\">\n<div class=\"card-contents\">\n<i><span style=\"font-weight: 400;\">const contractAddress = &#8216;your_contract_address_here&#8217;; \/\/ Replace with your deployed contract&#8217;s address <\/span><\/i><i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">const simpleStorage = new web3.eth.Contract(abi, contractAddress); <\/span><\/i><i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i> <i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">(async () =&gt; { <\/span><\/i><i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i> <i><span style=\"font-weight: 400;\">await simpleStorage.methods.set(42).send({ from: accounts[0] }); <\/span><\/i><i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i> <i><span style=\"font-weight: 400;\">const result = await simpleStorage.methods.get().call(); <\/span><\/i><i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i> <i><span style=\"font-weight: 400;\">console.log(&#8216;Stored value is:&#8217;, result); <\/span><\/i><i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">})();\u00a0<\/span><\/i>\n<\/div>\n<\/div>\n<p><span style=\"font-weight: 400;\">This script sets a value in the contract and then retrieves it, demonstrating how to interact with a smart contract using Node.js.\u00a0<\/span><\/p>\n<h3><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span><b>Conclusion\u00a0<\/b><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><span style=\"font-weight: 400;\">In this guide, we\u2019ve explored core concepts on how to implement a simple blockchain application with Node.js and even deploy and interact with a smart contract on the blockchain. These skills provide a solid foundation for building decentralized applications (dApps) and exploring more complex blockchain concepts. Remember, building a blockchain application is just the beginning. The possibilities with blockchain technology are endless for innovation and growth.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In recent years, blockchain technology has emerged as a revolutionary force across numerous industries. The centralized nature and the security it provides has given it the boost it required to go across the globe. Countries like El Salvador have adopted Bitcoin as a legal tender across the nation. Blockchain technology is transforming the way we [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":39665,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[128],"tags":[],"class_list":["post-39662","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-node-js"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/posts\/39662","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/comments?post=39662"}],"version-history":[{"count":7,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/posts\/39662\/revisions"}],"predecessor-version":[{"id":39947,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/posts\/39662\/revisions\/39947"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/media\/39665"}],"wp:attachment":[{"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/media?parent=39662"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/categories?post=39662"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/tags?post=39662"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}