ProxyAdmin.sol 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.6.0;
  3. import "../access/Ownable.sol";
  4. import "./TransparentUpgradeableProxy.sol";
  5. /**
  6. * @title ProxyAdmin
  7. * @dev This contract is the admin of a proxy, and is in charge
  8. * of upgrading it as well as transferring it to another admin.
  9. */
  10. contract ProxyAdmin is Ownable {
  11. /**
  12. * @dev Returns the current implementation of a proxy.
  13. * This is needed because only the proxy admin can query it.
  14. * @return The address of the current implementation of the proxy.
  15. */
  16. function getProxyImplementation(TransparentUpgradeableProxy proxy) public view returns (address) {
  17. // We need to manually run the static call since the getter cannot be flagged as view
  18. // bytes4(keccak256("implementation()")) == 0x5c60da1b
  19. (bool success, bytes memory returndata) = address(proxy).staticcall(hex"5c60da1b");
  20. require(success);
  21. return abi.decode(returndata, (address));
  22. }
  23. /**
  24. * @dev Returns the admin of a proxy. Only the admin can query it.
  25. * @return The address of the current admin of the proxy.
  26. */
  27. function getProxyAdmin(TransparentUpgradeableProxy proxy) public view returns (address) {
  28. // We need to manually run the static call since the getter cannot be flagged as view
  29. // bytes4(keccak256("admin()")) == 0xf851a440
  30. (bool success, bytes memory returndata) = address(proxy).staticcall(hex"f851a440");
  31. require(success);
  32. return abi.decode(returndata, (address));
  33. }
  34. /**
  35. * @dev Changes the admin of a proxy.
  36. * @param proxy Proxy to change admin.
  37. * @param newAdmin Address to transfer proxy administration to.
  38. */
  39. function changeProxyAdmin(TransparentUpgradeableProxy proxy, address newAdmin) public onlyOwner {
  40. proxy.changeAdmin(newAdmin);
  41. }
  42. /**
  43. * @dev Upgrades a proxy to the newest implementation of a contract.
  44. * @param proxy Proxy to be upgraded.
  45. * @param implementation the address of the Implementation.
  46. */
  47. function upgrade(TransparentUpgradeableProxy proxy, address implementation) public onlyOwner {
  48. proxy.upgradeTo(implementation);
  49. }
  50. /**
  51. * @dev Upgrades a proxy to the newest implementation of a contract and forwards a function call to it.
  52. * This is useful to initialize the proxied contract.
  53. * @param proxy Proxy to be upgraded.
  54. * @param implementation Address of the Implementation.
  55. * @param data Data to send as msg.data in the low level call.
  56. * It should include the signature and the parameters of the function to be called, as described in
  57. * https://solidity.readthedocs.io/en/v0.4.24/abi-spec.html#function-selector-and-argument-encoding.
  58. */
  59. function upgradeAndCall(TransparentUpgradeableProxy proxy, address implementation, bytes memory data) public payable onlyOwner {
  60. proxy.upgradeToAndCall{value: msg.value}(implementation, data);
  61. }
  62. }