-
Notifications
You must be signed in to change notification settings - Fork 20
/
CMTAT.sol
322 lines (292 loc) · 8.61 KB
/
CMTAT.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
//SPDX-License-Identifier: MPL-2.0
pragma solidity ^0.8.17;
// required OZ imports here
import "../openzeppelin-contracts-upgradeable/contracts/proxy/utils/Initializable.sol";
import "../openzeppelin-contracts-upgradeable/contracts/utils/ContextUpgradeable.sol";
import "./modules/BaseModule.sol";
import "./modules/AuthorizationModule.sol";
import "./modules/BurnModule.sol";
import "./modules/MintModule.sol";
import "./modules/BurnModule.sol";
import "./modules/EnforcementModule.sol";
import "./modules/PauseModule.sol";
import "./modules/ValidationModule.sol";
import "./modules/MetaTxModule.sol";
import "./modules/SnapshotModule.sol";
import "./interfaces/IRuleEngine.sol";
contract CMTAT is
Initializable,
ContextUpgradeable,
BaseModule,
AuthorizationModule,
PauseModule,
MintModule,
BurnModule,
EnforcementModule,
ValidationModule,
MetaTxModule,
SnapshotModule
{
uint8 constant TRANSFER_OK = 0;
string constant TEXT_TRANSFER_OK = "No restriction";
constructor(
address forwarder
) MetaTxModule(forwarder) {
}
function initialize(
address owner,
string memory name,
string memory symbol,
string memory tokenId,
string memory terms
) public initializer {
__CMTAT_init(owner, name, symbol, tokenId, terms);
}
/**
* @dev Grants `DEFAULT_ADMIN_ROLE`, `MINTER_ROLE` and `PAUSER_ROLE` to the
* account that deploys the contract.
*
* See {ERC20-constructor}.
*/
function __CMTAT_init(
address owner,
string memory name,
string memory symbol,
string memory tokenId,
string memory terms
) internal onlyInitializing {
__Context_init_unchained();
__Base_init_unchained(0, tokenId, terms);
__AccessControl_init_unchained();
__ERC20_init_unchained(name, symbol);
__Pausable_init_unchained();
__Enforcement_init_unchained();
__Snapshot_init_unchained();
__CMTAT_init_unchained(owner);
}
function __CMTAT_init_unchained(address owner) internal onlyInitializing {
_setupRole(DEFAULT_ADMIN_ROLE, owner);
_setupRole(ENFORCER_ROLE, owner);
_setupRole(MINTER_ROLE, owner);
_setupRole(BURNER_ROLE, owner);
_setupRole(PAUSER_ROLE, owner);
_setupRole(SNAPSHOOTER_ROLE, owner);
}
/**
* @dev Creates `amount` new tokens for `to`.
*
* See {ERC20-_mint}.
*
* Requirements:
*
* - the caller must have the `MINTER_ROLE`.
*/
function mint(address to, uint256 amount) public onlyRole(MINTER_ROLE) {
_mint(to, amount);
emit Mint(to, amount);
}
/**
* @dev Destroys `amount` tokens from `account`, deducting from the caller's
* allowance.
*
* See {ERC20-_burn} and {ERC20-allowance}.
*
* Requirements:
*
* - the caller must have allowance for ``accounts``'s tokens of at least
* `amount`.
*/
function burnFrom(address account, uint256 amount)
public
onlyRole(BURNER_ROLE)
{
uint256 currentAllowance = allowance(account, _msgSender());
require(
currentAllowance >= amount,
"CMTAT: burn amount exceeds allowance"
);
unchecked {
_approve(account, _msgSender(), currentAllowance - amount);
}
_burn(account, amount);
emit Burn(account, amount);
}
/**
* @dev Pauses all token transfers.
*
* See {ERC20Pausable} and {Pausable-_pause}.
*
* Requirements:
*
* - the caller must have the `PAUSER_ROLE`.
*/
function pause() public onlyRole(PAUSER_ROLE) {
_pause();
}
/**
* @dev Unpauses all token transfers.
*
* See {ERC20Pausable} and {Pausable-_unpause}.
*
* Requirements:
*
* - the caller must have the `PAUSER_ROLE`.
*/
function unpause() public onlyRole(PAUSER_ROLE) {
_unpause();
}
/**
* @dev Freezes an address.
*
*/
function freeze(address account)
public
onlyRole(ENFORCER_ROLE)
returns (bool)
{
return _freeze(account);
}
/**
* @dev Unfreezes an address.
*
*/
function unfreeze(address account)
public
onlyRole(ENFORCER_ROLE)
returns (bool)
{
return _unfreeze(account);
}
function decimals()
public
view
virtual
override(ERC20Upgradeable, BaseModule)
returns (uint8)
{
return super.decimals();
}
function transferFrom(
address sender,
address recipient,
uint256 amount
) public virtual override(ERC20Upgradeable, BaseModule) returns (bool) {
return super.transferFrom(sender, recipient, amount);
}
/**
* @dev ERC1404 check if _value token can be transferred from _from to _to
* @param from address The address which you want to send tokens from
* @param to address The address which you want to transfer to
* @param amount uint256 the amount of tokens to be transferred
* @return code of the rejection reason
*/
function detectTransferRestriction(
address from,
address to,
uint256 amount
) public view returns (uint8 code) {
if (paused()) {
return TRANSFER_REJECTED_PAUSED;
} else if (frozen(from)) {
return TRANSFER_REJECTED_FROZEN;
} else if (address(ruleEngine) != address(0)) {
return _detectTransferRestriction(from, to, amount);
}
return TRANSFER_OK;
}
/**
* @dev ERC1404 returns the human readable explaination corresponding to the error code returned by detectTransferRestriction
* @param restrictionCode The error code returned by detectTransferRestriction
* @return message The human readable explaination corresponding to the error code returned by detectTransferRestriction
*/
function messageForTransferRestriction(uint8 restrictionCode)
external
view
returns (string memory message)
{
if (restrictionCode == TRANSFER_OK) {
return TEXT_TRANSFER_OK;
} else if (restrictionCode == TRANSFER_REJECTED_PAUSED) {
return TEXT_TRANSFER_REJECTED_PAUSED;
} else if (restrictionCode == TRANSFER_REJECTED_FROZEN) {
return TEXT_TRANSFER_REJECTED_FROZEN;
} else if (address(ruleEngine) != address(0)) {
return _messageForTransferRestriction(restrictionCode);
}
}
function scheduleSnapshot(uint256 time)
public
onlyRole(SNAPSHOOTER_ROLE)
returns (uint256)
{
return _scheduleSnapshot(time);
}
function rescheduleSnapshot(uint256 oldTime, uint256 newTime)
public
onlyRole(SNAPSHOOTER_ROLE)
returns (uint256)
{
return _rescheduleSnapshot(oldTime, newTime);
}
function unscheduleSnapshot(uint256 time)
public
onlyRole(SNAPSHOOTER_ROLE)
returns (uint256)
{
return _unscheduleSnapshot(time);
}
function setTokenId(string memory tokenId_)
public
onlyRole(DEFAULT_ADMIN_ROLE)
{
tokenId = tokenId_;
}
function setTerms(string memory terms_)
public
onlyRole(DEFAULT_ADMIN_ROLE)
{
terms = terms_;
}
function kill() public onlyRole(DEFAULT_ADMIN_ROLE) {
selfdestruct(payable(_msgSender()));
}
function setRuleEngine(IRuleEngine ruleEngine_)
external
onlyRole(DEFAULT_ADMIN_ROLE)
{
ruleEngine = ruleEngine_;
emit RuleEngineSet(address(ruleEngine_));
}
function _beforeTokenTransfer(
address from,
address to,
uint256 amount
) internal override(SnapshotModule, ERC20Upgradeable) {
require(!paused(), "CMTAT: token transfer while paused");
require(!frozen(from), "CMTAT: token transfer while frozen");
super._beforeTokenTransfer(from, to, amount);
if (address(ruleEngine) != address(0)) {
require(
_validateTransfer(from, to, amount),
"CMTAT: transfer rejected by validation module"
);
}
}
function _msgSender()
internal
view
override(ERC2771ContextUpgradeable, ContextUpgradeable)
returns (address sender)
{
return super._msgSender();
}
function _msgData()
internal
view
override(ERC2771ContextUpgradeable, ContextUpgradeable)
returns (bytes calldata)
{
return super._msgData();
}
uint256[50] private __gap;
}