state.rs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. use {
  2. cosmwasm_std::{
  3. Addr,
  4. Binary,
  5. Coin,
  6. Storage,
  7. Timestamp,
  8. },
  9. cosmwasm_storage::{
  10. bucket,
  11. bucket_read,
  12. singleton,
  13. singleton_read,
  14. Bucket,
  15. ReadonlyBucket,
  16. ReadonlySingleton,
  17. Singleton,
  18. },
  19. pyth_sdk_cw::PriceFeed,
  20. schemars::JsonSchema,
  21. serde::{
  22. Deserialize,
  23. Serialize,
  24. },
  25. std::{
  26. collections::HashSet,
  27. time::Duration,
  28. },
  29. };
  30. pub static CONFIG_KEY: &[u8] = b"config";
  31. pub static PRICE_INFO_KEY: &[u8] = b"price_info_v3";
  32. #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Hash, JsonSchema)]
  33. pub struct PythDataSource {
  34. pub emitter: Binary,
  35. pub chain_id: u16,
  36. }
  37. #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
  38. pub struct ConfigInfo {
  39. pub wormhole_contract: Addr,
  40. pub data_sources: HashSet<PythDataSource>,
  41. pub governance_source: PythDataSource,
  42. // Index for preventing replay attacks on governance data source transfers.
  43. // This index increases every time the governance data source is changed, which prevents old
  44. // transfer request VAAs from being replayed.
  45. pub governance_source_index: u32,
  46. // The wormhole sequence number for governance messages. This value is increased every time the
  47. // a governance instruction is executed.
  48. //
  49. // This field differs from the one above in that it is generated by wormhole and applicable to all
  50. // governance messages, whereas the one above is generated by Pyth and only applicable to governance
  51. // source transfers.
  52. pub governance_sequence_number: u64,
  53. // FIXME: This id needs to agree with the wormhole chain id.
  54. // We should read this directly from wormhole.
  55. pub chain_id: u16,
  56. pub valid_time_period: Duration,
  57. // The fee to pay, denominated in fee_denom (typically, the chain's native token)
  58. pub fee: Coin,
  59. }
  60. #[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq, Eq, JsonSchema)]
  61. #[serde(rename_all = "snake_case")]
  62. pub struct PriceInfo {
  63. pub arrival_time: Timestamp,
  64. pub arrival_block: u64,
  65. pub attestation_time: Timestamp,
  66. pub price_feed: PriceFeed,
  67. }
  68. pub fn config(storage: &mut dyn Storage) -> Singleton<ConfigInfo> {
  69. singleton(storage, CONFIG_KEY)
  70. }
  71. pub fn config_read(storage: &dyn Storage) -> ReadonlySingleton<ConfigInfo> {
  72. singleton_read(storage, CONFIG_KEY)
  73. }
  74. pub fn price_info(storage: &mut dyn Storage) -> Bucket<PriceInfo> {
  75. bucket(storage, PRICE_INFO_KEY)
  76. }
  77. pub fn price_info_read(storage: &dyn Storage) -> ReadonlyBucket<PriceInfo> {
  78. bucket_read(storage, PRICE_INFO_KEY)
  79. }