-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathEthGame.sol
43 lines (33 loc) · 1.55 KB
/
EthGame.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
// SPDX-License-Identifier: MIT
//
// https://cryptomarketpool.com/deposit-14-eth-game-in-a-solidity-smart-contract/
pragma solidity ^0.8.0;
// A game where the 14th person that deposit ether wins all the ether in the contract
// the winner can claim the 14 ether
contract EthGame {
uint256 public targetAmount = 14 ether;
address public winner;
//need to create a balance state variable to prevent forcefully sending ether
uint256 public balance;
function deposit() public payable {
require(msg.value == 1 ether, "You can only send 1 Ether");
//if the current balance is greater then the targetAmount then the game is over
//to prevent forcefully sending eth the balance needs to be updated manually instead of just checking the balance amount
//to prevent forcefully sending ether update the balance state variable manually
balance += msg.value;
require(balance <= targetAmount, "Game is over");
//if the balance is == to the targetAmount when the 14th person sends ether then we set the winner to the message sender
if (balance == targetAmount) {
winner = msg.sender;
}
}
function claimReward() public {
require(msg.sender == winner, "Not winner");
//this will send all the ether in this contract to the winner
(bool sent, ) = msg.sender.call{value: address(this).balance}("");
require(sent, "Failed to send Ether");
}
function getBalance() public view returns (uint256) {
return address(this).balance;
}
}