BeaconProxy.sol 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. contract BeaconProxy is Proxy {
  13. /**
  14. * @dev The storage slot of the UpgradeableBeacon contract which defines the implementation for this proxy.
  15. * This is bytes32(uint256(keccak256('eip1967.proxy.beacon')) - 1)) and is validated in the constructor.
  16. */
  17. bytes32 private constant _BEACON_SLOT = 0xa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b35133d50;
  18. /**
  19. * @dev Initializes the proxy with `beacon`.
  20. *
  21. * If `data` is nonempty, it's used as data in a delegate call to the implementation returned by the beacon. This
  22. * will typically be an encoded function call, and allows initializating the storage of the proxy like a Solidity
  23. * constructor.
  24. *
  25. * Requirements:
  26. *
  27. * - `beacon` must be a contract with the interface {IBeacon}.
  28. */
  29. constructor(address beacon, bytes memory data) public payable {
  30. assert(_BEACON_SLOT == bytes32(uint256(keccak256("eip1967.proxy.beacon")) - 1));
  31. _setBeacon(beacon, data);
  32. }
  33. /**
  34. * @dev Returns the current beacon address.
  35. */
  36. function _beacon() internal view virtual returns (address beacon) {
  37. bytes32 slot = _BEACON_SLOT;
  38. // solhint-disable-next-line no-inline-assembly
  39. assembly {
  40. beacon := sload(slot)
  41. }
  42. }
  43. /**
  44. * @dev Returns the current implementation address of the associated beacon.
  45. */
  46. function _implementation() internal view virtual override returns (address) {
  47. return IBeacon(_beacon()).implementation();
  48. }
  49. /**
  50. * @dev Changes the proxy to use a new beacon.
  51. *
  52. * If `data` is nonempty, it's used as data in a delegate call to the implementation returned by the beacon.
  53. *
  54. * Requirements:
  55. *
  56. * - `beacon` must be a contract.
  57. * - The implementation returned by `beacon` must be a contract.
  58. */
  59. function _setBeacon(address beacon, bytes memory data) internal virtual {
  60. require(
  61. Address.isContract(beacon),
  62. "BeaconProxy: beacon is not a contract"
  63. );
  64. require(
  65. Address.isContract(IBeacon(beacon).implementation()),
  66. "BeaconProxy: beacon implementation is not a contract"
  67. );
  68. bytes32 slot = _BEACON_SLOT;
  69. // solhint-disable-next-line no-inline-assembly
  70. assembly {
  71. sstore(slot, beacon)
  72. }
  73. if (data.length > 0) {
  74. Address.functionDelegateCall(_implementation(), data, "BeaconProxy: function call failed");
  75. }
  76. }
  77. }