Building a Blockchain-Based Supply Chain System Using the Ethereum Stack Print

  • 0

Introduction

In this guide, we're going to walk through creating a simple blockchain-based supply chain system using the Ethereum stack. Our aim is to create a decentralized application (DApp) that allows us to track items through a supply chain.

Setting Up Your Development Environment

Firstly, ensure that your development environment is set up. You should have Node.js and npm installed. Install the Truffle suite, which provides a development environment for Ethereum, using npm:


npm install -g truffle

Also, install Ganache, a personal Ethereum blockchain which you can use to run tests, execute commands, and inspect state while controlling how the chain operates.

Setting Up Your Project

Initialize a new truffle project:


mkdir supply_chain
cd supply_chain
truffle init

This will create a basic truffle project structure. You'll mainly be working in the 'contracts' and 'migrations' directories.

Writing the Smart Contract

Create a new file in the 'contracts' directory named 'SupplyChain.sol'. Here's a simplified version of what the smart contract could look like:


pragma solidity >=0.5.16 <0.9.0;

contract SupplyChain {
// owner of the contract
address public owner;

// product counter
uint public productIndex;

// map the product counter to a product
mapping (uint => Product) public products;

// enum to maintain the product status
enum ProductStatus { Created, Paid, Shipped, Received }

// product structure
struct Product {
uint id;
string name;
uint price;
ProductStatus status;
address payable owner;
}

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

event LogProductCreated(uint indexed _productId);
event LogProductPaid(uint indexed _productId);

function createProduct(string memory _name, uint _price) public {
products[productIndex] = Product(productIndex, _name, _price, ProductStatus.Created, msg.sender);
emit LogProductCreated(productIndex);
productIndex++;
}

function payProduct(uint _productId) public payable {
Product memory _product = products[_productId];
require(msg.value >= _product.price, "Not enough funds sent");
_product.owner.transfer(msg.value);
_product.status = ProductStatus.Paid;
emit LogProductPaid(_productId);
}

// more functions could be added to change product status to shipped and received.
}

This contract creates and allows payment for products, each of which has a status indicating where it is in the supply chain.

Deploying the Smart Contract

Create a new file in the 'migrations' directory named '2_deploy_contracts.js', with the following content:


const SupplyChain = artifacts.require("SupplyChain");

module.exports = function(deployer) {
deployer.deploy(SupplyChain);
};

You can then deploy the contract to your Ganache network using:


truffle migrate --reset

**Interacting with the Smart Contract**

You can interact with the smart contract using the Truffle console:


truffle console

Then in the console, you can command as follows:


let instance = await SupplyChain.deployed()
let accounts = await web3.eth.getAccounts()

To create a product:


await instance.createProduct("Product 1", web3.utils.toWei("0.01", "ether"), { from: accounts[0] })

To pay for a product:


await instance.payProduct(0, { from: accounts[1], value: web3.utils.toWei("0.01", "ether") })

Conclusion

In this guide, we've shown how to create a basic supply chain system using Ethereum. The system allows for products to be added and paid for. Although it's a simple application, it demonstrates how blockchain can bring transparency and decentralization to the supply chain industry. You can extend this example by allowing for the status of a product to be updated, reflecting when it has been shipped and received.

Remember that this code should not be used in production - it's a simplified example and hasn't been audited for security.


Was this answer helpful?

« Back