ZapProject
Search…
Oracle Template
Introduction
Zap-oracle-template provides an almost-ready code to have your Oracle and Endpoint setup and running from ground up in couple simple steps.
There are only 2 components to be filled out : Config.ts and Responder.ts and the template will perform all neccessary step to register Oracle, create Endpoint, Set parameters, even save information about Endpoint to ipfs and save the hash to Oracle's params to be displayed in Oracle Market Cap or Zap Admin site. Giving Oracle more visibility to users. ( If you have existing Oracle and Endpoint, this step will automatically ignored)
After creating and setting up Oracle and Endpoint, template code will start listening to queries, get data with logic in Responder.ts and automatically respond to query.
Upon completing setup and run, you will have an Oracle that provides data without any manual management .

INSTALLATION

Set up oracle directory
1
$ git clone https://github.com/zapproject/zap-oracle-template.git
2
$ cd zap-oracle-template
3
$ yarn
Copied!

Layout :

    1.
    Config : data about your wallet , ethereum node and your provider's pubkey and title
    2.
    Oracle : Template for Create/Manage flow
    3.
    Responder : Stub callback function when receive query event and return result

SETUP

Config

Config.ts is where you configure your oracle, if you have not already done so in Zap-term or Zap Admin. If you haven't already initialized your oracle, config.ts is where Oracle.ts it gets its information to set up the current working Oracle. It requires the Oracle Title, public key, node websocket link, and the mnemonic for the address running the Oracle
1
2
export const Config = {
3
4
//==============================================================================================================
5
// Provider Constants
6
//==============================================================================================================
7
8
title: "OracleTitle",
9
public_key: "oraclepublickey",
10
NODE_WS: "wss://kovan.infura.io/ws/xeb916AFjrcttuQlezyq",
11
mnemonic: "your zap oracle mneumonic from meta mask or some other wallet address"
12
};
Copied!
1
import {endpointFunctionality} from "./Responder";
2
{
3
name: "Example Endpoint",//Title of the endpoint
4
curve :[1,1,100000],//Piecewise Polynomial Function Bonding Curve
5
broker: "",//The one address allowed to bond and query the oracle, leave blank for no broker
6
md: "", //link to ipfs markdown file to be shown in OracleMarketCap, optional
7
queryList: [{
8
query: "querytype",//type of query
9
params: ["param1", "param2"],//endpoint parameters
10
response: ["result1", "result2", "result3", "result4"],//response types
11
dynamic: true,//true if returning array of ints/bytes32, false if returning series of 1 to 4 strings
12
getResponse: endpointFunctionality//link to endpointFunctionality in responder.ts
13
}]
Copied!

Oracle.ts

There should be very few reasons to edit Oracle.ts, although it is something you can edit if editing Config.ts is not enough. Oracle.ts initializes your Oracle and endpoints if they haven't been already, and it runs your Oracle endpoints as if they have been given functionality. It parses incoming events from the Zap contracts and returns responses to them, allowing for subscribers to query your oracle.

Responder.ts

This is the area where you should place your oracle functionality. You can assume that the event parameter always has the same kind of key:value data. From the event you can parse out the query and the endpoint parameters in order to form a response, which should be in an array of either strings, integers, or bytes32.
1
export async function getResponse(query:string, params?:string[]|[]){
2
// Implement your logic to decide what data to return for this query
3
return ["some result"]
4
}
Copied!

RUNNING ORACLE

1
yarn start
Copied!

Congratulations, you have successfully created an oracle and start listening to query as well as providing responses to subscribers. Make sure to leave it running as a daemon, as it will only respond to queries while it is running.

Last modified 2yr ago