Keeper Bots
There is an open-source example keeper implementation available at fx-keeper-example.
State Syncing
In this implementation, the bot relies on the stateSync class to synchronize states locally and reassemble the on-chain status. Based on the synchronized data, it calculates relevant metrics and executes rebalance and liquidate operations accordingly.
Calculate how much to rebalance
There are two known restrictions from the contracts:
(debt - x) / (price * (coll - y * (1 + incentive))) β€ target_ratio
where
x = debt to be repaid
y = collateral to be removed
incentive = rebalance bounty ratio
debt / (price * coll) >= target_ratio
From these conditions, we derive the following formula:
x β₯ (debt - target_ratio Γ price Γ coll) / (1 - target_ratio Γ (1 + incentive))Code implementation(source):
function getRawDebtToRebalance(tick: ITickToBalance): bigint {
const rawDebts =
(tick.rawDebts * PRECISION * PRECISION - tick.debtRatio * tick.price * tick.rawColls) /
(PRECISION * PRECISION - (PRECISION * tick.debtRatio * (FEE_PRECISION + tick.bonusRatio)) / FEE_PRECISION);
return rawDebts;
}Calculate how much to liquidate:
Based on the restrictions from the contract:
We can derive:
Code implementation(source):
redeemByCreditNote
When CreditNote is received, you may use redeemByCreditNote to redeem to the underlying assets. You can check the example code FxProtocolLongBatchExecutor contract of the bots to see a full process to how to integrate it.
First, in a flashloan, it use the borrowed fund to do rebalance/liquidation
After this, it checks the USDC balance
Then, it converts all the newly fetched collateral assets to USDC
Then, it checks CreditNote balance and use
redeemByCreditNoteto obtain the assets and get more USDCThen try to repay the borrowed funds to finish the flashloan
Last updated