setup.move 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. module pyth::setup {
  2. use iota::object::{Self, UID};
  3. use iota::package::{Self, UpgradeCap};
  4. use iota::transfer::{Self};
  5. use iota::tx_context::{Self, TxContext};
  6. use pyth::state::{Self};
  7. use pyth::data_source::{DataSource};
  8. friend pyth::pyth;
  9. #[test_only]
  10. friend pyth::pyth_tests;
  11. /// Capability created at `init`, which will be destroyed once
  12. /// `init_and_share_state` is called. This ensures only the deployer can
  13. /// create the shared `State`.
  14. struct DeployerCap has key, store {
  15. id: UID
  16. }
  17. fun init(ctx: &mut TxContext) {
  18. transfer::public_transfer(
  19. DeployerCap {
  20. id: object::new(ctx)
  21. },
  22. tx_context::sender(ctx)
  23. );
  24. }
  25. #[test_only]
  26. public fun init_test_only(ctx: &mut TxContext) {
  27. init(ctx);
  28. // This will be created and sent to the transaction sender
  29. // automatically when the contract is published.
  30. transfer::public_transfer(
  31. iota::package::test_publish(object::id_from_address(@pyth), ctx),
  32. tx_context::sender(ctx)
  33. );
  34. }
  35. #[allow(lint(share_owned))]
  36. /// Only the owner of the `DeployerCap` can call this method. This
  37. /// method destroys the capability and shares the `State` object.
  38. public(friend) fun init_and_share_state(
  39. deployer: DeployerCap,
  40. upgrade_cap: UpgradeCap,
  41. stale_price_threshold: u64,
  42. base_update_fee: u64,
  43. governance_data_source: DataSource,
  44. sources: vector<DataSource>,
  45. ctx: &mut TxContext
  46. ) {
  47. wormhole::package_utils::assert_package_upgrade_cap<DeployerCap>(
  48. &upgrade_cap,
  49. package::compatible_policy(),
  50. 1
  51. );
  52. // Destroy deployer cap.
  53. let DeployerCap { id } = deployer;
  54. object::delete(id);
  55. // Share new state.
  56. transfer::public_share_object(
  57. state::new(
  58. upgrade_cap,
  59. sources,
  60. governance_data_source,
  61. stale_price_threshold,
  62. base_update_fee,
  63. ctx
  64. ));
  65. }
  66. }