Using Price Index Data
You can follow these instructions to view the most recent price data brought on-chain for a given price index.
Complete Example:
/**
* @notice Example for calling a protocol function with using an inedex value already stored on the
* Allora Adapter, 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 callProtocolFunctionWithExistingIndexValue(
uint256 protocolFunctionArgument,
uint256 topicId
) external payable {
TopicValue memory topicValue = IAlloraAdapter(0xBEd9F9B7509288fCfe4d49F761C625C832e6264A).getTopicValue(topicId, '');
if (topicValue.recentValueTime + 1 hours < block.timestamp) {
revert('AlloraAdapterBringPredictionOnChainExample: stale value');
}
_protocolFunctionRequiringPredictionValue(protocolFunctionArgument, topicValue.recentValue);
}
function _protocolFunctionRequiringPredictionValue(uint256 protocolFunctionArgument, uint256 value) internal {
// use arguments and value
}
Step by Step Guide:
- Call the
getTopicValue(uint256 topicId, bytes extraData)
function with the correcttopicId
on the Adapter contract for your desired chain. You can provide''
forextraData
in solidity, or'0x'
forextraData
in typescript. The list of deployed contracts and topicIds can be found under deployments. - Access the
recentValue
attribute. - Optional: check the unix-timestamp under the
recentValueTime
attribute to see how recently the value was updated.
Using Typescript
const alloraAdapter = (new AlloraAdapter__factory()).attach(sepoliaAddresses.AlloraAdapter).connect(wallet) as AlloraAdapter
const result = await alloraAdapter.getTopicValue(TARGET_TOPIC_ID, '0x')
console.info({
value: result.config.recentValue,
timestamp: result.config.recentValueTime,
})
IAlloraAdapter interface
/**
* @title Allora Adapter Interface
*/
interface IAlloraAdapter {
/**
* @notice Get a verified piece of numeric data for a given topic
*
* @param nd The numeric data to aggregate
*/
function verifyData(AlloraAdapterNumericData memory nd) external returns (
uint256 numericValue,
address dataProvider
);
/**
* @notice Get a verified piece of numeric data for a given topic without mutating state
*
* @param pd The numeric data to aggregate
*/
function verifyDataViewOnly(AlloraAdapterNumericData calldata pd) external view returns (
uint256 numericValue,
address dataProvider
);
/**
* @notice Get the topic data for a given topicId
*
* @param topicId The topicId to get the topic data for
* @param extraData The extraData to get the topic data for
* @return topicValue The topic data
*/
function getTopicValue(uint256 topicId, bytes calldata extraData) external view returns (TopicValue memory);
}
Code Links
- Open source adapter code (opens in a new tab)
- IAlloraAdapter (opens in a new tab), including the structs used for Solidity code.