state.move 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. module pyth::state {
  2. use std::vector;
  3. use sui::object::{Self, UID, ID};
  4. use sui::transfer::{Self};
  5. use sui::tx_context::{Self, TxContext};
  6. use sui::package::{Self, UpgradeCap};
  7. use pyth::data_source::{Self, DataSource};
  8. use pyth::price_info::{Self};
  9. use pyth::price_identifier::{PriceIdentifier};
  10. use wormhole::setup::{assert_package_upgrade_cap};
  11. friend pyth::pyth;
  12. friend pyth::pyth_tests;
  13. friend pyth::governance_action;
  14. friend pyth::set_update_fee;
  15. friend pyth::set_stale_price_threshold;
  16. friend pyth::set_data_sources;
  17. friend pyth::governance;
  18. friend pyth::set_governance_data_source;
  19. /// Capability for creating a bridge state object, granted to sender when this
  20. /// module is deployed
  21. struct DeployerCap has key, store {
  22. id: UID
  23. }
  24. struct State has key {
  25. id: UID,
  26. governance_data_source: DataSource,
  27. last_executed_governance_sequence: u64,
  28. stale_price_threshold: u64,
  29. base_update_fee: u64,
  30. upgrade_cap: UpgradeCap
  31. }
  32. fun init(ctx: &mut TxContext) {
  33. transfer::public_transfer(
  34. DeployerCap {
  35. id: object::new(ctx)
  36. },
  37. tx_context::sender(ctx)
  38. );
  39. }
  40. #[test_only]
  41. public fun init_test_only(ctx: &mut TxContext) {
  42. init(ctx);
  43. // This will be created and sent to the transaction sender
  44. // automatically when the contract is published.
  45. transfer::public_transfer(
  46. sui::package::test_publish(object::id_from_address(@pyth), ctx),
  47. tx_context::sender(ctx)
  48. );
  49. }
  50. // Initialization
  51. public(friend) fun init_and_share_state(
  52. deployer: DeployerCap,
  53. upgrade_cap: UpgradeCap,
  54. stale_price_threshold: u64,
  55. base_update_fee: u64,
  56. governance_data_source: DataSource,
  57. sources: vector<DataSource>,
  58. ctx: &mut TxContext
  59. ) {
  60. // TODO - version control
  61. // let version = wormhole::version_control::version();
  62. //assert!(version == 1, E_INVALID_BUILD_VERSION);
  63. let DeployerCap { id } = deployer;
  64. object::delete(id);
  65. assert_package_upgrade_cap<DeployerCap>(
  66. &upgrade_cap,
  67. package::compatible_policy(),
  68. 1 // version
  69. );
  70. let uid = object::new(ctx);
  71. // Create a set that contains all registered data sources and
  72. // attach it to uid as a dynamic field to minimize the
  73. // size of State.
  74. data_source::new_data_source_registry(&mut uid, ctx);
  75. // Create a table that tracks the object IDs of price feeds and
  76. // attach it to the uid as a dynamic object field to minimize the
  77. // size of State.
  78. price_info::new_price_info_registry(&mut uid, ctx);
  79. // Iterate through data sources and add them to the data source
  80. // registry in state.
  81. while (!vector::is_empty(&sources)) {
  82. data_source::add(&mut uid, vector::pop_back(&mut sources));
  83. };
  84. // Share state so that is a shared Sui object.
  85. transfer::share_object(
  86. State {
  87. id: uid,
  88. upgrade_cap,
  89. governance_data_source,
  90. last_executed_governance_sequence: 0,
  91. stale_price_threshold,
  92. base_update_fee,
  93. }
  94. );
  95. }
  96. // Accessors
  97. public fun get_stale_price_threshold_secs(s: &State): u64 {
  98. s.stale_price_threshold
  99. }
  100. public fun get_base_update_fee(s: &State): u64 {
  101. s.base_update_fee
  102. }
  103. public fun is_valid_data_source(s: &State, data_source: DataSource): bool {
  104. data_source::contains(&s.id, data_source)
  105. }
  106. public fun is_valid_governance_data_source(s: &State, source: DataSource): bool {
  107. s.governance_data_source == source
  108. }
  109. public fun get_last_executed_governance_sequence(s: &State): u64 {
  110. s.last_executed_governance_sequence
  111. }
  112. public fun price_feed_object_exists(s: &State, p: PriceIdentifier): bool {
  113. price_info::contains(&s.id, p)
  114. }
  115. // Setters
  116. public(friend) fun set_data_sources(s: &mut State, new_sources: vector<DataSource>) {
  117. // Empty the existing table of data sources registered in state.
  118. data_source::empty(&mut s.id);
  119. // Add the new data sources to the dynamic field registry.
  120. while (!vector::is_empty(&new_sources)) {
  121. data_source::add(&mut s.id, vector::pop_back(&mut new_sources));
  122. };
  123. }
  124. public(friend) fun register_price_info_object(s: &mut State, price_identifier: PriceIdentifier, id: ID) {
  125. price_info::add(&mut s.id, price_identifier, id);
  126. }
  127. public(friend) fun set_last_executed_governance_sequence(s: &mut State, sequence: u64) {
  128. s.last_executed_governance_sequence = sequence;
  129. }
  130. public(friend) fun set_governance_data_source(s: &mut State, source: DataSource) {
  131. s. governance_data_source = source;
  132. }
  133. public(friend) fun set_base_update_fee(s: &mut State, fee: u64) {
  134. s.base_update_fee = fee;
  135. }
  136. public(friend) fun set_stale_price_threshold_secs(s: &mut State, threshold_secs: u64) {
  137. s.stale_price_threshold = threshold_secs;
  138. }
  139. public(friend) fun register_price_feed(s: &mut State, p: PriceIdentifier, id: ID){
  140. price_info::add(&mut s.id, p, id);
  141. }
  142. }