If you are just trying to get started with our price feeds, see the self-contained code snippets below. If you’d like to use more advanced oracle functions please see the further information below. You can find a full sample data feed consumer contract here and the full Oracle interface specification is available here.
For some assets such as liquid staking tokens, it’s actually more relevant to use the conversion rate rather than the market price given the liquidity is often poor for these assets.
To use this special aggregation method, the base token first needs to be associated to a vault address which follows the ERC4626 standard.
use pragma_lib::abi::{IPragmaABIDispatcher, IPragmaABIDispatcherTrait};use pragma_lib::types::{AggregationMode, DataType, PragmaPricesResponse};use starknet::ContractAddress;use starknet::contract_address::contract_address_const;const KEY :felt252 = 1629317993172502401860; // felt252 conversion of "XSTRK/USD", can write const KEY : felt252 = 'XSTRK/USD'fn get_asset_conversion_rate(oracle_address: ContractAddress, asset : DataType) -> u128 { let oracle_dispatcher = IPragmaABIDispatcher{contract_address : oracle_address}; let output : PragmaPricesResponse= oracle_dispatcher.get_data(asset,AggregationMode::ConversionRate); return output.price;}
Alternatively, we implemented a specialized conversion rate feed that automatically calculates the conversion rate for specific feeds. Calling these feeds follows a process similar to retrieving spot BTC/USD prices.
Supported feeds:
Pair
Pair Id
Decimals
CONVERSION_XSTRK/USD
384270964630611589151504336040175440891848512324
8
CONVERSION_SSTRK/USD
384270964630611589151504335947941720523300754244
8
use pragma_lib::abi::{IPragmaABIDispatcher, IPragmaABIDispatcherTrait};use pragma_lib::types::{AggregationMode, DataType, PragmaPricesResponse};use starknet::ContractAddress;use starknet::contract_address::contract_address_const;const KEY :felt252 = 384270964630611589151504336040175440891848512324; // felt252 conversion of "CONVERSION_XSTRK/USD", can write const KEY : felt252 = 'CONVERSION_XSTRK/USD'fn get_asset_conversion_rate(oracle_address: ContractAddress, asset : DataType) -> u128 { let oracle_dispatcher = IPragmaABIDispatcher{contract_address : oracle_address}; let output : PragmaPricesResponse = oracle_dispatcher.get_data(asset,AggregationMode::Median); return output.price;}
It will only work with the Median aggregation mode.
data_type: enum of the data type you are requesting (See DataType structure). By providing the enum data type, you also provide the pair id (for spot entries), or the pair id and the expiration timestamp (for futures).