pyth.move 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. module pyth::pyth {
  2. use pyth::batch_price_attestation::{Self};
  3. use pyth::price_identifier::{Self, PriceIdentifier};
  4. use pyth::price_info::{Self, PriceInfo};
  5. use pyth::price_feed::{Self};
  6. use aptos_framework::coin::{Self, Coin, BurnCapability, MintCapability};
  7. use aptos_framework::aptos_coin::{Self, AptosCoin};
  8. use pyth::i64;
  9. use pyth::price::Price;
  10. use pyth::price;
  11. use pyth::data_source::{Self, DataSource};
  12. use aptos_framework::timestamp;
  13. use std::vector;
  14. use pyth::state;
  15. use wormhole::vaa;
  16. use wormhole::u16;
  17. use wormhole::external_address;
  18. use std::account;
  19. use deployer::deployer;
  20. use pyth::error;
  21. // -----------------------------------------------------------------------------
  22. // Initialisation functions
  23. public entry fun init(
  24. deployer: &signer,
  25. chain_id: u64,
  26. stale_price_threshold: u64,
  27. governance_emitter_chain_id: u64,
  28. governance_emitter_address: vector<u8>,
  29. update_fee: u64,
  30. ) {
  31. // Claim the signer capability from the deployer. Note that this is a one-time operation,
  32. // so that this function can only be called once.
  33. let signer_capability = deployer::claim_signer_capability(deployer, @pyth);
  34. init_internal(
  35. signer_capability,
  36. chain_id,
  37. stale_price_threshold,
  38. governance_emitter_chain_id,
  39. governance_emitter_address,
  40. update_fee
  41. )
  42. }
  43. fun init_internal(
  44. signer_capability: account::SignerCapability,
  45. chain_id: u64,
  46. stale_price_threshold: u64,
  47. governance_emitter_chain_id: u64,
  48. governance_emitter_address: vector<u8>,
  49. update_fee: u64) {
  50. let pyth = account::create_signer_with_capability(&signer_capability);
  51. state::init(
  52. &pyth,
  53. chain_id,
  54. stale_price_threshold,
  55. update_fee,
  56. data_source::new(
  57. governance_emitter_chain_id,
  58. external_address::from_bytes(governance_emitter_address)),
  59. signer_capability
  60. );
  61. coin::register<AptosCoin>(&pyth);
  62. }
  63. #[test_only]
  64. /// Expose a public initialization function for use in tests
  65. public fun init_test(
  66. signer_capability: account::SignerCapability,
  67. chain_id: u64,
  68. stale_price_threshold: u64,
  69. governance_emitter_chain_id: u64,
  70. governance_emitter_address: vector<u8>,
  71. update_fee: u64,
  72. ) {
  73. init_internal(
  74. signer_capability,
  75. chain_id,
  76. stale_price_threshold,
  77. governance_emitter_chain_id,
  78. governance_emitter_address,
  79. update_fee
  80. )
  81. }
  82. // -----------------------------------------------------------------------------
  83. // Update the cached prices
  84. /// Update the cached price feeds with the data in the given vaa_bytes payload.
  85. /// The given fee must contain a sufficient number of coins to pay the update fee:
  86. /// this amount can be queried by calling get_update_fee().
  87. public entry fun update_price_feeds(vaa_bytes: vector<u8>, fee: Coin<AptosCoin>) {
  88. // Deserialize the VAA
  89. let vaa = vaa::parse_and_verify(vaa_bytes);
  90. // Charge the message update fee
  91. assert!(state::get_update_fee() <= coin::value(&fee), error::insufficient_fee());
  92. coin::deposit(@pyth, fee);
  93. // Check that the VAA is from a valid data source (emitter)
  94. assert!(
  95. state::is_valid_data_source(
  96. data_source::new(
  97. u16::to_u64(vaa::get_emitter_chain(&vaa)),
  98. vaa::get_emitter_address(&vaa))),
  99. error::invalid_data_source());
  100. // Deserialize the batch price attestation
  101. update_cache(batch_price_attestation::destroy(
  102. batch_price_attestation::deserialize(vaa::destroy(vaa))));
  103. }
  104. /// Update the cache with given price updates, if they are newer than the ones currently cached.
  105. fun update_cache(updates: vector<PriceInfo>) {
  106. while (!vector::is_empty(&updates)) {
  107. let update = vector::pop_back(&mut updates);
  108. if (is_fresh_update(&update)) {
  109. let price_feed = price_info::get_price_feed(&update);
  110. let price_identifier = price_feed::get_price_identifier(price_feed);
  111. state::set_latest_price_info(
  112. *price_identifier,
  113. update,
  114. )
  115. }
  116. };
  117. vector::destroy_empty(updates);
  118. }
  119. /// Update the cached price feeds with the data in the given vaa_bytes payload, using
  120. /// update_price_feeds(). However, this function will only have an effect if any of the
  121. /// prices in the update are fresh. The price_identifiers and publish_times paramaters
  122. /// are used to determine if the update is fresh without doing any serialisation or verification
  123. /// of the VAA, potentially saving time and gas. If the update contains no fresh data, this function
  124. /// will revert with error::no_fresh_data().
  125. ///
  126. /// For a given price update i in the batch, that price is considered fresh if the current cached
  127. /// price for price_identifiers[i] is older than publish_times[i].
  128. public entry fun update_price_feeds_if_fresh(
  129. vaa_bytes: vector<u8>,
  130. price_identifiers: vector<PriceIdentifier>,
  131. publish_times: vector<u64>,
  132. fee: Coin<AptosCoin>) {
  133. assert!(vector::length(&price_identifiers) == vector::length(&publish_times),
  134. error::invalid_publish_times_length());
  135. let fresh_data = false;
  136. let i = 0;
  137. while (i < vector::length(&publish_times)) {
  138. let price_identifier = vector::borrow(&price_identifiers, i);
  139. if (!state::price_info_cached(*price_identifier)) {
  140. fresh_data = true;
  141. break
  142. };
  143. let cached_timestamp = price::get_timestamp(&get_price_unsafe(*price_identifier));
  144. if (cached_timestamp < *vector::borrow(&publish_times, i)) {
  145. fresh_data = true;
  146. break
  147. };
  148. i = i + 1;
  149. };
  150. assert!(fresh_data, error::no_fresh_data());
  151. update_price_feeds(vaa_bytes, fee);
  152. }
  153. /// Determine if the given price update is "fresh": we have nothing newer already cached for that
  154. /// price feed.
  155. fun is_fresh_update(update: &PriceInfo): bool {
  156. // Get the timestamp of the update's current price
  157. let price_feed = price_info::get_price_feed(update);
  158. let update_timestamp = price::get_timestamp(&price_feed::get_price(price_feed));
  159. // Get the timestamp of the cached data for the price identifier
  160. let price_identifier = price_feed::get_price_identifier(price_feed);
  161. if (!price_feed_exists(*price_identifier)) {
  162. return true
  163. };
  164. let cached_timestamp = price::get_timestamp(&get_price_unsafe(*price_identifier));
  165. update_timestamp > cached_timestamp
  166. }
  167. // -----------------------------------------------------------------------------
  168. // Query the cached prices
  169. //
  170. // It is strongly recommended to update the cached prices using the functions above,
  171. // before using the functions below to query the cached data.
  172. /// Determine if a price feed for the given price_identifier exists
  173. public fun price_feed_exists(price_identifier: PriceIdentifier): bool {
  174. state::price_info_cached(price_identifier)
  175. }
  176. /// Get the latest cached data for the given price identifier, if that data is
  177. /// no older than the stale price threshold.
  178. ///
  179. /// Important: it is recommended to call update_price_feeds() to update the cached data
  180. /// before calling this function.
  181. public fun get_price(price_identifier: PriceIdentifier): Price {
  182. get_price_no_older_than(price_identifier, state::get_stale_price_threshold_secs())
  183. }
  184. /// Get the latest cached data for the given price identifier, if that data is
  185. /// no older than the given age.
  186. public fun get_price_no_older_than(price_identifier: PriceIdentifier, max_age_secs: u64): Price {
  187. let price = get_price_unsafe(price_identifier);
  188. let age = abs_diff(timestamp::now_seconds(), price::get_timestamp(&price));
  189. assert!(age < max_age_secs, error::stale_price_update());
  190. price
  191. }
  192. /// Get the latest cached exponential moving average price for the given price_identifier.
  193. public fun get_ema_price(price_identifier: PriceIdentifier): Price {
  194. price_feed::get_ema_price(
  195. price_info::get_price_feed(&state::get_latest_price_info(price_identifier)))
  196. }
  197. /// Get the latest available price for the given price identifier.
  198. ///
  199. /// WARNING: the returned price can be from arbitrarily far in the past.
  200. /// This function makes no guarantees that the returned price is recent or
  201. /// useful for any particular application. Users of this function should check
  202. /// the returned timestamp to ensure that the returned price is sufficiently
  203. /// recent for their application. The checked get_price_no_older_than()
  204. /// function should be used in preference to this.
  205. public fun get_price_unsafe(price_identifier: PriceIdentifier): Price {
  206. price_feed::get_price(
  207. price_info::get_price_feed(&state::get_latest_price_info(price_identifier)))
  208. }
  209. fun abs_diff(x: u64, y: u64): u64 {
  210. if (x > y) {
  211. return x - y
  212. } else {
  213. return y - x
  214. }
  215. }
  216. /// Get the stale price threshold: the amount of time after which a cached price
  217. /// is considered stale and no longer returned by get_price().
  218. public fun get_stale_price_threshold_secs(): u64 {
  219. state::get_stale_price_threshold_secs()
  220. }
  221. /// Get the number of AptosCoin's required to perform one batch update
  222. public fun get_update_fee(): u64 {
  223. state::get_update_fee()
  224. }
  225. // -----------------------------------------------------------------------------
  226. // Tests
  227. #[test_only]
  228. fun setup_test(
  229. aptos_framework: &signer,
  230. chain_id: u64,
  231. stale_price_threshold: u64,
  232. governance_emitter_chain_id: u64,
  233. governance_emitter_address: vector<u8>,
  234. update_fee: u64,
  235. to_mint: u64): (BurnCapability<AptosCoin>, MintCapability<AptosCoin>, Coin<AptosCoin>) {
  236. // Initialize wormhole with a large message collection fee
  237. wormhole::wormhole_test::setup(100000);
  238. // Set the current time
  239. timestamp::update_global_time_for_test_secs(1663680745);
  240. // Deploy and initialize a test instance of the Pyth contract
  241. let deployer = account::create_signer_with_capability(&
  242. account::create_test_signer_cap(@0x277fa055b6a73c42c0662d5236c65c864ccbf2d4abd21f174a30c8b786eab84b));
  243. let (_pyth, signer_capability) = account::create_resource_account(&deployer, b"pyth");
  244. init_test(signer_capability, chain_id, stale_price_threshold, governance_emitter_chain_id, governance_emitter_address, update_fee);
  245. let (burn_capability, mint_capability) = aptos_coin::initialize_for_test(aptos_framework);
  246. let coins = coin::mint(to_mint, &mint_capability);
  247. (burn_capability, mint_capability, coins)
  248. }
  249. #[test_only]
  250. fun cleanup_test(burn_capability: BurnCapability<AptosCoin>, mint_capability: MintCapability<AptosCoin>) {
  251. coin::destroy_mint_cap(mint_capability);
  252. coin::destroy_burn_cap(burn_capability);
  253. }
  254. #[test_only]
  255. fun get_mock_price_infos(): vector<PriceInfo> {
  256. vector<PriceInfo>[
  257. price_info::new(
  258. 1663680747,
  259. 1663074349,
  260. price_feed::new(
  261. price_identifier::from_byte_vec(x"c6c75c89f14810ec1c54c03ab8f1864a4c4032791f05747f560faec380a695d1"),
  262. price::new(i64::new(1557, false), 7, i64::new(5, true), 1663680740),
  263. price::new(i64::new(1500, false), 3, i64::new(5, true), 1663680740),
  264. ),
  265. ),
  266. price_info::new(
  267. 1663680747,
  268. 1663074349,
  269. price_feed::new(
  270. price_identifier::from_byte_vec(x"3b9551a68d01d954d6387aff4df1529027ffb2fee413082e509feb29cc4904fe"),
  271. price::new(i64::new(1050, false), 3, i64::new(5, true), 1663680745),
  272. price::new(i64::new(1483, false), 3, i64::new(5, true), 1663680745),
  273. ),
  274. ),
  275. price_info::new(
  276. 1663680747,
  277. 1663074349,
  278. price_feed::new(
  279. price_identifier::from_byte_vec(x"33832fad6e36eb05a8972fe5f219b27b5b2bb2230a79ce79beb4c5c5e7ecc76d"),
  280. price::new(i64::new(1010, false), 2, i64::new(5, true), 1663680745),
  281. price::new(i64::new(1511, false), 3, i64::new(5, true), 1663680745),
  282. ),
  283. ),
  284. price_info::new(
  285. 1663680747,
  286. 1663074349,
  287. price_feed::new(
  288. price_identifier::from_byte_vec(x"21a28b4c6619968bd8c20e95b0aaed7df2187fd310275347e0376a2cd7427db8"),
  289. price::new(i64::new(1739, false), 1, i64::new(5, true), 1663680745),
  290. price::new(i64::new(1508, false), 3, i64::new(5, true), 1663680745),
  291. ),
  292. ),
  293. ]
  294. }
  295. #[test_only]
  296. /// A VAA with:
  297. /// - emitter chain ID 17
  298. /// - emitter address 0x71f8dcb863d176e2c420ad6610cf687359612b6fb392e0642b0ca6b1f186aa3b
  299. /// - payload corresponding to the batch price attestation of the prices returned by get_mock_price_infos()
  300. const TEST_VAA: vector<u8> = x"0100000000010036eb563b80a24f4253bee6150eb8924e4bdf6e4fa1dfc759a6664d2e865b4b134651a7b021b7f1ce3bd078070b688b6f2e37ce2de0d9b48e6a78684561e49d5201527e4f9b00000001001171f8dcb863d176e2c420ad6610cf687359612b6fb392e0642b0ca6b1f186aa3b0000000000000001005032574800030000000102000400951436e0be37536be96f0896366089506a59763d036728332d3e3038047851aea7c6c75c89f14810ec1c54c03ab8f1864a4c4032791f05747f560faec380a695d1000000000000049a0000000000000008fffffffb00000000000005dc0000000000000003000000000100000001000000006329c0eb000000006329c0e9000000006329c0e400000000000006150000000000000007215258d81468614f6b7e194c5d145609394f67b041e93e6695dcc616faadd0603b9551a68d01d954d6387aff4df1529027ffb2fee413082e509feb29cc4904fe000000000000041a0000000000000003fffffffb00000000000005cb0000000000000003010000000100000001000000006329c0eb000000006329c0e9000000006329c0e4000000000000048600000000000000078ac9cf3ab299af710d735163726fdae0db8465280502eb9f801f74b3c1bd190333832fad6e36eb05a8972fe5f219b27b5b2bb2230a79ce79beb4c5c5e7ecc76d00000000000003f20000000000000002fffffffb00000000000005e70000000000000003010000000100000001000000006329c0eb000000006329c0e9000000006329c0e40000000000000685000000000000000861db714e9ff987b6fedf00d01f9fea6db7c30632d6fc83b7bc9459d7192bc44a21a28b4c6619968bd8c20e95b0aaed7df2187fd310275347e0376a2cd7427db800000000000006cb0000000000000001fffffffb00000000000005e40000000000000003010000000100000001000000006329c0eb000000006329c0e9000000006329c0e400000000000007970000000000000001";
  301. #[test(aptos_framework = @aptos_framework)]
  302. #[expected_failure(abort_code = 6)]
  303. fun test_update_price_feeds_corrupt_vaa(aptos_framework: &signer) {
  304. let (burn_capability, mint_capability, coins) = setup_test(aptos_framework, 27, 500, 23, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", 50, 100);
  305. // Pass in a corrupt VAA, which should fail deseriaizing
  306. let corrupt_vaa = x"90F8bf6A479f320ead074411a4B0e7944Ea8c9C1";
  307. update_price_feeds(corrupt_vaa, coins);
  308. cleanup_test(burn_capability, mint_capability);
  309. }
  310. #[test(aptos_framework = @aptos_framework)]
  311. #[expected_failure(abort_code = 65539)]
  312. fun test_update_price_feeds_invalid_data_source_initially(aptos_framework: &signer) {
  313. let (burn_capability, mint_capability, coins) = setup_test(aptos_framework, 27, 500, 1, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", 50, 100);
  314. // Without setting any valid data source, the check should fail
  315. update_price_feeds(TEST_VAA, coins);
  316. cleanup_test(burn_capability, mint_capability);
  317. }
  318. #[test(aptos_framework = @aptos_framework)]
  319. #[expected_failure(abort_code = 65539)]
  320. fun test_update_price_feeds_invalid_data_source(aptos_framework: &signer) {
  321. let (burn_capability, mint_capability, coins) = setup_test(aptos_framework, 27, 500, 1, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", 50, 100);
  322. // Set some valid data sources, excluding our test VAA's source
  323. state::set_data_sources(vector<DataSource>[
  324. data_source::new(
  325. 4, external_address::from_bytes(x"0000000000000000000000000000000000000000000000000000000000007742")),
  326. data_source::new(
  327. 5, external_address::from_bytes(x"0000000000000000000000000000000000000000000000000000000000007637"))
  328. ]);
  329. update_price_feeds(TEST_VAA, coins);
  330. cleanup_test(burn_capability, mint_capability);
  331. }
  332. #[test_only]
  333. fun set_data_source_for_test_vaa() {
  334. // Set some valid data sources, including our test VAA's source
  335. state::set_data_sources(vector<DataSource>[
  336. data_source::new(
  337. 1, external_address::from_bytes(x"0000000000000000000000000000000000000000000000000000000000000004")),
  338. data_source::new(
  339. 5, external_address::from_bytes(x"0000000000000000000000000000000000000000000000000000000000007637")),
  340. data_source::new(
  341. 17, external_address::from_bytes(x"71f8dcb863d176e2c420ad6610cf687359612b6fb392e0642b0ca6b1f186aa3b"))
  342. ]);
  343. }
  344. #[test(aptos_framework = @aptos_framework)]
  345. #[expected_failure(abort_code = 65542)]
  346. fun test_update_price_feeds_insufficient_fee(aptos_framework: &signer) {
  347. let (burn_capability, mint_capability, coins) = setup_test(aptos_framework, 27, 500, 1,
  348. x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92",
  349. // Update fee
  350. 50,
  351. // Coins provided to update < update fee
  352. 20);
  353. // Set some valid data sources, excluding our test VAA's source
  354. state::set_data_sources(vector<DataSource>[
  355. data_source::new(
  356. 1, external_address::from_bytes(x"0000000000000000000000000000000000000000000000000000000000000004")),
  357. data_source::new(
  358. 5, external_address::from_bytes(x"0000000000000000000000000000000000000000000000000000000000007637"))
  359. ]);
  360. update_price_feeds(TEST_VAA, coins);
  361. cleanup_test(burn_capability, mint_capability);
  362. }
  363. #[test(aptos_framework = @aptos_framework)]
  364. fun test_update_price_feeds_success(aptos_framework: &signer) {
  365. let (burn_capability, mint_capability, coins) = setup_test(aptos_framework, 27, 500, 1, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", 50, 100);
  366. set_data_source_for_test_vaa();
  367. // Update the price feeds from the VAA
  368. update_price_feeds(TEST_VAA, coins);
  369. // Check that the cache has been updated
  370. let expected = get_mock_price_infos();
  371. check_price_feeds_cached(&expected);
  372. cleanup_test(burn_capability, mint_capability);
  373. }
  374. #[test_only]
  375. fun check_price_feeds_cached(expected: &vector<PriceInfo>) {
  376. // Check that we can retrieve the correct current price and ema price for each price feed
  377. let i = 0;
  378. while (i < vector::length(expected)) {
  379. let price_feed = price_info::get_price_feed(vector::borrow(expected, i));
  380. let price = price_feed::get_price(price_feed);
  381. let price_identifier = *price_feed::get_price_identifier(price_feed);
  382. assert!(price_feed_exists(price_identifier), 1);
  383. let cached_price = get_price(price_identifier);
  384. assert!(cached_price == price, 1);
  385. let ema_price = price_feed::get_ema_price(price_feed);
  386. let cached_ema_price = get_ema_price(price_identifier);
  387. assert!(cached_ema_price == ema_price, 1);
  388. i = i + 1;
  389. };
  390. }
  391. #[test(aptos_framework = @aptos_framework)]
  392. fun test_update_cache(aptos_framework: &signer) {
  393. let (burn_capability, mint_capability, coins) = setup_test(aptos_framework, 27, 500, 1, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", 50, 0);
  394. let updates = get_mock_price_infos();
  395. // Check that initially the price feeds are not cached
  396. let i = 0;
  397. while (i < vector::length(&updates)) {
  398. let price_feed = price_info::get_price_feed(vector::borrow(&updates, i));
  399. assert!(!price_feed_exists(*price_feed::get_price_identifier(price_feed)), 1);
  400. i = i + 1;
  401. };
  402. // Submit the updates
  403. update_cache(updates);
  404. // Check that the price feeds are now cached
  405. check_price_feeds_cached(&updates);
  406. cleanup_test(burn_capability, mint_capability);
  407. coin::destroy_zero(coins);
  408. }
  409. #[test(aptos_framework = @aptos_framework)]
  410. fun test_update_cache_old_update(aptos_framework: &signer) {
  411. let (burn_capability, mint_capability, coins) = setup_test(aptos_framework, 27, 500, 1, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", 50, 0);
  412. // Submit a price update
  413. let timestamp = 1663680700;
  414. let price_identifier = price_identifier::from_byte_vec(x"baa284eaf23edf975b371ba2818772f93dbae72836bbdea28b07d40f3cf8b485");
  415. let price = price::new(i64::new(7648, false), 674, i64::new(8, true), timestamp);
  416. let ema_price = price::new(i64::new(1536, true), 869, i64::new(100, false), 1257212500);
  417. let update = price_info::new(
  418. 1257278600,
  419. 1690226180,
  420. price_feed::new(
  421. price_identifier,
  422. price,
  423. ema_price,
  424. )
  425. );
  426. update_cache(vector<PriceInfo>[update]);
  427. // Check that we can retrieve the current price
  428. assert!(get_price(price_identifier) == price, 1);
  429. // Attempt to update the price with an update older than the current cached one
  430. let old_price = price::new(i64::new(1243, true), 9802, i64::new(6, false), timestamp - 200);
  431. let old_ema_price = price::new(i64::new(8976, true), 234, i64::new(897, false), timestamp - 200);
  432. let old_update = price_info::new(
  433. 1257278600,
  434. 1690226180,
  435. price_feed::new(
  436. price_identifier,
  437. old_price,
  438. old_ema_price,
  439. )
  440. );
  441. update_cache(vector<PriceInfo>[old_update]);
  442. // Confirm that the current price and ema price didn't change
  443. assert!(get_price(price_identifier) == price, 1);
  444. assert!(get_ema_price(price_identifier) == ema_price, 1);
  445. // Update the cache with a fresh update
  446. let fresh_price = price::new(i64::new(4857, true), 9979, i64::new(243, false), timestamp + 200);
  447. let fresh_ema_price = price::new(i64::new(74637, false), 9979, i64::new(1433, false), timestamp + 1);
  448. let fresh_update = price_info::new(
  449. 1257278600,
  450. 1690226180,
  451. price_feed::new(
  452. price_identifier,
  453. fresh_price,
  454. fresh_ema_price,
  455. )
  456. );
  457. update_cache(vector<PriceInfo>[fresh_update]);
  458. // Confirm that the current price was updated
  459. assert!(get_price(price_identifier) == fresh_price, 1);
  460. assert!(get_ema_price(price_identifier) == fresh_ema_price, 1);
  461. cleanup_test(burn_capability, mint_capability);
  462. coin::destroy_zero(coins);
  463. }
  464. #[test(aptos_framework = @aptos_framework)]
  465. #[expected_failure(abort_code = 524292)]
  466. fun test_stale_price_threshold_exceeded(aptos_framework: &signer) {
  467. let stale_price_threshold = 500;
  468. let (burn_capability, mint_capability, coins) = setup_test(aptos_framework, 27, stale_price_threshold, 1, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", 50, 0);
  469. // Submit a price update
  470. let current_timestamp = timestamp::now_seconds();
  471. let price_identifier = price_identifier::from_byte_vec(x"baa284eaf23edf975b371ba2818772f93dbae72836bbdea28b07d40f3cf8b485");
  472. let price = price::new(i64::new(7648, false), 674, i64::new(8, true), current_timestamp);
  473. let update = price_info::new(
  474. 1257278600,
  475. 1690226180,
  476. price_feed::new(
  477. price_identifier,
  478. price,
  479. price::new(i64::new(1536, true), 869, i64::new(100, false), 1257212500),
  480. )
  481. );
  482. update_cache(vector<PriceInfo>[update]);
  483. assert!(get_price(price_identifier) == price, 1);
  484. // Now advance the clock on the target chain, until the age of the cached update exceeds the
  485. // stale_price_threshold.
  486. timestamp::update_global_time_for_test_secs(current_timestamp + stale_price_threshold);
  487. // Check that we can access the price if we increase the threshold by 1
  488. assert!(get_price_no_older_than(
  489. price_identifier, get_stale_price_threshold_secs() + 1) == price, 1);
  490. // However, retrieving the latest price fails
  491. assert!(get_price(price_identifier) == price, 1);
  492. cleanup_test(burn_capability, mint_capability);
  493. coin::destroy_zero(coins);
  494. }
  495. #[test(aptos_framework = @aptos_framework)]
  496. #[expected_failure(abort_code = 65541)]
  497. fun test_update_price_feeds_if_fresh_invalid_length(aptos_framework: &signer) {
  498. let (burn_capability, mint_capability, coins) = setup_test(aptos_framework, 27, 500, 1, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", 50, 0);
  499. // Update the price feeds
  500. let bytes = vector[0u8, 1u8, 2u8];
  501. let price_identifiers = vector[
  502. price_identifier::from_byte_vec(x"baa284eaf23edf975b371ba2818772f93dbae72836bbdea28b07d40f3cf8b485"),
  503. price_identifier::from_byte_vec(x"c9d5fe0d836688f4c88c221415d23e4bcabee21a6a21124bfcc9a5410a297818"),
  504. price_identifier::from_byte_vec(x"eaa020c61cc479712813461ce153894a96a6c00b21ed0cfc2798d1f9a9e9c94a"),
  505. ];
  506. let publish_times = vector[
  507. 734639463
  508. ];
  509. update_price_feeds_if_fresh(bytes, price_identifiers, publish_times, coins);
  510. cleanup_test(burn_capability, mint_capability);
  511. }
  512. #[test(aptos_framework = @aptos_framework)]
  513. fun test_update_price_feeds_if_fresh_fresh_data(aptos_framework: &signer) {
  514. let (burn_capability, mint_capability, coins) = setup_test(aptos_framework, 27, 500, 1, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", 50, 50);
  515. set_data_source_for_test_vaa();
  516. // Update the price feeds
  517. let bytes = TEST_VAA;
  518. let price_identifiers = vector[
  519. price_identifier::from_byte_vec(x"c6c75c89f14810ec1c54c03ab8f1864a4c4032791f05747f560faec380a695d1"),
  520. price_identifier::from_byte_vec(x"3b9551a68d01d954d6387aff4df1529027ffb2fee413082e509feb29cc4904fe"),
  521. price_identifier::from_byte_vec(x"33832fad6e36eb05a8972fe5f219b27b5b2bb2230a79ce79beb4c5c5e7ecc76d"),
  522. price_identifier::from_byte_vec(x"21a28b4c6619968bd8c20e95b0aaed7df2187fd310275347e0376a2cd7427db8"),
  523. ];
  524. let publish_times = vector[
  525. 1663680745, 1663680730, 1663680760, 1663680720
  526. ];
  527. update_price_feeds_if_fresh(bytes, price_identifiers, publish_times, coins);
  528. // Check that the cache has been updated
  529. let expected = get_mock_price_infos();
  530. check_price_feeds_cached(&expected);
  531. cleanup_test(burn_capability, mint_capability);
  532. }
  533. #[test(aptos_framework = @aptos_framework)]
  534. #[expected_failure(abort_code = 524295)]
  535. fun test_update_price_feeds_if_fresh_stale_data(aptos_framework: &signer) {
  536. let (burn_capability, mint_capability, coins) = setup_test(aptos_framework, 27, 500, 1, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", 50, 50);
  537. set_data_source_for_test_vaa();
  538. // First populate the cache
  539. update_cache(get_mock_price_infos());
  540. // Now attempt to update the price feeds with publish_times that are older than those we have cached
  541. // This should abort with error::no_fresh_data()
  542. let bytes = TEST_VAA;
  543. let price_identifiers = vector[
  544. price_identifier::from_byte_vec(x"c6c75c89f14810ec1c54c03ab8f1864a4c4032791f05747f560faec380a695d1"),
  545. price_identifier::from_byte_vec(x"3b9551a68d01d954d6387aff4df1529027ffb2fee413082e509feb29cc4904fe"),
  546. price_identifier::from_byte_vec(x"33832fad6e36eb05a8972fe5f219b27b5b2bb2230a79ce79beb4c5c5e7ecc76d"),
  547. price_identifier::from_byte_vec(x"21a28b4c6619968bd8c20e95b0aaed7df2187fd310275347e0376a2cd7427db8"),
  548. ];
  549. let publish_times = vector[
  550. 67, 35, 26, 64
  551. ];
  552. update_price_feeds_if_fresh(bytes, price_identifiers, publish_times, coins);
  553. cleanup_test(burn_capability, mint_capability);
  554. }
  555. }