编译器
0.8.15+commit.e14f2714
文件 1 的 17:Address.sol
pragma solidity ^0.8.1;
library Address {
function isContract(address account) internal view returns (bool) {
return account.code.length > 0;
}
function sendValue(address payable recipient, uint256 amount) internal {
require(address(this).balance >= amount, "Address: insufficient balance");
(bool success, ) = recipient.call{value: amount}("");
require(success, "Address: unable to send value, recipient may have reverted");
}
function functionCall(address target, bytes memory data) internal returns (bytes memory) {
return functionCall(target, data, "Address: low-level call failed");
}
function functionCall(
address target,
bytes memory data,
string memory errorMessage
) internal returns (bytes memory) {
return functionCallWithValue(target, data, 0, errorMessage);
}
function functionCallWithValue(
address target,
bytes memory data,
uint256 value
) internal returns (bytes memory) {
return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
}
function functionCallWithValue(
address target,
bytes memory data,
uint256 value,
string memory errorMessage
) internal returns (bytes memory) {
require(address(this).balance >= value, "Address: insufficient balance for call");
require(isContract(target), "Address: call to non-contract");
(bool success, bytes memory returndata) = target.call{value: value}(data);
return verifyCallResult(success, returndata, errorMessage);
}
function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
return functionStaticCall(target, data, "Address: low-level static call failed");
}
function functionStaticCall(
address target,
bytes memory data,
string memory errorMessage
) internal view returns (bytes memory) {
require(isContract(target), "Address: static call to non-contract");
(bool success, bytes memory returndata) = target.staticcall(data);
return verifyCallResult(success, returndata, errorMessage);
}
function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
return functionDelegateCall(target, data, "Address: low-level delegate call failed");
}
function functionDelegateCall(
address target,
bytes memory data,
string memory errorMessage
) internal returns (bytes memory) {
require(isContract(target), "Address: delegate call to non-contract");
(bool success, bytes memory returndata) = target.delegatecall(data);
return verifyCallResult(success, returndata, errorMessage);
}
function verifyCallResult(
bool success,
bytes memory returndata,
string memory errorMessage
) internal pure returns (bytes memory) {
if (success) {
return returndata;
} else {
if (returndata.length > 0) {
assembly {
let returndata_size := mload(returndata)
revert(add(32, returndata), returndata_size)
}
} else {
revert(errorMessage);
}
}
}
}
文件 2 的 17:Context.sol
pragma solidity ^0.8.0;
abstract contract Context {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
return msg.data;
}
}
文件 3 的 17:Diamond.sol
pragma solidity ^0.8.15;
import "../access/ownable/OwnableStorage.sol";
import "../access/ownable/IERC173.sol";
import "../introspection/ERC165.sol";
import "../metatx/ERC2771Context.sol";
import "../diamond/IDiamondCut.sol";
import "../diamond/IDiamondLoupe.sol";
import "@openzeppelin/contracts/utils/Multicall.sol";
import "./DiamondStorage.sol";
contract Diamond is Multicall {
using ERC165Storage for ERC165Storage.Layout;
using OwnableStorage for OwnableStorage.Layout;
struct Initialization {
address initContract;
bytes initData;
}
struct CoreFacets {
address diamondCutFacet;
address diamondLoupeFacet;
address erc165Facet;
address erc173Facet;
}
constructor(
address owner,
CoreFacets memory _coreFacets,
IDiamondCut.FacetCut[] memory _facets,
Initialization[] memory _initializations
) {
ERC165Storage.Layout storage erc165 = ERC165Storage.layout();
bytes4[] memory selectorsDiamondCut = new bytes4[](1);
selectorsDiamondCut[0] = IDiamondCut.diamondCut.selector;
erc165.setSupportedInterface(type(IDiamondCut).interfaceId, true);
bytes4[] memory selectorsDiamondLoupe = new bytes4[](4);
selectorsDiamondLoupe[0] = IDiamondLoupe.facets.selector;
selectorsDiamondLoupe[1] = IDiamondLoupe.facetFunctionSelectors.selector;
selectorsDiamondLoupe[2] = IDiamondLoupe.facetAddresses.selector;
selectorsDiamondLoupe[3] = IDiamondLoupe.facetAddress.selector;
erc165.setSupportedInterface(type(IDiamondLoupe).interfaceId, true);
bytes4[] memory selectorsERC165 = new bytes4[](1);
selectorsERC165[0] = IERC165.supportsInterface.selector;
erc165.setSupportedInterface(type(IERC165).interfaceId, true);
bytes4[] memory selectorsERC173 = new bytes4[](2);
selectorsERC173[0] = IERC173.owner.selector;
selectorsERC173[1] = IERC173.transferOwnership.selector;
erc165.setSupportedInterface(type(IERC173).interfaceId, true);
DiamondStorage.addFunctions(_coreFacets.diamondCutFacet, selectorsDiamondCut);
DiamondStorage.addFunctions(_coreFacets.diamondLoupeFacet, selectorsDiamondLoupe);
DiamondStorage.addFunctions(_coreFacets.erc165Facet, selectorsERC165);
DiamondStorage.addFunctions(_coreFacets.erc173Facet, selectorsERC173);
OwnableStorage.layout().setOwner(owner);
for (uint256 i = 0; i < _facets.length; i++) {
DiamondStorage.addFunctions(_facets[i].facetAddress, _facets[i].functionSelectors);
}
for (uint256 i = 0; i < _initializations.length; i++) {
DiamondStorage.initializeDiamondCut(_initializations[i].initContract, _initializations[i].initData);
}
}
fallback() external payable {
DiamondStorage.Layout storage l;
bytes32 position = DiamondStorage.DIAMOND_STORAGE_POSITION;
assembly {
l.slot := position
}
address facet = l.selectorToFacetAndPosition[msg.sig].facetAddress;
require(facet != address(0), "BAD_FUNC");
assembly {
calldatacopy(0, 0, calldatasize())
let result := delegatecall(gas(), facet, 0, calldatasize(), 0, 0)
returndatacopy(0, 0, returndatasize())
switch result
case 0 {
revert(0, returndatasize())
}
default {
return(0, returndatasize())
}
}
}
receive() external payable {}
}
文件 4 的 17:DiamondStorage.sol
pragma solidity ^0.8.15;
import "../diamond/IDiamondCut.sol";
error ErrDiamondFacetAlreadyExists(address facet, bytes4 selector);
error ErrDiamondFacetSameFunction(address facet, bytes4 selector);
library DiamondStorage {
bytes32 constant DIAMOND_STORAGE_POSITION = keccak256("diamond.standard.diamond.storage");
struct FacetAddressAndPosition {
address facetAddress;
uint96 functionSelectorPosition;
}
struct FacetFunctionSelectors {
bytes4[] functionSelectors;
uint256 facetAddressPosition;
}
struct Layout {
mapping(bytes4 => FacetAddressAndPosition) selectorToFacetAndPosition;
mapping(address => FacetFunctionSelectors) facetFunctionSelectors;
address[] facetAddresses;
}
function layout() internal pure returns (Layout storage l) {
bytes32 position = DIAMOND_STORAGE_POSITION;
assembly {
l.slot := position
}
}
event DiamondCut(IDiamondCut.FacetCut[] _diamondCut, address _init, bytes _calldata);
function diamondCut(
IDiamondCut.FacetCut[] memory _diamondCut,
address _init,
bytes memory _calldata
) internal {
for (uint256 facetIndex; facetIndex < _diamondCut.length; facetIndex++) {
IDiamondCut.FacetCutAction action = _diamondCut[facetIndex].action;
if (action == IDiamondCut.FacetCutAction.Add) {
addFunctions(_diamondCut[facetIndex].facetAddress, _diamondCut[facetIndex].functionSelectors);
} else if (action == IDiamondCut.FacetCutAction.Replace) {
replaceFunctions(_diamondCut[facetIndex].facetAddress, _diamondCut[facetIndex].functionSelectors);
} else if (action == IDiamondCut.FacetCutAction.Remove) {
removeFunctions(_diamondCut[facetIndex].facetAddress, _diamondCut[facetIndex].functionSelectors);
} else {
revert("LibDiamondCut: Incorrect FacetCutAction");
}
}
emit DiamondCut(_diamondCut, _init, _calldata);
initializeDiamondCut(_init, _calldata);
}
function addFunctions(address _facetAddress, bytes4[] memory _functionSelectors) internal {
Layout storage l = layout();
uint96 selectorPosition = uint96(l.facetFunctionSelectors[_facetAddress].functionSelectors.length);
if (selectorPosition == 0) {
addFacet(l, _facetAddress);
}
for (uint256 selectorIndex; selectorIndex < _functionSelectors.length; selectorIndex++) {
bytes4 selector = _functionSelectors[selectorIndex];
address oldFacetAddress = l.selectorToFacetAndPosition[selector].facetAddress;
if (oldFacetAddress != address(0)) {
revert ErrDiamondFacetAlreadyExists(oldFacetAddress, selector);
}
addFunction(l, selector, selectorPosition, _facetAddress);
selectorPosition++;
}
}
function replaceFunctions(address _facetAddress, bytes4[] memory _functionSelectors) internal {
Layout storage l = layout();
uint96 selectorPosition = uint96(l.facetFunctionSelectors[_facetAddress].functionSelectors.length);
if (selectorPosition == 0) {
addFacet(l, _facetAddress);
}
for (uint256 selectorIndex; selectorIndex < _functionSelectors.length; selectorIndex++) {
bytes4 selector = _functionSelectors[selectorIndex];
address oldFacetAddress = l.selectorToFacetAndPosition[selector].facetAddress;
if (oldFacetAddress == _facetAddress) {
revert ErrDiamondFacetSameFunction(oldFacetAddress, selector);
}
removeFunction(l, oldFacetAddress, selector);
addFunction(l, selector, selectorPosition, _facetAddress);
selectorPosition++;
}
}
function removeFunctions(address, bytes4[] memory _functionSelectors) internal {
Layout storage l = layout();
for (uint256 selectorIndex; selectorIndex < _functionSelectors.length; selectorIndex++) {
bytes4 selector = _functionSelectors[selectorIndex];
address oldFacetAddress = l.selectorToFacetAndPosition[selector].facetAddress;
removeFunction(l, oldFacetAddress, selector);
}
}
function addFacet(Layout storage l, address _facetAddress) internal {
enforceHasContractCode(_facetAddress, "LibDiamondCut: New facet has no code");
l.facetFunctionSelectors[_facetAddress].facetAddressPosition = l.facetAddresses.length;
l.facetAddresses.push(_facetAddress);
}
function addFunction(
Layout storage l,
bytes4 _selector,
uint96 _selectorPosition,
address _facetAddress
) internal {
l.selectorToFacetAndPosition[_selector].functionSelectorPosition = _selectorPosition;
l.facetFunctionSelectors[_facetAddress].functionSelectors.push(_selector);
l.selectorToFacetAndPosition[_selector].facetAddress = _facetAddress;
}
function removeFunction(
Layout storage l,
address _facetAddress,
bytes4 _selector
) internal {
uint256 selectorPosition = l.selectorToFacetAndPosition[_selector].functionSelectorPosition;
uint256 lastSelectorPosition = l.facetFunctionSelectors[_facetAddress].functionSelectors.length - 1;
if (selectorPosition != lastSelectorPosition) {
bytes4 lastSelector = l.facetFunctionSelectors[_facetAddress].functionSelectors[lastSelectorPosition];
l.facetFunctionSelectors[_facetAddress].functionSelectors[selectorPosition] = lastSelector;
l.selectorToFacetAndPosition[lastSelector].functionSelectorPosition = uint96(selectorPosition);
}
l.facetFunctionSelectors[_facetAddress].functionSelectors.pop();
delete l.selectorToFacetAndPosition[_selector];
if (lastSelectorPosition == 0) {
uint256 lastFacetAddressPosition = l.facetAddresses.length - 1;
uint256 facetAddressPosition = l.facetFunctionSelectors[_facetAddress].facetAddressPosition;
if (facetAddressPosition != lastFacetAddressPosition) {
address lastFacetAddress = l.facetAddresses[lastFacetAddressPosition];
l.facetAddresses[facetAddressPosition] = lastFacetAddress;
l.facetFunctionSelectors[lastFacetAddress].facetAddressPosition = facetAddressPosition;
}
l.facetAddresses.pop();
delete l.facetFunctionSelectors[_facetAddress].facetAddressPosition;
}
}
function initializeDiamondCut(address _init, bytes memory _calldata) internal {
if (_init == address(0)) {
require(_calldata.length == 0, "LibDiamondCut: _init is address(0) but_calldata is not empty");
} else {
require(_calldata.length > 0, "LibDiamondCut: _calldata is empty but _init is not address(0)");
if (_init != address(this)) {
enforceHasContractCode(_init, "LibDiamondCut: _init address has no code");
}
(bool success, bytes memory error) = _init.delegatecall(_calldata);
if (!success) {
if (error.length > 0) {
revert(string(error));
} else {
revert("LibDiamondCut: _init function reverted");
}
}
}
}
function enforceHasContractCode(address _contract, string memory _errorMessage) internal view {
uint256 contractSize;
assembly {
contractSize := extcodesize(_contract)
}
require(contractSize > 0, _errorMessage);
}
}
文件 5 的 17:ERC165.sol
pragma solidity ^0.8.15;
import "@openzeppelin/contracts/utils/introspection/IERC165.sol";
import "./ERC165Storage.sol";
contract ERC165 is IERC165 {
using ERC165Storage for ERC165Storage.Layout;
function supportsInterface(bytes4 interfaceId) public view returns (bool) {
return ERC165Storage.layout().isSupportedInterface(interfaceId);
}
}
文件 6 的 17:ERC165Storage.sol
pragma solidity ^0.8.15;
library ERC165Storage {
struct Layout {
mapping(bytes4 => bool) supportedInterfaces;
}
bytes32 internal constant STORAGE_SLOT = keccak256("openzeppelin.contracts.storage.ERC165");
function layout() internal pure returns (Layout storage l) {
bytes32 slot = STORAGE_SLOT;
assembly {
l.slot := slot
}
}
function isSupportedInterface(Layout storage l, bytes4 interfaceId) internal view returns (bool) {
return l.supportedInterfaces[interfaceId];
}
function setSupportedInterface(
Layout storage l,
bytes4 interfaceId,
bool status
) internal {
require(interfaceId != 0xffffffff, "ERC165: invalid interface id");
l.supportedInterfaces[interfaceId] = status;
}
}
文件 7 的 17:ERC2771Context.sol
pragma solidity ^0.8.15;
import "./ERC2771ContextStorage.sol";
import "./ERC2771ContextInternal.sol";
import "./IERC2771Context.sol";
contract ERC2771Context is IERC2771Context, ERC2771ContextInternal {
using ERC2771ContextStorage for ERC2771ContextStorage.Layout;
function trustedForwarder() external view override returns (address) {
return ERC2771ContextStorage.layout().trustedForwarder;
}
function isTrustedForwarder(address forwarder) public view virtual returns (bool) {
return _isTrustedForwarder(forwarder);
}
}
文件 8 的 17:ERC2771ContextInternal.sol
pragma solidity ^0.8.15;
import "@openzeppelin/contracts/utils/Context.sol";
import "./ERC2771ContextStorage.sol";
abstract contract ERC2771ContextInternal is Context {
function _isTrustedForwarder(address operator) internal view returns (bool) {
return ERC2771ContextStorage.layout().trustedForwarder == operator;
}
function _msgSender() internal view virtual override returns (address sender) {
if (_isTrustedForwarder(msg.sender)) {
assembly {
sender := shr(96, calldataload(sub(calldatasize(), 20)))
}
} else {
return super._msgSender();
}
}
function _msgData() internal view virtual override returns (bytes calldata) {
if (_isTrustedForwarder(msg.sender)) {
return msg.data[:msg.data.length - 20];
} else {
return super._msgData();
}
}
}
文件 9 的 17:ERC2771ContextStorage.sol
pragma solidity ^0.8.15;
library ERC2771ContextStorage {
struct Layout {
address trustedForwarder;
}
bytes32 internal constant STORAGE_SLOT = keccak256("openzeppelin.contracts.storage.ERC2771Context");
function layout() internal pure returns (Layout storage l) {
bytes32 slot = STORAGE_SLOT;
assembly {
l.slot := slot
}
}
}
文件 10 的 17:IDiamondCut.sol
pragma solidity ^0.8.15;
interface IDiamondCut {
enum FacetCutAction {
Add,
Replace,
Remove
}
struct FacetCut {
address facetAddress;
FacetCutAction action;
bytes4[] functionSelectors;
}
function diamondCut(
FacetCut[] calldata _diamondCut,
address _init,
bytes calldata _calldata
) external;
event DiamondCut(FacetCut[] _diamondCut, address _init, bytes _calldata);
}
文件 11 的 17:IDiamondLoupe.sol
pragma solidity ^0.8.15;
interface IDiamondLoupe {
struct Facet {
address facetAddress;
bytes4[] functionSelectors;
}
function facets() external view returns (Facet[] memory facets_);
function facetFunctionSelectors(address _facet) external view returns (bytes4[] memory facetFunctionSelectors_);
function facetAddresses() external view returns (address[] memory facetAddresses_);
function facetAddress(bytes4 _functionSelector) external view returns (address facetAddress_);
}
文件 12 的 17:IERC165.sol
pragma solidity ^0.8.0;
interface IERC165 {
function supportsInterface(bytes4 interfaceId) external view returns (bool);
}
文件 13 的 17:IERC173.sol
pragma solidity ^0.8.15;
import "./IERC173Events.sol";
interface IERC173 is IERC173Events {
function owner() external view returns (address);
function transferOwnership(address account) external;
}
文件 14 的 17:IERC173Events.sol
pragma solidity ^0.8.15;
interface IERC173Events {
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
}
文件 15 的 17:IERC2771Context.sol
pragma solidity ^0.8.15;
interface IERC2771Context {
function trustedForwarder() external view returns (address);
function isTrustedForwarder(address forwarder) external view returns (bool);
}
文件 16 的 17:Multicall.sol
pragma solidity ^0.8.0;
import "./Address.sol";
abstract contract Multicall {
function multicall(bytes[] calldata data) external virtual returns (bytes[] memory results) {
results = new bytes[](data.length);
for (uint256 i = 0; i < data.length; i++) {
results[i] = Address.functionDelegateCall(address(this), data[i]);
}
return results;
}
}
文件 17 的 17:OwnableStorage.sol
pragma solidity ^0.8.15;
library OwnableStorage {
struct Layout {
address owner;
}
bytes32 internal constant STORAGE_SLOT = keccak256("openzeppelin.contracts.storage.Ownable");
function layout() internal pure returns (Layout storage l) {
bytes32 slot = STORAGE_SLOT;
assembly {
l.slot := slot
}
}
function setOwner(Layout storage l, address owner) internal {
l.owner = owner;
}
}
{
"compilationTarget": {
"src/diamond/Diamond.sol": "Diamond"
},
"evmVersion": "london",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs",
"useLiteralContent": true
},
"optimizer": {
"enabled": true,
"runs": 1337
},
"remappings": []
}
[{"inputs":[{"internalType":"address","name":"owner","type":"address"},{"components":[{"internalType":"address","name":"diamondCutFacet","type":"address"},{"internalType":"address","name":"diamondLoupeFacet","type":"address"},{"internalType":"address","name":"erc165Facet","type":"address"},{"internalType":"address","name":"erc173Facet","type":"address"}],"internalType":"struct Diamond.CoreFacets","name":"_coreFacets","type":"tuple"},{"components":[{"internalType":"address","name":"facetAddress","type":"address"},{"internalType":"enum IDiamondCut.FacetCutAction","name":"action","type":"uint8"},{"internalType":"bytes4[]","name":"functionSelectors","type":"bytes4[]"}],"internalType":"struct IDiamondCut.FacetCut[]","name":"_facets","type":"tuple[]"},{"components":[{"internalType":"address","name":"initContract","type":"address"},{"internalType":"bytes","name":"initData","type":"bytes"}],"internalType":"struct Diamond.Initialization[]","name":"_initializations","type":"tuple[]"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[{"internalType":"address","name":"facet","type":"address"},{"internalType":"bytes4","name":"selector","type":"bytes4"}],"name":"ErrDiamondFacetAlreadyExists","type":"error"},{"stateMutability":"payable","type":"fallback"},{"inputs":[{"internalType":"bytes[]","name":"data","type":"bytes[]"}],"name":"multicall","outputs":[{"internalType":"bytes[]","name":"results","type":"bytes[]"}],"stateMutability":"nonpayable","type":"function"},{"stateMutability":"payable","type":"receive"}]