Basic Ethereum Lottery Smart Contract
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.


Basic Ethereum Lottery Smart Contract using for scheduled execution

Demonstration for

Disclaimer: This solidity contract is imperfect. I do not recommend uploading this contract and running your own lottery. This is for demonstration purposes only! This sample lottery contract should not be uploaded. I cooked this up really quick for educational purposes. You'll find that it costs a lot of gas to run, the random number picker isn't perfect, and some features are missing. (Pseudo)Random number generators in smart contracts are not a simple task. Deterministic generation of a pseudorandom number on a public blockchain while preventing it from being manipulated or guessed is obviously difficult to secure. Current block hashes are unknown (the miner hasn't mined the block yet). Block hashes are only known for block heights: n-256 to n-1 where n is the current block height. The rest are 0. In this system, I stored the contract construction block height for its hash to be determined at a later transaction (state change). Each transaction (state change) stores that transaction's block height and keccak256 hashes the concatenation of the previously stored block height's hash with the latest concatenation. The final transaction (drawWinner) concatenates its n-1 block hash and performs a final keccak256 hash before calculating its modulo to the number of entries. It's a quick/naive approach to a complicated problem. And you probably should not actually run a lottery… it’s probably illegal where you live