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:
When this function is called, it performs a sequence of operations:
Verifies that the call is authorized by the Treasury
Records the initial token balances
Participates in the liquidation auction through the Blend pool
Verifies that the liquidation successfully closed the position
Swaps the acquired collateral for the original token
Calculates the profit from the operation
Ensures the operation is profitable
Transfers the profit to the fee taker
Approves the Treasury to take back the original amount
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:
This function:
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
Transfers the bid amount to the Blend pool
Submits the requests to the Blend pool
Returns the remaining positions after liquidation
AMM Integration
The Pegkeeper integrates with AMMs through the helper swap()
function:
This function:
Gets the router address from storage
Creates a token path from token A to token B
Transfers token A to the pair contract
Executes the swap through the router
Sets the minimum output amount and deadline
Contract Initialization
The Pegkeeper is initialized with two key parameters:
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 profitPositionStillOpen
(1507): The liquidation did not successfully close the position
Events
The Pegkeeper emits events for tracking:
init
: Contract initialization with Treasury and router addressesfl_receive
: Successful operation with token and amount details
Operational Flow
The complete operational flow of a Pegkeeper operation is as follows:
The user identifies a liquidatable position in a Blend pool
The user calls
keep_peg()
on the Treasury contract, which:Mints the required amount of stablecoins
Calls
fl_receive()
on the PegkeeperExpects repayment of the amount
The Pegkeeper executes the liquidation and swap
If profitable, the Pegkeeper:
Transfers the profit to the fee taker
Approves the Treasury to take back the original amount
The Treasury verifies repayment and burns the tokens
Last updated