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