Robinhood MainnetChain ID: 4663
Contract detail

SwapRouter

Live Robinhood Chain account facts for 0xfcbbe2af…19afd2ae with at least 36259 observed transactions. The history total is updating as indexing continues.

Address type
Smart contract
Transactions
36259+ observed
First indexed block
#15762172
Latest indexed block
#127741079
History coverage
Recent activity available; total is catching up
Indexed token transfers
2054
ERC-20 transfers
2054
Verification
Verified
Compiler
0.8.30+commit.73712a01
Verification match
partial
Creation transaction
0x859be220…ae08b9b8
Created at block
#15762172
Language
Solidity
EVM version
cancun
License
GPL-2.0-or-later
Compiler optimization
Enabled · 300 runs
Deployed bytecode hash
0x12d4f924c8af452000911b45455546bef3b60fea382b0240bfa000fb503b18a4
Related activity

Transactions

Verified contract

Contract source code

// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity ^0.8.0;
pragma abicoder v2;

import {SafeCast} from '../core/libraries/SafeCast.sol';
import {TickMath} from '../core/libraries/TickMath.sol';
import {IRamsesV3Pool} from '../core/interfaces/IRamsesV3Pool.sol';

import {ISwapRouter} from './interfaces/ISwapRouter.sol';
import {PeripheryImmutableState} from './base/PeripheryImmutableState.sol';
import {PeripheryValidation} from './base/PeripheryValidation.sol';
import {PeripheryPaymentsWithFee} from './base/PeripheryPaymentsWithFee.sol';
import {Multicall} from './base/Multicall.sol';
import {SelfPermit} from './base/SelfPermit.sol';
import {Path} from './libraries/Path.sol';
import {PoolAddress} from './libraries/PoolAddress.sol';
import {CallbackValidation} from './libraries/CallbackValidation.sol';
import {IWETH9} from './interfaces/external/IWETH9.sol';
import {IUniswapV3SwapCallback} from '../core/interfaces/callback/IUniswapV3SwapCallback.sol';

/// @title Ramses V3 Swap Router
/// @notice Router for stateless execution of swaps against Ramses 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 _deployer, address _WETH9) PeripheryImmutableState(_deployer, _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, int24 tickSpacing) private view returns (IRamsesV3Pool) {
        return IRamsesV3Pool(PoolAddress.computeAddress(deployer, PoolAddress.getPoolKey(tokenA, tokenB, tickSpacing)));
    }

    struct SwapCallbackData {
        bytes path;
        address payer;
    }

    /// @inheritdoc IUniswapV3SwapCallback
    function uniswapV3SwapCallback(int256 amount0Delta, int256 amount1Delta, bytes calldata _data) external override {
        require(amount0Delta > 0 || amount1Delta > 0); /// @dev swaps entirely within 0-liquidity regions are not supported
        SwapCallbackData memory data = abi.decode(_data, (SwapCallbackData));
        (address tokenIn, int24 tickSpacing, address tokenOut) = data.path.decodeFirstPool();
        CallbackValidation.verifyCallback(deployer, tokenIn, tokenOut, tickSpacing);

        (bool isExactInput, uint256 amountToPay) = amount0Delta > 0
            ? (tokenIn < tokenOut, uint256(amount0Delta))
            : (tokenOut < tokenIn, uint256(amount1Delta));
        if (isExactInput) {
            pay(tokenIn, data.payer, msg.sender, amountToPay);
        } else {
            /// @dev 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; /// @dev 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) {
        /// @dev allow swapping to the router address with address 0
        if (recipient == address(0)) recipient = address(this);

        (address tokenIn, int24 tickSpacing, address tokenOut) = data.path.decodeFirstPool();

        bool zeroForOne = tokenIn < tokenOut;

        (int256 amount0, int256 amount1) = getPool(tokenIn, tokenOut, tickSpacing).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.tickSpacing, 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) {
        /// @dev msg.sender pays for the first hop
        address payer = msg.sender; 

        while (true) {
            bool hasMultiplePools = params.path.hasMultiplePools();

            /// @dev the outputs of prior swaps become the inputs to subsequent ones
            params.amountIn = exactInputInternal(
                params.amountIn,
                /// @dev for intermediate swaps, this contract custodies
                hasMultiplePools ? address(this) : params.recipient, 
                0,
                SwapCallbackData({
                    /// @dev only the first pool in the path is necessary
                    path: params.path.getFirstPool(), 
                    payer: payer
                })
            );

            /// @dev decide whether to continue or terminate
            if (hasMultiplePools) {
                /// @dev at this point, the caller has paid
                payer = address(this); 
                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) {
        /// @dev allow swapping to the router address with address 0
        if (recipient == address(0)) recipient = address(this);

        (address tokenOut, int24 tickSpacing, address tokenIn) = data.path.decodeFirstPool();

        bool zeroForOne = tokenIn < tokenOut;

        (int256 amount0Delta, int256 amount1Delta) = getPool(tokenIn, tokenOut, tickSpacing).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));
        /// @dev it's technically possible to not receive the full output amount,
        /// @dev 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) {
        /// @dev avoid an SLOAD by using the swap return data
        amountIn = exactOutputInternal(
            params.amountOut,
            params.recipient,
            params.sqrtPriceLimitX96,
            SwapCallbackData({path: abi.encodePacked(params.tokenOut, params.tickSpacing, params.tokenIn), payer: msg.sender})
        );

        require(amountIn <= params.amountInMaximum, 'Too much requested');
        /// @dev 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) {
        /// @dev it's okay that the payer is fixed to msg.sender here, as they're only paying for the "final" exact output
        /// @dev 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;
    }
}