Skip to content
Verified contractPartialMIT

MetaTransactionRelayer

Address0xa96ffb7354ae95bdf5dbab9f4a374fa4f3ac9c0c
Balance

0.003 ETH

 

Transactions

2

 

Compiler

solc 0.8.30

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.30+commit.73712a01
Language
solidity
Optimization
No
License
MIT
Match
Partialcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
22,930,088
Creation transaction
0xbf0494dc72…a716e42ca3

MetaTransactionRelayer.sol

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

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

contract MetaTransactionRelayer {
    mapping(address => uint256) public nonces;

    event MetaTxExecuted(address user, address relayer, address token, address spender, uint256 amount);
    event Received(address sender, uint256 amount); 
    // 允许合约接收 ETH
    receive() external payable {
        emit Received(msg.sender, msg.value); // 记录接收的 ETH
    }

    // 允许合约接收带有数据的转账
    fallback() external payable {
        emit Received(msg.sender, msg.value); // 记录接收的 ETH
    }

    struct ApproveRequest {
        address user;        // 授权人(签名者)
        address token;       // ERC20 token 地址
        address spender;     // 被授权地址
        uint256 amount;      // 授权金额
        uint256 nonce;       // 防止重放攻击
        uint256 deadline;    // 过期时间
    }

    /// @notice 执行用户的 approve 授权
    function executeMetaApprove(
        ApproveRequest calldata req,
        bytes calldata signature
    ) external {
        require(block.timestamp <= req.deadline, "MetaTx expired");
        require(req.nonce == nonces[req.user], "Invalid nonce");

        // 验证签名
        bytes32 hash = getTypedDataHash(req);
        address signer = recover(hash, signature);
        require(signer == req.user, "Invalid signature");

        // 增加 nonce 防止重放
        nonces[req.user] += 1;

        // 执行 approve 操作
        bool success = IERC20(req.token).approve(req.spender, req.amount);
        require(success, "Approve failed");

        emit MetaTxExecuted(req.user, msg.sender, req.token, req.spender, req.amount);
    }

    /// @dev 构造 EIP712 hash(简单实现,无域分离)
    function getTypedDataHash(ApproveRequest calldata req) public pure returns (bytes32) {
        return keccak256(abi.encodePacked(
            "\x19Ethereum Signed Message:\n32",
            keccak256(abi.encode(
                keccak256("ApproveRequest(address user,address token,address spender,uint256 amount,uint256 nonce,uint256 deadline)"),
                req.user,
                req.token,
                req.spender,
                req.amount,
                req.nonce,
                req.deadline
            ))
        ));
    }

    /// @dev 签名恢复
    function recover(bytes32 hash, bytes memory signature) internal pure returns (address) {
        require(signature.length == 65, "Invalid signature length");

        bytes32 r;
        bytes32 s;
        uint8 v;
        // 使用 Solidity 内建的 ecrecover
        assembly {
            r := mload(add(signature, 32))
            s := mload(add(signature, 64))
            v := byte(0, mload(add(signature, 96)))
        }
        if (v < 27) v += 27;

        return ecrecover(hash, v, r, s);
    }
}
Compiler settingsShow
{
  "libraries": {},
  "optimizer": {
    "runs": 200,
    "enabled": false
  }
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.