Pyth on Sui
- 1.Background
- 2.How to Update and Consume Price Feeds
- 3.Examples
- 4.Contracts Registry (addresses)
- 5.Common Questions how How to Integrate with Pyth on Sui
Pyth price feeds on Sui are uniquely represented in the global store as
PriceInfoObjects
. These objects have the key
ability and serve as wrappers around the PriceInfo
object, which in turn contains the price info: namely the PriceFeed
, the arrival time of the latest price update, and the attestation time of the latest update.PriceInfoObject
s are central to Pyth on Sui, since they are in unique correspondence with each Pyth price feed and must be passed in to functions that update price feeds or which query info about price feeds, e.g.update_single_price_feed
update_single_price_feeds_if_fresh
get_price
We demo how to update and then consume a price feed by building a Sui programmable transaction off-chain, and then executing it to update a price feed and get an updated price.
As with other chains, one first obtains a batch price attestation VAA (of type
vector<u8>
) from a Pyth price service endpoint, which encodes update price information for a feed.Call
parse_and_verify
on the batch attestation VAA bytes to obtain a VAA
hot potato object.public fun parse_and_verify(
wormhole_state: &State,
buf: vector<u8>, // price update VAA bytes
the_clock: &Clock
): VAA
Use the verified VAA and create a hot potato vector containing the latest price updates.
public fun create_price_infos_hot_potato(
pyth_state: &PythState,
verified_vaas: vector<VAA>,
clock: &Clock
): HotPotatoVector<PriceInfo>
Use the hot potato price updates vector to update a price feed.
Note that conventional Pyth price IDs are found here. However, instead of passing in a Pyth price feed ID to update the price feed (which is what is done on other chains), one must pass in a
PriceInfoObject
ID instead.The
PriceInfoObject
IDs are distinct from Pyth price feed IDs, and are stored in a map on-chain (Pyth price feed ID => PriceInfoObject ID). We pulled this map into a local json file here. The PriceInfoObject
ID can also be queried on-chain by calling the pyth::state::get_price_info_object_id
found in the Pyth package. See the common questions section below for more info.public fun update_single_price_feed(
pyth_state: &PythState,
price_updates: HotPotatoVector<PriceInfo>,
price_info_object: &mut PriceInfoObject,
fee: Coin<SUI>,
clock: &Clock
): HotPotatoVector<PriceInfo>
Drop the hot potato. (You must call this function to drop the potato).
public fun destroy<T: copy + drop>(
hot_potato_vector: HotPotatoVector<T>
)
Finally, get the price of the updated price feed in
PriceInfoObject
🎉🎉🎉.public fun get_price(
state: &PythState,
price_info_object: &PriceInfoObject,
clock: &Clock
): Price
See the
./scripts
folder for examples of programmable transactions for creating price feeds and updating price feeds.To build and test the contracts, run the following
$ make test
$ make build
- PYTH_PRICE_FEED_ID_TO_PRICE_INFO_OBJECT_ID: 0xf8929174008c662266a1adde78e1e8e33016eb7ad37d379481e860b911e40ed5
- PYTH_PRICE_FEED_ID_TO_PRICE_INFO_OBJECT_ID: 0x14b4697477d24c30c8eecc31dd1bd49a3115a9fe0db6bd4fd570cf14640b79a0
Our Pyth
Move.toml
file contains the following dependencies. It depends on specific versions of the Sui Framework as well as Wormhole. To make your Sui package compatible, you must also specify the following dependencies verbatim in your Move.toml
file. We are locked in to this specific rev
because our package depends on Wormhole, which uses the rev
09b2081498366df936abae26eea4b2d5cafb2788
.[dependencies.Sui]
git = "https://github.com/MystenLabs/sui.git"
subdir = "crates/sui-framework/packages/sui-framework"
rev = "09b2081498366df936abae26eea4b2d5cafb2788"
[dependencies.Wormhole]
git = "https://github.com/wormhole-foundation/wormhole.git"
subdir = "sui/wormhole"
rev = "d050ad1d67a5b7da9fb65030aad12ef5d774ccad"
The mapping of Pyth price feed IDs to
PriceInfoObject
object IDs can be found here. (Note: this may go out of date as more price feeds are introduced and created over time).This mapping is also stored on-chain, and can be queried on-chain using the getter function
pyth::state::get_price_info_object_id
defined in the Pyth package.Last modified 7d ago