Skip to content
Verified contractExactMIT

WenLongReturnRouter

Address0x278da57e129805d6570edb7a5e2e22aaeee45c2a
Balance

0 ETH

 

Transactions

3

 

Compiler

solc 0.8.28

 

Same code

1 more contract

 

Details

Compiler
solc 0.8.28+commit.7893614a
Language
solidity
Optimization
Yes, 10,000 runs
EVM version
cancun
Via IR
yes
License
MIT
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,419,943
Creation transaction
0xdfc6e128c9…07af8ea0ec

src/WenLongReturnRouter.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

/// Circle FiatToken v2.2 (USDC on Ethereum): only what the router calls.
interface IFiatTokenV2 {
    function receiveWithAuthorization(
        address from,
        address to,
        uint256 value,
        uint256 validAfter,
        uint256 validBefore,
        bytes32 nonce,
        uint8 v,
        bytes32 r,
        bytes32 s
    ) external;
    function transfer(address to, uint256 value) external returns (bool);
    function approve(address spender, uint256 value) external returns (bool);
    function balanceOf(address account) external view returns (uint256);
    function allowance(address owner, address spender) external view returns (uint256);
}

/// Circle CCTP v2 TokenMessengerV2 (circlefin/evm-cctp-contracts 7d703109).
interface ITokenMessengerV2 {
    function depositForBurnWithHook(
        uint256 amount,
        uint32 destinationDomain,
        bytes32 mintRecipient,
        address burnToken,
        bytes32 destinationCaller,
        uint256 maxFee,
        uint32 minFinalityThreshold,
        bytes calldata hookData
    ) external;
}

