This repository contains a decentralized implementation of the classic Guess Two‑Thirds of the Average game, running on the Ethereum blockchain.
Players join by sending any amount of ETH. When the round ends, the player whose contribution is closest to 2 / 3 of the average wins the entire prize pool (minus a 1 % owner fee).
The stop‑condition is pseudo‑random and parametrised on deployment, so the game designer can decide how quickly a round should finish (e.g. 2–3 players, 5–10 players, etc.).
- Join by sending any positive amount of ETH.
- Each address may participate only once per round.
- When the round ends:
- Compute the average of all submitted amounts.
- Take 2 / 3 of that average as the target.
- The player whose amount is closest to the target wins.
- If there is a tie, the earliest participant wins.
- At deployment you choose
minX
andmaxX
(both ≥ 1,minX ≤ maxX
). - For every new participant count
n
, the contract draws
x ∈ [minX, maxX]
using on‑chain randomness (block.prevrandao
& block hash). - The game cannot end before
x
players have joined. - After that, another pseudo‑random draw decides whether the current
n
is the final count. - With
minX = 2, maxX = 3
(recommended for demos), a round typically ends within 6–9 players.
Main file: contracts/GuessTwoThirdsGame.sol
Features:
- Player registration, payment handling, and “one‑address‑one‑entry” guard.
- Constructor
GuessTwoThirdsGame(uint256 minX, uint256 maxX)
sets the early‑stop range. - Pseudo‑random end logic based on
block.prevrandao
andblockhash
. - Automatic prize/fee distribution (1 % fee to
owner
). - Written in Solidity ^0.8.19 – uses OpenZeppelin’s
ReentrancyGuard
.
This project uses [Hardhat] and [Ethers.js].
Run the full suite with:
npx hardhat test
Tests check:
- Single‑player participation
- Rejection of duplicate participation
- Round termination and winner determination under the new, quicker stop‑condition
Deploy with the Hardhat script, passing minX
and maxX
on the command line:
npx hardhat run --network <network-name> scripts/deploy.js <minX> <maxX>
Example (2–3 player threshold on Sepolia):
npx hardhat run --network sepolia scripts/deploy.js 2 3
guess-two-thirds-game/
│
├── contracts/
│ └── GuessTwoThirdsGame.sol # Main game contract
│
├── test/
│ └── GuessTwoThirdsGame.js # Mocha/Chai test suite
│
├── scripts/
│ └── deploy.js # Deployment script (takes minX, maxX)
│
├── hardhat.config.js # Hardhat configuration
└── package.json # Dependencies and scripts
npm install --save-dev hardhat @nomicfoundation/hardhat-toolbox
npm install @openzeppelin/contracts
The contract brings the classic experimental‑economics game “Guess 2 / 3 of the Average” to Web3, ensuring fairness through on‑chain pseudo‑randomness and public verifiability.
Distributed under the MIT License.