Quantillon Protocol

HedgerPoolAccountingLibrary

Git Source

Title: HedgerPoolAccountingLibrary

Existing pool accounting extracted to keep HedgerPool deployable.

Delegatecalled with explicit storage references; correction replay protection uses a dedicated hashed namespace.

Constants

LEGACY_CORRECTION_SLOT

bytes32 private constant LEGACY_CORRECTION_SLOT = keccak256("quantillon.storage.LegacyMarginCorrection.20260701")

LEGACY_CORRECTION_ID

bytes32 private constant LEGACY_CORRECTION_ID = keccak256("base:48054036:hedger-profit:388448")

LEGACY_POOL

address private constant LEGACY_POOL = 0xff5D7cE5c7671B2EA805Ee752B4f8eC9Ecf2975A

LEGACY_SAFE

address private constant LEGACY_SAFE = 0x1d7fF432a93d0085Fb69474c7E567f859829e6cd

LEGACY_CREDIT

uint256 private constant LEGACY_CREDIT = 388448

Functions

routeFee

Routes collected fees between the reward reserve and collector.

Reserve amounts remain in the calling pool.

Notes:

  • security: Caller validates the configured split and recipient.

  • validation: A zero fee is a no-op.

  • state-changes: Transfers the collector portion and updates allowance.

  • events: RewardReserveFunded and collector events.

  • errors: Propagates token and collector failures.

  • reentrancy: Calling pool holds its reentrancy guard.

  • access: Linked library.

  • oracle: None.

function routeFee(
    IERC20 usdc,
    address collector,
    uint256 split,
    address funder,
    uint256 fee,
    string memory sourceType
) external;

Parameters

NameTypeDescription
usdcIERC20Collateral token.
collectoraddressFee collector address.
splituint256Reserve fraction scaled by 1e18.
funderaddressAccount credited with funding the reserve.
feeuint256Collected fee in USDC units.
sourceTypestringCollector accounting category.

assignSingleHedger

Assigns the configured owner with the existing inactive-position restriction.

Extracted unchanged to preserve EIP-170 headroom for the correction entry point.

Notes:

  • security: Helper enforces the existing governance role.

  • validation: Nonzero recipient and no active position when replacing an owner.

  • state-changes: None; wrapper stores returned owner.

  • events: SingleHedgerRotationApplied.

  • errors: InvalidAddress, HedgerHasActivePosition.

  • reentrancy: No external calls.

  • access: Linked library only.

  • oracle: No oracle dependency.

function assignSingleHedger(HedgerPool.HedgePosition storage position, address next) external returns (address);

Parameters

NameTypeDescription
positionHedgerPool.HedgePositionFixed position one, used only to read its active flag.
nextaddressRequested owner.

Returns

NameTypeDescription
<none>addressConfigured owner after assignment.

correctLegacyMargin

Reclassifies exactly the verified historical residue as current Safe margin once.

No deposit, withdrawal, mint, cost-basis change or realized-PnL adjustment occurs. Exact surplus and opening-block checks reject drift or reuse for another position.

Notes:

  • security: Fixed chain/pool/owner/position and governance, pause and replay guards.

  • validation: Verifies global totals, supply/backing dust and exact existing surplus.

  • state-changes: Updates margin, position size and namespaced one-time marker.

  • events: LegacyMarginCorrected.

  • errors: LegacyCorrectionUnauthorized, LegacyCorrectionStateMismatch, LegacyCorrectionAlreadyApplied.

  • reentrancy: Pool wrapper is nonReentrant; external calls are static reads.

  • access: Linked library invoked by the fixed Base pool through delegatecall.

  • oracle: Independent of oracle price and cached valuation.

function correctLegacyMargin(HedgerPool.HedgePosition storage position)
    external
    returns (uint256 nextTotalMargin, uint256 nextTotalExposure);

Parameters

NameTypeDescription
positionHedgerPool.HedgePositionThe pool's fixed active position one.

Returns

NameTypeDescription
nextTotalMarginuint256Global margin after correction.
nextTotalExposureuint256Global nominal exposure after correction.

validateClosure

Preserves the pool's existing restriction on closing while users remain exposed.

Extracted unchanged; bounded QEURO dust is treated as an empty supply.

