solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
contract ITradeWallet is Ownable, ReentrancyGuard {
using SafeERC20 for IERC20;
// user => token => amount
mapping(address => mapping(address => uint256)) public userBalances;
bool public paused;
event DepositedToken(address indexed user, address indexed token, uint256 amount);
event WithdrawnToken(address indexed admin, address indexed token, uint256 amount);
event WithdrawnETH(address indexed admin, uint256 amount);
event EmergencyPaused(address indexed admin, bool status);
constructor() Ownable(msg.sender) {}
modifier whenNotPaused() {
require(!paused, "Contract paused");
_;
}
receive() external payable {}
fallback() external payable {}
// =====================
// USER
// =====================
function depositToken(address token, uint256 amount) external {
require(token != address(0), "Invalid token");
IERC20(token).safeTransferFrom(msg.sender, address(this), amount);
userBalances[msg.sender][token] += amount;
emit DepositedToken(msg.sender, token, amount);
}
// =====================
// ADMIN
// =====================
function withdrawETH(uint256 amount) external onlyOwner nonReentrant whenNotPaused {
require(address(this).balance >= amount, "Insufficient ETH");
(bool success, ) = owner().call{value: amount}("");
require(success, "ETH transfer failed");
emit WithdrawnETH(msg.sender, amount);
}
function withdrawToken(address token, uint256 amount) external onlyOwner nonReentrant whenNotPaused {
require(token != address(0), "Invalid token");
require(IERC20(token).balanceOf(address(this)) >= amount, "Insufficient token balance");
IERC20(token).safeTransfer(owner(), amount);
emit WithdrawnToken(msg.sender, token, amount);
}
function setPaused(bool _paused) external onlyOwner {
paused = _paused;
emit EmergencyPaused(msg.sender, _paused);
}
// =====================
// SEARCH
// =====================
function getBalance(address user, address token) external view returns (uint256) {
return userBalances[user][token];
}
function getETHBalance() external view returns (uint256) {
return address(this).balance;
}
function getTokenBalance(address token) external view returns (uint256) {
return IERC20(token).balanceOf(address(this));
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81