Building a Decentralized Supply Chain Application with QuickNode

Building a Decentralized Supply Chain Application with QuickNode

Introduction

In this tutorial, we'll walk you through the process of creating a decentralized supply chain application using QuickNode and the Ethereum blockchain. Supply chain management can greatly benefit from the transparency and immutability provided by blockchain technology. By leveraging QuickNode's infrastructure, we'll build a secure and efficient supply chain DApp.

Prerequisites

Before we begin, make sure you have the following:

  1. Node.js and npm (Node Package Manager) are installed on your computer.

  2. Basic knowledge of JavaScript and React.

  3. A QuickNode account (sign up here if you don't have one).

  4. Familiarity with Ethereum and Solidity.

Step 1: Set Up Your Development Environment

Start by creating a new directory for your project and navigating to it in your terminal:

mkdir decentralized-supply-chain-dapp
cd decentralized-supply-chain-dapp

Initialize a new Node.js project:

npm init -y

Install the necessary dependencies:

npm install web3 express

Step 2: Connect to QuickNode

To interact with the Ethereum blockchain, we'll use QuickNode's API. Sign in to your QuickNode account and obtain your API endpoint.

Create a file named web3.js in your project directory and add the following code:

const Web3 = require("web3");

const web3 = new Web3("<YOUR_QUICKNODE_API_ENDPOINT>");

module.exports = web3;

Replace <YOUR_QUICKNODE_API_ENDPOINT> with your QuickNode API endpoint.

Step 3: Create the Supply Chain Smart Contract

In this step, we'll create an Ethereum smart contract for our supply chain application. Create a file named SupplyChain.sol in your project directory with the following content:

// SupplyChain.sol
pragma solidity ^0.8.0;

contract SupplyChain {
    address public owner;
    uint256 public itemCount;
    mapping(uint256 => Item) public items;

    struct Item {
        uint256 id;
        string name;
        uint256 price;
        address owner;
        bool isSold;
    }

    constructor() {
        owner = msg.sender;
        itemCount = 0;
    }

    modifier onlyOwner() {
        require(msg.sender == owner, "Only the owner can perform this action.");
        _;
    }

    event ItemAdded(uint256 id, string name, uint256 price, address owner);
    event ItemSold(uint256 id, address buyer);

    function addItem(string memory _name, uint256 _price) public onlyOwner {
        itemCount++;
        items[itemCount] = Item(itemCount, _name, _price, msg.sender, false);
        emit ItemAdded(itemCount, _name, _price, msg.sender);
    }

    function buyItem(uint256 _id) public payable {
        require(_id <= itemCount && _id > 0, "Invalid item ID.");
        Item storage item = items[_id];
        require(!item.isSold, "Item is already sold.");
        require(msg.value >= item.price, "Insufficient funds.");

        address seller = item.owner;
        payable(seller).transfer(msg.value);
        item.owner = msg.sender;
        item.isSold = true;

        emit ItemSold(_id, msg.sender);
    }
}

This smart contract allows users to add items to the supply chain and purchase items from the supply chain.

Step 4: Compile and Deploy the Smart Contract

Next, we need to compile and deploy the smart contract. Create a file named deploy.js and add the following code:

// deploy.js
const Web3 = require("./web3");
const fs = require("fs");
const solc = require("solc");

const source = fs.readFileSync("SupplyChain.sol", "utf-8");
const compiledContract = solc.compile(source, 1);

const bytecode = compiledContract.contracts[":SupplyChain"].bytecode;
const abi = JSON.parse(compiledContract.contracts[":SupplyChain"].interface);

const deployContract = async () => {
    const accounts = await web3.eth.getAccounts();
    const gas = await web3.eth.estimateGas({ data: bytecode });

    const result = await new web3.eth.Contract(abi)
        .deploy({ data: bytecode })
        .send({ from: accounts[0], gas });

    console.log("Contract deployed to:", result.options.address);
};

deployContract();

This script compiles and deploys the SupplyChain smart contract to the Ethereum blockchain.

Step 5: Create the Frontend

We'll build a simple frontend using Express.js and React to interact with the supply chain smart contract.

Create a directory named client in your project and add the following files:

  • App.js

  • SupplyChain.js

Step 6: Implement the Supply Chain Interface

In the SupplyChain.js file, implement the user interface for the supply chain application.

// SupplyChain.js
import React, { useState, useEffect } from "react";
import web3 from "./web3";

function SupplyChain() {
    // TODO: Implement the supply chain interface
}

export default SupplyChain;

Step 7: Display the Supply Chain Interface

In the App.js file, display the supply chain interface component.

// App.js
import React from "react";
import SupplyChain from "./client/SupplyChain";

function App() {
    return (
        <div className="App">
            <h1>Decentralized Supply Chain Application</h1>
            <SupplyChain />
        </div>
    );
}

export default App;

Step 8: Testing the Supply Chain Application

To test the decentralized supply chain application, follow these steps:

  1. Start your Express.js server by running:
node server.js
  1. Run your React frontend with:
npm start

Your decentralized supply chain application will be available at http://localhost:3000. Users can interact with the supply chain smart contract, add items, and purchase items.

Conclusion

In this tutorial, you've learned how to create a decentralized supply chain application using QuickNode, Ethereum, React, and a smart contract. Blockchain technology provides transparency and security to supply chain management, ensuring the authenticity of products. You can further enhance this project by adding features like tracking the item's journey or integrating real-world data sources. Happy building!

I'd love to connect with you on Twitter | LinkedIn | Portfolio.

About QuickNode

QuickNode is building infrastructure to support the future of Web3. Since 2017, we've worked with hundreds of developers and companies, helping scale dApps and providing high-performance access to 24+ blockchains. Subscribe to our newsletter for more content like this, and stay in the loop with what's happening in Web3!‌