update.move 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. module pyth_lazer::update;
  2. use pyth_lazer::channel::{Self, Channel};
  3. use pyth_lazer::feed::{Self, Feed};
  4. use sui::bcs;
  5. // Error codes for update parsing
  6. const EInvalidPayload: u64 = 3;
  7. public struct Update has copy, drop {
  8. timestamp: u64,
  9. channel: Channel,
  10. feeds: vector<Feed>,
  11. }
  12. public(package) fun new(timestamp: u64, channel: Channel, feeds: vector<Feed>): Update {
  13. Update { timestamp, channel, feeds }
  14. }
  15. /// Get the timestamp of the update
  16. public fun timestamp(update: &Update): u64 {
  17. update.timestamp
  18. }
  19. /// Get a reference to the channel of the update
  20. public fun channel(update: &Update): Channel {
  21. update.channel
  22. }
  23. /// Get a reference to the feeds vector of the update
  24. public fun feeds(update: &Update): vector<Feed> {
  25. update.feeds
  26. }
  27. /// Parse the update from a BCS cursor containing the payload data
  28. /// This assumes the payload magic has already been validated and consumed
  29. public(package) fun parse_from_cursor(mut cursor: bcs::BCS): Update {
  30. // Parse timestamp
  31. let timestamp = cursor.peel_u64();
  32. // Parse channel
  33. let channel_value = cursor.peel_u8();
  34. let channel = channel::from_u8(channel_value);
  35. // Parse feeds
  36. let feed_count = cursor.peel_u8();
  37. let mut feeds = vector::empty<Feed>();
  38. let mut feed_i = 0;
  39. while (feed_i < feed_count) {
  40. let feed = feed::parse_from_cursor(&mut cursor);
  41. vector::push_back(&mut feeds, feed);
  42. feed_i = feed_i + 1;
  43. };
  44. // Verify no remaining bytes
  45. let remaining_bytes = cursor.into_remainder_bytes();
  46. assert!(remaining_bytes.length() == 0, EInvalidPayload);
  47. Update { timestamp, channel, feeds }
  48. }