price_status.move 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. module pyth::price_status {
  2. //use pyth::error;
  3. /// The price feed is not currently updating for an unknown reason.
  4. const UNKNOWN: u64 = 0;
  5. /// The price feed is updating as expected.
  6. const TRADING: u64 = 1;
  7. /// PriceStatus represents the availability status of a price feed.
  8. /// Prices should only be used if they have a status of trading.
  9. struct PriceStatus has copy, drop, store {
  10. status: u64,
  11. }
  12. public fun from_u64(status: u64): PriceStatus {
  13. assert!(status <= TRADING, 0); // error::invalid_price_status()
  14. PriceStatus {
  15. status: status
  16. }
  17. }
  18. public fun get_status(price_status: &PriceStatus): u64 {
  19. price_status.status
  20. }
  21. public fun new_unknown(): PriceStatus {
  22. PriceStatus {
  23. status: UNKNOWN,
  24. }
  25. }
  26. public fun new_trading(): PriceStatus {
  27. PriceStatus {
  28. status: TRADING,
  29. }
  30. }
  31. #[test]
  32. fun test_unknown_status() {
  33. assert!(PriceStatus{ status: UNKNOWN } == from_u64(0), 1);
  34. }
  35. #[test]
  36. fun test_trading_status() {
  37. assert!(PriceStatus{ status: TRADING } == from_u64(1), 1);
  38. }
  39. #[test]
  40. #[expected_failure]
  41. fun test_invalid_price_status() {
  42. from_u64(3);
  43. }
  44. }