Long Pool Integration

1. Rebalance Operations

For long pools, the rebalance operation has to be invoked through FxUSDBasePool.

Rebalance can only be operated when debt ratio is between the values fetched in getRebalanceRatios

As for tokenIn address, both fxUSD and USDC tokens are supported. The FxUSDBasePool contract uses the _beforeRebalanceOrLiquidate function to handle token conversion:

function _beforeRebalanceOrLiquidate(
    address tokenIn,
    uint256 maxAmount
) internal view returns (RebalanceMemoryVar memory op) {
    op.stablePrice = getStableTokenPriceWithScale();
    op.totalYieldToken = totalYieldToken;
    op.totalStableToken = totalStableToken;

    uint256 amountYieldToken = op.totalYieldToken;
    uint256 amountStableToken;
    
    if (tokenIn == yieldToken) {
        // User pays fxUSD - direct usage
        // ... fxUSD handling logic
    } else {
        // User pays USDC - convert to USD equivalent
        uint256 maxAmountInUSD = (maxAmount * op.stablePrice) / PRECISION;
        if (maxAmountInUSD < amountYieldToken) {
            amountYieldToken = maxAmountInUSD;
        } else {
            amountStableToken = ((maxAmountInUSD - amountYieldToken) * PRECISION) / op.stablePrice;
        }
    }
    
    // Ensure we don't exceed available stable tokens
    if (amountStableToken > op.totalStableToken) {
        amountStableToken = op.totalStableToken;
    }

    op.yieldTokenToUse = amountYieldToken;
    op.stableTokenToUse = amountStableToken;
}

1.1 Pool-Wide Rebalance

Usage Example:

1.2 Tick-Specific Rebalance

Notice: The tick provided must always be a root tickβ€”i.e., its node's parent must be 0. Otherwise, you need to trace the tick upward to identify the corresponding root tick.

Usage Example:

2. Liquidation Operations

Rebalance can only be operated when debt ratio is between the values fetched in getLiquidateRatios

Contract Interface

Usage Example:

3. CreditNote Received

If it happens that the long pool does not have enough collateral in the contract (it is still overcollateralized, but the collateral is held elsewhere β€” for example, lent to short pools), you may receive the CreditNote from the long pool instead of the collateral asset. In this case, you can use redeemByCreditNote to obtain the collateral asset.

  • for WBTC pools, you may receive fxBTC: 0xB25a554033C59e33e48c5dc05A7192Fb1bbDdfc6

  • for wstETH pools, you may receive fxETH: 0x7c5350BaC0eB97F86A366Ee4F9619a560480F05A

Please double-check those addresses before using them.

For more details, see CreditNotes

Last updated