Skip to content

Commit

Permalink
Add ruleEngine with OperateOnTransfer + remove useless init function …
Browse files Browse the repository at this point in the history
…in internal modules
  • Loading branch information
rya-sge committed Dec 1, 2023
1 parent 0f9109f commit 81aa58f
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 66 deletions.
2 changes: 1 addition & 1 deletion contracts/CMTAT_STANDALONE.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ contract CMTAT_STANDALONE is CMTAT_BASE {
uint8 decimalsIrrevocable,
string memory tokenId_,
string memory terms_,
IERC1404Wrapper ruleEngine_,
IRuleEngineCMTAT ruleEngine_,
string memory information_,
uint256 flag_
) MetaTxModule(forwarderIrrevocable) {
Expand Down
17 changes: 17 additions & 0 deletions contracts/interfaces/draft-IERC1404/IRuleEngineCMTAT.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// SPDX-License-Identifier: MPL-2.0

pragma solidity ^0.8.0;

import "./draft-IERC1404Wrapper.sol";

interface IRuleEngineCMTAT is IERC1404Wrapper {
/**
* @dev Returns true if the operation is a success, and false otherwise.
*/
function operateOnTransfer(
address _from,
address _to,
uint256 _amount
) external returns (bool isValid);

}
10 changes: 10 additions & 0 deletions contracts/mocks/RuleEngine/RuleEngineMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ contract RuleEngineMock is IRuleEngine {
return detectTransferRestriction(_from, _to, _amount) == 0;
}

/*
@dev
Warning: if you want to use this mock, you have to restrict the access to this function through an an access control
*/
function operateOnTransfer( address _from,
address _to,
uint256 _amount) public override returns (bool){
return validateTransfer(_from, _to, _amount);
}

/**
@dev
For all the rules, each restriction code has to be unique.
Expand Down
4 changes: 2 additions & 2 deletions contracts/mocks/RuleEngine/interfaces/IRuleEngine.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
pragma solidity ^0.8.0;

import "./IRule.sol";
import "../../../interfaces/draft-IERC1404/draft-IERC1404Wrapper.sol";
import "../../../interfaces/draft-IERC1404/IRuleEngineCMTAT.sol";

interface IRuleEngine is IERC1404Wrapper {
interface IRuleEngine is IRuleEngineCMTAT {
/**
* @dev define the rules, the precedent rules will be overwritten
*/
Expand Down
8 changes: 4 additions & 4 deletions contracts/modules/CMTAT_BASE.sol
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ abstract contract CMTAT_BASE is
uint8 decimalsIrrevocable,
string memory tokenId_,
string memory terms_,
IERC1404Wrapper ruleEngine_,
string memory information_,
IRuleEngineCMTAT ruleEngine_,
string memory information_,
uint256 flag_
) public initializer {
__CMTAT_init(
Expand Down Expand Up @@ -91,7 +91,7 @@ abstract contract CMTAT_BASE is
uint8 decimalsIrrevocable,
string memory tokenId_,
string memory terms_,
IERC1404Wrapper ruleEngine_,
IRuleEngineCMTAT ruleEngine_,
string memory information_,
uint256 flag_
) internal onlyInitializing {
Expand Down Expand Up @@ -183,7 +183,7 @@ abstract contract CMTAT_BASE is
address to,
uint256 amount
) internal override(ERC20Upgradeable) {
if (!ValidationModule.validateTransfer(from, to, amount)) {
if (!ValidationModule._operateOnTransfer(from, to, amount)) {
revert Errors.CMTAT_InvalidTransfer(from, to, amount);
}
/*
Expand Down
13 changes: 0 additions & 13 deletions contracts/modules/internal/ERC20SnapshotModuleInternal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,6 @@ abstract contract ERC20SnapshotModuleInternal is SnapshotModuleBase, ERC20Upgrad
*/
uint256[] private _scheduledSnapshots;

/**
* @dev Initializes the contract
*/
function __ERC20Snapshot_init(
string memory name_,
string memory symbol_
) internal onlyInitializing {
__Context_init_unchained();
__ERC20_init(name_, symbol_);
__SnapshotModuleBase_init_unchained();
__ERC20Snapshot_init_unchained();
}

function __ERC20Snapshot_init_unchained() internal onlyInitializing {
// Nothing to do
// _currentSnapshotTime & _currentSnapshotIndex are initialized to zero
Expand Down
8 changes: 0 additions & 8 deletions contracts/modules/internal/EnforcementModuleInternal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,6 @@ abstract contract EnforcementModuleInternal is

mapping(address => bool) private _frozen;

/**
* @dev Initializes the contract
*/
function __Enforcement_init() internal onlyInitializing {
__Context_init_unchained();
__Enforcement_init_unchained();
}

function __Enforcement_init_unchained() internal onlyInitializing {
// no variable to initialize
}
Expand Down
22 changes: 8 additions & 14 deletions contracts/modules/internal/ValidationModuleInternal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pragma solidity ^0.8.20;
import "../../../openzeppelin-contracts-upgradeable/contracts/utils/ContextUpgradeable.sol";
import "../../../openzeppelin-contracts-upgradeable/contracts/proxy/utils/Initializable.sol";
import "../../interfaces/draft-IERC1404/draft-IERC1404Wrapper.sol";

import "../../interfaces/draft-IERC1404/IRuleEngineCMTAT.sol";
/**
* @dev Validation module.
*
Expand All @@ -18,22 +18,12 @@ abstract contract ValidationModuleInternal is
/**
* @dev Emitted when a rule engine is set.
*/
event RuleEngine(IERC1404Wrapper indexed newRuleEngine);

IERC1404Wrapper public ruleEngine;
event RuleEngine(IRuleEngineCMTAT indexed newRuleEngine);

/**
* @dev Initializes the contract with rule engine.
*/
function __Validation_init(
IERC1404Wrapper ruleEngine_
) internal onlyInitializing {
__Context_init_unchained();
__Validation_init_unchained(ruleEngine_);
}
IRuleEngineCMTAT public ruleEngine;

function __Validation_init_unchained(
IERC1404Wrapper ruleEngine_
IRuleEngineCMTAT ruleEngine_
) internal onlyInitializing {
if (address(ruleEngine_) != address(0)) {
ruleEngine = ruleEngine_;
Expand Down Expand Up @@ -72,5 +62,9 @@ abstract contract ValidationModuleInternal is
return ruleEngine.detectTransferRestriction(from, to, amount);
}

function _operateOnTransfer(address from, address to, uint256 amount) virtual internal returns (bool) {
return ruleEngine.operateOnTransfer(from, to, amount);
}

uint256[50] private __gap;
}
17 changes: 0 additions & 17 deletions contracts/modules/security/AuthorizationModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,6 @@ abstract contract AuthorizationModule is AccessControlDefaultAdminRulesUpgradeab
// SnapshotModule
bytes32 public constant SNAPSHOOTER_ROLE = keccak256("SNAPSHOOTER_ROLE");



function __AuthorizationModule_init(
address admin,
uint48 initialDelay
) internal onlyInitializing {
/* OpenZeppelin */
__Context_init_unchained();
// AccessControlUpgradeable inherits from ERC165Upgradeable
__ERC165_init_unchained();
__AccessControl_init_unchained();
__AccessControlDefaultAdminRules_init_unchained(initialDelay, admin);

/* own function */
__AuthorizationModule_init_unchained();
}

/**
* @dev
*
Expand Down
25 changes: 23 additions & 2 deletions contracts/modules/wrapper/controllers/ValidationModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ abstract contract ValidationModule is
@param ruleEngine_ the call will be reverted if the new value of ruleEngine is the same as the current one
*/
function setRuleEngine(
IERC1404Wrapper ruleEngine_
IRuleEngineCMTAT ruleEngine_
) external onlyRole(DEFAULT_ADMIN_ROLE) {
if (ruleEngine == ruleEngine_)
revert Errors.CMTAT_ValidationModule_SameValue();
Expand Down Expand Up @@ -97,13 +97,24 @@ abstract contract ValidationModule is
return TEXT_UNKNOWN_CODE;
}
}

function validateTransferByModule(
address from,
address to,
uint256 /*amount*/
) internal view returns (bool) {
if (paused() || frozen(from) || frozen(to)) {
return false;
}
return true;
}

function validateTransfer(
address from,
address to,
uint256 amount
) public view override returns (bool) {
if (paused() || frozen(from) || frozen(to)) {
if (!validateTransferByModule(from, to, amount)) {
return false;
}
if (address(ruleEngine) != address(0)) {
Expand All @@ -112,5 +123,15 @@ abstract contract ValidationModule is
return true;
}

function _operateOnTransfer(address from, address to, uint256 amount) override internal returns (bool){
if (!validateTransferByModule(from, to, amount)){
return false;
}
if (address(ruleEngine) != address(0)) {
return ValidationModuleInternal._operateOnTransfer(from, to, amount);
}
return true;
}

uint256[50] private __gap;
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ contract CMTATSnapshotStandaloneTest is CMTAT_BASE_SnapshotTest {
uint8 decimalsIrrevocable,
string memory tokenId_,
string memory terms_,
IERC1404Wrapper ruleEngine_,
IRuleEngineCMTAT ruleEngine_,
string memory information_,
uint256 flag_
) MetaTxModule(forwarderIrrevocable) {
Expand Down
7 changes: 3 additions & 4 deletions contracts/test/CMTATSnapshot/CMTAT_BASE_SnapshotTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ abstract contract CMTAT_BASE_SnapshotTest is
uint8 decimalsIrrevocable,
string memory tokenId_,
string memory terms_,
IERC1404Wrapper ruleEngine_,
IRuleEngineCMTAT ruleEngine_,
string memory information_,
uint256 flag_
) public initializer {
Expand Down Expand Up @@ -82,7 +82,7 @@ abstract contract CMTAT_BASE_SnapshotTest is
uint8 decimalsIrrevocable,
string memory tokenId_,
string memory terms_,
IERC1404Wrapper ruleEngine_,
IRuleEngineCMTAT ruleEngine_,
string memory information_,
uint256 flag_
) internal onlyInitializing {
Expand Down Expand Up @@ -177,8 +177,7 @@ abstract contract CMTAT_BASE_SnapshotTest is
address to,
uint256 amount
) internal override(ERC20Upgradeable) {

if (!ValidationModule.validateTransfer(from, to, amount)){
if (!ValidationModule._operateOnTransfer(from, to, amount)){
revert Errors.CMTAT_InvalidTransfer(from, to, amount);
}
/*
Expand Down

0 comments on commit 81aa58f

Please sign in to comment.