TransparentUpgradeableProxy.sol 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.0;
  3. import "../ERC1967/ERC1967Proxy.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 interface of your proxy.
  24. */
  25. contract TransparentUpgradeableProxy is ERC1967Proxy {
  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 {ERC1967Proxy-constructor}.
  29. */
  30. constructor(address _logic, address admin_, bytes memory _data) payable ERC1967Proxy(_logic, _data) {
  31. assert(_ADMIN_SLOT == bytes32(uint256(keccak256("eip1967.proxy.admin")) - 1));
  32. _changeAdmin(admin_);
  33. }
  34. /**
  35. * @dev Modifier used internally that will delegate the call to the implementation unless the sender is the admin.
  36. */
  37. modifier ifAdmin() {
  38. if (msg.sender == _getAdmin()) {
  39. _;
  40. } else {
  41. _fallback();
  42. }
  43. }
  44. /**
  45. * @dev Returns the current admin.
  46. *
  47. * NOTE: Only the admin can call this function. See {ProxyAdmin-getProxyAdmin}.
  48. *
  49. * TIP: To get this value clients can read directly from the storage slot shown below (specified by EIP1967) using the
  50. * https://eth.wiki/json-rpc/API#eth_getstorageat[`eth_getStorageAt`] RPC call.
  51. * `0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103`
  52. */
  53. function admin() external ifAdmin returns (address admin_) {
  54. admin_ = _getAdmin();
  55. }
  56. /**
  57. * @dev Returns the current implementation.
  58. *
  59. * NOTE: Only the admin can call this function. See {ProxyAdmin-getProxyImplementation}.
  60. *
  61. * TIP: To get this value clients can read directly from the storage slot shown below (specified by EIP1967) using the
  62. * https://eth.wiki/json-rpc/API#eth_getstorageat[`eth_getStorageAt`] RPC call.
  63. * `0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc`
  64. */
  65. function implementation() external ifAdmin returns (address implementation_) {
  66. implementation_ = _implementation();
  67. }
  68. /**
  69. * @dev Changes the admin of the proxy.
  70. *
  71. * Emits an {AdminChanged} event.
  72. *
  73. * NOTE: Only the admin can call this function. See {ProxyAdmin-changeProxyAdmin}.
  74. */
  75. function changeAdmin(address newAdmin) external virtual ifAdmin {
  76. _changeAdmin(newAdmin);
  77. }
  78. /**
  79. * @dev Upgrade the implementation of the proxy.
  80. *
  81. * NOTE: Only the admin can call this function. See {ProxyAdmin-upgrade}.
  82. */
  83. function upgradeTo(address newImplementation) external ifAdmin {
  84. _upgradeToAndCall(newImplementation, bytes(""), false);
  85. }
  86. /**
  87. * @dev Upgrade the implementation of the proxy, and then call a function from the new implementation as specified
  88. * by `data`, which should be an encoded function call. This is useful to initialize new storage variables in the
  89. * proxied contract.
  90. *
  91. * NOTE: Only the admin can call this function. See {ProxyAdmin-upgradeAndCall}.
  92. */
  93. function upgradeToAndCall(address newImplementation, bytes calldata data) external payable ifAdmin {
  94. _upgradeToAndCall(newImplementation, data, true);
  95. }
  96. /**
  97. * @dev Returns the current admin.
  98. */
  99. function _admin() internal view virtual returns (address) {
  100. return _getAdmin();
  101. }
  102. /**
  103. * @dev Makes sure the admin cannot access the fallback function. See {Proxy-_beforeFallback}.
  104. */
  105. function _beforeFallback() internal virtual override {
  106. require(msg.sender != _getAdmin(), "TransparentUpgradeableProxy: admin cannot fallback to proxy target");
  107. super._beforeFallback();
  108. }
  109. }