|
|
2 lat temu | |
|---|---|---|
| .. | ||
| contracts | aa76c15d1c Log sui errors (#951) | 2 lat temu |
| scripts | a60a05f118 Merge branch 'main' into sui-scripts-cleanup | 2 lat temu |
| README.md | f3edf9c971 [sui 9/x] - hot potato fix for price updates (#805) | 2 lat temu |
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.
PriceInfoObjects 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_feedupdate_single_price_feeds_if_freshget_priceWe 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.
wormhole::vaa::parse_and_verifyCall 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
pyth::pyth::update_single_price_feedUse 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>
pyth::pyth::update_single_price_feedUse 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>
pyth::hot_potato_vector::destroyDrop the hot potato. (You must call this function to drop the potato).
public fun destroy<T: copy + drop>(
hot_potato_vector: HotPotatoVector<T>
)
pyth::pyth::get_priceFinally, 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
09b2081498366df936abae26eea4b2d5cafb2788). Why does it point to a specific commit hash instead of "main" or "devnet"?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.
Also recall that the list of Pyth price feed IDs can be found here.