Repository navigation
Expand file tree
/
Copy pathWETH.t.sol
More file actions
146 lines (100 loc) · 4.04 KB
/
Copy pathWETH.t.sol
File metadata and controls
146 lines (100 loc) · 4.04 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.15;
import {DSTestPlus} from "./utils/DSTestPlus.sol";
import {DSInvariantTest} from "./utils/DSInvariantTest.sol";
import {SafeTransferLib} from "../utils/SafeTransferLib.sol";
import {WETH} from "../tokens/WETH.sol";
contract WETHTest is DSTestPlus {
WETH weth;
function setUp() public {
weth = new WETH();
}
function testFallbackDeposit() public {
assertEq(weth.balanceOf(address(this)), 0);
assertEq(weth.totalSupply(), 0);
SafeTransferLib.safeTransferETH(address(weth), 1 ether);
assertEq(weth.balanceOf(address(this)), 1 ether);
assertEq(weth.totalSupply(), 1 ether);
}
function testDeposit() public {
assertEq(weth.balanceOf(address(this)), 0);
assertEq(weth.totalSupply(), 0);
weth.deposit{value: 1 ether}();
assertEq(weth.balanceOf(address(this)), 1 ether);
assertEq(weth.totalSupply(), 1 ether);
}
function testWithdraw() public {
uint256 startingBalance = address(this).balance;
weth.deposit{value: 1 ether}();
weth.withdraw(1 ether);
uint256 balanceAfterWithdraw = address(this).balance;
assertEq(balanceAfterWithdraw, startingBalance);
assertEq(weth.balanceOf(address(this)), 0);
assertEq(weth.totalSupply(), 0);
}
function testPartialWithdraw() public {
weth.deposit{value: 1 ether}();
uint256 balanceBeforeWithdraw = address(this).balance;
weth.withdraw(0.5 ether);
uint256 balanceAfterWithdraw = address(this).balance;
assertEq(balanceAfterWithdraw, balanceBeforeWithdraw + 0.5 ether);
assertEq(weth.balanceOf(address(this)), 0.5 ether);
assertEq(weth.totalSupply(), 0.5 ether);
}
function testFallbackDeposit(uint256 amount) public {
amount = bound(amount, 0, address(this).balance);
assertEq(weth.balanceOf(address(this)), 0);
assertEq(weth.totalSupply(), 0);
SafeTransferLib.safeTransferETH(address(weth), amount);
assertEq(weth.balanceOf(address(this)), amount);
assertEq(weth.totalSupply(), amount);
}
function testDeposit(uint256 amount) public {
amount = bound(amount, 0, address(this).balance);
assertEq(weth.balanceOf(address(this)), 0);
assertEq(weth.totalSupply(), 0);
weth.deposit{value: amount}();
assertEq(weth.balanceOf(address(this)), amount);
assertEq(weth.totalSupply(), amount);
}
function testWithdraw(uint256 depositAmount, uint256 withdrawAmount) public {
depositAmount = bound(depositAmount, 0, address(this).balance);
withdrawAmount = bound(withdrawAmount, 0, depositAmount);
weth.deposit{value: depositAmount}();
uint256 balanceBeforeWithdraw = address(this).balance;
weth.withdraw(withdrawAmount);
uint256 balanceAfterWithdraw = address(this).balance;
assertEq(balanceAfterWithdraw, balanceBeforeWithdraw + withdrawAmount);
assertEq(weth.balanceOf(address(this)), depositAmount - withdrawAmount);
assertEq(weth.totalSupply(), depositAmount - withdrawAmount);
}
receive() external payable {}
}
contract WETHInvariants is DSTestPlus, DSInvariantTest {
WETHTester wethTester;
WETH weth;
function setUp() public {
weth = new WETH();
wethTester = new WETHTester{value: address(this).balance}(weth);
addTargetContract(address(wethTester));
}
function invariantTotalSupplyEqualsBalance() public {
assertEq(address(weth).balance, weth.totalSupply());
}
}
contract WETHTester {
WETH weth;
constructor(WETH _weth) payable {
weth = _weth;
}
function deposit(uint256 amount) public {
weth.deposit{value: amount}();
}
function fallbackDeposit(uint256 amount) public {
SafeTransferLib.safeTransferETH(address(weth), amount);
}
function withdraw(uint256 amount) public {
weth.withdraw(amount);
}
receive() external payable {}
}