@@ -6,6 +6,7 @@ import {DeployRaffle} from "../../script/DeployRaffle.s.sol";
6
6
import {HelperConfig} from "../../script/HelperConfig.s.sol " ;
7
7
import {Raffle} from "../../src/Raffle.sol " ;
8
8
import {Vm} from "forge-std/Vm.sol " ;
9
+ import {VRFCoordinatorV2_5Mock} from "@chainlink/contracts/src/v0.8/vrf/mocks/VRFCoordinatorV2_5Mock.sol " ;
9
10
10
11
contract RaffleTest is Test {
11
12
Raffle public raffle;
@@ -136,4 +137,40 @@ contract RaffleTest is Test {
136
137
assert (uint256 (requestId) > 0 );
137
138
assert (uint256 (raffleState) == 1 );
138
139
}
140
+
141
+ // FULFILL RANDOM WORDS
142
+ function testFulfillRandomWordsCanOnlyBeCalledAfterPerformUpkeep (uint256 randomRequestId ) public raffleEntered {
143
+ vm.expectRevert (VRFCoordinatorV2_5Mock.InvalidRequest.selector );
144
+ VRFCoordinatorV2_5Mock (vrfCoordinator).fulfillRandomWords (randomRequestId, address (raffle));
145
+ }
146
+
147
+ function testFulFillRandomWordsPicksAWinnerResetsAndSendMoney () public raffleEntered {
148
+ uint256 additionalEntrants = 3 ;
149
+ uint256 startingIndex = 1 ;
150
+ address expectedWinner = address (1 );
151
+ for (uint256 i = 1 ; i < startingIndex + additionalEntrants; i++ ) {
152
+ address newPlayer = address (uint160 (i));
153
+ hoax (newPlayer, 1 ether);
154
+ raffle.enterRaffle {value: entranceFee}();
155
+ }
156
+ uint256 startingTimeStamp = raffle.getLastTimeStamp ();
157
+ uint256 winnerStartingBalance = expectedWinner.balance;
158
+
159
+ vm.recordLogs ();
160
+ raffle.performUpkeep ("" );
161
+ Vm.Log[] memory entries = vm.getRecordedLogs ();
162
+ bytes32 requestId = entries[1 ].topics[1 ];
163
+ VRFCoordinatorV2_5Mock (vrfCoordinator).fulfillRandomWords (uint256 (requestId), address (raffle));
164
+
165
+ address recentWinner = raffle.getRecentWinner ();
166
+ Raffle.RaffleState raffleState = raffle.getRaffleState ();
167
+ uint256 winnerBalance = recentWinner.balance;
168
+ uint256 endingTimeStamp = raffle.getLastTimeStamp ();
169
+ uint256 prize = entranceFee * (additionalEntrants + 1 );
170
+
171
+ assert (recentWinner == expectedWinner);
172
+ assert (uint256 (raffleState) == 0 );
173
+ assert (winnerBalance == winnerStartingBalance + prize);
174
+ assert (endingTimeStamp > startingTimeStamp);
175
+ }
139
176
}
0 commit comments