state.rs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. use std::time::Duration;
  2. use pyth_sdk::PriceFeed;
  3. use schemars::JsonSchema;
  4. use serde::{
  5. Deserialize,
  6. Serialize,
  7. };
  8. use cosmwasm_std::{
  9. StdResult,
  10. Storage,
  11. Timestamp,
  12. };
  13. use cosmwasm_storage::{
  14. bucket,
  15. bucket_read,
  16. singleton,
  17. singleton_read,
  18. Bucket,
  19. ReadonlyBucket,
  20. ReadonlySingleton,
  21. Singleton,
  22. };
  23. use wormhole::byte_utils::ByteUtils;
  24. type HumanAddr = String;
  25. pub static CONFIG_KEY: &[u8] = b"config";
  26. pub static PRICE_INFO_KEY: &[u8] = b"price_info_v2";
  27. /// Maximum acceptable time period before price is considered to be stale.
  28. ///
  29. /// This value considers attestation delay which currently might up to a minute.
  30. pub const VALID_TIME_PERIOD: Duration = Duration::from_secs(3*60);
  31. // Guardian set information
  32. #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
  33. pub struct ConfigInfo {
  34. pub wormhole_contract: HumanAddr,
  35. pub pyth_emitter: Vec<u8>,
  36. pub pyth_emitter_chain: u16,
  37. }
  38. #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
  39. #[serde(rename_all = "snake_case")]
  40. pub struct PriceInfo {
  41. pub arrival_time: Timestamp,
  42. pub arrival_block: u64,
  43. pub attestation_time: Timestamp,
  44. pub price_feed: PriceFeed,
  45. }
  46. pub fn config(storage: &mut dyn Storage) -> Singleton<ConfigInfo> {
  47. singleton(storage, CONFIG_KEY)
  48. }
  49. pub fn config_read(storage: &dyn Storage) -> ReadonlySingleton<ConfigInfo> {
  50. singleton_read(storage, CONFIG_KEY)
  51. }
  52. pub fn price_info(storage: &mut dyn Storage) -> Bucket<PriceInfo> {
  53. bucket(storage, PRICE_INFO_KEY)
  54. }
  55. pub fn price_info_read(storage: &dyn Storage) -> ReadonlyBucket<PriceInfo> {
  56. bucket_read(storage, PRICE_INFO_KEY)
  57. }
  58. pub struct UpgradeContract {
  59. pub new_contract: u64,
  60. }
  61. impl UpgradeContract {
  62. pub fn deserialize(data: &Vec<u8>) -> StdResult<Self> {
  63. let data = data.as_slice();
  64. let new_contract = data.get_u64(24);
  65. Ok(UpgradeContract { new_contract })
  66. }
  67. }