Everything I Know About Prediction Markets + How to Build One
My devconnect 2025 presentation as a blog post
Hey everyone — unusual Tuesday newsletter today.
I’m preparing for a Prediction Markets Workshop tomorrow at 10am, and I thought it would be useful (and fun) to write down what I’m learning in the form of a newsletter.
If you enjoy sports betting or just guessing what will happen in the future, you probably like prediction markets.
In this post I’ll cover:
What prediction markets are
Why you should care
How an onchain prediction market works (tokens, AMM, oracle)
And how to build your own prediction market from the Speedrun Ethereum challenge
All in simple English.
Quick disclaimer: This post is not a full step-by-step coding tutorial. I’m not going to recreate the whole Speedrun Ethereum Prediction Markets contracts here. My goal is to explain the main ideas + show small pieces of code so you understand how things fit together. If you want to actually build and ship the prediction market, you should go through the full challenge. My full code is here.
People Love Betting <3
People bet on everything:
Who will win a football match
Who will win an election
Who will win a TV show (and if you want to predict who gets eliminated in reality tv this Reddit entry is for you)
The idea is simple: You put money on what you think will happen. If you’re right, you win. If you’re wrong, you lose.
Prediction markets take the same idea and apply it to almost any future event.
Ok, but really what Are Prediction Markets?
A prediction market is a place where people buy and sell shares based on what they think will happen in the future.
There is a question about the future.
Example: “Will the green car win the race?”
There are outcomes. (Let’s think binary to make it simple, here and in the rest of the post)
YES
NO
You can buy a share of YES or a share of NO.
Each share has a price. That price acts like a probability.
As people trade, the price moves, and the price shows what the crowd believes about the chance of the event.
Why Should You Care?
A few reasons prediction markets are interesting (mostly to me, possibly to you):
1. Good at forecasting
When money is on the line, people tend to be more honest.
If you think the market is wrong, you can buy the “cheap” side.
If many smart people do this, the price moves toward a more accurate probability.
Economists like prediction markets because they often aggregate information better than simple surveys or polls.
2. Teach you important building blocks
Even a simple prediction market touches a lot of important concepts:
Markets: how prices move when people buy and sell
Incentives: why people provide liquidity or trade
In our case — Smart contracts: ERC20 tokens, pricing functions, oracles, etc. Building a prediction market is a great “mini app” for all these building blocks.
3. More: Popular, fun & concrete
“Will the green car win the race?” is much more fun to code than a random math example, well at least for me.
Also the fomo kicks in, seeing all the NYC mayor billboards in nyc.
How Does an Onchain Prediction Market Work?
Now let’s move from the general concept to the onchain version, I’m following the Speedrun Ethereum challenge. Everything is happening on Ethereum, inside smart contracts.
At a high level, there are 3 main parts that we need to build out:
Tokens that represent outcomes (YES and NO)
A pricing and trading mechanism (AMM)
An oracle to tell the contract who actually won
And there are 3 roles you need to make this work:
Market owner / Liquidity Provider
provides token to the contract
Oracle
gets the outcome of the event
User
buys/sells to participate in the market
Let’s break down each of these.
1. Outcome tokens: YES and NO as ERC20s
In this PredictionMarket, when you deploy the prediction market contract, it creates 2 ERC20 tokens:
A YES token
A NO token
Each token represents one outcome.
If YES wins, each YES token can be redeemed for a fixed amount of ETH
If YES loses, a YES token is worth 0 ETH.
Same for NO tokens if NO is the winning outcome.
So instead of a betting slip, you own a ERC20 token that lives onchain. You can trade it, send it, or redeem it once the result is known.
How does this look like on the contract
When the prediction market is deployed, it automatically creates the two tokens:
yesToken = new PredictionMarketToken(”Yes”, “Y”, msg.sender, initialTokenAmount);
noToken = new PredictionMarketToken(”No”, “N”, msg.sender, initialTokenAmount);The token contract is a simple ERC20—it just mints an initial supply and records who deployed the market:
contract PredictionMarketToken is ERC20 {
address public predictionMarket;
address public liquidityProvider;
constructor(string memory name, string memory symbol, address _lp, uint256 supply)
ERC20(name, symbol)
{
predictionMarket = msg.sender;
liquidityProvider = _lp;
_mint(msg.sender, supply);
}
}2. Liquidity: who puts up the ETH?
For the market to work, there must be tokens and ETH inside the contract so users can trade.
This is where the liquidity provider (LP) comes in:
The LP (market owner) deploys the contract and deposits ETH as collateral.
The contract mints YES and NO tokens and keeps them in a pool.
The LP can later add/remove liquidity.
The LP earns:
Trading revenue (fees / price spread)
BUT also takes risk (if many users bet correctly, the LP can lose some ETH).
Adding initial liquidity to the contract
In this contract, that money is the ETH put in by the market owner / LP.
When you deploy PredictionMarket, you must send ETH:
constructor(
address liquidityProvider,
...
) payable Ownable(liquidityProvider) {
if (msg.value == 0) {
revert PredictionMarket__MustProvideETHForInitialLiquidity();
}
ethCollateral = msg.value;
...
}liquidityProviderbecomes the owner of the marketmsg.valuebecomesethCollateral→ the pool of ETH that will be used later to pay out winning tokens
The market is “backed” by this ETH. The owner can add / remove ETH to the pool while the market is still open.
The market owner has one more role here that after the market is reported, they will call the settle function to end the market and pay-out the winners.
Reminder: For purpose of this blog post I am not posting the whole code — just the main ideas.
3. Pricing & trading mechanism
On centralized exchanges (and some prediction markets), you have an order book: one side places buy orders, the other side sells.
In this challenge, we don’t use an order book. We use an Automated Market Maker (AMM) — a formula that sets the price based on how many tokens are in the pool.
Here’s the idea:
When a lot of people buy YES tokens, the probability of YES goes up, so the price of YES goes up.
When people sell YES tokens, the probability and price go down.
Buy and sell functions
The contract has functions like:
function getBuyPriceInEth(Outcome _outcome, uint256 _tradingAmount)
public
view
returns (uint256)
{
return _calculatePriceInEth(_outcome, _tradingAmount, false);
}
function getSellPriceInEth(Outcome _outcome, uint256 _tradingAmount)
public
view
returns (uint256)
{
return _calculatePriceInEth(_outcome, _tradingAmount, true);
}
These use a linear model that:
Looks at how many tokens are “sold” vs. total
Computes a probability for that outcome
Uses that probability to calculate a price for the amount you want to buy / sell
No need to be a math expert here, you can think:
“More demand for YES → higher YES price → higher implied probability.”
And the nice part: Because it’s an AMM, you can always trade as long as there’s liquidity — you don’t need another person to be online at the same time as you.
4. The Oracle: who tells the contract the outcome?
Ethereum cannot see the real world. It doesn’t know who won the outcome of a prediction market (ie: who won the race).
Enter Oracles:
Blockchain oracles are entities that connect blockchains to external systems, enabling smart contracts to execute based on real-world inputs and outputs.
In simpler terms: something that tells the contract the true result.
For this prediction market, we’ll keep it simple:
The oracle is a trusted address
After the race, the oracle calls a function like
YESorNO.The contract stores the winning token and marks the market as reported.
From that moment on:
Prices stop changing
No more trading is allowed
You can only redeem the tokens from that point on
In real systems, oracles are more complex, but to explain the concept, a single trusted oracle is enough to understand the mechanics.
Defining the Oracle
address public immutable oracle; // trusted address
bool public isReported; // tracks if the market is already resolved
PredictionMarketToken public winningToken; // stores which token actually wonThe oracle calls report once the event is finished:
function report(Outcome _winningOutcome) external predictionNotReported {
if (msg.sender != oracle) {
revert PredictionMarket__OnlyOracleCanReport();
}
winningToken = _winningOutcome == Outcome.YES ? yesToken : i_noToken;
isReported = true;
emit MarketReported(msg.sender, _winningOutcome, address(winningToken));
}
What this does:
Only the oracle address can call
reportIt selects the winning token:
YESorNOIt flips
isReportedtotrueSome functions (like trading, adding/removing liquidity) only work before the result.
Others (like redeeming and final withdrawal) only work after the result
After report is called, the market is final, no more changing the outcome
5. Users: buy, sell, redeem
Once the market is created and funded, users can interact:
Buy tokens
Call
buyTokensWithETH(YES, amount)with some ETHThe contract checks the price using
getBuyPriceInEthYou receive YES tokens
Sell tokens
Call
sellTokensForEth(YES, amount)The contract calculates how much ETH you should get
Your YES tokens go back into the pool, you get ETH out
Redeem winning tokens
After the oracle reports the result, if you hold the winning token (YES or NO), you call
redeemWinningTokens(amount)The contract burns your tokens and sends you ETH
To closes the loop:
Liquidity provider funds → users trade → oracle reports → winners redeem → LP withdraws what’s left.
Wrap-Up
Prediction markets are like sports betting for ANY future event with a concrete outcome / time.
Prices act like probabilities: the higher the price, the higher the implied chance.
The Speedrun Ethereum Prediction Markets challenge makes you build this whole system yourself, step by step:
Set up the contract
Create the ERC20 tokens
Add / remove liquidity
Implement the pricing logic
Implement buy / sell functions
Implement the oracle
Let users redeem winning tokens
Finally, ship the app to testnet/mainnet!
🇦🇷 If you are in Buenos Aires, catch my presentation at tomorrow at 10am!








