TransparentUpgradeableProxy.sol 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.0.0) (proxy/transparent/TransparentUpgradeableProxy.sol)
  3. pragma solidity ^0.8.20;
  4. import {ERC1967Utils} from "../ERC1967/ERC1967Utils.sol";
  5. import {ERC1967Proxy} from "../ERC1967/ERC1967Proxy.sol";
  6. import {IERC1967} from "../../interfaces/IERC1967.sol";
  7. import {ProxyAdmin} from "./ProxyAdmin.sol";
  8. /**
  9. * @dev Interface for {TransparentUpgradeableProxy}. In order to implement transparency, {TransparentUpgradeableProxy}
  10. * does not implement this interface directly, and its upgradeability mechanism is implemented by an internal dispatch
  11. * mechanism. The compiler is unaware that these functions are implemented by {TransparentUpgradeableProxy} and will not
  12. * include them in the ABI so this interface must be used to interact with it.
  13. */
  14. interface ITransparentUpgradeableProxy is IERC1967 {
  15. /**
  16. * @dev Upgrade the implementation of the proxy to `newImplementation`, and subsequently execute the function call
  17. * encoded in `data`.
  18. *
  19. * See {UUPSUpgradeable-upgradeToAndCall}
  20. */
  21. function upgradeToAndCall(address newImplementation, bytes calldata data) external payable;
  22. }
  23. /**
  24. * @dev This contract implements a proxy that is upgradeable through an associated {ProxyAdmin} instance.
  25. *
  26. * To avoid https://medium.com/nomic-labs-blog/malicious-backdoors-in-ethereum-proxies-62629adf3357[proxy selector
  27. * clashing], which can potentially be used in an attack, this contract uses the
  28. * https://blog.openzeppelin.com/the-transparent-proxy-pattern/[transparent proxy pattern]. This pattern implies two
  29. * things that go hand in hand:
  30. *
  31. * 1. If any account other than the admin calls the proxy, the call will be forwarded to the implementation, even if
  32. * that call matches the {ITransparentUpgradeableProxy-upgradeToAndCall} function exposed by the proxy itself.
  33. * 2. If the admin calls the proxy, it can call the `upgradeToAndCall` function but any other call won't be forwarded to
  34. * the implementation. If the admin tries to call a function on the implementation it will fail with an error indicating
  35. * the proxy admin cannot fallback to the target implementation.
  36. *
  37. * These properties mean that the admin account can only be used for upgrading the proxy, so it's best if it's a
  38. * dedicated account that is not used for anything else. This will avoid headaches due to sudden errors when trying to
  39. * call a function from the proxy implementation. For this reason, the proxy deploys an instance of {ProxyAdmin} and
  40. * allows upgrades only if they come through it. You should think of the `ProxyAdmin` instance as the administrative
  41. * interface of the proxy, including the ability to change who can trigger upgrades by transferring ownership.
  42. *
  43. * NOTE: The real interface of this proxy is that defined in `ITransparentUpgradeableProxy`. This contract does not
  44. * inherit from that interface, and instead `upgradeToAndCall` is implicitly implemented using a custom dispatch
  45. * mechanism in `_fallback`. Consequently, the compiler will not produce an ABI for this contract. This is necessary to
  46. * fully implement transparency without decoding reverts caused by selector clashes between the proxy and the
  47. * implementation.
  48. *
  49. * NOTE: This proxy does not inherit from {Context} deliberately. The {ProxyAdmin} of this contract won't send a
  50. * meta-transaction in any way, and any other meta-transaction setup should be made in the implementation contract.
  51. *
  52. * IMPORTANT: This contract avoids unnecessary storage reads by setting the admin only during construction as an
  53. * immutable variable, preventing any changes thereafter. However, the admin slot defined in ERC-1967 can still be
  54. * overwritten by the implementation logic pointed to by this proxy. In such cases, the contract may end up in an
  55. * undesirable state where the admin slot is different from the actual admin. Relying on the value of the admin slot
  56. * is generally fine if the implementation is trusted.
  57. *
  58. * WARNING: It is not recommended to extend this contract to add additional external functions. If you do so, the
  59. * compiler will not check that there are no selector conflicts, due to the note above. A selector clash between any new
  60. * function and the functions declared in {ITransparentUpgradeableProxy} will be resolved in favor of the new one. This
  61. * could render the `upgradeToAndCall` function inaccessible, preventing upgradeability and compromising transparency.
  62. */
  63. contract TransparentUpgradeableProxy is ERC1967Proxy {
  64. // An immutable address for the admin to avoid unnecessary SLOADs before each call
  65. // at the expense of removing the ability to change the admin once it's set.
  66. // This is acceptable if the admin is always a ProxyAdmin instance or similar contract
  67. // with its own ability to transfer the permissions to another account.
  68. address private immutable _admin;
  69. /**
  70. * @dev The proxy caller is the current admin, and can't fallback to the proxy target.
  71. */
  72. error ProxyDeniedAdminAccess();
  73. /**
  74. * @dev Initializes an upgradeable proxy managed by an instance of a {ProxyAdmin} with an `initialOwner`,
  75. * backed by the implementation at `_logic`, and optionally initialized with `_data` as explained in
  76. * {ERC1967Proxy-constructor}.
  77. */
  78. constructor(address _logic, address initialOwner, bytes memory _data) payable ERC1967Proxy(_logic, _data) {
  79. _admin = address(new ProxyAdmin(initialOwner));
  80. // Set the storage value and emit an event for ERC-1967 compatibility
  81. ERC1967Utils.changeAdmin(_proxyAdmin());
  82. }
  83. /**
  84. * @dev Returns the admin of this proxy.
  85. */
  86. function _proxyAdmin() internal view virtual returns (address) {
  87. return _admin;
  88. }
  89. /**
  90. * @dev If caller is the admin process the call internally, otherwise transparently fallback to the proxy behavior.
  91. */
  92. function _fallback() internal virtual override {
  93. if (msg.sender == _proxyAdmin()) {
  94. if (msg.sig != ITransparentUpgradeableProxy.upgradeToAndCall.selector) {
  95. revert ProxyDeniedAdminAccess();
  96. } else {
  97. _dispatchUpgradeToAndCall();
  98. }
  99. } else {
  100. super._fallback();
  101. }
  102. }
  103. /**
  104. * @dev Upgrade the implementation of the proxy. See {ERC1967Utils-upgradeToAndCall}.
  105. *
  106. * Requirements:
  107. *
  108. * - If `data` is empty, `msg.value` must be zero.
  109. */
  110. function _dispatchUpgradeToAndCall() private {
  111. (address newImplementation, bytes memory data) = abi.decode(msg.data[4:], (address, bytes));
  112. ERC1967Utils.upgradeToAndCall(newImplementation, data);
  113. }
  114. }