UpgradeableProxy.sol 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity >=0.6.0 <0.8.0;
  3. import "./Proxy.sol";
  4. import "../utils/Address.sol";
  5. /**
  6. * @dev This contract implements an upgradeable proxy. It is upgradeable because calls are delegated to an
  7. * implementation address that can be changed. This address is stored in storage in the location specified by
  8. * https://eips.ethereum.org/EIPS/eip-1967[EIP1967], so that it doesn't conflict with the storage layout of the
  9. * implementation behind the proxy.
  10. *
  11. * Upgradeability is only provided internally through {_upgradeTo}. For an externally upgradeable proxy see
  12. * {TransparentUpgradeableProxy}.
  13. */
  14. contract UpgradeableProxy is Proxy {
  15. /**
  16. * @dev Initializes the upgradeable proxy with an initial implementation specified by `_logic`.
  17. *
  18. * If `_data` is nonempty, it's used as data in a delegate call to `_logic`. This will typically be an encoded
  19. * function call, and allows initializating the storage of the proxy like a Solidity constructor.
  20. */
  21. constructor(address _logic, bytes memory _data) public payable {
  22. assert(_IMPLEMENTATION_SLOT == bytes32(uint256(keccak256("eip1967.proxy.implementation")) - 1));
  23. _setImplementation(_logic);
  24. if(_data.length > 0) {
  25. Address.functionDelegateCall(_logic, _data);
  26. }
  27. }
  28. /**
  29. * @dev Emitted when the implementation is upgraded.
  30. */
  31. event Upgraded(address indexed implementation);
  32. /**
  33. * @dev Storage slot with the address of the current implementation.
  34. * This is the keccak-256 hash of "eip1967.proxy.implementation" subtracted by 1, and is
  35. * validated in the constructor.
  36. */
  37. bytes32 private constant _IMPLEMENTATION_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;
  38. /**
  39. * @dev Returns the current implementation address.
  40. */
  41. function _implementation() internal view virtual override returns (address impl) {
  42. bytes32 slot = _IMPLEMENTATION_SLOT;
  43. // solhint-disable-next-line no-inline-assembly
  44. assembly {
  45. impl := sload(slot)
  46. }
  47. }
  48. /**
  49. * @dev Upgrades the proxy to a new implementation.
  50. *
  51. * Emits an {Upgraded} event.
  52. */
  53. function _upgradeTo(address newImplementation) internal virtual {
  54. _setImplementation(newImplementation);
  55. emit Upgraded(newImplementation);
  56. }
  57. /**
  58. * @dev Stores a new address in the EIP1967 implementation slot.
  59. */
  60. function _setImplementation(address newImplementation) private {
  61. require(Address.isContract(newImplementation), "UpgradeableProxy: new implementation is not a contract");
  62. bytes32 slot = _IMPLEMENTATION_SLOT;
  63. // solhint-disable-next-line no-inline-assembly
  64. assembly {
  65. sstore(slot, newImplementation)
  66. }
  67. }
  68. }