BeaconProxy.sol 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity >=0.6.0 <0.8.0;
  3. import "./Proxy.sol";
  4. import "../utils/Address.sol";
  5. import "./IBeacon.sol";
  6. /**
  7. * @dev This contract implements a proxy that gets the implementation address for each call from a {UpgradeableBeacon}.
  8. *
  9. * The beacon address is stored in storage slot `uint256(keccak256('eip1967.proxy.beacon')) - 1`, so that it doesn't
  10. * conflict with the storage layout of the implementation behind the proxy.
  11. *
  12. * _Available since v3.4._
  13. */
  14. contract BeaconProxy is Proxy {
  15. /**
  16. * @dev The storage slot of the UpgradeableBeacon contract which defines the implementation for this proxy.
  17. * This is bytes32(uint256(keccak256('eip1967.proxy.beacon')) - 1)) and is validated in the constructor.
  18. */
  19. bytes32 private constant _BEACON_SLOT = 0xa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b35133d50;
  20. /**
  21. * @dev Initializes the proxy with `beacon`.
  22. *
  23. * If `data` is nonempty, it's used as data in a delegate call to the implementation returned by the beacon. This
  24. * will typically be an encoded function call, and allows initializating the storage of the proxy like a Solidity
  25. * constructor.
  26. *
  27. * Requirements:
  28. *
  29. * - `beacon` must be a contract with the interface {IBeacon}.
  30. */
  31. constructor(address beacon, bytes memory data) payable {
  32. assert(_BEACON_SLOT == bytes32(uint256(keccak256("eip1967.proxy.beacon")) - 1));
  33. _setBeacon(beacon, data);
  34. }
  35. /**
  36. * @dev Returns the current beacon address.
  37. */
  38. function _beacon() internal view virtual returns (address beacon) {
  39. bytes32 slot = _BEACON_SLOT;
  40. // solhint-disable-next-line no-inline-assembly
  41. assembly {
  42. beacon := sload(slot)
  43. }
  44. }
  45. /**
  46. * @dev Returns the current implementation address of the associated beacon.
  47. */
  48. function _implementation() internal view virtual override returns (address) {
  49. return IBeacon(_beacon()).implementation();
  50. }
  51. /**
  52. * @dev Changes the proxy to use a new beacon.
  53. *
  54. * If `data` is nonempty, it's used as data in a delegate call to the implementation returned by the beacon.
  55. *
  56. * Requirements:
  57. *
  58. * - `beacon` must be a contract.
  59. * - The implementation returned by `beacon` must be a contract.
  60. */
  61. function _setBeacon(address beacon, bytes memory data) internal virtual {
  62. require(
  63. Address.isContract(beacon),
  64. "BeaconProxy: beacon is not a contract"
  65. );
  66. require(
  67. Address.isContract(IBeacon(beacon).implementation()),
  68. "BeaconProxy: beacon implementation is not a contract"
  69. );
  70. bytes32 slot = _BEACON_SLOT;
  71. // solhint-disable-next-line no-inline-assembly
  72. assembly {
  73. sstore(slot, beacon)
  74. }
  75. if (data.length > 0) {
  76. Address.functionDelegateCall(_implementation(), data, "BeaconProxy: function call failed");
  77. }
  78. }
  79. }