Notes:

  • security: Read-only closure guard.

  • validation: Requires sufficient remaining hedger margin when QEURO is outstanding.

  • state-changes: None.

  • events: None.

  • errors: PositionClosureRestricted.

  • reentrancy: External static reads only.

  • access: Linked library invoked by HedgerPool.

  • oracle: No price dependency.

function validateClosure(IQuantillonVault vault, uint256 positionMargin) external view;

Parameters

NameTypeDescription
vaultIQuantillonVaultConfigured vault.
positionMarginuint256Closing position's recorded margin.

configure

Validates and applies the existing risk and fee configuration.

Scalar settings outside CoreParams remain assigned by the pool.

Notes:

  • security: Pool wrappers enforce caller authority and storage references.

  • validation: Uses the pool's existing validation rules.

  • state-changes: Updates only the explicitly supplied accounting storage.

  • events: None.

  • errors: Propagates accounting, validation and trusted integration errors.

  • reentrancy: State-changing pool wrappers are nonReentrant except governance configuration.

  • access: Linked library invoked by HedgerPool through delegatecall.

  • oracle: No oracle lookup; prices, where present, are supplied by the caller.

function configure(
    HedgerPool.CoreParams storage params,
    HedgerPool.HedgerRiskConfig calldata cfg,
    HedgerPool.HedgePosition storage position
) external;

Parameters

NameTypeDescription
paramsHedgerPool.CoreParamsPool core parameters in storage.
cfgHedgerPool.HedgerRiskConfigRequested complete risk and fee configuration.
positionHedgerPool.HedgePositionActive position used to bound the margin-ratio setting.

effectiveCollateral

Values the active position using remaining cost and the caller's price.

Floors negative effective collateral at zero; realized history is already in margin.

Notes:

  • security: Read-only helper.

  • validation: No input validation required unless described above.

  • state-changes: None.

  • events: None.

  • errors: None beyond checked arithmetic.

  • reentrancy: No state-changing external calls.

  • access: Linked library helper.

  • oracle: No oracle lookup.

function effectiveCollateral(HedgerPool.HedgePosition storage position, uint256 price)
    external
    view
    returns (uint256);

Parameters

NameTypeDescription
positionHedgerPool.HedgePositionActive position storage.
priceuint256EUR/USD price with 18 decimals.

Returns

NameTypeDescription
<none>uint256collateral Effective USDC collateral, floored at zero.

version

Reports the initial library release.

Uses no additional sequential storage slots.

Notes:

  • security: Read-only helper.

  • validation: No input validation required unless described above.

  • state-changes: None.

  • events: None.

  • errors: None beyond checked arithmetic.

  • reentrancy: No state-changing external calls.

  • access: Linked library helper.

  • oracle: No oracle lookup.

function version() external pure returns (string memory);

Returns

NameTypeDescription
<none>stringrelease Semantic version string.

checkpoint

Books interest earned up to the current exposure change.

Checkpointing before every exposure mutation prevents a later, larger exposure from being applied retroactively to an earlier accrual period.

Notes:

  • security: Uses only explicitly supplied storage and accounting inputs.

  • validation: Rejects reward overflow through the shared error library.

  • state-changes: Updates the hedger's pending reward and accrual clock.

  • events: None.

  • errors: RewardOverflow on uint128 overflow.

  • reentrancy: No external calls.

  • access: Linked library invoked by HedgerPool.

  • oracle: No oracle dependency.

function checkpoint(
    HedgerPool.HedgerRewardState storage rewardState,
    mapping(address => uint256) storage rewardTimes,
    HedgerPool.CoreParams storage params,
    uint256 totalExposure,
    uint256 currentTime,
    uint256 maxRewardPeriod,
    address hedger
) external returns (uint256 pendingRewards);

Parameters

NameTypeDescription
rewardStateHedgerPool.HedgerRewardStateHedger reward state.
rewardTimesmapping(address => uint256)Per-hedger accrual clocks.
paramsHedgerPool.CoreParamsPool interest-rate configuration.
totalExposureuint256Exposure that was active during the elapsed period.
currentTimeuint256Canonical protocol time.
maxRewardPerioduint256Maximum accrual duration.
hedgeraddressHedger whose clock is being checkpointed.

Returns

NameTypeDescription
pendingRewardsuint256Updated pending interest reward.

claim

Accrues interest into withdrawal escrow and claims external yield.

Preserves the pool's legacy-clock migration, narrowing casts and pull settlement.

