UpgradeableProxy.sol 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.6.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. // solhint-disable-next-line avoid-low-level-calls
  26. (bool success,) = _logic.delegatecall(_data);
  27. require(success);
  28. }
  29. }
  30. /**
  31. * @dev Emitted when the implementation is upgraded.
  32. */
  33. event Upgraded(address indexed implementation);
  34. /**
  35. * @dev Storage slot with the address of the current implementation.
  36. * This is the keccak-256 hash of "eip1967.proxy.implementation" subtracted by 1, and is
  37. * validated in the constructor.
  38. */
  39. bytes32 private constant _IMPLEMENTATION_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;
  40. /**
  41. * @dev Returns the current implementation address.
  42. */
  43. function _implementation() internal override view returns (address impl) {
  44. bytes32 slot = _IMPLEMENTATION_SLOT;
  45. // solhint-disable-next-line no-inline-assembly
  46. assembly {
  47. impl := sload(slot)
  48. }
  49. }
  50. /**
  51. * @dev Upgrades the proxy to a new implementation.
  52. *
  53. * Emits an {Upgraded} event.
  54. */
  55. function _upgradeTo(address newImplementation) internal {
  56. _setImplementation(newImplementation);
  57. emit Upgraded(newImplementation);
  58. }
  59. /**
  60. * @dev Stores a new address in the EIP1967 implementation slot.
  61. */
  62. function _setImplementation(address newImplementation) private {
  63. require(Address.isContract(newImplementation), "UpgradeableProxy: new implementation is not a contract");
  64. bytes32 slot = _IMPLEMENTATION_SLOT;
  65. // solhint-disable-next-line no-inline-assembly
  66. assembly {
  67. sstore(slot, newImplementation)
  68. }
  69. }
  70. }