Quantillon Protocol

CommonValidationLibrary

Git Source

Title: CommonValidationLibrary

Author: Quantillon Labs - Nicolas Bellengé - @chewbaccoin

Common validation functions used across multiple contracts

Main characteristics:

  • Consolidates common validation patterns
  • Reduces code duplication across contracts
  • Uses custom errors for gas efficiency
  • Maintains same validation logic

Note: security-contact: team@quantillon.money

Functions

validateNonZeroAddress

Validates that an address is not zero

Checks if the provided address is the zero address and reverts with appropriate error. Uses string comparison which is gas-intensive but maintains backward compatibility. For new code, prefer using validateNonZeroAddressWithType() with AddressType enum.

Notes:

  • security: Pure; no state change

  • validation: Reverts if addr is zero

  • state-changes: None

  • events: None

  • errors: InvalidAdmin, InvalidTreasury, InvalidToken, InvalidOracle, InvalidVault, InvalidAddress

  • reentrancy: No external calls

  • access: Internal library

  • oracle: None

function validateNonZeroAddress(address addr, string memory errorType) internal pure;

Parameters

NameTypeDescription
addraddressThe address to validate
errorTypestringThe type of address being validated (admin, treasury, token, oracle, vault)

validatePositiveAmount

Validates that an amount is positive

Reverts with InvalidAmount if amount is zero

Notes:

  • security: Pure; no state change

  • validation: Reverts if amount is zero

  • state-changes: None

  • events: None

  • errors: InvalidAmount

  • reentrancy: No external calls

  • access: Internal library

  • oracle: None

function validatePositiveAmount(uint256 amount) internal pure;

Parameters

NameTypeDescription
amountuint256The amount to validate

validateMinAmount

Validates that an amount is above minimum threshold

Reverts with InsufficientBalance if amount is below minimum

Notes:

  • security: Pure; no state change

  • validation: Reverts if amount < minAmount

  • state-changes: None

  • events: None

  • errors: InsufficientBalance

  • reentrancy: No external calls

  • access: Internal library

  • oracle: None

function validateMinAmount(uint256 amount, uint256 minAmount) internal pure;

Parameters

NameTypeDescription
amountuint256The amount to validate
minAmountuint256The minimum required amount

validateMaxAmount

Validates that an amount is below maximum threshold

Reverts with AboveLimit if amount exceeds maximum

Notes:

  • security: Pure; no state change

  • validation: Reverts if amount > maxAmount

  • state-changes: None

  • events: None

  • errors: AboveLimit

  • reentrancy: No external calls

  • access: Internal library

  • oracle: None

function validateMaxAmount(uint256 amount, uint256 maxAmount) internal pure;

Parameters

NameTypeDescription
amountuint256The amount to validate
maxAmountuint256The maximum allowed amount

validatePercentage

Validates that a percentage is within valid range

Reverts with AboveLimit if percentage exceeds maximum

Notes:

  • security: Pure; no state change

  • validation: Reverts if percentage > maxPercentage

  • state-changes: None

  • events: None

  • errors: AboveLimit

  • reentrancy: No external calls

  • access: Internal library

  • oracle: None

function validatePercentage(uint256 percentage, uint256 maxPercentage) internal pure;

Parameters

NameTypeDescription
percentageuint256The percentage to validate (in basis points)
maxPercentageuint256The maximum allowed percentage (in basis points)

validateDuration

Validates that a duration is within valid range

Reverts with HoldingPeriodNotMet if too short, AboveLimit if too long

Notes:

  • security: Pure; no state change

  • validation: Reverts if duration out of [minDuration, maxDuration]

  • state-changes: None

  • events: None

  • errors: HoldingPeriodNotMet, AboveLimit

  • reentrancy: No external calls

  • access: Internal library

  • oracle: None

function validateDuration(uint256 duration, uint256 minDuration, uint256 maxDuration) internal pure;

Parameters

NameTypeDescription
durationuint256The duration to validate
minDurationuint256The minimum allowed duration
maxDurationuint256The maximum allowed duration

validatePrice

Validates that a price is valid (greater than zero)

Reverts with InvalidPrice if price is zero

Notes:

  • security: Pure; no state change

  • validation: Reverts if price is zero

  • state-changes: None

  • events: None

  • errors: InvalidPrice

  • reentrancy: No external calls

  • access: Internal library

  • oracle: None

function validatePrice(uint256 price) internal pure;

Parameters

NameTypeDescription
priceuint256The price to validate

validateCondition

Validates that a boolean condition is true

Generic condition validator that throws specific errors based on error type

Notes:

  • security: Pure; no state change

  • validation: Reverts if condition is false

  • state-changes: None

  • events: None

  • errors: InvalidOracle, InsufficientCollateralization, NotAuthorized, InvalidCondition

  • reentrancy: No external calls

  • access: Internal library

  • oracle: None

