How to: Waves dApps — Prediction Markets example with RIDE -language. Part 1

Couple of days ago, Waves Labs announced new grants for Waves dApp developers.

In this article we’ll learn how to create the simple Prediction Market dApp (like Augur or Gnosis) to create new markets, trade shares and win if the prediction will come true.

Original: https://medium.com/@ConsenSys/why-how-decentralized-prediction-markets-will-change-just-about-everything-15ff02c98f7c

What are Prediction Markets?

It’s based on a binary event where something either will or won’t happen. In the finance world, participants trade with contracts where the payoff will vary depending on the outcome of a future event. Prediction markets make the result of this future event tradable.
Essentially it’s placing a bet on the probability of specific results in certain situations, such as elections, sales of a company, price fluctuations of commodities, even changes in the weather.
Example of User Interface (UI) for Prediction Markets tools (https://www.augur.net/)

Prediction Markets dApp demo case

Participants:

Alice — Prediction Markets dApp creator

Bob — Trader

Cooper — Trader

Elon — creator of market for an event: “The first human colony on Mars at 1 Jan 2020 by Elon Musk

Oracle — it’s the trusted account who is responsible for disclosing the final result (i.e.: happened or not happened within the disclosed time interval).

Main actions of simple Prediction Markets dApp

Actions:

[DEPOSIT & WITHDRAW]

Everyone is able to use dApp as a simple multi-user wallet. We have two deposits: 300 WAVES from Bob and 100 WAVES from Cooper.

[CREATE MARKET]

Everyone is able to create a new Prediction Market. For example: Elon has created a market for an exciting event: The first human colony on Mars at 1 Jan 2020 by Elon Musk

prediction market creation with “ORACLE” as a reporter

[TRADES & ORDERS]

Bob and Cooper now can trade for “Elon’s Market”-shares. One share represents the future contract equal to 0.01 WAVES if event will come true and 0 WAVES otherwise. The order-book price for 1-share is between 1 and 100 units, this number represents estimated probability for an event. We’ll describe only two types of trading orders for simplification: “Limit Sell order” and “Market Buy order”.

Note: to be able to sell shares you have to freeze some amount of WAVES as a warranty coverage for future trades
Note: to be able to buy shares somebody should provide “limit sell” orders first

[REPORT RESULTS]

When the right moment comes someone must report the final result. The creator of the market has identified the responsible oracle at the stage of creating the market.

Cooper was the most active shares buyer, so he had won when Oracle said YES for event of the Elon’s market.
Bob was the most active shares seller, so he had won when Oracle said NO for event of the Elon’s market.

We have described a comprehensive but slightly simplified example of a prediction market.
To create a full functional order-book we can use the same logic to add “Limit Buy” and “Market Sell” types of orders. Unfortunately RIDE for dApps language is based on high level of security principles, so it’s not possible to work with orders in the same way as it works within centralised matcher in exchanges. I.E.: all orders matching checks (prices, time priority etc) or partial executions should be implemented by client side application.
Also, in the Part 2. we’ll consider how to add the TCR-based dispute resolution module for our Prediction Markets dApp.


RIDE for dApps implementation

In recent article we have described how to create dApps on Waves platform. So, there is no new information about RIDE smart contracts development and integration.

We used complex logic for key-value data storage and data transactions to implement Prediction Markets dApp

To create a new market we have to specify title, reporter and interval for a predicted event
Note: to be able to sell shares you have to freeze some amount of WAVES as a warranty coverage for future trades
Note: to be able to buy shares somebody must provide “limit sell” orders first

One important moment is that our market should have a “default scenario” if the Oracle at that time has not yet announced the result. We agreed that it should be “NO”.

The most active shares seller had won when Oracle said NO for an event of the prediction market.

dApp Web Application

To allow non-technical users to work with Prediction Markets dApp we need to integrate it into some simple html+js web page. In recent article we have described that.

https://waves-prediction-market-demo.herokuapp.com/

To work with data from dApp data storage we can use accountData and currentHeight from nodeInteraction module.

import { invokeScript, broadcast, nodeInteraction } from '@waves/waves-transactions'
col. 1: Blockchain height, col. 2: Delta — negative value means that market has expired, col. 3: Final height for the market, col. 4: Market Id, col. 5: Market Title

The source code is open for everyone on GitHub.

Be careful with the seed phrase. Waves-Keeper will support RIDE for dApps (invokeScript) shortly.

Enjoy the dApp development with Waves!