How To Create a Daily Game Reward System in Solidity

Posted By : Pankaj

May 28, 2024

Creating a daily game reward system with Solidity helps increase user engagement by giving continuous rewards to returning players. This smart contract development system will allow gamers to register, save time, and have their benefits increased every 24 hours, which they can collect once a day. In this blog article, we'll walk you through the process of developing such a system, with a focus on the key functionality and smart contract structure. 

 

Check Out | How to Create a Simple Supply Chain Smart Contract

 

Prerequisites 

Before we go into the code, make sure you understand Solidity, Ethereum smart contracts, and how to use development tools like Remix IDE or Truffle. 

Step 1: Create the Smart Contract 

First, we'll establish the structure of our smart contract. We'll start by declaring the contract and importing the required libraries.

 // SPDX-License-Identifier: MIT pragma solidity ^0.8.0; contract DailyReward 
{
	Step 1: Create the Smart Contract 
	struct Player { uint256 lastClaimedTime; uint256 reward; } mapping(address => Player) public players; 
	uint256 public baseReward; uint256 public rewardIncrement; 
	constructor(uint256 _baseReward, uint256 _rewardIncrement) { 
		baseReward = _baseReward; rewardIncrement = _rewardIncrement; 
	} 
} 

Step 2: Registering Players Next, we need a function to register players. This function will initialize their last claimed time to the current time and set their reward to the base reward.

function register() public { 
	require(players[msg.sender].lastClaimedTime == 0, "Player already registered"); 
	players[msg.sender] = Player({ lastClaimedTime: block.timestamp, reward: baseReward }); 
} 

Step 3: Calculating Rewards To calculate rewards, we'll create a function that checks how many 24-hour periods have passed since the last claim and increase the reward accordingly.

function calculateReward(address player) internal view returns (uint256) { 
	Player storage p = players[player]; require(p.lastClaimedTime != 0, "Player not registered"); 
	uint256 timeElapsed = block.timestamp - p.lastClaimedTime; 
	uint256 daysElapsed = timeElapsed / 1 days; return p.reward + (daysElapsed rewardIncrement); 
} 

Step 4: Claiming Rewards The claim function allows players to claim their rewards. It updates the last claimed time and resets the reward for the next period.

function claimReward() public { 
	Player storage p = players[msg.sender]; 
	require(p.lastClaimedTime != 0, "Player not registered"); 
	uint256 reward = calculateReward(msg.sender); // Reset the player's reward and update the last claimed time 
	p.reward = baseReward; p.lastClaimedTime = block.timestamp; // Transfer the reward (assuming the reward is in Ether) 
	payable(msg.sender).transfer(reward); 
} 

Step 5: Funding the Contract For players to claim their rewards, the contract needs to have enough funds. We'll add a function to allow the contract owner to deposit funds.

function deposit() public payable { 
	// Allows the owner to deposit Ether into the contract 
} 
function getContractBalance() public view returns (uint256) { 
	return address(this).balance; 
} 

Step 6: Putting It All Together Here's the complete contract with all the functions combined.

// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; 
contract DailyReward 
{ 
struct Player { uint256 lastClaimedTime; uint256 reward; } mapping(address => Player) public players; 
uint256 public baseReward; 
uint256 public rewardIncrement; 
constructor(uint256 _baseReward, uint256 _rewardIncrement) { 
	baseReward = _baseReward; rewardIncrement = _rewardIncrement; 
} 
function register() public { 
	require(players[msg.sender].lastClaimedTime == 0, "Player already registered"); 
	players[msg.sender] = Player({ lastClaimedTime: block.timestamp, reward: baseReward }); 
} 
function calculateReward(address player) internal view returns (uint256) { 
	Player storage p = players[player]; 
	require(p.lastClaimedTime != 0, "Player not registered"); 
	uint256 timeElapsed = block.timestamp - p.lastClaimedTime; uint256 daysElapsed = timeElapsed / 1 days; 
	return p.reward + (daysElapsed rewardIncrement); 
} 
function claimReward() public { 
	Player storage p = players[msg.sender]; 
	require(p.lastClaimedTime != 0, "Player not registered"); 
	uint256 reward = calculateReward(msg.sender); // Reset the player's reward and update the last claimed time 
	p.reward = baseReward; 
	p.lastClaimedTime = block.timestamp; // Transfer the reward (assuming the reward is in Ether) 
	payable(msg.sender).transfer(reward); 
} 
function deposit() public payable { 
	// Allows the owner to deposit Ether into the contract 
}
function getContractBalance() public view returns (uint256) { 
	return address(this).balance; } 
} 

 

You may also read | How to Deploy a Smart Contract using Foundry 

 

Conclusion 

Follow these instructions to construct a daily game reward system in Solidity that increases player retention and engagement. This smart contract framework serves as a solid platform for future additions, such as more complex reward logic, integration with front-end applications, and the addition of security mechanisms to avoid misuse. Contact our blockchain developers today for much such insights.

Leave a

Comment

Name is required

Invalid Name

Comment is required

Recaptcha is required.

blog-detail

July 16, 2024 at 01:52 pm

Your comment is awaiting moderation.

By using this site, you allow our use of cookies. For more information on the cookies we use and how to delete or block them, please read our cookie notice.

Chat with Us
Contact Us

Oodles | Blockchain Development Company

Name is required

Please enter a valid Name

Please enter a valid Phone Number

Please remove URL from text