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

$ git clone https://github.com/zapproject/zap-oracle-template.git
$ cd zap-oracle-template
$ yarn

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


export const  Config = {

//==============================================================================================================
// Provider Constants
//==============================================================================================================

   	title: "OracleTitle",
    public_key: "oraclepublickey",
    NODE_WS: "wss://kovan.infura.io/ws/xeb916AFjrcttuQlezyq",
    mnemonic: "your zap oracle mneumonic from meta mask or some other wallet address"
};
import {endpointFunctionality} from "./Responder";
{
        name: "Example Endpoint",//Title of the endpoint
        curve :[1,1,100000],//Piecewise Polynomial Function Bonding Curve
        broker: "",//The one address allowed to bond and query the oracle, leave blank for no broker
        md: "", //link to ipfs markdown file to be shown in OracleMarketCap, optional
        queryList: [{
            query: "querytype",//type of query
            params: ["param1", "param2"],//endpoint parameters
            response: ["result1", "result2", "result3", "result4"],//response types
            dynamic: true,//true if returning array of ints/bytes32, false if returning series of 1 to 4 strings
            getResponse: endpointFunctionality//link to endpointFunctionality  in responder.ts
        }]

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.

export async function getResponse(query:string, params?:string[]|[]){
  // Implement your logic to decide what data to return for this query
    return ["some result"]
}

RUNNING ORACLE

yarn start

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 updated