TransparentUpgradeableProxy.sol 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 This contract implements a proxy that is upgradeable by an admin.
  7. *
  8. * To avoid https://medium.com/nomic-labs-blog/malicious-backdoors-in-ethereum-proxies-62629adf3357[proxy selector
  9. * clashing], which can potentially be used in an attack, this contract uses the
  10. * https://blog.openzeppelin.com/the-transparent-proxy-pattern/[transparent proxy pattern]. This pattern implies two
  11. * things that go hand in hand:
  12. *
  13. * 1. If any account other than the admin calls the proxy, the call will be forwarded to the implementation, even if
  14. * that call matches one of the admin functions exposed by the proxy itself.
  15. * 2. If the admin calls the proxy, it can access the admin functions, but its calls will never be forwarded to the
  16. * implementation. If the admin tries to call a function on the implementation it will fail with an error that says
  17. * "admin cannot fallback to proxy target".
  18. *
  19. * These properties mean that the admin account can only be used for admin actions like upgrading the proxy or changing
  20. * the admin, so it's best if it's a dedicated account that is not used for anything else. This will avoid headaches due
  21. * to sudden errors when trying to call a function from the proxy implementation.
  22. *
  23. * Our recommendation is for the dedicated account to be an instance of the {ProxyAdmin} contract. If set up this way,
  24. * you should think of the `ProxyAdmin` instance as the real administrative interface of your proxy.
  25. */
  26. contract TransparentUpgradeableProxy is ERC1967Proxy {
  27. /**
  28. * @dev Initializes an upgradeable proxy managed by `_admin`, backed by the implementation at `_logic`, and
  29. * optionally initialized with `_data` as explained in {ERC1967Proxy-constructor}.
  30. */
  31. constructor(
  32. address _logic,
  33. address admin_,
  34. bytes memory _data
  35. ) payable ERC1967Proxy(_logic, _data) {
  36. _changeAdmin(admin_);
  37. }
  38. /**
  39. * @dev Modifier used internally that will delegate the call to the implementation unless the sender is the admin.
  40. */
  41. modifier ifAdmin() {
  42. if (msg.sender == _getAdmin()) {
  43. _;
  44. } else {
  45. _fallback();
  46. }
  47. }
  48. /**
  49. * @dev Returns the current admin.
  50. *
  51. * NOTE: Only the admin can call this function. See {ProxyAdmin-getProxyAdmin}.
  52. *
  53. * TIP: To get this value clients can read directly from the storage slot shown below (specified by EIP1967) using the
  54. * https://eth.wiki/json-rpc/API#eth_getstorageat[`eth_getStorageAt`] RPC call.
  55. * `0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103`
  56. */
  57. function admin() external payable ifAdmin returns (address admin_) {
  58. _requireZeroValue();
  59. admin_ = _getAdmin();
  60. }
  61. /**
  62. * @dev Returns the current implementation.
  63. *
  64. * NOTE: Only the admin can call this function. See {ProxyAdmin-getProxyImplementation}.
  65. *
  66. * TIP: To get this value clients can read directly from the storage slot shown below (specified by EIP1967) using the
  67. * https://eth.wiki/json-rpc/API#eth_getstorageat[`eth_getStorageAt`] RPC call.
  68. * `0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc`
  69. */
  70. function implementation() external payable ifAdmin returns (address implementation_) {
  71. _requireZeroValue();
  72. implementation_ = _implementation();
  73. }
  74. /**
  75. * @dev Changes the admin of the proxy.
  76. *
  77. * Emits an {AdminChanged} event.
  78. *
  79. * NOTE: Only the admin can call this function. See {ProxyAdmin-changeProxyAdmin}.
  80. */
  81. function changeAdmin(address newAdmin) external payable virtual ifAdmin {
  82. _requireZeroValue();
  83. _changeAdmin(newAdmin);
  84. }
  85. /**
  86. * @dev Upgrade the implementation of the proxy.
  87. *
  88. * NOTE: Only the admin can call this function. See {ProxyAdmin-upgrade}.
  89. */
  90. function upgradeTo(address newImplementation) external payable ifAdmin {
  91. _requireZeroValue();
  92. _upgradeToAndCall(newImplementation, bytes(""), false);
  93. }
  94. /**
  95. * @dev Upgrade the implementation of the proxy, and then call a function from the new implementation as specified
  96. * by `data`, which should be an encoded function call. This is useful to initialize new storage variables in the
  97. * proxied contract.
  98. *
  99. * NOTE: Only the admin can call this function. See {ProxyAdmin-upgradeAndCall}.
  100. */
  101. function upgradeToAndCall(address newImplementation, bytes calldata data) external payable ifAdmin {
  102. _upgradeToAndCall(newImplementation, data, true);
  103. }
  104. /**
  105. * @dev Returns the current admin.
  106. */
  107. function _admin() internal view virtual returns (address) {
  108. return _getAdmin();
  109. }
  110. /**
  111. * @dev Makes sure the admin cannot access the fallback function. See {Proxy-_beforeFallback}.
  112. */
  113. function _beforeFallback() internal virtual override {
  114. require(msg.sender != _getAdmin(), "TransparentUpgradeableProxy: admin cannot fallback to proxy target");
  115. super._beforeFallback();
  116. }
  117. /**
  118. * @dev To keep this contract fully transparent, all `ifAdmin` functions must be payable. This helper is here to
  119. * emulate some proxy functions being non-payable while still allowing value to pass through.
  120. */
  121. function _requireZeroValue() private {
  122. require(msg.value == 0);
  123. }
  124. }