ERC20TimelockEitherBeneficiary

Description:

Smart contract deployed on Ethereum with Factory features.

Blockchain: Ethereum

Source Code: View Code On The Blockchain

Solidity Source Code:

{{
  "language": "Solidity",
  "sources": {
    "ERC20TimelockEitherBeneficiary.sol": {
      "content": "// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

/// @title ERC20TimelockEitherBeneficiary
/// @notice 三年时间锁,可多次存入;到期后由任一受益人独自提取全部余额到本人账户。
/// @dev 适用于任意 ERC20(例如 XAUT)。受益人地址在部署时指定且不可更改。
interface IERC20 {
    function balanceOf(address) external view returns (uint256);
    function transfer(address to, uint256 amount) external returns (bool);
    function transferFrom(address from, address to, uint256 amount) external returns (bool);
}

library SafeERC20 {
    function _call(address token, bytes memory data) private returns (bool) {
        (bool ok, bytes memory ret) = token.call(data);
        return ok && (ret.length == 0 || abi.decode(ret, (bool)));
    }
    function safeTransfer(IERC20 token, address to, uint256 value) internal {
        require(_call(address(token), abi.encodeWithSelector(token.transfer.selector, to, value)), "transfer failed");
    }
    function safeTransferFrom(IERC20 token, address from, address to, uint256 value) internal {
        require(_call(address(token), abi.encodeWithSelector(token.transferFrom.selector, from, to, value)), "transferFrom failed");
    }
}

contract ERC20TimelockEitherBeneficiary {
    using SafeERC20 for IERC20;

    /// @notice 被锁定的代币
    IERC20  public immutable token;
    /// @notice 受益人 A
    address public immutable beneficiary1;
    /// @notice 受益人 B
    address public immutable beneficiary2;
    /// @notice 释放时间(部署时刻 + 3*365 天)
    uint64  public immutable releaseTime;
    /// @notice 部署者(仅用于查看/记录,无提币权限)
    address public immutable owner;

    // 简单的重入保护
    bool private _locked;
    modifier nonReentrant() {
        require(!_locked, "reentrancy");
        _locked = true;
        _;
        _locked = false;
    }

    event Funded(address indexed from, uint256 amount);
    event Withdrawn(address indexed beneficiary, uint256 amount);

    /// @param _token ERC20 代币地址(主网 XAUT:0x68749665FF8D2d112Fa859AA293F07A622782F38)
    /// @param _beneficiary1 受益人 A
    /// @param _beneficiary2 受益人 B
    constructor(
        IERC20 _token,
        address _beneficiary1,
        address _beneficiary2
    ) {
        require(address(_token) != address(0), "bad token");
        require(_beneficiary1 != address(0) && _beneficiary2 != address(0), "bad bene");
        token        = _token;
        beneficiary1 = _beneficiary1;
        beneficiary2 = _beneficiary2;
        // 使用固定的 3*365 天,简单稳定(如需精确历法可改用构造参数传入具体时间戳)
        releaseTime  = uint64(block.timestamp + 3 * 365 days);
        owner        = msg.sender;
    }

    /// @notice 多次存入;需要先在代币合约对本合约地址 approve 对应数量
    function fund(uint256 amount) external {
        require(amount > 0, "zero amount");
        token.safeTransferFrom(msg.sender, address(this), amount);
        emit Funded(msg.sender, amount);
    }

    /// @notice 到期后,仅受益人 A 或 B 可调用;一次性把合约全部余额转到「调用者本人」
    function withdrawAll() external nonReentrant {
        require(block.timestamp >= releaseTime, "not yet");
        require(msg.sender == beneficiary1 || msg.sender == beneficiary2, "not beneficiary");

        uint256 amt = token.balanceOf(address(this));
        require(amt > 0, "nothing to withdraw");

        // 通过 SafeERC20 将全部余额发送给调用者本人
        token.safeTransfer(msg.sender, amt);

        emit Withdrawn(msg.sender, amt);
    }

    // -------- 便捷只读 --------
    function balance() external view returns (uint256) { return token.balanceOf(address(this)); }
    function canWithdraw(address who) external view returns (bool) {
        return (block.timestamp >= releaseTime) &&
               (who == beneficiary1 || who == beneficiary2) &&
               (token.balanceOf(address(this)) > 0);
    }
}

/*
辅助信息:
- withdrawAll() 的 4-byte 选择器(备用,无 ABI 也可用 calldata 调):0x3ccfd60b
- XAUT (ERC20, 6 decimals) 主网合约:0x68749665FF8D2d112Fa859AA293F07A622782F38
*/
"
    }
  },
  "settings": {
    "optimizer": {
      "enabled": false,
      "runs": 200
    },
    "outputSelection": {
      "*": {
        "*": [
          "evm.bytecode",
          "evm.deployedBytecode",
          "devdoc",
          "userdoc",
          "metadata",
          "abi"
        ]
      }
    },
    "remappings": []
  }
}}

Tags:
Factory|addr:0x5e8dea337a3336f61fc7c592488c1602667327f4|verified:true|block:23722331|tx:0xc272d4285e953c81b122e7b4cc94ca9a9b38d94d354bab8c3b70d16fdc3361f7|first_check:1762248306

Submitted on: 2025-11-04 10:25:09

Comments

Log in to comment.

No comments yet.