Skip to content

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

 
 

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. The Lottery Contract is also based on an internal LOT Erc20 token. Whenever a participant enters a lottery, he is issued an equal number of LOT tokens as the registration amount. This amount is minted by the contract and is issued to every participant. When the lottery is settled, the lottery contract burns these amount of LOT tokens for the winners and the lottery winnings are transferred to the winners. The other participants of the lottery still hold these LOT tokens and can use it for other applications/trade and transfer.

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. The function returns the participant's index. This is similar to unique registration id and can be used by the particpants to check if they are the winners when the lottery is settled. 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. The participant is also issued an equal amount of LOT tokens once he registers for the lottery. This LOT tokens are fundamental to the lottery contract and are used internally. The winners will need to burn their LOT tokens to claim the lottery winnings. The other participants of the lottery can keep hold of these tokens and use for other applications. 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 emits an event that contains the winning tickets or indexes. The contract transfers the admin fees to the admin and the lottery enters a closed state.

Collect Rewards

The smart contract function collectRewards is used by the winners to claim the winnings of the lottery. This function can only be called when the lottery winnings are settled and the lottery enters a closed state. The winners of the lottery can call this function to transfer their winnings from the lottery contract to their own address. The winners will need to burn their LOT tokens to claim the lottery rewards. This is executed by the lottery contract itself.

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

No packages published

Languages

  • Solidity 97.5%
  • JavaScript 2.5%