Getting Started with Consumers

Getting Started with Consumers

Sample code snippets to help you get started using inferences from Allora topics

Consumer contracts are essential for bringing Allora Network prices on-chain. You can find the code repository containing example consumer contracts here (opens in a new tab). Consumer contracts verify that the data is correctly formatted, and signed by a valid signer.

Consuming Allora Inferences

Below is a complete example of a contract that brings inference data on-chain for use in a protocol, and verifies the data against an Allora Consumer contract. This example code can be found here (opens in a new tab).

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
 
import { 
  IAlloraConsumer, 
  TopicValue, 
  AlloraConsumerNetworkInferenceData
} from '../interface/IAlloraConsumer.sol';
import { Ownable2Step } from "../../lib/openzeppelin-contracts/contracts/access/Ownable2Step.sol";
import { EnumerableSet } from "../../lib/openzeppelin-contracts/contracts/utils/structs/EnumerableSet.sol";
 
 
/**
 * @title AlloraConsumerBringPredictionOnChainExample
 * @notice Example contract for using the Allora consumer by bringing predictions on-chain
 */
contract AlloraConsumerBringPredictionOnChainExample is Ownable2Step {
 
    // Sepolia consumer Address
    IAlloraConsumer public alloraConsumer = IAlloraConsumer(0x4341a3F0a350C2428184a727BAb86e16D4ba7018);
 
    // ***************************************************************
    // * ================== USER INTERFACE ========================= *
    // ***************************************************************
 
    /**
     * @notice Example for calling a protocol function with using an inference and confidence 
     *   intervals already stored on the Allora Consumer, only if the value is not stale.
     * 
     * @param protocolFunctionArgument An argument for the protocol function
     * @param topicId The id of the topic to use the most recent stored value for
     */
    function callProtocolFunctionWithExistingValue(
        uint256 protocolFunctionArgument,
        uint256 topicId
    ) external payable {
        TopicValue memory topicValue = alloraConsumer.getTopicValue(topicId, '');
 
        if (topicValue.recentValueTime + 1 hours < block.timestamp) {
            revert('AlloraConsumerBringPredictionOnChainExample: stale value');
        }
 
        _protocolFunctionRequiringPredictionValue(
            protocolFunctionArgument, 
            topicValue.recentValue,
            topicValue.confidenceIntervalPercentiles,
            topicValue.confidenceIntervalValues
        );
    }
 
    /**
     * @notice Example for calling a protocol function with an inference value from the Allora Consumer
     * 
     * @param protocolFunctionArgument An argument for the protocol function
     * @param alloraNetworkInferenceData The signed data from the Allora Consumer
     */
    function callProtocolFunctionWithAlloraTopicInference(
        uint256 protocolFunctionArgument,
        AlloraConsumerNetworkInferenceData calldata alloraNetworkInferenceData
    ) external payable {
        (
            uint256 value,
            uint256[] memory confidenceIntervalPercentiles,
            uint256[] memory confidenceIntervalValues,
        ) = alloraConsumer.verifyNetworkInference(alloraNetworkInferenceData);
 
        _protocolFunctionRequiringPredictionValue(
            protocolFunctionArgument, 
            value,
            confidenceIntervalPercentiles,
            confidenceIntervalValues
        );
    }
 
    function _protocolFunctionRequiringPredictionValue(
        uint256 protocolFunctionArgument, 
        uint256 value,
        uint256[] memory confidenceIntervalPercentiles,
        uint256[] memory confidenceIntervalValues
    ) internal {
        // use arguments and value 
    }
 
    // ***************************************************************
    // * ========================= ADMIN =========================== *
    // ***************************************************************
 
    /**
     * @notice Set the AlloraConsumer contract address
     * 
     * @param alloraConsumer_ The AlloraConsumer contract address
     */
    function setAlloraConsumerContract(IAlloraConsumer alloraConsumer_) external onlyOwner {
        alloraConsumer = alloraConsumer_;
    }
}