// SPDX-License-Identifier: GPL-2.0-or-later pragma solidity =0.7.6; pragma abicoder v2; import '@airdao/astra-cl-periphery/contracts/base/PeripheryImmutableState.sol'; import '@airdao/astra-cl-core/contracts/libraries/SafeCast.sol'; import '@airdao/astra-cl-core/contracts/libraries/TickMath.sol'; import '@airdao/astra-cl-core/contracts/libraries/TickBitmap.sol'; import '@airdao/astra-cl-core/contracts/interfaces/IAstraCLPool.sol'; import '@airdao/astra-cl-core/contracts/interfaces/callback/IAstraCLSwapCallback.sol'; import '@airdao/astra-cl-periphery/contracts/libraries/Path.sol'; import '@airdao/astra-cl-periphery/contracts/libraries/PoolAddress.sol'; import '@airdao/astra-cl-periphery/contracts/libraries/CallbackValidation.sol'; import '@airdao/astra-contracts/contracts/core/interfaces/IAstraPair.sol'; import '../base/ImmutableState.sol'; import '../interfaces/IMixedRouteQuoterV1.sol'; import '../libraries/PoolTicksCounter.sol'; import '../libraries/AstraClassicLibrary.sol'; /// @title Provides on chain quotes for CL, Classic, and MixedRoute exact input swaps /// @notice Allows getting the expected amount out for a given swap without executing the swap /// @notice Does not support exact output swaps since using the contract balance between exactOut swaps is not supported /// @dev These functions are not gas efficient and should _not_ be called on chain. Instead, optimistically execute /// the swap and check the amounts in the callback. contract MixedRouteQuoterV1 is IMixedRouteQuoterV1, IAstraCLSwapCallback, PeripheryImmutableState { using Path for bytes; using SafeCast for uint256; using PoolTicksCounter for IAstraCLPool; address public immutable factoryClassic; /// @dev Value to bit mask with path fee to determine if Classic or CL route // max CL fee: 000011110100001001000000 (24 bits) // mask: 1 << 23 = 100000000000000000000000 = decimal value 8388608 uint24 private constant flagBitmask = 8388608; /// @dev Transient storage variable used to check a safety condition in exact output swaps. uint256 private amountOutCached; constructor( address _factory, address _factoryClassic, address _SAMB ) PeripheryImmutableState(_factory, _SAMB) { factoryClassic = _factoryClassic; } function getPool( address tokenA, address tokenB, uint24 fee ) private view returns (IAstraCLPool) { return IAstraCLPool(PoolAddress.computeAddress(factory, PoolAddress.getPoolKey(tokenA, tokenB, fee))); } /// @dev Given an amountIn, fetch the reserves of the Classic pair and get the amountOut function getPairAmountOut( uint256 amountIn, address tokenIn, address tokenOut ) private view returns (uint256) { (uint256 reserveIn, uint256 reserveOut) = AstraClassicLibrary.getReserves(factoryClassic, tokenIn, tokenOut); return AstraClassicLibrary.getAmountOut(amountIn, reserveIn, reserveOut); } /// @inheritdoc IAstraCLSwapCallback function astraCLSwapCallback( int256 amount0Delta, int256 amount1Delta, bytes memory path ) external view override { require(amount0Delta > 0 || amount1Delta > 0); // swaps entirely within 0-liquidity regions are not supported (address tokenIn, address tokenOut, uint24 fee) = path.decodeFirstPool(); CallbackValidation.verifyCallback(factory, tokenIn, tokenOut, fee); (bool isExactInput, uint256 amountReceived) = amount0Delta > 0 ? (tokenIn < tokenOut, uint256(-amount1Delta)) : (tokenOut < tokenIn, uint256(-amount0Delta)); IAstraCLPool pool = getPool(tokenIn, tokenOut, fee); (uint160 CLSqrtPriceX96After, int24 tickAfter, , , , , ) = pool.slot0(); if (isExactInput) { assembly { let ptr := mload(0x40) mstore(ptr, amountReceived) mstore(add(ptr, 0x20), CLSqrtPriceX96After) mstore(add(ptr, 0x40), tickAfter) revert(ptr, 0x60) } } else { /// since we don't support exactOutput, revert here revert('Exact output quote not supported'); } } /// @dev Parses a revert reason that should contain the numeric quote function parseRevertReason(bytes memory reason) private pure returns ( uint256 amount, uint160 sqrtPriceX96After, int24 tickAfter ) { if (reason.length != 0x60) { if (reason.length < 0x44) revert('Unexpected error'); assembly { reason := add(reason, 0x04) } revert(abi.decode(reason, (string))); } return abi.decode(reason, (uint256, uint160, int24)); } function handleCLRevert( bytes memory reason, IAstraCLPool pool, uint256 gasEstimate ) private view returns ( uint256 amount, uint160 sqrtPriceX96After, uint32 initializedTicksCrossed, uint256 ) { int24 tickBefore; int24 tickAfter; (, tickBefore, , , , , ) = pool.slot0(); (amount, sqrtPriceX96After, tickAfter) = parseRevertReason(reason); initializedTicksCrossed = pool.countInitializedTicksCrossed(tickBefore, tickAfter); return (amount, sqrtPriceX96After, initializedTicksCrossed, gasEstimate); } /// @dev Fetch an exactIn quote for a CL Pool on chain function quoteExactInputSingleCL(QuoteExactInputSingleCLParams memory params) public override returns ( uint256 amountOut, uint160 sqrtPriceX96After, uint32 initializedTicksCrossed, uint256 gasEstimate ) { bool zeroForOne = params.tokenIn < params.tokenOut; IAstraCLPool pool = getPool(params.tokenIn, params.tokenOut, params.fee); uint256 gasBefore = gasleft(); try pool.swap( address(this), // address(0) might cause issues with some tokens zeroForOne, params.amountIn.toInt256(), params.sqrtPriceLimitX96 == 0 ? (zeroForOne ? TickMath.MIN_SQRT_RATIO + 1 : TickMath.MAX_SQRT_RATIO - 1) : params.sqrtPriceLimitX96, abi.encodePacked(params.tokenIn, params.fee, params.tokenOut) ) {} catch (bytes memory reason) { gasEstimate = gasBefore - gasleft(); return handleCLRevert(reason, pool, gasEstimate); } } /// @dev Fetch an exactIn quote for a Classic pair on chain function quoteExactInputSingleClassic(QuoteExactInputSingleClassicParams memory params) public view override returns (uint256 amountOut) { amountOut = getPairAmountOut(params.amountIn, params.tokenIn, params.tokenOut); } /// @dev Get the quote for an exactIn swap between an array of Classic and/or CL pools /// @notice To encode a Classic pair within the path, use 0x800000 (hex value of 8388608) for the fee between the two token addresses function quoteExactInput(bytes memory path, uint256 amountIn) public override returns ( uint256 amountOut, uint160[] memory CLSqrtPriceX96AfterList, uint32[] memory CLInitializedTicksCrossedList, uint256 CLSwapGasEstimate ) { CLSqrtPriceX96AfterList = new uint160[](path.numPools()); CLInitializedTicksCrossedList = new uint32[](path.numPools()); uint256 i = 0; while (true) { (address tokenIn, address tokenOut, uint24 fee) = path.decodeFirstPool(); if (fee & flagBitmask != 0) { amountIn = quoteExactInputSingleClassic( QuoteExactInputSingleClassicParams({tokenIn: tokenIn, tokenOut: tokenOut, amountIn: amountIn}) ); } else { /// the outputs of prior swaps become the inputs to subsequent ones ( uint256 _amountOut, uint160 _sqrtPriceX96After, uint32 _initializedTicksCrossed, uint256 _gasEstimate ) = quoteExactInputSingleCL( QuoteExactInputSingleCLParams({ tokenIn: tokenIn, tokenOut: tokenOut, fee: fee, amountIn: amountIn, sqrtPriceLimitX96: 0 }) ); CLSqrtPriceX96AfterList[i] = _sqrtPriceX96After; CLInitializedTicksCrossedList[i] = _initializedTicksCrossed; CLSwapGasEstimate += _gasEstimate; amountIn = _amountOut; } i++; /// decide whether to continue or terminate if (path.hasMultiplePools()) { path = path.skipToken(); } else { return (amountIn, CLSqrtPriceX96AfterList, CLInitializedTicksCrossedList, CLSwapGasEstimate); } } } }