Push based oracles

Push Based Oracle

The Push-Based Oracle system enables contracts to receive real-time updates based on predefined criteria such as fixed intervals, specific price deviations, or a combination of both. This design provides flexibility and efficiency for decentralized applications needing accurate and timely data.

Usage

Method 1: Separate Oracle Contract

In this method, any contract that needs data can read directly from the Oracle contract. The Oracle maintains updates as a mapping, where each key maps to a Data struct containing the latest timestamp and value.

The updates mapping is a key-value store where:

  • Key: A unique identifier, typically a string, representing the asset or data type (e.g., DIA/USD, BTC/USD).

  • Value: A Data struct containing:

    • key: The identifier of the data entry (redundant for reference but useful for integrity checks).

    • timestamp: The timestamp of the latest update.

    • value: The most recent value associated with the key.

Example

contract PriceConsumer {
    Oracle public oracle;
    
    constructor(address _oracle) {
        oracle = Oracle(_oracle);
    }
    
    function getLatestPrice(string memory _key) public view returns (uint128) {
        return oracle.updates(_key).value;
    }
}

Example Oracle Contract:

Method 2: Direct Delivery to Contracts

In this method, updates are pushed to the receiving contract via a callback mechanism. The receiving contract must implement a specific interface and define a callback function to handle the incoming data.

  • Your contract must implement IMessageRecipient and IInterchainSecurityModule.

  • The Oracle invokes the handle callback function in the receiving contract to deliver updates. The data payload is decoded and can be stored or processed as needed.

function handle(
    uint32 _origin,
    bytes32 _sender,
    bytes calldata _data
) external payable virtual override {
    (string memory key, uint128 timestamp, uint128 value) = abi.decode(
        _data,
        (string, uint128, uint128)
    );
    // Store or process the received data
    updates[key] = Data({key: key, timestamp: timestamp, value: value});
    emit ReceivedMessage(key, timestamp, value);
}

Access push based oracle

  • To access push based oracles, use the updates(pair_name) function. Here’s how you can interact with it:

    • Use the full pair name (e.g., DIA/USD) as the pair_name

    • Query the contract using Etherscan's "Read" section.

  • The response contains the following data fields:

    • Key: The identifier of the asset pair (e.g., DIA/USD).

    • Timestamp: The time of the latest price update.

    • Value: The most recent price of the asset.

Last updated