github.com/klaytn/klaytn@v1.12.1/contracts/system_contracts/lib/ERC1967Proxy.sol (about)

     1  // Copyright 2023 The klaytn Authors
     2  // This file is part of the klaytn library.
     3  //
     4  // The klaytn library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The klaytn library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the klaytn library. If not, see <http://www.gnu.org/licenses/>.
    16  //
    17  // This flattened solidity file is from openzeppelin/proxy/ERC1967/ERC1967Proxy.sol
    18  // Sources flattened with hardhat v2.13.0 https://hardhat.org
    19  
    20  // File @openzeppelin/contracts/interfaces/draft-IERC1822.sol@v4.9.2
    21  
    22  // SPDX-License-Identifier: MIT
    23  // OpenZeppelin Contracts (last updated v4.5.0) (interfaces/draft-IERC1822.sol)
    24  
    25  pragma solidity ^0.8.0;
    26  
    27  /**
    28   * @dev ERC1822: Universal Upgradeable Proxy Standard (UUPS) documents a method for upgradeability through a simplified
    29   * proxy whose upgrades are fully controlled by the current implementation.
    30   */
    31  interface IERC1822Proxiable {
    32      /**
    33       * @dev Returns the storage slot that the proxiable contract assumes is being used to store the implementation
    34       * address.
    35       *
    36       * IMPORTANT: A proxy pointing at a proxiable contract should not be considered proxiable itself, because this risks
    37       * bricking a proxy that upgrades to it, by delegating to itself until out of gas. Thus it is critical that this
    38       * function revert if invoked through a proxy.
    39       */
    40      function proxiableUUID() external view returns (bytes32);
    41  }
    42  
    43  // File @openzeppelin/contracts/interfaces/IERC1967.sol@v4.9.2
    44  
    45  // OpenZeppelin Contracts (last updated v4.9.0) (interfaces/IERC1967.sol)
    46  
    47  pragma solidity ^0.8.0;
    48  
    49  /**
    50   * @dev ERC-1967: Proxy Storage Slots. This interface contains the events defined in the ERC.
    51   *
    52   * _Available since v4.8.3._
    53   */
    54  interface IERC1967 {
    55      /**
    56       * @dev Emitted when the implementation is upgraded.
    57       */
    58      event Upgraded(address indexed implementation);
    59  
    60      /**
    61       * @dev Emitted when the admin account has changed.
    62       */
    63      event AdminChanged(address previousAdmin, address newAdmin);
    64  
    65      /**
    66       * @dev Emitted when the beacon is changed.
    67       */
    68      event BeaconUpgraded(address indexed beacon);
    69  }
    70  
    71  // File @openzeppelin/contracts/proxy/beacon/IBeacon.sol@v4.9.2
    72  
    73  // OpenZeppelin Contracts v4.4.1 (proxy/beacon/IBeacon.sol)
    74  
    75  pragma solidity ^0.8.0;
    76  
    77  /**
    78   * @dev This is the interface that {BeaconProxy} expects of its beacon.
    79   */
    80  interface IBeacon {
    81      /**
    82       * @dev Must return an address that can be used as a delegate call target.
    83       *
    84       * {BeaconProxy} will check that this address is a contract.
    85       */
    86      function implementation() external view returns (address);
    87  }
    88  
    89  // File @openzeppelin/contracts/utils/Address.sol@v4.9.2
    90  
    91  // OpenZeppelin Contracts (last updated v4.9.0) (utils/Address.sol)
    92  
    93  pragma solidity ^0.8.1;
    94  
    95  /**
    96   * @dev Collection of functions related to the address type
    97   */
    98  library Address {
    99      /**
   100       * @dev Returns true if `account` is a contract.
   101       *
   102       * [IMPORTANT]
   103       * ====
   104       * It is unsafe to assume that an address for which this function returns
   105       * false is an externally-owned account (EOA) and not a contract.
   106       *
   107       * Among others, `isContract` will return false for the following
   108       * types of addresses:
   109       *
   110       *  - an externally-owned account
   111       *  - a contract in construction
   112       *  - an address where a contract will be created
   113       *  - an address where a contract lived, but was destroyed
   114       *
   115       * Furthermore, `isContract` will also return true if the target contract within
   116       * the same transaction is already scheduled for destruction by `SELFDESTRUCT`,
   117       * which only has an effect at the end of a transaction.
   118       * ====
   119       *
   120       * [IMPORTANT]
   121       * ====
   122       * You shouldn't rely on `isContract` to protect against flash loan attacks!
   123       *
   124       * Preventing calls from contracts is highly discouraged. It breaks composability, breaks support for smart wallets
   125       * like Gnosis Safe, and does not provide security since it can be circumvented by calling from a contract
   126       * constructor.
   127       * ====
   128       */
   129      function isContract(address account) internal view returns (bool) {
   130          // This method relies on extcodesize/address.code.length, which returns 0
   131          // for contracts in construction, since the code is only stored at the end
   132          // of the constructor execution.
   133  
   134          return account.code.length > 0;
   135      }
   136  
   137      /**
   138       * @dev Replacement for Solidity's `transfer`: sends `amount` wei to
   139       * `recipient`, forwarding all available gas and reverting on errors.
   140       *
   141       * https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
   142       * of certain opcodes, possibly making contracts go over the 2300 gas limit
   143       * imposed by `transfer`, making them unable to receive funds via
   144       * `transfer`. {sendValue} removes this limitation.
   145       *
   146       * https://consensys.net/diligence/blog/2019/09/stop-using-soliditys-transfer-now/[Learn more].
   147       *
   148       * IMPORTANT: because control is transferred to `recipient`, care must be
   149       * taken to not create reentrancy vulnerabilities. Consider using
   150       * {ReentrancyGuard} or the
   151       * https://solidity.readthedocs.io/en/v0.8.0/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
   152       */
   153      function sendValue(address payable recipient, uint256 amount) internal {
   154          require(address(this).balance >= amount, "Address: insufficient balance");
   155  
   156          (bool success, ) = recipient.call{ value: amount }("");
   157          require(success, "Address: unable to send value, recipient may have reverted");
   158      }
   159  
   160      /**
   161       * @dev Performs a Solidity function call using a low level `call`. A
   162       * plain `call` is an unsafe replacement for a function call: use this
   163       * function instead.
   164       *
   165       * If `target` reverts with a revert reason, it is bubbled up by this
   166       * function (like regular Solidity function calls).
   167       *
   168       * Returns the raw returned data. To convert to the expected return value,
   169       * use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
   170       *
   171       * Requirements:
   172       *
   173       * - `target` must be a contract.
   174       * - calling `target` with `data` must not revert.
   175       *
   176       * _Available since v3.1._
   177       */
   178      function functionCall(address target, bytes memory data) internal returns (bytes memory) {
   179          return functionCallWithValue(target, data, 0, "Address: low-level call failed");
   180      }
   181  
   182      /**
   183       * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with
   184       * `errorMessage` as a fallback revert reason when `target` reverts.
   185       *
   186       * _Available since v3.1._
   187       */
   188      function functionCall(
   189          address target,
   190          bytes memory data,
   191          string memory errorMessage
   192      ) internal returns (bytes memory) {
   193          return functionCallWithValue(target, data, 0, errorMessage);
   194      }
   195  
   196      /**
   197       * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
   198       * but also transferring `value` wei to `target`.
   199       *
   200       * Requirements:
   201       *
   202       * - the calling contract must have an ETH balance of at least `value`.
   203       * - the called Solidity function must be `payable`.
   204       *
   205       * _Available since v3.1._
   206       */
   207      function functionCallWithValue(address target, bytes memory data, uint256 value) internal returns (bytes memory) {
   208          return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
   209      }
   210  
   211      /**
   212       * @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but
   213       * with `errorMessage` as a fallback revert reason when `target` reverts.
   214       *
   215       * _Available since v3.1._
   216       */
   217      function functionCallWithValue(
   218          address target,
   219          bytes memory data,
   220          uint256 value,
   221          string memory errorMessage
   222      ) internal returns (bytes memory) {
   223          require(address(this).balance >= value, "Address: insufficient balance for call");
   224          (bool success, bytes memory returndata) = target.call{ value: value }(data);
   225          return verifyCallResultFromTarget(target, success, returndata, errorMessage);
   226      }
   227  
   228      /**
   229       * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
   230       * but performing a static call.
   231       *
   232       * _Available since v3.3._
   233       */
   234      function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
   235          return functionStaticCall(target, data, "Address: low-level static call failed");
   236      }
   237  
   238      /**
   239       * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
   240       * but performing a static call.
   241       *
   242       * _Available since v3.3._
   243       */
   244      function functionStaticCall(
   245          address target,
   246          bytes memory data,
   247          string memory errorMessage
   248      ) internal view returns (bytes memory) {
   249          (bool success, bytes memory returndata) = target.staticcall(data);
   250          return verifyCallResultFromTarget(target, success, returndata, errorMessage);
   251      }
   252  
   253      /**
   254       * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
   255       * but performing a delegate call.
   256       *
   257       * _Available since v3.4._
   258       */
   259      function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
   260          return functionDelegateCall(target, data, "Address: low-level delegate call failed");
   261      }
   262  
   263      /**
   264       * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
   265       * but performing a delegate call.
   266       *
   267       * _Available since v3.4._
   268       */
   269      function functionDelegateCall(
   270          address target,
   271          bytes memory data,
   272          string memory errorMessage
   273      ) internal returns (bytes memory) {
   274          (bool success, bytes memory returndata) = target.delegatecall(data);
   275          return verifyCallResultFromTarget(target, success, returndata, errorMessage);
   276      }
   277  
   278      /**
   279       * @dev Tool to verify that a low level call to smart-contract was successful, and revert (either by bubbling
   280       * the revert reason or using the provided one) in case of unsuccessful call or if target was not a contract.
   281       *
   282       * _Available since v4.8._
   283       */
   284      function verifyCallResultFromTarget(
   285          address target,
   286          bool success,
   287          bytes memory returndata,
   288          string memory errorMessage
   289      ) internal view returns (bytes memory) {
   290          if (success) {
   291              if (returndata.length == 0) {
   292                  // only check isContract if the call was successful and the return data is empty
   293                  // otherwise we already know that it was a contract
   294                  require(isContract(target), "Address: call to non-contract");
   295              }
   296              return returndata;
   297          } else {
   298              _revert(returndata, errorMessage);
   299          }
   300      }
   301  
   302      /**
   303       * @dev Tool to verify that a low level call was successful, and revert if it wasn't, either by bubbling the
   304       * revert reason or using the provided one.
   305       *
   306       * _Available since v4.3._
   307       */
   308      function verifyCallResult(
   309          bool success,
   310          bytes memory returndata,
   311          string memory errorMessage
   312      ) internal pure returns (bytes memory) {
   313          if (success) {
   314              return returndata;
   315          } else {
   316              _revert(returndata, errorMessage);
   317          }
   318      }
   319  
   320      function _revert(bytes memory returndata, string memory errorMessage) private pure {
   321          // Look for revert reason and bubble it up if present
   322          if (returndata.length > 0) {
   323              // The easiest way to bubble the revert reason is using memory via assembly
   324              /// @solidity memory-safe-assembly
   325              assembly {
   326                  let returndata_size := mload(returndata)
   327                  revert(add(32, returndata), returndata_size)
   328              }
   329          } else {
   330              revert(errorMessage);
   331          }
   332      }
   333  }
   334  
   335  // File @openzeppelin/contracts/utils/StorageSlot.sol@v4.9.2
   336  
   337  // OpenZeppelin Contracts (last updated v4.9.0) (utils/StorageSlot.sol)
   338  // This file was procedurally generated from scripts/generate/templates/StorageSlot.js.
   339  
   340  pragma solidity ^0.8.0;
   341  
   342  /**
   343   * @dev Library for reading and writing primitive types to specific storage slots.
   344   *
   345   * Storage slots are often used to avoid storage conflict when dealing with upgradeable contracts.
   346   * This library helps with reading and writing to such slots without the need for inline assembly.
   347   *
   348   * The functions in this library return Slot structs that contain a `value` member that can be used to read or write.
   349   *
   350   * Example usage to set ERC1967 implementation slot:
   351   * ```solidity
   352   * contract ERC1967 {
   353   *     bytes32 internal constant _IMPLEMENTATION_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;
   354   *
   355   *     function _getImplementation() internal view returns (address) {
   356   *         return StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value;
   357   *     }
   358   *
   359   *     function _setImplementation(address newImplementation) internal {
   360   *         require(Address.isContract(newImplementation), "ERC1967: new implementation is not a contract");
   361   *         StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value = newImplementation;
   362   *     }
   363   * }
   364   * ```
   365   *
   366   * _Available since v4.1 for `address`, `bool`, `bytes32`, `uint256`._
   367   * _Available since v4.9 for `string`, `bytes`._
   368   */
   369  library StorageSlot {
   370      struct AddressSlot {
   371          address value;
   372      }
   373  
   374      struct BooleanSlot {
   375          bool value;
   376      }
   377  
   378      struct Bytes32Slot {
   379          bytes32 value;
   380      }
   381  
   382      struct Uint256Slot {
   383          uint256 value;
   384      }
   385  
   386      struct StringSlot {
   387          string value;
   388      }
   389  
   390      struct BytesSlot {
   391          bytes value;
   392      }
   393  
   394      /**
   395       * @dev Returns an `AddressSlot` with member `value` located at `slot`.
   396       */
   397      function getAddressSlot(bytes32 slot) internal pure returns (AddressSlot storage r) {
   398          /// @solidity memory-safe-assembly
   399          assembly {
   400              r.slot := slot
   401          }
   402      }
   403  
   404      /**
   405       * @dev Returns an `BooleanSlot` with member `value` located at `slot`.
   406       */
   407      function getBooleanSlot(bytes32 slot) internal pure returns (BooleanSlot storage r) {
   408          /// @solidity memory-safe-assembly
   409          assembly {
   410              r.slot := slot
   411          }
   412      }
   413  
   414      /**
   415       * @dev Returns an `Bytes32Slot` with member `value` located at `slot`.
   416       */
   417      function getBytes32Slot(bytes32 slot) internal pure returns (Bytes32Slot storage r) {
   418          /// @solidity memory-safe-assembly
   419          assembly {
   420              r.slot := slot
   421          }
   422      }
   423  
   424      /**
   425       * @dev Returns an `Uint256Slot` with member `value` located at `slot`.
   426       */
   427      function getUint256Slot(bytes32 slot) internal pure returns (Uint256Slot storage r) {
   428          /// @solidity memory-safe-assembly
   429          assembly {
   430              r.slot := slot
   431          }
   432      }
   433  
   434      /**
   435       * @dev Returns an `StringSlot` with member `value` located at `slot`.
   436       */
   437      function getStringSlot(bytes32 slot) internal pure returns (StringSlot storage r) {
   438          /// @solidity memory-safe-assembly
   439          assembly {
   440              r.slot := slot
   441          }
   442      }
   443  
   444      /**
   445       * @dev Returns an `StringSlot` representation of the string storage pointer `store`.
   446       */
   447      function getStringSlot(string storage store) internal pure returns (StringSlot storage r) {
   448          /// @solidity memory-safe-assembly
   449          assembly {
   450              r.slot := store.slot
   451          }
   452      }
   453  
   454      /**
   455       * @dev Returns an `BytesSlot` with member `value` located at `slot`.
   456       */
   457      function getBytesSlot(bytes32 slot) internal pure returns (BytesSlot storage r) {
   458          /// @solidity memory-safe-assembly
   459          assembly {
   460              r.slot := slot
   461          }
   462      }
   463  
   464      /**
   465       * @dev Returns an `BytesSlot` representation of the bytes storage pointer `store`.
   466       */
   467      function getBytesSlot(bytes storage store) internal pure returns (BytesSlot storage r) {
   468          /// @solidity memory-safe-assembly
   469          assembly {
   470              r.slot := store.slot
   471          }
   472      }
   473  }
   474  
   475  // File @openzeppelin/contracts/proxy/ERC1967/ERC1967Upgrade.sol@v4.9.2
   476  
   477  // OpenZeppelin Contracts (last updated v4.9.0) (proxy/ERC1967/ERC1967Upgrade.sol)
   478  
   479  pragma solidity ^0.8.2;
   480  
   481  /**
   482   * @dev This abstract contract provides getters and event emitting update functions for
   483   * https://eips.ethereum.org/EIPS/eip-1967[EIP1967] slots.
   484   *
   485   * _Available since v4.1._
   486   */
   487  abstract contract ERC1967Upgrade is IERC1967 {
   488      // This is the keccak-256 hash of "eip1967.proxy.rollback" subtracted by 1
   489      bytes32 private constant _ROLLBACK_SLOT = 0x4910fdfa16fed3260ed0e7147f7cc6da11a60208b5b9406d12a635614ffd9143;
   490  
   491      /**
   492       * @dev Storage slot with the address of the current implementation.
   493       * This is the keccak-256 hash of "eip1967.proxy.implementation" subtracted by 1, and is
   494       * validated in the constructor.
   495       */
   496      bytes32 internal constant _IMPLEMENTATION_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;
   497  
   498      /**
   499       * @dev Returns the current implementation address.
   500       */
   501      function _getImplementation() internal view returns (address) {
   502          return StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value;
   503      }
   504  
   505      /**
   506       * @dev Stores a new address in the EIP1967 implementation slot.
   507       */
   508      function _setImplementation(address newImplementation) private {
   509          require(Address.isContract(newImplementation), "ERC1967: new implementation is not a contract");
   510          StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value = newImplementation;
   511      }
   512  
   513      /**
   514       * @dev Perform implementation upgrade
   515       *
   516       * Emits an {Upgraded} event.
   517       */
   518      function _upgradeTo(address newImplementation) internal {
   519          _setImplementation(newImplementation);
   520          emit Upgraded(newImplementation);
   521      }
   522  
   523      /**
   524       * @dev Perform implementation upgrade with additional setup call.
   525       *
   526       * Emits an {Upgraded} event.
   527       */
   528      function _upgradeToAndCall(address newImplementation, bytes memory data, bool forceCall) internal {
   529          _upgradeTo(newImplementation);
   530          if (data.length > 0 || forceCall) {
   531              Address.functionDelegateCall(newImplementation, data);
   532          }
   533      }
   534  
   535      /**
   536       * @dev Perform implementation upgrade with security checks for UUPS proxies, and additional setup call.
   537       *
   538       * Emits an {Upgraded} event.
   539       */
   540      function _upgradeToAndCallUUPS(address newImplementation, bytes memory data, bool forceCall) internal {
   541          // Upgrades from old implementations will perform a rollback test. This test requires the new
   542          // implementation to upgrade back to the old, non-ERC1822 compliant, implementation. Removing
   543          // this special case will break upgrade paths from old UUPS implementation to new ones.
   544          if (StorageSlot.getBooleanSlot(_ROLLBACK_SLOT).value) {
   545              _setImplementation(newImplementation);
   546          } else {
   547              try IERC1822Proxiable(newImplementation).proxiableUUID() returns (bytes32 slot) {
   548                  require(slot == _IMPLEMENTATION_SLOT, "ERC1967Upgrade: unsupported proxiableUUID");
   549              } catch {
   550                  revert("ERC1967Upgrade: new implementation is not UUPS");
   551              }
   552              _upgradeToAndCall(newImplementation, data, forceCall);
   553          }
   554      }
   555  
   556      /**
   557       * @dev Storage slot with the admin of the contract.
   558       * This is the keccak-256 hash of "eip1967.proxy.admin" subtracted by 1, and is
   559       * validated in the constructor.
   560       */
   561      bytes32 internal constant _ADMIN_SLOT = 0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103;
   562  
   563      /**
   564       * @dev Returns the current admin.
   565       */
   566      function _getAdmin() internal view returns (address) {
   567          return StorageSlot.getAddressSlot(_ADMIN_SLOT).value;
   568      }
   569  
   570      /**
   571       * @dev Stores a new address in the EIP1967 admin slot.
   572       */
   573      function _setAdmin(address newAdmin) private {
   574          require(newAdmin != address(0), "ERC1967: new admin is the zero address");
   575          StorageSlot.getAddressSlot(_ADMIN_SLOT).value = newAdmin;
   576      }
   577  
   578      /**
   579       * @dev Changes the admin of the proxy.
   580       *
   581       * Emits an {AdminChanged} event.
   582       */
   583      function _changeAdmin(address newAdmin) internal {
   584          emit AdminChanged(_getAdmin(), newAdmin);
   585          _setAdmin(newAdmin);
   586      }
   587  
   588      /**
   589       * @dev The storage slot of the UpgradeableBeacon contract which defines the implementation for this proxy.
   590       * This is bytes32(uint256(keccak256('eip1967.proxy.beacon')) - 1)) and is validated in the constructor.
   591       */
   592      bytes32 internal constant _BEACON_SLOT = 0xa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b35133d50;
   593  
   594      /**
   595       * @dev Returns the current beacon.
   596       */
   597      function _getBeacon() internal view returns (address) {
   598          return StorageSlot.getAddressSlot(_BEACON_SLOT).value;
   599      }
   600  
   601      /**
   602       * @dev Stores a new beacon in the EIP1967 beacon slot.
   603       */
   604      function _setBeacon(address newBeacon) private {
   605          require(Address.isContract(newBeacon), "ERC1967: new beacon is not a contract");
   606          require(
   607              Address.isContract(IBeacon(newBeacon).implementation()),
   608              "ERC1967: beacon implementation is not a contract"
   609          );
   610          StorageSlot.getAddressSlot(_BEACON_SLOT).value = newBeacon;
   611      }
   612  
   613      /**
   614       * @dev Perform beacon upgrade with additional setup call. Note: This upgrades the address of the beacon, it does
   615       * not upgrade the implementation contained in the beacon (see {UpgradeableBeacon-_setImplementation} for that).
   616       *
   617       * Emits a {BeaconUpgraded} event.
   618       */
   619      function _upgradeBeaconToAndCall(address newBeacon, bytes memory data, bool forceCall) internal {
   620          _setBeacon(newBeacon);
   621          emit BeaconUpgraded(newBeacon);
   622          if (data.length > 0 || forceCall) {
   623              Address.functionDelegateCall(IBeacon(newBeacon).implementation(), data);
   624          }
   625      }
   626  }
   627  
   628  // File @openzeppelin/contracts/proxy/Proxy.sol@v4.9.2
   629  
   630  // OpenZeppelin Contracts (last updated v4.6.0) (proxy/Proxy.sol)
   631  
   632  pragma solidity ^0.8.0;
   633  
   634  /**
   635   * @dev This abstract contract provides a fallback function that delegates all calls to another contract using the EVM
   636   * instruction `delegatecall`. We refer to the second contract as the _implementation_ behind the proxy, and it has to
   637   * be specified by overriding the virtual {_implementation} function.
   638   *
   639   * Additionally, delegation to the implementation can be triggered manually through the {_fallback} function, or to a
   640   * different contract through the {_delegate} function.
   641   *
   642   * The success and return data of the delegated call will be returned back to the caller of the proxy.
   643   */
   644  abstract contract Proxy {
   645      /**
   646       * @dev Delegates the current call to `implementation`.
   647       *
   648       * This function does not return to its internal call site, it will return directly to the external caller.
   649       */
   650      function _delegate(address implementation) internal virtual {
   651          assembly {
   652              // Copy msg.data. We take full control of memory in this inline assembly
   653              // block because it will not return to Solidity code. We overwrite the
   654              // Solidity scratch pad at memory position 0.
   655              calldatacopy(0, 0, calldatasize())
   656  
   657              // Call the implementation.
   658              // out and outsize are 0 because we don't know the size yet.
   659              let result := delegatecall(gas(), implementation, 0, calldatasize(), 0, 0)
   660  
   661              // Copy the returned data.
   662              returndatacopy(0, 0, returndatasize())
   663  
   664              switch result
   665              // delegatecall returns 0 on error.
   666              case 0 {
   667                  revert(0, returndatasize())
   668              }
   669              default {
   670                  return(0, returndatasize())
   671              }
   672          }
   673      }
   674  
   675      /**
   676       * @dev This is a virtual function that should be overridden so it returns the address to which the fallback function
   677       * and {_fallback} should delegate.
   678       */
   679      function _implementation() internal view virtual returns (address);
   680  
   681      /**
   682       * @dev Delegates the current call to the address returned by `_implementation()`.
   683       *
   684       * This function does not return to its internal call site, it will return directly to the external caller.
   685       */
   686      function _fallback() internal virtual {
   687          _beforeFallback();
   688          _delegate(_implementation());
   689      }
   690  
   691      /**
   692       * @dev Fallback function that delegates calls to the address returned by `_implementation()`. Will run if no other
   693       * function in the contract matches the call data.
   694       */
   695      fallback() external payable virtual {
   696          _fallback();
   697      }
   698  
   699      /**
   700       * @dev Fallback function that delegates calls to the address returned by `_implementation()`. Will run if call data
   701       * is empty.
   702       */
   703      receive() external payable virtual {
   704          _fallback();
   705      }
   706  
   707      /**
   708       * @dev Hook that is called before falling back to the implementation. Can happen as part of a manual `_fallback`
   709       * call, or as part of the Solidity `fallback` or `receive` functions.
   710       *
   711       * If overridden should call `super._beforeFallback()`.
   712       */
   713      function _beforeFallback() internal virtual {}
   714  }
   715  
   716  // File @openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol@v4.9.2
   717  
   718  // OpenZeppelin Contracts (last updated v4.7.0) (proxy/ERC1967/ERC1967Proxy.sol)
   719  
   720  pragma solidity ^0.8.0;
   721  
   722  /**
   723   * @dev This contract implements an upgradeable proxy. It is upgradeable because calls are delegated to an
   724   * implementation address that can be changed. This address is stored in storage in the location specified by
   725   * https://eips.ethereum.org/EIPS/eip-1967[EIP1967], so that it doesn't conflict with the storage layout of the
   726   * implementation behind the proxy.
   727   */
   728  contract ERC1967Proxy is Proxy, ERC1967Upgrade {
   729      /**
   730       * @dev Initializes the upgradeable proxy with an initial implementation specified by `_logic`.
   731       *
   732       * If `_data` is nonempty, it's used as data in a delegate call to `_logic`. This will typically be an encoded
   733       * function call, and allows initializing the storage of the proxy like a Solidity constructor.
   734       */
   735      constructor(address _logic, bytes memory _data) payable {
   736          _upgradeToAndCall(_logic, _data, false);
   737      }
   738  
   739      /**
   740       * @dev Returns the current implementation address.
   741       */
   742      function _implementation() internal view virtual override returns (address impl) {
   743          return ERC1967Upgrade._getImplementation();
   744      }
   745  }