-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathWETH.sol
38 lines (33 loc) · 1.28 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
38
// SPDX-License-Identifier: MIT
// author: 0xAA
// original contract on ETH: https://rinkeby.etherscan.io/token/0xc778417e063141139fce010982780140aa0cd5ab?a=0xe16c1623c1aa7d919cd2241d8b36d9e79c1be2a2#code
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);
}
}