-
Notifications
You must be signed in to change notification settings - Fork 0
/
CarbonProjectVintages.sol
273 lines (235 loc) · 8.23 KB
/
CarbonProjectVintages.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
// 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 './interfaces/IToucanContractRegistry.sol';
import './interfaces/ICarbonProjectVintages.sol';
import './CarbonProjectVintagesStorage.sol';
import './libraries/ProjectUtils.sol';
import './libraries/Modifiers.sol';
/// @notice The CarbonProjectVintages contract stores vintage-specific data
/// The data is stored in structs via ERC721 tokens
/// Most contracts in the protocol query the data stored here
/// Every `vintageData` struct points to a parent `CarbonProject`
contract CarbonProjectVintages is
CarbonProjectVintagesStorage,
ICarbonProjectVintages,
ERC721Upgradeable,
OwnableUpgradeable,
PausableUpgradeable,
AccessControlUpgradeable,
UUPSUpgradeable,
Modifiers,
ProjectUtils
{
// ----------------------------------------
// 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.0';
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 ProjectVintageMinted(
address receiver,
uint256 tokenId,
uint256 projectTokenId,
uint64 startTime
);
event ProjectVintageUpdated(uint256 tokenId);
/// @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 Project Vintages',
'TOUCAN-CPV'
);
__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-vintage along with attributes/data
/// @dev vintages can be added by data-managers
function addNewVintage(address to, VintageData memory _vintageData)
external
virtual
override
onlyManagers
whenNotPaused
returns (uint256)
{
checkProjectTokenExists(contractRegistry, _vintageData.projectTokenId);
require(
pvToTokenId[_vintageData.projectTokenId][_vintageData.startTime] ==
0,
'Error: vintage already added'
);
require(
_vintageData.startTime < _vintageData.endTime,
'Error: vintage startTime must be less than endTime'
);
/// @dev Increase `projectVintageTokenCounter` and mark current Id as valid
uint256 newItemId = projectVintageTokenCounter;
unchecked {
++newItemId;
++totalSupply;
}
projectVintageTokenCounter = uint128(newItemId);
validProjectVintageIds[newItemId] = true;
_mint(to, newItemId);
vintageData[newItemId] = _vintageData;
emit ProjectVintageMinted(
to,
newItemId,
_vintageData.projectTokenId,
_vintageData.startTime
);
pvToTokenId[_vintageData.projectTokenId][
_vintageData.startTime
] = newItemId;
return newItemId;
}
/// @dev Function to check whether a projectVintageToken exists,
/// to be called by other (external) contracts
function exists(uint256 tokenId)
external
view
virtual
override
returns (bool)
{
return super._exists(tokenId);
}
/// @notice Updates and existing carbon project
/// @dev Only data-managers can update the data for correction
/// except the sensitive `projectId`
function updateProjectVintage(
uint256 tokenId,
VintageData memory _vintageData
) external virtual onlyManagers whenNotPaused {
require(_exists(tokenId), 'Project not yet minted');
// @dev very sensitive data, better update via separate function
_vintageData.projectTokenId = vintageData[tokenId].projectTokenId;
vintageData[tokenId] = _vintageData;
emit ProjectVintageUpdated(tokenId);
}
/// @dev Removes a project-vintage and corresponding data
function removeVintage(uint256 tokenId)
external
virtual
onlyManagers
whenNotPaused
{
totalSupply--;
delete vintageData[tokenId];
}
/// @dev retrieve all data from VintageData struct
function getProjectVintageDataByTokenId(uint256 tokenId)
external
view
virtual
override
returns (VintageData memory)
{
return (vintageData[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 baseURI_) external virtual onlyOwner {
baseURI = baseURI_;
}
/// @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 = vintageData[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);
}
}