Posted By : Mohd
Smart contract development holds immense potential in supply chain management. By leveraging the transparency, immutability, and automation features of blockchain, supply chain smart contracts can streamline operations, enhance trust among stakeholders, and mitigate the risk of fraud or errors.
In this blog, we'll delve into the creation of a simple supply chain smart contract using Solidity. We'll break down the code step by step, explaining each component and its role in the contract's functionality.
You may also like | Supply Chain Development with Blockchain
Let's start by examining the structure of our supply chain smart contract. The contract consists of several key components:
1. Enum for Order Status: We define an enumeration named Status to represent the different stages an order can be in"”Pending, Shipped, and Delivered. This enum helps in tracking the progress of orders within the supply chain.
enum Status {
Pending,
Shipped,
Delivered
}
2. Order Struct: Next, we define a struct named Order to encapsulate the details of each order. It includes fields such as the seller's address, the buyer's address, the price of the order, and its current status.
struct Order {
address seller;
address buyer;
uint256 price;
Status status;
}
3. Mapping for Orders: We declare a mapping named orders to store instances of the Order struct. Each order is associated with a unique order ID, which serves as the key in the mapping.
mapping(uint256 => Order) public orders;
4. State Variables: We declare a state variable"”orderCount to keep track of the total number of orders and public to allow external access, and to increment order IDs sequentially.
You may also like | Blockchain for Procurement Management | Enabling Opportunities
uint256 public orderCount;
5. Events: We define three events"”OrderCreated, OrderShipped, and OrderDelivered"”to emit notifications whenever a new order is created, shipped, or delivered, respectively. Events provide a way to log and track important contract actions.
Also, Read | NFT Integration for Remodelling Supply Chain Processes
event OrderCreated(
uint256 orderId,
address indexed seller,
address indexed buyer,
uint256 price
);
event OrderShipped(uint256 orderId);
event OrderDelivered(uint256 orderId);
1. createOrder: This function allows sellers to create new orders by specifying the buyer's address and the price of the order. It validates the input parameters, increments the orderCount, creates a new Order instance, and stores it in the mapping of the order. Additionally, it emits the OrderCreated event to notify interested parties.
function createOrder(address _buyer, uint256 _price) external {
require(_buyer != address(0), "Invalid buyer address");
require(_price > 0, "Price must be greater than zero");
orderCount++;
orders[orderCount] = Order(msg.sender, _buyer, _price, Status.Pending);
emit OrderCreated(orderCount, msg.sender, _buyer, _price);
}
2. shipOrder: Sellers use this function to mark an order as shipped once they have dispatched the goods. The function verifies that the caller is the seller of the specified order and that the order is in the "Pending" status. If the conditions are met, it updates the order status to "Shipped" and emits the OrderShipped event.
function shipOrder(uint256 _orderId) external {
require(_orderId > 0 && _orderId <= orderCount, "Invalid order ID");
require(
msg.sender == orders[_orderId].seller,
"Only seller can ship the order"
);
require(
orders[_orderId].status == Status.Pending,
"Order has already been shipped or delivered"
);
orders[_orderId].status = Status.Shipped;
emit OrderShipped(_orderId);
}
3. deliverOrder: Buyers invoke this function to confirm the delivery of an order. Similar to the shipOrder function, it checks the caller's address and the current status of the order. If the conditions are satisfied, it updates the order status to "Delivered" and emits the OrderDelivered event.
function deliverOrder(uint256 _orderId) external {
require(_orderId > 0 && _orderId <= orderCount, "Invalid order ID");
require(
msg.sender == orders[_orderId].buyer,
"Only buyer can confirm delivery"
);
require(
orders[_orderId].status == Status.Shipped,
"Order has not been shipped yet"
);
orders[_orderId].status = Status.Delivered;
emit OrderDelivered(_orderId);
}
You may also like | Reimagining Supply Chain Management with NFTs
In conclusion, we have explored the development of a supply chain smart contract using Solidity. By employing blockchain technology and smart contracts, supply chain management processes can be made more efficient, transparent, and secure. The code provided in this article serves as a foundation for building more complex supply chain solutions, incorporating additional features such as payment processing, product tracking, and inventory management.
Here is the whole code for the smart contract:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract SupplyChain {
enum Status {
Pending,
Shipped,
Delivered
}
struct Order {
address seller;
address buyer;
uint256 price;
Status status;
}
mapping(uint256 => Order) public orders;
uint256 public orderCount;
event OrderCreated(
uint256 orderId,
address indexed seller,
address indexed buyer,
uint256 price
);
event OrderShipped(uint256 orderId);
event OrderDelivered(uint256 orderId);
function createOrder(address _buyer, uint256 _price) external {
require(_buyer != address(0), "Invalid buyer address");
require(_price > 0, "Price must be greater than zero");
orderCount++;
orders[orderCount] = Order(msg.sender, _buyer, _price, Status.Pending);
emit OrderCreated(orderCount, msg.sender, _buyer, _price);
}
function shipOrder(uint256 _orderId) external {
require(_orderId > 0 && _orderId <= orderCount, "Invalid order ID");
require(
msg.sender == orders[_orderId].seller,
"Only seller can ship the order"
);
require(
orders[_orderId].status == Status.Pending,
"Order has already been shipped or delivered"
);
orders[_orderId].status = Status.Shipped;
emit OrderShipped(_orderId);
}
function deliverOrder(uint256 _orderId) external {
require(_orderId > 0 && _orderId <= orderCount, "Invalid order ID");
require(
msg.sender == orders[_orderId].buyer,
"Only buyer can confirm delivery"
);
require(
orders[_orderId].status == Status.Shipped,
"Order has not been shipped yet"
);
orders[_orderId].status = Status.Delivered;
emit OrderDelivered(_orderId);
}
}
For more information about solutions development for supply chain management powered by blockchain and smart contracts, connect with our skilled smart contract developers.
November 8, 2024 at 06:16 pm
Your comment is awaiting moderation.