Layer LogoWAVS Docs
Build a service

7. Prediction market

Now that you've built a simple oracle service, take a look at the WAVS Demo Repo to see a similar component used in action to resolve a prediction market.

This page will give an overview of the prediction market demo, how it works, and how the oracle component is used to resolve markets.

Prediction market demo repo: https://github.com/Lay3rLabs/wavs-demos/tree/main

What is a prediction market?

A prediction market is a marketplace that gathers insights about the future by rewarding participants for making accurate predictions based on available information. For example, a prediction market could be created for whether it will snow in Oslo on November 5th. Users can create positions by depositing money based on two outcomes: yes or no. After the event transpires, an oracle service can be used to bring in the weather outcome, resolving the market and rewarding those who predicted correctly.

How does it work?

Market Solidity Contracts

These contracts handle the creation of markets and conditional tokens.

conditional-tokens-contracts - these contracts are forked from Gnosis and updated to a recent Solidity version, and they are the core protocol that creates a conditional share in a future outcome.

conditional-tokens-market-makers - these contracts are forked from Gnosis and updated to a recent Solidity version, and they are the market makers that create a market based on the conditional shares above.

PredictionMarketFactory.sol - this contract sets up all the contracts required for a functioning prediction market using the forked contracts above, and it has the power to resolve the market once the outcome is determined by the oracle AVS.

Extending the trigger contract

In this demo, the oracle that resolves the market is triggered by the PredictionMarketOracleController.sol contract.

This contract contains modifications to the WavsTrigger.sol contract from the WAVS Foundry Template repo. Similar to the simple trigger contract, it passes data to the oracle AVS via the NewTrigger event.

struct TriggerInputData {
address lmsrMarketMaker;
address conditionalTokens;
bool result;
}

In addition, it also contains logic to handle responses from the oracle service.

struct AvsOutputData {
address lmsrMarketMaker;
address conditionalTokens;
bool result;
}

It extends the contract by storing trigger metadata, validating signed AVS outputs, and interacting with the external contracts mentioned above (PredictionMarketFactory, MarketMaker, and ConditionalTokens).

It also contains logic to enforce a payment when a trigger is added:

function addTrigger(
TriggerInputData calldata triggerData
) external payable returns (ITypes.TriggerId triggerId) {
require(msg.value == 0.1 ether, "Payment must be exactly 0.1 ETH");

Take a look at the PredictionMarketOracleController.sol file to get an idea of how the contract is structured.

This contract is responsible for interacting with the oracle service, triggering WAVS to run the oracle, waiting for the oracle's response, and telling the market factory to resolve the market.

Oracle WASI Component

Similar to the oracle you created in the tutorial, the prediction market uses a simple oracle service to resolve a market by bringing off-chain data on-chain.

This WASI component runs in WAVS and fetches the prediction market's resolution when necessary. A wallet executes the PredictionMarketOracleController.sol contract's addTrigger function, which triggers WAVS to run this oracle by emitting an event. Then, WAVS commits the response from this oracle component with a signature back to the contract on-chain, and the market is resolved.

Performing this market resolution via WAVS means prediction markets can exist in a fully decentralized manner. Because money is on the line, entrusting any party to honestly resolve the market is a critical security decision—WAVS enables distributing this trust over multiple independent parties, taking advantage of the verifiability and security of the existing WAVS infrastructure instead of relying on any centralized individual.

Try it out

You can run the prediction market demo locally by following the steps from the README.

Edit on GitHub

On this page