PythUpgradable.sol 3.1 KB

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