Quantillon Protocol

Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

HedgerPoolLogicLibrary

Git Source

Logic functions for HedgerPool to reduce contract size

Functions

validateAndCalculatePositionParams

Validates position parameters and calculates derived values

Validates all position constraints and calculates fee, margin, and position size

Notes:

  • security: Validates all position constraints and limits

  • validation: Ensures amounts, leverage, and ratios are within limits

  • state-changes: None (pure function)

  • events: None

  • errors: Throws various validation errors if constraints not met

  • reentrancy: Not applicable - pure function

  • access: External pure function

  • oracle: Uses provided eurUsdPrice parameter

function validateAndCalculatePositionParams(
    uint256 usdcAmount,
    uint256 leverage,
    uint256 eurUsdPrice,
    uint256 entryFee,
    uint256 minMarginRatio,
    uint256 maxMarginRatio,
    uint256 maxLeverage,
    uint256 maxPositionsPerHedger,
    uint256 activePositionCount,
    uint256 maxMargin,
    uint256 maxPositionSize,
    uint256 maxEntryPrice,
    uint256 maxLeverageValue,
    uint256 currentTime
) external pure returns (uint256 fee, uint256 netMargin, uint256 positionSize, uint256 marginRatio);

Parameters

NameTypeDescription
usdcAmountuint256Amount of USDC to deposit
leverageuint256Leverage multiplier for the position
eurUsdPriceuint256Current EUR/USD price from oracle
entryFeeuint256Entry fee rate in basis points
minMarginRatiouint256Minimum margin ratio in basis points
maxMarginRatiouint256Maximum margin ratio in basis points
maxLeverageuint256Maximum allowed leverage
maxPositionsPerHedgeruint256Maximum positions per hedger
activePositionCountuint256Current active position count for hedger
maxMarginuint256Maximum margin per position
maxPositionSizeuint256Maximum position size
maxEntryPriceuint256Maximum entry price
maxLeverageValueuint256Maximum leverage value
currentTimeuint256Current timestamp

Returns

NameTypeDescription
feeuint256Calculated entry fee
netMarginuint256Net margin after fee deduction
positionSizeuint256Calculated position size
marginRatiouint256Calculated margin ratio

calculatePnL

Calculates profit or loss for a hedge position

Computes PnL based on price movement from entry to current price

Notes:

  • security: No security validations required for pure function

  • validation: None required for pure function

  • state-changes: None (pure function)

  • events: None

  • errors: None

  • reentrancy: Not applicable - pure function

  • access: Internal function

  • oracle: Uses provided currentPrice parameter

function calculatePnL(uint256 positionSize, uint256 entryPrice, uint256 currentPrice)
    internal
    pure
    returns (int256);

Parameters

NameTypeDescription
positionSizeuint256Size of the position in USDC
entryPriceuint256Price at which the position was opened
currentPriceuint256Current market price

Returns

NameTypeDescription
<none>int256Profit (positive) or loss (negative) amount

isPositionLiquidatable

Determines if a position is eligible for liquidation

Checks if position margin ratio is below liquidation threshold

Notes:

  • security: No security validations required for pure function

  • validation: None required for pure function

  • state-changes: None (pure function)

  • events: None

  • errors: None

  • reentrancy: Not applicable - pure function

  • access: External pure function

  • oracle: Uses provided currentPrice parameter

function isPositionLiquidatable(
    uint256 margin,
    uint256 positionSize,
    uint256 entryPrice,
    uint256 currentPrice,
    uint256 liquidationThreshold
) external pure returns (bool);

Parameters

NameTypeDescription
marginuint256Current margin amount for the position
positionSizeuint256Size of the position in USDC
entryPriceuint256Price at which the position was opened
currentPriceuint256Current market price
liquidationThresholduint256Liquidation threshold in basis points

Returns

NameTypeDescription
<none>boolTrue if position can be liquidated, false otherwise

calculateRewardUpdate

Calculates reward updates for hedgers based on interest rate differentials

Computes new pending rewards based on time elapsed and interest rates

Notes:

  • security: No security validations required for pure function

  • validation: None required for pure function

  • state-changes: None (pure function)

  • events: None

  • errors: None

  • reentrancy: Not applicable - pure function

  • access: External pure function

  • oracle: Not applicable

function calculateRewardUpdate(
    uint256 totalExposure,
    uint256 eurInterestRate,
    uint256 usdInterestRate,
    uint256 lastRewardBlock,
    uint256 currentBlock,
    uint256 maxRewardPeriod,
    uint256 currentPendingRewards
) external pure returns (uint256 newPendingRewards, uint256 newLastRewardBlock);

Parameters

NameTypeDescription
totalExposureuint256Total exposure across all positions
eurInterestRateuint256EUR interest rate in basis points
usdInterestRateuint256USD interest rate in basis points
lastRewardBlockuint256Block number of last reward calculation
currentBlockuint256Current block number
maxRewardPerioduint256Maximum reward period in blocks
currentPendingRewardsuint256Current pending rewards amount

Returns

NameTypeDescription
newPendingRewardsuint256Updated pending rewards amount
newLastRewardBlockuint256Updated last reward block

validateMarginOperation

Validates margin operations and calculates new margin values

Validates margin addition/removal and calculates resulting margin ratio

Notes:

  • security: Validates margin constraints and limits

  • validation: Ensures margin operations are within limits

  • state-changes: None (pure function)

  • events: None

  • errors: Throws InsufficientMargin or validation errors

  • reentrancy: Not applicable - pure function

  • access: External pure function

  • oracle: Not applicable

function validateMarginOperation(
    uint256 currentMargin,
    uint256 amount,
    bool isAddition,
    uint256 minMarginRatio,
    uint256 positionSize,
    uint256 maxMargin
) external pure returns (uint256 newMargin, uint256 newMarginRatio);

Parameters

NameTypeDescription
currentMarginuint256Current margin amount for the position
amountuint256Amount of margin to add or remove
isAdditionboolTrue if adding margin, false if removing
minMarginRatiouint256Minimum margin ratio in basis points
positionSizeuint256Size of the position in USDC
maxMarginuint256Maximum margin per position

Returns

NameTypeDescription
newMarginuint256New margin amount after operation
newMarginRatiouint256New margin ratio after operation

generateLiquidationCommitment

Generates a unique liquidation commitment hash

Creates a commitment hash for MEV protection in liquidation process

Notes:

  • security: No security validations required for pure function

  • validation: None required for pure function

  • state-changes: None (pure function)

  • events: None

  • errors: None

  • reentrancy: Not applicable - pure function

  • access: External pure function

  • oracle: Not applicable

function generateLiquidationCommitment(address hedger, uint256 positionId, bytes32 salt, address liquidator)
    external
    pure
    returns (bytes32);

Parameters

NameTypeDescription
hedgeraddressAddress of the hedger whose position will be liquidated
positionIduint256ID of the position to liquidate
saltbytes32Random salt for commitment uniqueness
liquidatoraddressAddress of the liquidator making the commitment

Returns

NameTypeDescription
<none>bytes32Commitment hash for liquidation process

Structs

PositionData

struct PositionData {
    uint256 positionSize;
    uint256 margin;
    uint256 entryPrice;
    uint32 entryTime;
    uint32 lastUpdateTime;
    int128 unrealizedPnL;
    uint16 leverage;
    bool isActive;
}

HedgerData

struct HedgerData {
    uint256 totalMargin;
    uint256 totalExposure;
    uint128 pendingRewards;
    uint64 lastRewardClaim;
    bool isActive;
}