Deposit 14 ETH game in a Solidity smart contract

The contract below is a gambling game where the 14th person to send ETH to the contract wins all ETH in the contract. It demonstrates how to prevent a forceful send of ETH and why the order of operations in a function are important.

This is a simple example of the dangers of a smart contracts and why testing and auditing are important. All developers have bugs in their code and it is only a matter of time until they are found.

Experiment with the contract below and learn the basics of a simple contract. Deploy the contract below to the Ethereum test network using Remix. Try to add another function.

pragma solidity ^0.6.10;

//A game where the 14th person that deposit ether wins all the ether in the contract
//the winner can claim the 14 ether

contract EtherGame {
    uint public targetAmount = 14 ether;
    address public winner;
    //need to create a balance state variable to prevent forcefully sending ether
    uint public balance;
    //to play you need to call the deposit function and send 1 ether
    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;
    //the winner can use the claim function to claim their reward
    function claimReward() public {
        require(msg.sender == winner, "Not Winner");
        //this will send all the ether in this contract to the winner
        (bool sent, ) ={value: address(this).balance}("");
        require(sent, "Failed to send Ether");
    function getBalance() public view returns (uint) {
        return address(this).balance;

Try it in Remix

This code is for learning and entertainment purposes only. The code has not been audited and use at your own risk. Remember smart contracts are experimental and could contain bugs.

Click here for more information about how to use the Ethereum test network and how to obtain test ETH.

Next Review – Create a delegate call smart contract in Solidity

Leave a Reply