/// @title WenLongReturnRouter
/// @notice Returns an owner's USDC from Ethereum to the same owner's HyperCore account in one off-chain signature.
/// The owner signs one EIP-3009 ReceiveWithAuthorization (USDC's own EIP-712 domain) naming this router as the payee.
/// Its nonce commits to every choice the relayer could otherwise make (WenLong fee, Circle fee cap, salt), and this
/// contract's address commits to everything else (USDC, messenger, route, fee recipient, caps). Anyone may submit;
/// the outcome is fixed: `fee` to FEE_RECIPIENT, `value - fee` burned by Circle CCTP v2 and forwarded by Circle's
/// Forwarding Service to HyperCore for `from`. Mirrors apps/api/src/derive/v3/cctp.ts (deriveV3CctpForwardRoute,
/// deriveV3HyperCoreHook, DERIVE_V3_FORWARD_FINALITY).
/// Only key holders (EOAs, incl. EIP-7702-delegated ones) can be `from`: HyperCore needs a key to use the credit.
/// No owner, no admin, no upgrade, no selfdestruct, no arbitrary calls, no storage; holds nothing between calls.
contract WenLongReturnRouter {
    /// Domain tag of the authorization nonce.
    bytes32 public constant NONCE_TAG = keccak256("WenLongReturnRouter.returnToHyperCore.v1");
    /// Circle fast transfer (cctp.ts DERIVE_V3_FORWARD_FINALITY); underpaid, Circle runs it as standard.
    uint32 public constant MIN_FINALITY_THRESHOLD = 1000;
    /// HyperCore destination dex in the forwarding hook: 0 = perps (cctp.ts DERIVE_V3_HYPERCORE.perpsDex).
    uint32 public constant HYPERCORE_DEX = 0;

    IFiatTokenV2 public immutable USDC;
    ITokenMessengerV2 public immutable TOKEN_MESSENGER;
    /// CCTP domain of HyperEVM (19).
    uint32 public immutable DESTINATION_DOMAIN;
    /// Circle's CctpForwarder on HyperEVM: mintRecipient and destinationCaller of the burn.
    address public immutable FORWARDER;
    address public immutable FEE_RECIPIENT;
    /// Upper bound of WenLong's network fee per return (USDC units).
    uint256 public immutable MAX_FEE;
    /// Upper bound of Circle's fee per return (USDC units).
    uint256 public immutable MAX_CCTP_FEE;

    // keccak256("WenLongReturnRouter.lock") — transient reentrancy lock (EIP-1153).
    bytes32 private constant LOCK_SLOT = 0xb84787be491125de7432f2fdd6c2d90d27e29398e4b81200ec07e6933748ed5a;

    event ReturnedToHyperCore(
        address indexed from, bytes32 indexed nonce, uint256 value, uint256 fee, uint256 burned, uint256 cctpMaxFee
    );

    error ZeroAddress();
    error BadFee();
    error BadAmount();
    error Reentrant();
    error TokenCallFailed();
    error BalanceMismatch();
    error AllowanceLeft();
    error NotEoa();

    constructor(
        address usdc,
        address tokenMessenger,
        uint32 destinationDomain,
        address forwarder,
        address feeRecipient,
        uint256 maxFee,
        uint256 maxCctpFee
    ) {
        if (usdc == address(0) || tokenMessenger == address(0) || forwarder == address(0) || feeRecipient == address(0)) {
            revert ZeroAddress();
        }
        USDC = IFiatTokenV2(usdc);
        TOKEN_MESSENGER = ITokenMessengerV2(tokenMessenger);
        DESTINATION_DOMAIN = destinationDomain;
        FORWARDER = forwarder;
        FEE_RECIPIENT = feeRecipient;
        MAX_FEE = maxFee;
        MAX_CCTP_FEE = maxCctpFee;
    }

    /// @notice The EIP-3009 nonce the owner must sign for this exact return.
    function authorizationNonce(
        address from,
        uint256 value,
        uint256 validAfter,
        uint256 validBefore,
        uint256 fee,
        uint256 cctpMaxFee,
        bytes32 salt
    ) public view returns (bytes32) {
        return keccak256(
            abi.encode(
                NONCE_TAG, block.chainid, address(this), from, value, validAfter, validBefore, fee, cctpMaxFee, salt
            )
        );
    }

    /// @notice The 56-byte Circle forwarding hook crediting `recipient` on HyperCore (cctp.ts deriveV3HyperCoreHook).
    function hyperCoreHook(address recipient) public pure returns (bytes memory) {
        // "cctp-forward" in 24 bytes, version 0, payload length 24, recipient, destination dex.
        return abi.encodePacked(bytes24("cctp-forward"), uint32(0), uint32(24), recipient, HYPERCORE_DEX);
    }

    /// @notice Pull `value` USDC from `from` under its signed authorization, pay `fee` to FEE_RECIPIENT and burn the
    /// rest to `from` on HyperCore. Reverts on any mismatch; the authorization is consumed only if everything succeeds.
    function returnToHyperCore(
        address from,
        uint256 value,
        uint256 validAfter,
        uint256 validBefore,
        uint256 fee,
        uint256 cctpMaxFee,
        bytes32 salt,
        uint8 v,
        bytes32 r,
        bytes32 s
    ) external {
        _lock();
        if (from == address(0)) revert ZeroAddress();
        _requireKeyHolder(from);
        if (fee > MAX_FEE || fee >= value) revert BadFee();
        uint256 burned = value - fee;
        if (cctpMaxFee > MAX_CCTP_FEE || cctpMaxFee >= burned) revert BadAmount();

        bytes32 nonce = authorizationNonce(from, value, validAfter, validBefore, fee, cctpMaxFee, salt);
        IFiatTokenV2 usdc = USDC;
        uint256 before = usdc.balanceOf(address(this));
        // EIP-3009: USDC requires msg.sender == to, so only this router can redeem an authorization naming it.
        usdc.receiveWithAuthorization(from, address(this), value, validAfter, validBefore, nonce, v, r, s);
        if (usdc.balanceOf(address(this)) != before + value) revert BalanceMismatch();

        if (fee != 0 && !usdc.transfer(FEE_RECIPIENT, fee)) revert TokenCallFailed();
        if (!usdc.approve(address(TOKEN_MESSENGER), burned)) revert TokenCallFailed();
        bytes32 forwarder = bytes32(uint256(uint160(FORWARDER)));
        TOKEN_MESSENGER.depositForBurnWithHook(
            burned,
            DESTINATION_DOMAIN,
            forwarder,
            address(usdc),
            forwarder,
            cctpMaxFee,
            MIN_FINALITY_THRESHOLD,
            hyperCoreHook(from)
        );
        if (usdc.balanceOf(address(this)) != before) revert BalanceMismatch();
        if (usdc.allowance(address(this), address(TOKEN_MESSENGER)) != 0) revert AllowanceLeft();

        emit ReturnedToHyperCore(from, nonce, value, fee, burned, cctpMaxFee);
        _unlock();
    }

    /// HyperCore credits `from`, and only a private key can act for an account there. USDC v2.2 also accepts ERC-1271
    /// contract signatures, so a smart-contract wallet could authorize a return whose credit nobody can then use.
    /// Accept a plain EOA (no code) or an EIP-7702-delegated EOA (exactly the 23-byte 0xef0100 designator); refuse
    /// every other contract.
    function _requireKeyHolder(address from) private view {
        uint256 size;
        uint256 prefix = 0;
        assembly ("memory-safe") {
            size := extcodesize(from)
            if eq(size, 23) {
                extcodecopy(from, 0, 0, 3)
                prefix := shr(232, mload(0))
            }
        }
        if (size != 0 && !(size == 23 && prefix == 0xef0100)) revert NotEoa();
    }

    function _lock() private {
        bytes32 slot = LOCK_SLOT;
        uint256 locked;
        assembly {
            locked := tload(slot)
        }
        if (locked != 0) revert Reentrant();
        assembly {
            tstore(slot, 1)
        }
    }

    function _unlock() private {
        bytes32 slot = LOCK_SLOT;
        assembly {
            tstore(slot, 0)
        }
    }
}
Compiler settingsShow
{
  "viaIR": true,
  "metadata": {
    "appendCBOR": true,
    "bytecodeHash": "ipfs",
    "useLiteralContent": false
  },
  "libraries": {},
  "optimizer": {
    "runs": 10000,
    "enabled": true
  },
  "evmVersion": "cancun"
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.