PythStructs.sol 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // SPDX-License-Identifier: Apache-2.0
  2. pragma solidity ^0.8.0;
  3. contract PythStructs {
  4. // A price with a degree of uncertainty, represented as a price +- a confidence interval.
  5. //
  6. // The confidence interval roughly corresponds to the standard error of a normal distribution.
  7. // Both the price and confidence are stored in a fixed-point numeric representation,
  8. // `x * (10^expo)`, where `expo` is the exponent.
  9. //
  10. // Please refer to the documentation at https://docs.pyth.network/documentation/pythnet-price-feeds/best-practices for how
  11. // to how this price safely.
  12. struct Price {
  13. // Price
  14. int64 price;
  15. // Confidence interval around the price
  16. uint64 conf;
  17. // Price exponent
  18. int32 expo;
  19. // Unix timestamp describing when the price was published
  20. uint publishTime;
  21. }
  22. // PriceFeed represents a current aggregate price from pyth publisher feeds.
  23. struct PriceFeed {
  24. // The price ID.
  25. bytes32 id;
  26. // Latest available price
  27. Price price;
  28. // Latest available exponentially-weighted moving average price
  29. Price emaPrice;
  30. }
  31. struct TwapPriceFeed {
  32. // The price ID.
  33. bytes32 id;
  34. // Start time of the TWAP
  35. uint64 startTime;
  36. // End time of the TWAP
  37. uint64 endTime;
  38. // TWAP price
  39. Price twap;
  40. // Down slot ratio represents the ratio of price feed updates that were missed or unavailable
  41. // during the TWAP period, expressed as a fixed-point number between 0 and 1e6 (100%).
  42. // For example:
  43. // - 0 means all price updates were available
  44. // - 500_000 means 50% of updates were missed
  45. // - 1_000_000 means all updates were missed
  46. // This can be used to assess the quality/reliability of the TWAP calculation.
  47. // Applications should define a maximum acceptable ratio (e.g. 100000 for 10%)
  48. // and revert if downSlotsRatio exceeds it.
  49. uint32 downSlotsRatio;
  50. }
  51. // Information used to calculate time-weighted average prices (TWAP)
  52. struct TwapPriceInfo {
  53. // slot 1
  54. int128 cumulativePrice;
  55. uint128 cumulativeConf;
  56. // slot 2
  57. uint64 numDownSlots;
  58. uint64 publishSlot;
  59. uint64 publishTime;
  60. uint64 prevPublishTime;
  61. // slot 3
  62. int32 expo;
  63. }
  64. }