pyth.move 29 KB

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