-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRaffleTest.t.sol
249 lines (217 loc) · 8.49 KB
/
RaffleTest.t.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
import {Test} from "forge-std/Test.sol"; // How am I accessing this when the file doesn't exist in the directory?
import {DeployRaffle} from "script/DeployRaffle.s.sol";
import {Raffle} from "src/Raffle.sol";
import {HelperConfig} from "script/HelperConfig.s.sol";
import {Vm} from "forge-std/Vm.sol"; // How am I accessing this when the file doesn't exist in the directory?
import {VRFCoordinatorV2_5Mock} from "lib/chainlink-brownie-contracts/contracts/src/v0.8/vrf/mocks/VRFCoordinatorV2_5Mock.sol";
import {CodeConstants} from "script/HelperConfig.s.sol";
contract RaffleTest is CodeConstants, Test {
Raffle public raffle;
HelperConfig public helperConfig;
event RequestedRaffleWinner(uint256 indexed requestId);
event RaffleEntered(address indexed player);
event WinnerPicked(address indexed player);
uint256 entranceFee;
uint256 interval;
address vrfCoordinator;
bytes32 gasLane;
uint256 subscriptionId;
uint32 callbackGasLimit;
address public PLAYER = makeAddr("player");
uint256 public constant STARTING_PLAYER_BALANCE = 10 ether;
function setUp() external {
DeployRaffle deployer = new DeployRaffle();
(raffle, helperConfig) = deployer.run();
HelperConfig.NetworkConfig memory config = helperConfig.getConfig();
entranceFee = config.entranceFee;
interval = config.interval;
vrfCoordinator = config.vrfCoordinator;
gasLane = config.gasLane;
subscriptionId = config.subscriptionId;
callbackGasLimit = config.callbackGasLimit;
vm.deal(PLAYER, STARTING_PLAYER_BALANCE);
}
function testRaffleInitializesInOpenState() public view {
assert(raffle.getRaffleState() == Raffle.RaffleState.OPEN);
}
/*//////////////////////////////////////////////////////////////
ENTER RAFFLE
//////////////////////////////////////////////////////////////*/
function testRaffleRevertsWhenYouDontPayEnough() public {
//Arrange
vm.prank(PLAYER);
//Act
vm.expectRevert(Raffle.Raffle__NotEnoughEthSentToEnterRaffle.selector);
raffle.enterRaffle();
//Assert
}
function testRaffleAddsPlayerToPlayersArray() public {
//Arrange
vm.prank(PLAYER);
//Act
raffle.enterRaffle{value: entranceFee}();
//Assert
address playerRecorded = raffle.getPlayer(0);
assert(playerRecorded == PLAYER);
}
function testEnteringRaffleEmitsEvent() public {
//Arrange
vm.prank(PLAYER);
//Act
vm.expectEmit(true, false, false, false, address(raffle));
emit RaffleEntered(PLAYER);
//Assert
raffle.enterRaffle{value: entranceFee}();
}
function testDontAllowPlayersToEnterWhileRaffleIsCalculating() public {
//Arrange
vm.prank(PLAYER);
//Act
raffle.enterRaffle{value: entranceFee}();
vm.warp(block.timestamp + interval + 1); // Ensure that we are past the raffle deadline
vm.roll(block.number + 1); // Ensure that we are in a new block
raffle.performUpkeep("");
//Assert
vm.expectRevert(Raffle.Raffle__RaffleNotOpen.selector);
raffle.enterRaffle{value: entranceFee}();
}
/*//////////////////////////////////////////////////////////////
CHECK UPKEEP
//////////////////////////////////////////////////////////////*/
function testCheckUpkeepReturnsFalseIfItHasNoBalance() public {
//Arrange
vm.warp(block.timestamp + interval + 1); // Ensure that we are past the raffle deadline
//Act
(bool upkeepNeeded, ) = raffle.checkUpkeep("");
//Assert
assert(!upkeepNeeded);
}
function testCheckUpkeepReturnsFalseIfRaffleIsNotOpen() public {
//Arrange
vm.prank(PLAYER);
raffle.enterRaffle{value: entranceFee}();
vm.warp(block.timestamp + interval + 1); // Ensure that we are past the raffle deadline
vm.roll(block.number + 1); // Ensure that we are in a new block
raffle.performUpkeep("");
//Act
(bool upkeepNeeded, ) = raffle.checkUpkeep("");
//Assert
assert(!upkeepNeeded);
}
/*//////////////////////////////////////////////////////////////
PERFORM UPKEEP
//////////////////////////////////////////////////////////////*/
function testPerformUpkeepCanOnlyRunIfCheckUpkeepIsTrue() public {
//Arrange
vm.prank(PLAYER);
raffle.enterRaffle{value: entranceFee}();
vm.warp(block.timestamp + interval + 1); // Ensure that we are past the raffle deadline
vm.roll(block.number + 1); // Ensure that we are in a new block
//Act/Assert
raffle.performUpkeep("");
}
function testPerformUpkeepRevertsIfCheckUpkeepIsFalse() public {
//Arrange
uint256 currentBalance = 0;
uint256 numPlayers = 0;
Raffle.RaffleState rState = raffle.getRaffleState();
vm.prank(PLAYER);
raffle.enterRaffle{value: entranceFee}();
currentBalance = currentBalance + entranceFee;
numPlayers = 1;
//Act/Assert
vm.expectRevert(
abi.encodeWithSelector(
Raffle.Raffle__UpkeepNotNeeded.selector,
currentBalance,
numPlayers,
rState
)
);
raffle.performUpkeep("");
}
modifier raffleEntered() {
vm.prank(PLAYER);
raffle.enterRaffle{value: entranceFee}();
vm.warp(block.timestamp + interval + 1); // Ensure that we are past the raffle deadline
vm.roll(block.number + 1); // Ensure that we are in a new block
_;
}
function testPerformUpkeepUpdatesRaffleStateAndEmitsRequestID()
public
raffleEntered
{
//Act
vm.recordLogs();
raffle.performUpkeep("");
Vm.Log[] memory entries = vm.getRecordedLogs();
bytes32 requestId = entries[1].topics[1];
//Assert
Raffle.RaffleState raffleState = raffle.getRaffleState();
assert(uint256(requestId) > 0);
assert(uint256(raffleState) == 1);
}
/*//////////////////////////////////////////////////////////////
FULLFILLRANDOMSWORDS
//////////////////////////////////////////////////////////////*/
modifier skipFork() {
if (block.chainid != LOCAL_CHAIN_ID) {
return;
}
_;
}
function testFulfillrandomwordsCanOnlyBeCalledAfterPerformUpkeep(
//This a fuzz test. The number of runs is configured in the TOML file
uint256 randomRequestId
) public raffleEntered skipFork {
//Act/Assert
vm.expectRevert(VRFCoordinatorV2_5Mock.InvalidRequest.selector);
VRFCoordinatorV2_5Mock(vrfCoordinator).fulfillRandomWords(
randomRequestId,
address(raffle)
);
}
function testFulfillRandomWordsPicksAWinnerResetsArrayAndSendMoney()
public
raffleEntered
skipFork
{
//End-to-end test
//Arrange
uint256 additionalEntrants = 3; //4 total players
uint256 startingIndex = 1;
address expectedWinner = address(1);
for (
uint256 i = startingIndex;
i < startingIndex + additionalEntrants;
i++
) {
address newPlayer = address(uint160(i));
hoax(newPlayer, 1 ether);
raffle.enterRaffle{value: entranceFee}();
}
uint256 startingTimeStamp = raffle.getLastTimeStamp();
uint256 winnerStartingBalance = expectedWinner.balance;
//Act
vm.recordLogs();
raffle.performUpkeep("");
Vm.Log[] memory entries = vm.getRecordedLogs();
bytes32 requestId = entries[1].topics[1];
VRFCoordinatorV2_5Mock(vrfCoordinator).fulfillRandomWords(
uint256(requestId),
address(raffle)
);
//Assert
address recentWinner = raffle.getRecentWinner();
Raffle.RaffleState raffleState = raffle.getRaffleState();
uint256 winnerBalance = recentWinner.balance;
uint256 endingTimeStamp = raffle.getLastTimeStamp();
uint256 prize = entranceFee * (additionalEntrants + 1); //4 players total
assert(recentWinner == expectedWinner);
assert(uint256(raffleState) == 0);
assert(winnerBalance == winnerStartingBalance + prize);
assert(endingTimeStamp > startingTimeStamp);
}
}