Skip to content

Commit

Permalink
refactor: rename to meaningfull name getCustodians
Browse files Browse the repository at this point in the history
  • Loading branch information
geolffreym committed Oct 15, 2024
1 parent fa1ea3b commit 2041f0a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
22 changes: 19 additions & 3 deletions contracts/economics/Tollgate.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { ITollgate } from "contracts/interfaces/economics/ITollgate.sol";

import { T } from "contracts/libraries/Types.sol";
import { C } from "contracts/libraries/Constants.sol";
import { FeesHelper } from "contracts/libraries/FeesHelper.sol";

/// @title Tollgate Contract
/// @dev This contract acts as a financial gateway, managing fees and the currencies allowed
Expand All @@ -20,6 +21,7 @@ import { C } from "contracts/libraries/Constants.sol";
/// @notice The name "Tollgate" reflects the contract's role as a checkpoint that regulates
/// financial access through fees and approved currencies.
contract Tollgate is Initializable, UUPSUpgradeable, GovernableUpgradeable, ITollgate {
using FeesHelper for uint256;
using ERC165Checker for address;
using EnumerableSet for EnumerableSet.AddressSet;

Expand All @@ -29,12 +31,15 @@ contract Tollgate is Initializable, UUPSUpgradeable, GovernableUpgradeable, ITol

/// @notice Emitted when fees are set.
/// @param fee The amount of fees being set.
/// @param ctx The context where the fees are set.
/// @param currency The currency of the fees being set.
/// @param setBy The address that set the fees.
event FeesSet(uint256 fee, T.Context ctx, address indexed currency, address indexed setBy);
/// @notice Error to be thrown when an unsupported currency is used.
/// @param currency The address of the unsupported currency.
error InvalidUnsupportedCurrency(address currency);
/// @notice Error to be thrown when basis point fees are invalid.
error InvalidBasisPointRange(T.Context ctx, uint256 bps);
/// @notice Error thrown when trying to operate with an unsupported currency.
/// @param currency The address of the unsupported currency.
error InvalidCurrency(address currency);
Expand All @@ -47,6 +52,14 @@ contract Tollgate is Initializable, UUPSUpgradeable, GovernableUpgradeable, ITol
_;
}

/// @notice Ensures valid fee representation based on the context.
/// @param ctx The context for which the fee is being set.
/// @param fee The fee to validate.
modifier onlyValidFeeRepresentation(T.Context ctx, uint256 fee) {
if (T.Context.RMA == ctx && !fee.isBasePoint()) revert InvalidBasisPointRange(ctx, fees);
_;
}

