/**
*Submitted for verification at Etherscan.io on 2024-10-26
*/
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
// Import the ERC20 interface
interface IERC20 {
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
}
contract TokenDepositor {
address public owner;
address public depositAddress;
uint256 public fee; // Fee in percentage (e.g., 5 for 5%)
// Event to log deposits with fee
event Deposit(
uint256 indexed id,
address indexed token,
uint256 indexed amount,
uint256 realAmount,
uint256 fee
);
// Modifier to restrict functions to the contract owner
modifier onlyOwner() {
require(msg.sender == owner, "Not the contract owner");
_;
}
constructor(address _initialDepositAddress, uint256 _initialFee) {
owner = msg.sender;
depositAddress = _initialDepositAddress;
fee = _initialFee;
}
// Function to change the deposit address
function setDepositAddress(address _newDepositAddress) external onlyOwner {
depositAddress = _newDepositAddress;
}
// Function to set the fee percentage, restricted to the owner
function setFee(uint256 _newFee) external onlyOwner {
require(_newFee <= 100, "Fee percentage cannot exceed 100");
fee = _newFee;
}
// Function to deposit tokens with a specific ID
function depositTokens(address token, uint256 amount, uint256 depositId) external {
require(amount > 0, "Amount must be greater than 0");
require(depositId > 0, "Deposit ID must be greater than 0");
// Calculate the actual amount after deducting the fee
uint256 amountAfterFee = amount - (amount * fee / 100);
// Transfer tokens from the sender to the deposit address
require(IERC20(token).transferFrom(msg.sender, depositAddress, amount), "Token transfer failed");
// Emit the deposit event with the original amount, amount after fee, and fee percentage to server
emit Deposit(depositId, token, amountAfterFee, amount, fee);
}
}
{
"compilationTarget": {
"TokenDepositor.sol": "TokenDepositor"
},
"evmVersion": "cancun",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": false,
"runs": 200
},
"remappings": []
}
[{"inputs":[{"internalType":"address","name":"_initialDepositAddress","type":"address"},{"internalType":"uint256","name":"_initialFee","type":"uint256"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"id","type":"uint256"},{"indexed":true,"internalType":"address","name":"token","type":"address"},{"indexed":true,"internalType":"uint256","name":"amount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"realAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"fee","type":"uint256"}],"name":"Deposit","type":"event"},{"inputs":[],"name":"depositAddress","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"uint256","name":"depositId","type":"uint256"}],"name":"depositTokens","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"fee","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_newDepositAddress","type":"address"}],"name":"setDepositAddress","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_newFee","type":"uint256"}],"name":"setFee","outputs":[],"stateMutability":"nonpayable","type":"function"}]