ProxyAdmin.sol 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts v4.4.1 (proxy/transparent/ProxyAdmin.sol)
  3. pragma solidity ^0.8.0;
  4. import "./TransparentUpgradeableProxy.sol";
  5. import "../../access/Ownable.sol";
  6. /**
  7. * @dev This is an auxiliary contract meant to be assigned as the admin of a {TransparentUpgradeableProxy}. For an
  8. * explanation of why you would want to use this see the documentation for {TransparentUpgradeableProxy}.
  9. */
  10. contract ProxyAdmin is Ownable {
  11. /**
  12. * @dev Returns the current implementation of `proxy`.
  13. *
  14. * Requirements:
  15. *
  16. * - This contract must be the admin of `proxy`.
  17. */
  18. function getProxyImplementation(TransparentUpgradeableProxy proxy) public view virtual returns (address) {
  19. // We need to manually run the static call since the getter cannot be flagged as view
  20. // bytes4(keccak256("implementation()")) == 0x5c60da1b
  21. (bool success, bytes memory returndata) = address(proxy).staticcall(hex"5c60da1b");
  22. require(success);
  23. return abi.decode(returndata, (address));
  24. }
  25. /**
  26. * @dev Returns the current admin of `proxy`.
  27. *
  28. * Requirements:
  29. *
  30. * - This contract must be the admin of `proxy`.
  31. */
  32. function getProxyAdmin(TransparentUpgradeableProxy proxy) public view virtual returns (address) {
  33. // We need to manually run the static call since the getter cannot be flagged as view
  34. // bytes4(keccak256("admin()")) == 0xf851a440
  35. (bool success, bytes memory returndata) = address(proxy).staticcall(hex"f851a440");
  36. require(success);
  37. return abi.decode(returndata, (address));
  38. }
  39. /**
  40. * @dev Changes the admin of `proxy` to `newAdmin`.
  41. *
  42. * Requirements:
  43. *
  44. * - This contract must be the current admin of `proxy`.
  45. */
  46. function changeProxyAdmin(TransparentUpgradeableProxy proxy, address newAdmin) public virtual onlyOwner {
  47. proxy.changeAdmin(newAdmin);
  48. }
  49. /**
  50. * @dev Upgrades `proxy` to `implementation`. See {TransparentUpgradeableProxy-upgradeTo}.
  51. *
  52. * Requirements:
  53. *
  54. * - This contract must be the admin of `proxy`.
  55. */
  56. function upgrade(TransparentUpgradeableProxy proxy, address implementation) public virtual onlyOwner {
  57. proxy.upgradeTo(implementation);
  58. }
  59. /**
  60. * @dev Upgrades `proxy` to `implementation` and calls a function on the new implementation. See
  61. * {TransparentUpgradeableProxy-upgradeToAndCall}.
  62. *
  63. * Requirements:
  64. *
  65. * - This contract must be the admin of `proxy`.
  66. */
  67. function upgradeAndCall(
  68. TransparentUpgradeableProxy proxy,
  69. address implementation,
  70. bytes memory data
  71. ) public payable virtual onlyOwner {
  72. proxy.upgradeToAndCall{value: msg.value}(implementation, data);
  73. }
  74. }