-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsupplyChainManagementContract.sol
96 lines (70 loc) · 3.11 KB
/
supplyChainManagementContract.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract SupplyChain{
// ################ GLOBAL VARIABLES ##################
address payable owner;
address[] listOfManufacturers;
// Struct for the details of the manufacturers
struct Manufacturers {
string name;
string description;
bool ethStakted;
}
Manufacturers public manufacturers;
mapping (address => Manufacturers) public detailsOfManufacturers;
// Struct for the details of the item/products
struct ITEM{
string name;
uint price;
string description;
address owner;
bytes32 unqProductID;
address manufacturer;
}
ITEM public products;
mapping (uint => ITEM) public productDetails; // mapping to store product details with its id
uint public productID = 1;
mapping (bytes32 => address) public prevOwners; //It will store all the previous owver to a product and we can se using it's uniqProductID
// ################ CONSTRUCTOR ##################
constructor (){
owner = payable(msg.sender);
}
// ################ MODIFIERS ##################
modifier onlyOwner() {
require(msg.sender == owner, "ONLY OWNER CAN ACESS THIS FUNCTION");
_;
}
modifier onlyManufacturer(){
require(detailsOfManufacturers[msg.sender].ethStakted == true, "ONLY MANUFACTURER CAN ACESS THIS FUNCTION");
_;
}
// ################ FUNCTIONS ##################
// BECOME MANUFACTURER FUNCITON
function becomeManufacturer(string memory _name, string memory _description) public payable
{
require(msg.value == 10 ether,"Please Stake 10 Ether to become a manufacturer");
listOfManufacturers.push(msg.sender);
Manufacturers memory _manufacturer = Manufacturers(_name, _description, true);
detailsOfManufacturers[msg.sender] = _manufacturer;
}
// ADD PRODUCT FUNCTION
function addProduct(string memory _name , uint _price, string memory _description) public onlyManufacturer {
// sha256(abi.encodePacked(block.timestamp, _name, productID));
productDetails[productID] = ITEM(_name, _price, _description, address(0) , sha256(abi.encodePacked(block.timestamp, _name, productID)) , msg.sender);
productID++;
}
// BUY FUNCTION
function buyProduct(uint _id) public payable {
require (productDetails[_id].price == msg.value, "Please pay right amount");
address payable _manufacturer = payable(productDetails[_id].manufacturer);
_manufacturer.transfer(msg.value); // this will transfer the value of the product to its manufacturer
productDetails[_id].owner = msg.sender; // this will set the new owner for the product
prevOwners[productDetails[_id].unqProductID] = msg.sender; // this will store the owners of that product
}
// Owner get the money of maintaining and veryfying all the details of the manufacturers
function withdrawRevenue() public payable onlyOwner
{
uint totalRevenue = address(this).balance;
owner.transfer(totalRevenue);
}
}