DEV Community

Cover image for Recurring Lottery game SmartContract deployed on XinFin XDC Network
MahaLakshmi Perumal
MahaLakshmi Perumal

Posted on

1

Recurring Lottery game SmartContract deployed on XinFin XDC Network

Deployed a recurring lottery smartcontract on XinFin XDC Network, and it is very easy and cheap to deploy on XinFin Network. The stable network where the new generation Developers should deploy their #DAPPS and #SmartContracts.

Check the Solidity Contract for Lottery:

pragma solidity ^0.4.18;
contract RecurringLottery {
struct Round {
uint endBlock;
uint drawBlock;
Entry[] entries;
uint totalQuantity;
address winner;
}
struct Entry {
address buyer;
uint quantity;
}
uint constant public TICKET_PRICE = 1e15;
mapping(uint => Round) public rounds;
uint public round;
uint public duration;
mapping (address => uint) public balances;
// duration is in blocks. 1 day = ~5500 blocks
function RecurringLottery (uint _duration) public {
duration = _duration;
round = 1;
rounds[round].endBlock = block.number + duration;
rounds[round].drawBlock = block.number + duration + 5;
}
function buy () payable public {
require(msg.value % TICKET_PRICE == 0);
if (block.number > rounds[round].endBlock) {
round += 1;
rounds[round].endBlock = block.number + duration;
rounds[round].drawBlock = block.number + duration + 5;
}
uint quantity = msg.value / TICKET_PRICE;
Entry memory entry = Entry(msg.sender, quantity);
rounds[round].entries.push(entry);
rounds[round].totalQuantity += quantity;
}
function drawWinner (uint roundNumber) public {
Round storage drawing = rounds[roundNumber];
require(drawing.winner == address(0));
require(block.number > drawing.drawBlock);
require(drawing.entries.length > 0);
// pick winner
bytes32 rand = keccak256(
block.blockhash(drawing.drawBlock)
);
uint counter = uint(rand) % drawing.totalQuantity;
for (uint i=0; i < drawing.entries.length; i++) {
uint quantity = drawing.entries[i].quantity;
if (quantity > counter) {
drawing.winner = drawing.entries[i].buyer;
break;
}
else
counter -= quantity;
}
balances[drawing.winner] += TICKET_PRICE * drawing.totalQuantity;
}
function withdraw () public {
uint amount = balances[msg.sender];
balances[msg.sender] = 0;
msg.sender.transfer(amount);
}
function deleteRound (uint _round) public {
require(block.number > rounds[_round].drawBlock + 100);
require(rounds[_round].winner != address(0));
delete rounds[_round];
}
function () payable public {
buy();
}
}

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay