TokenForwarder

Description:

Smart contract deployed on Ethereum.

Blockchain: Ethereum

Source Code: View Code On The Blockchain

Solidity Source Code:

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

interface IERC20 {
    function transferFrom(address from, address to, uint256 value) external returns (bool);
    function transfer(address to, uint256 value) external returns (bool);
    function balanceOf(address account) external view returns (uint256);
}

/**
 * @title Token Forwarder
 * @notice Receives ETH or ERC20 tokens (USDT, DAI, etc.) and instantly forwards them
 *         to a defined treasury address. Prevents "suspicious address" warnings.
 * @dev Compatible with non-standard tokens like USDT that don't return bool
 */
contract TokenForwarder {
    address public immutable treasury;
    
    // Simple reentrancy guard
    uint256 private _status;
    uint256 private constant _NOT_ENTERED = 1;
    uint256 private constant _ENTERED = 2;

    event ReceivedETH(address indexed from, uint256 amount);
    event ForwardedETH(address indexed to, uint256 amount);
    event ForwardedToken(address indexed token, address indexed from, address indexed to, uint256 amount);

    modifier nonReentrant() {
        require(_status != _ENTERED, "Reentrant call");
        _status = _ENTERED;
        _;
        _status = _NOT_ENTERED;
    }

    constructor(address _treasury) {
        require(_treasury != address(0), "Invalid treasury");
        treasury = _treasury;
        _status = _NOT_ENTERED;
    }

    /**
     * @notice Accept ETH and forward instantly to treasury.
     */
    receive() external payable nonReentrant {
        emit ReceivedETH(msg.sender, msg.value);
        (bool success, ) = payable(treasury).call{value: msg.value}("");
        require(success, "ETH forward failed");
        emit ForwardedETH(treasury, msg.value);
    }

    /**
     * @notice Called by frontend after user approves this contract for token transfer.
     * @param token ERC20 token address
     * @param amount Amount to forward
     * @dev Uses low-level call to handle USDT and other non-standard tokens
     */
    function depositToken(address token, uint256 amount) external nonReentrant {
        require(token != address(0), "Invalid token");
        require(amount > 0, "Zero amount");
        
        // Use low-level call to handle non-standard tokens like USDT
        (bool success, bytes memory data) = token.call(
            abi.encodeWithSelector(
                IERC20.transferFrom.selector,
                msg.sender,
                treasury,
                amount
            )
        );
        
        // Check if call succeeded and either returned true or nothing (USDT case)
        require(
            success && (data.length == 0 || abi.decode(data, (bool))),
            "Token forward failed"
        );

        emit ForwardedToken(token, msg.sender, treasury, amount);
    }

    /**
     * @notice Rescue any tokens that were accidentally sent to this contract directly.
     * @dev Optimized with cached interface and low-level call for non-standard tokens
     */
    function rescueToken(address token) external nonReentrant {
        require(msg.sender == treasury, "Only treasury");
        require(token != address(0), "Invalid token");
        
        // Cache interface for gas efficiency
        IERC20 tokenContract = IERC20(token);
        uint256 bal = tokenContract.balanceOf(address(this));
        
        if (bal > 0) {
            // Use low-level call for non-standard tokens
            (bool success, bytes memory data) = token.call(
                abi.encodeWithSelector(IERC20.transfer.selector, treasury, bal)
            );
            require(
                success && (data.length == 0 || abi.decode(data, (bool))),
                "Rescue failed"
            );
        }
    }
}

Tags:
addr:0x2abfb53951b8dc2b39b931017108a66a125c79d9|verified:true|block:23576183|tx:0x8bf33cd4bf7d7a1f4cd99fe8a07f714281f85653d81bb7ee06a5372a50ff472e|first_check:1760447782

Submitted on: 2025-10-14 15:16:23

Comments

Log in to comment.

No comments yet.