PythUpgradable.sol 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. address verifier
  29. ) public initializer {
  30. __Ownable_init();
  31. __UUPSUpgradeable_init();
  32. Pyth._initialize(
  33. wormhole,
  34. dataSourceEmitterChainIds,
  35. dataSourceEmitterAddresses,
  36. governanceEmitterChainId,
  37. governanceEmitterAddress,
  38. governanceInitialSequence,
  39. validTimePeriodSeconds,
  40. singleUpdateFeeInWei,
  41. verifier
  42. );
  43. renounceOwnership();
  44. }
  45. /// Ensures the contract cannot be uninitialized and taken over.
  46. /// @custom:oz-upgrades-unsafe-allow constructor
  47. constructor() initializer {}
  48. // Only allow the owner to upgrade the proxy to a new implementation.
  49. // The contract has no owner so this function will always revert
  50. // but UUPSUpgradeable expects this method to be implemented.
  51. function _authorizeUpgrade(address) internal override onlyOwner {}
  52. function pythUpgradableMagic() public pure returns (uint32) {
  53. return 0x97a6f304;
  54. }
  55. // Execute a UpgradeContract governance message
  56. function upgradeUpgradableContract(
  57. UpgradeContractPayload memory payload
  58. ) internal override {
  59. address oldImplementation = _getImplementation();
  60. _upgradeToAndCallUUPS(payload.newImplementation, new bytes(0), false);
  61. // Calling a method using `this.<method>` will cause a contract call that will use
  62. // the new contract. This call will fail if the method does not exists or the magic
  63. // is different.
  64. if (this.pythUpgradableMagic() != 0x97a6f304)
  65. revert PythErrors.InvalidGovernanceMessage();
  66. emit ContractUpgraded(oldImplementation, _getImplementation());
  67. }
  68. }