TransparentUpgradeableProxy.sol 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.6.0;
  3. import "./UpgradeableProxy.sol";
  4. /**
  5. * @title TransparentUpgradeableProxy
  6. * @dev This contract combines an upgradeability proxy with an authorization
  7. * mechanism for administrative tasks.
  8. * All external functions in this contract must be guarded by the
  9. * `ifAdmin` modifier. See ethereum/solidity#3864 for a Solidity
  10. * feature proposal that would enable this to be done automatically.
  11. */
  12. contract TransparentUpgradeableProxy is UpgradeableProxy {
  13. /**
  14. * Contract constructor.
  15. * @param _logic address of the initial implementation.
  16. * @param _admin Address of the proxy administrator.
  17. * @param _data Data to send as msg.data to the implementation to initialize the proxied contract.
  18. * It should include the signature and the parameters of the function to be called, as described in
  19. * https://solidity.readthedocs.io/en/v0.4.24/abi-spec.html#function-selector-and-argument-encoding.
  20. * This parameter is optional, if no data is given the initialization call to proxied contract will be skipped.
  21. */
  22. constructor(address _logic, address _admin, bytes memory _data) public payable UpgradeableProxy(_logic, _data) {
  23. assert(_ADMIN_SLOT == bytes32(uint256(keccak256("eip1967.proxy.admin")) - 1));
  24. _setAdmin(_admin);
  25. }
  26. /**
  27. * @dev Emitted when the administration has been transferred.
  28. * @param previousAdmin Address of the previous admin.
  29. * @param newAdmin Address of the new admin.
  30. */
  31. event AdminChanged(address previousAdmin, address newAdmin);
  32. /**
  33. * @dev Storage slot with the admin of the contract.
  34. * This is the keccak-256 hash of "eip1967.proxy.admin" subtracted by 1, and is
  35. * validated in the constructor.
  36. */
  37. bytes32 private constant _ADMIN_SLOT = 0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103;
  38. /**
  39. * @dev Modifier to check whether the `msg.sender` is the admin.
  40. * If it is, it will run the function. Otherwise, it will delegate the call
  41. * to the implementation.
  42. */
  43. modifier ifAdmin() {
  44. if (msg.sender == _admin()) {
  45. _;
  46. } else {
  47. _fallback();
  48. }
  49. }
  50. /**
  51. * @return The address of the proxy admin.
  52. */
  53. function admin() external ifAdmin returns (address) {
  54. return _admin();
  55. }
  56. /**
  57. * @return The address of the implementation.
  58. */
  59. function implementation() external ifAdmin returns (address) {
  60. return _implementation();
  61. }
  62. /**
  63. * @dev Changes the admin of the proxy.
  64. * Only the current admin can call this function.
  65. * @param newAdmin Address to transfer proxy administration to.
  66. */
  67. function changeAdmin(address newAdmin) external ifAdmin {
  68. require(newAdmin != address(0), "TransparentUpgradeableProxy: new admin is the zero address");
  69. emit AdminChanged(_admin(), newAdmin);
  70. _setAdmin(newAdmin);
  71. }
  72. /**
  73. * @dev Upgrade the backing implementation of the proxy.
  74. * Only the admin can call this function.
  75. * @param newImplementation Address of the new implementation.
  76. */
  77. function upgradeTo(address newImplementation) external ifAdmin {
  78. _upgradeTo(newImplementation);
  79. }
  80. /**
  81. * @dev Upgrade the backing implementation of the proxy and call a function
  82. * on the new implementation.
  83. * This is useful to initialize the proxied contract.
  84. * @param newImplementation Address of the new implementation.
  85. * @param data Data to send as msg.data in the low level call.
  86. * It should include the signature and the parameters of the function to be called, as described in
  87. * https://solidity.readthedocs.io/en/v0.4.24/abi-spec.html#function-selector-and-argument-encoding.
  88. */
  89. function upgradeToAndCall(address newImplementation, bytes calldata data) external payable ifAdmin {
  90. _upgradeTo(newImplementation);
  91. // solhint-disable-next-line avoid-low-level-calls
  92. (bool success,) = newImplementation.delegatecall(data);
  93. require(success);
  94. }
  95. /**
  96. * @return adm The admin slot.
  97. */
  98. function _admin() internal view returns (address adm) {
  99. bytes32 slot = _ADMIN_SLOT;
  100. // solhint-disable-next-line no-inline-assembly
  101. assembly {
  102. adm := sload(slot)
  103. }
  104. }
  105. /**
  106. * @dev Sets the address of the proxy admin.
  107. * @param newAdmin Address of the new proxy admin.
  108. */
  109. function _setAdmin(address newAdmin) internal {
  110. bytes32 slot = _ADMIN_SLOT;
  111. // solhint-disable-next-line no-inline-assembly
  112. assembly {
  113. sstore(slot, newAdmin)
  114. }
  115. }
  116. /**
  117. * @dev Only fallback when the sender is not the admin.
  118. */
  119. function _willFallback() internal override virtual {
  120. require(msg.sender != _admin(), "TransparentUpgradeableProxy: admin cannot fallback to proxy target");
  121. super._willFallback();
  122. }
  123. }