Skip to content
Verified contractExactUNLICENSED

RathCctpRouter

Address0x9cb8f4bd297bbd307e783da757bf048d4e3e41cf
Balance

0 ETH

 

Transactions

427

 

Compiler

solc 0.8.23

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.23+commit.f704f362
Language
solidity
Optimization
No
EVM version
shanghai
Via IR
yes
License
UNLICENSED
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
22,523,645
Creation transaction
0x9853b4b34d…588f5eedc1

src/RathCctpRouter.sol

/*
 ░█▀▄░█▀█░▀█▀░█░█░░░█▀▀░▀█▀░█▀█░█▀█░█▀█░█▀▀░█▀▀
 ░█▀▄░█▀█░░█░░█▀█░░░█▀▀░░█░░█░█░█▀█░█░█░█░░░█▀▀
 ░▀░▀░▀░▀░░▀░░▀░▀░░░▀░░░▀▀▀░▀░▀░▀░▀░▀░▀░▀▀▀░▀▀▀
*/

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

import "@openzeppelin/contracts/access/Ownable.sol";

/**
 * @notice Interface for receiving messages via CCTP.
 */
interface IReceiver {
    function receiveMessage(bytes calldata message, bytes calldata signature)
    external
    returns (bool success);
}

/**
 * @notice Interface for initiating deposit and burn via CCTP
 */
interface ITokenMessenger {
    function depositForBurnWithHook(
        uint256 amount,
        uint32 destinationDomain,
        bytes32 mintRecipient,
        address burnToken,
        bytes32 destinationCaller,
        uint256 maxFee,
        uint32 minFinalityThreshold,
        bytes calldata hookData
    ) external;
}

/**
 * @notice Interface for ERC20 token with permit functionality.
 */
interface IERC20Permit {
    function permit(
        address owner,
        address spender,
        uint256 value,
        uint256 deadline,
        uint8 v,
        bytes32 r,
        bytes32 s
    ) external;

    function transfer(address to, uint256 amount) external returns (bool);

    function transferFrom(address from, address to, uint256 amount) external returns (bool);

    function balanceOf(address account) external view returns (uint256);

    function approve(address spender, uint256 amount) external returns (bool);
}

/**
 * @title RathCctpReceiver
 * @dev This contract handles incoming messages and token transfers using the CCTP V2 protocol.
 * It forwards the message to CCTP message transmitter, deducts fees, and sends the remaining amount to the receiver.
 */
contract RathCctpRouter is Ownable {

    /**
     * @dev Constants defining byte positions for extracting relevant data from the message.
     */
    uint16 private constant MESSAGE_BODY_INDEX = 148;
    uint16 private constant AMOUNT_INDEX = MESSAGE_BODY_INDEX + 68;
    uint16 private constant CCTP_FEE_INDEX = MESSAGE_BODY_INDEX + 164;
    uint16 private constant HOOK_DATA_INDEX = MESSAGE_BODY_INDEX + 228;
    uint16 private constant RATH_FEE_INDEX = HOOK_DATA_INDEX + 20;

    /**
     * @dev Address of the message transmitter that receives CCTP messages.
     */
    address public immutable messageTransmitter;

    /**
     * @dev Address of the token messenger that initiates deposit and burn via CCTP.
     */
    address public immutable tokenMessenger;

    /**
     * @dev Address of the token that will be minted upon message processing.
     */
    address public immutable tokenAddress;

    /**
     * @dev Sets the message transmitter and token address upon deployment.
     * @param _messageTransmitter Address of the CCTP message transmitter.
     * @param _tokenMessenger Address of the CCTP token messenger.
     * @param _tokenAddress Address of the ERC20 token used for transfers.
     * @param _owner Address of the contract owner.
     */
    constructor(address _messageTransmitter, address _tokenMessenger, address _tokenAddress, address _owner) Ownable(_owner) {
        messageTransmitter = _messageTransmitter;
        tokenMessenger = _tokenMessenger;
        tokenAddress = _tokenAddress;
    }

    /**
     * @notice Deposits and burns tokens from sender to be minted on destination domain.
     * Uses permit to receive tokens from sender.
     * @param amount amount of tokens to burn (must be non-zero)
     * @param destinationDomain destination domain
     * @param mintRecipient address of mint recipient on destination domain
     * @param destinationCaller caller on the destination domain, as bytes32
     * @param maxFee maximum fee to pay on destination chain
     * @param hookData optional hook data for interpretation on destination chain
     * @param deadline Expiry timestamp for the permit signature
     * @param v ECDSA signature parameter v
     * @param r ECDSA signature parameter r
     * @param s ECDSA signature parameter s
     */
    function depositForBurnWithPermit(
        uint256 amount,
        uint32 destinationDomain,
        bytes32 mintRecipient,
        bytes32 destinationCaller,
        uint256 maxFee,
        uint32 minFinalityThreshold,
        bytes calldata hookData,
        uint256 deadline,
        uint8 v,
        bytes32 r,
        bytes32 s
    ) external {
        try IERC20Permit(tokenAddress).permit(msg.sender, address(this), amount, deadline, v, r, s) {
        } catch {
        }
        IERC20Permit(tokenAddress).transferFrom(msg.sender, address(this), amount);
        IERC20Permit(tokenAddress).approve(tokenMessenger, amount);
        ITokenMessenger(tokenMessenger).depositForBurnWithHook(
            amount,
            destinationDomain,
            mintRecipient,
            tokenAddress,
            destinationCaller,
            maxFee,
            minFinalityThreshold,
            hookData
        );
    }

    /**
     * @dev Processes an incoming message, extracts transfer details, deducts fees, and transfers the remaining tokens to the receiver
     * @param message The CCTP message payload containing transfer details
     * @param signature Signature verifying the authenticity of the message
     */
    function receiveMessage(bytes calldata message, bytes calldata signature) external {
        // Forward the message to CCTP message transmitter and receive minted token
        IReceiver(messageTransmitter).receiveMessage(message, signature);

        // Extract relevant data from the message
        uint256 amount = uint256(bytes32(message[AMOUNT_INDEX : AMOUNT_INDEX + 32]));
        uint256 cctpFee = uint256(bytes32(message[CCTP_FEE_INDEX : CCTP_FEE_INDEX + 32]));
        address receiver = address(bytes20(message[HOOK_DATA_INDEX : HOOK_DATA_INDEX + 20]));
        uint64 rathFee = uint64(bytes8(message[RATH_FEE_INDEX : RATH_FEE_INDEX + 8]));

        // Calculate the final amount to send after deducting fees
        uint256 amountToSend = amount - cctpFee - rathFee;

        // Transfer the remaining amount to the receiver
        IERC20Permit(tokenAddress).transfer(receiver, amountToSend);
    }

    /**
     * @dev Allows the contract owner to withdraw stuck tokens or Ether
     * @param token Address of the ERC20 token to rescue (use address(0) for Ether)
     */
    function rescue(address token) public onlyOwner {
        if (token == address(0)) {
            // Withdraw Ether balance to the contract owner
            payable(owner()).transfer(address(this).balance);
        } else {
            // Withdraw ERC20 token balance to the contract owner
            IERC20Permit(token).transfer(owner(), IERC20Permit(token).balanceOf(address(this)));
        }
    }
}

