Clearing Operations

Running clearing on a lending pool each epoch — the pre-clearing price and loyalty updates, every doClearing parameter, batched execution, and missed windows.

Role Required: ROLE_POOL_CLEARING_MANAGER Function: LendingPoolManager.doClearing()

Clearing must be executed for every lending pool, every epoch, during the clearing period. Before executing clearing, loyalty levels must be calculated for the current epoch.

Pre-Clearing Steps#

  1. Anyone calls SystemVariables.updateKsuEpochTokenPrice() to update the KSU price
  2. Anyone calls UserManager.batchCalculateUserLoyaltyLevels() to update loyalty levels

Clearing Parameters#

function doClearing(
    address lendingPoolAddress,
    uint256 targetEpoch,
    uint256 priorityCalculationBatchSize,
    uint256 acceptRequestsBatchSize,
    ClearingConfiguration calldata clearingConfigOverride,
    bool isConfigOverridden
) external;
ParameterDescription
lendingPoolAddressAddress of the lending pool
targetEpochEpoch number for which clearing is executed
priorityCalculationBatchSizeNumber of users to process per transaction in Step 2
acceptRequestsBatchSizeNumber of users to process per transaction in Step 4
isConfigOverriddenSet to true to use clearingConfigOverride values

Clearing Configuration Override#

struct ClearingConfiguration {
    uint256 drawAmount;
    uint256[] trancheDesiredRatios;
    uint256 maxExcessPercentage;
    uint256 minExcessPercentage;
}

These parameters override the pool's stored values for this clearing execution only.

Execution#

Clearing executes 5 steps sequentially (see Epochs & Clearing for details):

  1. Apply pool interest to tranches
  2. Calculate pending request priority
  3. Calculate accepted deposit/withdrawal amounts
  4. Process accepted requests
  5. Draw funds

Batched Execution#

Steps 2 and 4 may require multiple transactions if the number of requests exceeds block gas limits. Set priorityCalculationBatchSize and acceptRequestsBatchSize to control how many users are processed per call. Call doClearing() multiple times until all requests are processed.

Missing the Clearing Period#

If clearing is not completed during the clearing period:

  • Halted at Step 2: Call doClearing() to apply interest (Step 1) only. User requests are not processed for that epoch
  • Halted at Step 4: Some user requests have already been processed. The Pool Clearing Manager must finish executing clearing even after the clearing period has lapsed

In all cases, Step 1 (apply interest) must be processed for every epoch to maintain accounting continuity.

When to Execute#

Clearing should be executed every epoch during the clearing period for every active lending pool. The clearing period is the final 48 hours of each 7-day epoch.