Clearing Operations

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;
Parameter
Description

lendingPoolAddress

Address of the lending pool

targetEpoch

Epoch number for which clearing is executed

priorityCalculationBatchSize

Number of users to process per transaction in Step 2

acceptRequestsBatchSize

Number of users to process per transaction in Step 4

isConfigOverridden

Set to true to use clearingConfigOverride values

Clearing Configuration Override

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.

Last updated