Pegkeeper Usage and Operational Flow

The Pegkeeper contract maintains stablecoin pegs through a series of coordinated market operations. This guide explains how to interact with the Pegkeeper and understand its operational flow.

Core Concepts

Flash Loans

  • Uncollateralized loans that must be repaid in the same transaction

  • Used by Pegkeeper to obtain stablecoins for liquidations

  • Provided by the Treasury contract

  • Include built-in profitability checks

Liquidation Auctions

  • Triggered by a user when a positions become undercollateralized

  • Allow purchase of collateral at a discount

  • Execute through the Blend pool interface

  • Require specific parameter calculations

AMM Integration

  • Used for converting collateral to stablecoins

  • Facilitates profitable trading

Operational Flow

  1. Liquidation Initiation done by user

    • Monitor Blend pool for liquidatable positions

    • Start liquidation

    • Calculate required parameters:

      • bid_amount: Flash loan size

      • lot_amount: Expected collateral

      • liq_amount: Liquidation percentage

  2. Treasury Interaction

    • Call keep_peg on Treasury contract

    • Example parameters:

    treasury.keep_peg(
        Symbol::new("fl_receive"),
        vec![
            token,           // Stablecoin address
            amount,          // Flash loan amount
            blend_pool,      // Blend pool address
            auction,         // Auction contract
            collateral_token, // Collateral token
            lot_amount,      // Expected collateral
            liq_amount,      // Liquidation percentage
            amm,            // AMM address
            fee_taker       // Profit recipient
        ]
    )
  3. Flash Loan Process

    • Treasury mints stablecoins

    • Tokens sent to Pegkeeper

    • Pegkeeper executes operations

    • Loan must be repaid with profit

  4. Liquidation Execution

    • Pegkeeper participates in auction

    • Acquires collateral at discount

    • Verifies collateral receipt

    • Tracks balance changes

  5. AMM Trading

    • Converts acquired collateral to stablecoins

    • Optimizes for minimal slippage

    • Ensures profitable conversion

    • Uses configured AMM router

  6. Profit Management

    • Calculates operation profit

    • Transfers excess to fee_taker

    • Approves loan repayment

    • Reverts if unprofitable

  7. Operation Completion

    • Treasury burns repaid tokens

    • Events emitted for tracking

    • State updates finalized

Last updated