UpgradeableProxy.sol 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.6.0;
  3. import "./Proxy.sol";
  4. import "../utils/Address.sol";
  5. /**
  6. * @title UpgradeableProxy
  7. * @dev This contract implements a proxy that allows to change the
  8. * implementation address to which it will delegate.
  9. * Such a change is called an implementation upgrade.
  10. */
  11. contract UpgradeableProxy is Proxy {
  12. /**
  13. * @dev Contract constructor.
  14. * @param _logic Address of the initial implementation.
  15. * @param _data Data to send as msg.data to the implementation to initialize the proxied contract.
  16. * It should include the signature and the parameters of the function to be called, as described in
  17. * https://solidity.readthedocs.io/en/v0.4.24/abi-spec.html#function-selector-and-argument-encoding.
  18. * This parameter is optional, if no data is given the initialization call to proxied contract will be skipped.
  19. */
  20. constructor(address _logic, bytes memory _data) public payable {
  21. assert(_IMPLEMENTATION_SLOT == bytes32(uint256(keccak256("eip1967.proxy.implementation")) - 1));
  22. _setImplementation(_logic);
  23. if(_data.length > 0) {
  24. // solhint-disable-next-line avoid-low-level-calls
  25. (bool success,) = _logic.delegatecall(_data);
  26. require(success);
  27. }
  28. }
  29. /**
  30. * @dev Emitted when the implementation is upgraded.
  31. * @param implementation Address of the new implementation.
  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.
  42. * @return impl Address of the current implementation
  43. */
  44. function _implementation() internal override view returns (address impl) {
  45. bytes32 slot = _IMPLEMENTATION_SLOT;
  46. // solhint-disable-next-line no-inline-assembly
  47. assembly {
  48. impl := sload(slot)
  49. }
  50. }
  51. /**
  52. * @dev Upgrades the proxy to a new implementation.
  53. * @param newImplementation Address of the new implementation.
  54. */
  55. function _upgradeTo(address newImplementation) internal {
  56. _setImplementation(newImplementation);
  57. emit Upgraded(newImplementation);
  58. }
  59. /**
  60. * @dev Sets the implementation address of the proxy.
  61. * @param newImplementation Address of the new implementation.
  62. */
  63. function _setImplementation(address newImplementation) internal {
  64. require(Address.isContract(newImplementation), "UpgradeableProxy: new implementation is not a contract");
  65. bytes32 slot = _IMPLEMENTATION_SLOT;
  66. // solhint-disable-next-line no-inline-assembly
  67. assembly {
  68. sstore(slot, newImplementation)
  69. }
  70. }
  71. }