Created
July 27, 2023 17:10
-
-
Save bayological/0e321d60f09f0587bd33648d6ef73fcb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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