Skip to content

Commit

Permalink
Merge pull request #51 from CMTA/fb-format-code
Browse files Browse the repository at this point in the history
Fb format code
  • Loading branch information
rya-sge committed Oct 13, 2022
2 parents 5f047cf + ee29e61 commit 077b2cf
Show file tree
Hide file tree
Showing 27 changed files with 2,471 additions and 1,247 deletions.
517 changes: 308 additions & 209 deletions contracts/CMTAT.sol

Large diffs are not rendered by default.

26 changes: 18 additions & 8 deletions contracts/interfaces/IRule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,25 @@
pragma solidity ^0.8.17;

interface IRule {
function isTransferValid(
address _from, address _to, uint256 _amount)
external view returns (bool isValid);
function isTransferValid(
address _from,
address _to,
uint256 _amount
) external view returns (bool isValid);

function detectTransferRestriction(
address _from, address _to, uint256 _amount)
external view returns (uint8);
function detectTransferRestriction(
address _from,
address _to,
uint256 _amount
) external view returns (uint8);

function canReturnTransferRestrictionCode(uint8 _restrictionCode) external view returns (bool);
function canReturnTransferRestrictionCode(uint8 _restrictionCode)
external
view
returns (bool);

function messageForTransferRestriction(uint8 _restrictionCode) external view returns (string memory);
function messageForTransferRestriction(uint8 _restrictionCode)
external
view
returns (string memory);
}
35 changes: 20 additions & 15 deletions contracts/interfaces/IRuleEngine.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,28 @@ pragma solidity ^0.8.17;
import "./IRule.sol";

interface IRuleEngine {
function setRules(IRule[] calldata rules_) external;

function setRules(IRule[] calldata rules_) external;
function ruleLength() external view returns (uint256);
function rule(uint256 ruleId) external view returns (IRule);
function rules() external view returns(IRule[] memory);
function ruleLength() external view returns (uint256);

function validateTransfer(
address _from,
address _to,
uint256 _amount)
external view returns (bool);
function rule(uint256 ruleId) external view returns (IRule);

function detectTransferRestriction (
address _from,
address _to,
uint256 _value)
external view returns (uint8);
function rules() external view returns (IRule[] memory);

function messageForTransferRestriction (uint8 _restrictionCode) external view returns (string memory);
function validateTransfer(
address _from,
address _to,
uint256 _amount
) external view returns (bool);

function detectTransferRestriction(
address _from,
address _to,
uint256 _value
) external view returns (uint8);

function messageForTransferRestriction(uint8 _restrictionCode)
external
view
returns (string memory);
}
6 changes: 3 additions & 3 deletions contracts/mocks/MinimalForwarderMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pragma solidity ^0.8.17;
import "../../openzeppelin-contracts-upgradeable/contracts/metatx/MinimalForwarderUpgradeable.sol";

contract MinimalForwarderMock is MinimalForwarderUpgradeable {
function initialize() public initializer {
__MinimalForwarder_init();
}
function initialize() public initializer {
__MinimalForwarder_init();
}
}
97 changes: 52 additions & 45 deletions contracts/mocks/RuleEngineMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,60 +6,67 @@ import "../interfaces/IRule.sol";
import "../interfaces/IRuleEngine.sol";
import "./RuleMock.sol";


contract RuleEngineMock is IRuleEngine {
IRule[] internal _rules;
IRule[] internal _rules;

constructor() {
_rules.push(new RuleMock());
}
constructor() {
_rules.push(new RuleMock());
}

function setRules(IRule[] calldata rules_) external override {
_rules = rules_;
}
function setRules(IRule[] calldata rules_) external override {
_rules = rules_;
}

function ruleLength() external view override returns (uint256) {
return _rules.length;
}
function ruleLength() external view override returns (uint256) {
return _rules.length;
}

function rule(uint256 ruleId) external view override returns (IRule) {
return _rules[ruleId];
}
function rule(uint256 ruleId) external view override returns (IRule) {
return _rules[ruleId];
}

function rules() external view override returns(IRule[] memory) {
return _rules;
}
function rules() external view override returns (IRule[] memory) {
return _rules;
}

function detectTransferRestriction(
address _from,
address _to,
uint256 _amount)
public view override returns (uint8)
{
for (uint256 i = 0; i < _rules.length; i++) {
uint8 restriction = _rules[i].detectTransferRestriction(_from, _to, _amount);
if (restriction > 0) {
return restriction;
}
function detectTransferRestriction(
address _from,
address _to,
uint256 _amount
) public view override returns (uint8) {
for (uint256 i = 0; i < _rules.length; i++) {
uint8 restriction = _rules[i].detectTransferRestriction(
_from,
_to,
_amount
);
if (restriction > 0) {
return restriction;
}
}
return 0;
}
return 0;
}

function validateTransfer(
address _from,
address _to,
uint256 _amount)
public view override returns (bool)
{
return detectTransferRestriction(_from, _to, _amount) == 0;
}
function validateTransfer(
address _from,
address _to,
uint256 _amount
) public view override returns (bool) {
return detectTransferRestriction(_from, _to, _amount) == 0;
}

function messageForTransferRestriction(uint8 _restrictionCode) public view override returns (string memory) {
for (uint256 i = 0; i < _rules.length; i++) {
if (_rules[i].canReturnTransferRestrictionCode(_restrictionCode)) {
return _rules[i].messageForTransferRestriction(_restrictionCode);
}
function messageForTransferRestriction(uint8 _restrictionCode)
public
view
override
returns (string memory)
{
for (uint256 i = 0; i < _rules.length; i++) {
if (_rules[i].canReturnTransferRestrictionCode(_restrictionCode)) {
return
_rules[i].messageForTransferRestriction(_restrictionCode);
}
}
return "Unknown restriction code";
}
return "Unknown restriction code";
}
}
66 changes: 40 additions & 26 deletions contracts/mocks/RuleMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,45 @@ pragma solidity ^0.8.17;

import "../interfaces/IRule.sol";


contract RuleMock is IRule {
uint8 constant AMOUNT_TOO_HIGH = 10;
string constant TEXT_AMOUNT_TOO_HIGH = "Amount too high";
string constant TEXT_CODE_NOT_FOUND = "Code not found";

function isTransferValid(
address _from, address _to, uint256 _amount)
public pure override returns (bool isValid)
{
return detectTransferRestriction(_from, _to, _amount) == 0;
}

function detectTransferRestriction(
address /* _from */, address /* _to */, uint256 _amount)
public pure override returns (uint8)
{
return _amount < 20 ? 0 : AMOUNT_TOO_HIGH;
}

function canReturnTransferRestrictionCode(uint8 _restrictionCode) public pure override returns (bool) {
return _restrictionCode == AMOUNT_TOO_HIGH;
}

function messageForTransferRestriction(uint8 _restrictionCode) external pure override returns (string memory) {
return _restrictionCode == AMOUNT_TOO_HIGH ? TEXT_AMOUNT_TOO_HIGH : TEXT_CODE_NOT_FOUND;
}
uint8 constant AMOUNT_TOO_HIGH = 10;
string constant TEXT_AMOUNT_TOO_HIGH = "Amount too high";
string constant TEXT_CODE_NOT_FOUND = "Code not found";

function isTransferValid(
address _from,
address _to,
uint256 _amount
) public pure override returns (bool isValid) {
return detectTransferRestriction(_from, _to, _amount) == 0;
}

function detectTransferRestriction(
address, /* _from */
address, /* _to */
uint256 _amount
) public pure override returns (uint8) {
return _amount < 20 ? 0 : AMOUNT_TOO_HIGH;
}

function canReturnTransferRestrictionCode(uint8 _restrictionCode)
public
pure
override
returns (bool)
{
return _restrictionCode == AMOUNT_TOO_HIGH;
}

function messageForTransferRestriction(uint8 _restrictionCode)
external
pure
override
returns (string memory)
{
return
_restrictionCode == AMOUNT_TOO_HIGH
? TEXT_AMOUNT_TOO_HIGH
: TEXT_CODE_NOT_FOUND;
}
}
3 changes: 1 addition & 2 deletions contracts/modules/AuthorizationModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ pragma solidity ^0.8.17;

import "../../openzeppelin-contracts-upgradeable/contracts/access/AccessControlUpgradeable.sol";

abstract contract AuthorizationModule is AccessControlUpgradeable {
}
abstract contract AuthorizationModule is AccessControlUpgradeable {}
34 changes: 27 additions & 7 deletions contracts/modules/BaseModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ pragma solidity ^0.8.17;
import "../../openzeppelin-contracts-upgradeable/contracts/proxy/utils/Initializable.sol";
import "../../openzeppelin-contracts-upgradeable/contracts/token/ERC20/ERC20Upgradeable.sol";


abstract contract BaseModule is Initializable, ERC20Upgradeable {
/* Events */
event Spend (address indexed owner, address indexed spender, uint amount);
event Spend(address indexed owner, address indexed spender, uint256 amount);

/* Variables */
uint8 private _decimals;
Expand All @@ -23,14 +22,24 @@ abstract contract BaseModule is Initializable, ERC20Upgradeable {
* All two of these values are immutable: they can only be set once during
* construction.
*/
function __Base_init(string memory name_, string memory symbol_, uint8 decimals_, string memory tokenId_, string memory terms_) internal initializer {
function __Base_init(
string memory name_,
string memory symbol_,
uint8 decimals_,
string memory tokenId_,
string memory terms_
) internal initializer {
__ERC20_init(name_, symbol_);
_decimals = decimals_;
tokenId = tokenId_;
terms = terms_;
}

function __Base_init_unchained(uint8 decimals_, string memory tokenId_, string memory terms_) internal initializer {
function __Base_init_unchained(
uint8 decimals_,
string memory tokenId_,
string memory terms_
) internal initializer {
_decimals = decimals_;
tokenId = tokenId_;
terms = terms_;
Expand Down Expand Up @@ -67,7 +76,11 @@ abstract contract BaseModule is Initializable, ERC20Upgradeable {
* - the caller must have allowance for ``sender``'s tokens of at least
* `amount`.
*/
function transferFrom(address sender, address recipient, uint256 amount) public virtual override returns (bool) {
function transferFrom(
address sender,
address recipient,
uint256 amount
) public virtual override returns (bool) {
bool result = super.transferFrom(sender, recipient, amount);
if (result == true) {
emit Spend(sender, _msgSender(), amount);
Expand All @@ -83,8 +96,15 @@ abstract contract BaseModule is Initializable, ERC20Upgradeable {
*
* - `spender` cannot be the zero address.
*/
function approve(address spender, uint256 amount, uint256 currentAllowance) public virtual returns (bool) {
require(allowance(_msgSender(), spender) == currentAllowance, "CMTAT: current allowance is not right");
function approve(
address spender,
uint256 amount,
uint256 currentAllowance
) public virtual returns (bool) {
require(
allowance(_msgSender(), spender) == currentAllowance,
"CMTAT: current allowance is not right"
);
super.approve(spender, amount);
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion contracts/modules/BurnModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ import "../../openzeppelin-contracts-upgradeable/contracts/proxy/utils/Initializ
abstract contract BurnModule is Initializable {
bytes32 public constant BURNER_ROLE = keccak256("BURNER_ROLE");

event Burn (address indexed owner, uint amount);
event Burn(address indexed owner, uint256 amount);
}
Loading

0 comments on commit 077b2cf

Please sign in to comment.