One topic that is often discussed are Oracles on Blockchains. The Waves Platform provides a nice framework for the implementation of Oracles and this story will shortly introduce you to how and why to use Oracles.

Problem Description

The major problem that is solved by the usage of Oracles is the very point that Blockchains can only access data that is stored on the blockchain. This is often referred to as the Oracle Problem. Here, the point is that it is important that Smart Contracts can only access data that is stored on the blockchain, so that every execution of the contract leads to the same result at a given point in time. Therefore, Smart Contracts are not able to access data from outside the blockchain, e.g., provided by Web Services or other external sources of data. Nevertheless, a couple of interesting applications needs access to real world data, e.g., Smart Contracts for insurances, decentralized betting systems and so forth.

Here, the solution is quite straightforward: if external data is necessary for the execution of a Smart Contract, this data needs to be stored on the blockchain. In order to achieve this, there are usually small programs implemented that access the necessary data and write it to the blockchain. Those little programs are called Oracles.

The Waves Platform provides some nice infrastructure that allows to implement Oracles in a standardized way.

Waves Oracle infrastructure

Here, you can find the main landing page of the Oracle infrastructure of the Waves Platform. It provides an overview about existing Oracles on the Waves Platform, together with the possibility to create new Oracles, see Figure 1.

Figure 1 — Landing page for the Oracle infrastructure on the Waves Platform

An introduction to this infrastructure can be found here.

Example of an Oracle

As a small example, the following use case should be considered now: imagine you want to access data of players from the mobile game Clash of Clans, e.g., your Smart Contract needs access to the number of trophies a user has won already and the arena the player is currently playing. Therefore, we will create a new Oracle that allows to store this data, and furthermore, we will create a small program that periodically retrieves the corresponding data for a user from the Clash of Clans API and stores it in the Oracle for the user (in this model, each user needs it’s own Oracle for storing his Clash of Clans data).

Creation of the Oracle

The Waves Platform Oracle infrastructure is a Web3 dApp that utilizes WavesKeeper. The easiest way is to create a new address and hit the Create an Oracle link from Figure 1. A popup will be shown in which certain information about the oracle needs to be created, e.g., the name, a description, the parameters that the oracle should provide and so on. The form is shown in Figure 2.

Figure 2 — Form for the creation of an Oracle

In our case, we want to store two values, the arena of the player and the amount of trophies the player has already won. Therefore, we can define those two parameters as shown in Figure 3:

Figure 3 — Definition of parameters

After the form is filled, the we need to authenticate the necessary data transaction for the creation of the Oracle with WavesKeeper. Once we did so, the new Oracle is successfully created.

Implementation of a program to store the data in the Oracle

Finally, after we created the Oracle, what we need is a small program that retrieves the corresponding data from the Clash of Clans API and stores it in the Oracle. Here, we use Python for this, since on the one hand the interaction with the Clash of Clans API and also the storage of data via data transactions on the Waves Platform, can easily be achieved with Python.

import pywaves as pw

import requests

import time



def getData():

res = requests.get('https://api.royaleapi.com/player/<player id>', headers = { 'auth': '<auth key for the API>' })

data = res.json()



return { 'trophies': data['trophies'], 'arena': data['arena']['arenaID'] }



def writeData(data):

pw.setNode('http://waves.dsalab.de:6869', 'testnet')

address = pw.Address(seed = '<seed of the Oracle address>')

now = round(time.time() * 1000)

tx = address.dataTransaction([

{ "key": "trohpies", "value": data['trophies'], "type": "integer" },

{ "key": "arena", "value": data['arena'], "type": "integer" },

{ "key": "wpo_updatedate", "value": now, "type": "integer" },

])

print(tx)



data = getData()

writeData(data)

This small program basically provides two methods:

getData(): This method retrieves the data from the Clash of Clans API. Therefore, an API key is necessary that could be received by registering here.

writeData(): This is the method that writes the data received from the Clash of Clans API to the blockchain. It basically executes a data transaction with the corresponding data. In this case, the amount of trohpies, the arena and additionally the current time as a timestamp for the last update of the Oracle.

The first line in the writeData() function configures the connection to the Waves Platform testnet, since this is where the Oracle was created. Once the program is executed, the current values for the arena and the trophies of the user are written to the Oracle. This could easily be seen in the Waves blockexplorer, as shown in Figure 4.

Figure 4 — Data of the Oracle

Additionally, the timestamp for the last update of the oracle is updated to, see Figure 5.

Figure 5 — Timestamp of the last update of the Oracle (wpo_updatedate)

Usage of the data in the Oracle

Once this data is stored in the blockchain, Ride based Smart Contracts can now access this data (via the getInteger(), getString(), getBinary() and getBoolean() methods) and use it for their calculations, e.g., decide on the amount of payouts, sending of transactions, winners of a contest, …