Supra's Push oracle publishes price pairs on-chain regularly and at high frequencies, enabling consumers to get near real-time prices for their smart contracts. The Push oracle functions via partnerships Supra has with respective chains to share, ensuring that frequent feed updates are available for these destination chains without interruption. Both the Push model and Pull model (on demand) have the same number of data pairs (currently 200+) offered by Supra.
Integrating with Supra price feeds is quick and easy. The price feed value published by Supra is known as an S-Value (aka Supra Value). In the following sections, we will demonstrate how to retrieve an S-Value using Supra's Push model.
Please refer to the below resources for a better understanding of our price feeds.
Data Feeds - This explains how Supra calculates the S-value for an asset.
Data Feeds Index - This provides a list of data pairs currently offered by Supra.
Available Networks - This is a list of available networks and Supra contract addresses.
Let's begin!
Step 1: Create The S-Value Interface
In the first step, you need to build data structures to receive the data feeds and functions required to fetch data into configured data structures. You may add the following code to the Solidity smart contract that you wish to retrieve the S-Value.
pragmasolidity 0.8.19;// depending on the requirement, you may build one or more data structures given below. interface ISupraSValueFeed {// Data structure to hold the pair datastructpriceFeed {uint256 round;uint256 decimals;uint256 time;uint256 price; }// Data structure to hold the derived/connverted data pairs. This depends on your requirements.structderivedData{int256 roundDifference;uint256 derivedPrice;uint256 decimals;}// Below functions enable you to retrieve different flavours of S-Value// Term "pair ID" and "Pair index" both refer to the same, pair index mentioned in our data pairs list.// Function to retrieve the data for a single data pairfunctiongetSvalue(uint256_pairIndex)externalviewreturns (priceFeed memory);//Function to fetch the data for a multiple data pairsfunctiongetSvalues(uint256[] memory_pairIndexes)externalviewreturns (priceFeed[] memory);// Function to convert and derive new data pairs using two pair IDs and a mathematical operator multiplication(*) or division(/).
//** Curreently only available in testnetsfunctiongetDerivedSvalue(uint256 pair_id_1,uint256 pair_id_2,uint256 operation)externalviewreturns (derivedData memory);// Function to check the latest Timestamp on which a data pair is updated. This will help you check the staleness of a data pair before performing an action.
functiongetTimestamp(uint256_tradingPair) externalviewreturns (uint256);}
The above code creates the interface that you will later apply in order to fetch a price from SupraOracles.
Step 2: Configure The S-Value Feed Address
Next, in order to retrieve the S-Value, configure the S-Value feed using the Supra Smart Contract address as demonstrated below.
The Supra contract for each network can be found in our network addresses list, and the address used in the example below needs to be replaced.
contract ISupraSValueFeedExample { ISupraSValueFeed internal sValueFeed;constructor() { sValueFeed =ISupraSValueFeed(0xE92D276bBE234869Ecc9b85101F423c6bD26654A); }} // used above is a sample address and you should use the correct address of your preffered network
Step 3: Get The S-Value Crypto Price
Now you can access the S-Values for any of the trading pairs Supra publishes. THe below sample code retrieves S-value for single as well as multiple data pairs for demonstration purposes. You may use it as appropriate.
// requesting s-value for a single pairfunctiongetPrice(uint256_priceIndex)externalviewreturns (ISupraSValueFeed.priceFeedmemory) {return sValueFeed.getSvalue(_priceIndex);}// requesting s-values for multiple pairsfunctiongetPriceForMultiplePair(uint256[] memory_pairIndexes) externalviewreturns (ISupraSValueFeed.priceFeed[] memory) {return sValueFeed.getSvalues(_pairIndexes);}// Function to convert and derive a new data pair using two existing pair ids, and a mathematical operator division(1), or multiplication(0).
functiongetDerivedValueOfPair (uint256 pair_id_1,uint256 pair_id_2,uint256 operation)externalviewreturns(ISupraSValueFeed.derivedDatamemory){return sValueFeed.getDerivedSvalue(pair_id_1,pair_id_2,operation);}
Recommended Best Practices:
Create a function with access control that updates the sValueFeed using the function updateSupraSvalueFeed().
This will allow you to update the address of the Supra storage contract after deployment to future-proof your contract. Access control is mandatory to prevent the undesired modification of the address.
Now you can easily request S-values of any supported data pairs.
Import Sui dependency in: client_example/sources/push_client.move
use SupraOracle::SupraSValueFeed::{Self, OracleHolder};
Add the following structs to: client_example/sources/push_client.move
structExampleHolder has key, store { id: UID, feeds:VecMap<u32, ExampleEntry>,}structExampleEntry has store, copy, drop { value:u128, decimal:u16, timestamp:u128, round:u64,}
ExampleHolder is the main resource struct for this module. It has a unique identifier `id` and a `feeds` map that stores the price feeds.
ExampleEntry represents a price feed entry with a value, decimal, timestamp, and round.
This initializes the ExampleHolder with a new ID and an empty feeds vector map. Then, it shares this resource.
Add the `update_price` function. This Updates the price of a specific pair in the ExampleHolder. If the pair already exists, it updates the existing ExampleEntry; if not, it inserts a new ExampleEntry.
Add the following `get_update_price` function to retrieve S-value for one pair. This is a public entry function that fetches the price for a single pair from the oracle and updates the corresponding entry in the ExampleHolder.
public entry fun get_update_price( oracle_holder:&OracleHolder, resource:&mutExampleHolder, pair:u32, _ctx:&mutTxContext) {let (value, decimal, price, round) =SupraSValueFeed::get_price(oracle_holder, pair);update_price(resource, pair, value, decimal, price, round);}
If you want to retrieve a S-value for multiple pairs you need to add `get_update_prices` which is a public entry function that fetches prices for multiple pairs from the oracle and updates the corresponding entries in the ExampleHolder.
public entry fun get_update_prices(oracle_holder:&OracleHolder,resource:&mutExampleHolder,pairs: vector<u32>,_ctx:&mutTxContext ) {let prices =SupraSValueFeed::get_prices(oracle_holder, pairs);let n = vector::length(&prices);let i =0;while (i < n) {let price = vector::borrow(&prices, i);let (pair, value, decimal, timestamp, round) = svalue_feed_holder::extract_price(price);update_price(resource, pair, value, decimal, timestamp, round); i = i +1; } }
The typical usage of this module starts with initializing an ExampleHolder using the init function. The ExampleHolder is then updated with price information using the update_price function, which can be manually called or called through get_update_prices or get_update_price, which also fetch the price data from the oracle.
Please note that you need an instance of OracleHolder to use get_update_prices and get_update_price. These are public entry functions that can be called from external modules or scripts.
Derived Pairs - Convert a data pair to any currency with ease
There have been many requests to provide functionality convert the prices to USD. While we take data directly from exchange quoted pairs mostly in stable coins (USDT/USDC), conversion at our end adds more latency and extra layer of margin for error. (*The oracle services who provide USD prices are already converting the USDT prices at their backend with previously mentioned disadvantages of that method). Still, for those who are in need we provide that as a custom function to convert and derive any pair using operators of Multiplication(parameter=0) or Division(Parameter=1).
Add the following `get_update_derived_price` function to retrieve S-value for derived pair. This is a public entry function that fetches the price for a single derived pair from the oracle and updates the corresponding entry in the ExampleHolder.