Onchain Subscriber Example

This example provides usecase of Zap data users utilize data provided by Zap Oracles to trigger function/event in their onchain contracts

Whats on-chain subscriber

On-chain subscriber is a contract that talks to Zap contracts and Zap Oracles to get data and decide what to do with that data. Use cases can be anything from using data received to settle a prediction betting with others; calling out other contracts (voting contracts, survey, random number generator, ...) or doing calculation with data received and emitting events with result. Whatever you want to achieve with on blockchain automation, you can do that being on-chain subscriber.

Work flow

Requirements:

  • Owner has to have dots bonded to an Endpoint to pay for query

  • On-chain subscriber contract has to call query to Endpoint

  • On-chain subscriber contract has to implement callback function that will be called by Oracle.

Code

pragma solidity ^0.4.24;
import "./ERC20.sol";

// Zap contracts's methods that user can call knowing the contracts's addresses
contract ZapBridge{
	function getContract(string contractName) public view returns (address); //coordinator
	function calcZapForDots(address, bytes32, uint256) external view returns (uint256); //bondage
	function delegateBond(address holderAddress, address oracleAddress, bytes32 endpoint, uint256 numDots) external returns (uint256 boundZap); //bondage
	function query(address, string, bytes32, bytes32[]) external returns (uint256); //dispatch
	function respondIntArray(uint256, string) external returns (bool); //dispatch
}

contract Subscriber {

	address public owner;
	ZapBridge public coordinator;
	ERC20 token;

//Coordinator contract is one single contract that 
//knows all other Zap contract aaaaa addresses 
	constructor(address _coordinator) {
		owner = msg.sender;
		coordinator = ZapBridge(_coordinator);
		address zapTokenAddress = coordinator.getContract("ZAP_TOKEN");
		token = ERC20(zapTokenAddress);
	}

	//Set provider that contract will receive data (only owner allowed)
	function setProvider(address _provider, bytes32 _endpoint){
		require(msg.sender == owner);
		endpoint = _endpoint;
		provider = _provider;
	}
	
	//This function call can be ommitted if owner call delegateBond directly to Bondage
	function bond(byte32 _endpoint, int256 dots){
		address BondageAddress = coordinator.getContract("BONDAGE")
		return ZapBridge(BondageAddress).bond(provider,endpoint,dots)
	}

	//Query offchain or onchain provider.
	function queryProvider(string queryString, bytes32[] params) returns (uint256) {
		address dispatchAddress = coordinator.getContract("DISPATCH");
		id = ZapBridge(dispatchAddress).query(provider,queryString,endpoint,params);
		return id;
	}

    //Implementing callback that will accept provider's respondIntArray
    //Response method options  are  :respondBytes32Array, respondIntArray, respond1, respond2, respond3, respond4
	function callback(uint256 _id, int[] _response) external{
		require(_id==id);
        //Implement your logic with _response data here
	}

}

Using template

To make it easier for you to setup contract with custom logic , we created a template that includes most of things you need to get it up and running

Setup environment and Create on-chain subscriber

  1. Clone the repo:

  2. cd subscriber-template

  3. yarn

  4. Edit callback function

  5. Deploy:

    1. On mainnet : yarn deploy

    2. Or truffle migrate --network <network name>

  6. Check for successfully contract deployment on chain

Calling on-chain subscriber

Once contract is deployed on chain, you now can call call functions bond queryProvider

Last updated