In our previous blog post we went through the basic UI elements of Paralink Node Query Editor. Now is the time to create an example query with PQL and see it in action!

Query builder or query editor comes as standard with the Paralink Node. It allows you to test and publish PQL definitions. PQL definitions represent ETL (extract, transform, load) pipelines for sourcing, aggregating and validating information. They are the basis of oracle requests (jobs).

Let's say we want to get the Bitcoin price in USD from different sources and we want the end result to be the mean price of Bitcoin in USD (from three different sources).

For the purpose of this example we have selected the following data providers:

  1. CoinGecko (https://www.coingecko.com/en/api#explore-api)
  2. Bitfinex (https://docs.bitfinex.com/docs/introduction)
  3. Coindesk (https://www.coindesk.com/coindesk-api)

Creating a new PQL query

To open a new query, click the “Add new PQL” button under "Query list" tab.

Naming the query

To give your query (or project) a name, start writing in this field (shown below).

Now let's start with extracting the data from our selected data providers.

pql_2_1

CoinGecko

If we want to get the Bitcon price from CoinGecko we can use their public API. Click on “Add loader” and select “Http Get”.

We will use the following Request URL: https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd

After clicking “Submit” you should see the new loader and you can also rename it.

If you click the “Compile” button, the code will be compiled with the new PQL definitions.

pql_2_2

If you run the code you should get the following result (price will obviously be different):

{“bitcoin”:{“usd”:39418}}

But for our example we need only the price number. To do that we have to use the Traverse operator. Click "Add operator" and select "Traverse".

Insert two parameters: "bitcoin" and "usd" as shown below and click Submit.

If you compile and run the code your result should be only the price (number) as the result.

Now let's do the same with Bitfinex API.

Bitfinex

To get the Bitcon price from Bitfinex we can also use their public API. Again, click on “Add loader” and select “Http Get”.

We will use the following Request URL: https://api-pub.bitfinex.com/v2/ticker/tBTCUSD

If we do a partial run, we get the result as an array.

pql_2_3

To extract the specific number of retrieved array we have to use "Get index" operator. Click "Add operator" and select "Get index".

We need the first number from the array and the first number is indexed with 0. So we enter "0" and hit Submit.

Partial run should now return the correct format, only the price number (as shown below).

pql_2_4

Now we need the Bitcoin price from the last data feed.

Coindesk

If we want to get the Bitcon price from Coindesk we can use their public API. Like before, click on “Add loader” and select “Http Get”.

We will use the following Request URL: https://api.coindesk.com/v1/bpi/currentprice.json

Partial run will reveal the following result from Coindesk .json message.

pql_2_5

From this result, we have to extract only the Bitcoin price number. We can find it under "bpi", "USD", "rate_float". Once again we will use the traverse operator.

Click "Add operator" and select "Traverse". Then insert the following parameters.

Partial run should result in a Bitcoin price number.

pql_2_6

Now we have successfully retrieved Bitcoin price data from three different sources. But we need a mean of all three prices. How do we do that?

Mean price in USD

To get the mean price we have to click "Aggregate" and select "Mean".

Partial run will now aggregate all prices and show you the mean price in USD!

pql_2_7

At the end you can compile the code (click Compile) and save it by clicking the "Save" button.

How to get started?

This guide is made for developers who wish to start using Paralink as an oracle solution for their smart contracts, as well as for node operators who wish to provide a trusted oracle service or create their own oracle quorum.
We also invite you to visit our Github page where you can find more detailed information and documentation about Paralink solutions.

You can join our official Discord server where we have a dedicated channel for developers. You can join here:

https://discord.gg/QzaZAqJT6B