Skip to content

Cryption-Digital-Services/ethereum-lottery-smart-contract

Repository files navigation

Ethereum Lottery Smart Contract

A Ethereum Blockchain Smart Contract for conducting decentralized lottery based on ERC20 tokens.

Description -

The Smart Contract allows users to enter the lottery by paying in ERC20 tokens and then generates the winners of the lottery by RNG provided by Chainlink Oracle's VRF Smart Contract. The smart contract is very flexible and allows the Admin to set different lottery configurations and also allows the Admin to reset the Lottery. So the Admin can set the number of winners, the number of allowed players, the ERC20 token to be used for the lottery and others. Players can enter the lottery by providing the allowance to the Smart Contract Address for the minimum funds required for the lottery. Once the maxium allowed number of players enter the lottery, the Lottery moves on towards calculating the winners. The winners are calculated based on the random numbers generated by the Chainlink Oracle's VRF Smart Contract. It provides true randomness which cannot be influenced and fulfills the purpose of Decentralized Lottery. Based on the random numbers, the winning amount is transferred from the Contract to the players and the Admin Fee is transferred to the Admin. After the amount has been transferred, the lottery moves to a closed state and the Admin can reset the lottery configuration to start another lottery.

Prerequisites and Notes -

  • On contract deployment, we set the value of adminAddress to be the owner of the contract. We also initialize the required configuration and fees for VRF Smart Contract required for RNG. The adminAdress value is immutable along with the initial configuration of VRF Smart Contract. They can only be set once during construction. Thus only the adminAddress will be able to set the lottery configuration, start and reset the lottery.
  • The contract uses the VRF Smart Contract based on Chainlink Oracle for RNG and thus it requires funds for the same. Thus the contract must always have the required funds calculated in LINK tokens for RNG.
  • The contract code has been written for the Kovan Test Network and necessary values for VRF Contract and LINK Token Address can be updated for different networks.

All the necessary information about all the functions, required conditions and the events has been documented in the Smart Contract code and can be referenced here - here

Usage -

Starting a new Lottery

The smart contract function setLotteryRules is used to start the lottery. Only the Admin can start the lottery and function accepts the required configuration for the lottery. The lottery configuration is based on the following parameters -

  • numOfWinners - Number of winners that the lottery smart contract should generate.
  • playersLimit - Number of allowed players for the lottery.
  • registrationAmount - The minimum amount to enter the lottery. This includes decimals since the contract is based on ERC20
  • adminFeePercentage - The percentage of winnings that will be transferred to the Admin.
  • lotteryTokenAddress - The contract address of the ERC20 token that the lottery will be based on.
  • randomSeed - Any random uninfluenced number value that the Admin can add for generating the RNG for Vrf Smart Contract

An example of lottery config can be -

{
         numOfWinners = 2
         playersLimit = 6
         registrationAmount = 10000000000000000000 (Signifying value of 10)
         adminFeePercentage = 5
         lotteryTokenAddress = 0xcaa6D2579617eF2d861927Ca424Ff5F7893e223c (Sample ERC20 Token Address)
         randomSeed = 123333332 
}

Notes

  • For avoiding issues with extra gas fees and failing execution, the number of winners should be less than half the number of players allowed for the lottery. This has been set as an condition in the Smart Contract Code.
  • The value of lotteryTokenAddress should be carefully checked and entered for using the correct ERC20 token for lottery.

Entering the Lottery and Random Number Generation

The smart contract function enterLottery is used to enter the lottery. Players can call this function to enter the lottery. Only prerequisite is that the player has set the necessary allowance to the Lottery Smart Contract. If this is done, player enters the lottery and the registration amount of ERC20 tokens is transferred from the player to the contract. The contract also supports multiple entries by the same player. When the last player enters i.e the number of players entered in the lottey contract equals the allowed number of players, no more player can enter the lottery and the lottery moves to the next stage by calling an internal function to generate random number for calculating the winners. The internal function of the Smart Contract calls Chainlink Oracle to generate the random number.

Settle Lottery and Transfer Winnings

The smart contract function settleLottery is used to settle the lottery. Players can call this function to settle the lottery and this function can be called only once and only when the random number is generated by Chainlink. An event is fired RandomNumberGenerated by the smart contract indicating that the random number is generated and the players or the admin can then call this function to settle the lottery. The function calculates the winners and transfer the winnings to the winners and the admin.

Reset Lottery

The smart contract function resetLottery is used to reset the lottery. Only the Admin can reset the lottery and the function clears the existing state variable values and the lottery can be initialized again.

License

MIT

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages