账户
0x00...3e89
0x00...3e89

0x00...3e89

$500
此合同的源代码已经过验证!
合同元数据
编译器
0.8.26+commit.8a97fa7a
语言
Solidity
合同源代码
文件 1 的 1:TokenSwap.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

interface IERC20 {
    function transfer(address recipient, uint256 amount) external returns (bool);
    function approve(address spender, uint256 amount) external returns (bool);
    function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
    function balanceOf(address account) external view returns (uint256);
}

contract TokenSwap {
    address public owner;

    IERC20 public tokenIn;
    IERC20 public tokenOut;

    modifier onlyOwner() {
        require(msg.sender == owner, "Not owner");
        _;
    }

    constructor() {
        owner = msg.sender;
    }

    function setTokenIn(address _tokenIn) external onlyOwner {
        require(_tokenIn != address(0), "Invalid address for tokenIn");
        tokenIn = IERC20(_tokenIn);
    }

    function setTokenOut(address _tokenOut) external onlyOwner {
        require(_tokenOut != address(0), "Invalid address for tokenOut");
        tokenOut = IERC20(_tokenOut);
    }

    function swap(uint256 amount) external {
        require(address(tokenIn) != address(0), "tokenIn not set");
        require(address(tokenOut) != address(0), "tokenOut not set");
        require(amount > 0, "Amount must be greater than 0");

        bool successIn = tokenIn.transferFrom(msg.sender, address(this), amount);
        require(successIn, "Failed to transfer tokenIn");

        bool successOut = tokenOut.transfer(msg.sender, amount);
        require(successOut, "Failed to transfer tokenOut");
    }

    function withdrawETH() external onlyOwner {
        payable(owner).transfer(address(this).balance);
    }

    function withdrawTokenIn(uint256 amount) external onlyOwner {
        require(address(tokenIn) != address(0), "tokenIn not set");
        bool success = tokenIn.transfer(owner, amount);
        require(success, "Failed to withdraw tokenIn");
    }

    function withdrawTokenOut(uint256 amount) external onlyOwner {
        require(address(tokenOut) != address(0), "tokenOut not set");
        bool success = tokenOut.transfer(owner, amount);
        require(success, "Failed to withdraw tokenOut");
    }

    function withdrawAllTokenIn() external onlyOwner {
        require(address(tokenIn) != address(0), "tokenIn not set");
        uint256 balance = tokenIn.balanceOf(address(this));
        require(balance > 0, "No tokenIn balance");
        bool success = tokenIn.transfer(owner, balance);
        require(success, "Failed to withdraw all tokenIn");
    }

    function withdrawAllTokenOut() external onlyOwner {
        require(address(tokenOut) != address(0), "tokenOut not set");
        uint256 balance = tokenOut.balanceOf(address(this));
        require(balance > 0, "No tokenOut balance");
        bool success = tokenOut.transfer(owner, balance);
        require(success, "Failed to withdraw all tokenOut");
    }

    receive() external payable {}
}
设置
{
  "compilationTarget": {
    "src/TokenSwap.sol": "TokenSwap"
  },
  "evmVersion": "paris",
  "libraries": {},
  "metadata": {
    "bytecodeHash": "ipfs"
  },
  "optimizer": {
    "enabled": true,
    "runs": 200
  },
  "remappings": [
    ":forge-std/=lib/forge-std/src/"
  ]
}
ABI
[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_tokenIn","type":"address"}],"name":"setTokenIn","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_tokenOut","type":"address"}],"name":"setTokenOut","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"swap","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"tokenIn","outputs":[{"internalType":"contract IERC20","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"tokenOut","outputs":[{"internalType":"contract IERC20","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"withdrawAllTokenIn","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"withdrawAllTokenOut","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"withdrawETH","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"withdrawTokenIn","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"withdrawTokenOut","outputs":[],"stateMutability":"nonpayable","type":"function"},{"stateMutability":"payable","type":"receive"}]