pyth.move 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  1. module pyth::pyth {
  2. use std::vector;
  3. use sui::tx_context::{TxContext};
  4. use sui::coin::{Self, Coin};
  5. use sui::sui::{SUI};
  6. use sui::transfer::{Self};
  7. use sui::clock::{Self, Clock};
  8. use sui::package::{UpgradeCap};
  9. use pyth::event::{Self as pyth_event};
  10. use pyth::data_source::{Self, DataSource};
  11. use pyth::state::{Self as state, State as PythState, LatestOnly};
  12. use pyth::price_info::{Self, PriceInfo, PriceInfoObject};
  13. use pyth::batch_price_attestation::{Self};
  14. use pyth::price_feed::{Self};
  15. use pyth::price::{Self, Price};
  16. use pyth::price_identifier::{PriceIdentifier};
  17. use pyth::setup::{Self, DeployerCap};
  18. use pyth::hot_potato_vector::{Self, HotPotatoVector};
  19. use wormhole::external_address::{Self};
  20. use wormhole::vaa::{Self, VAA};
  21. use wormhole::bytes32::{Self};
  22. const E_DATA_SOURCE_EMITTER_ADDRESS_AND_CHAIN_IDS_DIFFERENT_LENGTHS: u64 = 0;
  23. const E_INVALID_DATA_SOURCE: u64 = 1;
  24. const E_INSUFFICIENT_FEE: u64 = 2;
  25. const E_STALE_PRICE_UPDATE: u64 = 3;
  26. const E_UPDATE_AND_PRICE_INFO_OBJECT_MISMATCH: u64 = 4;
  27. const E_PRICE_UPDATE_NOT_FOUND_FOR_PRICE_INFO_OBJECT: u64 = 5;
  28. #[test_only]
  29. friend pyth::pyth_tests;
  30. /// Init state and emit event corresponding to Pyth initialization.
  31. public entry fun init_pyth(
  32. deployer: DeployerCap,
  33. upgrade_cap: UpgradeCap,
  34. stale_price_threshold: u64,
  35. governance_emitter_chain_id: u64,
  36. governance_emitter_address: vector<u8>,
  37. data_sources_emitter_chain_ids: vector<u64>,
  38. data_sources_emitter_addresses: vector<vector<u8>>,
  39. update_fee: u64,
  40. ctx: &mut TxContext
  41. ) {
  42. setup::init_and_share_state(
  43. deployer,
  44. upgrade_cap,
  45. stale_price_threshold,
  46. update_fee,
  47. data_source::new(
  48. governance_emitter_chain_id,
  49. external_address::new((bytes32::from_bytes(governance_emitter_address)))
  50. ),
  51. parse_data_sources(
  52. data_sources_emitter_chain_ids,
  53. data_sources_emitter_addresses,
  54. ),
  55. ctx
  56. );
  57. // Emit Pyth initialization event.
  58. pyth_event::emit_pyth_initialization_event();
  59. }
  60. fun parse_data_sources(
  61. emitter_chain_ids: vector<u64>,
  62. emitter_addresses: vector<vector<u8>>
  63. ): vector<DataSource> {
  64. assert!(vector::length(&emitter_chain_ids) == vector::length(&emitter_addresses),
  65. E_DATA_SOURCE_EMITTER_ADDRESS_AND_CHAIN_IDS_DIFFERENT_LENGTHS);
  66. let sources = vector::empty();
  67. let i = 0;
  68. while (i < vector::length(&emitter_chain_ids)) {
  69. vector::push_back(&mut sources, data_source::new(
  70. *vector::borrow(&emitter_chain_ids, i),
  71. external_address::new(bytes32::from_bytes(*vector::borrow(&emitter_addresses, i)))
  72. ));
  73. i = i + 1;
  74. };
  75. sources
  76. }
  77. /// Create and share new price feed objects if they don't already exist.
  78. public fun create_price_feeds(
  79. pyth_state: &mut PythState,
  80. // These vaas have been verified and consumed, so we don't have to worry about
  81. // doing replay protection for them.
  82. verified_vaas: vector<VAA>,
  83. clock: &Clock,
  84. ctx: &mut TxContext
  85. ){
  86. // This capability ensures that the current build version is used.
  87. let latest_only = state::assert_latest_only(pyth_state);
  88. while (!vector::is_empty(&verified_vaas)) {
  89. let vaa = vector::pop_back(&mut verified_vaas);
  90. // Check that the VAA is from a valid data source (emitter)
  91. assert!(
  92. state::is_valid_data_source(
  93. pyth_state,
  94. data_source::new(
  95. (vaa::emitter_chain(&vaa) as u64),
  96. vaa::emitter_address(&vaa))
  97. ),
  98. E_INVALID_DATA_SOURCE
  99. );
  100. // Deserialize the batch price attestation
  101. let price_infos = batch_price_attestation::destroy(batch_price_attestation::deserialize(vaa::take_payload(vaa), clock));
  102. while (!vector::is_empty(&price_infos)){
  103. let cur_price_info = vector::pop_back(&mut price_infos);
  104. // Only create new Sui PriceInfoObject if not already
  105. // registered with the Pyth State object.
  106. if (!state::price_feed_object_exists(
  107. pyth_state,
  108. price_feed::get_price_identifier(
  109. price_info::get_price_feed(&cur_price_info)
  110. )
  111. )
  112. ){
  113. // Create and share newly created Sui PriceInfoObject containing a price feed,
  114. // and then register a copy of its ID with State.
  115. let new_price_info_object = price_info::new_price_info_object(cur_price_info, ctx);
  116. let price_identifier = price_info::get_price_identifier(&cur_price_info);
  117. let id = price_info::uid_to_inner(&new_price_info_object);
  118. state::register_price_info_object(&latest_only, pyth_state, price_identifier, id);
  119. transfer::public_share_object(new_price_info_object);
  120. }
  121. }
  122. };
  123. vector::destroy_empty(verified_vaas);
  124. }
  125. public fun create_price_infos_hot_potato(
  126. pyth_state: &PythState,
  127. verified_vaas: vector<VAA>,
  128. clock: &Clock
  129. ): HotPotatoVector<PriceInfo> {
  130. let _ = state::assert_latest_only(pyth_state);
  131. let price_updates = vector::empty<PriceInfo>();
  132. while (vector::length(&verified_vaas) != 0){
  133. let cur_vaa = vector::pop_back(&mut verified_vaas);
  134. assert!(
  135. state::is_valid_data_source(
  136. pyth_state,
  137. data_source::new(
  138. (vaa::emitter_chain(&cur_vaa) as u64),
  139. vaa::emitter_address(&cur_vaa))
  140. ),
  141. E_INVALID_DATA_SOURCE
  142. );
  143. let price_infos = batch_price_attestation::destroy(batch_price_attestation::deserialize(vaa::take_payload(cur_vaa), clock));
  144. while (vector::length(&price_infos) !=0 ){
  145. let cur_price_info = vector::pop_back(&mut price_infos);
  146. vector::push_back(&mut price_updates, cur_price_info);
  147. }
  148. };
  149. vector::destroy_empty(verified_vaas);
  150. return hot_potato_vector::new(price_updates)
  151. }
  152. /// Update a singular Pyth PriceInfoObject (containing a price feed) with the
  153. /// price data in the given hot potato vector (a vector of PriceInfo objects).
  154. ///
  155. /// The javascript https://github.com/pyth-network/pyth-js/tree/main/pyth-sui-js package
  156. /// should be used to fetch these VAAs from the Price Service. More information about this
  157. /// process can be found at https://docs.pyth.network/consume-data.
  158. ///
  159. /// The given fee must contain a sufficient number of coins to pay the update fee for the given vaas.
  160. /// The update fee amount can be queried by calling get_update_fee(&vaas).
  161. ///
  162. /// Please read more information about the update fee here: https://docs.pyth.network/consume-data/on-demand#fees
  163. public fun update_single_price_feed(
  164. pyth_state: &PythState,
  165. price_updates: HotPotatoVector<PriceInfo>,
  166. price_info_object: &mut PriceInfoObject,
  167. fee: Coin<SUI>,
  168. clock: &Clock
  169. ): HotPotatoVector<PriceInfo> {
  170. let latest_only = state::assert_latest_only(pyth_state);
  171. // Since we charge the base update fee per 5 price updates, here we check that
  172. // Coin Value >= update_fee / 5 (we only update a single price feed in this function).
  173. assert!(state::get_base_update_fee(pyth_state) <= 5 * coin::value(&fee), E_INSUFFICIENT_FEE);
  174. transfer::public_transfer(fee, state::get_fee_recipient(pyth_state));
  175. // Find price update corresponding to PriceInfoObject within the array of price_updates
  176. // and use it to update PriceInfoObject.
  177. let i = 0;
  178. let found = false;
  179. while (i < hot_potato_vector::length<PriceInfo>(&price_updates)){
  180. let cur_price_info = hot_potato_vector::borrow<PriceInfo>(&price_updates, i);
  181. if (has_same_price_identifier(cur_price_info, price_info_object)){
  182. found = true;
  183. update_cache(latest_only, cur_price_info, price_info_object, clock);
  184. break
  185. };
  186. i = i + 1;
  187. };
  188. if (found==false){
  189. abort E_PRICE_UPDATE_NOT_FOUND_FOR_PRICE_INFO_OBJECT
  190. };
  191. price_updates
  192. }
  193. fun has_same_price_identifier(price_info: &PriceInfo, price_info_object: &PriceInfoObject) : bool {
  194. let price_info_from_object = price_info::get_price_info_from_price_info_object(price_info_object);
  195. let price_identifier_from_object = price_info::get_price_identifier(&price_info_from_object);
  196. let price_identifier_from_price_info = price_info::get_price_identifier(price_info);
  197. price_identifier_from_object == price_identifier_from_price_info
  198. }
  199. /// Update PriceInfoObject with updated data from a PriceInfo
  200. public(friend) fun update_cache(
  201. _: LatestOnly,
  202. update: &PriceInfo,
  203. price_info_object: &mut PriceInfoObject,
  204. clock: &Clock,
  205. ){
  206. let has_same_price_identifier = has_same_price_identifier(update, price_info_object);
  207. assert!(has_same_price_identifier, E_UPDATE_AND_PRICE_INFO_OBJECT_MISMATCH);
  208. // Update the price info object with the new updated price info.
  209. if (is_fresh_update(update, price_info_object)){
  210. pyth_event::emit_price_feed_update(price_feed::from(price_info::get_price_feed(update)), clock::timestamp_ms(clock)/1000);
  211. price_info::update_price_info_object(
  212. price_info_object,
  213. update
  214. );
  215. }
  216. }
  217. /// Determine if the given price update is "fresh": we have nothing newer already cached for that
  218. /// price feed within a PriceInfoObject.
  219. fun is_fresh_update(update: &PriceInfo, price_info_object: &PriceInfoObject): bool {
  220. // Get the timestamp of the update's current price
  221. let price_feed = price_info::get_price_feed(update);
  222. let update_timestamp = price::get_timestamp(&price_feed::get_price(price_feed));
  223. // Get the timestamp of the cached data for the price identifier
  224. let cached_price_info = price_info::get_price_info_from_price_info_object(price_info_object);
  225. let cached_price_feed = price_info::get_price_feed(&cached_price_info);
  226. let cached_timestamp = price::get_timestamp(&price_feed::get_price(cached_price_feed));
  227. update_timestamp > cached_timestamp
  228. }
  229. // -----------------------------------------------------------------------------
  230. // Query the cached prices
  231. //
  232. // It is strongly recommended to update the cached prices using the functions above,
  233. // before using the functions below to query the cached data.
  234. /// Determine if a price feed for the given price_identifier exists
  235. public fun price_feed_exists(state: &PythState, price_identifier: PriceIdentifier): bool {
  236. state::price_feed_object_exists(state, price_identifier)
  237. }
  238. /// Get the latest available price cached for the given price identifier, if that price is
  239. /// no older than the stale price threshold.
  240. ///
  241. /// Please refer to the documentation at https://docs.pyth.network/consumers/best-practices for
  242. /// how to how this price safely.
  243. ///
  244. /// Important: Pyth uses an on-demand update model, where consumers need to update the
  245. /// cached prices before using them. Please read more about this at https://docs.pyth.network/consume-data/on-demand.
  246. /// get_price() is likely to abort unless you call update_price_feeds() to update the cached price
  247. /// beforehand, as the cached prices may be older than the stale price threshold.
  248. ///
  249. /// The price_info_object is a Sui object with the key ability that uniquely
  250. /// contains a price feed for a given price_identifier.
  251. ///
  252. public fun get_price(state: &PythState, price_info_object: &PriceInfoObject, clock: &Clock): Price {
  253. get_price_no_older_than(price_info_object, clock, state::get_stale_price_threshold_secs(state))
  254. }
  255. /// Get the latest available price cached for the given price identifier, if that price is
  256. /// no older than the given age.
  257. public fun get_price_no_older_than(price_info_object: &PriceInfoObject, clock: &Clock, max_age_secs: u64): Price {
  258. let price = get_price_unsafe(price_info_object);
  259. check_price_is_fresh(&price, clock, max_age_secs);
  260. price
  261. }
  262. /// Get the latest available price cached for the given price identifier.
  263. ///
  264. /// WARNING: the returned price can be from arbitrarily far in the past.
  265. /// This function makes no guarantees that the returned price is recent or
  266. /// useful for any particular application. Users of this function should check
  267. /// the returned timestamp to ensure that the returned price is sufficiently
  268. /// recent for their application. The checked get_price_no_older_than()
  269. /// function should be used in preference to this.
  270. public fun get_price_unsafe(price_info_object: &PriceInfoObject): Price {
  271. // TODO: extract Price from this guy...
  272. let price_info = price_info::get_price_info_from_price_info_object(price_info_object);
  273. price_feed::get_price(
  274. price_info::get_price_feed(&price_info)
  275. )
  276. }
  277. fun abs_diff(x: u64, y: u64): u64 {
  278. if (x > y) {
  279. return x - y
  280. } else {
  281. return y - x
  282. }
  283. }
  284. /// Get the stale price threshold: the amount of time after which a cached price
  285. /// is considered stale and no longer returned by get_price()/get_ema_price().
  286. public fun get_stale_price_threshold_secs(state: &PythState): u64 {
  287. state::get_stale_price_threshold_secs(state)
  288. }
  289. fun check_price_is_fresh(price: &Price, clock: &Clock, max_age_secs: u64) {
  290. let age = abs_diff(clock::timestamp_ms(clock)/1000, price::get_timestamp(price));
  291. assert!(age < max_age_secs, E_STALE_PRICE_UPDATE);
  292. }
  293. /// Please read more information about the update fee here: https://docs.pyth.network/consume-data/on-demand#fees
  294. public fun get_total_update_fee(pyth_state: &PythState, n: u64): u64 {
  295. state::get_base_update_fee(pyth_state) * n
  296. }
  297. }
  298. #[test_only]
  299. module pyth::pyth_tests{
  300. use std::vector::{Self};
  301. use sui::sui::SUI;
  302. use sui::coin::{Self, Coin};
  303. use sui::test_scenario::{Self, Scenario, ctx, take_shared, return_shared};
  304. use sui::package::Self;
  305. use sui::object::{Self, ID};
  306. use sui::clock::{Self, Clock};
  307. use pyth::state::{State as PythState};
  308. use pyth::setup::{Self};
  309. //use pyth::price_identifier::{Self};
  310. use pyth::price_info::{PriceInfo, PriceInfoObject};//, PriceInfo, PriceInfoObject};
  311. //use pyth::price_feed::{Self};
  312. use pyth::data_source::{Self, DataSource};
  313. //use pyth::i64::{Self};
  314. //use pyth::price::{Self};
  315. use pyth::pyth::{Self, create_price_infos_hot_potato, update_single_price_feed};
  316. use pyth::hot_potato_vector::{Self};
  317. use wormhole::setup::{Self as wormhole_setup, DeployerCap};
  318. use wormhole::external_address::{Self};
  319. use wormhole::bytes32::{Self};
  320. use wormhole::state::{State as WormState};
  321. use wormhole::vaa::{Self, VAA};
  322. const DEPLOYER: address = @0x1234;
  323. #[test_only]
  324. // /// A vector containing a single VAA with:
  325. // /// - emitter chain ID 17
  326. // /// - emitter address 0x71f8dcb863d176e2c420ad6610cf687359612b6fb392e0642b0ca6b1f186aa3b
  327. // /// - payload corresponding to the batch price attestation of the prices returned by get_mock_price_infos()
  328. const TEST_VAAS: vector<vector<u8>> = vector[x"0100000000010036eb563b80a24f4253bee6150eb8924e4bdf6e4fa1dfc759a6664d2e865b4b134651a7b021b7f1ce3bd078070b688b6f2e37ce2de0d9b48e6a78684561e49d5201527e4f9b00000001001171f8dcb863d176e2c420ad6610cf687359612b6fb392e0642b0ca6b1f186aa3b0000000000000001005032574800030000000102000400951436e0be37536be96f0896366089506a59763d036728332d3e3038047851aea7c6c75c89f14810ec1c54c03ab8f1864a4c4032791f05747f560faec380a695d1000000000000049a0000000000000008fffffffb00000000000005dc0000000000000003000000000100000001000000006329c0eb000000006329c0e9000000006329c0e400000000000006150000000000000007215258d81468614f6b7e194c5d145609394f67b041e93e6695dcc616faadd0603b9551a68d01d954d6387aff4df1529027ffb2fee413082e509feb29cc4904fe000000000000041a0000000000000003fffffffb00000000000005cb0000000000000003010000000100000001000000006329c0eb000000006329c0e9000000006329c0e4000000000000048600000000000000078ac9cf3ab299af710d735163726fdae0db8465280502eb9f801f74b3c1bd190333832fad6e36eb05a8972fe5f219b27b5b2bb2230a79ce79beb4c5c5e7ecc76d00000000000003f20000000000000002fffffffb00000000000005e70000000000000003010000000100000001000000006329c0eb000000006329c0e9000000006329c0e40000000000000685000000000000000861db714e9ff987b6fedf00d01f9fea6db7c30632d6fc83b7bc9459d7192bc44a21a28b4c6619968bd8c20e95b0aaed7df2187fd310275347e0376a2cd7427db800000000000006cb0000000000000001fffffffb00000000000005e40000000000000003010000000100000001000000006329c0eb000000006329c0e9000000006329c0e400000000000007970000000000000001"];
  329. fun get_verified_test_vaas(worm_state: &WormState, clock: &Clock): vector<VAA> {
  330. let test_vaas_: vector<vector<u8>> = vector[x"0100000000010036eb563b80a24f4253bee6150eb8924e4bdf6e4fa1dfc759a6664d2e865b4b134651a7b021b7f1ce3bd078070b688b6f2e37ce2de0d9b48e6a78684561e49d5201527e4f9b00000001001171f8dcb863d176e2c420ad6610cf687359612b6fb392e0642b0ca6b1f186aa3b0000000000000001005032574800030000000102000400951436e0be37536be96f0896366089506a59763d036728332d3e3038047851aea7c6c75c89f14810ec1c54c03ab8f1864a4c4032791f05747f560faec380a695d1000000000000049a0000000000000008fffffffb00000000000005dc0000000000000003000000000100000001000000006329c0eb000000006329c0e9000000006329c0e400000000000006150000000000000007215258d81468614f6b7e194c5d145609394f67b041e93e6695dcc616faadd0603b9551a68d01d954d6387aff4df1529027ffb2fee413082e509feb29cc4904fe000000000000041a0000000000000003fffffffb00000000000005cb0000000000000003010000000100000001000000006329c0eb000000006329c0e9000000006329c0e4000000000000048600000000000000078ac9cf3ab299af710d735163726fdae0db8465280502eb9f801f74b3c1bd190333832fad6e36eb05a8972fe5f219b27b5b2bb2230a79ce79beb4c5c5e7ecc76d00000000000003f20000000000000002fffffffb00000000000005e70000000000000003010000000100000001000000006329c0eb000000006329c0e9000000006329c0e40000000000000685000000000000000861db714e9ff987b6fedf00d01f9fea6db7c30632d6fc83b7bc9459d7192bc44a21a28b4c6619968bd8c20e95b0aaed7df2187fd310275347e0376a2cd7427db800000000000006cb0000000000000001fffffffb00000000000005e40000000000000003010000000100000001000000006329c0eb000000006329c0e9000000006329c0e400000000000007970000000000000001"];
  331. let verified_vaas_reversed = vector::empty<VAA>();
  332. let test_vaas = test_vaas_;
  333. let i = 0;
  334. while (i < vector::length(&test_vaas_)) {
  335. let cur_test_vaa = vector::pop_back(&mut test_vaas);
  336. let verified_vaa = vaa::parse_and_verify(worm_state, cur_test_vaa, clock);
  337. vector::push_back(&mut verified_vaas_reversed, verified_vaa);
  338. i=i+1;
  339. };
  340. let verified_vaas = vector::empty<VAA>();
  341. while (vector::length<VAA>(&verified_vaas_reversed)!=0){
  342. let cur = vector::pop_back(&mut verified_vaas_reversed);
  343. vector::push_back(&mut verified_vaas, cur);
  344. };
  345. vector::destroy_empty(verified_vaas_reversed);
  346. verified_vaas
  347. }
  348. #[test_only]
  349. /// Init Wormhole core bridge state.
  350. /// Init Pyth state.
  351. /// Set initial Sui clock time.
  352. /// Mint some SUI fee coins.
  353. fun setup_test(
  354. stale_price_threshold: u64,
  355. governance_emitter_chain_id: u64,
  356. governance_emitter_address: vector<u8>,
  357. data_sources: vector<DataSource>,
  358. base_update_fee: u64,
  359. to_mint: u64
  360. ): (Scenario, Coin<SUI>, Clock) {
  361. let scenario = test_scenario::begin(DEPLOYER);
  362. // Initialize Wormhole core bridge.
  363. wormhole_setup::init_test_only(ctx(&mut scenario));
  364. test_scenario::next_tx(&mut scenario, DEPLOYER);
  365. // Take the `DeployerCap` from the sender of the transaction.
  366. let deployer_cap =
  367. test_scenario::take_from_address<DeployerCap>(
  368. &scenario,
  369. DEPLOYER
  370. );
  371. // This will be created and sent to the transaction sender automatically
  372. // when the contract is published. This exists in place of grabbing
  373. // it from the sender.
  374. let upgrade_cap =
  375. package::test_publish(
  376. object::id_from_address(@wormhole),
  377. test_scenario::ctx(&mut scenario)
  378. );
  379. let governance_chain = 1234;
  380. let governance_contract =
  381. x"deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef";
  382. let initial_guardians =
  383. vector[
  384. x"beFA429d57cD18b7F8A4d91A2da9AB4AF05d0FBe"
  385. ];
  386. let guardian_set_seconds_to_live = 5678;
  387. let message_fee = 350;
  388. let guardian_set_index = 0;
  389. wormhole_setup::complete(
  390. deployer_cap,
  391. upgrade_cap,
  392. governance_chain,
  393. governance_contract,
  394. guardian_set_index,
  395. initial_guardians,
  396. guardian_set_seconds_to_live,
  397. message_fee,
  398. test_scenario::ctx(&mut scenario)
  399. );
  400. // Initialize Pyth state.
  401. let pyth_upgrade_cap=
  402. package::test_publish(
  403. object::id_from_address(@pyth),
  404. test_scenario::ctx(&mut scenario)
  405. );
  406. setup::init_test_only(ctx(&mut scenario));
  407. test_scenario::next_tx(&mut scenario, DEPLOYER);
  408. let pyth_deployer_cap = test_scenario::take_from_address<setup::DeployerCap>(
  409. &scenario,
  410. DEPLOYER
  411. );
  412. setup::init_and_share_state(
  413. pyth_deployer_cap,
  414. pyth_upgrade_cap,
  415. stale_price_threshold,
  416. base_update_fee,
  417. data_source::new(governance_emitter_chain_id, external_address::new(bytes32::from_bytes(governance_emitter_address))),
  418. data_sources,
  419. ctx(&mut scenario)
  420. );
  421. let coins = coin::mint_for_testing<SUI>(to_mint, ctx(&mut scenario));
  422. let clock = clock::create_for_testing(ctx(&mut scenario));
  423. (scenario, coins, clock)
  424. }
  425. // #[test_only]
  426. // fun get_mock_price_infos(): vector<PriceInfo> {
  427. // vector<PriceInfo>[
  428. // price_info::new_price_info(
  429. // 1663680747,
  430. // 1663074349,
  431. // price_feed::new(
  432. // price_identifier::from_byte_vec(x"c6c75c89f14810ec1c54c03ab8f1864a4c4032791f05747f560faec380a695d1"),
  433. // price::new(i64::new(1557, false), 7, i64::new(5, true), 1663680740),
  434. // price::new(i64::new(1500, false), 3, i64::new(5, true), 1663680740),
  435. // ),
  436. // ),
  437. // price_info::new_price_info(
  438. // 1663680747,
  439. // 1663074349,
  440. // price_feed::new(
  441. // price_identifier::from_byte_vec(x"3b9551a68d01d954d6387aff4df1529027ffb2fee413082e509feb29cc4904fe"),
  442. // price::new(i64::new(1050, false), 3, i64::new(5, true), 1663680745),
  443. // price::new(i64::new(1483, false), 3, i64::new(5, true), 1663680745),
  444. // ),
  445. // ),
  446. // price_info::new_price_info(
  447. // 1663680747,
  448. // 1663074349,
  449. // price_feed::new(
  450. // price_identifier::from_byte_vec(x"33832fad6e36eb05a8972fe5f219b27b5b2bb2230a79ce79beb4c5c5e7ecc76d"),
  451. // price::new(i64::new(1010, false), 2, i64::new(5, true), 1663680745),
  452. // price::new(i64::new(1511, false), 3, i64::new(5, true), 1663680745),
  453. // ),
  454. // ),
  455. // price_info::new_price_info(
  456. // 1663680747,
  457. // 1663074349,
  458. // price_feed::new(
  459. // price_identifier::from_byte_vec(x"21a28b4c6619968bd8c20e95b0aaed7df2187fd310275347e0376a2cd7427db8"),
  460. // price::new(i64::new(1739, false), 1, i64::new(5, true), 1663680745),
  461. // price::new(i64::new(1508, false), 3, i64::new(5, true), 1663680745),
  462. // ),
  463. // ),
  464. // ]
  465. // }
  466. // #[test_only]
  467. // /// Compare the expected price feed with the actual Pyth price feeds.
  468. // fun check_price_feeds_cached(expected: &vector<PriceInfo>, actual: &vector<PriceInfoObject>) {
  469. // // Check that we can retrieve the correct current price and ema price for each price feed
  470. // let i = 0;
  471. // while (i < vector::length(expected)) {
  472. // let price_feed = price_info::get_price_feed(vector::borrow(expected, i));
  473. // let price = price_feed::get_price(price_feed);
  474. // let ema_price = price_feed::get_ema_price(price_feed);
  475. // let price_identifier = price_info::get_price_identifier(vector::borrow(expected, i));
  476. // let actual_price_info = price_info::get_price_info_from_price_info_object(vector::borrow(actual, i));
  477. // let actual_price_feed = price_info::get_price_feed(&actual_price_info);
  478. // let actual_price = price_feed::get_price(actual_price_feed);
  479. // let actual_ema_price = price_feed::get_ema_price(actual_price_feed);
  480. // let actual_price_identifier = price_info::get_price_identifier(&actual_price_info);
  481. // assert!(price == actual_price, 0);
  482. // assert!(ema_price == actual_ema_price, 0);
  483. // assert!(price_identifier::get_bytes(&price_identifier) == price_identifier::get_bytes(&actual_price_identifier), 0);
  484. // i = i + 1;
  485. // };
  486. // }
  487. // #[test]
  488. // fun test_get_update_fee() {
  489. // let single_update_fee = 50;
  490. // let (scenario, test_coins, _clock) = setup_test(500, 23, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", vector[], single_update_fee, 0);
  491. // test_scenario::next_tx(&mut scenario, DEPLOYER, );
  492. // let pyth_state = take_shared<PythState>(&scenario);
  493. // // Pass in a single VAA
  494. // let single_vaa = vector[
  495. // x"fb1543888001083cf2e6ef3afdcf827e89b11efd87c563638df6e1995ada9f93",
  496. // ];
  497. // assert!(pyth::get_total_update_fee(&pyth_state, vector::length<vector<u8>>(&single_vaa)) == single_update_fee, 1);
  498. // let multiple_vaas = vector[
  499. // x"4ee17a1a4524118de513fddcf82b77454e51be5d6fc9e29fc72dd6c204c0e4fa",
  500. // x"c72fdf81cfc939d4286c93fbaaae2eec7bae28a5926fa68646b43a279846ccc1",
  501. // x"d9a8123a793529c31200339820a3210059ecace6c044f81ecad62936e47ca049",
  502. // x"84e4f21b3e65cef47fda25d15b4eddda1edf720a1d062ccbf441d6396465fbe6",
  503. // x"9e73f9041476a93701a0b9c7501422cc2aa55d16100bec628cf53e0281b6f72f"
  504. // ];
  505. // // Pass in multiple VAAs
  506. // assert!(pyth::get_total_update_fee(&pyth_state, vector::length<vector<u8>>(&multiple_vaas)) == 250, 1);
  507. // return_shared(pyth_state);
  508. // coin::burn_for_testing<SUI>(test_coins);
  509. // clock::destroy_for_testing(_clock);
  510. // test_scenario::end(scenario);
  511. // }
  512. #[test]
  513. #[expected_failure(abort_code = wormhole::vaa::E_WRONG_VERSION)]
  514. fun test_create_price_feeds_corrupt_vaa() {
  515. let (scenario, test_coins, clock) = setup_test(500, 23, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", vector[], 50, 0);
  516. test_scenario::next_tx(&mut scenario, DEPLOYER);
  517. let pyth_state = take_shared<PythState>(&scenario);
  518. let worm_state = take_shared<WormState>(&scenario);
  519. // Pass in a corrupt VAA, which should fail deseriaizing
  520. let corrupt_vaa = x"90F8bf6A479f320ead074411a4B0e7944Ea8c9C1";
  521. let verified_vaas = vector[vaa::parse_and_verify(&worm_state, corrupt_vaa, &clock)];
  522. // Create Pyth price feed
  523. pyth::create_price_feeds(
  524. &mut pyth_state,
  525. verified_vaas,
  526. &clock,
  527. ctx(&mut scenario)
  528. );
  529. return_shared(pyth_state);
  530. return_shared(worm_state);
  531. clock::destroy_for_testing(clock);
  532. coin::burn_for_testing<SUI>(test_coins);
  533. test_scenario::end(scenario);
  534. }
  535. // #[test]
  536. // #[expected_failure(abort_code = pyth::pyth::E_INVALID_DATA_SOURCE)]
  537. // fun test_create_price_feeds_invalid_data_source() {
  538. // // Initialize the contract with some valid data sources, excluding our test VAA's source
  539. // let data_sources = vector<DataSource>[
  540. // data_source::new(
  541. // 4, external_address::new(bytes32::new(x"0000000000000000000000000000000000000000000000000000000000007742"))
  542. // ),
  543. // data_source::new(
  544. // 5, external_address::new(bytes32::new(x"0000000000000000000000000000000000000000000000000000000000007637"))
  545. // )
  546. // ];
  547. // let (scenario, test_coins, clock) = setup_test(500, 23, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", data_sources, 50, 0);
  548. // test_scenario::next_tx(&mut scenario, DEPLOYER);
  549. // let pyth_state = take_shared<PythState>(&scenario);
  550. // let worm_state = take_shared<WormState>(&scenario);
  551. // let verified_vaas = get_verified_test_vaas(&worm_state, &clock);
  552. // pyth::create_price_feeds(
  553. // &mut pyth_state,
  554. // verified_vaas,
  555. // &clock,
  556. // ctx(&mut scenario)
  557. // );
  558. // return_shared(pyth_state);
  559. // return_shared(worm_state);
  560. // clock::destroy_for_testing(clock);
  561. // coin::burn_for_testing<SUI>(test_coins);
  562. // test_scenario::end(scenario);
  563. // }
  564. #[test_only]
  565. fun data_sources_for_test_vaa(): vector<DataSource> {
  566. // Set some valid data sources, including our test VAA's source
  567. vector<DataSource>[
  568. data_source::new(
  569. 1, external_address::new(bytes32::from_bytes(x"0000000000000000000000000000000000000000000000000000000000000004"))),
  570. data_source::new(
  571. 5, external_address::new(bytes32::new(x"0000000000000000000000000000000000000000000000000000000000007637"))),
  572. data_source::new(
  573. 17, external_address::new(bytes32::new(x"71f8dcb863d176e2c420ad6610cf687359612b6fb392e0642b0ca6b1f186aa3b")))
  574. ]
  575. }
  576. #[test]
  577. fun test_create_and_update_price_feeds_success() {
  578. let data_sources = data_sources_for_test_vaa();
  579. let base_update_fee = 50;
  580. let coins_to_mint = 5000;
  581. let (scenario, test_coins, clock) = setup_test(500, 23, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", data_sources, base_update_fee, coins_to_mint);
  582. test_scenario::next_tx(&mut scenario, DEPLOYER);
  583. let pyth_state = take_shared<PythState>(&scenario);
  584. let worm_state = take_shared<WormState>(&scenario);
  585. let verified_vaas = get_verified_test_vaas(&worm_state, &clock);
  586. test_scenario::next_tx(&mut scenario, DEPLOYER);
  587. pyth::create_price_feeds(
  588. &mut pyth_state,
  589. verified_vaas,
  590. &clock,
  591. ctx(&mut scenario)
  592. );
  593. // Affirm that 4 objects, which correspond to the 4 new price info objects
  594. // containing the price feeds were created and shared.
  595. let effects = test_scenario::next_tx(&mut scenario, DEPLOYER);
  596. let shared_ids = test_scenario::shared(&effects);
  597. let created_ids = test_scenario::created(&effects);
  598. assert!(vector::length<ID>(&shared_ids)==4, 0);
  599. assert!(vector::length<ID>(&created_ids)==4, 0);
  600. let price_info_object_1 = take_shared<PriceInfoObject>(&scenario);
  601. let price_info_object_2 = take_shared<PriceInfoObject>(&scenario);
  602. let price_info_object_3 = take_shared<PriceInfoObject>(&scenario);
  603. let price_info_object_4 = take_shared<PriceInfoObject>(&scenario);
  604. // Create vector of price info objects (Sui objects with key ability and living in global store),
  605. // which contain the price feeds we want to update. Note that these can be passed into
  606. // update_price_feeds in any order!
  607. //let price_info_object_vec = vector[price_info_object_1, price_info_object_2, price_info_object_3, price_info_object_4];
  608. verified_vaas = get_verified_test_vaas(&worm_state, &clock);
  609. test_scenario::next_tx(&mut scenario, DEPLOYER);
  610. let vaa_1 = vector::pop_back<VAA>(&mut verified_vaas);
  611. test_scenario::next_tx(&mut scenario, DEPLOYER);
  612. // Create hot potato.
  613. let potato = create_price_infos_hot_potato(
  614. &pyth_state,
  615. vector[vaa_1],
  616. &clock
  617. );
  618. test_scenario::next_tx(&mut scenario, DEPLOYER);
  619. potato = update_single_price_feed(
  620. &mut pyth_state,
  621. potato,
  622. &mut price_info_object_1,
  623. test_coins,
  624. &clock
  625. );
  626. test_scenario::next_tx(&mut scenario, DEPLOYER);
  627. hot_potato_vector::destroy<PriceInfo>(potato);
  628. vector::destroy_empty(verified_vaas);
  629. return_shared(pyth_state);
  630. return_shared(worm_state);
  631. return_shared(price_info_object_1);
  632. return_shared(price_info_object_2);
  633. return_shared(price_info_object_3);
  634. return_shared(price_info_object_4);
  635. clock::destroy_for_testing(clock);
  636. test_scenario::end(scenario);
  637. }
  638. // #[test]
  639. // #[expected_failure(abort_code = pyth::pyth::E_PRICE_INFO_OBJECT_NOT_FOUND)]
  640. // fun test_create_and_update_price_feeds_price_info_object_not_found_failure() {
  641. // let data_sources = data_sources_for_test_vaa();
  642. // let base_update_fee = 50;
  643. // let coins_to_mint = 5000;
  644. // let (scenario, test_coins, clock) = setup_test(500, 23, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", data_sources, base_update_fee, coins_to_mint);
  645. // test_scenario::next_tx(&mut scenario, DEPLOYER);
  646. // let pyth_state = take_shared<PythState>(&scenario);
  647. // let worm_state = take_shared<WormState>(&scenario);
  648. // let verified_vaas = get_verified_test_vaas(&worm_state, &clock);
  649. // pyth::create_price_feeds(
  650. // &mut pyth_state,
  651. // verified_vaas,
  652. // &clock,
  653. // ctx(&mut scenario)
  654. // );
  655. // // Affirm that 4 objects, which correspond to the 4 new price info objects
  656. // // containing the price feeds were created and shared.
  657. // let effects = test_scenario::next_tx(&mut scenario, DEPLOYER);
  658. // let shared_ids = test_scenario::shared(&effects);
  659. // let created_ids = test_scenario::created(&effects);
  660. // assert!(vector::length<ID>(&shared_ids)==4, 0);
  661. // assert!(vector::length<ID>(&created_ids)==4, 0);
  662. // let price_info_object_1 = take_shared<PriceInfoObject>(&scenario);
  663. // let price_info_object_2 = take_shared<PriceInfoObject>(&scenario);
  664. // let price_info_object_3 = take_shared<PriceInfoObject>(&scenario);
  665. // let price_info_object_4 = take_shared<PriceInfoObject>(&scenario);
  666. // // Note that here we only pass in 3 price info objects corresponding to 3 out
  667. // // of the 4 price feeds.
  668. // let price_info_object_vec = vector[price_info_object_1, price_info_object_2, price_info_object_3];
  669. // let verified_vaas = get_verified_test_vaas(&worm_state, &clock);
  670. // pyth::update_price_feeds(
  671. // &mut pyth_state,
  672. // verified_vaas,
  673. // &mut price_info_object_vec,
  674. // test_coins,
  675. // &clock
  676. // );
  677. // price_info_object_3 = vector::pop_back(&mut price_info_object_vec);
  678. // price_info_object_2 = vector::pop_back(&mut price_info_object_vec);
  679. // price_info_object_1 = vector::pop_back(&mut price_info_object_vec);
  680. // vector::destroy_empty(price_info_object_vec);
  681. // return_shared(pyth_state);
  682. // return_shared(worm_state);
  683. // return_shared(price_info_object_1);
  684. // return_shared(price_info_object_2);
  685. // return_shared(price_info_object_3);
  686. // return_shared(price_info_object_4);
  687. // return_shared(clock);
  688. // test_scenario::end(scenario);
  689. // }
  690. // #[test]
  691. // #[expected_failure(abort_code = pyth::pyth::E_INSUFFICIENT_FEE)]
  692. // fun test_create_and_update_price_feeds_insufficient_fee() {
  693. // let data_sources = data_sources_for_test_vaa();
  694. // let base_update_fee = 50;
  695. // let coins_to_mint = 5;
  696. // let (scenario, test_coins, clock) = setup_test(500, 23, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", data_sources, base_update_fee, coins_to_mint);
  697. // test_scenario::next_tx(&mut scenario, DEPLOYER);
  698. // let pyth_state = take_shared<PythState>(&scenario);
  699. // let worm_state = take_shared<WormState>(&scenario);
  700. // let verified_vaas = get_verified_test_vaas(&worm_state, &clock);
  701. // pyth::create_price_feeds(
  702. // &mut pyth_state,
  703. // verified_vaas,
  704. // &clock,
  705. // ctx(&mut scenario)
  706. // );
  707. // test_scenario::next_tx(&mut scenario, DEPLOYER);
  708. // let price_info_object = take_shared<PriceInfoObject>(&scenario);
  709. // let price_info_object_vec = vector[price_info_object];
  710. // verified_vaas = get_verified_test_vaas(&worm_state, &clock);
  711. // pyth::update_price_feeds(
  712. // &mut pyth_state,
  713. // verified_vaas,
  714. // &mut price_info_object_vec,
  715. // test_coins,
  716. // &clock
  717. // );
  718. // price_info_object = vector::pop_back(&mut price_info_object_vec);
  719. // vector::destroy_empty(price_info_object_vec);
  720. // return_shared(pyth_state);
  721. // return_shared(worm_state);
  722. // return_shared(price_info_object);
  723. // return_shared(clock);
  724. // test_scenario::end(scenario);
  725. // }
  726. // #[test]
  727. // fun test_update_cache(){
  728. // let data_sources = data_sources_for_test_vaa();
  729. // let base_update_fee = 50;
  730. // let coins_to_mint = 5000;
  731. // let (scenario, test_coins, clock) = setup_test(500, 23, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", data_sources, base_update_fee, coins_to_mint);
  732. // test_scenario::next_tx(&mut scenario, DEPLOYER);
  733. // let pyth_state = take_shared<PythState>(&scenario);
  734. // let worm_state = take_shared<WormState>(&scenario);
  735. // let verified_vaas = get_verified_test_vaas(&worm_state, &clock);
  736. // // Update cache is called by create_price_feeds.
  737. // pyth::create_price_feeds(
  738. // &mut pyth_state,
  739. // verified_vaas,
  740. // &clock,
  741. // ctx(&mut scenario)
  742. // );
  743. // test_scenario::next_tx(&mut scenario, DEPLOYER);
  744. // let price_info_object_1 = take_shared<PriceInfoObject>(&scenario);
  745. // let price_info_object_2 = take_shared<PriceInfoObject>(&scenario);
  746. // let price_info_object_3 = take_shared<PriceInfoObject>(&scenario);
  747. // let price_info_object_4 = take_shared<PriceInfoObject>(&scenario);
  748. // // These updates are price infos that correspond to the ones in TEST_VAAS.
  749. // let updates = get_mock_price_infos();
  750. // let price_info_object_vec = vector[
  751. // price_info_object_1,
  752. // price_info_object_2,
  753. // price_info_object_3,
  754. // price_info_object_4
  755. // ];
  756. // // Check that TEST_VAAS was indeed used to instantiate the price feeds correctly,
  757. // // by confirming that the info in updates is contained in price_info_object_vec.
  758. // check_price_feeds_cached(&updates, &price_info_object_vec);
  759. // price_info_object_4 = vector::pop_back(&mut price_info_object_vec);
  760. // price_info_object_3 = vector::pop_back(&mut price_info_object_vec);
  761. // price_info_object_2 = vector::pop_back(&mut price_info_object_vec);
  762. // price_info_object_1 = vector::pop_back(&mut price_info_object_vec);
  763. // vector::destroy_empty(price_info_object_vec);
  764. // return_shared(pyth_state);
  765. // return_shared(worm_state);
  766. // return_shared(price_info_object_1);
  767. // return_shared(price_info_object_2);
  768. // return_shared(price_info_object_3);
  769. // return_shared(price_info_object_4);
  770. // coin::burn_for_testing<SUI>(test_coins);
  771. // clock::destroy_for_testing(clock);
  772. // test_scenario::end(scenario);
  773. // }
  774. // #[test]
  775. // fun test_update_cache_old_update() {
  776. // let data_sources = data_sources_for_test_vaa();
  777. // let base_update_fee = 50;
  778. // let coins_to_mint = 5000;
  779. // let (scenario, test_coins, clock) = setup_test(500, 23, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", data_sources, base_update_fee, coins_to_mint);
  780. // test_scenario::next_tx(&mut scenario, DEPLOYER);
  781. // let pyth_state = take_shared<PythState>(&scenario);
  782. // let worm_state = take_shared<WormState>(&scenario);
  783. // let verified_vaas = get_verified_test_vaas(&worm_state, &clock);
  784. // pyth::create_price_feeds(
  785. // &mut pyth_state,
  786. // verified_vaas,
  787. // &clock,
  788. // ctx(&mut scenario)
  789. // );
  790. // test_scenario::next_tx(&mut scenario, DEPLOYER);
  791. // let price_info_object_1 = take_shared<PriceInfoObject>(&scenario);
  792. // let price_info_object_2 = take_shared<PriceInfoObject>(&scenario);
  793. // let price_info_object_3 = take_shared<PriceInfoObject>(&scenario);
  794. // let price_info_object_4 = take_shared<PriceInfoObject>(&scenario);
  795. // let price_info_object_vec = vector[
  796. // price_info_object_1,
  797. // price_info_object_2,
  798. // price_info_object_3,
  799. // price_info_object_4
  800. // ];
  801. // // Hardcode the price identifier, price, and ema_price for price_info_object_1, because
  802. // // it's easier than unwrapping price_info_object_1 and getting the quantities via getters.
  803. // let timestamp = 1663680740;
  804. // let price_identifier = price_identifier::from_byte_vec(x"c6c75c89f14810ec1c54c03ab8f1864a4c4032791f05747f560faec380a695d1");
  805. // let price = price::new(i64::new(1557, false), 7, i64::new(5, true), timestamp);
  806. // let ema_price = price::new(i64::new(1500, false), 3, i64::new(5, true), timestamp);
  807. // // Attempt to update the price with an update older than the current cached one.
  808. // let old_price = price::new(i64::new(1243, true), 9802, i64::new(6, false), timestamp - 200);
  809. // let old_ema_price = price::new(i64::new(8976, true), 234, i64::new(897, false), timestamp - 200);
  810. // let old_update = price_info::new_price_info(
  811. // 1257278600,
  812. // 1690226180,
  813. // price_feed::new(
  814. // price_identifier,
  815. // old_price,
  816. // old_ema_price,
  817. // )
  818. // );
  819. // pyth::update_cache(vector<PriceInfo>[old_update], &mut price_info_object_vec, &clock);
  820. // price_info_object_4 = vector::pop_back(&mut price_info_object_vec);
  821. // price_info_object_3 = vector::pop_back(&mut price_info_object_vec);
  822. // price_info_object_2 = vector::pop_back(&mut price_info_object_vec);
  823. // price_info_object_1 = vector::pop_back(&mut price_info_object_vec);
  824. // vector::destroy_empty(price_info_object_vec);
  825. // let current_price_info = price_info::get_price_info_from_price_info_object(&price_info_object_1);
  826. // let current_price_feed = price_info::get_price_feed(&current_price_info);
  827. // let current_price = price_feed::get_price(current_price_feed);
  828. // let current_ema_price = price_feed::get_ema_price(current_price_feed);
  829. // // Confirm that no price update occurred when we tried to update cache with an
  830. // // outdated update: old_update.
  831. // assert!(current_price == price, 1);
  832. // assert!(current_ema_price == ema_price, 1);
  833. // test_scenario::next_tx(&mut scenario, DEPLOYER);
  834. // // Update the cache with a fresh update.
  835. // let fresh_price = price::new(i64::new(5243, true), 2, i64::new(3, false), timestamp + 200);
  836. // let fresh_ema_price = price::new(i64::new(8976, true), 21, i64::new(32, false), timestamp + 200);
  837. // let fresh_update = price_info::new_price_info(
  838. // 1257278600,
  839. // 1690226180,
  840. // price_feed::new(
  841. // price_identifier,
  842. // fresh_price,
  843. // fresh_ema_price,
  844. // )
  845. // );
  846. // let price_info_object_vec = vector[
  847. // price_info_object_1,
  848. // price_info_object_2,
  849. // price_info_object_3,
  850. // price_info_object_4
  851. // ];
  852. // pyth::update_cache(vector<PriceInfo>[fresh_update], &mut price_info_object_vec, &clock);
  853. // price_info_object_4 = vector::pop_back(&mut price_info_object_vec);
  854. // price_info_object_3 = vector::pop_back(&mut price_info_object_vec);
  855. // price_info_object_2 = vector::pop_back(&mut price_info_object_vec);
  856. // price_info_object_1 = vector::pop_back(&mut price_info_object_vec);
  857. // vector::destroy_empty(price_info_object_vec);
  858. // // Confirm that the Pyth cached price got updated to fresh_price.
  859. // let current_price_info = price_info::get_price_info_from_price_info_object(&price_info_object_1);
  860. // let current_price_feed = price_info::get_price_feed(&current_price_info);
  861. // let current_price = price_feed::get_price(current_price_feed);
  862. // let current_ema_price = price_feed::get_ema_price(current_price_feed);
  863. // assert!(current_price==fresh_price, 0);
  864. // assert!(current_ema_price==fresh_ema_price, 0);
  865. // return_shared(pyth_state);
  866. // return_shared(worm_state);
  867. // return_shared(price_info_object_1);
  868. // return_shared(price_info_object_2);
  869. // return_shared(price_info_object_3);
  870. // return_shared(price_info_object_4);
  871. // coin::burn_for_testing<SUI>(test_coins);
  872. // clock::destroy_for_testing(clock);
  873. // test_scenario::end(scenario);
  874. // }
  875. }