/// @dev Constructor that disables initializers to prevent the implementation contract from being initialized.
/// @notice This constructor prevents the implementation contract from being initialized.
/// @dev See https://forum.openzeppelin.com/t/uupsupgradeable-vulnerability-post-mortem/15680
Expand Down Expand Up @@ -97,10 +110,13 @@ contract Tollgate is Initializable, UUPSUpgradeable, GovernableUpgradeable, ITol
/// @param fee The new fee to set (can be flat fee or basis points depending on the context).
/// @param currency The currency associated with the fee (can be ERC-20 or native currency).
/// @dev Only the governance account can call this function to set or update fees.
function setFees(T.Context ctx, uint256 fee, address currency) external onlyGov onlyValidCurrency(currency) {
if (isCurrencySupported(ctx, currency)) return;
registeredCurrencies[ctx].add(currency);
function setFees(
T.Context ctx,
uint256 fee,
address currency
) external onlyGov onlyValidFeeRepresentation(ctx, fee) onlyValidCurrency(currency) {
currencyFees[currency][ctx] = fee;
registeredCurrencies[ctx].add(currency); // set avoid duplication..
emit FeesSet(fee, ctx, currency, msg.sender);
}

Expand Down
16 changes: 16 additions & 0 deletions contracts/libraries/FeesHelper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@ pragma solidity 0.8.26;
import { C } from "contracts/libraries/Constants.sol";

library FeesHelper {
/// @notice Checks if the given fee value is expressed in basis points (bps).
/// @param fees The fee amount to check.
/// @return True if the fee is within the valid basis point range (0 to 10,000), otherwise false.
function isBasePoint(uint256 fees) internal pure returns (bool) {
// 10,000 bps = 100%
return (fees <= C.BPS_MAX);
}

/// @notice Checks if the given fee value is expressed in nominal percentage.
/// @param fees The fee amount to check.
/// @return True if the fee is within the valid nominal range (0 to SCALE_FACTOR), otherwise false.
function isNominal(uint256 fees) internal pure returns (bool) {
// SCALE_FACTOR = 100% nominal
return (fees <= C.SCALE_FACTOR);
}

/// @dev Calculates the percentage of `amount` based on the given `bps` (basis points).
/// @param amount The amount to calculate the percentage of.
/// @param bps The basis points to use for the calculation.
Expand Down
14 changes: 7 additions & 7 deletions contracts/syndication/DistributorReferendum.sol
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ contract DistributorReferendum is

/// @notice Modifier to ensure that the given distributor contract supports the IDistributor interface.
/// @param distributor The distributor contract address.
modifier withValidDistributor(address distributor) {
modifier onlyValidDistributor(address distributor) {
if (!distributor.supportsInterface(INTERFACE_ID_IDISTRIBUTOR)) revert InvalidDistributorContract(distributor);
_;
}
Expand Down Expand Up @@ -106,7 +106,7 @@ contract DistributorReferendum is
/// @notice Registers a distributor by sending a payment to the contract.
/// @param distributor The address of the distributor to register.
/// @param currency The currency used to pay enrollment.
function register(address distributor, address currency) external payable withValidDistributor(distributor) {
function register(address distributor, address currency) external payable onlyValidDistributor(distributor) {
// !IMPORTANT if fees manager does not support the currency, will revert..
uint256 fees = tollgate.getFees(T.Context.SYN, currency);
uint256 total = msg.sender.safeDeposit(fees, currency);
Expand All @@ -120,15 +120,15 @@ contract DistributorReferendum is

/// @notice Revokes the registration of a distributor.
/// @param distributor The address of the distributor to revoke.
function revoke(address distributor) external onlyGov withValidDistributor(distributor) {
function revoke(address distributor) external onlyGov onlyValidDistributor(distributor) {
enrollmentsCount--;
_revoke(uint160(distributor));
emit Revoked(distributor);
}

/// @notice Approves a distributor's registration.
/// @param distributor The address of the distributor to approve.
function approve(address distributor) external onlyGov withValidDistributor(distributor) {
function approve(address distributor) external onlyGov onlyValidDistributor(distributor) {
// reset ledger..
enrollmentsCount++;
_approve(uint160(distributor));
Expand Down Expand Up @@ -158,7 +158,7 @@ contract DistributorReferendum is
/// @dev This function verifies the active status of the distributor.
/// @param distributor The distributor's address to check.
/// @return bool True if the distributor is active, false otherwise.
function isActive(address distributor) public view withValidDistributor(distributor) returns (bool) {
function isActive(address distributor) public view onlyValidDistributor(distributor) returns (bool) {
// this mechanisms helps to verify the availability of the distributor forcing recurrent registrations and status verification.
return _status(uint160(distributor)) == Status.Active && enrollmentTime[distributor] > block.timestamp;
}
Expand All @@ -167,15 +167,15 @@ contract DistributorReferendum is
/// @dev This function verifies the waiting status of the distributor.
/// @param distributor The distributor's address to check.
/// @return bool True if the distributor is waiting, false otherwise.
function isWaiting(address distributor) public view withValidDistributor(distributor) returns (bool) {
function isWaiting(address distributor) public view onlyValidDistributor(distributor) returns (bool) {
return _status(uint160(distributor)) == Status.Waiting;
}

/// @notice Checks if the entity is blocked.
/// @dev This function verifies the blocked status of the distributor.
/// @param distributor The distributor's address to check.
/// @return bool True if the distributor is blocked, false otherwise.
function isBlocked(address distributor) public view withValidDistributor(distributor) returns (bool) {
function isBlocked(address distributor) public view onlyValidDistributor(distributor) returns (bool) {
return _status(uint160(distributor)) == Status.Blocked;
}

Expand Down

0 comments on commit 2041f0a

Please sign in to comment.