forked from CMTA/CMTAT
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidationModuleRuleEngine.sol
More file actions
148 lines (136 loc) · 4.8 KB
/
ValidationModuleRuleEngine.sol
File metadata and controls
148 lines (136 loc) · 4.8 KB
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
// SPDX-License-Identifier: MPL-2.0
pragma solidity ^0.8.20;
/* ==== Engine === */
import {IRuleEngine} from "../../../../interfaces/engine/IRuleEngine.sol";
/* ==== ValidationModule === */
import {ValidationModuleCore} from "../../core/ValidationModuleCore.sol";
import {ValidationModuleRuleEngineInternal} from "../../../internal/ValidationModuleRuleEngineInternal.sol";
/**
* @dev Validation module with RuleEngine
*
* Useful for to restrict and validate transfers
*/
abstract contract ValidationModuleRuleEngine is
ValidationModuleCore,
ValidationModuleRuleEngineInternal
{
/**
* @notice Reverts if attempting to set the RuleEngine to its current value.
*/
error CMTAT_ValidationModule_SameValue();
/* ============ Modifier ============ */
modifier onlyRuleEngineManager() {
_authorizeRuleEngineManagement();
_;
}
/*//////////////////////////////////////////////////////////////
PUBLIC/EXTERNAL FUNCTIONS
//////////////////////////////////////////////////////////////*/
/* ============ State functions ============ */
/**
* @notice Updates the RuleEngine used for validation/compliance transfer logic.
* @dev Reverts with `CMTAT_ValidationModule_SameValue` if the new RuleEngine is the same as the current one.
* Requirements:
* - Caller must have `DEFAULT_ADMIN_ROLE`.
* Emits a {RuleEngine} event.
* @param ruleEngine_ The new RuleEngine contract to set.
* @custom:access-control
* - The caller must have the `DEFAULT_ADMIN_ROLE`.
*/
function setRuleEngine(
IRuleEngine ruleEngine_
) public virtual onlyRuleEngineManager {
require(address(ruleEngine_) != address(ruleEngine()), CMTAT_ValidationModule_SameValue());
_setRuleEngine(ruleEngine_);
}
/* ============ View functions ============ */
/**
* @inheritdoc ValidationModuleCore
* @dev call the ruleEngine if set
*/
function canTransfer(
address from,
address to,
uint256 value
) public view virtual override(ValidationModuleCore) returns (bool) {
return _canTransfer(from, to, value);
}
/**
* @inheritdoc ValidationModuleCore
* @dev call the ruleEngine if set
*/
function canTransferFrom(
address spender,
address from,
address to,
uint256 value
) public view virtual override(ValidationModuleCore) returns (bool) {
return _canTransferFrom(spender, from, to, value);
}
/*//////////////////////////////////////////////////////////////
INTERNAL/PRIVATE FUNCTIONS
//////////////////////////////////////////////////////////////*/
/* ============ View functions ============ */
function _canTransfer(
address from,
address to,
uint256 value)
internal view virtual returns (bool) {
if (!ValidationModuleCore.canTransfer(from, to, value)) {
return false;
} else {
return _canTransferWithRuleEngine(from, to, value);
}
}
function _canTransferFrom(
address spender,
address from,
address to,
uint256 value
) internal view virtual returns (bool) {
if (!ValidationModuleCore.canTransferFrom(spender, from, to, value)) {
return false;
} else {
return _canTransferFromWithRuleEngine(spender, from, to, value);
}
}
function _canTransferFromWithRuleEngine(
address spender,
address from,
address to,
uint256 value
) internal view virtual returns (bool) {
IRuleEngine ruleEngine_ = ruleEngine();
if (address(ruleEngine_) != address(0)) {
return ruleEngine_.canTransferFrom(spender, from, to, value);
} else{
return true;
}
}
function _canTransferWithRuleEngine(
address from,
address to,
uint256 value
) internal view virtual returns (bool) {
IRuleEngine ruleEngine_ = ruleEngine();
if (address(ruleEngine_) != address(0)) {
return ruleEngine_.canTransfer(from, to, value);
} else{
return true;
}
}
/* ============ Access Control ============ */
function _authorizeRuleEngineManagement() internal virtual;
/* ============ State functions ============ */
function _transferred(address spender, address from, address to, uint256 value) internal virtual{
_canTransferGenericByModuleAndRevert(spender, from, to);
IRuleEngine ruleEngine_ = ruleEngine();
if (address(ruleEngine_) != address(0)){
if(spender != address(0)){
ruleEngine_.transferred(spender, from, to, value);
} else {
ruleEngine_.transferred(from, to, value);
}
}
}
}