feed.move 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. module pyth_lazer::feed;
  2. use pyth_lazer::i16::I16;
  3. use pyth_lazer::i64::I64;
  4. /// The feed struct is based on the Lazer rust protocol definition defined here:
  5. /// https://github.com/pyth-network/pyth-crosschain/blob/main/lazer/sdk/rust/protocol/src/payload.rs
  6. ///
  7. /// Some fields in Lazer are optional, as in Lazer might return None for them due to some conditions (for example,
  8. /// not having enough publishers to calculate the price) and that is why they are represented as Option<Option<T>>.
  9. /// The first Option<T> is for the existence of the field within the update data and the second Option<T> is for the
  10. /// value of the field.
  11. public struct Feed has copy, drop {
  12. /// Unique identifier for the price feed (e.g., 1 for BTC/USD, 2 for ETH/USD)
  13. feed_id: u32,
  14. /// Current aggregate price from all publishers
  15. price: Option<Option<I64>>,
  16. /// Best bid price available across all publishers
  17. best_bid_price: Option<Option<I64>>,
  18. /// Best ask price available across all publishers
  19. best_ask_price: Option<Option<I64>>,
  20. /// Number of publishers contributing to this price feed
  21. publisher_count: Option<u16>,
  22. /// Price exponent (typically negative, e.g., -8 means divide price by 10^8)
  23. exponent: Option<I16>,
  24. /// Confidence interval representing price uncertainty
  25. confidence: Option<Option<I64>>,
  26. /// Funding rate for derivative products (e.g., perpetual futures)
  27. funding_rate: Option<Option<I64>>,
  28. /// Timestamp when the funding rate was last updated
  29. funding_timestamp: Option<Option<u64>>,
  30. /// How often the funding rate and funding payments are calculated, in microseconds
  31. funding_rate_interval: Option<Option<u64>>,
  32. }
  33. /// Create a new Feed with the specified parameters
  34. public(package) fun new(
  35. feed_id: u32,
  36. price: Option<Option<I64>>,
  37. best_bid_price: Option<Option<I64>>,
  38. best_ask_price: Option<Option<I64>>,
  39. publisher_count: Option<u16>,
  40. exponent: Option<I16>,
  41. confidence: Option<Option<I64>>,
  42. funding_rate: Option<Option<I64>>,
  43. funding_timestamp: Option<Option<u64>>,
  44. funding_rate_interval: Option<Option<u64>>,
  45. ): Feed {
  46. Feed {
  47. feed_id,
  48. price,
  49. best_bid_price,
  50. best_ask_price,
  51. publisher_count,
  52. exponent,
  53. confidence,
  54. funding_rate,
  55. funding_timestamp,
  56. funding_rate_interval
  57. }
  58. }
  59. /// Get the feed ID
  60. public fun feed_id(feed: &Feed): u32 {
  61. feed.feed_id
  62. }
  63. /// Get the price
  64. public fun price(feed: &Feed): Option<Option<I64>> {
  65. feed.price
  66. }
  67. /// Get the best bid price
  68. public fun best_bid_price(feed: &Feed): Option<Option<I64>> {
  69. feed.best_bid_price
  70. }
  71. /// Get the best ask price
  72. public fun best_ask_price(feed: &Feed): Option<Option<I64>> {
  73. feed.best_ask_price
  74. }
  75. /// Get the publisher count
  76. public fun publisher_count(feed: &Feed): Option<u16> {
  77. feed.publisher_count
  78. }
  79. /// Get the exponent
  80. public fun exponent(feed: &Feed): Option<I16> {
  81. feed.exponent
  82. }
  83. /// Get the confidence interval
  84. public fun confidence(feed: &Feed): Option<Option<I64>> {
  85. feed.confidence
  86. }
  87. /// Get the funding rate
  88. public fun funding_rate(feed: &Feed): Option<Option<I64>> {
  89. feed.funding_rate
  90. }
  91. /// Get the funding timestamp
  92. public fun funding_timestamp(feed: &Feed): Option<Option<u64>> {
  93. feed.funding_timestamp
  94. }
  95. /// Get the funding rate interval
  96. public fun funding_rate_interval(feed: &Feed): Option<Option<u64>> {
  97. feed.funding_rate_interval
  98. }
  99. /// Set the feed ID
  100. public(package) fun set_feed_id(feed: &mut Feed, feed_id: u32) {
  101. feed.feed_id = feed_id;
  102. }
  103. /// Set the price
  104. public(package) fun set_price(feed: &mut Feed, price: Option<Option<I64>>) {
  105. feed.price = price;
  106. }
  107. /// Set the best bid price
  108. public(package) fun set_best_bid_price(feed: &mut Feed, best_bid_price: Option<Option<I64>>) {
  109. feed.best_bid_price = best_bid_price;
  110. }
  111. /// Set the best ask price
  112. public(package) fun set_best_ask_price(feed: &mut Feed, best_ask_price: Option<Option<I64>>) {
  113. feed.best_ask_price = best_ask_price;
  114. }
  115. /// Set the publisher count
  116. public(package) fun set_publisher_count(feed: &mut Feed, publisher_count: Option<u16>) {
  117. feed.publisher_count = publisher_count;
  118. }
  119. /// Set the exponent
  120. public(package) fun set_exponent(feed: &mut Feed, exponent: Option<I16>) {
  121. feed.exponent = exponent;
  122. }
  123. /// Set the confidence interval
  124. public(package) fun set_confidence(feed: &mut Feed, confidence: Option<Option<I64>>) {
  125. feed.confidence = confidence;
  126. }
  127. /// Set the funding rate
  128. public(package) fun set_funding_rate(feed: &mut Feed, funding_rate: Option<Option<I64>>) {
  129. feed.funding_rate = funding_rate;
  130. }
  131. /// Set the funding timestamp
  132. public(package) fun set_funding_timestamp(feed: &mut Feed, funding_timestamp: Option<Option<u64>>) {
  133. feed.funding_timestamp = funding_timestamp;
  134. }
  135. /// Set the funding rate interval
  136. public(package) fun set_funding_rate_interval(feed: &mut Feed, funding_rate_interval: Option<Option<u64>>) {
  137. feed.funding_rate_interval = funding_rate_interval;
  138. }