pyth.move 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549
  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 pyth::accumulator::{Self};
  20. use wormhole::external_address::{Self};
  21. use wormhole::vaa::{Self, VAA};
  22. use wormhole::bytes32::{Self};
  23. use wormhole::cursor::{Self};
  24. const E_DATA_SOURCE_EMITTER_ADDRESS_AND_CHAIN_IDS_DIFFERENT_LENGTHS: u64 = 0;
  25. const E_INVALID_DATA_SOURCE: u64 = 1;
  26. const E_INSUFFICIENT_FEE: u64 = 2;
  27. const E_STALE_PRICE_UPDATE: u64 = 3;
  28. const E_UPDATE_AND_PRICE_INFO_OBJECT_MISMATCH: u64 = 4;
  29. const E_PRICE_UPDATE_NOT_FOUND_FOR_PRICE_INFO_OBJECT: u64 = 5;
  30. #[test_only]
  31. friend pyth::pyth_tests;
  32. /// Init state and emit event corresponding to Pyth initialization.
  33. public entry fun init_pyth(
  34. deployer: DeployerCap,
  35. upgrade_cap: UpgradeCap,
  36. stale_price_threshold: u64,
  37. governance_emitter_chain_id: u64,
  38. governance_emitter_address: vector<u8>,
  39. data_sources_emitter_chain_ids: vector<u64>,
  40. data_sources_emitter_addresses: vector<vector<u8>>,
  41. update_fee: u64,
  42. ctx: &mut TxContext
  43. ) {
  44. setup::init_and_share_state(
  45. deployer,
  46. upgrade_cap,
  47. stale_price_threshold,
  48. update_fee,
  49. data_source::new(
  50. governance_emitter_chain_id,
  51. external_address::new((bytes32::from_bytes(governance_emitter_address)))
  52. ),
  53. parse_data_sources(
  54. data_sources_emitter_chain_ids,
  55. data_sources_emitter_addresses,
  56. ),
  57. ctx
  58. );
  59. // Emit Pyth initialization event.
  60. pyth_event::emit_pyth_initialization_event();
  61. }
  62. fun parse_data_sources(
  63. emitter_chain_ids: vector<u64>,
  64. emitter_addresses: vector<vector<u8>>
  65. ): vector<DataSource> {
  66. assert!(vector::length(&emitter_chain_ids) == vector::length(&emitter_addresses),
  67. E_DATA_SOURCE_EMITTER_ADDRESS_AND_CHAIN_IDS_DIFFERENT_LENGTHS);
  68. let sources = vector::empty();
  69. let i = 0;
  70. while (i < vector::length(&emitter_chain_ids)) {
  71. vector::push_back(&mut sources, data_source::new(
  72. *vector::borrow(&emitter_chain_ids, i),
  73. external_address::new(bytes32::from_bytes(*vector::borrow(&emitter_addresses, i)))
  74. ));
  75. i = i + 1;
  76. };
  77. sources
  78. }
  79. /// Create and share new price feed objects if they don't already exist using accumulator message.
  80. public fun create_price_feeds_using_accumulator(
  81. pyth_state: &mut PythState,
  82. accumulator_message: vector<u8>,
  83. vaa: VAA, // the verified version of the vaa bytes encoded within the accumulator_message
  84. clock: &Clock,
  85. ctx: &mut TxContext
  86. ){
  87. // This capability ensures that the current build version is used.
  88. let latest_only = state::assert_latest_only(pyth_state);
  89. // Check that the VAA is from a valid data source (emitter)
  90. assert!(
  91. state::is_valid_data_source(
  92. pyth_state,
  93. data_source::new(
  94. (vaa::emitter_chain(&vaa) as u64),
  95. vaa::emitter_address(&vaa))
  96. ),
  97. E_INVALID_DATA_SOURCE
  98. );
  99. // decode the price info updates from the VAA payload (first check if it is an accumulator or batch price update)
  100. let accumulator_message_cursor = cursor::new(accumulator_message);
  101. let price_infos = accumulator::parse_and_verify_accumulator_message(&mut accumulator_message_cursor, vaa::take_payload(vaa), clock);
  102. // Create and share new price info objects, if not already exists.
  103. create_and_share_price_feeds_using_verified_price_infos(&latest_only, pyth_state, price_infos, ctx);
  104. // destroy rest of cursor
  105. cursor::take_rest(accumulator_message_cursor);
  106. }
  107. /// Create and share new price feed objects if they don't already exist using batch price attestation.
  108. /// The name of the function is kept as is to remain backward compatible
  109. public fun create_price_feeds(
  110. pyth_state: &mut PythState,
  111. // These vaas have been verified and consumed, so we don't have to worry about
  112. // doing replay protection for them.
  113. verified_vaas: vector<VAA>,
  114. clock: &Clock,
  115. ctx: &mut TxContext
  116. ){
  117. // This capability ensures that the current build version is used.
  118. let latest_only = state::assert_latest_only(pyth_state);
  119. while (!vector::is_empty(&verified_vaas)) {
  120. let vaa = vector::pop_back(&mut verified_vaas);
  121. // Check that the VAA is from a valid data source (emitter)
  122. assert!(
  123. state::is_valid_data_source(
  124. pyth_state,
  125. data_source::new(
  126. (vaa::emitter_chain(&vaa) as u64),
  127. vaa::emitter_address(&vaa))
  128. ),
  129. E_INVALID_DATA_SOURCE
  130. );
  131. // Deserialize the batch price attestation
  132. let price_infos = batch_price_attestation::destroy(batch_price_attestation::deserialize(vaa::take_payload(vaa), clock));
  133. // Create and share new price info objects, if not already exists.
  134. create_and_share_price_feeds_using_verified_price_infos(&latest_only, pyth_state, price_infos, ctx);
  135. };
  136. vector::destroy_empty(verified_vaas);
  137. }
  138. #[allow(lint(share_owned))]
  139. // create_and_share_price_feeds_using_verified_price_infos is a private function used by
  140. // 1) create_price_feeds
  141. // 2) create_price_feeds_using_accumulator
  142. // to create new price feeds for symbols.
  143. fun create_and_share_price_feeds_using_verified_price_infos(latest_only: &LatestOnly, pyth_state: &mut PythState, price_infos: vector<PriceInfo>, ctx: &mut TxContext){
  144. while (!vector::is_empty(&price_infos)){
  145. let cur_price_info = vector::pop_back(&mut price_infos);
  146. // Only create new Sui PriceInfoObject if not already
  147. // registered with the Pyth State object.
  148. if (!state::price_feed_object_exists(
  149. pyth_state,
  150. price_feed::get_price_identifier(
  151. price_info::get_price_feed(&cur_price_info)
  152. )
  153. )
  154. ){
  155. // Create and share newly created Sui PriceInfoObject containing a price feed,
  156. // and then register a copy of its ID with State.
  157. let new_price_info_object = price_info::new_price_info_object(cur_price_info, ctx);
  158. let price_identifier = price_info::get_price_identifier(&cur_price_info);
  159. let id = price_info::uid_to_inner(&new_price_info_object);
  160. state::register_price_info_object(latest_only, pyth_state, price_identifier, id);
  161. transfer::public_share_object(new_price_info_object);
  162. }
  163. }
  164. }
  165. // verified_vaa is the verified version of the VAA encoded within the accumulator_message
  166. public fun create_authenticated_price_infos_using_accumulator(
  167. pyth_state: &PythState,
  168. accumulator_message: vector<u8>,
  169. verified_vaa: VAA,
  170. clock: &Clock,
  171. ): HotPotatoVector<PriceInfo> {
  172. state::assert_latest_only(pyth_state);
  173. // verify that the VAA originates from a valid data source
  174. assert!(
  175. state::is_valid_data_source(
  176. pyth_state,
  177. data_source::new(
  178. (vaa::emitter_chain(&verified_vaa) as u64),
  179. vaa::emitter_address(&verified_vaa))
  180. ),
  181. E_INVALID_DATA_SOURCE
  182. );
  183. // decode the price info updates from the VAA payload (first check if it is an accumulator or batch price update)
  184. let accumulator_message_cursor = cursor::new(accumulator_message);
  185. let price_infos = accumulator::parse_and_verify_accumulator_message(&mut accumulator_message_cursor, vaa::take_payload(verified_vaa), clock);
  186. // check that accumulator message has been fully consumed
  187. cursor::destroy_empty(accumulator_message_cursor);
  188. hot_potato_vector::new(price_infos)
  189. }
  190. /// Creates authenticated price infos using batch price attestation
  191. /// Name is kept as is to remain backward compatible
  192. public fun create_price_infos_hot_potato(
  193. pyth_state: &PythState,
  194. verified_vaas: vector<VAA>,
  195. clock: &Clock
  196. ): HotPotatoVector<PriceInfo> {
  197. state::assert_latest_only(pyth_state);
  198. let price_updates = vector::empty<PriceInfo>();
  199. while (vector::length(&verified_vaas) != 0){
  200. let cur_vaa = vector::pop_back(&mut verified_vaas);
  201. assert!(
  202. state::is_valid_data_source(
  203. pyth_state,
  204. data_source::new(
  205. (vaa::emitter_chain(&cur_vaa) as u64),
  206. vaa::emitter_address(&cur_vaa))
  207. ),
  208. E_INVALID_DATA_SOURCE
  209. );
  210. let price_infos = batch_price_attestation::destroy(batch_price_attestation::deserialize(vaa::take_payload(cur_vaa), clock));
  211. while (vector::length(&price_infos) !=0 ){
  212. let cur_price_info = vector::pop_back(&mut price_infos);
  213. vector::push_back(&mut price_updates, cur_price_info);
  214. }
  215. };
  216. vector::destroy_empty(verified_vaas);
  217. return hot_potato_vector::new(price_updates)
  218. }
  219. /// Update a singular Pyth PriceInfoObject (containing a price feed) with the
  220. /// price data in the authenticated price infos vector (a vector of PriceInfo objects).
  221. ///
  222. /// For more information on the end-to-end process for updating a price feed, please see the README.
  223. ///
  224. /// The given fee must contain a sufficient number of coins to pay the update fee for the given vaas.
  225. /// The update fee amount can be queried by calling get_update_fee(&vaas).
  226. ///
  227. /// Please read more information about the update fee here: https://docs.pyth.network/documentation/pythnet-price-feeds/on-demand#fees
  228. public fun update_single_price_feed(
  229. pyth_state: &PythState,
  230. price_updates: HotPotatoVector<PriceInfo>,
  231. price_info_object: &mut PriceInfoObject,
  232. fee: Coin<SUI>,
  233. clock: &Clock
  234. ): HotPotatoVector<PriceInfo> {
  235. let latest_only = state::assert_latest_only(pyth_state);
  236. // On Sui, users get to choose which price feeds to update. They specify a single price feed to
  237. // update at a time. We therefore charge the base fee for each such individual update.
  238. // This is a departure from Eth, where users don't get to necessarily choose.
  239. assert!(state::get_base_update_fee(pyth_state) <= coin::value(&fee), E_INSUFFICIENT_FEE);
  240. // store fee coins within price info object
  241. price_info::deposit_fee_coins(price_info_object, fee);
  242. // Find price update corresponding to PriceInfoObject within the array of price_updates
  243. // and use it to update PriceInfoObject.
  244. let i = 0;
  245. let found = false;
  246. while (i < hot_potato_vector::length<PriceInfo>(&price_updates)){
  247. let cur_price_info = hot_potato_vector::borrow<PriceInfo>(&price_updates, i);
  248. if (has_same_price_identifier(cur_price_info, price_info_object)){
  249. found = true;
  250. update_cache(latest_only, cur_price_info, price_info_object, clock);
  251. break
  252. };
  253. i = i + 1;
  254. };
  255. if (found==false){
  256. abort E_PRICE_UPDATE_NOT_FOUND_FOR_PRICE_INFO_OBJECT
  257. };
  258. price_updates
  259. }
  260. fun has_same_price_identifier(price_info: &PriceInfo, price_info_object: &PriceInfoObject) : bool {
  261. let price_info_from_object = price_info::get_price_info_from_price_info_object(price_info_object);
  262. let price_identifier_from_object = price_info::get_price_identifier(&price_info_from_object);
  263. let price_identifier_from_price_info = price_info::get_price_identifier(price_info);
  264. price_identifier_from_object == price_identifier_from_price_info
  265. }
  266. /// Update PriceInfoObject with updated data from a PriceInfo
  267. public(friend) fun update_cache(
  268. _: LatestOnly,
  269. update: &PriceInfo,
  270. price_info_object: &mut PriceInfoObject,
  271. clock: &Clock,
  272. ){
  273. let has_same_price_identifier = has_same_price_identifier(update, price_info_object);
  274. assert!(has_same_price_identifier, E_UPDATE_AND_PRICE_INFO_OBJECT_MISMATCH);
  275. // Update the price info object with the new updated price info.
  276. if (is_fresh_update(update, price_info_object)){
  277. pyth_event::emit_price_feed_update(price_feed::from(price_info::get_price_feed(update)), clock::timestamp_ms(clock)/1000);
  278. price_info::update_price_info_object(
  279. price_info_object,
  280. update
  281. );
  282. }
  283. }
  284. /// Determine if the given price update is "fresh": we have nothing newer already cached for that
  285. /// price feed within a PriceInfoObject.
  286. fun is_fresh_update(update: &PriceInfo, price_info_object: &PriceInfoObject): bool {
  287. // Get the timestamp of the update's current price
  288. let price_feed = price_info::get_price_feed(update);
  289. let update_timestamp = price::get_timestamp(&price_feed::get_price(price_feed));
  290. // Get the timestamp of the cached data for the price identifier
  291. let cached_price_info = price_info::get_price_info_from_price_info_object(price_info_object);
  292. let cached_price_feed = price_info::get_price_feed(&cached_price_info);
  293. let cached_timestamp = price::get_timestamp(&price_feed::get_price(cached_price_feed));
  294. update_timestamp > cached_timestamp
  295. }
  296. // -----------------------------------------------------------------------------
  297. // Query the cached prices
  298. //
  299. // It is strongly recommended to update the cached prices using the functions above,
  300. // before using the functions below to query the cached data.
  301. /// Determine if a price feed for the given price_identifier exists
  302. public fun price_feed_exists(state: &PythState, price_identifier: PriceIdentifier): bool {
  303. state::price_feed_object_exists(state, price_identifier)
  304. }
  305. /// Get the latest available price cached for the given price identifier, if that price is
  306. /// no older than the stale price threshold.
  307. ///
  308. /// Please refer to the documentation at https://docs.pyth.network/documentation/pythnet-price-feeds/best-practices for
  309. /// how to how this price safely.
  310. ///
  311. /// Important: Pyth uses an on-demand update model, where consumers need to update the
  312. /// cached prices before using them. Please read more about this at https://docs.pyth.network/documentation/pythnet-price-feeds/on-demand.
  313. /// get_price() is likely to abort unless you call update_price_feeds() to update the cached price
  314. /// beforehand, as the cached prices may be older than the stale price threshold.
  315. ///
  316. /// The price_info_object is a Sui object with the key ability that uniquely
  317. /// contains a price feed for a given price_identifier.
  318. ///
  319. public fun get_price(state: &PythState, price_info_object: &PriceInfoObject, clock: &Clock): Price {
  320. get_price_no_older_than(price_info_object, clock, state::get_stale_price_threshold_secs(state))
  321. }
  322. /// Get the latest available price cached for the given price identifier, if that price is
  323. /// no older than the given age.
  324. public fun get_price_no_older_than(price_info_object: &PriceInfoObject, clock: &Clock, max_age_secs: u64): Price {
  325. let price = get_price_unsafe(price_info_object);
  326. check_price_is_fresh(&price, clock, max_age_secs);
  327. price
  328. }
  329. /// Get the latest available price cached for the given price identifier.
  330. ///
  331. /// WARNING: the returned price can be from arbitrarily far in the past.
  332. /// This function makes no guarantees that the returned price is recent or
  333. /// useful for any particular application. Users of this function should check
  334. /// the returned timestamp to ensure that the returned price is sufficiently
  335. /// recent for their application. The checked get_price_no_older_than()
  336. /// function should be used in preference to this.
  337. public fun get_price_unsafe(price_info_object: &PriceInfoObject): Price {
  338. // TODO: extract Price from this guy...
  339. let price_info = price_info::get_price_info_from_price_info_object(price_info_object);
  340. price_feed::get_price(
  341. price_info::get_price_feed(&price_info)
  342. )
  343. }
  344. fun abs_diff(x: u64, y: u64): u64 {
  345. if (x > y) {
  346. return x - y
  347. } else {
  348. return y - x
  349. }
  350. }
  351. /// Get the stale price threshold: the amount of time after which a cached price
  352. /// is considered stale and no longer returned by get_price()/get_ema_price().
  353. public fun get_stale_price_threshold_secs(state: &PythState): u64 {
  354. state::get_stale_price_threshold_secs(state)
  355. }
  356. fun check_price_is_fresh(price: &Price, clock: &Clock, max_age_secs: u64) {
  357. let age = abs_diff(clock::timestamp_ms(clock)/1000, price::get_timestamp(price));
  358. assert!(age < max_age_secs, E_STALE_PRICE_UPDATE);
  359. }
  360. /// Please read more information about the update fee here: https://docs.pyth.network/documentation/pythnet-price-feeds/on-demand#fees
  361. public fun get_total_update_fee(pyth_state: &PythState, n: u64): u64 {
  362. state::get_base_update_fee(pyth_state) * n
  363. }
  364. }
  365. #[test_only]
  366. module pyth::pyth_tests{
  367. use std::vector::{Self};
  368. use sui::sui::SUI;
  369. use sui::coin::{Self, Coin};
  370. use sui::test_scenario::{Self, Scenario, ctx, take_shared, return_shared};
  371. use sui::package::Self;
  372. use sui::object::{Self, ID};
  373. use sui::clock::{Self, Clock};
  374. use pyth::state::{State as PythState};
  375. use pyth::setup::{Self};
  376. use pyth::price_info::{Self, PriceInfo, PriceInfoObject};//, PriceInfo, PriceInfoObject};
  377. use pyth::data_source::{Self, DataSource};
  378. use pyth::pyth::{Self, create_price_infos_hot_potato, update_single_price_feed};
  379. use pyth::hot_potato_vector::{Self};
  380. use pyth::price_identifier::{Self};
  381. use pyth::price_feed::{Self};
  382. use pyth::accumulator::{Self};
  383. use pyth::deserialize::{Self};
  384. use wormhole::setup::{Self as wormhole_setup, DeployerCap};
  385. use wormhole::external_address::{Self};
  386. use wormhole::bytes32::{Self};
  387. use wormhole::state::{State as WormState};
  388. use wormhole::vaa::{Self, VAA};
  389. use wormhole::cursor::{Self};
  390. const DEPLOYER: address = @0x1234;
  391. const ACCUMULATOR_TESTS_EMITTER_ADDRESS: vector<u8> = x"71f8dcb863d176e2c420ad6610cf687359612b6fb392e0642b0ca6b1f186aa3b";
  392. const ACCUMULATOR_TESTS_INITIAL_GUARDIANS: vector<vector<u8>> = vector[x"7E5F4552091A69125d5DfCb7b8C2659029395Bdf"];
  393. const DEFAULT_BASE_UPDATE_FEE: u64 = 50;
  394. const DEFAULT_COIN_TO_MINT: u64 = 5000;
  395. const BATCH_ATTESTATION_TEST_INITIAL_GUARDIANS: vector<vector<u8>> = vector[x"beFA429d57cD18b7F8A4d91A2da9AB4AF05d0FBe"];
  396. fun ACCUMULATOR_TESTS_DATA_SOURCE(): vector<DataSource> {
  397. vector[data_source::new(1, external_address::new(bytes32::from_bytes(ACCUMULATOR_TESTS_EMITTER_ADDRESS)))]
  398. }
  399. fun get_verified_test_vaas(worm_state: &WormState, clock: &Clock): vector<VAA> {
  400. let test_vaas_: vector<vector<u8>> = vector[x"0100000000010036eb563b80a24f4253bee6150eb8924e4bdf6e4fa1dfc759a6664d2e865b4b134651a7b021b7f1ce3bd078070b688b6f2e37ce2de0d9b48e6a78684561e49d5201527e4f9b00000001001171f8dcb863d176e2c420ad6610cf687359612b6fb392e0642b0ca6b1f186aa3b0000000000000001005032574800030000000102000400951436e0be37536be96f0896366089506a59763d036728332d3e3038047851aea7c6c75c89f14810ec1c54c03ab8f1864a4c4032791f05747f560faec380a695d1000000000000049a0000000000000008fffffffb00000000000005dc0000000000000003000000000100000001000000006329c0eb000000006329c0e9000000006329c0e400000000000006150000000000000007215258d81468614f6b7e194c5d145609394f67b041e93e6695dcc616faadd0603b9551a68d01d954d6387aff4df1529027ffb2fee413082e509feb29cc4904fe000000000000041a0000000000000003fffffffb00000000000005cb0000000000000003010000000100000001000000006329c0eb000000006329c0e9000000006329c0e4000000000000048600000000000000078ac9cf3ab299af710d735163726fdae0db8465280502eb9f801f74b3c1bd190333832fad6e36eb05a8972fe5f219b27b5b2bb2230a79ce79beb4c5c5e7ecc76d00000000000003f20000000000000002fffffffb00000000000005e70000000000000003010000000100000001000000006329c0eb000000006329c0e9000000006329c0e40000000000000685000000000000000861db714e9ff987b6fedf00d01f9fea6db7c30632d6fc83b7bc9459d7192bc44a21a28b4c6619968bd8c20e95b0aaed7df2187fd310275347e0376a2cd7427db800000000000006cb0000000000000001fffffffb00000000000005e40000000000000003010000000100000001000000006329c0eb000000006329c0e9000000006329c0e400000000000007970000000000000001"];
  401. let verified_vaas_reversed = vector::empty<VAA>();
  402. let test_vaas = test_vaas_;
  403. let i = 0;
  404. while (i < vector::length(&test_vaas_)) {
  405. let cur_test_vaa = vector::pop_back(&mut test_vaas);
  406. let verified_vaa = vaa::parse_and_verify(worm_state, cur_test_vaa, clock);
  407. vector::push_back(&mut verified_vaas_reversed, verified_vaa);
  408. i=i+1;
  409. };
  410. let verified_vaas = vector::empty<VAA>();
  411. while (vector::length<VAA>(&verified_vaas_reversed)!=0){
  412. let cur = vector::pop_back(&mut verified_vaas_reversed);
  413. vector::push_back(&mut verified_vaas, cur);
  414. };
  415. vector::destroy_empty(verified_vaas_reversed);
  416. verified_vaas
  417. }
  418. // get_verified_vaa_from_accumulator_message parses the accumulator message up until the vaa, then
  419. // parses the vaa, yielding a verified wormhole::vaa::VAA object
  420. fun get_verified_vaa_from_accumulator_message(worm_state: &WormState, accumulator_message: vector<u8>, clock: &Clock): VAA {
  421. let _PYTHNET_ACCUMULATOR_UPDATE_MAGIC: u64 = 1347305813;
  422. let cursor = cursor::new(accumulator_message);
  423. let header: u32 = deserialize::deserialize_u32(&mut cursor);
  424. assert!((header as u64) == _PYTHNET_ACCUMULATOR_UPDATE_MAGIC, 0);
  425. let _major = deserialize::deserialize_u8(&mut cursor);
  426. let _minor = deserialize::deserialize_u8(&mut cursor);
  427. let trailing_size = deserialize::deserialize_u8(&mut cursor);
  428. deserialize::deserialize_vector(&mut cursor, (trailing_size as u64));
  429. let proof_type = deserialize::deserialize_u8(&mut cursor);
  430. assert!(proof_type == 0, 0);
  431. let vaa_size = deserialize::deserialize_u16(&mut cursor);
  432. let vaa = deserialize::deserialize_vector(&mut cursor, (vaa_size as u64));
  433. cursor::take_rest(cursor);
  434. vaa::parse_and_verify(worm_state, vaa, clock)
  435. }
  436. #[test_only]
  437. /// Init Wormhole core bridge state.
  438. /// Init Pyth state.
  439. /// Set initial Sui clock time.
  440. /// Mint some SUI fee coins.
  441. public fun setup_test(
  442. stale_price_threshold: u64,
  443. governance_emitter_chain_id: u64,
  444. governance_emitter_address: vector<u8>,
  445. data_sources: vector<DataSource>,
  446. initial_guardians: vector<vector<u8>>,
  447. base_update_fee: u64,
  448. to_mint: u64
  449. ): (Scenario, Coin<SUI>, Clock) {
  450. let scenario = test_scenario::begin(DEPLOYER);
  451. // Initialize Wormhole core bridge.
  452. wormhole_setup::init_test_only(ctx(&mut scenario));
  453. test_scenario::next_tx(&mut scenario, DEPLOYER);
  454. // Take the `DeployerCap` from the sender of the transaction.
  455. let deployer_cap =
  456. test_scenario::take_from_address<DeployerCap>(
  457. &scenario,
  458. DEPLOYER
  459. );
  460. // This will be created and sent to the transaction sender automatically
  461. // when the contract is published. This exists in place of grabbing
  462. // it from the sender.
  463. let upgrade_cap =
  464. package::test_publish(
  465. object::id_from_address(@wormhole),
  466. test_scenario::ctx(&mut scenario)
  467. );
  468. let governance_chain = 1234;
  469. let governance_contract =
  470. x"deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef";
  471. let guardian_set_seconds_to_live = 5678;
  472. let message_fee = 350;
  473. let guardian_set_index = 0;
  474. wormhole_setup::complete(
  475. deployer_cap,
  476. upgrade_cap,
  477. governance_chain,
  478. governance_contract,
  479. guardian_set_index,
  480. initial_guardians,
  481. guardian_set_seconds_to_live,
  482. message_fee,
  483. test_scenario::ctx(&mut scenario)
  484. );
  485. // Initialize Pyth state.
  486. let pyth_upgrade_cap=
  487. package::test_publish(
  488. object::id_from_address(@pyth),
  489. test_scenario::ctx(&mut scenario)
  490. );
  491. setup::init_test_only(ctx(&mut scenario));
  492. test_scenario::next_tx(&mut scenario, DEPLOYER);
  493. let pyth_deployer_cap = test_scenario::take_from_address<setup::DeployerCap>(
  494. &scenario,
  495. DEPLOYER
  496. );
  497. setup::init_and_share_state(
  498. pyth_deployer_cap,
  499. pyth_upgrade_cap,
  500. stale_price_threshold,
  501. base_update_fee,
  502. data_source::new(governance_emitter_chain_id, external_address::new(bytes32::from_bytes(governance_emitter_address))),
  503. data_sources,
  504. ctx(&mut scenario)
  505. );
  506. let coins = coin::mint_for_testing<SUI>(to_mint, ctx(&mut scenario));
  507. let clock = clock::create_for_testing(ctx(&mut scenario));
  508. (scenario, coins, clock)
  509. }
  510. fun get_mock_price_infos(): vector<PriceInfo> {
  511. use pyth::i64::Self;
  512. use pyth::price::{Self};
  513. vector<PriceInfo>[
  514. price_info::new_price_info(
  515. 1663680747,
  516. 1663074349,
  517. price_feed::new(
  518. price_identifier::from_byte_vec(x"c6c75c89f14810ec1c54c03ab8f1864a4c4032791f05747f560faec380a695d1"),
  519. price::new(i64::new(1557, false), 7, i64::new(5, true), 1663680740),
  520. price::new(i64::new(1500, false), 3, i64::new(5, true), 1663680740),
  521. ),
  522. ),
  523. price_info::new_price_info(
  524. 1663680747,
  525. 1663074349,
  526. price_feed::new(
  527. price_identifier::from_byte_vec(x"3b9551a68d01d954d6387aff4df1529027ffb2fee413082e509feb29cc4904fe"),
  528. price::new(i64::new(1050, false), 3, i64::new(5, true), 1663680745),
  529. price::new(i64::new(1483, false), 3, i64::new(5, true), 1663680745),
  530. ),
  531. ),
  532. price_info::new_price_info(
  533. 1663680747,
  534. 1663074349,
  535. price_feed::new(
  536. price_identifier::from_byte_vec(x"33832fad6e36eb05a8972fe5f219b27b5b2bb2230a79ce79beb4c5c5e7ecc76d"),
  537. price::new(i64::new(1010, false), 2, i64::new(5, true), 1663680745),
  538. price::new(i64::new(1511, false), 3, i64::new(5, true), 1663680745),
  539. ),
  540. ),
  541. price_info::new_price_info(
  542. 1663680747,
  543. 1663074349,
  544. price_feed::new(
  545. price_identifier::from_byte_vec(x"21a28b4c6619968bd8c20e95b0aaed7df2187fd310275347e0376a2cd7427db8"),
  546. price::new(i64::new(1739, false), 1, i64::new(5, true), 1663680745),
  547. price::new(i64::new(1508, false), 3, i64::new(5, true), 1663680745),
  548. ),
  549. ),
  550. ]
  551. }
  552. /// Compare the expected price feed with the actual Pyth price feeds.
  553. fun check_price_feeds_cached(expected: &vector<PriceInfo>, actual: &vector<PriceInfoObject>) {
  554. // Check that we can retrieve the correct current price and ema price for each price feed
  555. let i = 0;
  556. while (i < vector::length(expected)) {
  557. let price_feed = price_info::get_price_feed(vector::borrow(expected, i));
  558. let price = price_feed::get_price(price_feed);
  559. let ema_price = price_feed::get_ema_price(price_feed);
  560. let price_identifier = price_info::get_price_identifier(vector::borrow(expected, i));
  561. let actual_price_info = price_info::get_price_info_from_price_info_object(vector::borrow(actual, i));
  562. let actual_price_feed = price_info::get_price_feed(&actual_price_info);
  563. let actual_price = price_feed::get_price(actual_price_feed);
  564. let actual_ema_price = price_feed::get_ema_price(actual_price_feed);
  565. let actual_price_identifier = price_info::get_price_identifier(&actual_price_info);
  566. assert!(price == actual_price, 0);
  567. assert!(ema_price == actual_ema_price, 0);
  568. assert!(price_identifier::get_bytes(&price_identifier) == price_identifier::get_bytes(&actual_price_identifier), 0);
  569. i = i + 1;
  570. };
  571. }
  572. #[test]
  573. fun test_get_update_fee() {
  574. let (scenario, test_coins, _clock) = setup_test(500 /* stale_price_threshold */, 23 /* governance emitter chain */, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", vector[], BATCH_ATTESTATION_TEST_INITIAL_GUARDIANS, DEFAULT_BASE_UPDATE_FEE, 0);
  575. test_scenario::next_tx(&mut scenario, DEPLOYER, );
  576. let pyth_state = take_shared<PythState>(&scenario);
  577. // Pass in a single VAA
  578. let single_vaa = vector[
  579. x"fb1543888001083cf2e6ef3afdcf827e89b11efd87c563638df6e1995ada9f93",
  580. ];
  581. assert!(pyth::get_total_update_fee(&pyth_state, vector::length<vector<u8>>(&single_vaa)) == DEFAULT_BASE_UPDATE_FEE, 1);
  582. let multiple_vaas = vector[
  583. x"4ee17a1a4524118de513fddcf82b77454e51be5d6fc9e29fc72dd6c204c0e4fa",
  584. x"c72fdf81cfc939d4286c93fbaaae2eec7bae28a5926fa68646b43a279846ccc1",
  585. x"d9a8123a793529c31200339820a3210059ecace6c044f81ecad62936e47ca049",
  586. x"84e4f21b3e65cef47fda25d15b4eddda1edf720a1d062ccbf441d6396465fbe6",
  587. x"9e73f9041476a93701a0b9c7501422cc2aa55d16100bec628cf53e0281b6f72f"
  588. ];
  589. // Pass in multiple VAAs
  590. assert!(pyth::get_total_update_fee(&pyth_state, vector::length<vector<u8>>(&multiple_vaas)) == 5*DEFAULT_BASE_UPDATE_FEE, 1);
  591. return_shared(pyth_state);
  592. coin::burn_for_testing<SUI>(test_coins);
  593. clock::destroy_for_testing(_clock);
  594. test_scenario::end(scenario);
  595. }
  596. #[test]
  597. #[expected_failure(abort_code = wormhole::vaa::E_WRONG_VERSION)]
  598. fun test_create_price_feeds_corrupt_vaa() {
  599. let (scenario, test_coins, clock) = setup_test(500 /* stale_price_threshold */, 23 /* governance emitter chain */, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", vector[], vector[x"beFA429d57cD18b7F8A4d91A2da9AB4AF05d0FBe"], 50, 0);
  600. test_scenario::next_tx(&mut scenario, DEPLOYER);
  601. let (pyth_state, worm_state) = take_wormhole_and_pyth_states(&scenario);
  602. // Pass in a corrupt VAA, which should fail deseriaizing
  603. let corrupt_vaa = x"90F8bf6A479f320ead074411a4B0e7944Ea8c9C1";
  604. let verified_vaas = vector[vaa::parse_and_verify(&worm_state, corrupt_vaa, &clock)];
  605. // Create Pyth price feed
  606. pyth::create_price_feeds(
  607. &mut pyth_state,
  608. verified_vaas,
  609. &clock,
  610. ctx(&mut scenario)
  611. );
  612. cleanup_worm_state_pyth_state_and_clock(worm_state, pyth_state, clock);
  613. coin::burn_for_testing<SUI>(test_coins);
  614. test_scenario::end(scenario);
  615. }
  616. #[test]
  617. #[expected_failure(abort_code = pyth::pyth::E_INVALID_DATA_SOURCE)]
  618. fun test_create_price_feeds_invalid_data_source() {
  619. // Initialize the contract with some valid data sources, excluding our test VAA's source
  620. let data_sources = vector<DataSource>[
  621. data_source::new(
  622. 4, external_address::new(bytes32::new(x"0000000000000000000000000000000000000000000000000000000000007742"))
  623. ),
  624. data_source::new(
  625. 5, external_address::new(bytes32::new(x"0000000000000000000000000000000000000000000000000000000000007637"))
  626. )
  627. ];
  628. let (scenario, test_coins, clock) = setup_test(500, 23, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", data_sources, BATCH_ATTESTATION_TEST_INITIAL_GUARDIANS, 50, 0);
  629. test_scenario::next_tx(&mut scenario, DEPLOYER);
  630. let (pyth_state, worm_state) = take_wormhole_and_pyth_states(&scenario);
  631. let verified_vaas = get_verified_test_vaas(&worm_state, &clock);
  632. pyth::create_price_feeds(
  633. &mut pyth_state,
  634. verified_vaas,
  635. &clock,
  636. ctx(&mut scenario)
  637. );
  638. cleanup_worm_state_pyth_state_and_clock(worm_state, pyth_state, clock);
  639. coin::burn_for_testing<SUI>(test_coins);
  640. test_scenario::end(scenario);
  641. }
  642. public fun data_sources_for_test_vaa(): vector<DataSource> {
  643. // Set some valid data sources, including our test VAA's source
  644. vector<DataSource>[
  645. data_source::new(
  646. 1, external_address::new(bytes32::from_bytes(x"0000000000000000000000000000000000000000000000000000000000000004"))),
  647. data_source::new(
  648. 5, external_address::new(bytes32::new(x"0000000000000000000000000000000000000000000000000000000000007637"))),
  649. data_source::new(
  650. 17, external_address::new(bytes32::new(ACCUMULATOR_TESTS_EMITTER_ADDRESS)))
  651. ]
  652. }
  653. #[test]
  654. // test_create_and_update_price_feeds_with_batch_attestation_success tests the creation and updating of price
  655. // feeds, as well as depositing fee coins into price info objects
  656. fun test_create_and_update_price_feeds_with_batch_attestation_success() {
  657. let (scenario, test_coins, clock) = setup_test(500, 23, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", data_sources_for_test_vaa(), vector[x"beFA429d57cD18b7F8A4d91A2da9AB4AF05d0FBe"], DEFAULT_BASE_UPDATE_FEE, DEFAULT_COIN_TO_MINT);
  658. test_scenario::next_tx(&mut scenario, DEPLOYER);
  659. let (pyth_state, worm_state) = take_wormhole_and_pyth_states(&scenario);
  660. let verified_vaas = get_verified_test_vaas(&worm_state, &clock);
  661. test_scenario::next_tx(&mut scenario, DEPLOYER);
  662. pyth::create_price_feeds(
  663. &mut pyth_state,
  664. verified_vaas,
  665. &clock,
  666. ctx(&mut scenario)
  667. );
  668. // Affirm that 4 objects, which correspond to the 4 new price info objects
  669. // containing the price feeds were created and shared.
  670. let effects = test_scenario::next_tx(&mut scenario, DEPLOYER);
  671. let shared_ids = test_scenario::shared(&effects);
  672. let created_ids = test_scenario::created(&effects);
  673. assert!(vector::length<ID>(&shared_ids)==4, 0);
  674. assert!(vector::length<ID>(&created_ids)==4, 0);
  675. let price_info_object_1 = take_shared<PriceInfoObject>(&scenario);
  676. let price_info_object_2 = take_shared<PriceInfoObject>(&scenario);
  677. let price_info_object_3 = take_shared<PriceInfoObject>(&scenario);
  678. let price_info_object_4 = take_shared<PriceInfoObject>(&scenario);
  679. // Create vector of price info objects (Sui objects with key ability and living in global store),
  680. // which contain the price feeds we want to update. Note that these can be passed into
  681. // update_price_feeds in any order!
  682. //let price_info_object_vec = vector[price_info_object_1, price_info_object_2, price_info_object_3, price_info_object_4];
  683. verified_vaas = get_verified_test_vaas(&worm_state, &clock);
  684. test_scenario::next_tx(&mut scenario, DEPLOYER);
  685. let vaa_1 = vector::pop_back<VAA>(&mut verified_vaas);
  686. test_scenario::next_tx(&mut scenario, DEPLOYER);
  687. // Create authenticated price infos
  688. let vec = create_price_infos_hot_potato(
  689. &pyth_state,
  690. vector[vaa_1],
  691. &clock
  692. );
  693. test_scenario::next_tx(&mut scenario, DEPLOYER);
  694. let fee_coins = coin::split(&mut test_coins, DEFAULT_BASE_UPDATE_FEE, ctx(&mut scenario));
  695. vec = update_single_price_feed(
  696. &pyth_state,
  697. vec,
  698. &mut price_info_object_1,
  699. fee_coins,
  700. &clock
  701. );
  702. test_scenario::next_tx(&mut scenario, DEPLOYER);
  703. // check price feed updated
  704. assert!(price_feeds_equal(hot_potato_vector::borrow(&vec, 3), &price_info::get_price_info_from_price_info_object(&price_info_object_1)), 0);
  705. // check fee coins are deposited in the price info object
  706. assert!(price_info::get_balance(&price_info_object_1)==DEFAULT_BASE_UPDATE_FEE, 0);
  707. test_scenario::next_tx(&mut scenario, DEPLOYER);
  708. hot_potato_vector::destroy<PriceInfo>(vec);
  709. vector::destroy_empty(verified_vaas);
  710. return_shared(price_info_object_1);
  711. return_shared(price_info_object_2);
  712. return_shared(price_info_object_3);
  713. return_shared(price_info_object_4);
  714. coin::burn_for_testing(test_coins);
  715. cleanup_worm_state_pyth_state_and_clock(worm_state, pyth_state, clock);
  716. test_scenario::end(scenario);
  717. }
  718. // TEST_ACCUMULATOR_SINGLE_FEED details:
  719. // Price Identifier: 0xb10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf6
  720. // Price: 6887568746747646632
  721. // Conf: 13092246197863718329
  722. // Exponent: 1559537863
  723. // EMA Price: 4772242609775910581
  724. // EMA Conf: 358129956189946877
  725. // EMA Expo: 1559537863
  726. // Published Time: 1687276661
  727. const TEST_ACCUMULATOR_SINGLE_FEED: vector<u8> = x"504e41550100000000a0010000000001005d461ac1dfffa8451edda17e4b28a46c8ae912422b2dc0cb7732828c497778ea27147fb95b4d250651931845e7f3e22c46326716bcf82be2874a9c9ab94b6e42000000000000000000000171f8dcb863d176e2c420ad6610cf687359612b6fb392e0642b0ca6b1f186aa3b0000000000000000004155575600000000000000000000000000da936d73429246d131873a0bab90ad7b416510be01005500b10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf65f958f4883f9d2a8b5b1008d1fa01db95cf4a8c7000000006491cc757be59f3f377c0d3f423a695e81ad1eb504f8554c3620c3fd02f2ee15ea639b73fa3db9b34a245bdfa015c260c5a8a1180177cf30b2c0bebbb1adfe8f7985d051d2";
  728. #[test]
  729. fun test_create_and_update_single_price_feed_with_accumulator_success() {
  730. let (scenario, coins, clock) = setup_test(500, 23, ACCUMULATOR_TESTS_EMITTER_ADDRESS, ACCUMULATOR_TESTS_DATA_SOURCE(), ACCUMULATOR_TESTS_INITIAL_GUARDIANS, DEFAULT_BASE_UPDATE_FEE, DEFAULT_COIN_TO_MINT);
  731. test_scenario::next_tx(&mut scenario, DEPLOYER);
  732. let (pyth_state, worm_state) = take_wormhole_and_pyth_states(&scenario);
  733. let verified_vaa = get_verified_vaa_from_accumulator_message(&worm_state, TEST_ACCUMULATOR_SINGLE_FEED, &clock);
  734. test_scenario::next_tx(&mut scenario, DEPLOYER);
  735. pyth::create_price_feeds_using_accumulator(
  736. &mut pyth_state,
  737. TEST_ACCUMULATOR_SINGLE_FEED,
  738. verified_vaa,
  739. &clock,
  740. ctx(&mut scenario)
  741. );
  742. // Affirm that 1 object, which correspond to the 1 new price info object
  743. // containing the price feeds were created and shared.
  744. let effects = test_scenario::next_tx(&mut scenario, DEPLOYER);
  745. let shared_ids = test_scenario::shared(&effects);
  746. let created_ids = test_scenario::created(&effects);
  747. assert!(vector::length<ID>(&shared_ids)==1, 0);
  748. assert!(vector::length<ID>(&created_ids)==1, 0);
  749. let price_info_object_1 = take_shared<PriceInfoObject>(&scenario);
  750. // Create authenticated price infos
  751. verified_vaa = get_verified_vaa_from_accumulator_message(&worm_state, TEST_ACCUMULATOR_SINGLE_FEED, &clock);
  752. let auth_price_infos = pyth::create_authenticated_price_infos_using_accumulator(
  753. &pyth_state,
  754. TEST_ACCUMULATOR_SINGLE_FEED,
  755. verified_vaa,
  756. &clock
  757. );
  758. test_scenario::next_tx(&mut scenario, DEPLOYER);
  759. auth_price_infos = update_single_price_feed(
  760. &pyth_state,
  761. auth_price_infos,
  762. &mut price_info_object_1,
  763. coins,
  764. &clock
  765. );
  766. // assert that price info obejct is as expected
  767. let expected = accumulator_test_1_to_price_info();
  768. assert!(price_feeds_equal(&expected, &price_info::get_price_info_from_price_info_object(&price_info_object_1)), 0);
  769. // clean up test scenario
  770. test_scenario::next_tx(&mut scenario, DEPLOYER);
  771. hot_potato_vector::destroy<PriceInfo>(auth_price_infos);
  772. return_shared(price_info_object_1);
  773. cleanup_worm_state_pyth_state_and_clock(worm_state, pyth_state, clock);
  774. test_scenario::end(scenario);
  775. }
  776. #[test]
  777. #[expected_failure(abort_code = pyth::accumulator::E_INVALID_PROOF)]
  778. fun test_create_and_update_single_price_feed_with_accumulator_failure() {
  779. let (scenario, coins, clock) = setup_test(500, 23, ACCUMULATOR_TESTS_EMITTER_ADDRESS, ACCUMULATOR_TESTS_DATA_SOURCE(), ACCUMULATOR_TESTS_INITIAL_GUARDIANS, DEFAULT_BASE_UPDATE_FEE, DEFAULT_COIN_TO_MINT);
  780. test_scenario::next_tx(&mut scenario, DEPLOYER);
  781. let (pyth_state, worm_state) = take_wormhole_and_pyth_states(&scenario);
  782. // the verified vaa here contains the wrong merkle root
  783. let verified_vaa = get_verified_vaa_from_accumulator_message(&worm_state, TEST_ACCUMULATOR_3_MSGS, &clock);
  784. test_scenario::next_tx(&mut scenario, DEPLOYER);
  785. pyth::create_price_feeds_using_accumulator(
  786. &mut pyth_state,
  787. TEST_ACCUMULATOR_SINGLE_FEED,
  788. verified_vaa,
  789. &clock,
  790. ctx(&mut scenario)
  791. );
  792. // clean up test scenario
  793. test_scenario::next_tx(&mut scenario, DEPLOYER);
  794. coin::burn_for_testing<SUI>(coins);
  795. cleanup_worm_state_pyth_state_and_clock(worm_state, pyth_state, clock);
  796. test_scenario::end(scenario);
  797. }
  798. #[test_only]
  799. const TEST_ACCUMULATOR_INVALID_PROOF_1: vector<u8> = x"504e41550100000000a001000000000100110db9cd8325ccfab0dae92eeb9ea70a1faba5c5e96dc21ff46a8ddc560afc9a60df096b8ff21172804692bbdc958153e838437d8b474cbf45f0dc2a8acae831000000000000000000000171f8dcb863d176e2c420ad6610cf687359612b6fb392e0642b0ca6b1f186aa3b0000000000000000004155575600000000000000000000000000a8bea2b5f12f3177ff9b3929d77c3476ab2d32c602005500b10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf6fa75cd3aa3bb5ace5e2516446f71f85be36bd19bb0703f3154bb3db07be59f3f377c0d3f44661d9a8736c68884c8169e8b636ee3043202397384073120dce9e5d0efe24b44b4a0d62da8a1180177cf30b2c0bebbb1adfe8f7985d051d205a01e2504d9f0c06e7e7cb0cf24116098ca202ac5f6ade2e8f5a12ec006b16d46be1f0228b94d950055006e1540171b6c0c960b71a7020d9f60077f6af931a8bbf590da0223dacf75c7af5f958f4883f9d2a8b5b1008d1fa01db95cf4a8c7423a695e81ad1eb504f8554c3620c3fd40b40f7d581ac802e2de5cb82a9ae672043202397384073120dce9e5d0efe24b44b4a0d62da8a1180177cf30b2c0bebbb1adfe8f7985d051d205a01e2504d9f0c06e7e7cb0cf24116098ca202ac5f6ade2e8f5a12ec006b16d46be1f0228b94d95";
  800. #[test]
  801. #[expected_failure(abort_code = pyth::accumulator::E_INVALID_PROOF)]
  802. fun test_accumulator_invalid_proof() {
  803. let (scenario, coins, clock) = setup_test(500, 23, ACCUMULATOR_TESTS_EMITTER_ADDRESS, ACCUMULATOR_TESTS_DATA_SOURCE(), ACCUMULATOR_TESTS_INITIAL_GUARDIANS, DEFAULT_BASE_UPDATE_FEE, DEFAULT_COIN_TO_MINT);
  804. test_scenario::next_tx(&mut scenario, DEPLOYER);
  805. let (pyth_state, worm_state) = take_wormhole_and_pyth_states(&scenario);
  806. let verified_vaa = get_verified_vaa_from_accumulator_message(&worm_state, TEST_ACCUMULATOR_INVALID_PROOF_1, &clock);
  807. test_scenario::next_tx(&mut scenario, DEPLOYER);
  808. pyth::create_price_feeds_using_accumulator(
  809. &mut pyth_state,
  810. TEST_ACCUMULATOR_INVALID_PROOF_1,
  811. verified_vaa,
  812. &clock,
  813. ctx(&mut scenario)
  814. );
  815. // clean up test scenario
  816. test_scenario::next_tx(&mut scenario, DEPLOYER);
  817. coin::burn_for_testing<SUI>(coins);
  818. cleanup_worm_state_pyth_state_and_clock(worm_state, pyth_state, clock);
  819. test_scenario::end(scenario);
  820. }
  821. #[test_only]
  822. const TEST_ACCUMULATOR_INVALID_MAJOR_VERSION: vector<u8> = x"504e41553c00000000a001000000000100496b7fbd18dca2f0e690712fd8ca522ff79ca7d9d6d22e9f5d753fba4bd16fff440a811bad710071c79859290bcb1700de49dd8400db90b048437b521200123e010000000000000000000171f8dcb863d176e2c420ad6610cf687359612b6fb392e0642b0ca6b1f186aa3b000000000000000000415557560000000000000000000000000005f5db4488a7cae9f9a6c1938340c0fbf4beb9090200550031ecc21a745e3968a04e9570e4425bc18fa8019c68028196b546d1669c200c6879bc5a3617ec3444d93c06501cf6a0909c38d4ec81d96026b71ec475e87d69c7b5124289adbf24212bed8c15db354391d2378d2e0454d2655c6c34e7e50580fd8c94511322968bbc6da8a1180177cf30b2c0bebbb1adfe8f7985d051d205a01e2504d9f0c06e7e7cb0cf24116098ca202ac5f6ade2e8f5a12ec006b16d46be1f0228b94d95005500944998273e477b495144fb8794c914197f3ccb46be2900f4698fd0ef743c9695a573a6ff665ff63edb5f9a85ad579dc14500a2112c09680fc146134f9a539ca82cb6e3501c801278fd08d80732a24118292866bb049e6e88181a1e1e8b6d3c6bbb95135a73041f3b56a8a1180177cf30b2c0bebbb1adfe8f7985d051d205a01e2504d9f0c06e7e7cb0cf24116098ca202ac5f6ade2e8f5a12ec006b16d46be1f0228b94d95";
  823. #[test]
  824. #[expected_failure(abort_code = pyth::accumulator::E_INVALID_ACCUMULATOR_PAYLOAD)]
  825. fun test_accumulator_invalid_major_version() {
  826. let (scenario, coins, clock) = setup_test(500, 23, ACCUMULATOR_TESTS_EMITTER_ADDRESS, ACCUMULATOR_TESTS_DATA_SOURCE(), ACCUMULATOR_TESTS_INITIAL_GUARDIANS, DEFAULT_BASE_UPDATE_FEE, DEFAULT_COIN_TO_MINT);
  827. test_scenario::next_tx(&mut scenario, DEPLOYER);
  828. let (pyth_state, worm_state) = take_wormhole_and_pyth_states(&scenario);
  829. let verified_vaa = get_verified_vaa_from_accumulator_message(&worm_state, TEST_ACCUMULATOR_INVALID_MAJOR_VERSION, &clock);
  830. test_scenario::next_tx(&mut scenario, DEPLOYER);
  831. pyth::create_price_feeds_using_accumulator(
  832. &mut pyth_state,
  833. TEST_ACCUMULATOR_INVALID_MAJOR_VERSION,
  834. verified_vaa,
  835. &clock,
  836. ctx(&mut scenario)
  837. );
  838. // clean up test scenario
  839. test_scenario::next_tx(&mut scenario, DEPLOYER);
  840. coin::burn_for_testing<SUI>(coins);
  841. cleanup_worm_state_pyth_state_and_clock(worm_state, pyth_state, clock);
  842. test_scenario::end(scenario);
  843. }
  844. #[test_only]
  845. const TEST_ACCUMULATOR_INVALID_WH_MSG: vector<u8> = x"504e41550100000000a001000000000100e87f98238c5357730936cfdfde3a37249e5219409a4f41b301924b8eb10815a43ea2f96e4fe1bc8cd398250f39448d3b8ca57c96f9cf7a2be292517280683caa010000000000000000000171f8dcb863d176e2c420ad6610cf687359612b6fb392e0642b0ca6b1f186aa3b00000000000000000041555755000000000000000000000000000fb6f9f2b3b6cc1c9ef6708985fef226d92a3c0801005500b10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf6fa75cd3aa3bb5ace5e2516446f71f85be36bd19b000000006491cc747be59f3f377c0d3f44661d9a8736c68884c8169e8b636ee301f2ee15ea639b73fa3db9b34a245bdfa015c260c5";
  846. #[test]
  847. #[expected_failure(abort_code = pyth::accumulator::E_INVALID_WORMHOLE_MESSAGE)]
  848. fun test_accumulator_invalid_wormhole_message() {
  849. let (scenario, coins, clock) = setup_test(500, 23, ACCUMULATOR_TESTS_EMITTER_ADDRESS, ACCUMULATOR_TESTS_DATA_SOURCE(), ACCUMULATOR_TESTS_INITIAL_GUARDIANS, DEFAULT_BASE_UPDATE_FEE, DEFAULT_COIN_TO_MINT);
  850. test_scenario::next_tx(&mut scenario, DEPLOYER);
  851. let (pyth_state, worm_state) = take_wormhole_and_pyth_states(&scenario);
  852. let verified_vaa = get_verified_vaa_from_accumulator_message(&worm_state, TEST_ACCUMULATOR_INVALID_WH_MSG, &clock);
  853. test_scenario::next_tx(&mut scenario, DEPLOYER);
  854. pyth::create_price_feeds_using_accumulator(
  855. &mut pyth_state,
  856. TEST_ACCUMULATOR_INVALID_WH_MSG,
  857. verified_vaa,
  858. &clock,
  859. ctx(&mut scenario)
  860. );
  861. // clean up test scenario
  862. test_scenario::next_tx(&mut scenario, DEPLOYER);
  863. coin::burn_for_testing<SUI>(coins);
  864. cleanup_worm_state_pyth_state_and_clock(worm_state, pyth_state, clock);
  865. test_scenario::end(scenario);
  866. }
  867. #[test_only]
  868. const TEST_ACCUMULATOR_INCREASED_MINOR_VERSION: vector<u8> = x"504e4155010a000000a001000000000100496b7fbd18dca2f0e690712fd8ca522ff79ca7d9d6d22e9f5d753fba4bd16fff440a811bad710071c79859290bcb1700de49dd8400db90b048437b521200123e010000000000000000000171f8dcb863d176e2c420ad6610cf687359612b6fb392e0642b0ca6b1f186aa3b000000000000000000415557560000000000000000000000000005f5db4488a7cae9f9a6c1938340c0fbf4beb9090200550031ecc21a745e3968a04e9570e4425bc18fa8019c68028196b546d1669c200c6879bc5a3617ec3444d93c06501cf6a0909c38d4ec81d96026b71ec475e87d69c7b5124289adbf24212bed8c15db354391d2378d2e0454d2655c6c34e7e50580fd8c94511322968bbc6da8a1180177cf30b2c0bebbb1adfe8f7985d051d205a01e2504d9f0c06e7e7cb0cf24116098ca202ac5f6ade2e8f5a12ec006b16d46be1f0228b94d95005500944998273e477b495144fb8794c914197f3ccb46be2900f4698fd0ef743c9695a573a6ff665ff63edb5f9a85ad579dc14500a2112c09680fc146134f9a539ca82cb6e3501c801278fd08d80732a24118292866bb049e6e88181a1e1e8b6d3c6bbb95135a73041f3b56a8a1180177cf30b2c0bebbb1adfe8f7985d051d205a01e2504d9f0c06e7e7cb0cf24116098ca202ac5f6ade2e8f5a12ec006b16d46be1f0228b94d95";
  869. #[test_only]
  870. const TEST_ACCUMULATOR_EXTRA_PAYLOAD: vector<u8> = x"504e41550100000000a001000000000100b2d11f181d81b4ff10beca30091754b464dc48bc1f7432d114f64a7a8f660e7964f2a0c6121bae6c1977514d46ee7a29d9395b20a45f2086071715c1dc19ab74000000000000000000000171f8dcb863d176e2c420ad6610cf687359612b6fb392e0642b0ca6b1f186aa3b000000000000000000415557560000000000000000000000000013f83cfdf63a5a1b3189182fa0a52e6de53ba7d002005d0031ecc21a745e3968a04e9570e4425bc18fa8019c68028196b546d1669c200c6879bc5a3617ec3444d93c06501cf6a0909c38d4ec81d96026b71ec475e87d69c7b5124289adbf24212bed8c15db354391d2378d2e000000000000000004a576f4a87f443f7d961a682f508c4f7b06ee1595a8a1180177cf30b2c0bebbb1adfe8f7985d051d205a01e2504d9f0c06e7e7cb0cf24116098ca202ac5f6ade2e8f5a12ec006b16d46be1f0228b94d95005d00944998273e477b495144fb8794c914197f3ccb46be2900f4698fd0ef743c9695a573a6ff665ff63edb5f9a85ad579dc14500a2112c09680fc146134f9a539ca82cb6e3501c801278fd08d80732a24118292866bb0000000000000000045be67ba87a8dfbea404827ccbf07790299b6c023a8a1180177cf30b2c0bebbb1adfe8f7985d051d205a01e2504d9f0c06e7e7cb0cf24116098ca202ac5f6ade2e8f5a12ec006b16d46be1f0228b94d95";
  871. #[test]
  872. fun test_accumulator_forward_compatibility() {
  873. let (scenario, coins, clock) = setup_test(500, 23, ACCUMULATOR_TESTS_EMITTER_ADDRESS, ACCUMULATOR_TESTS_DATA_SOURCE(), ACCUMULATOR_TESTS_INITIAL_GUARDIANS, DEFAULT_BASE_UPDATE_FEE, DEFAULT_COIN_TO_MINT);
  874. test_scenario::next_tx(&mut scenario, DEPLOYER);
  875. let (pyth_state, worm_state) = take_wormhole_and_pyth_states(&scenario);
  876. test_scenario::next_tx(&mut scenario, DEPLOYER);
  877. pyth::create_price_feeds_using_accumulator(
  878. &mut pyth_state,
  879. TEST_ACCUMULATOR_EXTRA_PAYLOAD,
  880. get_verified_vaa_from_accumulator_message(&worm_state, TEST_ACCUMULATOR_EXTRA_PAYLOAD, &clock),
  881. &clock,
  882. ctx(&mut scenario)
  883. );
  884. test_scenario::next_tx(&mut scenario, DEPLOYER);
  885. pyth::create_price_feeds_using_accumulator(
  886. &mut pyth_state,
  887. TEST_ACCUMULATOR_INCREASED_MINOR_VERSION,
  888. get_verified_vaa_from_accumulator_message(&worm_state, TEST_ACCUMULATOR_INCREASED_MINOR_VERSION, &clock),
  889. &clock,
  890. ctx(&mut scenario)
  891. );
  892. // clean up test scenario
  893. test_scenario::next_tx(&mut scenario, DEPLOYER);
  894. coin::burn_for_testing<SUI>(coins);
  895. cleanup_worm_state_pyth_state_and_clock(worm_state, pyth_state, clock);
  896. test_scenario::end(scenario);
  897. }
  898. // TEST_ACCUMULATOR_3_MSGS details:
  899. // Price Identifier: 0xb10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf6
  900. // Price: 100
  901. // Conf: 50
  902. // Exponent: 9
  903. // EMA Price: 99
  904. // EMA Conf: 52
  905. // EMA Expo: 9
  906. // Published Time: 1687276660
  907. // Price Identifier: 0x6e1540171b6c0c960b71a7020d9f60077f6af931a8bbf590da0223dacf75c7af
  908. // Price: 101
  909. // Conf: 51
  910. // Exponent: 10
  911. // EMA Price: 100
  912. // EMA Conf: 53
  913. // EMA Expo: 10
  914. // Published Time: 1687276661
  915. // Price Identifier: 0x31ecc21a745e3968a04e9570e4425bc18fa8019c68028196b546d1669c200c68
  916. // Price: 102
  917. // Conf: 52
  918. // Exponent: 11
  919. // EMA Price: 101
  920. // EMA Conf: 54
  921. // EMA Expo: 11
  922. // Published Time: 1687276662
  923. const TEST_ACCUMULATOR_3_MSGS: vector<u8> = x"504e41550100000000a001000000000100d39b55fa311213959f91866d52624f3a9c07350d8956f6d42cfbb037883f31575c494a2f09fea84e4884dc9c244123fd124bc7825cd64d7c11e33ba5cfbdea7e010000000000000000000171f8dcb863d176e2c420ad6610cf687359612b6fb392e0642b0ca6b1f186aa3b000000000000000000415557560000000000000000000000000029da4c066b6e03b16a71e77811570dd9e19f258103005500b10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf60000000000000064000000000000003200000009000000006491cc747be59f3f377c0d3f000000000000006300000000000000340436992facb15658a7e9f08c4df4848ca80750f61fadcd96993de66b1fe7aef94e29e3bbef8b12db2305a01e2504d9f0c06e7e7cb0cf24116098ca202ac5f6ade2e8f5a12ec006b16d46be1f0228b94d950055006e1540171b6c0c960b71a7020d9f60077f6af931a8bbf590da0223dacf75c7af000000000000006500000000000000330000000a000000006491cc7504f8554c3620c3fd0000000000000064000000000000003504171ed10ac4f1eacf3a4951e1da6b119f07c45da5adcd96993de66b1fe7aef94e29e3bbef8b12db2305a01e2504d9f0c06e7e7cb0cf24116098ca202ac5f6ade2e8f5a12ec006b16d46be1f0228b94d9500550031ecc21a745e3968a04e9570e4425bc18fa8019c68028196b546d1669c200c68000000000000006600000000000000340000000b000000006491cc76e87d69c7b51242890000000000000065000000000000003604f2ee15ea639b73fa3db9b34a245bdfa015c260c5fe83e4772e0e346613de00e5348158a01bcb27b305a01e2504d9f0c06e7e7cb0cf24116098ca202ac5f6ade2e8f5a12ec006b16d46be1f0228b94d95";
  924. #[test]
  925. fun test_create_and_update_multiple_price_feeds_with_accumulator_success() {
  926. use sui::coin::Self;
  927. let (scenario, coins, clock) = setup_test(500, 23, ACCUMULATOR_TESTS_EMITTER_ADDRESS, ACCUMULATOR_TESTS_DATA_SOURCE(), ACCUMULATOR_TESTS_INITIAL_GUARDIANS, DEFAULT_BASE_UPDATE_FEE, DEFAULT_COIN_TO_MINT);
  928. test_scenario::next_tx(&mut scenario, DEPLOYER);
  929. let (pyth_state, worm_state) = take_wormhole_and_pyth_states(&scenario);
  930. let verified_vaa = get_verified_vaa_from_accumulator_message(&worm_state, TEST_ACCUMULATOR_3_MSGS, &clock);
  931. test_scenario::next_tx(&mut scenario, DEPLOYER);
  932. pyth::create_price_feeds_using_accumulator(
  933. &mut pyth_state,
  934. TEST_ACCUMULATOR_3_MSGS,
  935. verified_vaa,
  936. &clock,
  937. ctx(&mut scenario)
  938. );
  939. // Affirm that 3 objects, which correspond to the 3 new price info objects
  940. // containing the price feeds were created and shared.
  941. let effects = test_scenario::next_tx(&mut scenario, DEPLOYER);
  942. let shared_ids = test_scenario::shared(&effects);
  943. let created_ids = test_scenario::created(&effects);
  944. assert!(vector::length<ID>(&shared_ids)==3, 0);
  945. assert!(vector::length<ID>(&created_ids)==3, 0);
  946. // Create authenticated price infos
  947. verified_vaa = get_verified_vaa_from_accumulator_message(&worm_state, TEST_ACCUMULATOR_3_MSGS, &clock);
  948. let auth_price_infos = pyth::create_authenticated_price_infos_using_accumulator(
  949. &pyth_state,
  950. TEST_ACCUMULATOR_3_MSGS,
  951. verified_vaa,
  952. &clock
  953. );
  954. let idx = 0;
  955. let expected_price_infos = accumulator_test_3_to_price_info(0 /*offset argument*/);
  956. while (idx < 3){
  957. let coin_split = coin::split(&mut coins, 1000, ctx(&mut scenario));
  958. let price_info_object = take_shared<PriceInfoObject>(&scenario);
  959. auth_price_infos = update_single_price_feed(
  960. &pyth_state,
  961. auth_price_infos,
  962. &mut price_info_object,
  963. coin_split,
  964. &clock
  965. );
  966. let price_info = price_info::get_price_info_from_price_info_object(&price_info_object);
  967. assert!(price_feeds_equal(&price_info, vector::borrow(&expected_price_infos, idx)), 0);
  968. return_shared(price_info_object);
  969. idx = idx + 1;
  970. };
  971. coin::burn_for_testing<SUI>(coins);
  972. // clean up test scenario
  973. test_scenario::next_tx(&mut scenario, DEPLOYER);
  974. hot_potato_vector::destroy<PriceInfo>(auth_price_infos);
  975. cleanup_worm_state_pyth_state_and_clock(worm_state, pyth_state, clock);
  976. test_scenario::end(scenario);
  977. }
  978. #[test]
  979. #[expected_failure(abort_code = pyth::pyth::E_INSUFFICIENT_FEE)]
  980. fun test_create_and_update_price_feeds_insufficient_fee() {
  981. // this is not enough fee and will cause a failure
  982. let coins_to_mint = 1;
  983. let (scenario, test_coins, clock) = setup_test(500, 23, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", data_sources_for_test_vaa(), vector[x"beFA429d57cD18b7F8A4d91A2da9AB4AF05d0FBe"], DEFAULT_BASE_UPDATE_FEE, coins_to_mint);
  984. test_scenario::next_tx(&mut scenario, DEPLOYER);
  985. let (pyth_state, worm_state) = take_wormhole_and_pyth_states(&scenario);
  986. let verified_vaas = get_verified_test_vaas(&worm_state, &clock);
  987. test_scenario::next_tx(&mut scenario, DEPLOYER);
  988. pyth::create_price_feeds(
  989. &mut pyth_state,
  990. verified_vaas,
  991. &clock,
  992. ctx(&mut scenario)
  993. );
  994. // Affirm that 4 objects, which correspond to the 4 new price info objects
  995. // containing the price feeds were created and shared.
  996. let effects = test_scenario::next_tx(&mut scenario, DEPLOYER);
  997. let shared_ids = test_scenario::shared(&effects);
  998. let created_ids = test_scenario::created(&effects);
  999. assert!(vector::length<ID>(&shared_ids)==4, 0);
  1000. assert!(vector::length<ID>(&created_ids)==4, 0);
  1001. let price_info_object_1 = take_shared<PriceInfoObject>(&scenario);
  1002. let price_info_object_2 = take_shared<PriceInfoObject>(&scenario);
  1003. let price_info_object_3 = take_shared<PriceInfoObject>(&scenario);
  1004. let price_info_object_4 = take_shared<PriceInfoObject>(&scenario);
  1005. // Create vector of price info objects (Sui objects with key ability and living in global store),
  1006. // which contain the price feeds we want to update. Note that these can be passed into
  1007. // update_price_feeds in any order!
  1008. //let price_info_object_vec = vector[price_info_object_1, price_info_object_2, price_info_object_3, price_info_object_4];
  1009. verified_vaas = get_verified_test_vaas(&worm_state, &clock);
  1010. test_scenario::next_tx(&mut scenario, DEPLOYER);
  1011. let vaa_1 = vector::pop_back<VAA>(&mut verified_vaas);
  1012. test_scenario::next_tx(&mut scenario, DEPLOYER);
  1013. // Create authenticated price infos
  1014. let vec = create_price_infos_hot_potato(
  1015. &pyth_state,
  1016. vector[vaa_1],
  1017. &clock
  1018. );
  1019. test_scenario::next_tx(&mut scenario, DEPLOYER);
  1020. vec = update_single_price_feed(
  1021. &pyth_state,
  1022. vec,
  1023. &mut price_info_object_1,
  1024. test_coins,
  1025. &clock
  1026. );
  1027. test_scenario::next_tx(&mut scenario, DEPLOYER);
  1028. hot_potato_vector::destroy<PriceInfo>(vec);
  1029. vector::destroy_empty(verified_vaas);
  1030. return_shared(price_info_object_1);
  1031. return_shared(price_info_object_2);
  1032. return_shared(price_info_object_3);
  1033. return_shared(price_info_object_4);
  1034. cleanup_worm_state_pyth_state_and_clock(worm_state, pyth_state, clock);
  1035. test_scenario::end(scenario);
  1036. }
  1037. #[test]
  1038. fun test_update_cache(){
  1039. let (scenario, test_coins, clock) = setup_test(500, 23, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", data_sources_for_test_vaa(), BATCH_ATTESTATION_TEST_INITIAL_GUARDIANS, DEFAULT_BASE_UPDATE_FEE, DEFAULT_COIN_TO_MINT);
  1040. test_scenario::next_tx(&mut scenario, DEPLOYER);
  1041. let (pyth_state, worm_state) = take_wormhole_and_pyth_states(&scenario);
  1042. let verified_vaas = get_verified_test_vaas(&worm_state, &clock);
  1043. // Update cache is called by create_price_feeds.
  1044. pyth::create_price_feeds(
  1045. &mut pyth_state,
  1046. verified_vaas,
  1047. &clock,
  1048. ctx(&mut scenario)
  1049. );
  1050. test_scenario::next_tx(&mut scenario, DEPLOYER);
  1051. let price_info_object_1 = take_shared<PriceInfoObject>(&scenario);
  1052. let price_info_object_2 = take_shared<PriceInfoObject>(&scenario);
  1053. let price_info_object_3 = take_shared<PriceInfoObject>(&scenario);
  1054. let price_info_object_4 = take_shared<PriceInfoObject>(&scenario);
  1055. // These updates are price infos that correspond to the ones in TEST_VAAS.
  1056. let updates = get_mock_price_infos();
  1057. let price_info_object_vec = vector[
  1058. price_info_object_1,
  1059. price_info_object_2,
  1060. price_info_object_3,
  1061. price_info_object_4
  1062. ];
  1063. // Check that TEST_VAAS was indeed used to instantiate the price feeds correctly,
  1064. // by confirming that the info in updates is contained in price_info_object_vec.
  1065. check_price_feeds_cached(&updates, &price_info_object_vec);
  1066. price_info_object_4 = vector::pop_back(&mut price_info_object_vec);
  1067. price_info_object_3 = vector::pop_back(&mut price_info_object_vec);
  1068. price_info_object_2 = vector::pop_back(&mut price_info_object_vec);
  1069. price_info_object_1 = vector::pop_back(&mut price_info_object_vec);
  1070. vector::destroy_empty(price_info_object_vec);
  1071. return_shared(price_info_object_1);
  1072. return_shared(price_info_object_2);
  1073. return_shared(price_info_object_3);
  1074. return_shared(price_info_object_4);
  1075. coin::burn_for_testing<SUI>(test_coins);
  1076. cleanup_worm_state_pyth_state_and_clock(worm_state, pyth_state, clock);
  1077. test_scenario::end(scenario);
  1078. }
  1079. #[test]
  1080. fun test_update_cache_old_update() {
  1081. use pyth::i64::Self;
  1082. use pyth::price::Self;
  1083. let (scenario, test_coins, clock) = setup_test(500, 23, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", data_sources_for_test_vaa(), BATCH_ATTESTATION_TEST_INITIAL_GUARDIANS, DEFAULT_BASE_UPDATE_FEE, DEFAULT_COIN_TO_MINT);
  1084. test_scenario::next_tx(&mut scenario, DEPLOYER);
  1085. let (pyth_state, worm_state) = take_wormhole_and_pyth_states(&scenario);
  1086. let verified_vaas = get_verified_test_vaas(&worm_state, &clock);
  1087. pyth::create_price_feeds(
  1088. &mut pyth_state,
  1089. verified_vaas,
  1090. &clock,
  1091. ctx(&mut scenario)
  1092. );
  1093. test_scenario::next_tx(&mut scenario, DEPLOYER);
  1094. let price_info_object_1 = take_shared<PriceInfoObject>(&scenario);
  1095. let price_info_object_2 = take_shared<PriceInfoObject>(&scenario);
  1096. let price_info_object_3 = take_shared<PriceInfoObject>(&scenario);
  1097. let price_info_object_4 = take_shared<PriceInfoObject>(&scenario);
  1098. // Hardcode the price identifier, price, and ema_price for price_info_object_1, because
  1099. // it's easier than unwrapping price_info_object_1 and getting the quantities via getters.
  1100. let timestamp = 1663680740;
  1101. let price_identifier = price_identifier::from_byte_vec(x"c6c75c89f14810ec1c54c03ab8f1864a4c4032791f05747f560faec380a695d1");
  1102. let price = price::new(i64::new(1557, false), 7, i64::new(5, true), timestamp);
  1103. let ema_price = price::new(i64::new(1500, false), 3, i64::new(5, true), timestamp);
  1104. // Attempt to update the price with an update older than the current cached one.
  1105. let old_price = price::new(i64::new(1243, true), 9802, i64::new(6, false), timestamp - 200);
  1106. let old_ema_price = price::new(i64::new(8976, true), 234, i64::new(897, false), timestamp - 200);
  1107. let old_update = price_info::new_price_info(
  1108. 1257278600,
  1109. 1690226180,
  1110. price_feed::new(
  1111. price_identifier,
  1112. old_price,
  1113. old_ema_price,
  1114. )
  1115. );
  1116. let latest_only = pyth::state::create_latest_only_for_test();
  1117. pyth::update_cache(latest_only, &old_update, &mut price_info_object_1, &clock);
  1118. let current_price_info = price_info::get_price_info_from_price_info_object(&price_info_object_1);
  1119. let current_price_feed = price_info::get_price_feed(&current_price_info);
  1120. let current_price = price_feed::get_price(current_price_feed);
  1121. let current_ema_price = price_feed::get_ema_price(current_price_feed);
  1122. // Confirm that no price update occurred when we tried to update cache with an
  1123. // outdated update: old_update.
  1124. assert!(current_price == price, 1);
  1125. assert!(current_ema_price == ema_price, 1);
  1126. test_scenario::next_tx(&mut scenario, DEPLOYER);
  1127. // Update the cache with a fresh update.
  1128. let fresh_price = price::new(i64::new(5243, true), 2, i64::new(3, false), timestamp + 200);
  1129. let fresh_ema_price = price::new(i64::new(8976, true), 21, i64::new(32, false), timestamp + 200);
  1130. let fresh_update = price_info::new_price_info(
  1131. 1257278600,
  1132. 1690226180,
  1133. price_feed::new(
  1134. price_identifier,
  1135. fresh_price,
  1136. fresh_ema_price,
  1137. )
  1138. );
  1139. let latest_only = pyth::state::create_latest_only_for_test();
  1140. pyth::update_cache(latest_only, &fresh_update, &mut price_info_object_1, &clock);
  1141. // Confirm that the Pyth cached price got updated to fresh_price.
  1142. let current_price_info = price_info::get_price_info_from_price_info_object(&price_info_object_1);
  1143. let current_price_feed = price_info::get_price_feed(&current_price_info);
  1144. let current_price = price_feed::get_price(current_price_feed);
  1145. let current_ema_price = price_feed::get_ema_price(current_price_feed);
  1146. assert!(current_price==fresh_price, 0);
  1147. assert!(current_ema_price==fresh_ema_price, 0);
  1148. return_shared(price_info_object_1);
  1149. return_shared(price_info_object_2);
  1150. return_shared(price_info_object_3);
  1151. return_shared(price_info_object_4);
  1152. coin::burn_for_testing<SUI>(test_coins);
  1153. cleanup_worm_state_pyth_state_and_clock(worm_state, pyth_state, clock);
  1154. test_scenario::end(scenario);
  1155. }
  1156. // pyth accumulator tests (included in this file instead of pyth_accumulator.move to avoid dependency cycle - as we need pyth_tests::setup_test)
  1157. #[test]
  1158. fun test_parse_and_verify_accumulator_updates(){
  1159. use sui::test_scenario::{Self, take_shared, return_shared};
  1160. use sui::transfer::{Self};
  1161. let (scenario, coins, clock) = setup_test(500, 23, ACCUMULATOR_TESTS_EMITTER_ADDRESS, vector[], ACCUMULATOR_TESTS_INITIAL_GUARDIANS, 50, 0);
  1162. let worm_state = take_shared<WormState>(&scenario);
  1163. test_scenario::next_tx(&mut scenario, @0x123);
  1164. let verified_vaa = get_verified_vaa_from_accumulator_message(&worm_state, TEST_ACCUMULATOR_3_MSGS, &clock);
  1165. let cur = cursor::new(TEST_ACCUMULATOR_3_MSGS);
  1166. let price_info_updates = accumulator::parse_and_verify_accumulator_message(&mut cur, vaa::take_payload(verified_vaa), &clock);
  1167. let expected_price_infos = accumulator_test_3_to_price_info(0);
  1168. let num_updates = vector::length<PriceInfo>(&price_info_updates);
  1169. let i = 0;
  1170. while (i < num_updates){
  1171. assert!(price_feeds_equal(vector::borrow(&price_info_updates, i), vector::borrow(&expected_price_infos, i)), 0);
  1172. i = i + 1;
  1173. };
  1174. // clean-up
  1175. cursor::take_rest(cur);
  1176. transfer::public_transfer(coins, @0x1234);
  1177. clock::destroy_for_testing(clock);
  1178. return_shared(worm_state);
  1179. test_scenario::end(scenario);
  1180. }
  1181. #[test]
  1182. fun test_parse_and_verify_accumulator_updates_with_extra_bytes_at_end_of_message(){
  1183. use sui::test_scenario::{Self, take_shared, return_shared};
  1184. use sui::transfer::{Self};
  1185. let (scenario, coins, clock) = setup_test(500, 23, ACCUMULATOR_TESTS_EMITTER_ADDRESS, vector[], ACCUMULATOR_TESTS_INITIAL_GUARDIANS, 50, 0);
  1186. let worm_state = take_shared<WormState>(&scenario);
  1187. test_scenario::next_tx(&mut scenario, @0x123);
  1188. let verified_vaa = get_verified_vaa_from_accumulator_message(&worm_state, TEST_ACCUMULATOR_3_MSGS, &clock);
  1189. // append some extra garbage bytes at the end of the accumulator message, and make sure
  1190. // that parse_and_verify_accumulator_message does not error out
  1191. let test_accumulator_3_msgs_modified = TEST_ACCUMULATOR_3_MSGS;
  1192. vector::append(&mut test_accumulator_3_msgs_modified, x"1234123412341234");
  1193. let cur = cursor::new(TEST_ACCUMULATOR_3_MSGS);
  1194. let price_info_updates = accumulator::parse_and_verify_accumulator_message(&mut cur, vaa::take_payload(verified_vaa), &clock);
  1195. let expected_price_infos = accumulator_test_3_to_price_info(0);
  1196. let num_updates = vector::length<PriceInfo>(&price_info_updates);
  1197. let i = 0;
  1198. while (i < num_updates){
  1199. assert!(price_feeds_equal(vector::borrow(&price_info_updates, i), vector::borrow(&expected_price_infos, i)), 0);
  1200. i = i + 1;
  1201. };
  1202. // clean-up
  1203. cursor::take_rest(cur);
  1204. transfer::public_transfer(coins, @0x1234);
  1205. clock::destroy_for_testing(clock);
  1206. return_shared(worm_state);
  1207. test_scenario::end(scenario);
  1208. }
  1209. fun price_feeds_equal(p1: &PriceInfo, p2: &PriceInfo): bool{
  1210. price_info::get_price_feed(p1)== price_info::get_price_feed(p2)
  1211. }
  1212. // helper functions for setting up tests
  1213. // accumulator_test_3_to_price_info gets the data encoded within TEST_ACCUMULATOR_3_MSGS
  1214. fun accumulator_test_3_to_price_info(offset: u64): vector<PriceInfo> {
  1215. use pyth::i64::{Self};
  1216. use pyth::price::{Self};
  1217. let i = 0;
  1218. let feed_ids = vector[x"b10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf6",
  1219. x"6e1540171b6c0c960b71a7020d9f60077f6af931a8bbf590da0223dacf75c7af",
  1220. x"31ecc21a745e3968a04e9570e4425bc18fa8019c68028196b546d1669c200c68"];
  1221. let expected: vector<PriceInfo> = vector[];
  1222. while (i < 3) {
  1223. vector::push_back(&mut expected, price_info::new_price_info(
  1224. 1663680747,
  1225. 1663074349,
  1226. price_feed::new(
  1227. price_identifier::from_byte_vec(
  1228. *vector::borrow(&feed_ids, i)
  1229. ),
  1230. price::new(
  1231. i64::new(100 + i + offset, false),
  1232. 50 + i + offset,
  1233. i64::new(9 + i + offset, false),
  1234. 1687276660 + i + offset
  1235. ),
  1236. price::new(
  1237. i64::new(99 + i + offset, false),
  1238. 52 + i + offset,
  1239. i64::new(9 + i + offset, false),
  1240. 1687276660 + i + offset
  1241. ),
  1242. ),
  1243. ));
  1244. i = i + 1;
  1245. };
  1246. return expected
  1247. }
  1248. // accumulator_test_1_to_price_info gets the data encoded within TEST_ACCUMULATOR_SINGLE_FEED
  1249. fun accumulator_test_1_to_price_info(): PriceInfo {
  1250. use pyth::i64::{Self};
  1251. use pyth::price::{Self};
  1252. price_info::new_price_info(
  1253. 1663680747,
  1254. 1663074349,
  1255. price_feed::new(
  1256. price_identifier::from_byte_vec(
  1257. x"b10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf6"
  1258. ),
  1259. price::new(
  1260. i64::new(6887568746747646632, false),
  1261. 13092246197863718329,
  1262. i64::new(1559537863, false),
  1263. 1687276661
  1264. ),
  1265. price::new(
  1266. i64::new(4772242609775910581, false),
  1267. 358129956189946877,
  1268. i64::new(1559537863, false),
  1269. 1687276661
  1270. ),
  1271. ),
  1272. )
  1273. }
  1274. public fun cleanup_worm_state_pyth_state_and_clock(worm_state: WormState, pyth_state: PythState, clock: Clock){
  1275. return_shared(worm_state);
  1276. return_shared(pyth_state);
  1277. clock::destroy_for_testing(clock);
  1278. }
  1279. public fun take_wormhole_and_pyth_states(scenario: &Scenario): (PythState, WormState){
  1280. (take_shared<PythState>(scenario), take_shared<WormState>(scenario))
  1281. }
  1282. }