-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTokenBank.sol
More file actions
37 lines (27 loc) · 1.12 KB
/
Copy pathTokenBank.sol
File metadata and controls
37 lines (27 loc) · 1.12 KB
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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
interface IERC20 {
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
function transfer(address recipient, uint256 amount) external returns (bool);
}
contract TokenBank {
IERC20 public token; // ERC20 Token 合约引用
// 每个地址的存款余额
mapping(address => uint256) public balances;
constructor(address tokenAddress) {
token = IERC20(tokenAddress); // ✅ 使用传入参数
}
function deposit(uint256 amount) external {
require(amount > 0, "deposit amount must be > 0");
bool success = token.transferFrom(msg.sender, address(this), amount);
require(success, "deposit failed");
balances[msg.sender] += amount;
}
function withdraw(uint256 amount) external {
require(amount > 0, "withdraw amount must be > 0");
require(balances[msg.sender] >= amount, "insufficient balance");
balances[msg.sender] -= amount;
bool success = token.transfer(msg.sender, amount);
require(success, "withdraw failed");
}
}