setup.move 2.3 KB

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