Liquidation Bot

Dependencies

How to install and run it

Cloning the project:
git clone [email protected]:exactly-protocol/liquidation-bot.git
Deploying flash loan contracts:
npm i --legacy-peer-deps
npx hardhat --network <network> deploy
Running the project:
A .env file should be created on the root directory of the project with the following parameters:
CHAIN_ID=[ID of the chain used]
ETH_[CHAIN_NAME]_PROVIDER=[Link to the RPC provider]
PRIVATE_KEY=[WALLET PRIVATE KEY]
After the .env file has been created, run the project with the command:
cargo run

How it works

The liquidation bot works by analyzing the users' positions using the events emitted by the protocol with the bare minimum number of calls to the contracts, thus making the bot more efficient in recreating such states.
After connecting to the RPC provider through a WebSocket, the bot subscribes to receive the events stream.
Each one of those events are parsed and transcribed into the user's data.
Whenever there's an idle moment on receiving new events, the bot does a check for liquidations.
If a position is in a state to be liquidated (with a Health Factor less than 1), the liquidation function in the flash loan contract is called.
Important: to avoid reentrancy issues on Uniswap's contract the bot must pick a pair of assets that is not the same pair of debt/collateral.
After this call the bot liquidates the user's debt seizing the collateral with the highest value.
After the liquidations the bot waits for more events and recreate the user's positions.

Flash loan contract

The flash loan contract calls the liquidation function in the protocol.
It checks its amount on the specific debt asset available on contract's balance to repay the user's debt. In case it has less than the amount needed to liquidate the user, it does as follow:
  1. 1.
    Borrow on Uniswap V3 the difference between what it has and the user's debt
  2. 2.
    Waits for a callback notifying it that the amount was received
  3. 3.
    Repays the debt
  4. 4.
    Receives the collateral
  5. 5.
    Swaps it to the same as the user's debt
  6. 6.
    Repays Uniswap V3

Structure

The project is structured as follows:
  • main.rs
    • Setups the bot to connect correctly to RPC Provider.
    • Starts the service and handles most of the errors.
  • service.rs: It's where most of the tasks are executed.
    • A subscription to the event's stream is made on the main thread
    • Each event received is parsed
    • Positions are created
    • A debounce for idleness is made in another thread
    • When the bot is idle, this thread checks for liquidations
  • borrower.rs: This structure is used to store users' data.
  • exactly_oracle.rs: Helper to access price protocol used by the .protocol.
  • fixed_lender.rs: Stores updated information created by the protocol's emitted events about all the markets.
  • exactly_events.rs: Redirect the events to suitable structures.
  • config.rs: Handle environment variables such as RPC provider link access, wallet's private key, etc.
Copy link
On this page
Dependencies
How to install and run it
How it works
Structure