-
Notifications
You must be signed in to change notification settings - Fork 0
/
CarbonProjects.sol
337 lines (295 loc) · 10.8 KB
/
CarbonProjects.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// SPDX-FileCopyrightText: 2021 Toucan Labs
//
// SPDX-License-Identifier: UNLICENSED
// If you encounter a vulnerability or an issue, please contact <[email protected]> or visit security.toucan.earth
pragma solidity 0.8.14;
import '@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol';
import '@openzeppelin/contracts-upgradeable/token/ERC721/ERC721Upgradeable.sol';
import '@openzeppelin/contracts-upgradeable/security/PausableUpgradeable.sol';
import '@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol';
import '@openzeppelin/contracts-upgradeable/access/AccessControlUpgradeable.sol';
import './CarbonProjectsStorage.sol';
import './interfaces/ICarbonProjects.sol';
import './libraries/Modifiers.sol';
/// @notice The CarbonProjects contract stores carbon project-specific data
/// The data is stored in structs via ERC721 tokens
/// Most contracts in the protocol query the data stored here
/// The attributes in the Project-NFTs are constant over all vintages of the project
/// @dev Each project can have up to n vintages, with data stored in the
/// `CarbonProjectVintages` contract. `vintageTokenId`s are mapped to `projectTokenId`s
/// via `pvToTokenId` in the vintage contract.
contract CarbonProjects is
ICarbonProjects,
CarbonProjectsStorage,
ERC721Upgradeable,
OwnableUpgradeable,
PausableUpgradeable,
Modifiers,
AccessControlUpgradeable,
UUPSUpgradeable
{
// ----------------------------------------
// Constants
// ----------------------------------------
/// @dev Version-related parameters. VERSION keeps track of production
/// releases. VERSION_RELEASE_CANDIDATE keeps track of iterations
/// of a VERSION in our staging environment.
string public constant VERSION = '1.1.1';
uint256 public constant VERSION_RELEASE_CANDIDATE = 1;
/// @dev All roles related to accessing this contract
bytes32 public constant MANAGER_ROLE = keccak256('MANAGER_ROLE');
// ----------------------------------------
// Events
// ----------------------------------------
event ProjectMinted(address receiver, uint256 tokenId);
event ProjectUpdated(uint256 tokenId);
event ProjectIdUpdated(uint256 tokenId);
event BeneficiaryUpdated(
uint256 indexed tokenId,
address oldBeneficiary,
address newBeneficiary
);
/// @custom:oz-upgrades-unsafe-allow constructor
constructor() {
_disableInitializers();
}
// ----------------------------------------
// Upgradable related functions
// ----------------------------------------
function initialize() external virtual initializer {
__Context_init_unchained();
__ERC721_init_unchained(
'Toucan Protocol: Carbon Projects',
'TOUCAN-CP'
);
__Ownable_init_unchained();
__Pausable_init_unchained();
__AccessControl_init_unchained();
__UUPSUpgradeable_init_unchained();
/// @dev granting the deployer==owner the rights to grant other roles
_grantRole(DEFAULT_ADMIN_ROLE, msg.sender);
}
function _authorizeUpgrade(address newImplementation)
internal
virtual
override
onlyOwner
{}
// ------------------------
// Admin functions
// ------------------------
/// @dev modifier that only lets the contract's owner and elected managers add/update/remove project data
modifier onlyManagers() {
require(
hasRole(MANAGER_ROLE, msg.sender) || owner() == msg.sender,
'Caller is not authorized'
);
_;
}
/// @notice Emergency function to disable contract's core functionality
/// @dev wraps _pause(), only Admin
function pause() external virtual onlyBy(contractRegistry, owner()) {
_pause();
}
/// @dev unpause the system, wraps _unpause(), only Admin
function unpause() external virtual onlyBy(contractRegistry, owner()) {
_unpause();
}
function setToucanContractRegistry(address _address)
external
virtual
onlyOwner
{
contractRegistry = _address;
}
/// @notice Adds a new carbon project along with attributes/data
/// @dev Projects can be added by data-managers
function addNewProject(
address to,
string memory projectId,
string memory standard,
string memory methodology,
string memory region,
string memory storageMethod,
string memory method,
string memory emissionType,
string memory category,
string memory uri,
address beneficiary
) external virtual override onlyManagers whenNotPaused returns (uint256) {
require(!strcmp(projectId, ''), 'ProjectId cannot be empty');
require(!projectIds[projectId], 'Project already exists');
projectIds[projectId] = true;
uint256 newItemId = projectTokenCounter;
unchecked {
++newItemId;
++totalSupply;
}
projectTokenCounter = uint128(newItemId);
validProjectTokenIds[newItemId] = true;
_mint(to, newItemId);
projectData[newItemId].projectId = projectId;
projectData[newItemId].standard = standard;
projectData[newItemId].methodology = methodology;
projectData[newItemId].region = region;
projectData[newItemId].storageMethod = storageMethod;
projectData[newItemId].method = method;
projectData[newItemId].emissionType = emissionType;
projectData[newItemId].category = category;
projectData[newItemId].uri = uri;
projectData[newItemId].beneficiary = beneficiary;
emit ProjectMinted(to, newItemId);
pidToTokenId[projectId] = newItemId;
return newItemId;
}
/// @notice Updates and existing carbon project
/// @dev Projects can be updated by data-managers
function updateProject(
uint256 tokenId,
string memory newStandard,
string memory newMethodology,
string memory newRegion,
string memory newStorageMethod,
string memory newMethod,
string memory newEmissionType,
string memory newCategory,
string memory newUri,
address beneficiary
) external virtual onlyManagers whenNotPaused {
require(_exists(tokenId), 'Project not yet minted');
projectData[tokenId].standard = newStandard;
projectData[tokenId].methodology = newMethodology;
projectData[tokenId].region = newRegion;
projectData[tokenId].storageMethod = newStorageMethod;
projectData[tokenId].method = newMethod;
projectData[tokenId].emissionType = newEmissionType;
projectData[tokenId].category = newCategory;
projectData[tokenId].uri = newUri;
projectData[tokenId].beneficiary = beneficiary;
emit ProjectUpdated(tokenId);
}
/// @dev Projects and their projectId's must be unique, changing them must be handled carefully
function updateProjectId(uint256 tokenId, string memory newProjectId)
external
virtual
onlyManagers
whenNotPaused
{
require(_exists(tokenId), 'Project not yet minted');
if (bytes(newProjectId).length != 0) {
require(
projectIds[newProjectId] == false,
'Cant change current projectId to an existing one'
);
}
string memory oldProjectId = projectData[tokenId].projectId;
projectIds[oldProjectId] = false;
projectData[tokenId].projectId = newProjectId;
projectIds[newProjectId] = true;
emit ProjectIdUpdated(tokenId);
}
/// @notice Updates the project beneficiary
function updateBeneficiary(uint256 tokenId, address beneficiary)
external
virtual
onlyManagers
whenNotPaused
{
require(_exists(tokenId), 'Project not yet minted');
address oldBeneficiary = projectData[tokenId].beneficiary;
projectData[tokenId].beneficiary = beneficiary;
emit BeneficiaryUpdated(tokenId, oldBeneficiary, beneficiary);
}
/// @dev Removes a project and corresponding data, sets projectTokenId invalid
function removeProject(uint256 projectTokenId)
external
virtual
onlyManagers
whenNotPaused
{
delete projectData[projectTokenId];
/// @dev set projectTokenId to invalid
totalSupply--;
validProjectTokenIds[projectTokenId] = false;
}
/// @dev Function used by the utility function `checkProjectTokenExists`
function isValidProjectTokenId(uint256 projectTokenId)
external
view
virtual
override
returns (bool)
{
return validProjectTokenIds[projectTokenId];
}
/// @dev retrieve all data from ProjectData struct
function getProjectDataByTokenId(uint256 tokenId)
external
view
virtual
override
returns (ProjectData memory)
{
return (projectData[tokenId]);
}
function supportsInterface(bytes4 interfaceId)
public
view
virtual
override(
AccessControlUpgradeable,
ERC721Upgradeable,
IERC165Upgradeable
)
returns (bool)
{
return
interfaceId == type(IAccessControlUpgradeable).interfaceId ||
ERC721Upgradeable.supportsInterface(interfaceId);
}
function _baseURI() internal view virtual override returns (string memory) {
return baseURI;
}
function setBaseURI(string memory gateway) external virtual onlyOwner {
baseURI = gateway;
}
/// @dev Returns the Uniform Resource Identifier (URI) for `tokenId` token.
/// based on the ERC721URIStorage implementation
function tokenURI(uint256 tokenId)
public
view
virtual
override
returns (string memory)
{
require(
_exists(tokenId),
'ERC721URIStorage: URI query for nonexistent token'
);
string memory uri = projectData[tokenId].uri;
string memory base = _baseURI();
// If there is no base URI, return the token URI.
if (bytes(base).length == 0) {
return uri;
}
// If both are set, concatenate the baseURI and tokenURI (via abi.encodePacked).
if (bytes(uri).length > 0) {
return string(abi.encodePacked(base, uri));
}
return super.tokenURI(tokenId);
}
function memcmp(bytes memory a, bytes memory b)
internal
pure
returns (bool)
{
return (a.length == b.length) && (keccak256(a) == keccak256(b));
}
function strcmp(string memory a, string memory b)
internal
pure
returns (bool)
{
return memcmp(bytes(a), bytes(b));
}
}