channel.move 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. module pyth_lazer::channel;
  2. // Error codes for channel parsing
  3. const EInvalidChannel: u64 = 1;
  4. public enum Channel has copy, drop {
  5. RealTime,
  6. FixedRate50ms,
  7. FixedRate200ms,
  8. }
  9. /// Create a new RealTime channel
  10. public fun new_real_time(): Channel {
  11. Channel::RealTime
  12. }
  13. /// Create a new FixedRate50ms channel
  14. public fun new_fixed_rate_50ms(): Channel {
  15. Channel::FixedRate50ms
  16. }
  17. /// Create a new FixedRate200ms channel
  18. public fun new_fixed_rate_200ms(): Channel {
  19. Channel::FixedRate200ms
  20. }
  21. /// Parse channel from a channel value byte
  22. public fun from_u8(channel_value: u8): Channel {
  23. if (channel_value == 1) {
  24. new_real_time()
  25. } else if (channel_value == 2) {
  26. new_fixed_rate_50ms()
  27. } else if (channel_value == 3) {
  28. new_fixed_rate_200ms()
  29. } else {
  30. abort EInvalidChannel
  31. }
  32. }
  33. /// Check if the channel is RealTime
  34. public fun is_real_time(channel: &Channel): bool {
  35. match (channel) {
  36. Channel::RealTime => true,
  37. _ => false,
  38. }
  39. }
  40. /// Check if the channel is FixedRate50ms
  41. public fun is_fixed_rate_50ms(channel: &Channel): bool {
  42. match (channel) {
  43. Channel::FixedRate50ms => true,
  44. _ => false,
  45. }
  46. }
  47. /// Check if the channel is FixedRate200ms
  48. public fun is_fixed_rate_200ms(channel: &Channel): bool {
  49. match (channel) {
  50. Channel::FixedRate200ms => true,
  51. _ => false,
  52. }
  53. }
  54. /// Get the update interval in milliseconds for fixed rate channels, returns 0 for non-fixed rate channels
  55. public fun get_update_interval_ms(channel: &Channel): u64 {
  56. match (channel) {
  57. Channel::FixedRate50ms => 50,
  58. Channel::FixedRate200ms => 200,
  59. _ => 0,
  60. }
  61. }