Example Contract

To connect your smart-contract to existing TrueQuery Oracle data feed, you should do these steps:

  • Open Feed Explorer page and filter existing feeds or search for exact name. If you want to make your own custom Feed, go to Build Section for more info.

  • Write down Feed address.

  • Create (or edit) a smart-contract that will send requests to chosen task. Here is the simple example:

pragma ever-solidity ^0.64;

import "./int/IMedianizedConsumer.tsol";
import "../task/int/IMedianizedFeed.tsol";

contract ExampleConsumerTest is IMedianizedConsumer {

    uint128 _medianizedResult;
    address _currentTask;

    constructor() public {
        require(msg.pubkey() == tvm.pubkey(), 199); //NOT_MY_OWNER
        tvm.accept();
    }

    // ****************************************************************
    // Getter
    // ****************************************************************

    function getMedianizedResult() external view returns (uint128) {
        return _medianizedResult;
    }

    // ****************************************************************
    // External
    // ****************************************************************

    function calculateMedianizedFeed(address taskAddress_, address payerAddress_) external {
		// << CALL THIS METHOD TO GET RESPONSE >> 
        require(taskAddress_.value != 0, 444);
        require(msg.pubkey() == tvm.pubkey(), 199); //NOT_MY_OWNER
        tvm.accept();
        _currentTask = taskAddress_;
        IMedianizedFeed(taskAddress_).medianizedFeedRequest{value: 500_000_000, flag: 1, bounce: true}(payerAddress_);
    }

    function medianizedCallback(uint128 response_) override external internalMsg {
        require(msg.sender == _currentTask && msg.sender.value != 0, 198); // NOT_AWAITED_TASK
        _medianizedResult = response_;
		// << INSERT YOUR ACTIONS AFTER ORACLE RESPONSE HERE >> 
    }

}
  • This contract implements IMedianizedConsumer interface and uses IMedianizedFeed interface to address task. This means that this contract works with task with Data Feed trigger type and Medianized consensus type. You can implement other interfaces from here and here to work with other types of tasks.

  • Example accepts taskAddress_ as a parameter, but in your real environment, you should set exact Feed address that you wrote down before.

  • Go to Deposits page and deposit enough gas & tokens from any wallet that you have (or ask anyone who wants to act as your Payer). After depositting enough (check Execution Fee param of Single Task or Data Feed that you want to connect to.

  • On the same Deposits page, scroll down to Allowances List and add allowance. You should specify address of your consumer contract, address of the chosen feed and how much value and token you allow as Payer to spend on this Consumer-Task pair.

Last updated