Skip to content

Instantly share code, notes, and snippets.

@bayological
Created July 27, 2023 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bayological/0e321d60f09f0587bd33648d6ef73fcb to your computer and use it in GitHub Desktop.
Save bayological/0e321d60f09f0587bd33648d6ef73fcb to your computer and use it in GitHub Desktop.
/**
* @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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment