|
| 1 | +use crate::feed_id::ID; |
| 2 | +use crate::report::base::{ReportBase, ReportError}; |
| 3 | + |
| 4 | +use num_bigint::BigInt; |
| 5 | + |
| 6 | +/// Represents a Report Data V10 Schema. |
| 7 | +/// |
| 8 | +/// This schema extends the V8 schema with additional fields for multipliers and tokenized pricing. |
| 9 | +/// |
| 10 | +/// # Parameters |
| 11 | +/// - `feed_id`: The feed ID the report has data for. |
| 12 | +/// - `valid_from_timestamp`: Earliest timestamp for which price is applicable. |
| 13 | +/// - `observations_timestamp`: Latest timestamp for which price is applicable. |
| 14 | +/// - `native_fee`: Base cost to validate a transaction using the report, denominated in the chain's native token (e.g., WETH/ETH). |
| 15 | +/// - `link_fee`: Base cost to validate a transaction using the report, denominated in LINK. |
| 16 | +/// - `expires_at`: Latest timestamp where the report can be verified onchain. |
| 17 | +/// - `last_update_timestamp`: Timestamp of the last valid price update. |
| 18 | +/// - `price`: DON's consensus price (18 decimal precision). |
| 19 | +/// - `market_status`: Market status - 0 (Unknown), 1 (Closed), 2 (Open). |
| 20 | +/// - `current_multiplier`: Currently applied multiplier accounting for past corporate actions. |
| 21 | +/// - `new_multiplier`: Multiplier to be applied at the `activation_date_time` (set to 0 if none is scheduled). |
| 22 | +/// - `activation_date_time`: When the next corporate action takes effect (set to 0 if none is scheduled). |
| 23 | +/// - `tokenized_price`: 24/7 tokenized equity price. |
| 24 | +/// |
| 25 | +/// # Solidity Equivalent |
| 26 | +/// ```solidity |
| 27 | +/// struct ReportDataV10 { |
| 28 | +/// bytes32 feedId; |
| 29 | +/// uint32 validFromTimestamp; |
| 30 | +/// uint32 observationsTimestamp; |
| 31 | +/// uint192 nativeFee; |
| 32 | +/// uint192 linkFee; |
| 33 | +/// uint32 expiresAt; |
| 34 | +/// uint64 lastUpdateTimestamp; |
| 35 | +/// int192 price; |
| 36 | +/// uint32 marketStatus; |
| 37 | +/// int192 currentMultiplier; |
| 38 | +/// int192 newMultiplier; |
| 39 | +/// uint32 activationDateTime; |
| 40 | +/// int192 tokenizedPrice; |
| 41 | +/// } |
| 42 | +/// ``` |
| 43 | +#[derive(Debug)] |
| 44 | +pub struct ReportDataV10 { |
| 45 | + pub feed_id: ID, |
| 46 | + pub valid_from_timestamp: u32, |
| 47 | + pub observations_timestamp: u32, |
| 48 | + pub native_fee: BigInt, |
| 49 | + pub link_fee: BigInt, |
| 50 | + pub expires_at: u32, |
| 51 | + pub last_update_timestamp: u64, |
| 52 | + pub price: BigInt, |
| 53 | + pub market_status: u32, |
| 54 | + pub current_multiplier: BigInt, |
| 55 | + pub new_multiplier: BigInt, |
| 56 | + pub activation_date_time: u32, |
| 57 | + pub tokenized_price: BigInt, |
| 58 | +} |
| 59 | + |
| 60 | +impl ReportDataV10 { |
| 61 | + /// Decodes an ABI-encoded `ReportDataV10` from bytes. |
| 62 | + /// |
| 63 | + /// # Parameters |
| 64 | + /// |
| 65 | + /// - `data`: The encoded report data. |
| 66 | + /// |
| 67 | + /// # Returns |
| 68 | + /// |
| 69 | + /// The decoded `ReportDataV10`. |
| 70 | + /// |
| 71 | + /// # Errors |
| 72 | + /// |
| 73 | + /// Returns a `ReportError` if the data is too short or if the data is invalid. |
| 74 | + pub fn decode(data: &[u8]) -> Result<Self, ReportError> { |
| 75 | + if data.len() < 13 * ReportBase::WORD_SIZE { |
| 76 | + return Err(ReportError::DataTooShort("ReportDataV10")); |
| 77 | + } |
| 78 | + |
| 79 | + let feed_id = ID(data[..ReportBase::WORD_SIZE] |
| 80 | + .try_into() |
| 81 | + .map_err(|_| ReportError::InvalidLength("feed_id (bytes32)"))?); |
| 82 | + |
| 83 | + let valid_from_timestamp = ReportBase::read_uint32(data, ReportBase::WORD_SIZE)?; |
| 84 | + let observations_timestamp = ReportBase::read_uint32(data, 2 * ReportBase::WORD_SIZE)?; |
| 85 | + let native_fee = ReportBase::read_uint192(data, 3 * ReportBase::WORD_SIZE)?; |
| 86 | + let link_fee = ReportBase::read_uint192(data, 4 * ReportBase::WORD_SIZE)?; |
| 87 | + let expires_at = ReportBase::read_uint32(data, 5 * ReportBase::WORD_SIZE)?; |
| 88 | + let last_update_timestamp = ReportBase::read_uint64(data, 6 * ReportBase::WORD_SIZE)?; |
| 89 | + let price = ReportBase::read_int192(data, 7 * ReportBase::WORD_SIZE)?; |
| 90 | + let market_status = ReportBase::read_uint32(data, 8 * ReportBase::WORD_SIZE)?; |
| 91 | + let current_multiplier = ReportBase::read_int192(data, 9 * ReportBase::WORD_SIZE)?; |
| 92 | + let new_multiplier = ReportBase::read_int192(data, 10 * ReportBase::WORD_SIZE)?; |
| 93 | + let activation_date_time = ReportBase::read_uint32(data, 11 * ReportBase::WORD_SIZE)?; |
| 94 | + let tokenized_price = ReportBase::read_int192(data, 12 * ReportBase::WORD_SIZE)?; |
| 95 | + |
| 96 | + Ok(Self { |
| 97 | + feed_id, |
| 98 | + valid_from_timestamp, |
| 99 | + observations_timestamp, |
| 100 | + native_fee, |
| 101 | + link_fee, |
| 102 | + expires_at, |
| 103 | + last_update_timestamp, |
| 104 | + price, |
| 105 | + market_status, |
| 106 | + current_multiplier, |
| 107 | + new_multiplier, |
| 108 | + activation_date_time, |
| 109 | + tokenized_price, |
| 110 | + }) |
| 111 | + } |
| 112 | + |
| 113 | + /// Encodes the `ReportDataV10` into an ABI-encoded byte array. |
| 114 | + /// |
| 115 | + /// # Returns |
| 116 | + /// |
| 117 | + /// The ABI-encoded report data. |
| 118 | + /// |
| 119 | + /// # Errors |
| 120 | + /// |
| 121 | + /// Returns a `ReportError` if the data is invalid. |
| 122 | + pub fn abi_encode(&self) -> Result<Vec<u8>, ReportError> { |
| 123 | + let mut buffer = Vec::with_capacity(13 * ReportBase::WORD_SIZE); |
| 124 | + |
| 125 | + buffer.extend_from_slice(&self.feed_id.0); |
| 126 | + buffer.extend_from_slice(&ReportBase::encode_uint32(self.valid_from_timestamp)?); |
| 127 | + buffer.extend_from_slice(&ReportBase::encode_uint32(self.observations_timestamp)?); |
| 128 | + buffer.extend_from_slice(&ReportBase::encode_uint192(&self.native_fee)?); |
| 129 | + buffer.extend_from_slice(&ReportBase::encode_uint192(&self.link_fee)?); |
| 130 | + buffer.extend_from_slice(&ReportBase::encode_uint32(self.expires_at)?); |
| 131 | + buffer.extend_from_slice(&ReportBase::encode_uint64(self.last_update_timestamp)?); |
| 132 | + buffer.extend_from_slice(&ReportBase::encode_int192(&self.price)?); |
| 133 | + buffer.extend_from_slice(&ReportBase::encode_uint32(self.market_status)?); |
| 134 | + buffer.extend_from_slice(&ReportBase::encode_int192(&self.current_multiplier)?); |
| 135 | + buffer.extend_from_slice(&ReportBase::encode_int192(&self.new_multiplier)?); |
| 136 | + buffer.extend_from_slice(&ReportBase::encode_uint32(self.activation_date_time)?); |
| 137 | + buffer.extend_from_slice(&ReportBase::encode_int192(&self.tokenized_price)?); |
| 138 | + |
| 139 | + Ok(buffer) |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +#[cfg(test)] |
| 144 | +mod tests { |
| 145 | + use super::*; |
| 146 | + use crate::report::tests::{ |
| 147 | + generate_mock_report_data_v10, MOCK_FEE, MOCK_PRICE, MOCK_TIMESTAMP, MARKET_STATUS_OPEN |
| 148 | + }; |
| 149 | + |
| 150 | + const V10_FEED_ID_STR: &str = |
| 151 | + "0x000a6b4aa7e57ca7b68ae1bf45653f56b656fd3aa335ef7fae696b663f1b8472"; |
| 152 | + |
| 153 | + #[test] |
| 154 | + fn test_decode_report_data_v10() { |
| 155 | + let report_data = generate_mock_report_data_v10(); |
| 156 | + let encoded = report_data.abi_encode().unwrap(); |
| 157 | + let decoded = ReportDataV10::decode(&encoded).unwrap(); |
| 158 | + |
| 159 | + const MOCK_MULTIPLIER: isize = 1000000000000000000; |
| 160 | + |
| 161 | + let expected_feed_id = ID::from_hex_str(V10_FEED_ID_STR).unwrap(); |
| 162 | + let expected_timestamp: u32 = MOCK_TIMESTAMP; |
| 163 | + let expected_fee = BigInt::from(MOCK_FEE); |
| 164 | + let expected_price = BigInt::from(MOCK_PRICE); |
| 165 | + let expected_market_status: u32 = MARKET_STATUS_OPEN; |
| 166 | + let expected_multiplier = BigInt::from(MOCK_MULTIPLIER); // 1.0 with 18 decimals |
| 167 | + let expected_tokenized_price = BigInt::from(MOCK_PRICE * 2); // Example tokenized price |
| 168 | + |
| 169 | + assert_eq!(decoded.feed_id, expected_feed_id); |
| 170 | + assert_eq!(decoded.valid_from_timestamp, expected_timestamp); |
| 171 | + assert_eq!(decoded.observations_timestamp, expected_timestamp); |
| 172 | + assert_eq!(decoded.native_fee, expected_fee); |
| 173 | + assert_eq!(decoded.link_fee, expected_fee); |
| 174 | + assert_eq!(decoded.expires_at, expected_timestamp + 100); |
| 175 | + assert_eq!(decoded.last_update_timestamp, expected_timestamp as u64); |
| 176 | + assert_eq!(decoded.price, expected_price); |
| 177 | + assert_eq!(decoded.market_status, expected_market_status); |
| 178 | + assert_eq!(decoded.current_multiplier, expected_multiplier); |
| 179 | + assert_eq!(decoded.new_multiplier, expected_multiplier); |
| 180 | + assert_eq!(decoded.activation_date_time, expected_timestamp + 200); |
| 181 | + assert_eq!(decoded.tokenized_price, expected_tokenized_price); |
| 182 | + } |
| 183 | +} |
0 commit comments