-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRuleConditionalTransfer.sol
401 lines (375 loc) · 12.8 KB
/
RuleConditionalTransfer.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
// SPDX-License-Identifier: MPL-2.0
pragma solidity ^0.8.20;
import "OZ/access/AccessControl.sol";
import "../../interfaces/IRuleOperation.sol";
import "./../../modules/MetaTxModuleStandalone.sol";
import "./abstract/RuleConditionalTransferInvariantStorage.sol";
import "./abstract/RuleConditionalTransferOperator.sol";
import "../validation/abstract/RuleValidateTransfer.sol";
import "CMTAT/interfaces/engine/IRuleEngine.sol";
/**
* @title RuleConditionalTransfer
*/
contract RuleConditionalTransfer is
RuleValidateTransfer,
IRuleOperation,
RuleConditionalTransferOperator,
MetaTxModuleStandalone
{
/**
* @param admin Address of the contract (Access Control)
* @param forwarderIrrevocable Address of the forwarder, required for the gasless support
*/
constructor(
address admin,
address forwarderIrrevocable,
IRuleEngine ruleEngineContract,
OPTION memory options_
) MetaTxModuleStandalone(forwarderIrrevocable) {
if (admin == address(0)) {
revert RuleConditionalTransfer_AdminWithAddressZeroNotAllowed();
}
_grantRole(DEFAULT_ADMIN_ROLE, admin);
if (address(ruleEngineContract) != address(0x0)) {
_grantRole(RULE_ENGINE_CONTRACT_ROLE, address(ruleEngineContract));
}
options = options_;
}
/*//////////////////////////////////////////////////////////////
PUBLIC/EXTERNAL FUNCTIONS
//////////////////////////////////////////////////////////////*/
/**
* @notice function called by the RuleEngine
* @dev Returns true if the transfer is valid, and false otherwise.
* Add access control with the RuleEngine
*/
function operateOnTransfer(
address _from,
address _to,
uint256 _amount
)
public
override
onlyRole(RULE_ENGINE_CONTRACT_ROLE)
returns (bool isValid)
{
if (_validateTransfer(_from, _to)) {
return true;
} else {
bytes32 key = keccak256(abi.encode(_from, _to, _amount));
if (_validateApproval(key)) {
_updateProcessedTransfer(key);
return true;
} else {
return false;
}
}
}
/**
* @notice Create a request of transfer for yourselves
* @param to recipient of tokens
* @param value amount of tokens to transfer
*/
function createTransferRequest(address to, uint256 value) public {
// WAIT => Will set a new delay to approve
// APPROVED => will overwrite previous status
// DENIED => reject
address from = _msgSender();
bytes32 key = keccak256(abi.encode(from, to, value));
if (transferRequests[key].status == STATUS.DENIED) {
revert RuleConditionalTransfer_TransferDenied();
}
if (_checkRequestStatus(key)) {
uint256 requestIdLocal = requestId;
TransferRequest memory newTransferApproval = TransferRequest({
key: key,
id: requestIdLocal,
keyElement: TransferRequestKeyElement({
from: from,
to: to,
value: value
}),
askTime: block.timestamp,
maxTime: 0,
status: STATUS.WAIT
});
transferRequests[key] = newTransferApproval;
IdToKey[requestIdLocal] = key;
emit transferWaiting(key, from, to, value, requestId);
++requestId;
} else {
// Overwrite previous approval
transferRequests[key].askTime = block.timestamp;
transferRequests[key].status = STATUS.WAIT;
emit transferWaiting(
key,
from,
to,
value,
transferRequests[key].id
);
}
}
/**
* @notice Batch version of {createTransferRequest}
*/
function createTransferRequestBatch(
address[] memory tos,
uint256[] memory values
) public {
if (tos.length == 0) {
revert RuleConditionalTransfer_EmptyArray();
}
if (tos.length != values.length) {
revert RuleConditionalTransfer_InvalidLengthArray();
}
for (uint256 i = 0; i < tos.length; ++i) {
createTransferRequest(tos[i], values[i]);
}
}
/**
* @notice allow a token holder to cancel/reset his own request
*/
function cancelTransferRequest(uint256 requestId_) public {
_cancelTransferRequest(requestId_);
}
/**
* @notice allow a token holder to cancel/reset his own request
*/
function cancelTransferRequestBatch(uint256[] memory requestIds) public {
if (requestIds.length == 0) {
revert RuleConditionalTransfer_EmptyArray();
}
// Check id validity before performing actions
for (uint256 i = 0; i < requestIds.length; ++i) {
if (requestIds[i] + 1 > requestId) {
revert RuleConditionalTransfer_InvalidId();
}
}
for (uint256 i = 0; i < requestIds.length; ++i) {
_cancelTransferRequest(requestIds[i]);
}
}
function getRequestTrade(
address from,
address to,
uint256 value
) public view returns (TransferRequest memory) {
bytes32 key = keccak256(abi.encode(from, to, value));
return transferRequests[key];
}
/**
* @notice get Trade by status
* @param _targetStatus The status of the transactions you want to retrieve
* @return array with corresponding transactions
*/
function getRequestByStatus(
STATUS _targetStatus
) public view returns (TransferRequest[] memory) {
uint totalRequestCount = requestId;
uint requestCount = 0;
uint currentIndex = 0;
// We count the number of requests matching the criteria
for (uint i = 0; i < totalRequestCount; ++i) {
if (transferRequests[IdToKey[i]].status == _targetStatus) {
requestCount += 1;
}
}
// We reserve the memory to store the trade
TransferRequest[] memory requests = new TransferRequest[](requestCount);
// We create an array with the list of trade
for (uint i = 0; i < totalRequestCount; ++i) {
if (transferRequests[IdToKey[i]].status == _targetStatus) {
//uint currentId = i + 1;
TransferRequest memory currentRequest = transferRequests[
IdToKey[i]
];
requests[currentIndex] = currentRequest;
currentIndex += 1;
}
}
return requests;
}
/**
* @notice Check if the transfer is valid
* @param _from the origin address
* @param _to the destination address
* @return The restricion code or REJECTED_CODE_BASE.TRANSFER_OK
**/
function detectTransferRestriction(
address _from,
address _to,
uint256 _amount
) public view override returns (uint8) {
// No need of approval if from and to are in the whitelist
if (_validateTransfer(_from, _to)) {
return uint8(REJECTED_CODE_BASE.TRANSFER_OK);
}
bytes32 key = keccak256(abi.encode(_from, _to, _amount));
if (_validateApproval(key)) {
return uint8(REJECTED_CODE_BASE.TRANSFER_OK);
} else {
return CODE_TRANSFER_REQUEST_NOT_APPROVED;
}
}
/**
* @notice To know if the restriction code is valid for this rule or not.
* @param _restrictionCode The target restriction code
* @return true if the restriction code is known, false otherwise
**/
function canReturnTransferRestrictionCode(
uint8 _restrictionCode
) external pure override returns (bool) {
return _restrictionCode == CODE_TRANSFER_REQUEST_NOT_APPROVED;
}
/**
* @notice Return the corresponding message
* @param _restrictionCode The target restriction code
* @return true if the transfer is valid, false otherwise
**/
function messageForTransferRestriction(
uint8 _restrictionCode
) external pure override returns (string memory) {
if (_restrictionCode == CODE_TRANSFER_REQUEST_NOT_APPROVED) {
return TEXT_TRANSFER_REQUEST_NOT_APPROVED;
} else {
return TEXT_CODE_NOT_FOUND;
}
}
/*//////////////////////////////////////////////////////////////
INTERNAL FUNCTIONS
//////////////////////////////////////////////////////////////*/
function _validateTransfer(
address _from,
address _to
) internal view returns (bool) {
// No need of approval if from and to are in the whitelist
if (address(whitelistConditionalTransfer) != address(0)) {
if (
whitelistConditionalTransfer.addressIsListed(_from) &&
whitelistConditionalTransfer.addressIsListed(_to)
) {
return true;
}
}
// Mint & Burn
if (_validateBurnMint(_from, _to)) {
return true;
}
return false;
}
function _cancelTransferRequest(uint256 requestId_) internal {
if (requestId_ + 1 > requestId) {
revert RuleConditionalTransfer_InvalidId();
}
bytes32 key = IdToKey[requestId_];
// Check Sender
if (transferRequests[key].keyElement.from != _msgSender()) {
revert RuleConditionalTransfer_InvalidSender();
}
// Check status
if (
transferRequests[key].status != STATUS.WAIT &&
transferRequests[key].status != STATUS.APPROVED
) {
revert RuleConditionalTransfer_Wrong_Status();
}
_resetRequestStatus(key);
}
/**
*
* @dev
* Test burn and mint condition
* Returns true if the transfer is valid, and false otherwise.
*
*/
function _validateBurnMint(
address _from,
address _to
) internal view returns (bool isValid) {
// Mint & Burn
if (
(_from == address(0) &&
options.issuance.authorizedMintWithoutApproval) ||
(_to == address(0) &&
options.issuance.authorizedBurnWithoutApproval)
) {
return true;
}
return false;
}
/**
*
* @dev
* Test transfer approval condition
* Returns true if the transfer is valid, and false otherwise.
*/
function _validateApproval(
bytes32 key
) internal view returns (bool isValid) {
// If automatic approval is activate and time to approve the request has passed
if(transferRequests[key].status == STATUS.NONE
||
transferRequests[key].status == STATUS.DENIED
||
transferRequests[key].status == STATUS.EXECUTED)
{
return false;
}
bool isTransferApproved;
bool automaticApprovalCondition;
if(transferRequests[key].status ==
STATUS.APPROVED){
isTransferApproved = (transferRequests[key].maxTime >= block.timestamp);
} else if(options
.automaticApproval
.isActivate){
// Warning: overflow possible if timeLimitBeforeAutomaticApproval == max(uint256)
automaticApprovalCondition= block.timestamp >=
(transferRequests[key].askTime +
options.automaticApproval.timeLimitBeforeAutomaticApproval);
}
// If the transfer is approved and delay to perform the transfer is respected
if (automaticApprovalCondition || isTransferApproved) {
return true;
} else {
return false;
}
}
/*//////////////////////////////////////////////////////////////
ERC-2771
//////////////////////////////////////////////////////////////*/
/**
* @dev This surcharge is not necessary if you do not use the MetaTxModule
*/
function _msgSender()
internal
view
override(ERC2771Context, Context)
returns (address sender)
{
return ERC2771Context._msgSender();
}
/**
* @dev This surcharge is not necessary if you do not use the MetaTxModule
*/
function _msgData()
internal
view
override(ERC2771Context, Context)
returns (bytes calldata)
{
return ERC2771Context._msgData();
}
/**
* @dev This surcharge is not necessary if you do not use the MetaTxModule
*/
function _contextSuffixLength()
internal
view
override(ERC2771Context, Context)
returns (uint256)
{
return ERC2771Context._contextSuffixLength();
}
}