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:
rawDebts / price * (1 + bonus) <= position.rawColls + balance
We can derive:
rawDebts <= (position.rawColls + balance) / (1 + bonus) * price
Code implementation(source):
function getRawDebtToLiquidate(position: IPositionToLiquidate, balance: bigint): bigint {
// rawDebts / price * (1 + bonus) <= position.rawColls + balance
// rawDebts <= (position.rawColls + balance) / (1 + bonus) * price
let rawDebts =
((((position.rawColls + balance) * position.price) / PRECISION) * FEE_PRECISION) /
(FEE_PRECISION + position.bonusRatio);
(position.rawColls * position.price) / PRECISION;
if (rawDebts > position.rawDebts) rawDebts = position.rawDebts;
return rawDebts;
}
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
redeemByCreditNote
to obtain the assets and get more USDCThen try to repay the borrowed funds to finish the flashloan
// do rebalance or liquidate
uint256 amountBase;
address pool;
if (callType == 0) {
(pool, amountBase, assets) = _doRebalance(tokenToUse, assets, userData);
} else if (callType == 1) {
(pool, amountBase, assets) = _doLiquidate(tokenToUse, assets, userData);
}
// swap base to USDC
uint256 usdcAmount = IERC20(USDC).balanceOf(address(this));
{
(, address swapTarget, bytes memory swapData) = abi.decode(userData[1:], (address, address, bytes));
IERC20(IPool(pool).collateralToken()).forceApprove(swapTarget, amountBase);
if (amountBase > 0) {
(bool success, ) = swapTarget.call(swapData);
_popupRevertReason(success);
}
}
usdcAmount = IERC20(USDC).balanceOf(address(this)) - usdcAmount;
// check if we have credit note
address shortPool = IPool(pool).counterparty();
if (shortPool != address(0)) {
address creditNote = IShortPool(shortPool).creditNote();
uint256 balance = IERC20(creditNote).balanceOf(address(this));
if (balance > 0) {
IERC20(creditNote).forceApprove(shortPoolManager, balance);
balance = IShortPoolManager(shortPoolManager).redeemByCreditNote(shortPool, balance, 0);
// swap fxUSD to USDC
usdcAmount += ICurveStableSwapNG(USDC_fxUSD_POOL).exchange(1, 0, balance, 0);
}
}
Last updated