Orbit Documentation
  • Github
  • Protocol Overview
    • What is Orbit Protocol
    • Ecosystem Participants
    • Smart Contracts
    • Whitepaper
    • Litepaper
  • Technical Documentation
    • Treasury
    • Bridge Oracle
    • PegKeeper
    • DAO
    • System Diagram
Powered by GitBook
On this page
  • Operational Mechanism
  • Liquidation and Trading
  • Liquidation Process
  • AMM Integration
  • Contract Initialization
  • Error Handling
  • Events
  • Operational Flow
  1. Technical Documentation

PegKeeper

The Pegkeeper contract is a critical component of the Orbit Protocol that maintains stablecoin pegs through automated market operations. It executes liquidations and performs AMM trades to ensure stablecoin stability.

Operational Mechanism

The Pegkeeper works by participating in liquidation auctions and trading the acquired collateral, generating profits while simultaneously helping maintain stablecoin pegs.

Liquidation and Trading

The core functionality of the Pegkeeper is implemented in the fl_receive() function:

fn fl_receive(
    e: Env, 
    token: Address,              // Stablecoin address
    amount: i128,                // Received amount
    blend_pool: Address,         // Blend pool address
    auction: Address,            // Auction contract address
    collateral_token: Address,   // Collateral token address
    lot_amount: i128,            // Amount of collateral to withdraw
    liq_amount: i128,            // Liquidation percentage (0-100)
    amm: Address,                // AMM address for swaps
    min_profit: i128,            // Minimum acceptable profit
    fee_taker: Address           // Address to receive profits
)

When this function is called, it performs a sequence of operations:

  1. Verifies that the call is authorized by the Treasury

  2. Records the initial token balances

  3. Participates in the liquidation auction through the Blend pool

  4. Verifies that the liquidation successfully closed the position

  5. Swaps the acquired collateral for the original token

  6. Calculates the profit from the operation

  7. Ensures the operation is profitable

  8. Transfers the profit to the fee taker

  9. Approves the Treasury to take back the original amount

  10. Emits an event recording the operation

All of these steps must complete successfully, or the function will revert with an appropriate error.

Liquidation Process

The Pegkeeper uses the helper liquidate() function to participate in liquidation auctions:

fn liquidate(
    e: &Env,
    auction_creator: Address,
    token_a: Address,
    token_a_bid_amount: i128,
    token_b: Address,
    token_b_lot_amount: i128,
    blend_pool: Address,
    liq_amount: i128
) -> Positions

This function:

  1. Creates a set of liquidation requests:

    • An auction request with the liquidation percentage

    • A repayment request with the bid amount

    • A collateral withdrawal request for the expected lot

  2. Transfers the bid amount to the Blend pool

  3. Submits the requests to the Blend pool

  4. Returns the remaining positions after liquidation

AMM Integration

The Pegkeeper integrates with AMMs through the helper swap() function:

fn swap(
    e: &Env,
    pair: Address,
    token_a: Address,
    token_b: Address,
    amount_a: i128,
    amount_b: i128
)

This function:

  1. Gets the router address from storage

  2. Creates a token path from token A to token B

  3. Transfers token A to the pair contract

  4. Executes the swap through the router

  5. Sets the minimum output amount and deadline

Contract Initialization

The Pegkeeper is initialized with two key parameters:

fn __constructor(e: Env, treasury: Address, router: Address)

During initialization:

  • The Treasury address is stored for authorization

  • The router address is stored for AMM operations

  • An initialization event is emitted

Error Handling

The Pegkeeper contract may produce the following errors:

  • NotProfitable (1505): The operation did not generate the minimum required profit

  • PositionStillOpen (1507): The liquidation did not successfully close the position

Events

The Pegkeeper emits events for tracking:

  • init: Contract initialization with Treasury and router addresses

  • fl_receive: Successful operation with token and amount details

Operational Flow

The complete operational flow of a Pegkeeper operation is as follows:

  1. The user identifies a liquidatable position in a Blend pool

  2. The user calls keep_peg() on the Treasury contract, which:

    • Mints the required amount of stablecoins

    • Calls fl_receive() on the Pegkeeper

    • Expects repayment of the amount

  3. The Pegkeeper executes the liquidation and swap

  4. If profitable, the Pegkeeper:

    • Transfers the profit to the fee taker

    • Approves the Treasury to take back the original amount

  5. The Treasury verifies repayment and burns the tokens

PreviousBridge OracleNextDAO

Last updated 18 days ago