Verified contractPartialGPL-2.0-or-later
SwapRouter
Address0x2e6cd2d30aa43f40aa81619ff4b6e0a41479b13f
- Balance
0 ETH
- Transactions
863
- Compiler
solc 0.7.6
- Same code
6 more contracts
Details
- Compiler
- solc 0.7.6+commit.7338295f
- Language
- solidity
- Optimization
- Yes, 1,000,000 runs
- EVM version
- istanbul
- License
- GPL-2.0-or-later
- Match
- Partialcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 18,062,260
- Creation transaction
- 0x00fb4044e0…e5e176fa84
- Main copy (most transactions)
- 0xe592427a…05861564
225 files
The source code is large: one file is shown at a time. Choose a file in the list.
- contracts/SwapRouter.solmain9.12 KB
- @openzeppelin/contracts/cryptography/ECDSA.sol3.9 KB
- @openzeppelin/contracts/drafts/EIP712.sol4.71 KB
- @openzeppelin/contracts/drafts/ERC20Permit.sol2.66 KB
- @openzeppelin/contracts/drafts/IERC20Permit.sol2.12 KB
- @openzeppelin/contracts/introspection/ERC165.sol1.69 KB
- @openzeppelin/contracts/introspection/IERC165.sol786 B
- @openzeppelin/contracts/math/SafeMath.sol7.22 KB
- @openzeppelin/contracts/math/SignedSafeMath.sol2.66 KB
- @openzeppelin/contracts/token/ERC20/ERC20.sol10.78 KB
- @openzeppelin/contracts/token/ERC20/IERC20.sol2.7 KB
- @openzeppelin/contracts/token/ERC721/ERC721.sol17.09 KB
- @openzeppelin/contracts/token/ERC721/IERC721.sol4.58 KB
- @openzeppelin/contracts/token/ERC721/IERC721Enumerable.sol936 B
- @openzeppelin/contracts/token/ERC721/IERC721Metadata.sol682 B
- @openzeppelin/contracts/token/ERC721/IERC721Receiver.sol858 B
- @openzeppelin/contracts/utils/Address.sol7.81 KB
- @openzeppelin/contracts/utils/Context.sol941 B
- @openzeppelin/contracts/utils/Counters.sol1.51 KB
- @openzeppelin/contracts/utils/EnumerableMap.sol9.73 KB
- @openzeppelin/contracts/utils/EnumerableSet.sol9.38 KB
- @openzeppelin/contracts/utils/Strings.sol930 B
- @uniswap/lib/contracts/libraries/AddressStringUtil.sol1.39 KB
- @uniswap/lib/contracts/libraries/SafeERC20Namer.sol3.83 KB
- @uniswap/v2-core/contracts/interfaces/IUniswapV2Pair.sol2.42 KB
- @uniswap/v3-core/contracts/interfaces/IUniswapV3Factory.sol4.04 KB
- @uniswap/v3-core/contracts/interfaces/IUniswapV3Pool.sol809 B
- @uniswap/v3-core/contracts/interfaces/callback/IUniswapV3FlashCallback.sol982 B
- @uniswap/v3-core/contracts/interfaces/callback/IUniswapV3MintCallback.sol980 B
- @uniswap/v3-core/contracts/interfaces/callback/IUniswapV3SwapCallback.sol1.29 KB
- @uniswap/v3-core/contracts/interfaces/pool/IUniswapV3PoolActions.sol6.3 KB
- @uniswap/v3-core/contracts/interfaces/pool/IUniswapV3PoolDerivedState.sol2.6 KB
- @uniswap/v3-core/contracts/interfaces/pool/IUniswapV3PoolEvents.sol5.96 KB
- @uniswap/v3-core/contracts/interfaces/pool/IUniswapV3PoolImmutables.sol1.81 KB
- @uniswap/v3-core/contracts/interfaces/pool/IUniswapV3PoolOwnerActions.sol1.19 KB
- @uniswap/v3-core/contracts/interfaces/pool/IUniswapV3PoolState.sol6.51 KB
- @uniswap/v3-core/contracts/libraries/BitMath.sol2.79 KB
- @uniswap/v3-core/contracts/libraries/FixedPoint128.sol311 B
- @uniswap/v3-core/contracts/libraries/FixedPoint96.sol380 B
- @uniswap/v3-core/contracts/libraries/FullMath.sol5.12 KB
- @uniswap/v3-core/contracts/libraries/LiquidityMath.sol622 B
- @uniswap/v3-core/contracts/libraries/LowGasSafeMath.sol1.7 KB
- @uniswap/v3-core/contracts/libraries/SafeCast.sol1.05 KB
- @uniswap/v3-core/contracts/libraries/Tick.sol9.52 KB
- @uniswap/v3-core/contracts/libraries/TickBitmap.sol4.11 KB
- @uniswap/v3-core/contracts/libraries/TickMath.sol8.64 KB
- @uniswap/v3-core/contracts/libraries/UnsafeMath.sol660 B
- base64-sol/base64.sol2.28 KB
- contracts/V3Migrator.sol4.11 KB
- contracts/base/BlockTimestamp.sol428 B
- contracts/base/ERC721Permit.sol3.33 KB
- contracts/base/LiquidityManagement.sol3.18 KB
- contracts/base/Multicall.sol964 B
- contracts/base/PeripheryImmutableState.sol574 B
- contracts/base/PeripheryPayments.sol2.46 KB
- contracts/base/PeripheryPaymentsWithFee.sol1.98 KB
- contracts/base/PeripheryValidation.sol297 B
- contracts/base/PoolInitializer.sol1.21 KB
- contracts/base/SelfPermit.sol2 KB
- contracts/examples/PairFlash.sol6.04 KB
- contracts/interfaces/IERC20Metadata.sol626 B
- contracts/interfaces/IERC721Permit.sol1.49 KB
- contracts/interfaces/IMulticall.sol705 B
- contracts/interfaces/INonfungiblePositionManager.sol8.78 KB
- contracts/interfaces/INonfungibleTokenPositionDescriptor.sol797 B
- contracts/interfaces/IPeripheryImmutableState.sol419 B
- contracts/interfaces/IPeripheryPayments.sol1.42 KB
- contracts/interfaces/IPeripheryPaymentsWithFee.sol1.18 KB
- contracts/interfaces/IPoolInitializer.sol1.18 KB
- contracts/interfaces/IQuoter.sol2.84 KB
- contracts/interfaces/IQuoterV2.sol5.06 KB
- contracts/interfaces/ISelfPermit.sol3.88 KB
- contracts/interfaces/ISwapRouter.sol2.72 KB
- contracts/interfaces/ITickLens.sol1.11 KB
- contracts/interfaces/IV3Migrator.sol1.6 KB
- contracts/interfaces/external/IERC1271.sol873 B
- contracts/interfaces/external/IERC20PermitAllowed.sol1.2 KB
- contracts/interfaces/external/IWETH9.sol376 B
- contracts/lens/Quoter.sol6.77 KB
- contracts/lens/QuoterV2.sol10.59 KB
- contracts/lens/TickLens.sol1.56 KB
- contracts/libraries/BytesLib.sol4.2 KB
- contracts/libraries/CallbackValidation.sol1.47 KB
- contracts/libraries/ChainId.sol370 B
- contracts/libraries/HexStrings.sol1.24 KB
- contracts/libraries/LiquidityAmounts.sol6.95 KB
- contracts/libraries/NFTSVG.sol19.74 KB
- contracts/libraries/OracleLibrary.sol9.52 KB
- contracts/libraries/Path.sol2.79 KB
- contracts/libraries/PoolAddress.sol1.82 KB
- contracts/libraries/PoolTicksCounter.sol4.24 KB
- contracts/libraries/PositionKey.sol386 B
- contracts/libraries/PositionValue.sol7.35 KB
- contracts/libraries/SqrtPriceMathPartial.sol2.88 KB
- contracts/libraries/TokenRatioSortOrder.sol353 B
- contracts/libraries/TransferHelper.sol2.54 KB
- contracts/test/Base64Test.sol456 B
- contracts/test/LiquidityAmountsTest.sol4.27 KB
- contracts/test/MockTimeSwapRouter.sol457 B
- contracts/test/NonfungiblePositionManagerPositionsGasTest.sol638 B
- contracts/test/OracleTest.sol2.23 KB
- contracts/test/PathTest.sol1 KB
- contracts/test/PeripheryImmutableStateTest.sol288 B
- contracts/test/PoolAddressTest.sol938 B
- contracts/test/PoolTicksCounterTest.sol548 B
- contracts/test/PositionValueTest.sol1.81 KB
- contracts/test/SelfPermitTest.sol195 B
- contracts/test/TestCallbackValidation.sol421 B
- contracts/test/TestERC20.sol311 B
- contracts/test/TestERC20Metadata.sol379 B
- contracts/test/TestERC20PermitAllowed.sol793 B
- contracts/test/TestMulticall.sol703 B
- contracts/test/TestPositionNFTOwner.sol792 B
- contracts/test/TestUniswapV3Callee.sol2.15 KB
- contracts/test/TickLensTest.sol532 B
- contracts/uniswap-v3-periphery/SwapRouter.sol9.16 KB
- contracts/uniswap-v3-periphery/base/BlockTimestamp.sol428 B
- contracts/uniswap-v3-periphery/base/Multicall.sol964 B
- contracts/uniswap-v3-periphery/base/PeripheryImmutableState.sol574 B
- contracts/uniswap-v3-periphery/base/PeripheryPayments.sol2.47 KB
- contracts/uniswap-v3-periphery/base/PeripheryPaymentsWithFee.sol2.01 KB
- contracts/uniswap-v3-periphery/base/PeripheryValidation.sol297 B
- contracts/uniswap-v3-periphery/base/SelfPermit.sol2.03 KB
- contracts/uniswap-v3-periphery/interfaces/IMulticall.sol705 B
- contracts/uniswap-v3-periphery/interfaces/IPeripheryImmutableState.sol419 B
- contracts/uniswap-v3-periphery/interfaces/IPeripheryPayments.sol1.42 KB
- contracts/uniswap-v3-periphery/interfaces/IPeripheryPaymentsWithFee.sol1.18 KB
- contracts/uniswap-v3-periphery/interfaces/ISelfPermit.sol3.88 KB
- contracts/uniswap-v3-periphery/interfaces/ISwapRouter.sol2.73 KB
- contracts/uniswap-v3-periphery/interfaces/external/IERC20PermitAllowed.sol1.2 KB
- contracts/uniswap-v3-periphery/interfaces/external/IWETH9.sol389 B
- contracts/uniswap-v3-periphery/libraries/BytesLib.sol4.2 KB
- contracts/uniswap-v3-periphery/libraries/CallbackValidation.sol1.48 KB
- contracts/uniswap-v3-periphery/libraries/Path.sol2.79 KB
- contracts/uniswap-v3-periphery/libraries/PoolAddress.sol1.82 KB
- contracts/uniswap-v3-periphery/libraries/TransferHelper.sol2.55 KB
- contracts/v3-periphery/SwapRouter.sol9.12 KB
- contracts/v3-periphery/base/BlockTimestamp.sol428 B
- contracts/v3-periphery/base/Multicall.sol964 B
- contracts/v3-periphery/base/PeripheryImmutableState.sol574 B
- contracts/v3-periphery/base/PeripheryPayments.sol2.46 KB
- contracts/v3-periphery/base/PeripheryPaymentsWithFee.sol1.98 KB
- contracts/v3-periphery/base/PeripheryValidation.sol297 B
- contracts/v3-periphery/base/SelfPermit.sol2 KB
- contracts/v3-periphery/contracts/SwapRouter.sol9.12 KB
- contracts/v3-periphery/contracts/base/BlockTimestamp.sol428 B
- contracts/v3-periphery/contracts/base/Multicall.sol966 B
- contracts/v3-periphery/contracts/base/PeripheryImmutableState.sol574 B
- contracts/v3-periphery/contracts/base/PeripheryPayments.sol2.46 KB
- contracts/v3-periphery/contracts/base/PeripheryPaymentsWithFee.sol1.98 KB
- contracts/v3-periphery/contracts/base/PeripheryValidation.sol297 B
- contracts/v3-periphery/contracts/base/SelfPermit.sol2 KB
- contracts/v3-periphery/contracts/interfaces/IMulticall.sol705 B
- contracts/v3-periphery/contracts/interfaces/IPeripheryImmutableState.sol419 B
- contracts/v3-periphery/contracts/interfaces/IPeripheryPayments.sol1.42 KB
- contracts/v3-periphery/contracts/interfaces/IPeripheryPaymentsWithFee.sol1.18 KB
- contracts/v3-periphery/contracts/interfaces/ISelfPermit.sol3.88 KB
- contracts/v3-periphery/contracts/interfaces/ISwapRouter.sol2.72 KB
- contracts/v3-periphery/contracts/interfaces/external/IERC20PermitAllowed.sol1.2 KB
- contracts/v3-periphery/contracts/interfaces/external/IWETH9.sol376 B
- contracts/v3-periphery/contracts/libraries/BytesLib.sol4.2 KB
- contracts/v3-periphery/contracts/libraries/CallbackValidation.sol1.47 KB
- contracts/v3-periphery/contracts/libraries/Path.sol2.79 KB
- contracts/v3-periphery/contracts/libraries/PoolAddress.sol1.82 KB
- contracts/v3-periphery/contracts/libraries/TransferHelper.sol2.54 KB
- contracts/v3-periphery/interfaces/IMulticall.sol705 B
- contracts/v3-periphery/interfaces/IPeripheryImmutableState.sol419 B
- contracts/v3-periphery/interfaces/IPeripheryPayments.sol1.42 KB
- contracts/v3-periphery/interfaces/IPeripheryPaymentsWithFee.sol1.18 KB
- contracts/v3-periphery/interfaces/ISelfPermit.sol3.88 KB
- contracts/v3-periphery/interfaces/ISwapRouter.sol2.72 KB
- contracts/v3-periphery/interfaces/external/IERC20PermitAllowed.sol1.2 KB
- contracts/v3-periphery/interfaces/external/IWETH9.sol376 B
- contracts/v3-periphery/libraries/BytesLib.sol4.2 KB
- contracts/v3-periphery/libraries/CallbackValidation.sol1.47 KB
- contracts/v3-periphery/libraries/Path.sol2.79 KB
- contracts/v3-periphery/libraries/PoolAddress.sol1.82 KB
- contracts/v3-periphery/libraries/TransferHelper.sol2.54 KB
- node_modules/@openzeppelin/contracts/drafts/IERC20Permit.sol2.12 KB
- node_modules/@openzeppelin/contracts/token/ERC20/IERC20.sol2.7 KB
- node_modules/@uniswap/v3-core/contracts/interfaces/IUniswapV3Pool.sol809 B
- node_modules/@uniswap/v3-core/contracts/interfaces/callback/IUniswapV3SwapCallback.sol1.29 KB
- node_modules/@uniswap/v3-core/contracts/interfaces/pool/IUniswapV3PoolActions.sol6.3 KB
- node_modules/@uniswap/v3-core/contracts/interfaces/pool/IUniswapV3PoolDerivedState.sol2.6 KB
- node_modules/@uniswap/v3-core/contracts/interfaces/pool/IUniswapV3PoolEvents.sol5.96 KB
- node_modules/@uniswap/v3-core/contracts/interfaces/pool/IUniswapV3PoolImmutables.sol1.81 KB
- node_modules/@uniswap/v3-core/contracts/interfaces/pool/IUniswapV3PoolOwnerActions.sol1.19 KB
- node_modules/@uniswap/v3-core/contracts/interfaces/pool/IUniswapV3PoolState.sol6.51 KB
- node_modules/@uniswap/v3-core/contracts/libraries/LowGasSafeMath.sol1.7 KB
- node_modules/@uniswap/v3-core/contracts/libraries/SafeCast.sol1.05 KB
- node_modules/@uniswap/v3-core/contracts/libraries/TickMath.sol8.65 KB
- npm/@openzeppelin/[email protected]/drafts/IERC20Permit.sol2.12 KB
- npm/@openzeppelin/[email protected]/token/ERC20/IERC20.sol2.7 KB
- project/contracts/v3-core/interfaces/IUniswapV3Pool.sol809 B
- project/contracts/v3-core/interfaces/callback/IUniswapV3SwapCallback.sol1.29 KB
- project/contracts/v3-core/interfaces/pool/IUniswapV3PoolActions.sol6.3 KB
- project/contracts/v3-core/interfaces/pool/IUniswapV3PoolDerivedState.sol2.6 KB
- project/contracts/v3-core/interfaces/pool/IUniswapV3PoolEvents.sol5.96 KB
- project/contracts/v3-core/interfaces/pool/IUniswapV3PoolImmutables.sol1.81 KB
- project/contracts/v3-core/interfaces/pool/IUniswapV3PoolOwnerActions.sol1.19 KB
- project/contracts/v3-core/interfaces/pool/IUniswapV3PoolState.sol6.51 KB
- project/contracts/v3-core/libraries/LowGasSafeMath.sol1.7 KB
- project/contracts/v3-core/libraries/SafeCast.sol1.05 KB
- project/contracts/v3-core/libraries/TickMath.sol8.65 KB
- project/contracts/v3-periphery/SwapRouter.sol9.12 KB
- project/contracts/v3-periphery/base/BlockTimestamp.sol428 B
- project/contracts/v3-periphery/base/Multicall.sol964 B
- project/contracts/v3-periphery/base/PeripheryImmutableState.sol574 B
- project/contracts/v3-periphery/base/PeripheryPayments.sol2.46 KB
- project/contracts/v3-periphery/base/PeripheryPaymentsWithFee.sol1.98 KB
- project/contracts/v3-periphery/base/PeripheryValidation.sol297 B
- project/contracts/v3-periphery/base/SelfPermit.sol2 KB
- project/contracts/v3-periphery/interfaces/IMulticall.sol705 B
- project/contracts/v3-periphery/interfaces/IPeripheryImmutableState.sol419 B
- project/contracts/v3-periphery/interfaces/IPeripheryPayments.sol1.42 KB
- project/contracts/v3-periphery/interfaces/IPeripheryPaymentsWithFee.sol1.18 KB
- project/contracts/v3-periphery/interfaces/ISelfPermit.sol3.88 KB
- project/contracts/v3-periphery/interfaces/ISwapRouter.sol2.72 KB
- project/contracts/v3-periphery/interfaces/external/IERC20PermitAllowed.sol1.2 KB
- project/contracts/v3-periphery/interfaces/external/IWETH9.sol376 B
- project/contracts/v3-periphery/libraries/BytesLib.sol4.2 KB
- project/contracts/v3-periphery/libraries/CallbackValidation.sol1.47 KB
- project/contracts/v3-periphery/libraries/Path.sol2.79 KB
- project/contracts/v3-periphery/libraries/PoolAddress.sol1.82 KB
- project/contracts/v3-periphery/libraries/TransferHelper.sol2.54 KB
contracts/SwapRouter.sol
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity =0.7.6;
pragma abicoder v2;
import '@uniswap/v3-core/contracts/libraries/SafeCast.sol';
import '@uniswap/v3-core/contracts/libraries/TickMath.sol';
import '@uniswap/v3-core/contracts/interfaces/IUniswapV3Pool.sol';
import './interfaces/ISwapRouter.sol';
import './base/PeripheryImmutableState.sol';
import './base/PeripheryValidation.sol';
import './base/PeripheryPaymentsWithFee.sol';
import './base/Multicall.sol';
import './base/SelfPermit.sol';
import './libraries/Path.sol';
import './libraries/PoolAddress.sol';
import './libraries/CallbackValidation.sol';
import './interfaces/external/IWETH9.sol';
/// @title Uniswap V3 Swap Router
/// @notice Router for stateless execution of swaps against Uniswap V3
contract SwapRouter is
ISwapRouter,
PeripheryImmutableState,
PeripheryValidation,
PeripheryPaymentsWithFee,
Multicall,
SelfPermit
{
using Path for bytes;
using SafeCast for uint256;
/// @dev Used as the placeholder value for amountInCached, because the computed amount in for an exact output swap
/// can never actually be this value
uint256 private constant DEFAULT_AMOUNT_IN_CACHED = type(uint256).max;
/// @dev Transient storage variable used for returning the computed amount in for an exact output swap.
uint256 private amountInCached = DEFAULT_AMOUNT_IN_CACHED;
constructor(address _factory, address _WETH9) PeripheryImmutableState(_factory, _WETH9) {}
/// @dev Returns the pool for the given token pair and fee. The pool contract may or may not exist.
function getPool(
address tokenA,
address tokenB,
uint24 fee
) private view returns (IUniswapV3Pool) {
return IUniswapV3Pool(PoolAddress.computeAddress(factory, PoolAddress.getPoolKey(tokenA, tokenB, fee)));
}
struct SwapCallbackData {
bytes path;
address payer;
}
/// @inheritdoc IUniswapV3SwapCallback
function uniswapV3SwapCallback(
int256 amount0Delta,
int256 amount1Delta,
bytes calldata _data
) external override {
require(amount0Delta > 0 || amount1Delta > 0); // swaps entirely within 0-liquidity regions are not supported
SwapCallbackData memory data = abi.decode(_data, (SwapCallbackData));
(address tokenIn, address tokenOut, uint24 fee) = data.path.decodeFirstPool();
CallbackValidation.verifyCallback(factory, tokenIn, tokenOut, fee);
(bool isExactInput, uint256 amountToPay) =
amount0Delta > 0
? (tokenIn < tokenOut, uint256(amount0Delta))
: (tokenOut < tokenIn, uint256(amount1Delta));
if (isExactInput) {
pay(tokenIn, data.payer, msg.sender, amountToPay);
} else {
// either initiate the next swap or pay
if (data.path.hasMultiplePools()) {
data.path = data.path.skipToken();
exactOutputInternal(amountToPay, msg.sender, 0, data);
} else {
amountInCached = amountToPay;
tokenIn = tokenOut; // swap in/out because exact output swaps are reversed
pay(tokenIn, data.payer, msg.sender, amountToPay);
}
}
}
/// @dev Performs a single exact input swap
function exactInputInternal(
uint256 amountIn,
address recipient,
uint160 sqrtPriceLimitX96,
SwapCallbackData memory data
) private returns (uint256 amountOut) {
// allow swapping to the router address with address 0
if (recipient == address(0)) recipient = address(this);
(address tokenIn, address tokenOut, uint24 fee) = data.path.decodeFirstPool();
bool zeroForOne = tokenIn < tokenOut;
(int256 amount0, int256 amount1) =
getPool(tokenIn, tokenOut, fee).swap(
recipient,
zeroForOne,
amountIn.toInt256(),
sqrtPriceLimitX96 == 0
? (zeroForOne ? TickMath.MIN_SQRT_RATIO + 1 : TickMath.MAX_SQRT_RATIO - 1)
: sqrtPriceLimitX96,
abi.encode(data)
);
return uint256(-(zeroForOne ? amount1 : amount0));
}
/// @inheritdoc ISwapRouter
function exactInputSingle(ExactInputSingleParams calldata params)
external
payable
override
checkDeadline(params.deadline)
returns (uint256 amountOut)
{
amountOut = exactInputInternal(
params.amountIn,
params.recipient,
params.sqrtPriceLimitX96,
SwapCallbackData({path: abi.encodePacked(params.tokenIn, params.fee, params.tokenOut), payer: msg.sender})
);
require(amountOut >= params.amountOutMinimum, 'Too little received');
}
/// @inheritdoc ISwapRouter
function exactInput(ExactInputParams memory params)
external
payable
override
checkDeadline(params.deadline)
returns (uint256 amountOut)
{
address payer = msg.sender; // msg.sender pays for the first hop
while (true) {
bool hasMultiplePools = params.path.hasMultiplePools();
// the outputs of prior swaps become the inputs to subsequent ones
params.amountIn = exactInputInternal(
params.amountIn,
hasMultiplePools ? address(this) : params.recipient, // for intermediate swaps, this contract custodies
0,
SwapCallbackData({
path: params.path.getFirstPool(), // only the first pool in the path is necessary
payer: payer
})
);
// decide whether to continue or terminate
if (hasMultiplePools) {
payer = address(this); // at this point, the caller has paid
params.path = params.path.skipToken();
} else {
amountOut = params.amountIn;
break;
}
}
require(amountOut >= params.amountOutMinimum, 'Too little received');
}
/// @dev Performs a single exact output swap
function exactOutputInternal(
uint256 amountOut,
address recipient,
uint160 sqrtPriceLimitX96,
SwapCallbackData memory data
) private returns (uint256 amountIn) {
// allow swapping to the router address with address 0
if (recipient == address(0)) recipient = address(this);
(address tokenOut, address tokenIn, uint24 fee) = data.path.decodeFirstPool();
bool zeroForOne = tokenIn < tokenOut;
(int256 amount0Delta, int256 amount1Delta) =
getPool(tokenIn, tokenOut, fee).swap(
recipient,
zeroForOne,
-amountOut.toInt256(),
sqrtPriceLimitX96 == 0
? (zeroForOne ? TickMath.MIN_SQRT_RATIO + 1 : TickMath.MAX_SQRT_RATIO - 1)
: sqrtPriceLimitX96,
abi.encode(data)
);
uint256 amountOutReceived;
(amountIn, amountOutReceived) = zeroForOne
? (uint256(amount0Delta), uint256(-amount1Delta))
: (uint256(amount1Delta), uint256(-amount0Delta));
// it's technically possible to not receive the full output amount,
// so if no price limit has been specified, require this possibility away
if (sqrtPriceLimitX96 == 0) require(amountOutReceived == amountOut);
}
/// @inheritdoc ISwapRouter
function exactOutputSingle(ExactOutputSingleParams calldata params)
external
payable
override
checkDeadline(params.deadline)
returns (uint256 amountIn)
{
// avoid an SLOAD by using the swap return data
amountIn = exactOutputInternal(
params.amountOut,
params.recipient,
params.sqrtPriceLimitX96,
SwapCallbackData({path: abi.encodePacked(params.tokenOut, params.fee, params.tokenIn), payer: msg.sender})
);
require(amountIn <= params.amountInMaximum, 'Too much requested');
// has to be reset even though we don't use it in the single hop case
amountInCached = DEFAULT_AMOUNT_IN_CACHED;
}
/// @inheritdoc ISwapRouter
function exactOutput(ExactOutputParams calldata params)
external
payable
override
checkDeadline(params.deadline)
returns (uint256 amountIn)
{
// it's okay that the payer is fixed to msg.sender here, as they're only paying for the "final" exact output
// swap, which happens first, and subsequent swaps are paid for within nested callback frames
exactOutputInternal(
params.amountOut,
params.recipient,
0,
SwapCallbackData({path: params.path, payer: msg.sender})
);
amountIn = amountInCached;
require(amountIn <= params.amountInMaximum, 'Too much requested');
amountInCached = DEFAULT_AMOUNT_IN_CACHED;
}
}
Compiler settingsShow
{
"metadata": {
"bytecodeHash": "none",
"useLiteralContent": true
},
"libraries": {},
"optimizer": {
"runs": 1000000,
"enabled": true
},
"evmVersion": "istanbul",
"remappings": []
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.