lib.rs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //! Lazer type definitions and utilities.
  2. /// Types describing Lazer HTTP and WebSocket APIs.
  3. pub mod api;
  4. /// Binary delivery format for WebSocket.
  5. pub mod binary_update;
  6. mod dynamic_value;
  7. mod feed_kind;
  8. /// Lazer Agent JSON-RPC API.
  9. pub mod jrpc;
  10. /// Types describing Lazer's verifiable messages containing signature and payload.
  11. pub mod message;
  12. /// Types describing Lazer's message payload.
  13. pub mod payload;
  14. mod price;
  15. /// Legacy Websocket API for publishers.
  16. pub mod publisher;
  17. mod rate;
  18. mod serde_price_as_i64;
  19. mod serde_str;
  20. mod symbol_state;
  21. /// Lazer's types for time representation.
  22. pub mod time;
  23. use derive_more::derive::{From, Into};
  24. use serde::{Deserialize, Serialize};
  25. pub use crate::{
  26. dynamic_value::DynamicValue,
  27. feed_kind::FeedKind,
  28. price::{Price, PriceError},
  29. rate::{Rate, RateError},
  30. symbol_state::SymbolState,
  31. };
  32. #[derive(
  33. Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize, From, Into,
  34. )]
  35. pub struct PublisherId(pub u16);
  36. #[derive(
  37. Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize, From, Into,
  38. )]
  39. pub struct PriceFeedId(pub u32);
  40. #[derive(
  41. Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize, From, Into,
  42. )]
  43. pub struct ChannelId(pub u8);
  44. impl ChannelId {
  45. pub const FIXED_RATE_1: ChannelId = ChannelId(1);
  46. pub const FIXED_RATE_50: ChannelId = ChannelId(2);
  47. pub const FIXED_RATE_200: ChannelId = ChannelId(3);
  48. }
  49. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
  50. #[serde(rename_all = "camelCase")]
  51. pub enum PriceFeedProperty {
  52. Price,
  53. BestBidPrice,
  54. BestAskPrice,
  55. PublisherCount,
  56. Exponent,
  57. Confidence,
  58. FundingRate,
  59. FundingTimestamp,
  60. FundingRateInterval,
  61. // More fields may be added later.
  62. }
  63. // Operation and coefficient for converting value to mantissa.
  64. enum ExponentFactor {
  65. // mantissa = value * factor
  66. Mul(i64),
  67. // mantissa = value / factor
  68. Div(i64),
  69. }
  70. impl ExponentFactor {
  71. fn get(exponent: i16) -> Option<Self> {
  72. if exponent >= 0 {
  73. let exponent: u32 = exponent.try_into().ok()?;
  74. Some(ExponentFactor::Div(10_i64.checked_pow(exponent)?))
  75. } else {
  76. let minus_exponent: u32 = exponent.checked_neg()?.try_into().ok()?;
  77. Some(ExponentFactor::Mul(10_i64.checked_pow(minus_exponent)?))
  78. }
  79. }
  80. }
  81. #[test]
  82. fn magics_in_big_endian() {
  83. use crate::{
  84. binary_update::BINARY_UPDATE_FORMAT_MAGIC,
  85. message::format_magics_le::{
  86. EVM_FORMAT_MAGIC, JSON_FORMAT_MAGIC, LE_ECDSA_FORMAT_MAGIC, LE_UNSIGNED_FORMAT_MAGIC,
  87. SOLANA_FORMAT_MAGIC,
  88. },
  89. payload::PAYLOAD_FORMAT_MAGIC,
  90. };
  91. // The values listed in this test can be used when reading the magic headers in BE format
  92. // (e.g., on EVM).
  93. assert_eq!(u32::swap_bytes(BINARY_UPDATE_FORMAT_MAGIC), 1937213467);
  94. assert_eq!(u32::swap_bytes(PAYLOAD_FORMAT_MAGIC), 1976813459);
  95. assert_eq!(u32::swap_bytes(SOLANA_FORMAT_MAGIC), 3103857282);
  96. assert_eq!(u32::swap_bytes(JSON_FORMAT_MAGIC), 2584795844);
  97. assert_eq!(u32::swap_bytes(EVM_FORMAT_MAGIC), 706910618);
  98. assert_eq!(u32::swap_bytes(LE_ECDSA_FORMAT_MAGIC), 3837609805);
  99. assert_eq!(u32::swap_bytes(LE_UNSIGNED_FORMAT_MAGIC), 206398297);
  100. for magic in [
  101. BINARY_UPDATE_FORMAT_MAGIC,
  102. PAYLOAD_FORMAT_MAGIC,
  103. SOLANA_FORMAT_MAGIC,
  104. JSON_FORMAT_MAGIC,
  105. EVM_FORMAT_MAGIC,
  106. LE_ECDSA_FORMAT_MAGIC,
  107. LE_UNSIGNED_FORMAT_MAGIC,
  108. ] {
  109. // Required to distinguish between byte orders.
  110. assert_ne!(u32::swap_bytes(magic), magic);
  111. }
  112. }