Do-It-Yourself Real-Time Prediction Market With Streamr

What is the most powerful prediction engine in the world? A crystal ball? A deep neural network? The human brain? The collective brainpower of mankind?

The collective wisdom of the crowd (including algorithms too) can produce unbeatable future estimates of asset values, sports outcomes, and presidential elections. Prediction markets attempt to capture this wisdom by creating a mechanism in which probabilities emerge as a result of collective betting. This is similar to how company valuations emerge on the stock market as the result of collective bidding. Both phenomena are based on the efficient-market hypothesis. For an entertaining exploration of the ideas, “The Wisdom of Crowds” by James Surowiecki is very much worth reading.

To realize the relationship between betting and prediction markets, consider a simple betting game with only two possible outcomes: “up” or “down”. Say, you witness a situation where many bets have already been placed, and 90% of the bets are on “up” and only 10% on “down”. Which would you consider the much more likely outcome?

In the blockchain space, projects such as Augur and Gnosis are building decentralized prediction markets based on Ethereum. Inspired by these, we wanted to build a real-time version which leverages our data platform and the visual editor. The demo is based on the streaming real-time ETH/USD price feed from GDAX, an Ethereum smart contract to hold bets and handle payouts, and a Streamr canvas which keeps track of the prediction rounds. It also acts as an oracle, calculating the correct outcome and reporting it to the smart contract.

In the demo, participants can predict ETH/USD price changes 5 minutes into the future. This is a simple game where the binary outcome is either “up” or “down”. Submitting predictions for a round closes 5 minutes before the resolution. To incentivize correct predictions and disincentivize incorrect ones, players place bets in ether to back their predictions. Bets placed on the incorrect outcome are distributed to the winners in proportion of the bets.

Obviously, the distribution of bets will only give meaningful predictions on a market with enough liquidity (players), which our little demo may not have at all times. 🙂

This example works in the Rinkeby testnet. The chart below, embedded from the Streamr canvas, shows the recent ETH/USD price and the outcomes of the most recent 5 min prediction rounds. The demo is live and interactive. Enter your bet in ETH, and press either the “up” or “down” button to lay your (crypto)money down!

In case the betting controls are not visible, your browser is not blockchain-enabled. Try the Metamask plugin for Chrome, or Mist, and get some test ETH from the Rinkeby faucet or by asking someone on our Slack.


The payout logic is coded to this smart contract which is called by the Streamr canvas that calculates the correct outcomes and drives the above chart. An embedded view of the canvas is shown below (open it in full screen or in the editor).

Questions and comments about this post and Streamr in general are appreciated! Join us on Slack, and of course feel free to follow us on Twitter as well.

Watching the flippening on Twitter with Streamr and Oraclize

We’ve met with the Oraclize people a few times, and we love what they do. There are also important synergies in what Streamr and Oraclize can do together. Let me explain, and show an example.

For fun, and to demonstrate Streamr and Oraclize working in tandem, we built a little example that watches the flippening (Ethereum potentially overtaking Bitcoin as the leading blockchain) in real-time. This idea was inspired by flippening.watch, which lists many metrics such as market cap and number of transactions, but none related to social media. So we chose to add some analytics using social media data. Using the streaming Twitter API and Streamr we build a process that listens to raw tweets mentioning either Ethereum or Bitcoin, counts the tweets in various timeframes, and makes the results available from our API. Later in this post, we’ll query this data from a smart contract.

Just to show you what the data currently looks like, here’s a table of real-time Twitter statistics, as received from our streaming API:

Window Ethereum tweets Bitcoin tweets Flippening
1 min
1 hour
24 hours


In the Streamr Engine, there’s two ways to convey data to smart contracts. The first method is event-driven: sending transactions directly from the Canvas via the EthereumCall module (see this or this example). The second option is to query for data from our web API when needed. This is preferable in use cases where a smart contract needs the data upon request instead of being constantly notified about the newest data.

Oraclize can easily facilitate this. They offer a request-response bridge between the blockchain and any web API such as Streamr’s. A smart contract can call a function on the Oraclize smart contract in order to request for data from a source such as an URL. Their off-chain system watches for these calls, and when one occurs, they go ahead and fetch the required data, and send the response back to the requesting smart contract by calling its callback function. Oraclize can even generate a proof if required, cryptographically showing that the data really came from our web API.

The results are being calculated from raw tweets using a canvas that counts the tweets in 1 minute, 1 hour, and 24 hour rolling windows and produces the results to a new stream. The result stream can be listened to by external applications, similarly to how this web page subscribes to it to show the table above. However, events in the stream can also be queried via our HTTP API. The following URL returns the latest event in the stream:

https://eth.streamr.com/api/v1/streams/I1AWyGXDRg28AO33ztPBZg/data/partitions/0/last?wrapper=object&content=json
To enable a smart contract to get the latest data on demand (for betting purposes, for example 😊), you can use Oraclize. Below is an example for Ethereum, written in Solidity. It requests Oraclize to fetch the current statistics from the Streamr API by calling the oraclize_query function. The result is soon thereafter delivered to the __callback function by Oraclize:
pragma solidity ^0.4.0;
import "github.com/oraclize/ethereum-api/oraclizeAPI.sol";

contract StreamrFlippeningDemo is usingOraclize {

    string public latest;

    event newOraclizeQuery(string description, uint256 fee);
    event newFlippeningData(string data);

    function StreamrFlippeningDemo() {
        // update();
    }

    function __callback(bytes32 myid, string result) {
        if (msg.sender != oraclize_cbAddress()) throw;
        latest = result;
        newFlippeningData(latest);
    }

    function update() payable {
        uint256 fee = oraclize_getPrice("URL");
        if (fee > this.balance) {
            newOraclizeQuery("Oraclize query was NOT sent, please add some ETH to cover for the query fee.", fee);
        } else {
            newOraclizeQuery("Oraclize query was sent, standing by for the answer..", fee);
            oraclize_query("URL", "json(https://eth.streamr.com/api/v1/streams/I1AWyGXDRg28AO33ztPBZg/data/partitions/0/last?wrapper=object&content=json).0.content.flippening_24h");
        }
    }
}

Streamr Editor is a low-code environment that allows users to build data-driven processes visually using drag and drop. Below, you can see the Canvas that counts tweets for the various time frames on both source streams, and produces the result into another stream (click here to open in full screen):

Take a look at our white paper draft if you want to dig deeper into our stack. In the draft we explain how streaming data can be tokenized and traded in the peer-to-peer Streamr Network powered by a token called DATAcoin. As all data in the Streamr Network will be signed at the source, smart contracts always receive trusted data from the real world. In this scenario, Oraclize can act as a valuable request-response bridge which natively supports data queries from the Streamr Network.

This simple example is just a taster, but it should immediately help you get started in building data-driven smart contracts. There’s so much more that can be achieved using Streamr and Oraclize either together or separately, depending on the use case. We’ll be hard at work in making the data-driven decentralized vision a reality!

Questions and comments about this post and Streamr in general are appreciated! Join us on Slack, and of course feel free to follow us on Twitter as well.