DeltaPrime
Search…
⌃K

How to run your own liquidation bot

Liquidation is an action of forcibly repaying part of the debt in order to bring loan back to a solvent state. It is only possible to perform this action for account that are insolvent (LTV > 500%).
In order to perform liquidations one first needs to obtain SmartLoan(s) address(es) which can be achieved by interacting with the SmartLoansFactoryTUP (TransparentUpgradeableProxy) that is deployed on the Avalanche mainnet under the address 0xf3cdfA877bB0615b50D066e41404668f016feE1E. The SmartLoansFactory.sol contract exposes a public getAllLoans() method that returns an array containing all SmartLoans addresses.
With the use of a SmartLoan address one can call that contract's getFullLoanStatus() returns an array consisting of the following [totalValue, debt, LTV, isSolvent]. To execute a liquidation of a chosen loan one has to execute the SmartLoan.sol liquidateLoan(uint256 repayAmount) method. The repayAmount parameter specifies how much AVAX needs to be repaid in order to bring the loan back to a solvent state. repayAmount should be calculated in a way that after a liquidation the LTV will be greater than minSelloutLtv (can be obtained by calling the getMinSelloutLtv() method of a SmartLoan.sol) and smaller than maxLtv (can be obtained by calling the getMaxLtv() method of a SmartLoan.sol) In order to be able to calculate the repayAmount you can transform the formula used to calculate the LTV that is: debt/collateral where debt = amount of AVAX borrowed, collateral = totalValue - debt. There is a 10% liquidation bonus that will be transferred to the liquidator at the end of a successful liquidation. This bonus has to be accounted for in the repayAmount calculations as well.
The final formula to achieve a targetLTV (inbetween minSelloutLtv and maxLtv) is the following:
(targetLTV(totalValuedebt)debt)/(targetLTVbonus1)(targetLTV * (totalValue - debt) - debt) / (targetLTV * bonus - 1)
An example javascript implementation of a liquidation bot can be found here.