Skip to content

Commit

Permalink
Add debtEngine, documentEngine and UUPS proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
rya-sge committed Aug 5, 2024
1 parent 9e49b1d commit 51e0f0c
Show file tree
Hide file tree
Showing 10 changed files with 204 additions and 28 deletions.
60 changes: 60 additions & 0 deletions contracts/CMTAT_PROXY_UUPS.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//SPDX-License-Identifier: MPL-2.0

pragma solidity ^0.8.20;
import "../openzeppelin-contracts-upgradeable/contracts/proxy/utils/UUPSUpgradeable.sol";
import "./modules/CMTAT_BASE.sol";

contract CMTAT_PROXY_UUPS is CMTAT_BASE, UUPSUpgradeable {
/**
* @notice Contract version for the deployment with a proxy
* @param forwarderIrrevocable address of the forwarder, required for the gasless support
*/
/// @custom:oz-upgrades-unsafe-allow constructor
constructor(
address forwarderIrrevocable
) MetaTxModule(forwarderIrrevocable) {
// Disable the possibility to initialize the implementation
_disableInitializers();
}

/**
* @notice
* initialize the proxy contract
* The calls to this function will revert if the contract was deployed without a proxy
* @param admin address of the admin of contract (Access Control)
* @param nameIrrevocable name of the token
* @param symbolIrrevocable name of the symbol
* @param decimalsIrrevocable number of decimals of the token, must be 0 to be compliant with Swiss law as per CMTAT specifications (non-zero decimal number may be needed for other use cases)
* @param tokenId_ name of the tokenId
* @param terms_ terms associated with the token
* @param ruleEngine_ address of the ruleEngine to apply rules to transfers
* @param information_ additional information to describe the token
* @param flag_ add information under the form of bit(0, 1)
*/
function initialize( address admin,
IAuthorizationEngine authorizationEngineIrrevocable,
string memory nameIrrevocable,
string memory symbolIrrevocable,
uint8 decimalsIrrevocable,
string memory tokenId_,
string memory terms_,
IRuleEngine ruleEngine_,
string memory information_,
uint256 flag_) public override initializer {
CMTAT_BASE.initialize( admin,
authorizationEngineIrrevocable,
nameIrrevocable,
symbolIrrevocable,
decimalsIrrevocable,
tokenId_,
terms_,
ruleEngine_,
information_,
flag_);
__UUPSUpgradeable_init_unchained();
}

function _authorizeUpgrade(address) internal override onlyRole("DEFAULT_ADMIN_ROLE") {}

uint256[50] private __gap;
}
16 changes: 16 additions & 0 deletions contracts/interfaces/engine/IDebtEngine.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// SPDX-License-Identifier: MPL-2.0

pragma solidity ^0.8.20;
import "../IDebtGlobal.sol";

interface IDebtEngine is IDebtGlobal {
/**
* @dev Returns true if the operation is authorized, and false otherwise.
*/
function debt() external returns(IDebtGlobal.DebtBase memory);
/**
* @dev Returns true if the operation is authorized, and false otherwise.
*/
function creditEvents() external returns(IDebtGlobal.CreditEvents memory);

}
14 changes: 9 additions & 5 deletions contracts/modules/CMTAT_BASE.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,28 @@ import "./wrapper/extensions/ERC20SnapshotModule.sol";

import "./wrapper/controllers/ValidationModule.sol";
import "./wrapper/extensions/MetaTxModule.sol";
import "./wrapper/extensions/DebtModule/DebtBaseModule.sol";
import "./wrapper/extensions/DebtModule/CreditEventsModule.sol";
import "./wrapper/extensions/DebtModule.sol";
import "./wrapper/extensions/DocumentModule.sol";
import "./security/AuthorizationModule.sol";

import "../libraries/Errors.sol";

