Let us take a look at an example and some sample code with respect to Oracle contract
Creating Oracle request
An Oracle contract can be deployed on the Ontology network to send Oracle requests and fetch data from outside world and make it accessible to other smart contracts.
In the future, all the deployed Oracle contracts will be listed on the Oracle market
The sample Oracle contract that we will be looking at here fetches sports statistics, i.e., match details.
Let's say the address of an example Oracle contract is as follows:
Test net - e0d635c7eb2c5eaa7d2207756a4c03a89790934a
Main net - a6ee997b142b002d49670ab73803403b09a23fa0
The structure of the Oracle request is of the form:
JsonParse will parse the http response with the parameter path list as key. The result will then be serialized to form a data structure, as defined by the user, and then finally written in the Oracle contract.
Here is a list of the parameters:
Parameter
Description
data
Data structure defined by the user
type
Data type, support int, float(* decimal as int), string, array, map, struct
sub_type
Sub-type of the array, map and struct
decimal
decimal places of floating point value
path
Iterator list of JSON parse key, if data is json, write key in list, if data is array, write index as string in list
This option is used to fix the time when a contract or task will be executed. It's format is as follows:
{"type":"","params":"",}
Currently it only supports one execution method, runAfter.
runAfter is used to specify a time when the task is to be run. For example, this can be used to send scores to the user after a game has ended. If the type field is left empty, the task will be run immediately.While the params field is used to specify the time in this format: YYYY-MM-DD HH:MM:SSExample - "2018-06-15 08:37:18"
An application contract deserializes the result as follows:
[ [2,4,4,1,5,3 ]]
The HTTP post example above fetches a random number from random.org. This Ontology Oracle packages a more convenient method randomOrg to get a signed random number.