function validateCondition(bool condition, string memory errorType) internal pure;

Parameters

NameTypeDescription
conditionboolThe condition to validate
errorTypestringThe type of error to throw if condition is false

_keccak256Bytes

Internal keccak256 of string using inline assembly (gas-efficient)

function _keccak256Bytes(string memory s) private pure returns (bytes32);

validateCountLimit

Validates that a count is within limits

Reverts with TooManyPositions if count exceeds or equals maximum

Notes:

  • security: Pure; no state change

  • validation: Reverts if count >= maxCount

  • state-changes: None

  • events: None

  • errors: TooManyPositions

  • reentrancy: No external calls

  • access: Internal library

  • oracle: None

function validateCountLimit(uint256 count, uint256 maxCount) internal pure;

Parameters

NameTypeDescription
countuint256The current count
maxCountuint256The maximum allowed count

validateSufficientBalance

Validates that a balance is sufficient

Reverts with InsufficientBalance if balance is below required amount

Notes:

  • security: Pure; no state change

  • validation: Reverts if balance < requiredAmount

  • state-changes: None

  • events: None

  • errors: InsufficientBalance

  • reentrancy: No external calls

  • access: Internal library

  • oracle: None

function validateSufficientBalance(uint256 balance, uint256 requiredAmount) internal pure;

Parameters

NameTypeDescription
balanceuint256The current balance
requiredAmountuint256The required amount

validateNotContract

Validates that an address is not a contract (for security)

Prevents sending funds to potentially malicious contracts

Notes:

  • security: View; checks extcodesize

  • validation: Reverts if addr has code

  • state-changes: None

  • events: None

  • errors: InvalidTreasury, InvalidAddress

  • reentrancy: No external calls

  • access: Internal library

  • oracle: None

function validateNotContract(address addr, string memory errorType) internal view;

Parameters

NameTypeDescription
addraddressThe address to validate
errorTypestringThe type of error to throw if validation fails

validateTreasuryAddress

Validates treasury address is not zero address

Reverts with ZeroAddress if treasury is zero address

Notes:

  • security: Pure; no state change

  • validation: Reverts if treasury is zero

  • state-changes: None

  • events: None

  • errors: ZeroAddress

  • reentrancy: No external calls

  • access: Internal library

  • oracle: None

function validateTreasuryAddress(address treasury) internal pure;

Parameters

NameTypeDescription
treasuryaddressThe treasury address to validate

validateSlippage

Validates slippage protection for token swaps/trades

Reverts with InvalidParameter if slippage exceeds tolerance

Notes:

  • security: Pure; no state change

  • validation: Reverts if received below expected minus tolerance

  • state-changes: None

  • events: None

  • errors: InvalidParameter

  • reentrancy: No external calls

  • access: Internal library

  • oracle: None

function validateSlippage(uint256 received, uint256 expected, uint256 tolerance) internal pure;

Parameters

NameTypeDescription
receiveduint256The actual amount received
expecteduint256The expected amount
toleranceuint256The slippage tolerance in basis points

validateThresholdValue

Validates that a value meets minimum threshold requirements

Reverts with BelowThreshold if value is below minimum

Notes:

  • security: Pure; no state change

  • validation: Reverts if value < threshold

  • state-changes: None

  • events: None

  • errors: BelowThreshold

  • reentrancy: No external calls

  • access: Internal library

  • oracle: None

function validateThresholdValue(uint256 value, uint256 threshold) internal pure;

Parameters

NameTypeDescription
valueuint256The value to validate
thresholduint256The minimum required threshold

validateFee

Validates fee amount against maximum allowed fee

Reverts with InvalidParameter if fee exceeds maximum

Notes:

  • security: Pure; no state change

  • validation: Reverts if fee > maxFee

  • state-changes: None

  • events: None

  • errors: InvalidParameter

  • reentrancy: No external calls

  • access: Internal library

  • oracle: None

function validateFee(uint256 fee, uint256 maxFee) internal pure;

Parameters

NameTypeDescription
feeuint256The fee amount to validate
maxFeeuint256The maximum allowed fee

validateThreshold

Validates threshold value against maximum limit

Reverts with InvalidParameter if threshold exceeds maximum

Notes:

  • security: Pure; no state change

  • validation: Reverts if threshold > maxThreshold

  • state-changes: None

  • events: None

  • errors: InvalidParameter

  • reentrancy: No external calls

  • access: Internal library

  • oracle: None

function validateThreshold(uint256 threshold, uint256 maxThreshold) internal pure;

Parameters

NameTypeDescription
thresholduint256The threshold value to validate
maxThresholduint256The maximum allowed threshold