Created
December 18, 2022 22:54
-
-
Save 1finalyearproject/2f059ecea504105f728356a690720844 to your computer and use it in GitHub Desktop.
Blockchain-based Supply Chain Management
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.7.0; | |
import "https://github.com/OpenZeppelin/openzeppelin-solidity/contracts/SafeMath.sol"; | |
import "https://github.com/OpenZeppelin/openzeppelin-solidity/contracts/Ownable.sol"; | |
// Source: https://www.final-yearproject.com/ | |
contract SupplyChain is Ownable { | |
using SafeMath for uint256; | |
// Structs | |
struct Product { | |
uint256 id; | |
string name; | |
string description; | |
uint256 price; | |
address owner; | |
bool available; | |
uint256 createdAt; | |
} | |
// Mapping | |
mapping(uint256 => Product) public products; | |
uint256 public productCount; | |
// Events | |
event ProductCreated(uint256 id, string name, string description, uint256 price, address owner, uint256 createdAt); | |
event ProductUpdated(uint256 id, string name, string description, uint256 price, address owner); | |
event ProductSold(uint256 id, address from, address to); | |
// Functions | |
function createProduct(string memory _name, string memory _description, uint256 _price) public { | |
require(_name.length > 0 && _description.length > 0 && _price > 0, "Invalid product details"); | |
require(msg.sender == owner, "Only owner can create products"); | |
productCount = productCount.add(1); | |
uint256 id = productCount; | |
products[id] = Product(id, _name, _description, _price, msg.sender, true, now); | |
emit ProductCreated(id, _name, _description, _price, msg.sender, now); | |
} | |
function updateProduct(uint256 _id, string memory _name, string memory _description, uint256 _price) public { | |
require(_id > 0 && _name.length > 0 && _description.length > 0 && _price > 0, "Invalid product details"); | |
require(products[_id].owner == msg.sender, "Only owner can update product"); | |
products[_id].name = _name; | |
products[_id].description = _description; | |
products[_id].price = _price; | |
emit ProductUpdated(_id, _name, _description, _price, msg.sender); | |
} | |
function sellProduct(uint256 _id, address _to) public { | |
require(_id > 0 && _to != address(0), "Invalid product or buyer address"); | |
require(products[_id].owner == msg.sender && products[_id].available, "Product is not available for sale"); | |
products[_id].owner = _to; | |
products[_id].available = false; | |
emit ProductSold(_id, msg.sender, _to); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This sample code defines a Solidity contract for a supply chain management system on the Ethereum platform. The contract includes functions for creating, updating, and selling products, as well as events for logging these actions. It also includes a mapping to store the details of each product, as well as a counter to keep track of the number of products in the supply chain.