pyth_lazer_tests.move 5.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #[test_only]
  2. module pyth_lazer::pyth_lazer_tests;
  3. use pyth_lazer::pyth_lazer::parse_and_verify_le_ecdsa_update;
  4. use pyth_lazer::channel::new_fixed_rate_200ms;
  5. use pyth_lazer::i16::{Self};
  6. use pyth_lazer::i64::{Self};
  7. #[test]
  8. public fun test_parse_and_verify_le_ecdsa_update() {
  9. /*
  10. The test data is from the Lazer subscription:
  11. > Request
  12. {"subscriptionId": 1, "type": "subscribe", "priceFeedIds": [1, 2, 112], "properties": ["price", "bestBidPrice", "bestAskPrice", "exponent", "fundingRate", "fundingTimestamp", "fundingRateInterval"], "chains": ["leEcdsa"], "channel": "fixed_rate@200ms", "jsonBinaryEncoding": "hex"}
  13. < Response
  14. {
  15. "type": "streamUpdated",
  16. "subscriptionId": 1,
  17. "parsed": {
  18. "timestampUs": "1755625313400000",
  19. "priceFeeds": [
  20. {
  21. "priceFeedId": 1,
  22. "price": "11350721594969",
  23. "bestBidPrice": "11350696257890",
  24. "bestAskPrice": "11350868428965",
  25. "exponent": -8
  26. },
  27. {
  28. "priceFeedId": 2,
  29. "price": "417775510136",
  30. "bestBidPrice": "417771266475",
  31. "bestAskPrice": "417782074042",
  32. "exponent": -8
  33. },
  34. {
  35. "priceFeedId": 112,
  36. "price": "113747064619385816",
  37. "exponent": -12,
  38. "fundingRate": 31670000,
  39. "fundingTimestamp": 1755619200000000,
  40. "fundingRateInterval": 28800000000
  41. }
  42. ]
  43. },
  44. "leEcdsa": {
  45. "encoding": "hex",
  46. "data": "e4bd474d42e3c9c3477b30f2c5527ebe2fb2c8adadadacaddfa7d95243b80fb8f0d813b453e587f140cf40a1120d75f1ffee8ad4337267e4fcbd23eabb2a555804f85ec101a10075d3c793c0f4295fbb3c060003030100000007005986bacb520a00000162e937ca520a000002a5087bd4520a000004f8ff06000700080002000000070078625c456100000001aba11b456100000002ba8ac0456100000004f8ff060007000800700000000700d8c3e1445a1c940101000000000000000002000000000000000004f4ff0601f03ee30100000000070100e0c6f2b93c0600080100209db406000000"
  47. }
  48. }
  49. */
  50. let hex_message =
  51. x"e4bd474d42e3c9c3477b30f2c5527ebe2fb2c8adadadacaddfa7d95243b80fb8f0d813b453e587f140cf40a1120d75f1ffee8ad4337267e4fcbd23eabb2a555804f85ec101a10075d3c793c0f4295fbb3c060003030100000007005986bacb520a00000162e937ca520a000002a5087bd4520a000004f8ff06000700080002000000070078625c456100000001aba11b456100000002ba8ac0456100000004f8ff060007000800700000000700d8c3e1445a1c940101000000000000000002000000000000000004f4ff0601f03ee30100000000070100e0c6f2b93c0600080100209db406000000";
  52. let update = parse_and_verify_le_ecdsa_update(hex_message);
  53. // If we reach this point, the function worked correctly
  54. // (no assertion failures in parse_and_validate_update)
  55. assert!(update.timestamp() == 1755625313400000, 0);
  56. assert!(update.channel() == new_fixed_rate_200ms(), 0);
  57. assert!(vector::length(&update.feeds()) == 3, 0);
  58. let feed_1 = vector::borrow(&update.feeds(), 0);
  59. assert!(feed_1.feed_id() == 1, 0);
  60. assert!(feed_1.price() == option::some(option::some(i64::from_u64(11350721594969))), 0);
  61. assert!(feed_1.best_bid_price() == option::some(option::some(i64::from_u64(11350696257890))), 0);
  62. assert!(feed_1.best_ask_price() == option::some(option::some(i64::from_u64(11350868428965))), 0);
  63. assert!(feed_1.exponent() == option::some(i16::new(8, true)), 0);
  64. assert!(feed_1.publisher_count() == option::none(), 0);
  65. assert!(feed_1.confidence() == option::none(), 0);
  66. assert!(feed_1.funding_rate() == option::some(option::none()), 0);
  67. assert!(feed_1.funding_timestamp() == option::some(option::none()), 0);
  68. assert!(feed_1.funding_rate_interval() == option::some(option::none()), 0);
  69. let feed_2 = vector::borrow(&update.feeds(), 1);
  70. assert!(feed_2.feed_id() == 2, 0);
  71. assert!(feed_2.price() == option::some(option::some(i64::from_u64(417775510136))), 0);
  72. assert!(feed_2.best_bid_price() == option::some(option::some(i64::from_u64(417771266475))), 0);
  73. assert!(feed_2.best_ask_price() == option::some(option::some(i64::from_u64(417782074042))), 0);
  74. assert!(feed_2.exponent() == option::some(i16::new(8, true)), 0);
  75. assert!(feed_2.publisher_count() == option::none(), 0);
  76. assert!(feed_2.confidence() == option::none(), 0);
  77. assert!(feed_2.funding_rate() == option::some(option::none()), 0);
  78. assert!(feed_2.funding_timestamp() == option::some(option::none()), 0);
  79. assert!(feed_2.funding_rate_interval() == option::some(option::none()), 0);
  80. let feed_3 = vector::borrow(&update.feeds(), 2);
  81. assert!(feed_3.feed_id() == 112, 0);
  82. assert!(feed_3.price() == option::some(option::some(i64::from_u64(113747064619385816))), 0);
  83. assert!(feed_3.best_bid_price() == option::some(option::none()), 0);
  84. assert!(feed_3.best_ask_price() == option::some(option::none()), 0);
  85. assert!(feed_3.exponent() == option::some(i16::new(12, true)), 0);
  86. assert!(feed_3.publisher_count() == option::none(), 0);
  87. assert!(feed_3.confidence() == option::none(), 0);
  88. assert!(feed_3.funding_rate() == option::some(option::some(i64::from_u64(31670000))), 0);
  89. assert!(feed_3.funding_timestamp() == option::some(option::some(1755619200000000)), 0);
  90. assert!(feed_3.funding_rate_interval() == option::some(option::some(28800000000)), 0);
  91. }