Created
November 23, 2023 17:08
-
-
Save anontheauditor/1010d9963bbd04ac5fe9448ffd7e5d95 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity 0.8.20; | |
contract Winner 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)) { | |
currentleader = msg.sender; | |
lastDepositedAmount = msg.value; | |
currentLeaderReward = nextLeaderReward; | |
nextLeaderReward += lastDepositedAmount / 10; | |
} | |
else { | |
uint256 refundAmount = lastDepositedAmount * 9 / 10; | |
address prevLeader = currentleader; | |
currentleader = msg.sender; | |
lastDepositedAmount = msg.value; | |
currentLeaderReward = nextLeaderReward; | |
nextLeaderReward += lastDepositedAmount / 10; | |
(bool success, ) = prevLeader.call{value: refundAmount}(""); | |
require(success, "Failed to send Ether"); | |
} | |
} | |
// @notice 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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment