TransparentUpgradeableProxy.sol 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.7.0;
  3. import "./UpgradeableProxy.sol";
  4. /**
  5. * @dev This contract implements a proxy that is upgradeable by an admin.
  6. *
  7. * To avoid https://medium.com/nomic-labs-blog/malicious-backdoors-in-ethereum-proxies-62629adf3357[proxy selector
  8. * clashing], which can potentially be used in an attack, this contract uses the
  9. * https://blog.openzeppelin.com/the-transparent-proxy-pattern/[transparent proxy pattern]. This pattern implies two
  10. * things that go hand in hand:
  11. *
  12. * 1. If any account other than the admin calls the proxy, the call will be forwarded to the implementation, even if
  13. * that call matches one of the admin functions exposed by the proxy itself.
  14. * 2. If the admin calls the proxy, it can access the admin functions, but its calls will never be forwarded to the
  15. * implementation. If the admin tries to call a function on the implementation it will fail with an error that says
  16. * "admin cannot fallback to proxy target".
  17. *
  18. * These properties mean that the admin account can only be used for admin actions like upgrading the proxy or changing
  19. * the admin, so it's best if it's a dedicated account that is not used for anything else. This will avoid headaches due
  20. * to sudden errors when trying to call a function from the proxy implementation.
  21. *
  22. * Our recommendation is for the dedicated account to be an instance of the {ProxyAdmin} contract. If set up this way,
  23. * you should think of the `ProxyAdmin` instance as the real administrative inerface of your proxy.
  24. */
  25. contract TransparentUpgradeableProxy is UpgradeableProxy {
  26. /**
  27. * @dev Initializes an upgradeable proxy managed by `_admin`, backed by the implementation at `_logic`, and
  28. * optionally initialized with `_data` as explained in {UpgradeableProxy-constructor}.
  29. */
  30. constructor(address _logic, address _admin, bytes memory _data) payable UpgradeableProxy(_logic, _data) {
  31. assert(_ADMIN_SLOT == bytes32(uint256(keccak256("eip1967.proxy.admin")) - 1));
  32. _setAdmin(_admin);
  33. }
  34. /**
  35. * @dev Emitted when the admin account has changed.
  36. */
  37. event AdminChanged(address previousAdmin, address newAdmin);
  38. /**
  39. * @dev Storage slot with the admin of the contract.
  40. * This is the keccak-256 hash of "eip1967.proxy.admin" subtracted by 1, and is
  41. * validated in the constructor.
  42. */
  43. bytes32 private constant _ADMIN_SLOT = 0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103;
  44. /**
  45. * @dev Modifier used internally that will delegate the call to the implementation unless the sender is the admin.
  46. */
  47. modifier ifAdmin() {
  48. if (msg.sender == _admin()) {
  49. _;
  50. } else {
  51. _fallback();
  52. }
  53. }
  54. /**
  55. * @dev Returns the current admin.
  56. *
  57. * NOTE: Only the admin can call this function. See {ProxyAdmin-getProxyAdmin}.
  58. *
  59. * TIP: To get this value clients can read directly from the storage slot shown below (specified by EIP1967) using the
  60. * https://eth.wiki/json-rpc/API#eth_getstorageat[`eth_getStorageAt`] RPC call.
  61. * `0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103`
  62. */
  63. function admin() external ifAdmin returns (address) {
  64. return _admin();
  65. }
  66. /**
  67. * @dev Returns the current implementation.
  68. *
  69. * NOTE: Only the admin can call this function. See {ProxyAdmin-getProxyImplementation}.
  70. *
  71. * TIP: To get this value clients can read directly from the storage slot shown below (specified by EIP1967) using the
  72. * https://eth.wiki/json-rpc/API#eth_getstorageat[`eth_getStorageAt`] RPC call.
  73. * `0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc`
  74. */
  75. function implementation() external ifAdmin returns (address) {
  76. return _implementation();
  77. }
  78. /**
  79. * @dev Changes the admin of the proxy.
  80. *
  81. * Emits an {AdminChanged} event.
  82. *
  83. * NOTE: Only the admin can call this function. See {ProxyAdmin-changeProxyAdmin}.
  84. */
  85. function changeAdmin(address newAdmin) external ifAdmin {
  86. require(newAdmin != address(0), "TransparentUpgradeableProxy: new admin is the zero address");
  87. emit AdminChanged(_admin(), newAdmin);
  88. _setAdmin(newAdmin);
  89. }
  90. /**
  91. * @dev Upgrade the implementation of the proxy.
  92. *
  93. * NOTE: Only the admin can call this function. See {ProxyAdmin-upgrade}.
  94. */
  95. function upgradeTo(address newImplementation) external ifAdmin {
  96. _upgradeTo(newImplementation);
  97. }
  98. /**
  99. * @dev Upgrade the implementation of the proxy, and then call a function from the new implementation as specified
  100. * by `data`, which should be an encoded function call. This is useful to initialize new storage variables in the
  101. * proxied contract.
  102. *
  103. * NOTE: Only the admin can call this function. See {ProxyAdmin-upgradeAndCall}.
  104. */
  105. function upgradeToAndCall(address newImplementation, bytes calldata data) external payable ifAdmin {
  106. _upgradeTo(newImplementation);
  107. // solhint-disable-next-line avoid-low-level-calls
  108. (bool success,) = newImplementation.delegatecall(data);
  109. require(success);
  110. }
  111. /**
  112. * @dev Returns the current admin.
  113. */
  114. function _admin() internal view returns (address adm) {
  115. bytes32 slot = _ADMIN_SLOT;
  116. // solhint-disable-next-line no-inline-assembly
  117. assembly {
  118. adm := sload(slot)
  119. }
  120. }
  121. /**
  122. * @dev Stores a new address in the EIP1967 admin slot.
  123. */
  124. function _setAdmin(address newAdmin) private {
  125. bytes32 slot = _ADMIN_SLOT;
  126. // solhint-disable-next-line no-inline-assembly
  127. assembly {
  128. sstore(slot, newAdmin)
  129. }
  130. }
  131. /**
  132. * @dev Makes sure the admin cannot access the fallback function. See {Proxy-_beforeFallback}.
  133. */
  134. function _beforeFallback() internal override virtual {
  135. require(msg.sender != _admin(), "TransparentUpgradeableProxy: admin cannot fallback to proxy target");
  136. super._beforeFallback();
  137. }
  138. }