lib/openzeppelin-contracts/contracts/access/Ownable.sol

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (access/Ownable.sol)

pragma solidity ^0.8.20;

import {Context} from "../utils/Context.sol";

/**
 * @dev Contract module which provides a basic access control mechanism, where
 * there is an account (an owner) that can be granted exclusive access to
 * specific functions.
 *
 * The initial owner is set to the address provided by the deployer. This can
 * later be changed with {transferOwnership}.
 *
 * This module is used through inheritance. It will make available the modifier
 * `onlyOwner`, which can be applied to your functions to restrict their use to
 * the owner.
 */
abstract contract Ownable is Context {
    address private _owner;

    /**
     * @dev The caller account is not authorized to perform an operation.
     */
    error OwnableUnauthorizedAccount(address account);

    /**
     * @dev The owner is not a valid owner account. (eg. `address(0)`)
     */
    error OwnableInvalidOwner(address owner);

    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

    /**
     * @dev Initializes the contract setting the address provided by the deployer as the initial owner.
     */
    constructor(address initialOwner) {
        if (initialOwner == address(0)) {
            revert OwnableInvalidOwner(address(0));
        }
        _transferOwnership(initialOwner);
    }

    /**
     * @dev Throws if called by any account other than the owner.
     */
    modifier onlyOwner() {
        _checkOwner();
        _;
    }

    /**
     * @dev Returns the address of the current owner.
     */
    function owner() public view virtual returns (address) {
        return _owner;
    }

    /**
     * @dev Throws if the sender is not the owner.
     */
    function _checkOwner() internal view virtual {
        if (owner() != _msgSender()) {
            revert OwnableUnauthorizedAccount(_msgSender());
        }
    }

    /**
     * @dev Leaves the contract without owner. It will not be possible to call
     * `onlyOwner` functions. Can only be called by the current owner.
     *
     * NOTE: Renouncing ownership will leave the contract without an owner,
     * thereby disabling any functionality that is only available to the owner.
     */
    function renounceOwnership() public virtual onlyOwner {
        _transferOwnership(address(0));
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Can only be called by the current owner.
     */
    function transferOwnership(address newOwner) public virtual onlyOwner {
        if (newOwner == address(0)) {
            revert OwnableInvalidOwner(address(0));
        }
        _transferOwnership(newOwner);
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Internal function without access restriction.
     */
    function _transferOwnership(address newOwner) internal virtual {
        address oldOwner = _owner;
        _owner = newOwner;
        emit OwnershipTransferred(oldOwner, newOwner);
    }
}

lib/openzeppelin-contracts/contracts/utils/Context.sol

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.1) (utils/Context.sol)

pragma solidity ^0.8.20;

/**
 * @dev Provides information about the current execution context, including the
 * sender of the transaction and its data. While these are generally available
 * via msg.sender and msg.data, they should not be accessed in such a direct
 * manner, since when dealing with meta-transactions the account sending and
 * paying for execution may not be the actual sender (as far as an application
 * is concerned).
 *
 * This contract is only required for intermediate, library-like contracts.
 */
abstract contract Context {
    function _msgSender() internal view virtual returns (address) {
        return msg.sender;
    }

    function _msgData() internal view virtual returns (bytes calldata) {
        return msg.data;
    }

    function _contextSuffixLength() internal view virtual returns (uint256) {
        return 0;
    }
}
Compiler settingsShow
{
  "viaIR": true,
  "metadata": {
    "appendCBOR": true,
    "bytecodeHash": "ipfs",
    "useLiteralContent": false
  },
  "libraries": {},
  "optimizer": {
    "runs": 200,
    "enabled": false
  },
  "evmVersion": "shanghai",
  "remappings": [
    "@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/",
    "erc4626-tests/=lib/openzeppelin-contracts/lib/erc4626-tests/",
    "forge-std/=lib/forge-std/src/",
    "halmos-cheatcodes/=lib/openzeppelin-contracts/lib/halmos-cheatcodes/src/",
    "openzeppelin-contracts/=lib/openzeppelin-contracts/"
  ]
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.