Oracle Integration
Overview
By leveraging oracles, our platform provides access to trusted, real-time external data that powers AI model execution and decision-making.
Why We Need Oracle Integration
Oracles are essential for the following reasons:
- Real-time data feeds: AI models may need external inputs like cryptocurrency prices to execute predictions and decisions in real-time. Oracles allow smart contracts to access this data securely and in a decentralized manner.
- Verification of inputs: AI models must rely on authentic and untampered inputs, especially when handling off-chain data. Oracles help ensure the security and reliability of these inputs, which are critical for secure AI inference.
- Dynamic pricing of resources: For running AI models and secure inference, compute resources are priced based on demand. Oracles help provide real-time pricing information for these resources, ensuring that execution costs remain fair and decentralized.
Oracle Integration Overview
We are integrating oracles using Skip's Connect solution. This out-of-process service efficiently fetches price data from multiple sources, aggregates it into a single price feed, and periodically updates our chain using the new values. We are fetching the latest prices for each new block in our chain. Users and dApps can query the most recent prices directly from the blockchain.
In the rest of this document, we will provide two approaches to interact with the oracle and market map modules:
Using the API
API Endpoints
1. Get All Currency Pairs
- Endpoint:
/connect/oracle/v2/get_all_tickers
- Method:
GET
- Description: Retrieve all currency pairs tracked by the oracle module.
2. Get Market Map
- Endpoint:
/connect/marketmap/v2/marketmap
- Method: GET
- Description: Retrieves the entire market map, including all markets and configurations.
3. Get price
- Endpoint:
/connect/oracle/v2/get_price
- Method: GET
- Description: This endpoint is responsible for fetching the current price of a specific currency pair tracked by the oracle.
curl http://NODE_IP_ADDRESS:1317/connect/oracle/v2/get_price?currency_pair=ETH/USD
Response:
{
"price": {
"price": "2610437225",
"block_timestamp": "2024-10-15T09:27:10.734785284Z",
"block_height": "5753"
},
"nonce": "5480",
"decimals": "6",
"id": "25"
}
Smart Contract Example
The following Solidity smart contract demonstrates how to interact with the Oracle module to retrieve prices.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
// Import the ICosmos interface
import "x/evm/contracts/i_cosmos/ICosmos.sol";
contract OracleTest {
// The ICosmos contract's address on the EVM module
ICosmos public cosmosContract;
// Constructor that sets the address of the ICosmos contract
constructor() {
address cosmosContractAddress = 0x00000000000000000000000000000000000000f1;
cosmosContract = ICosmos(cosmosContractAddress);
}
// Function to query the price of a given token pair (e.g., "ETH/USD")
function queryCosmosPrice(string memory tokenPair) public view returns (uint256) {
// Path to the oracle module's GetPrices endpoint
string memory path = "/connect.oracle.v2.Query/GetPrices";
// Create the request body with the token pair
string memory request = string(abi.encodePacked('{"currency_pair_ids": ["', tokenPair, '"]}'));
// Call the query_cosmos function to send the request to Cosmos
string memory result = cosmosContract.query_cosmos(path, request);
// Convert the result string to uint256 (assuming the result is numeric)
uint256 price = parsePrice(result); // Implement a parsePrice function to extract the numeric value.
return price;
}
}
Explanation: queryCosmosPrice: Queries the price of a specific currency pair (e.g., "ETH/USD") and returns the price as a uint256.