编译器
0.8.20+commit.a1b79de6
文件 1 的 2:IERC20.sol
pragma solidity ^0.8.20;
interface IERC20 {
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address to, uint256 value) external returns (bool);
function allowance(address owner, address spender) external view returns (uint256);
function approve(address spender, uint256 value) external returns (bool);
function transferFrom(address from, address to, uint256 value) external returns (bool);
}
文件 2 的 2:SwapDVT.sol
pragma solidity ^0.8.20;
import "./IERC20.sol";
contract SwapDVT {
address public owner;
address public old_token;
address public new_token;
uint256 public rate;
event Swapped(address indexed user, uint256 sent, uint256 received);
constructor(address _old, address _new, uint256 _rate) {
owner = msg.sender;
old_token = _old;
new_token = _new;
rate = _rate == 0 ? 10: _rate;
}
function old_token_balance_of(address _user) public view returns (uint256) {
return IERC20(old_token).balanceOf(_user);
}
function new_token_balance_of(address _user) public view returns (uint256) {
return IERC20(new_token).balanceOf(_user);
}
function swap(uint256 _amount) external {
require(_amount > rate, "Amount must be greater than rate value");
uint256 received_amount = _amount / rate;
IERC20(old_token).transferFrom(msg.sender, address(this), _amount);
IERC20(new_token).transfer(msg.sender, received_amount);
emit Swapped(msg.sender, _amount, received_amount);
}
}
{
"compilationTarget": {
"contracts/SwapDVT.sol": "SwapDVT"
},
"evmVersion": "paris",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": true,
"runs": 800
},
"remappings": []
}
[{"inputs":[{"internalType":"address","name":"_old","type":"address"},{"internalType":"address","name":"_new","type":"address"},{"internalType":"uint256","name":"_rate","type":"uint256"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"user","type":"address"},{"indexed":false,"internalType":"uint256","name":"sent","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"received","type":"uint256"}],"name":"Swapped","type":"event"},{"inputs":[],"name":"new_token","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_user","type":"address"}],"name":"new_token_balance_of","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"old_token","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_user","type":"address"}],"name":"old_token_balance_of","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"rate","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"swap","outputs":[],"stateMutability":"nonpayable","type":"function"}]