Liquidation Bot
How does the Liquidation Bot work?
The liquidation bot comprises two distinct processes: one utilizes Mirai to source accounts, while the other involves conducting liquidation simulations on accounts with a health score below 1.
1. Sourcing of Accounts
To obtain a comprehensive list of accounts utilizing Mirai, the liquidation bot employs two steps:
The bot utilizes an Apollo client, executing a query to retrieve all account IDs, thus providing a complete list of accounts utilizing Mirai.
Mirai leverages Subgraph to monitor all accounts interacting with its protocol. Subgraph specifically tracks entities identified as "Account," enabling the identification of all accounts associated with Mirai.
The bot utilizes an Apollo client, executing a query to retrieve all account IDs, thus providing a complete list of accounts utilizing Mirai.
After compiling a list of accounts, the bot proceeds to employ MiraiLens, a smart contract, to obtain the health score of each account. MiraiLens includes a function named getAccountStatus. This function requires an account address as a parameter and returns the health score of the account along with other pertinent details.
In order to minimize waiting periods, it is possible to utilize multicall to obtain the health scores of all accounts through a single smart contract call.
Aside from health scores, acquiring data pertaining to the markets that Mirai Protocol users are utilizing is crucial for liquidation purposes. To accomplish this, the doQueryAccountLiquidity function available through the Mirai General view Contract can be employed.
The second parameter of this function involves submitting an array of account addresses alongside the Mirai address. Upon execution, it returns the corresponding market data for the specified accounts.
All of the aforementioned data is stored in Redis, a database optimized for swift and effective data storage and retrieval. This approach facilitates easy and rapid access to the information whenever it is required
Liquidation Simulations
Getting Account Details
The bot begins by gathering account details using Redis read functions, which enable it to obtain information on all the accounts it needs to monitor.
Cartesian product of the user's collateral and liability markets It then generates the Cartesian product of the user's collateral and liability markets with each other. For example, if the user's collateral markets are ["USDT", "DAI"] and liabilities are ["WETH", "WMATIC"], the resulting Cartesian products would look like this:
Getting Repayment Amount Next, the bot loops through this array and calls the checkLiquidation function on the Mirai liquidation contract for each pair of collateral and liability addresses. This function returns the amount that needs to be repaid to settle the debt.
Generating Swap Path
In Mirai Liquidation happens as follows
Liquidator receives the appropriate amount of both collateral tokens and debt tokens of user
Liquidator swaps collateral into debt tokens using Uniswap handler
Swapped debt tokens are used to repay loans
Uniswap v3 introduced multiple pools for each token pair with different swapping fees
Four fee tiers: 0.01%, 0.05%, 0.30%, and 1%
You can find more information here Uniswap Pool Fees Tiers
After creating an array of swap paths for each pair in the Cartesian product of the user's collateral and liability markets that we already generated in step 2
Bot Loops through an array of swap paths to simulate the Liquidation process by doing the static calls to contract
An array of swap paths is created for each pair in the cartesian product of the user's collateral and liability markets
Bot loops through an array of swap paths to simulate the liquidation process by doing static calls to contract
Static Calls made to contract:
balanceOfUnderlying function on eToken Contract: returns eToken balance before liquidation
Liquidate function on liquidation contract: after executing, liquidator receives appropriate e-tokens and d-tokens
swapAndRepay function on swapHub contract: converts e-tokens into the appropriate amount of d-tokens to cover debt and pays debt
exitMarket on markets contract
Last updated