Part of the complete guide: Crypto lottery: the complete guide
The vending machine analogy
A vending machine is a simple contract: put in the right money, press a button and the machine delivers the item with no cashier. A smart contract does the same with digital assets. For a lottery, the contract can take ticket money, hold it, pick a winner when a condition is met and send the prize, with no person pressing a button. The rules are in the code, and everyone can read them.
How it works
- 1A developer writes the program, commonly in a language such as Solidity for Ethereum-compatible networks.
- 2It is compiled and deployed to the blockchain, which gives it an address.
- 3Anyone can call its functions by sending a transaction, and pay a fee.
- 4Every node runs the code and agrees on the result, which is recorded on the blockchain.
- 5The contract's state, such as balances and tickets sold, is public and permanent.
What contracts can and cannot do
- They can hold and send tokens, enforce rules and record who did what.
- They cannot access the outside world, so data such as random numbers and prices must be delivered by oracles.
- They cannot undo a mistake. A bug in a deployed contract is hard to fix, which is why some contracts include upgrade mechanisms, with their own risks.
- They cannot make a bad rule fair. If the rules are unfair, the code just enforces them.
Famous failures
Smart-contract history includes expensive lessons. In 2016, a bug in an early investment contract known as The DAO allowed an attacker to drain millions of dollars of Ether, which led to a controversial change to the Ethereum blockchain itself. Since then, many contracts have lost funds to re-entrancy bugs, access-control mistakes, bad price feeds and manipulated randomness. These are why audits, bug bounties and simple designs are valued.
What to ask about a lottery contract
- 1Is the source code verified on a block explorer, so you can read what is deployed?
- 2Has it been audited, by whom, and were the findings fixed?
- 3Who holds admin keys? Can they upgrade the code, pause it or withdraw the pool?
- 4How does it get randomness, and what happens if randomness fails?
- 5Has it held real money for a meaningful time without incident?
Our guides on smart-contract lottery risks and on-chain jackpot lotteries cover these in detail.
A tiny lottery contract in plain language
Here is what a very simple lottery contract does, described in words rather than code. It has a ticket price and an end time. A function lets anyone send the ticket price to buy a ticket, and it records their address. After the end time, another function asks the randomness source for a number, divides it by the number of tickets and sends the whole pool to the ticket at that position. That is it. Real contracts add many more safeguards, such as stopping sales before the draw, handling refunds if randomness fails and taking a fee, and each added piece is a place a bug can hide. This is why simple, well-audited contracts are valued.
Reading a contract page on a block explorer
- 1Open the contract address on the block explorer for its network.
- 2Look for a green tick or "Contract source code verified".
- 3Open the "Read contract" tab to see public values such as the ticket price and pool balance.
- 4Check the "Write contract" tab for functions only an owner can call.
- 5Look at recent transactions to see how the contract is being used.
Ready?
A weekly draw you can check yourself.
$5 tickets, a public random value, and every result published with the data to recompute it.
Frequently asked questions
Is a smart contract a legal contract?
Not by itself. It is code. Whether it has legal effect depends on the law and context.
Can a smart contract be changed?
Not once deployed, unless it was built with an upgrade mechanism, which requires trusting whoever controls it.
Are smart contracts safe?
They can be, with careful design and audits. Many have been exploited. Safety is never guaranteed.
What is a smart contract used for in a lottery?
Taking ticket payments, holding the prize pool, requesting randomness and paying winners.
Who can see a smart contract?
Anyone, if it is verified. Its code and balance are public on a block explorer.
Can I cancel a smart contract transaction?
No, once confirmed. Some wallets let you replace a pending transaction before it confirms.
Are smart contracts only for crypto lotteries?
No. They run lending, exchanges, games, collectibles and much more.