ProxyAdmin.sol 2.6 KB

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