-
Notifications
You must be signed in to change notification settings - Fork 119
/
WETH.sol
37 lines (31 loc) · 1.12 KB
/
WETH.sol
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;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract WETH is ERC20{
// 事件:存款和取款
event Deposit(address indexed dst, uint wad);
event Withdrawal(address indexed src, uint wad);
// 构造函数,初始化ERC20的名字和代号
constructor() ERC20("WETH", "WETH"){
}
// 回调函数,当用户往WETH合约转ETH时,会触发deposit()函数
fallback() external payable {
deposit();
}
// 回调函数,当用户往WETH合约转ETH时,会触发deposit()函数
receive() external payable {
deposit();
}
// 存款函数,当用户存入ETH时,给他铸造等量的WETH
function deposit() public payable {
_mint(msg.sender, msg.value);
emit Deposit(msg.sender, msg.value);
}
// 提款函数,用户销毁WETH,取回等量的ETH
function withdraw(uint amount) public {
require(balanceOf(msg.sender) >= amount);
_burn(msg.sender, amount);
payable(msg.sender).transfer(amount);
emit Withdrawal(msg.sender, amount);
}
}