编译器
0.8.19+commit.7dd6d404
文件 1 的 6:BaseStorage.sol
pragma solidity ^0.8.19;
import {INiftyKitAppRegistry} from "../interfaces/INiftyKitAppRegistry.sol";
import {INiftyKitV3} from "../interfaces/INiftyKitV3.sol";
library BaseStorage {
enum Transfer {
AllowAll,
AllowedOperatorsOnly,
BlockAll
}
struct URIEntry {
bool isValue;
string tokenURI;
}
bytes32 private constant STORAGE_SLOT = keccak256("niftykit.base.storage");
uint256 public constant ADMIN_ROLE = 1 << 0;
uint256 public constant MANAGER_ROLE = 1 << 1;
uint256 public constant API_ROLE = 1 << 2;
struct Layout {
mapping(bytes32 => INiftyKitAppRegistry.App) _apps;
mapping(address => bool) _allowedOperators;
mapping(uint256 => bool) _blockedTokenIds;
mapping(uint256 => URIEntry) _tokenURIs;
bool _operatorFilteringEnabled;
Transfer _transferStatus;
INiftyKitV3 _niftyKit;
uint8 _baseVersion;
address _treasury;
string _baseURI;
address _mintSigner;
address _trustedForwarder;
}
function layout() internal pure returns (Layout storage ds) {
bytes32 position = STORAGE_SLOT;
assembly {
ds.slot := position
}
}
}
文件 2 的 6:DiamondCollection.sol
pragma solidity ^0.8.19;
import {INiftyKitAppRegistry} from "../interfaces/INiftyKitAppRegistry.sol";
import {INiftyKitV3} from "../interfaces/INiftyKitV3.sol";
import {IDiamondCut} from "../interfaces/IDiamondCut.sol";
import {LibDiamond} from "../libraries/LibDiamond.sol";
import {BaseStorage} from "./BaseStorage.sol";
contract DiamondCollection {
constructor(INiftyKitV3.DiamondArgs memory args) {
BaseStorage.Layout storage layout = BaseStorage.layout();
layout._niftyKit = INiftyKitV3(msg.sender);
INiftyKitAppRegistry registry = INiftyKitAppRegistry(
layout._niftyKit.appRegistry()
);
INiftyKitAppRegistry.Base memory base = registry.getBase();
IDiamondCut.FacetCut[] memory facetCuts = new IDiamondCut.FacetCut[](
args.apps.length + 1
);
layout._treasury = args.treasury;
layout._baseVersion = base.version;
layout._baseURI = args.baseURI;
layout._trustedForwarder = args.trustedForwarder;
facetCuts = _appFacets(facetCuts, layout, registry, args.apps);
facetCuts = _baseFacet(facetCuts, base);
LibDiamond.diamondCut(
facetCuts,
base.implementation,
abi.encodeWithSignature(
"_initialize(address,address,string,string,address,uint16)",
args.owner,
args.admin,
args.name,
args.symbol,
args.royalty,
args.royaltyBps
)
);
}
function _appFacets(
IDiamondCut.FacetCut[] memory facetCuts,
BaseStorage.Layout storage layout,
INiftyKitAppRegistry registry,
bytes32[] memory apps
) internal returns (IDiamondCut.FacetCut[] memory) {
LibDiamond.DiamondStorage storage ds = LibDiamond.diamondStorage();
uint256 appsLength = apps.length;
for (uint256 i = 0; i < appsLength; ) {
INiftyKitAppRegistry.App memory app = registry.getApp(apps[i]);
if (app.version == 0) revert("App does not exist");
facetCuts[i] = IDiamondCut.FacetCut({
facetAddress: app.implementation,
action: IDiamondCut.FacetCutAction.Add,
functionSelectors: app.selectors
});
ds.supportedInterfaces[app.interfaceId] = true;
layout._apps[apps[i]] = app;
unchecked {
i++;
}
}
return facetCuts;
}
function _baseFacet(
IDiamondCut.FacetCut[] memory facetCuts,
INiftyKitAppRegistry.Base memory base
) internal returns (IDiamondCut.FacetCut[] memory) {
LibDiamond.DiamondStorage storage ds = LibDiamond.diamondStorage();
facetCuts[facetCuts.length - 1] = IDiamondCut.FacetCut({
facetAddress: base.implementation,
action: IDiamondCut.FacetCutAction.Add,
functionSelectors: base.selectors
});
uint256 idsLength = base.interfaceIds.length;
for (uint256 i = 0; i < idsLength; ) {
ds.supportedInterfaces[base.interfaceIds[i]] = true;
unchecked {
i++;
}
}
return facetCuts;
}
fallback() external payable {
LibDiamond.DiamondStorage storage ds;
bytes32 position = LibDiamond.DIAMOND_STORAGE_POSITION;
assembly {
ds.slot := position
}
address facet = address(bytes20(ds.facets[msg.sig]));
require(facet != address(0), "Diamond: Function does not exist");
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 {}
}
文件 3 的 6:IDiamondCut.sol
pragma solidity ^0.8.0;
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);
}
文件 4 的 6:INiftyKitAppRegistry.sol
pragma solidity ^0.8.19;
interface INiftyKitAppRegistry {
struct App {
address implementation;
bytes4 interfaceId;
bytes4[] selectors;
uint8 version;
}
struct Base {
address implementation;
bytes4[] interfaceIds;
bytes4[] selectors;
uint8 version;
}
function getApp(bytes32 name) external view returns (App memory);
function getBase() external view returns (Base memory);
}
文件 5 的 6:INiftyKitV3.sol
pragma solidity ^0.8.19;
interface INiftyKitV3 {
struct DiamondArgs {
address owner;
address admin;
address treasury;
address royalty;
address trustedForwarder;
uint16 royaltyBps;
string name;
string symbol;
string baseURI;
bytes32[] apps;
}
function appRegistry() external returns (address);
function commission(
address collection,
uint256 amount
) external view returns (uint256, uint256);
function getFees(uint256 amount) external view returns (uint256, uint256);
}
文件 6 的 6:LibDiamond.sol
pragma solidity ^0.8.0;
import { IDiamondCut } from "../interfaces/IDiamondCut.sol";
error InitializationFunctionReverted(address _initializationContractAddress, bytes _calldata);
library LibDiamond {
bytes32 constant DIAMOND_STORAGE_POSITION = keccak256("diamond.standard.diamond.storage");
struct DiamondStorage {
mapping(bytes4 => bytes32) facets;
mapping(uint256 => bytes32) selectorSlots;
uint16 selectorCount;
mapping(bytes4 => bool) supportedInterfaces;
address contractOwner;
}
function diamondStorage() internal pure returns (DiamondStorage storage ds) {
bytes32 position = DIAMOND_STORAGE_POSITION;
assembly {
ds.slot := position
}
}
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
function setContractOwner(address _newOwner) internal {
DiamondStorage storage ds = diamondStorage();
address previousOwner = ds.contractOwner;
ds.contractOwner = _newOwner;
emit OwnershipTransferred(previousOwner, _newOwner);
}
function contractOwner() internal view returns (address contractOwner_) {
contractOwner_ = diamondStorage().contractOwner;
}
function enforceIsContractOwner() internal view {
require(msg.sender == diamondStorage().contractOwner, "LibDiamond: Must be contract owner");
}
event DiamondCut(IDiamondCut.FacetCut[] _diamondCut, address _init, bytes _calldata);
bytes32 constant CLEAR_ADDRESS_MASK = bytes32(uint256(0xffffffffffffffffffffffff));
bytes32 constant CLEAR_SELECTOR_MASK = bytes32(uint256(0xffffffff << 224));
function diamondCut(
IDiamondCut.FacetCut[] memory _diamondCut,
address _init,
bytes memory _calldata
) internal {
DiamondStorage storage ds = diamondStorage();
uint256 originalSelectorCount = ds.selectorCount;
uint256 selectorCount = originalSelectorCount;
bytes32 selectorSlot;
if (selectorCount & 7 > 0) {
selectorSlot = ds.selectorSlots[selectorCount >> 3];
}
for (uint256 facetIndex; facetIndex < _diamondCut.length; ) {
(selectorCount, selectorSlot) = addReplaceRemoveFacetSelectors(
selectorCount,
selectorSlot,
_diamondCut[facetIndex].facetAddress,
_diamondCut[facetIndex].action,
_diamondCut[facetIndex].functionSelectors
);
unchecked {
facetIndex++;
}
}
if (selectorCount != originalSelectorCount) {
ds.selectorCount = uint16(selectorCount);
}
if (selectorCount & 7 > 0) {
ds.selectorSlots[selectorCount >> 3] = selectorSlot;
}
emit DiamondCut(_diamondCut, _init, _calldata);
initializeDiamondCut(_init, _calldata);
}
function addReplaceRemoveFacetSelectors(
uint256 _selectorCount,
bytes32 _selectorSlot,
address _newFacetAddress,
IDiamondCut.FacetCutAction _action,
bytes4[] memory _selectors
) internal returns (uint256, bytes32) {
DiamondStorage storage ds = diamondStorage();
require(_selectors.length > 0, "LibDiamondCut: No selectors in facet to cut");
if (_action == IDiamondCut.FacetCutAction.Add) {
enforceHasContractCode(_newFacetAddress, "LibDiamondCut: Add facet has no code");
for (uint256 selectorIndex; selectorIndex < _selectors.length; ) {
bytes4 selector = _selectors[selectorIndex];
bytes32 oldFacet = ds.facets[selector];
require(address(bytes20(oldFacet)) == address(0), "LibDiamondCut: Can't add function that already exists");
ds.facets[selector] = bytes20(_newFacetAddress) | bytes32(_selectorCount);
uint256 selectorInSlotPosition = (_selectorCount & 7) << 5;
_selectorSlot = (_selectorSlot & ~(CLEAR_SELECTOR_MASK >> selectorInSlotPosition)) | (bytes32(selector) >> selectorInSlotPosition);
if (selectorInSlotPosition == 224) {
ds.selectorSlots[_selectorCount >> 3] = _selectorSlot;
_selectorSlot = 0;
}
_selectorCount++;
unchecked {
selectorIndex++;
}
}
} else if (_action == IDiamondCut.FacetCutAction.Replace) {
enforceHasContractCode(_newFacetAddress, "LibDiamondCut: Replace facet has no code");
for (uint256 selectorIndex; selectorIndex < _selectors.length; ) {
bytes4 selector = _selectors[selectorIndex];
bytes32 oldFacet = ds.facets[selector];
address oldFacetAddress = address(bytes20(oldFacet));
require(oldFacetAddress != address(this), "LibDiamondCut: Can't replace immutable function");
require(oldFacetAddress != _newFacetAddress, "LibDiamondCut: Can't replace function with same function");
require(oldFacetAddress != address(0), "LibDiamondCut: Can't replace function that doesn't exist");
ds.facets[selector] = (oldFacet & CLEAR_ADDRESS_MASK) | bytes20(_newFacetAddress);
unchecked {
selectorIndex++;
}
}
} else if (_action == IDiamondCut.FacetCutAction.Remove) {
require(_newFacetAddress == address(0), "LibDiamondCut: Remove facet address must be address(0)");
uint256 selectorSlotCount = _selectorCount >> 3;
uint256 selectorInSlotIndex = _selectorCount & 7;
for (uint256 selectorIndex; selectorIndex < _selectors.length; ) {
if (_selectorSlot == 0) {
selectorSlotCount--;
_selectorSlot = ds.selectorSlots[selectorSlotCount];
selectorInSlotIndex = 7;
} else {
selectorInSlotIndex--;
}
bytes4 lastSelector;
uint256 oldSelectorsSlotCount;
uint256 oldSelectorInSlotPosition;
{
bytes4 selector = _selectors[selectorIndex];
bytes32 oldFacet = ds.facets[selector];
require(address(bytes20(oldFacet)) != address(0), "LibDiamondCut: Can't remove function that doesn't exist");
require(address(bytes20(oldFacet)) != address(this), "LibDiamondCut: Can't remove immutable function");
lastSelector = bytes4(_selectorSlot << (selectorInSlotIndex << 5));
if (lastSelector != selector) {
ds.facets[lastSelector] = (oldFacet & CLEAR_ADDRESS_MASK) | bytes20(ds.facets[lastSelector]);
}
delete ds.facets[selector];
uint256 oldSelectorCount = uint16(uint256(oldFacet));
oldSelectorsSlotCount = oldSelectorCount >> 3;
oldSelectorInSlotPosition = (oldSelectorCount & 7) << 5;
}
if (oldSelectorsSlotCount != selectorSlotCount) {
bytes32 oldSelectorSlot = ds.selectorSlots[oldSelectorsSlotCount];
oldSelectorSlot =
(oldSelectorSlot & ~(CLEAR_SELECTOR_MASK >> oldSelectorInSlotPosition)) |
(bytes32(lastSelector) >> oldSelectorInSlotPosition);
ds.selectorSlots[oldSelectorsSlotCount] = oldSelectorSlot;
} else {
_selectorSlot =
(_selectorSlot & ~(CLEAR_SELECTOR_MASK >> oldSelectorInSlotPosition)) |
(bytes32(lastSelector) >> oldSelectorInSlotPosition);
}
if (selectorInSlotIndex == 0) {
delete ds.selectorSlots[selectorSlotCount];
_selectorSlot = 0;
}
unchecked {
selectorIndex++;
}
}
_selectorCount = selectorSlotCount * 8 + selectorInSlotIndex;
} else {
revert("LibDiamondCut: Incorrect FacetCutAction");
}
return (_selectorCount, _selectorSlot);
}
function initializeDiamondCut(address _init, bytes memory _calldata) internal {
if (_init == address(0)) {
return;
}
enforceHasContractCode(_init, "LibDiamondCut: _init address has no code");
(bool success, bytes memory error) = _init.delegatecall(_calldata);
if (!success) {
if (error.length > 0) {
assembly {
let returndata_size := mload(error)
revert(add(32, error), returndata_size)
}
} else {
revert InitializationFunctionReverted(_init, _calldata);
}
}
}
function enforceHasContractCode(address _contract, string memory _errorMessage) internal view {
uint256 contractSize;
assembly {
contractSize := extcodesize(_contract)
}
require(contractSize > 0, _errorMessage);
}
}
{
"compilationTarget": {
"contracts/diamond/DiamondCollection.sol": "DiamondCollection"
},
"evmVersion": "paris",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs",
"useLiteralContent": true
},
"optimizer": {
"enabled": true,
"runs": 200
},
"remappings": []
}
[{"inputs":[{"components":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"address","name":"admin","type":"address"},{"internalType":"address","name":"treasury","type":"address"},{"internalType":"address","name":"royalty","type":"address"},{"internalType":"address","name":"trustedForwarder","type":"address"},{"internalType":"uint16","name":"royaltyBps","type":"uint16"},{"internalType":"string","name":"name","type":"string"},{"internalType":"string","name":"symbol","type":"string"},{"internalType":"string","name":"baseURI","type":"string"},{"internalType":"bytes32[]","name":"apps","type":"bytes32[]"}],"internalType":"struct INiftyKitV3.DiamondArgs","name":"args","type":"tuple"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[{"internalType":"address","name":"_initializationContractAddress","type":"address"},{"internalType":"bytes","name":"_calldata","type":"bytes"}],"name":"InitializationFunctionReverted","type":"error"},{"stateMutability":"payable","type":"fallback"},{"stateMutability":"payable","type":"receive"}]