-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdev4.sol
100 lines (84 loc) · 3.63 KB
/
dev4.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
97
98
99
100
pragma solidity ^0.7.0;
pragma experimental ABIEncoderV2;
contract SupplyChain {
struct Batch {
address owner;
string batchID;
string harvestDate;
string location;
address processor;
string packagingDate;
bool isProcessed;
}
mapping(string => Batch) public batches;
mapping(string => SupplyChainEvent[]) public batchHistory;
struct SupplyChainEvent {
string eventType;
uint timestamp;
string eventData;
}
function createBatch(string memory _batchID) public {
batches[_batchID] = Batch({
owner: msg.sender,
batchID: _batchID,
harvestDate: "",
location: "",
processor: address(0),
packagingDate: "",
isProcessed: true
});
}
function addBatchInfo(string memory _batchID, string memory _harvestDate, string memory _location) public {
require(msg.sender == batches[_batchID].owner);
batches[_batchID].harvestDate = _harvestDate;
batches[_batchID].location = _location;
bytes memory data = abi.encode(_harvestDate, " | ", _location);
string memory dataString = string(data);
addSupplyChainEvent(_batchID, "Harvest", dataString);
}
function updateHarvestDate(string memory _batchID, string memory _newHarvestDate) public {
require(msg.sender == batches[_batchID].owner);
batches[_batchID].harvestDate = _newHarvestDate;
addSupplyChainEvent(_batchID, "Harvest Date Updated", _newHarvestDate);
}
function updateTransportationInfo(string memory _batchID, string memory _carrier, string memory _route) public {
require(msg.sender == batches[_batchID].owner);
bytes memory data = abi.encode(_carrier," | ", _route);
string memory dataString = string(data);
addSupplyChainEvent(_batchID, "Transportation", dataString);
}
function updateBatchStatus(string memory _batchID, string memory _currentStatus) public {
require(msg.sender == batches[_batchID].owner);
addSupplyChainEvent(_batchID, "Status Update", _currentStatus);
}
function transferOwnership(string memory _batchID, address _newOwner) public {
require(msg.sender == batches[_batchID].owner);
batches[_batchID].owner = _newOwner;
addSupplyChainEvent(_batchID, "Ownership Transfer", addressToString(_newOwner));
}
function verifyCompliance(string memory _batchID) public view returns (bool) {
// Placeholder function, implementation would depend on specific regulations
return true;
}
function addSupplyChainEvent(string memory _batchID, string memory _eventType, string memory _eventData) internal {
batchHistory[_batchID].push(SupplyChainEvent(_eventType, block.timestamp, _eventData));
}
function getBatchHistory(string memory _batchID) view public returns (SupplyChainEvent[] memory) {
bytes memory data = abi.encode(batchHistory[_batchID]);
return abi.decode(data, (SupplyChainEvent[]));
}
function addressToString(address _address) internal pure returns (string memory) {
return toString(abi.encodePacked(_address));
}
function toString(bytes memory data) public pure returns(string memory) {
bytes memory alphabet = "0123456789abcdef";
bytes memory str = new bytes(2 + data.length * 2);
str[0] = "0";
str[1] = "x";
for (uint i = 0; i < data.length; i++) {
str[2+i*2] = alphabet[uint(uint8(data[i] >> 4))];
str[3+i*2] = alphabet[uint(uint8(data[i] & 0x0f))];
}
return string(str);
}
}