Skip to content

Instantly share code, notes, and snippets.

@dhruvjhamb1
Created May 28, 2023 14:36
Show Gist options
  • Save dhruvjhamb1/f457b2b3f88da6b1c04b84c7fa0920f8 to your computer and use it in GitHub Desktop.
Save dhruvjhamb1/f457b2b3f88da6b1c04b84c7fa0920f8 to your computer and use it in GitHub Desktop.
pragma solidity ^0.8.0;
contract SupplyChain {
enum ProductStatus { Created, Shipped, Delivered }
struct Product {
uint256 id;
string name;
uint256 price;
address seller;
address buyer;
ProductStatus status;
}
uint256 public productCount;
mapping(uint256 => Product) public products;
event ProductCreated(uint256 id, string name, uint256 price, address seller);
event ProductShip(uint256 id);
event ProductDelivered(uint256 id);
function createProduct(string memory _name, uint256 _price) public {
productCount++;
products[productCount] = Product(productCount, _name, _price, msg.sender, address(0), ProductStatus.Created);
emit ProductCreated(productCount, _name, _price, msg.sender);
}
function shipProduct(uint256 _id) public {
Product storage product = products[_id];
require(product.status == ProductStatus.Created, "Product is not in the Created status");
require(product.seller == msg.sender, "Only the seller can ship the product");
product.status = ProductStatus.Ship;
emit ProductShip(_id);
}
function deliverProduct(uint256 _id) public {
Product storage product = products[_id];
require(product.status == ProductStatus.Shipped, "Product is not in the Shipped status");
require(product.buyer == msg.sender, "Only the buyer can deliver the product");
product.status = ProductStatus.Delivered;
emit ProductDelivered(_id);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment