> For the complete documentation index, see [llms.txt](https://fxprotocol.gitbook.io/fx-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://fxprotocol.gitbook.io/fx-docs/developers/processing-the-rebalances-and-liquidations/keeper-bots.md).

# Keeper Bots

There is an open-source example keeper implementation available at [fx-keeper-example](https://github.com/starit/fx-protocol-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:

1. `(debt - x) / (price * (coll - y * (1 + incentive))) ≤ target_ratio`

where

* x = debt to be repaid
* y = collateral to be removed
* incentive = rebalance bounty ratio

2. `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](https://github.com/AladdinDAO/aladdin-dao-sdk/blob/920f3918384e2773257670731042501bde90c254/src/bots/fx/Rebalance.ts#L45)):

```
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](https://github.com/AladdinDAO/aladdin-dao-sdk/blob/920f3918384e2773257670731042501bde90c254/src/bots/fx/Liquidate.ts#L38)):

```
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 USDC
* Then 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);
      }
    }
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://fxprotocol.gitbook.io/fx-docs/developers/processing-the-rebalances-and-liquidations/keeper-bots.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
