-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathInsecureWinnerTakesItAll.sol
88 lines (67 loc) · 3.52 KB
/
InsecureWinnerTakesItAll.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
// SPDX-License-Identifier: BSL-1.0 (Boost Software License 1.0)
//-------------------------------------------------------------------------------------//
// Copyright (c) 2022 - 2023 serial-coder: Phuwanai Thummavet ([email protected]) //
//-------------------------------------------------------------------------------------//
// For more info, please refer to my article:
// - On Medium: https://medium.com/valixconsulting/solidity-security-by-example-09-denial-of-service-with-revert-814f55b61e02
// - On serial-coder.com: https://www.serial-coder.com/post/solidity-smart-contract-security-by-example-09-denial-of-service-with-revert/
pragma solidity 0.8.19;
import "./Dependencies.sol";
contract InsecureWinnerTakesItAll is ReentrancyGuard {
address public currentleader;
uint256 public lastDepositedAmount;
uint256 public currentLeaderReward;
uint256 public nextLeaderReward;
bool public rewardClaimed;
uint256 public immutable challengeEnd;
constructor(uint256 _challengePeriod) payable {
require(msg.value == 10 ether, "Require an initial 10 Ethers reward");
currentleader = address(0);
lastDepositedAmount = msg.value;
currentLeaderReward = 0;
nextLeaderReward = msg.value;
rewardClaimed = false;
challengeEnd = block.timestamp + _challengePeriod;
}
function claimLeader() external payable noReentrant {
require(block.timestamp < challengeEnd, "Challenge is finished");
require(msg.sender != currentleader, "You are the current leader");
require(msg.value > lastDepositedAmount, "You must pay more than the current leader");
if (currentleader == address(0)) { // First claimer (no need to refund the initial reward)
// Assign the new leader
currentleader = msg.sender;
lastDepositedAmount = msg.value;
currentLeaderReward = nextLeaderReward; // Accrue the reward
nextLeaderReward += lastDepositedAmount / 10; // Deduct 10% from the last deposited amount for the next leader
}
else { // Next claimers
// Refund the previous leader with 90% of his deposit
uint256 refundAmount = lastDepositedAmount * 9 / 10;
// Assign the new leader
address prevLeader = currentleader;
currentleader = msg.sender;
lastDepositedAmount = msg.value;
currentLeaderReward = nextLeaderReward; // Accrue the reward
nextLeaderReward += lastDepositedAmount / 10; // Deduct 10% from the last deposited amount for the next leader
(bool success, ) = prevLeader.call{value: refundAmount}("");
require(success, "Failed to send Ether");
}
}
// For the winner to claim principal and reward
function claimPrincipalAndReward() external noReentrant {
require(block.timestamp >= challengeEnd, "Challenge is not finished yet");
require(msg.sender == currentleader, "You are not the winner");
require(!rewardClaimed, "Reward was claimed");
rewardClaimed = true;
// Transfer principal + reward to the winner
uint256 amount = lastDepositedAmount + currentLeaderReward;
(bool success, ) = currentleader.call{value: amount}("");
require(success, "Failed to send Ether");
}
function getEtherBalance() external view returns (uint256) {
return address(this).balance;
}
function isChallengeEnd() external view returns (bool) {
return block.timestamp >= challengeEnd;
}
}