-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathBank.sol
More file actions
62 lines (44 loc) · 1.79 KB
/
Copy pathBank.sol
File metadata and controls
62 lines (44 loc) · 1.79 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/extensions/draft-IERC20Permit.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/interfaces/IERC1820Registry.sol";
interface TokenRecipient {
function tokensReceived(address sender, uint amount) external returns (bool);
}
contract Bank is TokenRecipient {
mapping(address => uint) public deposited;
address public immutable token;
// keccak256("ERC777TokensRecipient")
bytes32 constant private TOKENS_RECIPIENT_INTERFACE_HASH =
0xb281fc8c12954d22544db45de3159a39272895b169a852b314f9cc762e44c53b;
IERC1820Registry private erc1820 = IERC1820Registry(0x1820a4B7618BdE71Dce8cdc73aAB6C95905faD24);
constructor(address _token) {
token = _token;
//erc1820.setInterfaceImplementer(address(this), TOKENS_RECIPIENT_INTERFACE_HASH, address(this));
}
function tokensReceived(address sender, uint amount) external returns (bool) {
require(msg.sender == token, "invalid");
deposited[sender] += amount;
return true;
}
function deposit(address user, uint amount) public {
IERC20(token).transferFrom(msg.sender, address(this), amount);
deposited[user] += amount;
}
function permitDeposit(address user, uint amount, uint deadline, uint8 v, bytes32 r, bytes32 s) external {
IERC20Permit(token).permit(msg.sender, address(this), amount, deadline, v, r, s);
deposit(user, amount);
}
// 收款时被回调
function tokensReceived(
address operator,
address from,
address to,
uint amount,
bytes calldata userData,
bytes calldata operatorData
) external {
require(msg.sender == token, "invalid");
deposited[from] += amount;
}
}