PythUpgradable.sol 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // SPDX-License-Identifier: Apache 2
  2. pragma solidity ^0.8.0;
  3. import "./Pyth.sol";
  4. import "./PythInternalStructs.sol";
  5. import "./PythGetters.sol";
  6. import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
  7. import "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
  8. import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
  9. import "./PythGovernance.sol";
  10. import "./Pyth.sol";
  11. import "@pythnetwork/pyth-sdk-solidity/PythErrors.sol";
  12. contract PythUpgradable is
  13. Initializable,
  14. OwnableUpgradeable,
  15. UUPSUpgradeable,
  16. Pyth,
  17. PythGovernance
  18. {
  19. function initialize(
  20. address wormhole,
  21. uint16[] calldata dataSourceEmitterChainIds,
  22. bytes32[] calldata dataSourceEmitterAddresses,
  23. uint16 governanceEmitterChainId,
  24. bytes32 governanceEmitterAddress,
  25. uint64 governanceInitialSequence,
  26. uint validTimePeriodSeconds,
  27. uint singleUpdateFeeInWei
  28. ) public initializer {
  29. __Ownable_init();
  30. __UUPSUpgradeable_init();
  31. Pyth._initialize(
  32. wormhole,
  33. dataSourceEmitterChainIds,
  34. dataSourceEmitterAddresses,
  35. governanceEmitterChainId,
  36. governanceEmitterAddress,
  37. governanceInitialSequence,
  38. validTimePeriodSeconds,
  39. singleUpdateFeeInWei
  40. );
  41. renounceOwnership();
  42. }
  43. /// Ensures the contract cannot be uninitialized and taken over.
  44. /// @custom:oz-upgrades-unsafe-allow constructor
  45. constructor() initializer {}
  46. // Only allow the owner to upgrade the proxy to a new implementation.
  47. // The contract has no owner so this function will always revert
  48. // but UUPSUpgradeable expects this method to be implemented.
  49. function _authorizeUpgrade(address) internal override onlyOwner {}
  50. function pythUpgradableMagic() public pure returns (uint32) {
  51. return 0x97a6f304;
  52. }
  53. // Execute a UpgradeContract governance message
  54. function upgradeUpgradableContract(
  55. UpgradeContractPayload memory payload
  56. ) internal override {
  57. address oldImplementation = _getImplementation();
  58. _upgradeToAndCallUUPS(payload.newImplementation, new bytes(0), false);
  59. // Calling a method using `this.<method>` will cause a contract call that will use
  60. // the new contract. This call will fail if the method does not exists or the magic
  61. // is different.
  62. if (this.pythUpgradableMagic() != 0x97a6f304)
  63. revert PythErrors.InvalidGovernanceMessage();
  64. emit ContractUpgraded(oldImplementation, _getImplementation());
  65. }
  66. }