Skip to content

Instantly share code, notes, and snippets.

@bayological
Created July 27, 2023 17:10

Revisions

  1. bayological created this gist Jul 27, 2023.
    35 changes: 35 additions & 0 deletions BiPoolManager.sol
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    /**
    * @title BiPoolExchangeManager
    * @notice An exchange manager that manages asset exchanges consisting of two assets
    */
    contract BiPoolManager {

    /**
    * @notice Execute a token swap with fixed amountIn
    * @param exchangeId The id of exchange, i.e. PoolExchange to use
    * @param tokenIn The token to be sold
    * @param tokenOut The token to be bought
    * @param amountIn The amount of tokenIn to be sold
    * @return amountOut The amount of tokenOut to be bought
    */
    function swapIn(
    bytes32 exchangeId,
    address tokenIn,
    address tokenOut,
    uint256 amountIn
    ) external onlyBroker returns (uint256 amountOut) {
    PoolExchange memory exchange = getPoolExchange(exchangeId);
    require(
    breakerBox.getRateFeedTradingMode(exchange.config.referenceRateFeedID) == TRADING_MODE_BIDIRECTIONAL,
    "Trading is suspended for this reference rate"
    );

    uint256 scaledAmountIn = amountIn.mul(tokenPrecisionMultipliers[tokenIn]);
    (uint256 scaledAmountOut, bool bucketsUpdated) = _getAmountOut(exchange, tokenIn, tokenOut, scaledAmountIn);
    executeSwap(exchangeId, exchange, tokenIn, scaledAmountIn, scaledAmountOut, bucketsUpdated);

    amountOut = scaledAmountOut.div(tokenPrecisionMultipliers[tokenOut]);
    return amountOut;
    }

    }