PythUpgradable.sol 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. contract PythUpgradable is Initializable, OwnableUpgradeable, UUPSUpgradeable, Pyth, PythGovernance {
  12. function initialize(
  13. address wormhole,
  14. uint16 pyth2WormholeChainId,
  15. bytes32 pyth2WormholeEmitter
  16. ) initializer override public {
  17. __Ownable_init();
  18. __UUPSUpgradeable_init();
  19. Pyth.initialize(wormhole, pyth2WormholeChainId, pyth2WormholeEmitter);
  20. }
  21. /// Privileged function to specify additional data sources in the contract
  22. function addDataSource(uint16 chainId, bytes32 emitter) onlyOwner public {
  23. PythInternalStructs.DataSource memory ds = PythInternalStructs.DataSource(chainId, emitter);
  24. require(!PythGetters.isValidDataSource(ds.chainId, ds.emitterAddress), "Data source already added");
  25. _state.isValidDataSource[hashDataSource(ds)] = true;
  26. _state.validDataSources.push(ds);
  27. }
  28. /// Privileged fucntion to remove the specified data source. Assumes _state.validDataSources has no duplicates.
  29. function removeDataSource(uint16 chainId, bytes32 emitter) onlyOwner public {
  30. PythInternalStructs.DataSource memory ds = PythInternalStructs.DataSource(chainId, emitter);
  31. require(PythGetters.isValidDataSource(ds.chainId, ds.emitterAddress), "Data source not found, not removing");
  32. _state.isValidDataSource[hashDataSource(ds)] = false;
  33. for (uint i = 0; i < _state.validDataSources.length; ++i) {
  34. // Find the source to remove
  35. if (_state.validDataSources[i].chainId == ds.chainId || _state.validDataSources[i].emitterAddress == ds.emitterAddress) {
  36. // Copy last element to overwrite the target data source
  37. _state.validDataSources[i] = _state.validDataSources[_state.validDataSources.length - 1];
  38. // Remove the last element we just preserved
  39. _state.validDataSources.pop();
  40. break;
  41. }
  42. }
  43. }
  44. /// Privileged function to update the price update fee
  45. function updateSingleUpdateFeeInWei(uint newFee) onlyOwner public {
  46. PythSetters.setSingleUpdateFeeInWei(newFee);
  47. }
  48. /// Privileged function to update the valid time period for a price.
  49. function updateValidTimePeriodSeconds(uint newValidTimePeriodSeconds) onlyOwner public {
  50. PythSetters.setValidTimePeriodSeconds(newValidTimePeriodSeconds);
  51. }
  52. // Privileged function to update the governance emitter
  53. function updateGovernanceDataSource(uint16 chainId, bytes32 emitter, uint64 sequence) onlyOwner public {
  54. PythInternalStructs.DataSource memory ds = PythInternalStructs.DataSource(chainId, emitter);
  55. PythSetters.setGovernanceDataSource(ds);
  56. PythSetters.setLastExecutedGovernanceSequence(sequence);
  57. }
  58. /// Ensures the contract cannot be uninitialized and taken over.
  59. /// @custom:oz-upgrades-unsafe-allow constructor
  60. constructor() initializer {}
  61. // Only allow the owner to upgrade the proxy to a new implementation.
  62. function _authorizeUpgrade(address) internal override onlyOwner {}
  63. // Execute a UpgradeContract governance message
  64. function upgradeUpgradableContract(UpgradeContractPayload memory payload) override internal {
  65. address oldImplementation = _getImplementation();
  66. _upgradeToAndCallUUPS(payload.newImplementation, new bytes(0), false);
  67. require(isPyth(), "the new implementation is not a Pyth contract");
  68. emit ContractUpgraded(oldImplementation, _getImplementation());
  69. }
  70. }