-
Notifications
You must be signed in to change notification settings - Fork 579
Expand file tree
/
Copy pathMockWETH.sol
More file actions
77 lines (65 loc) · 2.23 KB
/
MockWETH.sol
File metadata and controls
77 lines (65 loc) · 2.23 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity ^0.8.22;
import {IWETH} from "../token/interfaces/IWETH.sol";
/// @dev Minimal WETH9-compatible mock for tests.
contract MockWETH is IWETH {
string public constant name = "Wrapped Ether";
string public constant symbol = "WETH";
uint8 public constant decimals = 18;
mapping(address => uint256) public override balanceOf;
mapping(address => mapping(address => uint256)) public override allowance;
event Deposit(address indexed account, uint256 amount);
event Withdrawal(address indexed account, uint256 amount);
function totalSupply() external view override returns (uint256) {
return address(this).balance;
}
function deposit() public payable override {
balanceOf[msg.sender] += msg.value;
emit Deposit(msg.sender, msg.value);
}
function withdraw(uint256 amount) external override {
require(balanceOf[msg.sender] >= amount, "WETH: balance");
balanceOf[msg.sender] -= amount;
(bool ok, ) = msg.sender.call{value: amount}("");
require(ok, "WETH: send failed");
emit Withdrawal(msg.sender, amount);
}
function approve(
address spender,
uint256 amount
) external override returns (bool) {
allowance[msg.sender][spender] = amount;
return true;
}
function transfer(
address to,
uint256 amount
) external override returns (bool) {
return _transfer(msg.sender, to, amount);
}
function transferFrom(
address from,
address to,
uint256 amount
) external override returns (bool) {
uint256 allowed = allowance[from][msg.sender];
if (allowed != type(uint256).max) {
require(allowed >= amount, "WETH: allowance");
allowance[from][msg.sender] = allowed - amount;
}
return _transfer(from, to, amount);
}
function _transfer(
address from,
address to,
uint256 amount
) internal returns (bool) {
require(balanceOf[from] >= amount, "WETH: balance");
balanceOf[from] -= amount;
balanceOf[to] += amount;
return true;
}
receive() external payable {
deposit();
}
}