Skip to content

Commit

Permalink
Merge pull request #217 from CMTA/custom-error-hardhat
Browse files Browse the repository at this point in the history
Custom error with hardhat support
  • Loading branch information
rya-sge authored Aug 29, 2023
2 parents a4c660f + b28ef33 commit bc082bb
Show file tree
Hide file tree
Showing 90 changed files with 6,522 additions and 10,715 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Truffle CI
name: Hardhat CI

on:
push:
Expand Down Expand Up @@ -27,5 +27,5 @@ jobs:
- name: Install Project Dependencies
run: npm install

- name: Run Truffle Test
run: npx truffle test
- name: Run Hardhat Test
run: npx hardhat test
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ artifacts
cache
#manticore
mcore_*
#secrets
.env
10 changes: 5 additions & 5 deletions contracts/CMTAT_PROXY.sol
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
//SPDX-License-Identifier: MPL-2.0

pragma solidity ^0.8.17;
pragma solidity ^0.8.20;

import "./modules/CMTAT_BASE.sol";

contract CMTAT_PROXY is CMTAT_BASE {
/**
* @notice Contract version for the deployment with a proxy
* @param forwarderIrrevocable address of the forwarder, required for the gasless support
*/
/**
* @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
Expand Down
28 changes: 14 additions & 14 deletions contracts/CMTAT_STANDALONE.sol
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
//SPDX-License-Identifier: MPL-2.0

pragma solidity ^0.8.17;
pragma solidity ^0.8.20;

import "./modules/CMTAT_BASE.sol";

contract CMTAT_STANDALONE is CMTAT_BASE {
/**
* @notice Contract version for standalone deployment
* @param forwarderIrrevocable address of the forwarder, required for the gasless support
* @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 used to get its user representation, should be 0 to be compliant with the CMTAT specifications.
* @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)
*/
/**
* @notice Contract version for standalone deployment
* @param forwarderIrrevocable address of the forwarder, required for the gasless support
* @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 used to get its user representation, should be 0 to be compliant with the CMTAT specifications.
* @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)
*/
/// @custom:oz-upgrades-unsafe-allow constructor
constructor(
address forwarderIrrevocable,
Expand Down
64 changes: 64 additions & 0 deletions contracts/libraries/Errors.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//SPDX-License-Identifier: MPL-2.0

pragma solidity ^0.8.20;

library Errors {
// CMTAT
error CMTAT_InvalidTransfer(address from, address to, uint256 amount);

// SnapshotModule
error CMTAT_SnapshotModule_SnapshotScheduledInThePast(
uint256 time,
uint256 timestamp
);
error CMTAT_SnapshotModule_SnapshotTimestampBeforeLastSnapshot(
uint256 time,
uint256 lastSnapshotTimestamp
);
error CMTAT_SnapshotModule_SnapshotTimestampAfterNextSnapshot(
uint256 time,
uint256 nextSnapshotTimestamp
);
error CMTAT_SnapshotModule_SnapshotTimestampBeforePreviousSnapshot(
uint256 time,
uint256 previousSnapshotTimestamp
);
error CMTAT_SnapshotModule_SnapshotAlreadyExists();
error CMTAT_SnapshotModule_SnapshotAlreadyDone();
error CMTAT_SnapshotModule_NoSnapshotScheduled();
error CMTAT_SnapshotModule_SnapshotNotFound();

// OnlyDelegateCallModule
error CMTAT_OnlyDelegateCallModule_DirectCallToImplementation();

// ERC20BaseModule
error CMTAT_ERC20BaseModule_WrongAllowance(
address spender,
uint256 currentAllowance,
uint256 allowanceProvided
);

// BurnModule
error CMTAT_BurnModule_EmptyAccounts();
error CMTAT_BurnModule_AccountsValueslengthMismatch();

// MintModule
error CMTAT_MintModule_EmptyAccounts();
error CMTAT_MintModule_AccountsValueslengthMismatch();

// ERC20BaseModule
error CMTAT_ERC20BaseModule_EmptyTos();
error CMTAT_ERC20BaseModule_TosValueslengthMismatch();

// DebtModule
error CMTAT_DebtModule_SameValue();

// BaseModule
error CMTAT_BaseModule_SameValue();

// ValidationModule
error CMTAT_ValidationModule_SameValue();

// AuthorizationModule
error CMTAT_AuthorizationModule_AddressZeroNotAllowed();
}
11 changes: 6 additions & 5 deletions contracts/mocks/MinimalForwarderMock.sol
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
//SPDX-License-Identifier: MPL-2.0

pragma solidity ^0.8.17;
pragma solidity ^0.8.20;

import "../../openzeppelin-contracts-upgradeable/contracts/metatx/MinimalForwarderUpgradeable.sol";
import "../../openzeppelin-contracts-upgradeable/contracts/metatx/ERC2771ForwarderUpgradeable.sol";

contract MinimalForwarderMock is MinimalForwarderUpgradeable {
function initialize() public initializer {
__MinimalForwarder_init();
contract MinimalForwarderMock is ERC2771ForwarderUpgradeable {
function initialize(string memory name) public initializer {
__EIP712_init_unchained(name, "1");
__ERC2771Forwarder_init_unchained("");
}
}
2 changes: 1 addition & 1 deletion contracts/mocks/RuleEngine/CodeList.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: MPL-2.0

pragma solidity ^0.8.17;
pragma solidity ^0.8.20;

abstract contract CodeList {
// Used by RuleMock.sol
Expand Down
12 changes: 9 additions & 3 deletions contracts/mocks/RuleEngine/RuleEngineMock.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//SPDX-License-Identifier: MPL-2.0

pragma solidity ^0.8.17;
pragma solidity ^0.8.20;

import "./interfaces/IRule.sol";
import "./interfaces/IRuleEngine.sol";
Expand Down Expand Up @@ -43,7 +43,7 @@ contract RuleEngineMock is IRuleEngine {
uint256 _amount
) public view override returns (uint8) {
uint256 ruleArrayLength = _rules.length;
for (uint256 i = 0; i < ruleArrayLength; ++i) {
for (uint256 i; i < ruleArrayLength; ) {
uint8 restriction = _rules[i].detectTransferRestriction(
_from,
_to,
Expand All @@ -52,6 +52,9 @@ contract RuleEngineMock is IRuleEngine {
if (restriction != uint8(REJECTED_CODE_BASE.TRANSFER_OK)) {
return restriction;
}
unchecked {
++i;
}
}
return uint8(REJECTED_CODE_BASE.TRANSFER_OK);
}
Expand All @@ -72,11 +75,14 @@ contract RuleEngineMock is IRuleEngine {
uint8 _restrictionCode
) public view override returns (string memory) {
uint256 ruleArrayLength = _rules.length;
for (uint256 i = 0; i < ruleArrayLength; ++i) {
for (uint256 i; i < ruleArrayLength; ) {
if (_rules[i].canReturnTransferRestrictionCode(_restrictionCode)) {
return
_rules[i].messageForTransferRestriction(_restrictionCode);
}
unchecked {
++i;
}
}
return "Unknown restriction code";
}
Expand Down
2 changes: 1 addition & 1 deletion contracts/mocks/RuleEngine/RuleMock.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//SPDX-License-Identifier: MPL-2.0

pragma solidity ^0.8.17;
pragma solidity ^0.8.20;

import "./interfaces/IRule.sol";
import "./CodeList.sol";
Expand Down
78 changes: 40 additions & 38 deletions contracts/modules/CMTAT_BASE.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//SPDX-License-Identifier: MPL-2.0

pragma solidity ^0.8.17;
pragma solidity ^0.8.20;

// required OZ imports here
import "../../openzeppelin-contracts-upgradeable/contracts/proxy/utils/Initializable.sol";
Expand All @@ -24,6 +24,8 @@ import "./wrapper/optional/DebtModule/CreditEventsModule.sol";
import "./security/AuthorizationModule.sol";
import "../interfaces/IEIP1404/IEIP1404Wrapper.sol";

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

abstract contract CMTAT_BASE is
Initializable,
ContextUpgradeable,
Expand All @@ -40,19 +42,19 @@ abstract contract CMTAT_BASE is
CreditEventsModule
{
/**
* @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)
*/
* @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,
string memory nameIrrevocable,
Expand All @@ -78,8 +80,8 @@ abstract contract CMTAT_BASE is
}

/**
* @dev calls the different initialize functions from the different modules
*/
* @dev calls the different initialize functions from the different modules
*/
function __CMTAT_init(
address admin,
string memory nameIrrevocable,
Expand Down Expand Up @@ -142,8 +144,8 @@ abstract contract CMTAT_BASE is
}

/**
* @notice Returns the number of decimals used to get its user representation.
*/
* @notice Returns the number of decimals used to get its user representation.
*/
function decimals()
public
view
Expand All @@ -167,33 +169,33 @@ abstract contract CMTAT_BASE is
return ERC20BaseModule.transferFrom(sender, recipient, amount);
}

/**
* @dev
* SnapshotModule:
* - override SnapshotModuleInternal if you add the SnapshotModule
* e.g. override(SnapshotModuleInternal, ERC20Upgradeable)
* - remove the keyword view
*/
function _beforeTokenTransfer(
/**
* @dev
* SnapshotModule:
* - override SnapshotModuleInternal if you add the SnapshotModule
* e.g. override(SnapshotModuleInternal, ERC20Upgradeable)
* - remove the keyword view
*/
function _update(
address from,
address to,
uint256 amount
) internal view override(ERC20Upgradeable) {
require(
ValidationModule.validateTransfer(from, to, amount),
"CMTAT: transfer rejected by validation module"
);
) internal override(ERC20Upgradeable) {
if (!ValidationModule.validateTransfer(from, to, amount)) {
revert Errors.CMTAT_InvalidTransfer(from, to, amount);
}
ERC20Upgradeable._update(from, to, amount);
// We call the SnapshotModule only if the transfer is valid
/*
SnapshotModule:
Add this call in case you add the SnapshotModule
SnapshotModuleInternal._beforeTokenTransfer(from, to, amount);
SnapshotModuleInternal._update(from, to, amount);
*/
}

/**
* @dev This surcharge is not necessary if you do not use the MetaTxModule
*/
/**
* @dev This surcharge is not necessary if you do not use the MetaTxModule
*/
function _msgSender()
internal
view
Expand All @@ -203,9 +205,9 @@ abstract contract CMTAT_BASE is
return MetaTxModule._msgSender();
}

/**
* @dev This surcharge is not necessary if you do not use the MetaTxModule
*/
/**
* @dev This surcharge is not necessary if you do not use the MetaTxModule
*/
function _msgData()
internal
view
Expand Down
6 changes: 3 additions & 3 deletions contracts/modules/internal/EnforcementModuleInternal.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//SPDX-License-Identifier: MPL-2.0

pragma solidity ^0.8.17;
pragma solidity ^0.8.20;

import "../../../openzeppelin-contracts-upgradeable/contracts/utils/ContextUpgradeable.sol";
import "../../../openzeppelin-contracts-upgradeable/contracts/proxy/utils/Initializable.sol";
Expand Down Expand Up @@ -64,7 +64,7 @@ abstract contract EnforcementModuleInternal is
*/
function _freeze(
address account,
string memory reason
string calldata reason
) internal virtual returns (bool) {
if (_frozen[account]) return false;
_frozen[account] = true;
Expand All @@ -79,7 +79,7 @@ abstract contract EnforcementModuleInternal is
*/
function _unfreeze(
address account,
string memory reason
string calldata reason
) internal virtual returns (bool) {
if (!_frozen[account]) return false;
_frozen[account] = false;
Expand Down
Loading

0 comments on commit bc082bb

Please sign in to comment.