Event-Based Prediction Market
Building a prediction market that uses the UMA Optimistic Oracle for settlement and event identification
In this section, we'll talk about the Event Based Prediction market contract, which you can find in the developer's quick-start repo. This tutorial will show how event-based OO data requests can be used in a binary prediction market.
You will find out how this smart contract works and how to test it and deploy it. Refer to the Optimistic Oracle V2 contract for additional information on the event-based price requests.
The Event-Based Prediction Market
This smart contract lets you set up prediction markets that are based on an event-based price request.
For example, you could ask a question like, "Will BTC be over $1M when the first Moon hotel opens?"
(Note: Although these are called "price" requests in the code, you can request any kind of data from the optimistic oracle. If it's helpful, you can mentally replace "price" with "data" when you see it in the code and documentation.)
In addition, an event-based price request cannot expire early and automatically returns the requester's reward in the event of a dispute. By participating in the contract, users are issued long and short position tokens, which, when held, provide exposure to the prediction market. This contract's logic is a streamlined version of the LongShortPair with an event-based pricing request.
Development environment and tests
Clone the dev-quickstart repo
To install dependencies, you will need to install the long-term support version of nodejs, currently nodejs v16, and yarn. You can then install dependencies by running yarn with no arguments:
Compiling your contracts
We will need to run the following command to compile the contracts and make the typescript interfaces so that they are easy to work with:
Contract design
Contract creation and initialization
The contract is created by setting up the prediction market with a series of parameters. With the parameters, you can choose what kind of collateral you want to use with _collateralToken
. With _pairName
, we can choose a name for the pair of long and short tokens. _customAncillaryData
is where we define the price request question. The addresses of the _finder
and _timerAddress
set the rest of the contracts addresses we interact with. For reference, here is the full list of UMA contract deployments.
Once the contract has been deployed, the owner can call initializeMarket()
after approving the proposerReward
amount to be paid to the wallet that resolves the price request. To keep things simple, proposerReward
is set to 10e18.
Also observe how priceIdentifier
is set to "YES_OR_NO_QUERY"
.
This function sets up the prediction market by getting the proposer reward and calling _requestOraclePrice
. This last function starts the price request in Optimistic Oracle V2 and sets up a number of options that are explained below.
_requestOraclePrice
is in charge of initializing the price request in the Optimistic Oracle V2 by performing the following actions:
Create the price request with the above-mentioned parameters.
Define the custom liveness period that a proposed oracle response must sit through before being accepted as truth.
Specify the bond that the proposer and disputer must post in order to resolve a request.
Set the type of request to Event Based.
Turn on the
priceSettled
andpriceDisputed
callbacks so that our contract can use OO callbacks to respond to these kinds of events
Long and Short tokens creation and redemption
Any user can now call the create
function with the tokensToCreate
parameter to mint the same number of short and long tokens. Having both tokens in the same proportion means being in a neutral position, as is the case when calling create
.
Holding only long tokens (by transferring short tokens to another wallet or adding liquidity to an AMM pair), gives exposure to the long position and vice versa.
At any time a token holder with both tokens in the same proportion can exchange them for collateral with `redeem`.
Any long-short token holder can settle tokens for collateral with settle
if the oracle has processed the price request.
The returned collateral amount is a function of longTokensToRedeem
, shortTokensToRedeem
, and settlementPrice
.
Price request lifecycle callbacks: priceSettled and priceDisputed
When the price request we set up above is settled in the Optimistic Oracle V2, the priceSettled
function of this contract is invoked.
This function calculates and stores settlementPrice as 0
, 0.5
, or 1
. This number is used in the settle
function to calculate the collateral to pay in exchange for long tokens and short tokens.
In the same way, this contract's priceDisputed
function is called when a price request is disputed. This function re-starts the same price request with the bond amount that was given back to the requester, which in our case is the EventBasedPredictionMarket
.
Tests
To execute the EventBasedPredictionMarket tests, run:
Deployment
Before deploying the contract check/edit the default arguments defined in the deployment script.
To deploy EventBasedPredictionMarket
in Görli network, run:
Optionally, you can verify the deployed contract on Etherscan:
Last updated