-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLowCarbonProject.sol
77 lines (63 loc) · 2.77 KB
/
LowCarbonProject.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract CarbonProject {
enum VerificationStatus { Pending, Verified, Failed }
struct Project {
uint256 id;
address owner;
string serialNumber;
uint256 carbonAmount;
VerificationStatus verificationStatus;
bool carbonCreditRecorded;
}
uint256 private nextProjectId = 1;
mapping(uint256 => Project) public projects;
address private admin;
event ProjectCreated(uint256 indexed id, address owner, string serialNumber, uint256 carbonAmount);
event CarbonCreditVerified(uint256 indexed projectId, VerificationStatus status);
event CarbonCreditRecorded(uint256 indexed projectId, uint256 carbonAmount);
constructor() {
admin = msg.sender;
}
modifier onlyAdmin() {
require(msg.sender == admin, "Only admin can perform this action");
_;
}
modifier projectExists(uint256 projectId) {
require(projectId < nextProjectId && projects[projectId].id != 0, "Project does not exist");
_;
}
function isValidSerialNumberFormat(string memory serialNumber) internal pure returns (bool) {
bytes memory serialBytes = bytes(serialNumber);
uint256 expectedSegments = 12;
uint256 segmentCount = 1;
for(uint256 i = 0; i < serialBytes.length; i++) {
if (serialBytes[i] == '-') {
segmentCount++;
}
}
return segmentCount == expectedSegments;
}
function createProject(string memory serialNumber, uint256 carbonAmount) public {
require(isValidSerialNumberFormat(serialNumber), "Invalid serial number format");
uint256 projectId = nextProjectId++;
projects[projectId] = Project(projectId, msg.sender, serialNumber, carbonAmount, VerificationStatus.Pending, false);
emit ProjectCreated(projectId, msg.sender, serialNumber, carbonAmount);
}
// Function to verify a carbon credit; if verified, record it
function verifyCarbonCredit(uint256 projectId, bool isVerified) public onlyAdmin projectExists(projectId) {
Project storage project = projects[projectId];
project.verificationStatus = isVerified ? VerificationStatus.Verified : VerificationStatus.Failed;
emit CarbonCreditVerified(projectId, project.verificationStatus);
if (isVerified) {
recordCarbonCredit(projectId);
}
}
// Internal function to record a verified carbon credit
function recordCarbonCredit(uint256 projectId) internal {
Project storage project = projects[projectId];
require(!project.carbonCreditRecorded, "Carbon credit already recorded");
project.carbonCreditRecorded = true;
emit CarbonCreditRecorded(projectId, project.carbonAmount);
}
}