Walkthrough: Using an existing Topic Inference On-chain

Fetching an existing on-chain topic inference

You can follow these instructions to view the most recent inference data brought on-chain for a given topic.

    /**
     * @notice Example for calling a protocol function with an existing inference value 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 inference value for
     */
    function callProtocolFunctionWithExistingValue(
        uint256 protocolFunctionArgument,
        uint256 topicId
    ) external payable {
        TopicValue memory topicValue = IAlloraConsumer(0x08f305A3449F51BFd79D7582A1630304664B1474).getTopicValue(topicId, '');
 
        if (topicValue.recentValueTime + 1 hours < block.timestamp) {
            revert('AlloraConsumerBringPredictionOnChainExample: stale value');
        }
 
        _protocolFunctionRequiringPredictionValue(
            protocolFunctionArgument, 
            topicValue.recentValue,
            topicValue.confidenceIntervalPercentiles,
            topicValue.confidenceIntervalValues
        );
    }

Step by Step Guide:

  1. Call the getTopicValue(uint256 topicId, bytes extraData) function with the correct topicId on the consumer contract for your desired chain. You can provide '' for extraData in solidity, or '0x' for extraData in typescript. The list of deployed contracts and topicIds can be found under deployments.
  2. Access the recentValue attribute.
  3. Optional: check the unix-timestamp under the recentValueTime attribute to see how recently the value was updated.

You can see the complete example here: Retrieve topic value example (opens in a new tab)

Using Typescript

const alloraConsumer = (new AlloraConsumer__factory()).attach(sepoliaAddresses.AlloraConsumer).connect(wallet) as AlloraConsumer
const { recentValue, recentValueTime } = await alloraConsumer.getTopicValue(TARGET_TOPIC_ID, '0x')
console.info({
  value: recentValue,
  timestamp: recentValueTime,
})

IAlloraConsumer interface

/**
 * @title Allora Consumer Interface
 */
interface IAlloraConsumer {
 
    /**
     * @notice Verify network inference and confidence interval for a given topic
     * 
     * @param nd The network inference and confidence interval data to verify
     */
    function verifyNetworkInference(
        AlloraConsumerNetworkInferenceData memory nd
    ) external returns (
        uint256 networkInference, 
        uint256[] memory confidenceIntervalPercentiles, 
        uint256[] memory confidenceIntervalValues, 
        address dataProvider
    );
 
    /**
     * @notice Verify network inference for a given topic without mutating state
     * 
     * @param nd The network inference data to verify
     */
    function verifyNetworkInferenceViewOnly(
        AlloraConsumerNetworkInferenceData memory nd
    ) external view returns (
        uint256 networkInference, 
        uint256[] memory confidenceIntervalPercentiles, 
        uint256[] memory confidenceIntervalValues, 
        address dataProvider
    );
 
    /**
     * @notice The message that must be signed by the provider to provide valid data
     *   recognized by verifyData
     * 
     * @param networkInferenceData The numerical data to verify
     */
    function getNetworkInferenceMessage(
        NetworkInferenceData memory networkInferenceData
    ) external view returns (bytes32);
 
    /**
     * @notice Get the inference for a given topicId
     * 
     * @param topicId The topicId to get the inference and confidence interval for
     * @param extraData The extraData to get the inference and confidence interval for
     * @return topicValue The topic data
     */
    function getTopicValue(uint256 topicId, bytes calldata extraData) external view returns (TopicValue memory topicValue);
}

Code Links

https://github.com/allora-network/allora-consumer/blob/main/script/retrieveTopicValue.ts (opens in a new tab)