| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031 |
- module pyth::pyth {
- use std::vector;
- use sui::tx_context::{TxContext};
- use sui::coin::{Self, Coin};
- use sui::sui::{SUI};
- use sui::transfer::{Self};
- use sui::clock::{Self, Clock};
- use sui::package::{UpgradeCap};
- use pyth::event::{Self as pyth_event};
- use pyth::data_source::{Self, DataSource};
- use pyth::state::{Self as state, State as PythState, DeployerCap};
- use pyth::price_info::{Self, PriceInfo, PriceInfoObject};
- use pyth::batch_price_attestation::{Self};
- use pyth::price_feed::{Self};
- use pyth::price::{Self, Price};
- use pyth::price_identifier::{PriceIdentifier};
- use pyth::version_control::{UpdatePriceFeeds, CreatePriceFeeds};
- use wormhole::external_address::{Self};
- use wormhole::vaa::{Self};
- use wormhole::state::{State as WormState};
- use wormhole::bytes32::{Self};
- const E_DATA_SOURCE_EMITTER_ADDRESS_AND_CHAIN_IDS_DIFFERENT_LENGTHS: u64 = 0;
- const E_INVALID_DATA_SOURCE: u64 = 1;
- const E_INSUFFICIENT_FEE: u64 = 2;
- const E_STALE_PRICE_UPDATE: u64 = 3;
- const E_PRICE_INFO_OBJECT_NOT_FOUND: u64 = 4;
- const E_INVALID_PUBLISH_TIMES_LENGTH: u64 = 5;
- const E_NO_FRESH_DATA: u64 = 6;
- friend pyth::pyth_tests;
- /// Call init_and_share_state with deployer cap to initialize
- /// state and emit event corresponding to Pyth initialization.
- public entry fun init_pyth(
- deployer: DeployerCap,
- upgrade_cap: UpgradeCap,
- stale_price_threshold: u64,
- governance_emitter_chain_id: u64,
- governance_emitter_address: vector<u8>,
- data_sources_emitter_chain_ids: vector<u64>,
- data_sources_emitter_addresses: vector<vector<u8>>,
- update_fee: u64,
- ctx: &mut TxContext
- ) {
- state::init_and_share_state(
- deployer,
- upgrade_cap,
- stale_price_threshold,
- update_fee,
- data_source::new(
- governance_emitter_chain_id,
- external_address::new((bytes32::from_bytes(governance_emitter_address)))
- ),
- parse_data_sources(
- data_sources_emitter_chain_ids,
- data_sources_emitter_addresses,
- ),
- ctx
- );
- // Emit Pyth initialization event.
- pyth_event::emit_pyth_initialization_event();
- }
- fun parse_data_sources(
- emitter_chain_ids: vector<u64>,
- emitter_addresses: vector<vector<u8>>
- ): vector<DataSource> {
- assert!(vector::length(&emitter_chain_ids) == vector::length(&emitter_addresses),
- E_DATA_SOURCE_EMITTER_ADDRESS_AND_CHAIN_IDS_DIFFERENT_LENGTHS);
- let sources = vector::empty();
- let i = 0;
- while (i < vector::length(&emitter_chain_ids)) {
- vector::push_back(&mut sources, data_source::new(
- *vector::borrow(&emitter_chain_ids, i),
- external_address::new(bytes32::from_bytes(*vector::borrow(&emitter_addresses, i)))
- ));
- i = i + 1;
- };
- sources
- }
- /// Create and share new price feed objects if they don't already exist.
- public fun create_price_feeds(
- worm_state: &WormState,
- pyth_state: &mut PythState,
- vaas: vector<vector<u8>>,
- clock: &Clock,
- ctx: &mut TxContext
- ){
- // Version control.
- state::check_minimum_requirement<CreatePriceFeeds>(pyth_state);
- while (!vector::is_empty(&vaas)) {
- let vaa = vector::pop_back(&mut vaas);
- // Deserialize the VAA
- let vaa = vaa::parse_and_verify(worm_state, vaa, clock);
- // Check that the VAA is from a valid data source (emitter)
- assert!(
- state::is_valid_data_source(
- pyth_state,
- data_source::new(
- (vaa::emitter_chain(&vaa) as u64),
- vaa::emitter_address(&vaa))
- ),
- E_INVALID_DATA_SOURCE);
- // Deserialize the batch price attestation
- let price_infos = batch_price_attestation::destroy(batch_price_attestation::deserialize(vaa::take_payload(vaa), clock));
- while (!vector::is_empty(&price_infos)){
- let cur_price_info = vector::pop_back(&mut price_infos);
- // Only create new Sui PriceInfoObject if not already
- // registered with the Pyth State object.
- if (!state::price_feed_object_exists(
- pyth_state,
- price_feed::get_price_identifier(
- price_info::get_price_feed(&cur_price_info)
- )
- )
- ){
- // Create and share newly created Sui PriceInfoObject containing a price feed,
- // and then register a copy of its ID with State.
- let new_price_info_object = price_info::new_price_info_object(cur_price_info, ctx);
- let price_identifier = price_info::get_price_identifier(&cur_price_info);
- let id = price_info::uid_to_inner(&new_price_info_object);
- state::register_price_info_object(pyth_state, price_identifier, id);
- transfer::public_share_object(new_price_info_object);
- }
- }
- };
- }
- /// Update Pyth Price Info objects (containing price feeds) with the
- /// price data in the given VAAs.
- ///
- /// The vaas argument is a vector of VAAs encoded as bytes.
- ///
- /// The javascript https://github.com/pyth-network/pyth-js/tree/main/pyth-sui-js package
- /// should be used to fetch these VAAs from the Price Service. More information about this
- /// process can be found at https://docs.pyth.network/consume-data.
- ///
- /// The given fee must contain a sufficient number of coins to pay the update fee for the given vaas.
- /// The update fee amount can be queried by calling get_update_fee(&vaas).
- ///
- /// Please read more information about the update fee here: https://docs.pyth.network/consume-data/on-demand#fees
- public fun update_price_feeds(
- worm_state: &WormState,
- pyth_state: &PythState,
- vaas: vector<vector<u8>>,
- price_info_objects: &mut vector<PriceInfoObject>,
- fee: Coin<SUI>,
- clock: &Clock
- ){
- // Version control.
- state::check_minimum_requirement<UpdatePriceFeeds>(pyth_state);
- // Charge the message update fee
- assert!(get_total_update_fee(pyth_state, &vaas) <= coin::value(&fee), E_INSUFFICIENT_FEE);
- // TODO: use Wormhole fee collector instead of transferring funds to deployer address.
- transfer::public_transfer(fee, @pyth);
- // Update the price feed from each VAA
- while (!vector::is_empty(&vaas)) {
- update_price_feed_from_single_vaa(
- worm_state,
- pyth_state,
- vector::pop_back(&mut vaas),
- price_info_objects,
- clock
- );
- };
- }
- /// Make sure that a Sui object of type PriceInfoObject exists for each update
- /// encoded in the worm_vaa (batch_attestation_vaa). These should be passed in
- /// via the price_info_objects argument. If for any price feed update, a
- /// a PriceInfoObject with a matching price identifier is not found, the update_cache
- /// function will revert, causing this function to revert.
- fun update_price_feed_from_single_vaa(
- worm_state: &WormState,
- pyth_state: &PythState,
- worm_vaa: vector<u8>,
- price_info_objects: &mut vector<PriceInfoObject>,
- clock: &Clock
- ) {
- // Deserialize the VAA
- let vaa = vaa::parse_and_verify(worm_state, worm_vaa, clock);
- // Check that the VAA is from a valid data source (emitter)
- assert!(
- state::is_valid_data_source(
- pyth_state,
- data_source::new(
- (vaa::emitter_chain(&vaa) as u64),
- vaa::emitter_address(&vaa))
- ),
- E_INVALID_DATA_SOURCE);
- // Deserialize the batch price attestation
- let price_infos = batch_price_attestation::destroy(batch_price_attestation::deserialize(vaa::take_payload(vaa), clock));
- // Update price info objects.
- update_cache(price_infos, price_info_objects, clock);
- }
- /// Update PriceInfoObjects using up-to-date PriceInfos.
- public(friend) fun update_cache(
- updates: vector<PriceInfo>,
- price_info_objects: &mut vector<PriceInfoObject>,
- clock: &Clock,
- ){
- while (!vector::is_empty(&updates)) {
- let update = vector::pop_back(&mut updates);
- let i = 0;
- let found = false;
- // Note - Would it be worth it to construct an in-memory hash-map to make look-ups faster?
- // This loop might be expensive if there are a large number of price_info_objects
- // passed in.
- while (i < vector::length<PriceInfoObject>(price_info_objects) && found == false){
- // Check if the current price info object corresponds to the price feed that
- // the update is meant for.
- let price_info = price_info::get_price_info_from_price_info_object(vector::borrow(price_info_objects, i));
- if (price_info::get_price_identifier(&price_info) ==
- price_info::get_price_identifier(&update)){
- found = true;
- pyth_event::emit_price_feed_update(price_feed::from(price_info::get_price_feed(&update)), clock::timestamp_ms(clock)/1000);
- // Update the price info object with the new updated price info.
- if (is_fresh_update(&update, vector::borrow(price_info_objects, i))){
- price_info::update_price_info_object(
- vector::borrow_mut(price_info_objects, i),
- update
- );
- }
- };
- i = i + 1;
- };
- if (!found){
- abort(E_PRICE_INFO_OBJECT_NOT_FOUND)
- }
- };
- vector::destroy_empty(updates);
- }
- /// Update the cached price feeds with the data in the given VAAs, using
- /// update_price_feeds(). However, this function will only have an effect if any of the
- /// prices in the update are fresh. The price_identifiers and publish_times parameters
- /// are used to determine if the update is fresh without doing any serialisation or verification
- /// of the VAAs, potentially saving time and gas. If the update contains no fresh data, this function
- /// will revert with error::no_fresh_data().
- ///
- /// For a given price update i in the batch, that price is considered fresh if the current cached
- /// price for price_identifiers[i] is older than publish_times[i].
- public fun update_price_feeds_if_fresh(
- vaas: vector<vector<u8>>,
- worm_state: &WormState,
- pyth_state: &PythState,
- price_info_objects: &mut vector<PriceInfoObject>,
- publish_times: vector<u64>,
- fee: Coin<SUI>,
- clock: &Clock
- ) {
- assert!(vector::length(price_info_objects) == vector::length(&publish_times),
- E_INVALID_PUBLISH_TIMES_LENGTH
- );
- let fresh_data = false;
- let i = 0;
- while (i < vector::length(&publish_times)) {
- let cur_price_info = price_info::get_price_info_from_price_info_object(vector::borrow(price_info_objects, i));
- let cur_price_feed = price_info::get_price_feed(&cur_price_info);
- let cur_price = price_feed::get_price(cur_price_feed);
- let cached_timestamp = price::get_timestamp(&cur_price);
- if (cached_timestamp < *vector::borrow(&publish_times, i)) {
- fresh_data = true;
- break
- };
- i = i + 1;
- };
- assert!(fresh_data, E_NO_FRESH_DATA);
- update_price_feeds(worm_state, pyth_state, vaas, price_info_objects, fee, clock);
- }
- /// Determine if the given price update is "fresh": we have nothing newer already cached for that
- /// price feed within a PriceInfoObject.
- fun is_fresh_update(update: &PriceInfo, price_info_object: &PriceInfoObject): bool {
- // Get the timestamp of the update's current price
- let price_feed = price_info::get_price_feed(update);
- let update_timestamp = price::get_timestamp(&price_feed::get_price(price_feed));
- // Get the timestamp of the cached data for the price identifier
- let cached_price_info = price_info::get_price_info_from_price_info_object(price_info_object);
- let cached_price_feed = price_info::get_price_feed(&cached_price_info);
- let cached_timestamp = price::get_timestamp(&price_feed::get_price(cached_price_feed));
- update_timestamp > cached_timestamp
- }
- // -----------------------------------------------------------------------------
- // Query the cached prices
- //
- // It is strongly recommended to update the cached prices using the functions above,
- // before using the functions below to query the cached data.
- /// Determine if a price feed for the given price_identifier exists
- public fun price_feed_exists(state: &PythState, price_identifier: PriceIdentifier): bool {
- state::price_feed_object_exists(state, price_identifier)
- }
- /// Get the latest available price cached for the given price identifier, if that price is
- /// no older than the stale price threshold.
- ///
- /// Please refer to the documentation at https://docs.pyth.network/consumers/best-practices for
- /// how to how this price safely.
- ///
- /// Important: Pyth uses an on-demand update model, where consumers need to update the
- /// cached prices before using them. Please read more about this at https://docs.pyth.network/consume-data/on-demand.
- /// get_price() is likely to abort unless you call update_price_feeds() to update the cached price
- /// beforehand, as the cached prices may be older than the stale price threshold.
- ///
- /// The price_info_object is a Sui object with the key ability that uniquely
- /// contains a price feed for a given price_identifier.
- ///
- public fun get_price(state: &PythState, price_info_object: &PriceInfoObject, clock: &Clock): Price {
- get_price_no_older_than(price_info_object, clock, state::get_stale_price_threshold_secs(state))
- }
- /// Get the latest available price cached for the given price identifier, if that price is
- /// no older than the given age.
- public fun get_price_no_older_than(price_info_object: &PriceInfoObject, clock: &Clock, max_age_secs: u64): Price {
- let price = get_price_unsafe(price_info_object);
- check_price_is_fresh(&price, clock, max_age_secs);
- price
- }
- /// Get the latest available price cached for the given price identifier.
- ///
- /// WARNING: the returned price can be from arbitrarily far in the past.
- /// This function makes no guarantees that the returned price is recent or
- /// useful for any particular application. Users of this function should check
- /// the returned timestamp to ensure that the returned price is sufficiently
- /// recent for their application. The checked get_price_no_older_than()
- /// function should be used in preference to this.
- public fun get_price_unsafe(price_info_object: &PriceInfoObject): Price {
- // TODO: extract Price from this guy...
- let price_info = price_info::get_price_info_from_price_info_object(price_info_object);
- price_feed::get_price(
- price_info::get_price_feed(&price_info)
- )
- }
- fun abs_diff(x: u64, y: u64): u64 {
- if (x > y) {
- return x - y
- } else {
- return y - x
- }
- }
- /// Get the stale price threshold: the amount of time after which a cached price
- /// is considered stale and no longer returned by get_price()/get_ema_price().
- public fun get_stale_price_threshold_secs(state: &PythState): u64 {
- state::get_stale_price_threshold_secs(state)
- }
- fun check_price_is_fresh(price: &Price, clock: &Clock, max_age_secs: u64) {
- let age = abs_diff(clock::timestamp_ms(clock)/1000, price::get_timestamp(price));
- assert!(age < max_age_secs, E_STALE_PRICE_UPDATE);
- }
- /// Please read more information about the update fee here: https://docs.pyth.network/consume-data/on-demand#fees
- public fun get_total_update_fee(pyth_state: &PythState, update_data: &vector<vector<u8>>): u64 {
- state::get_base_update_fee(pyth_state) * vector::length(update_data)
- }
- }
- module pyth::pyth_tests{
- use std::vector::{Self};
- use sui::sui::SUI;
- use sui::coin::{Self, Coin};
- use sui::test_scenario::{Self, Scenario, ctx, take_shared, return_shared};
- use sui::package::Self;
- use sui::object::{Self, ID};
- use sui::clock::{Self, Clock};
- use pyth::state::{Self, State as PythState};
- use pyth::price_identifier::{Self};
- use pyth::price_info::{Self, PriceInfo, PriceInfoObject};
- use pyth::price_feed::{Self};
- use pyth::data_source::{Self, DataSource};
- use pyth::i64::{Self};
- use pyth::price::{Self};
- use pyth::pyth::{Self};
- use wormhole::setup::{Self as wormhole_setup, DeployerCap};
- use wormhole::external_address::{Self};
- use wormhole::bytes32::{Self};
- use wormhole::state::{State as WormState};
- const DEPLOYER: address = @0x1234;
- #[test_only]
- /// A vector containing a single VAA with:
- /// - emitter chain ID 17
- /// - emitter address 0x71f8dcb863d176e2c420ad6610cf687359612b6fb392e0642b0ca6b1f186aa3b
- /// - payload corresponding to the batch price attestation of the prices returned by get_mock_price_infos()
- const TEST_VAAS: vector<vector<u8>> = vector[x"0100000000010036eb563b80a24f4253bee6150eb8924e4bdf6e4fa1dfc759a6664d2e865b4b134651a7b021b7f1ce3bd078070b688b6f2e37ce2de0d9b48e6a78684561e49d5201527e4f9b00000001001171f8dcb863d176e2c420ad6610cf687359612b6fb392e0642b0ca6b1f186aa3b0000000000000001005032574800030000000102000400951436e0be37536be96f0896366089506a59763d036728332d3e3038047851aea7c6c75c89f14810ec1c54c03ab8f1864a4c4032791f05747f560faec380a695d1000000000000049a0000000000000008fffffffb00000000000005dc0000000000000003000000000100000001000000006329c0eb000000006329c0e9000000006329c0e400000000000006150000000000000007215258d81468614f6b7e194c5d145609394f67b041e93e6695dcc616faadd0603b9551a68d01d954d6387aff4df1529027ffb2fee413082e509feb29cc4904fe000000000000041a0000000000000003fffffffb00000000000005cb0000000000000003010000000100000001000000006329c0eb000000006329c0e9000000006329c0e4000000000000048600000000000000078ac9cf3ab299af710d735163726fdae0db8465280502eb9f801f74b3c1bd190333832fad6e36eb05a8972fe5f219b27b5b2bb2230a79ce79beb4c5c5e7ecc76d00000000000003f20000000000000002fffffffb00000000000005e70000000000000003010000000100000001000000006329c0eb000000006329c0e9000000006329c0e40000000000000685000000000000000861db714e9ff987b6fedf00d01f9fea6db7c30632d6fc83b7bc9459d7192bc44a21a28b4c6619968bd8c20e95b0aaed7df2187fd310275347e0376a2cd7427db800000000000006cb0000000000000001fffffffb00000000000005e40000000000000003010000000100000001000000006329c0eb000000006329c0e9000000006329c0e400000000000007970000000000000001"];
- #[test_only]
- /// Init Wormhole core bridge state.
- /// Init Pyth state.
- /// Set initial Sui clock time.
- /// Mint some SUI fee coins.
- fun setup_test(
- stale_price_threshold: u64,
- governance_emitter_chain_id: u64,
- governance_emitter_address: vector<u8>,
- data_sources: vector<DataSource>,
- base_update_fee: u64,
- to_mint: u64
- ): (Scenario, Coin<SUI>, Clock) {
- let scenario = test_scenario::begin(DEPLOYER);
- // Initialize Wormhole core bridge.
- wormhole_setup::init_test_only(ctx(&mut scenario));
- test_scenario::next_tx(&mut scenario, DEPLOYER);
- // Take the `DeployerCap` from the sender of the transaction.
- let deployer_cap =
- test_scenario::take_from_address<DeployerCap>(
- &scenario,
- DEPLOYER
- );
- // This will be created and sent to the transaction sender automatically
- // when the contract is published. This exists in place of grabbing
- // it from the sender.
- let upgrade_cap =
- package::test_publish(
- object::id_from_address(@wormhole),
- test_scenario::ctx(&mut scenario)
- );
- let governance_chain = 1234;
- let governance_contract =
- x"deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef";
- let initial_guardians =
- vector[
- x"beFA429d57cD18b7F8A4d91A2da9AB4AF05d0FBe"
- ];
- let guardian_set_seconds_to_live = 5678;
- let message_fee = 350;
- wormhole_setup::complete(
- deployer_cap,
- upgrade_cap,
- governance_chain,
- governance_contract,
- initial_guardians,
- guardian_set_seconds_to_live,
- message_fee,
- test_scenario::ctx(&mut scenario)
- );
- // Initialize Pyth state.
- let pyth_upgrade_cap=
- package::test_publish(
- object::id_from_address(@pyth),
- test_scenario::ctx(&mut scenario)
- );
- state::init_test_only(ctx(&mut scenario));
- test_scenario::next_tx(&mut scenario, DEPLOYER);
- let pyth_deployer_cap = test_scenario::take_from_address<state::DeployerCap>(
- &scenario,
- DEPLOYER
- );
- state::init_and_share_state(
- pyth_deployer_cap,
- pyth_upgrade_cap,
- stale_price_threshold,
- base_update_fee,
- data_source::new(governance_emitter_chain_id, external_address::new(bytes32::from_bytes(governance_emitter_address))),
- data_sources,
- ctx(&mut scenario)
- );
- let coins = coin::mint_for_testing<SUI>(to_mint, ctx(&mut scenario));
- let clock = clock::create_for_testing(ctx(&mut scenario));
- (scenario, coins, clock)
- }
- #[test_only]
- fun get_mock_price_infos(): vector<PriceInfo> {
- vector<PriceInfo>[
- price_info::new_price_info(
- 1663680747,
- 1663074349,
- price_feed::new(
- price_identifier::from_byte_vec(x"c6c75c89f14810ec1c54c03ab8f1864a4c4032791f05747f560faec380a695d1"),
- price::new(i64::new(1557, false), 7, i64::new(5, true), 1663680740),
- price::new(i64::new(1500, false), 3, i64::new(5, true), 1663680740),
- ),
- ),
- price_info::new_price_info(
- 1663680747,
- 1663074349,
- price_feed::new(
- price_identifier::from_byte_vec(x"3b9551a68d01d954d6387aff4df1529027ffb2fee413082e509feb29cc4904fe"),
- price::new(i64::new(1050, false), 3, i64::new(5, true), 1663680745),
- price::new(i64::new(1483, false), 3, i64::new(5, true), 1663680745),
- ),
- ),
- price_info::new_price_info(
- 1663680747,
- 1663074349,
- price_feed::new(
- price_identifier::from_byte_vec(x"33832fad6e36eb05a8972fe5f219b27b5b2bb2230a79ce79beb4c5c5e7ecc76d"),
- price::new(i64::new(1010, false), 2, i64::new(5, true), 1663680745),
- price::new(i64::new(1511, false), 3, i64::new(5, true), 1663680745),
- ),
- ),
- price_info::new_price_info(
- 1663680747,
- 1663074349,
- price_feed::new(
- price_identifier::from_byte_vec(x"21a28b4c6619968bd8c20e95b0aaed7df2187fd310275347e0376a2cd7427db8"),
- price::new(i64::new(1739, false), 1, i64::new(5, true), 1663680745),
- price::new(i64::new(1508, false), 3, i64::new(5, true), 1663680745),
- ),
- ),
- ]
- }
- #[test_only]
- /// Compare the expected price feed with the actual Pyth price feeds.
- fun check_price_feeds_cached(expected: &vector<PriceInfo>, actual: &vector<PriceInfoObject>) {
- // Check that we can retrieve the correct current price and ema price for each price feed
- let i = 0;
- while (i < vector::length(expected)) {
- let price_feed = price_info::get_price_feed(vector::borrow(expected, i));
- let price = price_feed::get_price(price_feed);
- let ema_price = price_feed::get_ema_price(price_feed);
- let price_identifier = price_info::get_price_identifier(vector::borrow(expected, i));
- let actual_price_info = price_info::get_price_info_from_price_info_object(vector::borrow(actual, i));
- let actual_price_feed = price_info::get_price_feed(&actual_price_info);
- let actual_price = price_feed::get_price(actual_price_feed);
- let actual_ema_price = price_feed::get_ema_price(actual_price_feed);
- let actual_price_identifier = price_info::get_price_identifier(&actual_price_info);
- assert!(price == actual_price, 0);
- assert!(ema_price == actual_ema_price, 0);
- assert!(price_identifier::get_bytes(&price_identifier) == price_identifier::get_bytes(&actual_price_identifier), 0);
- i = i + 1;
- };
- }
- #[test]
- fun test_get_update_fee() {
- let single_update_fee = 50;
- let (scenario, test_coins, _clock) = setup_test(500, 23, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", vector[], single_update_fee, 0);
- test_scenario::next_tx(&mut scenario, DEPLOYER, );
- let pyth_state = take_shared<PythState>(&scenario);
- // Pass in a single VAA
- assert!(pyth::get_total_update_fee(&pyth_state, &vector[
- x"fb1543888001083cf2e6ef3afdcf827e89b11efd87c563638df6e1995ada9f93",
- ]) == single_update_fee, 1);
- // Pass in multiple VAAs
- assert!(pyth::get_total_update_fee(&pyth_state, &vector[
- x"4ee17a1a4524118de513fddcf82b77454e51be5d6fc9e29fc72dd6c204c0e4fa",
- x"c72fdf81cfc939d4286c93fbaaae2eec7bae28a5926fa68646b43a279846ccc1",
- x"d9a8123a793529c31200339820a3210059ecace6c044f81ecad62936e47ca049",
- x"84e4f21b3e65cef47fda25d15b4eddda1edf720a1d062ccbf441d6396465fbe6",
- x"9e73f9041476a93701a0b9c7501422cc2aa55d16100bec628cf53e0281b6f72f"
- ]) == 250, 1);
- return_shared(pyth_state);
- coin::burn_for_testing<SUI>(test_coins);
- clock::destroy_for_testing(_clock);
- test_scenario::end(scenario);
- }
- #[test]
- #[expected_failure(abort_code = wormhole::vaa::E_WRONG_VERSION)]
- fun test_create_price_feeds_corrupt_vaa() {
- let (scenario, test_coins, clock) = setup_test(500, 23, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", vector[], 50, 0);
- test_scenario::next_tx(&mut scenario, DEPLOYER);
- let pyth_state = take_shared<PythState>(&scenario);
- let worm_state = take_shared<WormState>(&scenario);
- // Pass in a corrupt VAA, which should fail deseriaizing
- let corrupt_vaa = x"90F8bf6A479f320ead074411a4B0e7944Ea8c9C1";
- // Create Pyth price feed
- pyth::create_price_feeds(
- &mut worm_state,
- &mut pyth_state,
- vector[corrupt_vaa],
- &clock,
- ctx(&mut scenario)
- );
- return_shared(pyth_state);
- return_shared(worm_state);
- clock::destroy_for_testing(clock);
- coin::burn_for_testing<SUI>(test_coins);
- test_scenario::end(scenario);
- }
- #[test]
- #[expected_failure(abort_code = pyth::pyth::E_INVALID_DATA_SOURCE)]
- fun test_create_price_feeds_invalid_data_source() {
- // Initialize the contract with some valid data sources, excluding our test VAA's source
- let data_sources = vector<DataSource>[
- data_source::new(
- 4, external_address::new(bytes32::new(x"0000000000000000000000000000000000000000000000000000000000007742"))
- ),
- data_source::new(
- 5, external_address::new(bytes32::new(x"0000000000000000000000000000000000000000000000000000000000007637"))
- )
- ];
- let (scenario, test_coins, clock) = setup_test(500, 23, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", data_sources, 50, 0);
- test_scenario::next_tx(&mut scenario, DEPLOYER);
- let pyth_state = take_shared<PythState>(&scenario);
- let worm_state = take_shared<WormState>(&scenario);
- pyth::create_price_feeds(
- &mut worm_state,
- &mut pyth_state,
- TEST_VAAS,
- &clock,
- ctx(&mut scenario)
- );
- return_shared(pyth_state);
- return_shared(worm_state);
- clock::destroy_for_testing(clock);
- coin::burn_for_testing<SUI>(test_coins);
- test_scenario::end(scenario);
- }
- #[test_only]
- fun data_sources_for_test_vaa(): vector<DataSource> {
- // Set some valid data sources, including our test VAA's source
- vector<DataSource>[
- data_source::new(
- 1, external_address::new(bytes32::from_bytes(x"0000000000000000000000000000000000000000000000000000000000000004"))),
- data_source::new(
- 5, external_address::new(bytes32::new(x"0000000000000000000000000000000000000000000000000000000000007637"))),
- data_source::new(
- 17, external_address::new(bytes32::new(x"71f8dcb863d176e2c420ad6610cf687359612b6fb392e0642b0ca6b1f186aa3b")))
- ]
- }
- #[test]
- fun test_create_and_update_price_feeds_success() {
- let data_sources = data_sources_for_test_vaa();
- let base_update_fee = 50;
- let coins_to_mint = 5000;
- let (scenario, test_coins, clock) = setup_test(500, 23, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", data_sources, base_update_fee, coins_to_mint);
- test_scenario::next_tx(&mut scenario, DEPLOYER);
- let pyth_state = take_shared<PythState>(&scenario);
- let worm_state = take_shared<WormState>(&scenario);
- pyth::create_price_feeds(
- &mut worm_state,
- &mut pyth_state,
- TEST_VAAS,
- &clock,
- ctx(&mut scenario)
- );
- // Affirm that 4 objects, which correspond to the 4 new price info objects
- // containing the price feeds were created and shared.
- let effects = test_scenario::next_tx(&mut scenario, DEPLOYER);
- let shared_ids = test_scenario::shared(&effects);
- let created_ids = test_scenario::created(&effects);
- assert!(vector::length<ID>(&shared_ids)==4, 0);
- assert!(vector::length<ID>(&created_ids)==4, 0);
- let price_info_object_1 = take_shared<PriceInfoObject>(&scenario);
- let price_info_object_2 = take_shared<PriceInfoObject>(&scenario);
- let price_info_object_3 = take_shared<PriceInfoObject>(&scenario);
- let price_info_object_4 = take_shared<PriceInfoObject>(&scenario);
- // Create vector of price info objects (Sui objects with key ability and living in global store),
- // which contain the price feeds we want to update. Note that these can be passed into
- // update_price_feeds in any order!
- let price_info_object_vec = vector[price_info_object_1, price_info_object_2, price_info_object_3, price_info_object_4];
- pyth::update_price_feeds(
- &mut worm_state,
- &mut pyth_state,
- TEST_VAAS,
- &mut price_info_object_vec,
- test_coins,
- &clock
- );
- price_info_object_4 = vector::pop_back(&mut price_info_object_vec);
- price_info_object_3 = vector::pop_back(&mut price_info_object_vec);
- price_info_object_2 = vector::pop_back(&mut price_info_object_vec);
- price_info_object_1 = vector::pop_back(&mut price_info_object_vec);
- vector::destroy_empty(price_info_object_vec);
- return_shared(pyth_state);
- return_shared(worm_state);
- return_shared(price_info_object_1);
- return_shared(price_info_object_2);
- return_shared(price_info_object_3);
- return_shared(price_info_object_4);
- clock::destroy_for_testing(clock);
- test_scenario::end(scenario);
- }
- #[test]
- #[expected_failure(abort_code = pyth::pyth::E_PRICE_INFO_OBJECT_NOT_FOUND)]
- fun test_create_and_update_price_feeds_price_info_object_not_found_failure() {
- let data_sources = data_sources_for_test_vaa();
- let base_update_fee = 50;
- let coins_to_mint = 5000;
- let (scenario, test_coins, clock) = setup_test(500, 23, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", data_sources, base_update_fee, coins_to_mint);
- test_scenario::next_tx(&mut scenario, DEPLOYER);
- let pyth_state = take_shared<PythState>(&scenario);
- let worm_state = take_shared<WormState>(&scenario);
- pyth::create_price_feeds(
- &mut worm_state,
- &mut pyth_state,
- TEST_VAAS,
- &clock,
- ctx(&mut scenario)
- );
- // Affirm that 4 objects, which correspond to the 4 new price info objects
- // containing the price feeds were created and shared.
- let effects = test_scenario::next_tx(&mut scenario, DEPLOYER);
- let shared_ids = test_scenario::shared(&effects);
- let created_ids = test_scenario::created(&effects);
- assert!(vector::length<ID>(&shared_ids)==4, 0);
- assert!(vector::length<ID>(&created_ids)==4, 0);
- let price_info_object_1 = take_shared<PriceInfoObject>(&scenario);
- let price_info_object_2 = take_shared<PriceInfoObject>(&scenario);
- let price_info_object_3 = take_shared<PriceInfoObject>(&scenario);
- let price_info_object_4 = take_shared<PriceInfoObject>(&scenario);
- // Note that here we only pass in 3 price info objects corresponding to 3 out
- // of the 4 price feeds.
- let price_info_object_vec = vector[price_info_object_1, price_info_object_2, price_info_object_3];
- pyth::update_price_feeds(
- &mut worm_state,
- &mut pyth_state,
- TEST_VAAS,
- &mut price_info_object_vec,
- test_coins,
- &clock
- );
- price_info_object_3 = vector::pop_back(&mut price_info_object_vec);
- price_info_object_2 = vector::pop_back(&mut price_info_object_vec);
- price_info_object_1 = vector::pop_back(&mut price_info_object_vec);
- vector::destroy_empty(price_info_object_vec);
- return_shared(pyth_state);
- return_shared(worm_state);
- return_shared(price_info_object_1);
- return_shared(price_info_object_2);
- return_shared(price_info_object_3);
- return_shared(price_info_object_4);
- return_shared(clock);
- test_scenario::end(scenario);
- }
- #[test]
- #[expected_failure(abort_code = pyth::pyth::E_INSUFFICIENT_FEE)]
- fun test_create_and_update_price_feeds_insufficient_fee() {
- let data_sources = data_sources_for_test_vaa();
- let base_update_fee = 50;
- let coins_to_mint = 5;
- let (scenario, test_coins, clock) = setup_test(500, 23, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", data_sources, base_update_fee, coins_to_mint);
- test_scenario::next_tx(&mut scenario, DEPLOYER);
- let pyth_state = take_shared<PythState>(&scenario);
- let worm_state = take_shared<WormState>(&scenario);
- pyth::create_price_feeds(
- &mut worm_state,
- &mut pyth_state,
- TEST_VAAS,
- &clock,
- ctx(&mut scenario)
- );
- test_scenario::next_tx(&mut scenario, DEPLOYER);
- let price_info_object = take_shared<PriceInfoObject>(&scenario);
- let price_info_object_vec = vector[price_info_object];
- pyth::update_price_feeds(
- &mut worm_state,
- &mut pyth_state,
- TEST_VAAS,
- &mut price_info_object_vec,
- test_coins,
- &clock
- );
- price_info_object = vector::pop_back(&mut price_info_object_vec);
- vector::destroy_empty(price_info_object_vec);
- return_shared(pyth_state);
- return_shared(worm_state);
- return_shared(price_info_object);
- return_shared(clock);
- test_scenario::end(scenario);
- }
- #[test]
- fun test_update_cache(){
- let data_sources = data_sources_for_test_vaa();
- let base_update_fee = 50;
- let coins_to_mint = 5000;
- let (scenario, test_coins, clock) = setup_test(500, 23, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", data_sources, base_update_fee, coins_to_mint);
- test_scenario::next_tx(&mut scenario, DEPLOYER);
- let pyth_state = take_shared<PythState>(&scenario);
- let worm_state = take_shared<WormState>(&scenario);
- // Update cache is called by create_price_feeds.
- pyth::create_price_feeds(
- &mut worm_state,
- &mut pyth_state,
- TEST_VAAS,
- &clock,
- ctx(&mut scenario)
- );
- test_scenario::next_tx(&mut scenario, DEPLOYER);
- let price_info_object_1 = take_shared<PriceInfoObject>(&scenario);
- let price_info_object_2 = take_shared<PriceInfoObject>(&scenario);
- let price_info_object_3 = take_shared<PriceInfoObject>(&scenario);
- let price_info_object_4 = take_shared<PriceInfoObject>(&scenario);
- // These updates are price infos that correspond to the ones in TEST_VAAS.
- let updates = get_mock_price_infos();
- let price_info_object_vec = vector[
- price_info_object_1,
- price_info_object_2,
- price_info_object_3,
- price_info_object_4
- ];
- // Check that TEST_VAAS was indeed used to instantiate the price feeds correctly,
- // by confirming that the info in updates is contained in price_info_object_vec.
- check_price_feeds_cached(&updates, &price_info_object_vec);
- price_info_object_4 = vector::pop_back(&mut price_info_object_vec);
- price_info_object_3 = vector::pop_back(&mut price_info_object_vec);
- price_info_object_2 = vector::pop_back(&mut price_info_object_vec);
- price_info_object_1 = vector::pop_back(&mut price_info_object_vec);
- vector::destroy_empty(price_info_object_vec);
- return_shared(pyth_state);
- return_shared(worm_state);
- return_shared(price_info_object_1);
- return_shared(price_info_object_2);
- return_shared(price_info_object_3);
- return_shared(price_info_object_4);
- coin::burn_for_testing<SUI>(test_coins);
- clock::destroy_for_testing(clock);
- test_scenario::end(scenario);
- }
- #[test]
- fun test_update_cache_old_update() {
- let data_sources = data_sources_for_test_vaa();
- let base_update_fee = 50;
- let coins_to_mint = 5000;
- let (scenario, test_coins, clock) = setup_test(500, 23, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", data_sources, base_update_fee, coins_to_mint);
- test_scenario::next_tx(&mut scenario, DEPLOYER);
- let pyth_state = take_shared<PythState>(&scenario);
- let worm_state = take_shared<WormState>(&scenario);
- pyth::create_price_feeds(
- &mut worm_state,
- &mut pyth_state,
- TEST_VAAS,
- &clock,
- ctx(&mut scenario)
- );
- test_scenario::next_tx(&mut scenario, DEPLOYER);
- let price_info_object_1 = take_shared<PriceInfoObject>(&scenario);
- let price_info_object_2 = take_shared<PriceInfoObject>(&scenario);
- let price_info_object_3 = take_shared<PriceInfoObject>(&scenario);
- let price_info_object_4 = take_shared<PriceInfoObject>(&scenario);
- let price_info_object_vec = vector[
- price_info_object_1,
- price_info_object_2,
- price_info_object_3,
- price_info_object_4
- ];
- // Hardcode the price identifier, price, and ema_price for price_info_object_1, because
- // it's easier than unwrapping price_info_object_1 and getting the quantities via getters.
- let timestamp = 1663680740;
- let price_identifier = price_identifier::from_byte_vec(x"c6c75c89f14810ec1c54c03ab8f1864a4c4032791f05747f560faec380a695d1");
- let price = price::new(i64::new(1557, false), 7, i64::new(5, true), timestamp);
- let ema_price = price::new(i64::new(1500, false), 3, i64::new(5, true), timestamp);
- // Attempt to update the price with an update older than the current cached one.
- let old_price = price::new(i64::new(1243, true), 9802, i64::new(6, false), timestamp - 200);
- let old_ema_price = price::new(i64::new(8976, true), 234, i64::new(897, false), timestamp - 200);
- let old_update = price_info::new_price_info(
- 1257278600,
- 1690226180,
- price_feed::new(
- price_identifier,
- old_price,
- old_ema_price,
- )
- );
- pyth::update_cache(vector<PriceInfo>[old_update], &mut price_info_object_vec, &clock);
- price_info_object_4 = vector::pop_back(&mut price_info_object_vec);
- price_info_object_3 = vector::pop_back(&mut price_info_object_vec);
- price_info_object_2 = vector::pop_back(&mut price_info_object_vec);
- price_info_object_1 = vector::pop_back(&mut price_info_object_vec);
- vector::destroy_empty(price_info_object_vec);
-
- let current_price_info = price_info::get_price_info_from_price_info_object(&price_info_object_1);
- let current_price_feed = price_info::get_price_feed(¤t_price_info);
- let current_price = price_feed::get_price(current_price_feed);
- let current_ema_price = price_feed::get_ema_price(current_price_feed);
- // Confirm that no price update occurred when we tried to update cache with an
- // outdated update: old_update.
- assert!(current_price == price, 1);
- assert!(current_ema_price == ema_price, 1);
- test_scenario::next_tx(&mut scenario, DEPLOYER);
- // Update the cache with a fresh update.
- let fresh_price = price::new(i64::new(5243, true), 2, i64::new(3, false), timestamp + 200);
- let fresh_ema_price = price::new(i64::new(8976, true), 21, i64::new(32, false), timestamp + 200);
- let fresh_update = price_info::new_price_info(
- 1257278600,
- 1690226180,
- price_feed::new(
- price_identifier,
- fresh_price,
- fresh_ema_price,
- )
- );
- let price_info_object_vec = vector[
- price_info_object_1,
- price_info_object_2,
- price_info_object_3,
- price_info_object_4
- ];
- pyth::update_cache(vector<PriceInfo>[fresh_update], &mut price_info_object_vec, &clock);
- price_info_object_4 = vector::pop_back(&mut price_info_object_vec);
- price_info_object_3 = vector::pop_back(&mut price_info_object_vec);
- price_info_object_2 = vector::pop_back(&mut price_info_object_vec);
- price_info_object_1 = vector::pop_back(&mut price_info_object_vec);
- vector::destroy_empty(price_info_object_vec);
- // Confirm that the Pyth cached price got updated to fresh_price.
- let current_price_info = price_info::get_price_info_from_price_info_object(&price_info_object_1);
- let current_price_feed = price_info::get_price_feed(¤t_price_info);
- let current_price = price_feed::get_price(current_price_feed);
- let current_ema_price = price_feed::get_ema_price(current_price_feed);
- assert!(current_price==fresh_price, 0);
- assert!(current_ema_price==fresh_ema_price, 0);
- return_shared(pyth_state);
- return_shared(worm_state);
- return_shared(price_info_object_1);
- return_shared(price_info_object_2);
- return_shared(price_info_object_3);
- return_shared(price_info_object_4);
- coin::burn_for_testing<SUI>(test_coins);
- clock::destroy_for_testing(clock);
- test_scenario::end(scenario);
- }
- }
- // TODO - pyth tests
- // https://github.com/pyth-network/pyth-crosschain/blob/main/target_chains/aptos/contracts/sources/pyth.move#L384
|