Liquidation Bot
Build a Lotus liquidation bot that monitors positions, encodes lmData, executes liquidations, and uses flash-funded callbacks with profitability checks.
Prerequisites
Step 1 — Monitor Positions
import { ethers } from "ethers";
const provider = new ethers.JsonRpcProvider(RPC_URL);
const signer = new ethers.Wallet(PRIVATE_KEY, provider);
const lotus = new ethers.Contract(LOTUS_ADDRESS, lotusAbi, signer);
const ORACLE_PRICE_SCALE = 10n ** 36n;
const WAD = 10n ** 18n;
async function findUnhealthyPositions(marketId: string, marketParams: any) {
const numTranches = await lotus.getNumMarketTranches(marketId);
for (let t = 0; t < numTranches; t++) {
// Use accrueInterestAndReturnMaxBorrow for accurate health checks
const borrowers = await getActiveBorrowers(marketId, t); // from indexed events
for (const borrower of borrowers) {
const position = await lotus.getPosition(marketId, borrower);
const borrowShares = position.borrowShares[t];
if (borrowShares === 0n) continue;
const tranches = await lotus.getMarketTranches(marketId);
const tranche = tranches[t];
const borrowed = borrowShares * tranche.trancheBorrowAssets / tranche.trancheBorrowShares;
const oracle = new ethers.Contract(marketParams.oracles[t], oracleAbi, provider);
const price = await oracle.price();
const collateral = position.collateral[t];
const collateralValue = collateral * price / ORACLE_PRICE_SCALE;
const maxBorrow = collateralValue * marketParams.lltvs[t] / WAD;
if (borrowed > maxBorrow) {
console.log(`Unhealthy: ${borrower} tranche ${t}`);
await executeLiquidation(marketParams, t, borrower, borrowShares, price);
}
}
}
}Step 2 — Encode lmData
Step 3 — Execute Liquidation
Step 4 — Flash-Funded Liquidation
Profitability Calculation
See Also
Last updated

