TransparentUpgradeableProxy.sol 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.7.0) (proxy/transparent/TransparentUpgradeableProxy.sol)
  3. pragma solidity ^0.8.0;
  4. import "../ERC1967/ERC1967Proxy.sol";
  5. /**
  6. * @dev Interface for the {TransparentUpgradeableProxy}. This is useful because {TransparentUpgradeableProxy} uses a
  7. * custom call-routing mechanism, the compiler is unaware of the functions being exposed, and cannot list them. Also
  8. * {TransparentUpgradeableProxy} does not inherit from this interface because it's implemented in a way that the
  9. * compiler doesn't understand and cannot verify.
  10. */
  11. interface ITransparentUpgradeableProxy is IERC1967 {
  12. function admin() external view returns (address);
  13. function implementation() external view returns (address);
  14. function changeAdmin(address) external;
  15. function upgradeTo(address) external;
  16. function upgradeToAndCall(address, bytes memory) external payable;
  17. }
  18. /**
  19. * @dev This contract implements a proxy that is upgradeable by an admin.
  20. *
  21. * To avoid https://medium.com/nomic-labs-blog/malicious-backdoors-in-ethereum-proxies-62629adf3357[proxy selector
  22. * clashing], which can potentially be used in an attack, this contract uses the
  23. * https://blog.openzeppelin.com/the-transparent-proxy-pattern/[transparent proxy pattern]. This pattern implies two
  24. * things that go hand in hand:
  25. *
  26. * 1. If any account other than the admin calls the proxy, the call will be forwarded to the implementation, even if
  27. * that call matches one of the admin functions exposed by the proxy itself.
  28. * 2. If the admin calls the proxy, it can access the admin functions, but its calls will never be forwarded to the
  29. * implementation. If the admin tries to call a function on the implementation it will fail with an error that says
  30. * "admin cannot fallback to proxy target".
  31. *
  32. * These properties mean that the admin account can only be used for admin actions like upgrading the proxy or changing
  33. * the admin, so it's best if it's a dedicated account that is not used for anything else. This will avoid headaches due
  34. * to sudden errors when trying to call a function from the proxy implementation.
  35. *
  36. * Our recommendation is for the dedicated account to be an instance of the {ProxyAdmin} contract. If set up this way,
  37. * you should think of the `ProxyAdmin` instance as the real administrative interface of your proxy.
  38. *
  39. * WARNING: This contract does not inherit from {ITransparentUpgradeableProxy}, and the admin function is implicitly
  40. * implemented using a custom call-routing mechanism in `_fallback`. Consequently, the compiler will not produce an
  41. * ABI for this contract. Also, if you inherit from this contract and add additional functions, the compiler will not
  42. * check that there are no selector conflicts. A selector clash between any new function and the functions declared in
  43. * {ITransparentUpgradeableProxy} will be resolved in favor of the new one. This could render the admin operations
  44. * inaccessible, which could prevent upgradeability.
  45. */
  46. contract TransparentUpgradeableProxy is ERC1967Proxy {
  47. /**
  48. * @dev Initializes an upgradeable proxy managed by `_admin`, backed by the implementation at `_logic`, and
  49. * optionally initialized with `_data` as explained in {ERC1967Proxy-constructor}.
  50. */
  51. constructor(
  52. address _logic,
  53. address admin_,
  54. bytes memory _data
  55. ) payable ERC1967Proxy(_logic, _data) {
  56. _changeAdmin(admin_);
  57. }
  58. /**
  59. * @dev Modifier used internally that will delegate the call to the implementation unless the sender is the admin.
  60. *
  61. * CAUTION: This modifier is deprecated, as it could cause issues if the modified function has arguments, and the
  62. * implementation provides a function with the same selector.
  63. */
  64. modifier ifAdmin() {
  65. if (msg.sender == _getAdmin()) {
  66. _;
  67. } else {
  68. _fallback();
  69. }
  70. }
  71. /**
  72. * @dev If caller is the admin process the call internally, otherwise transparently fallback to the proxy behavior
  73. */
  74. function _fallback() internal virtual override {
  75. if (msg.sender == _getAdmin()) {
  76. bytes memory ret;
  77. bytes4 selector = msg.sig;
  78. if (selector == ITransparentUpgradeableProxy.upgradeTo.selector) {
  79. ret = _dispatchUpgradeTo();
  80. } else if (selector == ITransparentUpgradeableProxy.upgradeToAndCall.selector) {
  81. ret = _dispatchUpgradeToAndCall();
  82. } else if (selector == ITransparentUpgradeableProxy.changeAdmin.selector) {
  83. ret = _dispatchChangeAdmin();
  84. } else if (selector == ITransparentUpgradeableProxy.admin.selector) {
  85. ret = _dispatchAdmin();
  86. } else if (selector == ITransparentUpgradeableProxy.implementation.selector) {
  87. ret = _dispatchImplementation();
  88. } else {
  89. revert("TransparentUpgradeableProxy: admin cannot fallback to proxy target");
  90. }
  91. assembly {
  92. return(add(ret, 0x20), mload(ret))
  93. }
  94. } else {
  95. super._fallback();
  96. }
  97. }
  98. /**
  99. * @dev Returns the current admin.
  100. *
  101. * TIP: To get this value clients can read directly from the storage slot shown below (specified by EIP1967) using the
  102. * https://eth.wiki/json-rpc/API#eth_getstorageat[`eth_getStorageAt`] RPC call.
  103. * `0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103`
  104. */
  105. function _dispatchAdmin() private returns (bytes memory) {
  106. _requireZeroValue();
  107. address admin = _getAdmin();
  108. return abi.encode(admin);
  109. }
  110. /**
  111. * @dev Returns the current implementation.
  112. *
  113. * TIP: To get this value clients can read directly from the storage slot shown below (specified by EIP1967) using the
  114. * https://eth.wiki/json-rpc/API#eth_getstorageat[`eth_getStorageAt`] RPC call.
  115. * `0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc`
  116. */
  117. function _dispatchImplementation() private returns (bytes memory) {
  118. _requireZeroValue();
  119. address implementation = _implementation();
  120. return abi.encode(implementation);
  121. }
  122. /**
  123. * @dev Changes the admin of the proxy.
  124. *
  125. * Emits an {AdminChanged} event.
  126. */
  127. function _dispatchChangeAdmin() private returns (bytes memory) {
  128. _requireZeroValue();
  129. address newAdmin = abi.decode(msg.data[4:], (address));
  130. _changeAdmin(newAdmin);
  131. return "";
  132. }
  133. /**
  134. * @dev Upgrade the implementation of the proxy.
  135. */
  136. function _dispatchUpgradeTo() private returns (bytes memory) {
  137. _requireZeroValue();
  138. address newImplementation = abi.decode(msg.data[4:], (address));
  139. _upgradeToAndCall(newImplementation, bytes(""), false);
  140. return "";
  141. }
  142. /**
  143. * @dev Upgrade the implementation of the proxy, and then call a function from the new implementation as specified
  144. * by `data`, which should be an encoded function call. This is useful to initialize new storage variables in the
  145. * proxied contract.
  146. */
  147. function _dispatchUpgradeToAndCall() private returns (bytes memory) {
  148. (address newImplementation, bytes memory data) = abi.decode(msg.data[4:], (address, bytes));
  149. _upgradeToAndCall(newImplementation, data, true);
  150. return "";
  151. }
  152. /**
  153. * @dev Returns the current admin.
  154. */
  155. function _admin() internal view virtual returns (address) {
  156. return _getAdmin();
  157. }
  158. /**
  159. * @dev To keep this contract fully transparent, all `ifAdmin` functions must be payable. This helper is here to
  160. * emulate some proxy functions being non-payable while still allowing value to pass through.
  161. */
  162. function _requireZeroValue() private {
  163. require(msg.value == 0);
  164. }
  165. }