abstract contract CMTAT_BASE is
Initializable,
ContextUpgradeable,
// Core
BaseModule,
PauseModule,
ERC20MintModule,
ERC20BurnModule,
EnforcementModule,
ValidationModule,
MetaTxModule,
ERC20BaseModule,
ERC20SnapshotModule
// Extension
MetaTxModule,
ERC20SnapshotModule,
DebtModule,
DocumentModule
{
/**
* @notice
Expand All @@ -64,7 +68,7 @@ abstract contract CMTAT_BASE is
IRuleEngine ruleEngine_,
string memory information_,
uint256 flag_
) public initializer {
) public virtual initializer {
__CMTAT_init(
admin,
authorizationEngineIrrevocable,
Expand Down
3 changes: 1 addition & 2 deletions contracts/modules/security/AuthorizationModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ abstract contract AuthorizationModule is AccessControlUpgradeable {
_grantRole(DEFAULT_ADMIN_ROLE, admin);
if (address(authorizationEngine) != address (0)) {
authorizationEngine = authorizationEngine_;
emit AuthorizationEngine(authorizationEngine_);
}


}

/*
Expand Down
2 changes: 1 addition & 1 deletion contracts/modules/wrapper/controllers/ValidationModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ abstract contract ValidationModule is
emit RuleEngine(ruleEngine_);
}

/**
/**
* @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
Expand Down
20 changes: 1 addition & 19 deletions contracts/modules/wrapper/core/BaseModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ pragma solidity ^0.8.20;
// required OZ imports here
import "../../security/AuthorizationModule.sol";
import "../../../libraries/Errors.sol";
import "../../../interfaces/draft-IERC1643.sol";
abstract contract BaseModule is IERC1643, AuthorizationModule {
abstract contract BaseModule is AuthorizationModule {
/**
* @notice
* Get the current version of the smart contract
Expand All @@ -27,9 +26,6 @@ abstract contract BaseModule is IERC1643, AuthorizationModule {
string public information;
// additional attribute to store information as an uint256
uint256 public flag;
IERC1643 DocumentEngine;



/* Initializers */
/**
Expand Down Expand Up @@ -81,19 +77,5 @@ abstract contract BaseModule is IERC1643, AuthorizationModule {
emit Information(information_, information_);
}

function getDocument(bytes32 _name) public view returns (string memory, bytes32, uint256){
if(address(DocumentEngine) != address(0)){
return DocumentEngine.getDocument( _name);
} else{
return ("",0x0, 0);
}
}

function getAllDocuments() public view returns (bytes32[] memory documents){
if(address(DocumentEngine) != address(0)){
documents = DocumentEngine.getAllDocuments();
}
}

uint256[50] private __gap;
}
1 change: 1 addition & 0 deletions contracts/modules/wrapper/core/ERC20BaseModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import "../../../../openzeppelin-contracts-upgradeable/contracts/token/ERC20/ERC
import "../../../libraries/Errors.sol";

abstract contract ERC20BaseModule is ERC20Upgradeable {

/* Events */
/**
* @notice Emitted when the specified `spender` spends the specified `value` tokens owned by the specified `owner` reducing the corresponding allowance.
Expand Down
57 changes: 57 additions & 0 deletions contracts/modules/wrapper/extensions/DebtModule.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//SPDX-License-Identifier: MPL-2.0

pragma solidity ^0.8.20;

import "../../security/AuthorizationModule.sol";
import "../../../libraries/Errors.sol";
import "../../../interfaces/engine/IDebtEngine.sol";

abstract contract DebtModule is AuthorizationModule, IDebtEngine {
bytes32 public constant DEBT_ROLE = keccak256("DEBT_ROLE");
IDebtEngine public debtEngine;
/**
* @dev Emitted when a rule engine is set.
*/
event DebtEngine(IDebtEngine indexed newDebtEngine);
/**
* @dev
*
* - The grant to the admin role is done by AccessControlDefaultAdminRules
* - The control of the zero address is done by AccessControlDefaultAdminRules
*
*/
function __DebtModule_init_unchained(IDebtEngine debtEngine_)
internal onlyInitializing {
if (address(debtEngine_) != address (0)) {
debtEngine = debtEngine_;
emit DebtEngine(debtEngine_);
}


}

/*
* @notice set an authorizationEngine if not already set
*
*/
function setDebtEngine(
IDebtEngine debtEngine_
) external onlyRole(DEBT_ROLE) {
debtEngine = debtEngine_;
emit DebtEngine(debtEngine_);
}

function debt() external returns(DebtBase memory debtBaseResult){
if(address(debtEngine) != address(0)){
debtBaseResult = debtEngine.debt();
}
}

function creditEvents() external returns(CreditEvents memory creditEventsResult){
if(address(debtEngine) != address(0)){
creditEventsResult = debtEngine.creditEvents();
}
}

uint256[50] private __gap;
}
58 changes: 58 additions & 0 deletions contracts/modules/wrapper/extensions/DocumentModule.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//SPDX-License-Identifier: MPL-2.0

pragma solidity ^0.8.20;

import "../../security/AuthorizationModule.sol";
import "../../../libraries/Errors.sol";
import "../../../interfaces/draft-IERC1643.sol";
abstract contract DocumentModule is AuthorizationModule, IERC1643 {
bytes32 public constant DOCUMENT_ROLE = keccak256("DOCUMENT_ROLE");
IERC1643 public documentEngine;
/**
* @dev Emitted when a rule engine is set.
*/
event DocumentEngine(IERC1643 indexed newDocumentEngine);
/**
* @dev
*
* - The grant to the admin role is done by AccessControlDefaultAdminRules
* - The control of the zero address is done by AccessControlDefaultAdminRules
*
*/
function __DocumentModule_init_unchained(IERC1643 documentEngine_)
internal onlyInitializing {
if (address(documentEngine_) != address (0)) {
documentEngine = documentEngine_;
emit DocumentEngine(documentEngine_);
}
}

/*
* @notice set an authorizationEngine if not already set
*
*/
function setDocumentEngine(
IERC1643 documentEngine_
) external onlyRole(DOCUMENT_ROLE) {
documentEngine = documentEngine_;
emit DocumentEngine(documentEngine_);
}


function getDocument(bytes32 _name) public view returns (string memory, bytes32, uint256){
if(address(documentEngine) != address(0)){
return documentEngine.getDocument( _name);
} else{
return ("",0x0, 0);
}
}

function getAllDocuments() public view returns (bytes32[] memory documents){
if(address(documentEngine) != address(0)){
documents = documentEngine.getAllDocuments();
}
}


uint256[50] private __gap;
}
1 change: 0 additions & 1 deletion test/proxy/general/UpgradeProxy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ describe(
Functions used: balanceOf, totalSupply, mint
*/
it('testKeepStorageForTokens', async function () {
/// // ADAPT TRUFFLE TEST TO HARDHAT
/* Factory & Artefact */
const ETHERS_CMTAT_PROXY_FACTORY = await ethers.getContractFactory(
'CMTAT_PROXY'
Expand Down

0 comments on commit 51e0f0c

Please sign in to comment.