-
Notifications
You must be signed in to change notification settings - Fork 0
/
ToucanCarbonOffsetsFactory.sol
288 lines (249 loc) · 9.48 KB
/
ToucanCarbonOffsetsFactory.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
// 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;
// ============ External Imports ============
import '@openzeppelin/contracts/token/ERC721/IERC721.sol';
import '@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol';
import '@openzeppelin/contracts-upgradeable/security/PausableUpgradeable.sol';
import '@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol';
import '@openzeppelin/contracts/utils/Strings.sol';
import '@openzeppelin/contracts/proxy/beacon/BeaconProxy.sol';
// ============ Internal Imports ============
import './interfaces/IToucanContractRegistry.sol';
import './interfaces/ICarbonOffsetBatches.sol';
import './libraries/ProjectUtils.sol';
import './libraries/ProjectVintageUtils.sol';
import './libraries/Modifiers.sol';
import './ToucanCarbonOffsetsFactoryStorage.sol';
/// @notice This TCO2 factory creates project-vintage-specific ERC20 contracts for Batch-NFT fractionalization
/// Locks in received ERC721 Batch-NFTs and can mint corresponding quantity of ERC20s
/// Permissionless, anyone can deploy new ERC20s unless they do not yet exist and pid exists
contract ToucanCarbonOffsetsFactory is
ToucanCarbonOffsetsFactoryStorageV1,
OwnableUpgradeable,
PausableUpgradeable,
UUPSUpgradeable,
ProjectUtils,
ProjectVintageUtils,
Modifiers,
ToucanCarbonOffsetsFactoryStorage
{
// ----------------------------------------
// 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.2.1';
uint256 public constant VERSION_RELEASE_CANDIDATE = 1;
/// @dev divider to calculate fees in basis points
uint256 public constant bridgeFeeDivider = 1e4;
// ----------------------------------------
// Events
// ----------------------------------------
event TokenCreated(uint256 vintageTokenId, address tokenAddress);
event AddedToAllowedBridges(address account);
event RemovedFromallowedBridges(address account);
/// @custom:oz-upgrades-unsafe-allow constructor
constructor() {
_disableInitializers();
}
// ----------------------------------------
// Upgradable related functions
// ----------------------------------------
function initialize(address _contractRegistry)
external
virtual
initializer
{
__Context_init_unchained();
__Ownable_init_unchained();
__Pausable_init_unchained();
__UUPSUpgradeable_init_unchained();
contractRegistry = _contractRegistry;
}
// ----------------------------------------
// Admin functions
// ----------------------------------------
function _authorizeUpgrade(address newImplementation)
internal
virtual
override
onlyOwner
{}
/// @dev sets the Beacon that tracks the current implementation logic of the TCO2s
function setBeacon(address _beacon) external virtual onlyOwner {
beacon = _beacon;
}
/// @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();
}
/// @dev set the registry contract to be tracked
function setToucanContractRegistry(address _address)
external
virtual
onlyOwner
{
contractRegistry = _address;
}
/// @notice adds account to the allowedBridges list
/// meant to be used only for cross-chain bridging
function addToAllowedBridges(address account) external virtual onlyOwner {
bool isAllowed = allowedBridges[account];
require(!isAllowed, 'Already allowed');
allowedBridges[account] = true;
emit AddedToAllowedBridges(account);
}
/// @notice removes account from the allowedBridges list
/// meant to be used only for cross-chain bridging
function removeFromAllowedBridges(address account)
external
virtual
onlyOwner
{
bool isAllowed = allowedBridges[account];
require(isAllowed, 'Already not allowed');
allowedBridges[account] = false;
emit RemovedFromallowedBridges(account);
}
// ----------------------------------------
// Permissionless functions
// ----------------------------------------
/// @notice internal factory function to deploy new TCO2 (ERC20) contracts
/// @dev the function creates a new BeaconProxy for each TCO2
/// @param projectVintageTokenId links the vintage-specific data to the TCO2 contract
function deployNewProxy(uint256 projectVintageTokenId)
internal
virtual
whenNotPaused
{
require(beacon != address(0), 'Error: Beacon for proxy not set');
require(
!checkExistence(projectVintageTokenId),
'pvERC20 already exists'
);
checkProjectVintageTokenExists(contractRegistry, projectVintageTokenId);
/// @dev generate payload for initialize function
string memory signature = 'initialize(string,string,uint256,address)';
bytes memory payload = abi.encodeWithSignature(
signature,
'Toucan Protocol: TCO2',
'TCO2',
projectVintageTokenId,
contractRegistry
);
//slither-disable-next-line reentrancy-no-eth
BeaconProxy proxyTCO2 = new BeaconProxy(beacon, payload);
IToucanContractRegistry(contractRegistry).addERC20(address(proxyTCO2));
deployedContracts.push(address(proxyTCO2));
pvIdtoERC20[projectVintageTokenId] = address(proxyTCO2);
emit TokenCreated(projectVintageTokenId, address(proxyTCO2));
}
/// @dev Deploys a TCO2 contract based on a project vintage
/// @param projectVintageTokenId numeric tokenId from vintage in `CarbonProjectVintages`
function deployFromVintage(uint256 projectVintageTokenId)
external
virtual
whenNotPaused
{
deployNewProxy(projectVintageTokenId);
}
/// @dev Checks if same project vintage has already been deployed
function checkExistence(uint256 projectVintageTokenId)
internal
view
virtual
returns (bool)
{
if (pvIdtoERC20[projectVintageTokenId] == address(0)) {
return false;
} else {
return true;
}
}
function increaseTotalRetired(uint256 amount) external {
bool isTCO2 = IToucanContractRegistry(contractRegistry).checkERC20(
msg.sender
);
require(isTCO2 || msg.sender == owner(), 'Invalid sender');
totalRetired += amount;
}
/// @dev Returns all addresses of deployed TCO2 contracts
function getContracts() external view virtual returns (address[] memory) {
return deployedContracts;
}
function bridgeFeeReceiverAddress()
external
view
virtual
returns (address)
{
return bridgeFeeReceiver;
}
function getBridgeFeeAndBurnAmount(uint256 _quantity)
external
view
virtual
returns (uint256, uint256)
{
//slither-disable-next-line divide-before-multiply
uint256 feeAmount = (_quantity * bridgeFeePercentageInBase) /
bridgeFeeDivider;
//slither-disable-next-line divide-before-multiply
uint256 burnAmount = (feeAmount * bridgeFeeBurnPercentageInBase) /
bridgeFeeDivider;
return (feeAmount, burnAmount);
}
/// @notice Update the bridge fee percentage
/// @param _bridgeFeePercentageInBase percentage of bridge fee in base
function setBridgeFeePercentage(uint256 _bridgeFeePercentageInBase)
external
virtual
onlyOwner
{
require(
_bridgeFeePercentageInBase < bridgeFeeDivider,
'bridge fee percentage must be lower than bridge fee divider'
);
bridgeFeePercentageInBase = _bridgeFeePercentageInBase;
}
/// @notice Update the bridge fee receiver
/// @param _bridgeFeeReceiver address to transfer the fees
function setBridgeFeeReceiver(address _bridgeFeeReceiver)
external
virtual
onlyOwner
{
bridgeFeeReceiver = _bridgeFeeReceiver;
}
/// @notice Update the bridge fee burning percentage
/// @param _bridgeFeeBurnPercentageInBase percentage of bridge fee in base
function setBridgeFeeBurnPercentage(uint256 _bridgeFeeBurnPercentageInBase)
external
virtual
onlyOwner
{
require(
_bridgeFeeBurnPercentageInBase < bridgeFeeDivider,
'burn fee percentage must be lower than bridge fee divider'
);
bridgeFeeBurnPercentageInBase = _bridgeFeeBurnPercentageInBase;
}
/// @notice Update the bridge fee burn address
/// @param _bridgeFeeBurnAddress address to transfer the fees to burn
function setBridgeFeeBurnAddress(address _bridgeFeeBurnAddress)
external
virtual
onlyOwner
{
bridgeFeeBurnAddress = _bridgeFeeBurnAddress;
}
}