🧩 Project Description
The SpinGame smart contract is a beginner-friendly decentralized game that demonstrates how to use Solidity to create a luck-based game with pseudo-random outcomes.
It allows users to:
Spin for rewards.
Claim their winnings.
Watch transactions and rewards on-chain for full transparency.
This project is perfect for developers who are learning Solidity, randomness handling, and basic smart contract design patterns.
my github link: https://github.com/Anusua-2005/game_spin
The mission of the SpinGame project is to provide a simple, educational, and fully transparent decentralized game that helps beginners learn the fundamentals of Solidity smart contract development. By creating a luck-based spinning game with pseudo-random outcomes, the project aims to make it easier for new developers to understand how blockchain interactions, randomness, payments, and reward systems work on-chain.
SpinGame demonstrates essential concepts including:
Accepting and validating user payments
Generating pseudo-random values in Solidity
Storing player-specific data securely
Emitting events for on-chain transparency
Allowing users to claim rewards in a safe and verifiable way
Through hands-on experience with this contract, new blockchain developers gain confidence and practical skills in writing, testing, and interacting with smart contracts. The mission is to make decentralized app development more approachable, engaging, and beginner-friendly.
Developers who are new to Solidity often struggle to understand how decentralized games work, especially those that involve randomness, player rewards, and transparent on-chain interactions. Creating a simple luck-based game that demonstrates these concepts can help beginners learn essential smart contract patterns.
The challenge is to design and implement a decentralized SpinGame smart contract where:
Players can spin a virtual wheel by paying a fixed spin price.
Each spin generates a pseudo-random reward.
The contract should store rewards securely for each player.
Players must be able to claim their accumulated winnings.
All spins, rewards, and claims should be fully transparent and viewable on-chain.
The entire design should remain simple, beginner-friendly, and illustrate proper smart contract development practices.
This problem aims to give new Solidity developers hands-on experience with:
Handling randomness securely,
Managing payments,
Storing player-specific data,
Emitting and tracking events,
Designing clean, readable, and safe smart contracts.
To solve the problem of teaching beginners how a decentralized luck-based game works, we implement a Solidity smart contract called SpinGame. This solution demonstrates randomness handling, payments, events, and reward claiming in a simple and practical way.
✔️ Smart Contract: SpinGame
How the Solution Works
Owner Setup
The contract sets the deployer as the owner and initializes a fixed spin price (0.01 ETH).
Spin Function (Core Logic)
A player sends exactly the spin price to play.
The contract generates a pseudo-random reward by hashing:
keccak256(abi.encodePacked(block.timestamp, msg.sender, block.prevrandao))
The reward is stored in a mapping under the player’s address.
An event Spun is emitted for on-chain transparency.
Claim Rewards Function
A player can withdraw their entire accumulated reward balance.
After payout, their reward balance is reset to zero.
Event logging ensures transparency.
View Functions
Players can check their rewards anytime.
Everything is stored on-chain for full transparency.
Security & Best Practices
Requires exact payment.
Updates state before transferring ETH (checks-effects-interactions pattern).
Uses events for tracking.
✔️ Complete Solidity Solution
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract SpinGame {
address public owner;
uint256 public spinPrice = 0.01 ether;
mapping(address => uint256) public rewards;
event Spun(address indexed player, uint256 reward);
event RewardClaimed(address indexed player, uint256 amount);
constructor() {
owner = msg.sender;
}
// Spin the wheel
function spin() public payable {
require(msg.value == spinPrice, "Send exact spin price");
// Generate pseudo-random reward
uint256 random = uint256(
keccak256(
abi.encodePacked(
block.timestamp,
msg.sender,
block.prevrandao
)
)
);
uint256 reward = (random % 10 + 1) * 0.001 ether; // Reward: 0.001–0.01 ETH
rewards[msg.sender] += reward;
emit Spun(msg.sender, reward);
}
// Claim accumulated rewards
function claimRewards() public {
uint256 amount = rewards[msg.sender];
require(amount > 0, "No rewards to claim");
rewards[msg.sender] = 0;
(bool success, ) = msg.sender.call{value: amount}("");
require(success, "Transfer failed");
emit RewardClaimed(msg.sender, amount);
}
// Check player's rewards
function checkRewards(address player) public view returns (uint256) {
return rewards[player];
}
}
✔️ Why This Is a Complete Solution
This contract shows beginners how to:
Use mappings
Handle ETH payments
Emit and listen to events
Generate pseudo-random numbers
Maintain transparency on-chain
Follow safe Solidity design patterns
It meets all project requirements:
✅ Spin for rewards
✅ Claim winnings
✅ On-chain transparency
✅ Beginner-friendly Solidity
✅ Demonstrates randomness
✅ Clean contract architecture