Notes:

  • security: Pool wrappers enforce caller authority and storage references.

  • validation: Uses the pool's existing validation rules.

  • state-changes: Updates only the explicitly supplied accounting storage.

  • events: None.

  • errors: Propagates accounting, validation and trusted integration errors.

  • reentrancy: State-changing pool wrappers are nonReentrant except governance configuration.

  • access: Linked library invoked by HedgerPool through delegatecall.

  • oracle: No oracle lookup; prices, where present, are supplied by the caller.

function claim(
    HedgerPool.HedgerRewardState storage rewardState,
    mapping(address => uint256) storage rewardTimes,
    mapping(address => uint256) storage pendingWithdrawals,
    HedgerPool.CoreParams storage params,
    uint256 totalExposure,
    uint256 currentTime,
    uint256 maxRewardPeriod,
    IYieldShift yieldShift
) external returns (uint256 interestDifferential, uint256 yieldShiftRewards, uint256 totalRewards);

Parameters

NameTypeDescription
rewardStateHedgerPool.HedgerRewardStateCurrent hedger reward state.
rewardTimesmapping(address => uint256)Per-hedger accrual clocks.
pendingWithdrawalsmapping(address => uint256)Per-hedger withdrawal escrow.
paramsHedgerPool.CoreParamsPool interest rate configuration.
totalExposureuint256Pool notional exposure.
currentTimeuint256Canonical protocol time.
maxRewardPerioduint256Maximum accrual duration.
yieldShiftIYieldShiftTrusted yield distributor.

Returns

NameTypeDescription
interestDifferentialuint256Interest queued for withdrawal.
yieldShiftRewardsuint256Yield paid by YieldShift.
totalRewardsuint256Sum of both reward sources.

withdraw

Pays the caller's funded escrow balance to their chosen recipient.

Leaves any unfunded remainder available for a later withdrawal.

Notes:

  • security: Pool wrappers enforce caller authority and storage references.

  • validation: Uses the pool's existing validation rules.

  • state-changes: Reduces caller escrow by funded amount and transfers USDC.

  • events: None.

  • errors: Propagates accounting, validation and trusted integration errors.

  • reentrancy: Invoked from the nonReentrant withdrawal wrapper.

  • access: Linked library invoked by HedgerPool through delegatecall.

  • oracle: No oracle lookup; prices, where present, are supplied by the caller.

function withdraw(mapping(address => uint256) storage pendingWithdrawals, IERC20 usdc, address recipient) external;

Parameters

NameTypeDescription
pendingWithdrawalsmapping(address => uint256)Per-hedger withdrawal escrow.
usdcIERC20Pool collateral token.
recipientaddressCaller-selected payout recipient.

applyMarginTransition

Applies realized profit or loss to margin and notional exposure.

Keeps aggregate exposure aligned with the resulting position size.

Notes:

  • security: Caller checkpoints rewards before changing exposure.

  • validation: Shared math validates position bounds.

  • state-changes: Updates position margin and size.

  • events: None.

  • errors: Propagates checked arithmetic errors.

  • reentrancy: No external state-changing calls.

  • access: Linked library.

  • oracle: Caller supplies settled profit or loss.

function applyMarginTransition(
    HedgerPool.HedgePosition storage pos,
    uint256 totalMargin,
    uint256 totalExposure,
    int256 realizedDelta
) external returns (uint256 nextMargin, uint256 nextExposure);

Parameters

NameTypeDescription
posHedgerPool.HedgePositionPosition to update.
totalMarginuint256Aggregate margin before the transition.
totalExposureuint256Aggregate exposure before the transition.
realizedDeltaint256Realized USDC profit or loss.

Returns

NameTypeDescription
nextMarginuint256Aggregate margin after settlement.
nextExposureuint256Aggregate exposure after settlement.

Events

LegacyMarginCorrected

event LegacyMarginCorrected(bytes32 indexed correctionId, address indexed hedger, uint256 amount);

SingleHedgerRotationApplied

event SingleHedgerRotationApplied(address indexed previousHedger, address indexed newHedger);

RewardReserveFunded

event RewardReserveFunded(address indexed funder, uint256 amount);

Errors

LegacyCorrectionUnauthorized

error LegacyCorrectionUnauthorized();

LegacyCorrectionStateMismatch

error LegacyCorrectionStateMismatch();

LegacyCorrectionAlreadyApplied

error LegacyCorrectionAlreadyApplied();