-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathWETH.sol
58 lines (45 loc) · 1.52 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// SPDX-License-Identifier: MIT
//
// https://www.youtube.com/watch?v=UqKQ1bTatUs
pragma solidity ^0.8.0;
import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
/// @notice Just a demo of Wrapped Ether implementation.
/// @author Inspired by solmate's WETH (https://github.com/transmissions11/solmate/blob/main/src/tokens/WETH.sol)
/// @author Inspired by WETH9 (https://github.com/dapphub/ds-weth/blob/master/src/weth9.sol)
contract WETH is ERC20 {
event Deposit(address indexed from, uint256 amount);
event Withdraw(address indexed to, uint256 amount);
constructor() ERC20("Wrapped ETH", "WETH") {}
/**
* @dev `18` is already the default value, but just wanted to show it explicit.
*/
function decimals() public pure override returns (uint8) {
return 18;
}
// send ETH
// |
// is msg.data empty?
// / \
// yes no
// / \
// receive() exists? fallback()
// / \
// yes no
// / \
// receive() fallback()
receive() external payable {
deposit();
}
fallback() external payable {
deposit();
}
function deposit() public payable {
_mint(msg.sender, msg.value);
emit Deposit(msg.sender, msg.value);
}
function withdraw(uint256 amount) external {
_burn(msg.sender, amount);
emit Withdraw(msg.sender, amount);
payable(msg.sender).transfer(amount);
}
}