// SPDX-License-Identifier: MIT// OpenZeppelin Contracts v4.3.2 (utils/Address.sol)pragmasolidity ^0.8.0;/**
* @dev Collection of functions related to the address type
*/libraryAddress{
/**
* @dev Returns true if `account` is a contract.
*
* [IMPORTANT]
* ====
* It is unsafe to assume that an address for which this function returns
* false is an externally-owned account (EOA) and not a contract.
*
* Among others, `isContract` will return false for the following
* types of addresses:
*
* - an externally-owned account
* - a contract in construction
* - an address where a contract will be created
* - an address where a contract lived, but was destroyed
* ====
*/functionisContract(address account) internalviewreturns (bool) {
// This method relies on extcodesize, which returns 0 for contracts in// construction, since the code is only stored at the end of the// constructor execution.uint256 size;
assembly {
size :=extcodesize(account)
}
return size >0;
}
/**
* @dev Replacement for Solidity's `transfer`: sends `amount` wei to
* `recipient`, forwarding all available gas and reverting on errors.
*
* https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
* of certain opcodes, possibly making contracts go over the 2300 gas limit
* imposed by `transfer`, making them unable to receive funds via
* `transfer`. {sendValue} removes this limitation.
*
* https://diligence.consensys.net/posts/2019/09/stop-using-soliditys-transfer-now/[Learn more].
*
* IMPORTANT: because control is transferred to `recipient`, care must be
* taken to not create reentrancy vulnerabilities. Consider using
* {ReentrancyGuard} or the
* https://solidity.readthedocs.io/en/v0.5.11/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
*/functionsendValue(addresspayable 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"
);
}
/**
* @dev Performs a Solidity function call using a low level `call`. A
* plain `call` is an unsafe replacement for a function call: use this
* function instead.
*
* If `target` reverts with a revert reason, it is bubbled up by this
* function (like regular Solidity function calls).
*
* Returns the raw returned data. To convert to the expected return value,
* use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
*
* Requirements:
*
* - `target` must be a contract.
* - calling `target` with `data` must not revert.
*
* _Available since v3.1._
*/functionfunctionCall(address target, bytesmemory data)
internalreturns (bytesmemory)
{
return functionCall(target, data, "Address: low-level call failed");
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with
* `errorMessage` as a fallback revert reason when `target` reverts.
*
* _Available since v3.1._
*/functionfunctionCall(address target,
bytesmemory data,
stringmemory errorMessage
) internalreturns (bytesmemory) {
return functionCallWithValue(target, data, 0, errorMessage);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but also transferring `value` wei to `target`.
*
* Requirements:
*
* - the calling contract must have an ETH balance of at least `value`.
* - the called Solidity function must be `payable`.
*
* _Available since v3.1._
*/functionfunctionCallWithValue(address target,
bytesmemory data,
uint256 value
) internalreturns (bytesmemory) {
return
functionCallWithValue(
target,
data,
value,
"Address: low-level call with value failed"
);
}
/**
* @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but
* with `errorMessage` as a fallback revert reason when `target` reverts.
*
* _Available since v3.1._
*/functionfunctionCallWithValue(address target,
bytesmemory data,
uint256 value,
stringmemory errorMessage
) internalreturns (bytesmemory) {
require(
address(this).balance>= value,
"Address: insufficient balance for call"
);
require(isContract(target), "Address: call to non-contract");
(bool success, bytesmemory returndata) = target.call{value: value}(
data
);
return verifyCallResult(success, returndata, errorMessage);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but performing a static call.
*
* _Available since v3.3._
*/functionfunctionStaticCall(address target, bytesmemory data)
internalviewreturns (bytesmemory)
{
return
functionStaticCall(
target,
data,
"Address: low-level static call failed"
);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
* but performing a static call.
*
* _Available since v3.3._
*/functionfunctionStaticCall(address target,
bytesmemory data,
stringmemory errorMessage
) internalviewreturns (bytesmemory) {
require(isContract(target), "Address: static call to non-contract");
(bool success, bytesmemory returndata) = target.staticcall(data);
return verifyCallResult(success, returndata, errorMessage);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but performing a delegate call.
*
* _Available since v3.4._
*/functionfunctionDelegateCall(address target, bytesmemory data)
internalreturns (bytesmemory)
{
return
functionDelegateCall(
target,
data,
"Address: low-level delegate call failed"
);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
* but performing a delegate call.
*
* _Available since v3.4._
*/functionfunctionDelegateCall(address target,
bytesmemory data,
stringmemory errorMessage
) internalreturns (bytesmemory) {
require(isContract(target), "Address: delegate call to non-contract");
(bool success, bytesmemory returndata) = target.delegatecall(data);
return verifyCallResult(success, returndata, errorMessage);
}
/**
* @dev Tool to verifies that a low level call was successful, and revert if it wasn't, either by bubbling the
* revert reason using the provided one.
*
* _Available since v4.3._
*/functionverifyCallResult(bool success,
bytesmemory returndata,
stringmemory errorMessage
) internalpurereturns (bytesmemory) {
if (success) {
return returndata;
} else {
// Look for revert reason and bubble it up if presentif (returndata.length>0) {
// The easiest way to bubble the revert reason is using memory via assemblyassembly {
let returndata_size :=mload(returndata)
revert(add(32, returndata), returndata_size)
}
} else {
revert(errorMessage);
}
}
}
}
Contract Source Code
File 2 of 15: ComposableTopDown.sol
// SPDX-License-Identifier: UNLICENSEDpragmasolidity ^0.8.0;import"./IERC998ERC721BottomUp.sol";
import"./IERC998ERC721TopDown.sol";
import"./IERC998ERC721TopDownEnumerable.sol";
import"./ERC165.sol";
import"./IERC721.sol";
import"./IERC721Receiver.sol";
import"./Address.sol";
import"./Strings.sol";
import"./EnumerableSet.sol";
import"./IERC721Metadata.sol";
contractComposableTopDownisERC165,
IERC721,
IERC998ERC721TopDown,
IERC998ERC721TopDownEnumerable,
IERC721Metadata{
usingAddressforaddress;
usingStringsforuint256;
usingEnumerableSetforEnumerableSet.UintSet;
usingEnumerableSetforEnumerableSet.AddressSet;
// return this.rootOwnerOf.selector ^ this.rootOwnerOfChild.selector ^// this.tokenOwnerOf.selector ^ this.ownerOfChild.selector;bytes4constant ERC998_MAGIC_VALUE =0xcd740db5;
bytes32constant ERC998_MAGIC_VALUE_32 =0xcd740db500000000000000000000000000000000000000000000000000000000;
uint256 tokenCount =0;
// tokenId => token ownermapping(uint256=>address) private tokenIdToTokenOwner;
// Mapping from holder address to their (enumerable) set of owned tokensmapping(address=> EnumerableSet.UintSet) private _holderTokens;
// tokenId => last state hash indicatormapping(uint256=>uint256) private tokenIdToStateHash;
// root token owner address => (tokenId => approved address)mapping(address=>mapping(uint256=>address))
private rootOwnerAndTokenIdToApprovedAddress;
// token owner address => token countmapping(address=>uint256) private tokenOwnerToTokenCount;
// token owner => (operator address => bool)mapping(address=>mapping(address=>bool)) private tokenOwnerToOperators;
// Token namestringprivate _name;
// Token symbolstringprivate _symbol;
constructor(stringmemory name_, stringmemory symbol_) {
_name = name_;
_symbol = symbol_;
}
function_safeMint(address _to) internalvirtualreturns (uint256) {
require(_to !=address(0), "CTD: _to zero addr");
tokenCount++;
uint256 tokenCount_ = tokenCount;
tokenIdToTokenOwner[tokenCount_] = _to;
_holderTokens[_to].add(tokenCount_);
tokenOwnerToTokenCount[_to]++;
tokenIdToStateHash[tokenCount] =uint256(
keccak256(
abi.encodePacked(uint256(uint160(address(this))), tokenCount)
)
);
require(
_checkOnERC721Received(address(0), _to, tokenCount_, ""),
"CTD: transfer to non ERC721Receiver"
);
emit Transfer(address(0), _to, tokenCount_);
return tokenCount_;
}
//from zepellin ERC721Receiver.sol//old versionbytes4constant ERC721_RECEIVED_OLD =0xf0b9e5ba;
//new versionbytes4constant ERC721_RECEIVED_NEW =0x150b7a02;
bytes4constant ALLOWANCE =bytes4(keccak256("allowance(address,address)"));
bytes4constant APPROVE =bytes4(keccak256("approve(address,uint256)"));
bytes4constant ROOT_OWNER_OF_CHILD =bytes4(keccak256("rootOwnerOfChild(address,uint256)"));
////////////////////////////////////////////////////////// ERC721 implementation////////////////////////////////////////////////////////functionrootOwnerOf(uint256 _tokenId)
publicviewoverridereturns (bytes32 rootOwner)
{
return rootOwnerOfChild(address(0), _tokenId);
}
// returns the owner at the top of the tree of composables// Use Cases handled:// Case 1: Token owner is this contract and token.// Case 2: Token owner is other top-down composable// Case 3: Token owner is other contract// Case 4: Token owner is userfunctionrootOwnerOfChild(address _childContract, uint256 _childTokenId)
publicviewoverridereturns (bytes32 rootOwner)
{
address rootOwnerAddress;
if (_childContract !=address(0)) {
(rootOwnerAddress, _childTokenId) = _ownerOfChild(
_childContract,
_childTokenId
);
} else {
rootOwnerAddress = tokenIdToTokenOwner[_childTokenId];
require(
rootOwnerAddress !=address(0),
"CTD: ownerOf _tokenId zero addr"
);
}
// Case 1: Token owner is this contract and token.while (rootOwnerAddress ==address(this)) {
(rootOwnerAddress, _childTokenId) = _ownerOfChild(
rootOwnerAddress,
_childTokenId
);
}
bytesmemory callData =abi.encodeWithSelector(
ROOT_OWNER_OF_CHILD,
address(this),
_childTokenId
);
(bool callSuccess, bytesmemory data) = rootOwnerAddress.staticcall(
callData
);
if (callSuccess) {
assembly {
rootOwner :=mload(add(data, 0x20))
}
}
if (
callSuccess ==true&&
rootOwner &0xffffffff00000000000000000000000000000000000000000000000000000000==
ERC998_MAGIC_VALUE_32
) {
// Case 2: Token owner is other top-down composablereturn rootOwner;
} else {
// Case 3: Token owner is other contract// Or// Case 4: Token owner is userassembly {
rootOwner :=or(ERC998_MAGIC_VALUE_32, rootOwnerAddress)
}
}
}
// returns the owner at the top of the tree of composablesfunctionownerOf(uint256 _tokenId)
publicviewoverridereturns (address tokenOwner)
{
tokenOwner = tokenIdToTokenOwner[_tokenId];
require(tokenOwner !=address(0), "CTD: ownerOf _tokenId zero addr");
return tokenOwner;
}
functionbalanceOf(address _tokenOwner)
publicviewoverridereturns (uint256)
{
require(
_tokenOwner !=address(0),
"CTD: balanceOf _tokenOwner zero addr"
);
return tokenOwnerToTokenCount[_tokenOwner];
}
functionapprove(address _approved, uint256 _tokenId) externaloverride{
address rootOwner =address(uint160(uint256(rootOwnerOf(_tokenId))));
require(
rootOwner ==msg.sender||
tokenOwnerToOperators[rootOwner][msg.sender],
"CTD: approve msg.sender not owner"
);
rootOwnerAndTokenIdToApprovedAddress[rootOwner][_tokenId] = _approved;
emit Approval(rootOwner, _approved, _tokenId);
}
functiongetApproved(uint256 _tokenId)
publicviewoverridereturns (address)
{
address rootOwner =address(uint160(uint256(rootOwnerOf(_tokenId))));
return rootOwnerAndTokenIdToApprovedAddress[rootOwner][_tokenId];
}
functionsetApprovalForAll(address _operator, bool _approved)
externaloverride{
require(_operator !=address(0), "CTD: _operator zero addr");
tokenOwnerToOperators[msg.sender][_operator] = _approved;
emit ApprovalForAll(msg.sender, _operator, _approved);
}
functionisApprovedForAll(address _owner, address _operator)
externalviewoverridereturns (bool)
{
require(_owner !=address(0), "CTD: _owner zero addr");
require(_operator !=address(0), "CTD: _operator zero addr");
return tokenOwnerToOperators[_owner][_operator];
}
functiontransferFrom(address _from,
address _to,
uint256 _tokenId
) publicoverride{
_transferFrom(_from, _to, _tokenId);
}
functionsafeTransferFrom(address _from,
address _to,
uint256 _tokenId
) publicoverride{
_transferFrom(_from, _to, _tokenId);
if (_to.isContract()) {
bytes4 retval = IERC721Receiver(_to).onERC721Received(
msg.sender,
_from,
_tokenId,
""
);
require(
retval == ERC721_RECEIVED_OLD || retval == ERC721_RECEIVED_NEW,
"CTD: safeTransferFrom(3) onERC721Received invalid return value"
);
}
}
functionsafeTransferFrom(address _from,
address _to,
uint256 _tokenId,
bytesmemory _data
) publicoverride{
_transferFrom(_from, _to, _tokenId);
if (_to.isContract()) {
bytes4 retval = IERC721Receiver(_to).onERC721Received(
msg.sender,
_from,
_tokenId,
_data
);
require(
retval == ERC721_RECEIVED_OLD || retval == ERC721_RECEIVED_NEW,
"CTD: safeTransferFrom(4) onERC721Received invalid return value"
);
rootOwnerOf(_tokenId);
}
}
function_transferFrom(address _from,
address _to,
uint256 _tokenId
) private{
require(_from !=address(0), "CTD: _from zero addr");
require(tokenIdToTokenOwner[_tokenId] == _from, "CTD: _from not owner");
require(_to !=address(0), "CTD: _to zero address");
if (msg.sender!= _from) {
bytesmemory callData =abi.encodeWithSelector(
ROOT_OWNER_OF_CHILD,
address(this),
_tokenId
);
(bool callSuccess, bytesmemory data) = _from.staticcall(callData);
if (callSuccess ==true) {
bytes32 rootOwner;
assembly {
rootOwner :=mload(add(data, 0x20))
}
require(
rootOwner &0xffffffff00000000000000000000000000000000000000000000000000000000!=
ERC998_MAGIC_VALUE_32,
"CTD: token is child of other top down composable"
);
}
require(
tokenOwnerToOperators[_from][msg.sender] ||
rootOwnerAndTokenIdToApprovedAddress[_from][_tokenId] ==msg.sender,
"CTD: msg.sender not approved"
);
}
// clear approvalif (
rootOwnerAndTokenIdToApprovedAddress[_from][_tokenId] !=address(0)
) {
delete rootOwnerAndTokenIdToApprovedAddress[_from][_tokenId];
emit Approval(_from, address(0), _tokenId);
}
// remove and transfer tokenif (_from != _to) {
assert(tokenOwnerToTokenCount[_from] >0);
tokenOwnerToTokenCount[_from]--;
tokenIdToTokenOwner[_tokenId] = _to;
_holderTokens[_from].remove(_tokenId);
_holderTokens[_to].add(_tokenId);
tokenOwnerToTokenCount[_to]++;
}
emit Transfer(_from, _to, _tokenId);
}
////////////////////////////////////////////////////////// NFT Extendsion Metadata implementation/////////////////////////////////////////////////////////**
* @dev See {IERC721Metadata-name}.
*/functionname() publicviewvirtualoverridereturns (stringmemory) {
return _name;
}
/**
* @dev See {IERC721Metadata-symbol}.
*/functionsymbol() publicviewvirtualoverridereturns (stringmemory) {
return _symbol;
}
/**
* @dev Returns whether `tokenId` exists.
*
* Tokens can be managed by their owner or approved accounts via {approve} or {setApprovalForAll}.
*
* Tokens start existing when they are minted (`_mint`),
* and stop existing when they are burned (`_burn`).
*/function_exists(uint256 tokenId) internalviewvirtualreturns (bool) {
return tokenIdToTokenOwner[tokenId] !=address(0);
}
functiontokenURI(uint256 tokenId)
publicviewvirtualoverridereturns (stringmemory)
{
require(
_exists(tokenId),
"ERC721Metadata: URI query for nonexistent token"
);
stringmemory baseURI = _baseURI();
returnbytes(baseURI).length>0
? string(abi.encodePacked(baseURI, tokenId.toString()))
: "";
}
/**
* @dev Base URI for computing {tokenURI}. If set, the resulting URI for each
* token will be the concatenation of the `baseURI` and the `tokenId`. Empty
* by default, can be overriden in child contracts.
*/function_baseURI() internalviewvirtualreturns (stringmemory) {
return"";
}
/**
* @dev See {IERC721Enumerable-tokenOfOwnerByIndex}.
*/functiontokenOfOwnerByIndex(address owner, uint256 index)
publicviewvirtualreturns (uint256)
{
return _holderTokens[owner].at(index);
}
functiongetTokenCount() publicviewreturns (uint256) {
return tokenCount;
}
////////////////////////////////////////////////////////// ERC998ERC721 and ERC998ERC721Enumerable implementation////////////////////////////////////////////////////////// tokenId => child contractmapping(uint256=> EnumerableSet.AddressSet) private childContracts;
// tokenId => (child address => array of child tokens)mapping(uint256=>mapping(address=> EnumerableSet.UintSet))
private childTokens;
// child address => childId => tokenIdmapping(address=>mapping(uint256=>uint256)) private childTokenOwner;
functionsafeTransferChild(uint256 _fromTokenId,
address _to,
address _childContract,
uint256 _childTokenId
) externaloverride{
_transferChild(_fromTokenId, _to, _childContract, _childTokenId);
IERC721(_childContract).safeTransferFrom(
address(this),
_to,
_childTokenId
);
emit TransferChild(_fromTokenId, _to, _childContract, _childTokenId);
}
functionsafeTransferChild(uint256 _fromTokenId,
address _to,
address _childContract,
uint256 _childTokenId,
bytesmemory _data
) externaloverride{
_transferChild(_fromTokenId, _to, _childContract, _childTokenId);
IERC721(_childContract).safeTransferFrom(
address(this),
_to,
_childTokenId,
_data
);
emit TransferChild(_fromTokenId, _to, _childContract, _childTokenId);
}
functiontransferChild(uint256 _fromTokenId,
address _to,
address _childContract,
uint256 _childTokenId
) externaloverride{
_transferChild(_fromTokenId, _to, _childContract, _childTokenId);
//this is here to be compatible with cryptokitties and other old contracts that require being owner and approved// before transferring.//does not work with current standard which does not allow approving self, so we must let it fail in that case.bytesmemory callData =abi.encodeWithSelector(
APPROVE,
this,
_childTokenId
);
_childContract.call(callData);
IERC721(_childContract).transferFrom(address(this), _to, _childTokenId);
emit TransferChild(_fromTokenId, _to, _childContract, _childTokenId);
}
functiontransferChildToParent(uint256 _fromTokenId,
address _toContract,
uint256 _toTokenId,
address _childContract,
uint256 _childTokenId,
bytesmemory _data
) externaloverride{
_transferChild(
_fromTokenId,
_toContract,
_childContract,
_childTokenId
);
IERC998ERC721BottomUp(_childContract).transferToParent(
address(this),
_toContract,
_toTokenId,
_childTokenId,
_data
);
emit TransferChild(
_fromTokenId,
_toContract,
_childContract,
_childTokenId
);
}
// this contract has to be approved first in _childContractfunctiongetChild(address _from,
uint256 _tokenId,
address _childContract,
uint256 _childTokenId
) externaloverride{
receiveChild(_from, _tokenId, _childContract, _childTokenId);
require(
_from ==msg.sender||
IERC721(_childContract).isApprovedForAll(_from, msg.sender) ||
IERC721(_childContract).getApproved(_childTokenId) ==msg.sender,
"CTD: msg.sender not approved"
);
IERC721(_childContract).transferFrom(
_from,
address(this),
_childTokenId
);
// a check for looped ownership chain
rootOwnerOf(_tokenId);
}
functiononERC721Received(address _from,
uint256 _childTokenId,
bytescalldata _data
) externalreturns (bytes4) {
require(
_data.length>0,
"CTD: onERC721Received(3) _data must contain the uint256 tokenId to transfer the child token to"
);
// convert up to 32 bytes of _data to uint256, owner nft tokenId passed as uint in bytesuint256 tokenId = _parseTokenId(_data);
receiveChild(_from, tokenId, msg.sender, _childTokenId);
require(
IERC721(msg.sender).ownerOf(_childTokenId) !=address(0),
"CTD: onERC721Received(3) child token not owned"
);
// a check for looped ownership chain
rootOwnerOf(tokenId);
return ERC721_RECEIVED_OLD;
}
functiononERC721Received(address,
address _from,
uint256 _childTokenId,
bytescalldata _data
) externaloverridereturns (bytes4) {
require(
_data.length>0,
"CTD: onERC721Received(4) _data must contain the uint256 tokenId to transfer the child token to"
);
// convert up to 32 bytes of _data to uint256, owner nft tokenId passed as uint in bytesuint256 tokenId = _parseTokenId(_data);
receiveChild(_from, tokenId, msg.sender, _childTokenId);
require(
IERC721(msg.sender).ownerOf(_childTokenId) !=address(0),
"CTD: onERC721Received(4) child token not owned"
);
// a check for looped ownership chain
rootOwnerOf(tokenId);
return ERC721_RECEIVED_NEW;
}
functionchildExists(address _childContract, uint256 _childTokenId)
externalviewreturns (bool)
{
uint256 tokenId = childTokenOwner[_childContract][_childTokenId];
return tokenId !=0;
}
functiontotalChildContracts(uint256 _tokenId)
publicviewoverridereturns (uint256)
{
return childContracts[_tokenId].length();
}
functionchildContractByIndex(uint256 _tokenId, uint256 _index)
publicviewoverridereturns (address childContract)
{
return childContracts[_tokenId].at(_index);
}
functiontotalChildTokens(uint256 _tokenId, address _childContract)
publicviewoverridereturns (uint256)
{
return childTokens[_tokenId][_childContract].length();
}
functionchildTokenByIndex(uint256 _tokenId,
address _childContract,
uint256 _index
) publicviewoverridereturns (uint256 childTokenId) {
return childTokens[_tokenId][_childContract].at(_index);
}
functionownerOfChild(address _childContract, uint256 _childTokenId)
externalviewoverridereturns (bytes32 parentTokenOwner, uint256 parentTokenId)
{
parentTokenId = childTokenOwner[_childContract][_childTokenId];
require(parentTokenId !=0, "CTD: not found");
address parentTokenOwnerAddress = tokenIdToTokenOwner[parentTokenId];
assembly {
parentTokenOwner :=or(
ERC998_MAGIC_VALUE_32,
parentTokenOwnerAddress
)
}
}
function_transferChild(uint256 _fromTokenId,
address _to,
address _childContract,
uint256 _childTokenId
) private{
uint256 tokenId = childTokenOwner[_childContract][_childTokenId];
require(tokenId !=0, "CTD: _childContract _childTokenId not found");
require(tokenId == _fromTokenId, "CTD: wrong tokenId found");
require(_to !=address(0), "CTD: _to zero addr");
address rootOwner =address(uint160(uint256(rootOwnerOf(tokenId))));
require(
rootOwner ==msg.sender||
tokenOwnerToOperators[rootOwner][msg.sender] ||
rootOwnerAndTokenIdToApprovedAddress[rootOwner][tokenId] ==msg.sender,
"CTD: msg.sender not eligible"
);
removeChild(tokenId, _childContract, _childTokenId);
}
function_ownerOfChild(address _childContract, uint256 _childTokenId)
privateviewreturns (address parentTokenOwner, uint256 parentTokenId)
{
parentTokenId = childTokenOwner[_childContract][_childTokenId];
require(parentTokenId !=0, "CTD: not found");
return (tokenIdToTokenOwner[parentTokenId], parentTokenId);
}
function_parseTokenId(bytesmemory _data)
privatepurereturns (uint256 tokenId)
{
// convert up to 32 bytes of_data to uint256, owner nft tokenId passed as uint in bytesassembly {
tokenId :=mload(add(_data, 0x20))
}
if (_data.length<32) {
tokenId = tokenId >> (256- _data.length*8);
}
}
function_checkOnERC721Received(addressfrom,
address to,
uint256 tokenId,
bytesmemory _data
) privatereturns (bool) {
if (to.isContract()) {
try
IERC721Receiver(to).onERC721Received(
msg.sender,
from,
tokenId,
_data
)
returns (bytes4 retval) {
return retval == IERC721Receiver(to).onERC721Received.selector;
} catch (bytesmemory reason) {
if (reason.length==0) {
revert(
"ERC721: transfer to non ERC721Receiver implementer"
);
} else {
// solhint-disable-next-line no-inline-assemblyassembly {
revert(add(32, reason), mload(reason))
}
}
}
} else {
returntrue;
}
}
functionremoveChild(uint256 _tokenId,
address _childContract,
uint256 _childTokenId
) private{
// remove child tokenuint256 lastTokenIndex = childTokens[_tokenId][_childContract]
.length() -1;
childTokens[_tokenId][_childContract].remove(_childTokenId);
delete childTokenOwner[_childContract][_childTokenId];
// remove contractif (lastTokenIndex ==0) {
childContracts[_tokenId].remove(_childContract);
}
if (_childContract ==address(this)) {
_updateStateHash(
_tokenId,
uint256(uint160(_childContract)),
tokenIdToStateHash[_childTokenId]
);
} else {
_updateStateHash(
_tokenId,
uint256(uint160(_childContract)),
_childTokenId
);
}
}
functionreceiveChild(address _from,
uint256 _tokenId,
address _childContract,
uint256 _childTokenId
) private{
require(
tokenIdToTokenOwner[_tokenId] !=address(0),
"CTD: _tokenId does not exist."
);
require(
childTokenOwner[_childContract][_childTokenId] != _tokenId,
"CTD: _childTokenId already received"
);
uint256 childTokensLength = childTokens[_tokenId][_childContract]
.length();
if (childTokensLength ==0) {
childContracts[_tokenId].add(_childContract);
}
childTokens[_tokenId][_childContract].add(_childTokenId);
childTokenOwner[_childContract][_childTokenId] = _tokenId;
if (_childContract ==address(this)) {
_updateStateHash(
_tokenId,
uint256(uint160(_childContract)),
tokenIdToStateHash[_childTokenId]
);
} else {
_updateStateHash(
_tokenId,
uint256(uint160(_childContract)),
_childTokenId
);
}
emit ReceivedChild(_from, _tokenId, _childContract, _childTokenId);
}
////////////////////////////////////////////////////////// ERC165 implementation/////////////////////////////////////////////////////////**
* @dev See {IERC165-supportsInterface}.
* The interface id 0x1bc995e4 is added. The spec claims it to be the interface id of IERC998ERC721TopDown.
* But it is not.
* It is added anyway in case some contract checks it being compliant with the spec.
*/functionsupportsInterface(bytes4 interfaceId)
publicviewoverride(IERC165, ERC165)
returns (bool)
{
return
interfaceId ==type(IERC721).interfaceId||
interfaceId ==type(IERC998ERC721TopDown).interfaceId||
interfaceId ==type(IERC998ERC721TopDownEnumerable).interfaceId||
interfaceId ==0x1bc995e4||super.supportsInterface(interfaceId);
}
////////////////////////////////////////////////////////// Last State Hash/////////////////////////////////////////////////////////**
* Update the state hash of tokenId and all its ancestors.
* @param tokenId token id
* @param childReference generalization of a child contract adddress
* @param value new balance of ERC20, childTokenId of ERC721 or a child's state hash (if childContract==address(this))
*/function_updateStateHash(uint256 tokenId,
uint256 childReference,
uint256 value
) private{
uint256 _newStateHash =uint256(
keccak256(
abi.encodePacked(
tokenIdToStateHash[tokenId],
childReference,
value
)
)
);
tokenIdToStateHash[tokenId] = _newStateHash;
while (tokenIdToTokenOwner[tokenId] ==address(this)) {
tokenId = childTokenOwner[address(this)][tokenId];
_newStateHash =uint256(
keccak256(
abi.encodePacked(
tokenIdToStateHash[tokenId],
uint256(uint160(address(this))),
_newStateHash
)
)
);
tokenIdToStateHash[tokenId] = _newStateHash;
}
}
functionstateHash(uint256 tokenId) publicviewreturns (uint256) {
uint256 _stateHash = tokenIdToStateHash[tokenId];
require(_stateHash >0, "CTD: stateHash of _tokenId is zero");
return _stateHash;
}
/**
* @dev See {safeTransferFrom}.
* Check the state hash and call safeTransferFrom.
*/functionsafeCheckedTransferFrom(addressfrom,
address to,
uint256 tokenId,
uint256 expectedStateHash
) external{
require(
expectedStateHash == tokenIdToStateHash[tokenId],
"CTD: stateHash mismatch (1)"
);
safeTransferFrom(from, to, tokenId);
}
/**
* @dev See {transferFrom}.
* Check the state hash and call transferFrom.
*/functioncheckedTransferFrom(addressfrom,
address to,
uint256 tokenId,
uint256 expectedStateHash
) external{
require(
expectedStateHash == tokenIdToStateHash[tokenId],
"CTD: stateHash mismatch (2)"
);
transferFrom(from, to, tokenId);
}
/**
* @dev See {safeTransferFrom}.
* Check the state hash and call safeTransferFrom.
*/functionsafeCheckedTransferFrom(addressfrom,
address to,
uint256 tokenId,
uint256 expectedStateHash,
bytescalldata data
) external{
require(
expectedStateHash == tokenIdToStateHash[tokenId],
"CTD: stateHash mismatch (3)"
);
safeTransferFrom(from, to, tokenId, data);
}
}
Contract Source Code
File 3 of 15: Context.sol
// SPDX-License-Identifier: MIT// OpenZeppelin Contracts v4.3.2 (utils/Context.sol)pragmasolidity ^0.8.0;/**
* @dev Provides information about the current execution context, including the
* sender of the transaction and its data. While these are generally available
* via msg.sender and msg.data, they should not be accessed in such a direct
* manner, since when dealing with meta-transactions the account sending and
* paying for execution may not be the actual sender (as far as an application
* is concerned).
*
* This contract is only required for intermediate, library-like contracts.
*/abstractcontractContext{
function_msgSender() internalviewvirtualreturns (address) {
returnmsg.sender;
}
function_msgData() internalviewvirtualreturns (bytescalldata) {
returnmsg.data;
}
}
Contract Source Code
File 4 of 15: ERC165.sol
// SPDX-License-Identifier: MIT// OpenZeppelin Contracts v4.3.2 (utils/introspection/ERC165.sol)pragmasolidity ^0.8.0;import"./IERC165.sol";
/**
* @dev Implementation of the {IERC165} interface.
*
* Contracts that want to implement ERC165 should inherit from this contract and override {supportsInterface} to check
* for the additional interface id that will be supported. For example:
*
* ```solidity
* function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
* return interfaceId == type(MyInterface).interfaceId || super.supportsInterface(interfaceId);
* }
* ```
*
* Alternatively, {ERC165Storage} provides an easier to use but more expensive implementation.
*/abstractcontractERC165isIERC165{
/**
* @dev See {IERC165-supportsInterface}.
*/functionsupportsInterface(bytes4 interfaceId)
publicviewvirtualoverridereturns (bool)
{
return interfaceId ==type(IERC165).interfaceId;
}
}
Contract Source Code
File 5 of 15: EnumerableSet.sol
// SPDX-License-Identifier: MIT// OpenZeppelin Contracts v4.3.2 (utils/structs/EnumerableSet.sol)pragmasolidity ^0.8.0;/**
* @dev Library for managing
* https://en.wikipedia.org/wiki/Set_(abstract_data_type)[sets] of primitive
* types.
*
* Sets have the following properties:
*
* - Elements are added, removed, and checked for existence in constant time
* (O(1)).
* - Elements are enumerated in O(n). No guarantees are made on the ordering.
*
* ```
* contract Example {
* // Add the library methods
* using EnumerableSet for EnumerableSet.AddressSet;
*
* // Declare a set state variable
* EnumerableSet.AddressSet private mySet;
* }
* ```
*
* As of v3.3.0, sets of type `bytes32` (`Bytes32Set`), `address` (`AddressSet`)
* and `uint256` (`UintSet`) are supported.
*/libraryEnumerableSet{
// To implement this library for multiple types with as little code// repetition as possible, we write it in terms of a generic Set type with// bytes32 values.// The Set implementation uses private functions, and user-facing// implementations (such as AddressSet) are just wrappers around the// underlying Set.// This means that we can only create new EnumerableSets for types that fit// in bytes32.structSet {
// Storage of set valuesbytes32[] _values;
// Position of the value in the `values` array, plus 1 because index 0// means a value is not in the set.mapping(bytes32=>uint256) _indexes;
}
/**
* @dev Add a value to a set. O(1).
*
* Returns true if the value was added to the set, that is if it was not
* already present.
*/function_add(Set storage set, bytes32 value) privatereturns (bool) {
if (!_contains(set, value)) {
set._values.push(value);
// The value is stored at length-1, but we add 1 to all indexes// and use 0 as a sentinel value
set._indexes[value] = set._values.length;
returntrue;
} else {
returnfalse;
}
}
/**
* @dev Removes a value from a set. O(1).
*
* Returns true if the value was removed from the set, that is if it was
* present.
*/function_remove(Set storage set, bytes32 value) privatereturns (bool) {
// We read and store the value's index to prevent multiple reads from the same storage slotuint256 valueIndex = set._indexes[value];
if (valueIndex !=0) {
// Equivalent to contains(set, value)// To delete an element from the _values array in O(1), we swap the element to delete with the last one in// the array, and then remove the last element (sometimes called as 'swap and pop').// This modifies the order of the array, as noted in {at}.uint256 toDeleteIndex = valueIndex -1;
uint256 lastIndex = set._values.length-1;
if (lastIndex != toDeleteIndex) {
bytes32 lastvalue = set._values[lastIndex];
// Move the last value to the index where the value to delete is
set._values[toDeleteIndex] = lastvalue;
// Update the index for the moved value
set._indexes[lastvalue] = valueIndex; // Replace lastvalue's index to valueIndex
}
// Delete the slot where the moved value was stored
set._values.pop();
// Delete the index for the deleted slotdelete set._indexes[value];
returntrue;
} else {
returnfalse;
}
}
/**
* @dev Returns true if the value is in the set. O(1).
*/function_contains(Set storage set, bytes32 value)
privateviewreturns (bool)
{
return set._indexes[value] !=0;
}
/**
* @dev Returns the number of values on the set. O(1).
*/function_length(Set storage set) privateviewreturns (uint256) {
return set._values.length;
}
/**
* @dev Returns the value stored at position `index` in the set. O(1).
*
* Note that there are no guarantees on the ordering of values inside the
* array, and it may change when more values are added or removed.
*
* Requirements:
*
* - `index` must be strictly less than {length}.
*/function_at(Set storage set, uint256 index)
privateviewreturns (bytes32)
{
return set._values[index];
}
/**
* @dev Return the entire set in an array
*
* WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
* to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
* this function has an unbounded cost, and using it as part of a state-changing function may render the function
* uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
*/function_values(Set storage set) privateviewreturns (bytes32[] memory) {
return set._values;
}
// Bytes32SetstructBytes32Set {
Set _inner;
}
/**
* @dev Add a value to a set. O(1).
*
* Returns true if the value was added to the set, that is if it was not
* already present.
*/functionadd(Bytes32Set storage set, bytes32 value)
internalreturns (bool)
{
return _add(set._inner, value);
}
/**
* @dev Removes a value from a set. O(1).
*
* Returns true if the value was removed from the set, that is if it was
* present.
*/functionremove(Bytes32Set storage set, bytes32 value)
internalreturns (bool)
{
return _remove(set._inner, value);
}
/**
* @dev Returns true if the value is in the set. O(1).
*/functioncontains(Bytes32Set storage set, bytes32 value)
internalviewreturns (bool)
{
return _contains(set._inner, value);
}
/**
* @dev Returns the number of values in the set. O(1).
*/functionlength(Bytes32Set storage set) internalviewreturns (uint256) {
return _length(set._inner);
}
/**
* @dev Returns the value stored at position `index` in the set. O(1).
*
* Note that there are no guarantees on the ordering of values inside the
* array, and it may change when more values are added or removed.
*
* Requirements:
*
* - `index` must be strictly less than {length}.
*/functionat(Bytes32Set storage set, uint256 index)
internalviewreturns (bytes32)
{
return _at(set._inner, index);
}
/**
* @dev Return the entire set in an array
*
* WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
* to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
* this function has an unbounded cost, and using it as part of a state-changing function may render the function
* uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
*/functionvalues(Bytes32Set storage set)
internalviewreturns (bytes32[] memory)
{
return _values(set._inner);
}
// AddressSetstructAddressSet {
Set _inner;
}
/**
* @dev Add a value to a set. O(1).
*
* Returns true if the value was added to the set, that is if it was not
* already present.
*/functionadd(AddressSet storage set, address value)
internalreturns (bool)
{
return _add(set._inner, bytes32(uint256(uint160(value))));
}
/**
* @dev Removes a value from a set. O(1).
*
* Returns true if the value was removed from the set, that is if it was
* present.
*/functionremove(AddressSet storage set, address value)
internalreturns (bool)
{
return _remove(set._inner, bytes32(uint256(uint160(value))));
}
/**
* @dev Returns true if the value is in the set. O(1).
*/functioncontains(AddressSet storage set, address value)
internalviewreturns (bool)
{
return _contains(set._inner, bytes32(uint256(uint160(value))));
}
/**
* @dev Returns the number of values in the set. O(1).
*/functionlength(AddressSet storage set) internalviewreturns (uint256) {
return _length(set._inner);
}
/**
* @dev Returns the value stored at position `index` in the set. O(1).
*
* Note that there are no guarantees on the ordering of values inside the
* array, and it may change when more values are added or removed.
*
* Requirements:
*
* - `index` must be strictly less than {length}.
*/functionat(AddressSet storage set, uint256 index)
internalviewreturns (address)
{
returnaddress(uint160(uint256(_at(set._inner, index))));
}
/**
* @dev Return the entire set in an array
*
* WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
* to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
* this function has an unbounded cost, and using it as part of a state-changing function may render the function
* uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
*/functionvalues(AddressSet storage set)
internalviewreturns (address[] memory)
{
bytes32[] memory store = _values(set._inner);
address[] memory result;
assembly {
result := store
}
return result;
}
// UintSetstructUintSet {
Set _inner;
}
/**
* @dev Add a value to a set. O(1).
*
* Returns true if the value was added to the set, that is if it was not
* already present.
*/functionadd(UintSet storage set, uint256 value) internalreturns (bool) {
return _add(set._inner, bytes32(value));
}
/**
* @dev Removes a value from a set. O(1).
*
* Returns true if the value was removed from the set, that is if it was
* present.
*/functionremove(UintSet storage set, uint256 value)
internalreturns (bool)
{
return _remove(set._inner, bytes32(value));
}
/**
* @dev Returns true if the value is in the set. O(1).
*/functioncontains(UintSet storage set, uint256 value)
internalviewreturns (bool)
{
return _contains(set._inner, bytes32(value));
}
/**
* @dev Returns the number of values on the set. O(1).
*/functionlength(UintSet storage set) internalviewreturns (uint256) {
return _length(set._inner);
}
/**
* @dev Returns the value stored at position `index` in the set. O(1).
*
* Note that there are no guarantees on the ordering of values inside the
* array, and it may change when more values are added or removed.
*
* Requirements:
*
* - `index` must be strictly less than {length}.
*/functionat(UintSet storage set, uint256 index)
internalviewreturns (uint256)
{
returnuint256(_at(set._inner, index));
}
/**
* @dev Return the entire set in an array
*
* WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
* to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
* this function has an unbounded cost, and using it as part of a state-changing function may render the function
* uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
*/functionvalues(UintSet storage set)
internalviewreturns (uint256[] memory)
{
bytes32[] memory store = _values(set._inner);
uint256[] memory result;
assembly {
result := store
}
return result;
}
}
Contract Source Code
File 6 of 15: IERC165.sol
// SPDX-License-Identifier: MIT// OpenZeppelin Contracts v4.3.2 (utils/introspection/IERC165.sol)pragmasolidity ^0.8.0;/**
* @dev Interface of the ERC165 standard, as defined in the
* https://eips.ethereum.org/EIPS/eip-165[EIP].
*
* Implementers can declare support of contract interfaces, which can then be
* queried by others ({ERC165Checker}).
*
* For an implementation, see {ERC165}.
*/interfaceIERC165{
/**
* @dev Returns true if this contract implements the interface defined by
* `interfaceId`. See the corresponding
* https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified[EIP section]
* to learn more about how these ids are created.
*
* This function call must use less than 30 000 gas.
*/functionsupportsInterface(bytes4 interfaceId) externalviewreturns (bool);
}
Contract Source Code
File 7 of 15: IERC721.sol
// SPDX-License-Identifier: MIT// OpenZeppelin Contracts v4.3.2 (token/ERC721/IERC721.sol)pragmasolidity ^0.8.0;import"./IERC165.sol";
/**
* @dev Required interface of an ERC721 compliant contract.
*/interfaceIERC721isIERC165{
/**
* @dev Emitted when `tokenId` token is transferred from `from` to `to`.
*/eventTransfer(addressindexedfrom,
addressindexed to,
uint256indexed tokenId
);
/**
* @dev Emitted when `owner` enables `approved` to manage the `tokenId` token.
*/eventApproval(addressindexed owner,
addressindexed approved,
uint256indexed tokenId
);
/**
* @dev Emitted when `owner` enables or disables (`approved`) `operator` to manage all of its assets.
*/eventApprovalForAll(addressindexed owner,
addressindexed operator,
bool approved
);
/**
* @dev Returns the number of tokens in ``owner``'s account.
*/functionbalanceOf(address owner) externalviewreturns (uint256 balance);
/**
* @dev Returns the owner of the `tokenId` token.
*
* Requirements:
*
* - `tokenId` must exist.
*/functionownerOf(uint256 tokenId) externalviewreturns (address owner);
/**
* @dev Safely transfers `tokenId` token from `from` to `to`, checking first that contract recipients
* are aware of the ERC721 protocol to prevent tokens from being forever locked.
*
* Requirements:
*
* - `from` cannot be the zero address.
* - `to` cannot be the zero address.
* - `tokenId` token must exist and be owned by `from`.
* - If the caller is not `from`, it must be have been allowed to move this token by either {approve} or {setApprovalForAll}.
* - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
*
* Emits a {Transfer} event.
*/functionsafeTransferFrom(addressfrom,
address to,
uint256 tokenId
) external;
/**
* @dev Transfers `tokenId` token from `from` to `to`.
*
* WARNING: Usage of this method is discouraged, use {safeTransferFrom} whenever possible.
*
* Requirements:
*
* - `from` cannot be the zero address.
* - `to` cannot be the zero address.
* - `tokenId` token must be owned by `from`.
* - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
*
* Emits a {Transfer} event.
*/functiontransferFrom(addressfrom,
address to,
uint256 tokenId
) external;
/**
* @dev Gives permission to `to` to transfer `tokenId` token to another account.
* The approval is cleared when the token is transferred.
*
* Only a single account can be approved at a time, so approving the zero address clears previous approvals.
*
* Requirements:
*
* - The caller must own the token or be an approved operator.
* - `tokenId` must exist.
*
* Emits an {Approval} event.
*/functionapprove(address to, uint256 tokenId) external;
/**
* @dev Returns the account approved for `tokenId` token.
*
* Requirements:
*
* - `tokenId` must exist.
*/functiongetApproved(uint256 tokenId)
externalviewreturns (address operator);
/**
* @dev Approve or remove `operator` as an operator for the caller.
* Operators can call {transferFrom} or {safeTransferFrom} for any token owned by the caller.
*
* Requirements:
*
* - The `operator` cannot be the caller.
*
* Emits an {ApprovalForAll} event.
*/functionsetApprovalForAll(address operator, bool _approved) external;
/**
* @dev Returns if the `operator` is allowed to manage all of the assets of `owner`.
*
* See {setApprovalForAll}
*/functionisApprovedForAll(address owner, address operator)
externalviewreturns (bool);
/**
* @dev Safely transfers `tokenId` token from `from` to `to`.
*
* Requirements:
*
* - `from` cannot be the zero address.
* - `to` cannot be the zero address.
* - `tokenId` token must exist and be owned by `from`.
* - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
* - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
*
* Emits a {Transfer} event.
*/functionsafeTransferFrom(addressfrom,
address to,
uint256 tokenId,
bytescalldata data
) external;
}
// SPDX-License-Identifier: MIT// OpenZeppelin Contracts v4.3.2 (token/ERC721/IERC721Receiver.sol)pragmasolidity ^0.8.0;/**
* @title ERC721 token receiver interface
* @dev Interface for any contract that wants to support safeTransfers
* from ERC721 asset contracts.
*/interfaceIERC721Receiver{
/**
* @dev Whenever an {IERC721} `tokenId` token is transferred to this contract via {IERC721-safeTransferFrom}
* by `operator` from `from`, this function is called.
*
* It must return its Solidity selector to confirm the token transfer.
* If any other value is returned or the interface is not implemented by the recipient, the transfer will be reverted.
*
* The selector can be obtained in Solidity with `IERC721.onERC721Received.selector`.
*/functiononERC721Received(address operator,
addressfrom,
uint256 tokenId,
bytescalldata data
) externalreturns (bytes4);
}
Contract Source Code
File 10 of 15: IERC998ERC721BottomUp.sol
// SPDX-License-Identifier: UNLICENSEDpragmasolidity ^0.8.0;interfaceIERC998ERC721BottomUp{
eventTransferToParent(addressindexed _toContract,
uint256indexed _toTokenId,
uint256 _tokenId
);
eventTransferFromParent(addressindexed _fromContract,
uint256indexed _fromTokenId,
uint256 _tokenId
);
functionrootOwnerOf(uint256 _tokenId)
externalviewreturns (bytes32 rootOwner);
/**
* The tokenOwnerOf function gets the owner of the _tokenId which can be a user address or another ERC721 token.
* The tokenOwner address return value can be either a user address or an ERC721 contract address.
* If the tokenOwner address is a user address then parentTokenId will be 0 and should not be used or considered.
* If tokenOwner address is a user address then isParent is false, otherwise isChild is true, which means that
* tokenOwner is an ERC721 contract address and _tokenId is a child of tokenOwner and parentTokenId.
*/functiontokenOwnerOf(uint256 _tokenId)
externalviewreturns (bytes32 tokenOwner,
uint256 parentTokenId,
bool isParent
);
// Transfers _tokenId as a child to _toContract and _toTokenIdfunctiontransferToParent(address _from,
address _toContract,
uint256 _toTokenId,
uint256 _tokenId,
bytesmemory _data
) external;
// Transfers _tokenId from a parent ERC721 token to a user address.functiontransferFromParent(address _fromContract,
uint256 _fromTokenId,
address _to,
uint256 _tokenId,
bytesmemory _data
) external;
// Transfers _tokenId from a parent ERC721 token to a parent ERC721 token.functiontransferAsChild(address _fromContract,
uint256 _fromTokenId,
address _toContract,
uint256 _toTokenId,
uint256 _tokenId,
bytesmemory _data
) external;
}
// SPDX-License-Identifier: MITpragmasolidity ^0.8.0;import"./ComposableTopDown.sol";
import"./Address.sol";
import"./Strings.sol";
import"./Ownable.sol";
contractMetaBoomisComposableTopDown, Ownable{
usingAddressforaddress;
usingStringsforuint256;
uint256publicconstant maxSupply =5000;
uint256publicconstant price =0.05ether;
uint256publicconstant airDropMaxSupply =300;
uint256public totalSupply =0;
uint256public totalAirDrop =0;
stringpublic baseTokenURI;
stringpublic subTokenURI;
boolpublic paused =false;
uint256public preSaleTime =1636682400;
uint256public publicSaleTime =1637028000;
mapping(address=>bool) public airDropList;
mapping(address=>bool) public whiteList;
mapping(address=>uint8) public prePaidNumAry;
mapping(address=>uint8) public holdedNumAry;
mapping(address=>uint8) public claimed;
mapping(uint256=>string) private _tokenURIs;
eventMetaBoomPop(uint256indexed tokenId, addressindexed tokenOwner);
constructor(stringmemory _name,
stringmemory _symbol,
stringmemory _uri,
stringmemory _subUri
) ComposableTopDown(_name, _symbol) {
baseTokenURI = _uri;
subTokenURI = _subUri;
}
functionpreSale(uint8 _purchaseNum) externalpayableonlyWhiteList{
require(!paused, "MetaBoom: currently paused");
require(
block.timestamp>= preSaleTime,
"MetaBoom: preSale is not open"
);
require(
(totalSupply + _purchaseNum) <= (maxSupply - airDropMaxSupply),
"MetaBoom: reached max supply"
);
require(
(holdedNumAry[_msgSender()] + _purchaseNum) <=5,
"MetaBoom: can not hold more than 5"
);
require(
msg.value>= (price * _purchaseNum),
"MetaBoom: price is incorrect"
);
holdedNumAry[_msgSender()] = holdedNumAry[_msgSender()] + _purchaseNum;
prePaidNumAry[_msgSender()] =
prePaidNumAry[_msgSender()] +
_purchaseNum;
totalSupply = totalSupply + _purchaseNum;
}
functionpublicSale(uint8 _purchaseNum) externalpayable{
require(!paused, "MetaBoom: currently paused");
require(
block.timestamp>= publicSaleTime,
"MetaBoom: publicSale is not open"
);
require(
(totalSupply + _purchaseNum) <= (maxSupply - airDropMaxSupply),
"MetaBoom: reached max supply"
);
require(
(holdedNumAry[_msgSender()] + _purchaseNum) <=5,
"MetaBoom: can not hold more than 5"
);
require(
msg.value>= (price * _purchaseNum),
"MetaBoom: price is incorrect"
);
holdedNumAry[_msgSender()] = holdedNumAry[_msgSender()] + _purchaseNum;
prePaidNumAry[_msgSender()] =
prePaidNumAry[_msgSender()] +
_purchaseNum;
totalSupply = totalSupply + _purchaseNum;
}
functionownerMInt(address _addr)
externalonlyOwnerreturns (uint256 tokenId_)
{
require(
totalSupply < (maxSupply - airDropMaxSupply),
"MetaBoom: reached max supply"
);
require(holdedNumAry[_addr] <5, "MetaBoom: can not hold more than 5");
tokenId_ = _safeMint(_addr);
holdedNumAry[_addr]++;
claimed[_addr]++;
totalSupply++;
emit MetaBoomPop(tokenId_, _addr);
return tokenId_;
}
functionclaimAirdrop() externalonlyAirDrop{
require(
block.timestamp>= preSaleTime,
"MetaBoom: Not able to claim yet."
);
uint256 tokenId_ = _safeMint(_msgSender());
airDropList[_msgSender()] =false;
emit MetaBoomPop(tokenId_, _msgSender());
holdedNumAry[_msgSender()]++;
claimed[_msgSender()]++;
}
functionclaimAll() external{
require(
block.timestamp>= preSaleTime,
"MetaBoom: Not able to claim yet"
);
require(
prePaidNumAry[_msgSender()] >0,
"MetaBoom: already claimed all"
);
for (uint8 i =0; i < prePaidNumAry[_msgSender()]; i++) {
uint256 tokenId_ = _safeMint(_msgSender());
emit MetaBoomPop(tokenId_, _msgSender());
}
claimed[_msgSender()] += prePaidNumAry[_msgSender()];
prePaidNumAry[_msgSender()] =0;
}
modifieronlyWhiteList() {
require(whiteList[_msgSender()], "MetaBoom: caller not in WhiteList");
_;
}
modifieronlyAirDrop() {
require(
airDropList[_msgSender()],
"MetaBoom: caller not in AirdropList"
);
_;
}
functionsetBaseURI(stringmemory _baseURI) externalonlyOwner{
baseTokenURI = _baseURI;
}
functionsetSubURI(stringmemory _subURI) externalonlyOwner{
subTokenURI = _subURI;
}
functionsetTokenURI(uint256 _tokenId, stringmemory _tokenURI)
externalonlyOwner{
_tokenURIs[_tokenId] = _tokenURI;
}
functionsetPreSaleTime(uint256 _time) externalonlyOwner{
preSaleTime = _time;
}
functionsetPublicSaleTime(uint256 _time) externalonlyOwner{
publicSaleTime = _time;
}
functionpauseSale() externalonlyOwner{
paused =!paused;
}
functionaddBatchWhiteList(address[] memory _accounts) externalonlyOwner{
for (uint256 i =0; i < _accounts.length; i++) {
whiteList[_accounts[i]] =true;
}
}
functionaddBatchAirDropList(address[] memory _accounts)
externalonlyOwner{
require(
totalAirDrop + _accounts.length<= airDropMaxSupply,
"reached max airDropSupply"
);
for (uint256 i =0; i < _accounts.length; i++) {
require(holdedNumAry[_accounts[i]] <5, "can not hold more than 5");
airDropList[_accounts[i]] =true;
}
totalAirDrop = totalAirDrop + _accounts.length;
}
functionwithdraw() externalonlyOwner{
payable(owner()).transfer(address(this).balance);
}
functiontokensOfOwner(address _owner)
externalviewreturns (uint256[] memory)
{
uint256 tokenCount = balanceOf(_owner);
if (tokenCount ==0) {
returnnewuint256[](0);
} else {
uint256[] memory result =newuint256[](tokenCount);
uint256 index;
for (index =0; index < tokenCount; index++) {
result[index] = tokenOfOwnerByIndex(_owner, index);
}
return result;
}
}
functionchildContractOfToken(uint256 _tokenId)
externalviewreturns (address[] memory)
{
uint256 childCount = totalChildContracts(_tokenId);
if (childCount ==0) {
returnnewaddress[](0);
} else {
address[] memory result =newaddress[](childCount);
uint256 index;
for (index =0; index < childCount; index++) {
result[index] = childContractByIndex(_tokenId, index);
}
return result;
}
}
functionchildTokensOfChildContract(uint256 _tokenId, address _childAddr)
externalviewreturns (uint256[] memory)
{
uint256 tokenCount = totalChildTokens(_tokenId, _childAddr);
if (tokenCount ==0) {
returnnewuint256[](0);
} else {
uint256[] memory result =newuint256[](tokenCount);
uint256 index;
for (index =0; index < tokenCount; index++) {
result[index] = childTokenByIndex(_tokenId, _childAddr, index);
}
return result;
}
}
functiontokenURI(uint256 _tokenId)
publicviewoverridereturns (stringmemory)
{
returnbytes(_tokenURIs[_tokenId]).length>0
? string(abi.encodePacked(subTokenURI, _tokenURIs[_tokenId]))
: string(
abi.encodePacked(baseTokenURI, Strings.toString(_tokenId))
);
}
}
Contract Source Code
File 14 of 15: Ownable.sol
// SPDX-License-Identifier: MIT// OpenZeppelin Contracts v4.3.2 (access/Ownable.sol)pragmasolidity ^0.8.0;import"./Context.sol";
/**
* @dev Contract module which provides a basic access control mechanism, where
* there is an account (an owner) that can be granted exclusive access to
* specific functions.
*
* By default, the owner account will be the one that deploys the contract. This
* can later be changed with {transferOwnership}.
*
* This module is used through inheritance. It will make available the modifier
* `onlyOwner`, which can be applied to your functions to restrict their use to
* the owner.
*/abstractcontractOwnableisContext{
addressprivate _owner;
eventOwnershipTransferred(addressindexed previousOwner,
addressindexed newOwner
);
/**
* @dev Initializes the contract setting the deployer as the initial owner.
*/constructor() {
_transferOwnership(_msgSender());
}
/**
* @dev Returns the address of the current owner.
*/functionowner() publicviewvirtualreturns (address) {
return _owner;
}
/**
* @dev Throws if called by any account other than the owner.
*/modifieronlyOwner() {
require(owner() == _msgSender(), "Ownable: caller is not the owner");
_;
}
/**
* @dev Leaves the contract without owner. It will not be possible to call
* `onlyOwner` functions anymore. Can only be called by the current owner.
*
* NOTE: Renouncing ownership will leave the contract without an owner,
* thereby removing any functionality that is only available to the owner.
*/functionrenounceOwnership() publicvirtualonlyOwner{
_transferOwnership(address(0));
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
*/functiontransferOwnership(address newOwner) publicvirtualonlyOwner{
require(
newOwner !=address(0),
"Ownable: new owner is the zero address"
);
_transferOwnership(newOwner);
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Internal function without access restriction.
*/function_transferOwnership(address newOwner) internalvirtual{
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
}
Contract Source Code
File 15 of 15: Strings.sol
// SPDX-License-Identifier: MIT// OpenZeppelin Contracts v4.3.2 (utils/Strings.sol)pragmasolidity ^0.8.0;/**
* @dev String operations.
*/libraryStrings{
bytes16privateconstant _HEX_SYMBOLS ="0123456789abcdef";
/**
* @dev Converts a `uint256` to its ASCII `string` decimal representation.
*/functiontoString(uint256 value) internalpurereturns (stringmemory) {
// Inspired by OraclizeAPI's implementation - MIT licence// https://github.com/oraclize/ethereum-api/blob/b42146b063c7d6ee1358846c198246239e9360e8/oraclizeAPI_0.4.25.solif (value ==0) {
return"0";
}
uint256 temp = value;
uint256 digits;
while (temp !=0) {
digits++;
temp /=10;
}
bytesmemory buffer =newbytes(digits);
while (value !=0) {
digits -=1;
buffer[digits] =bytes1(uint8(48+uint256(value %10)));
value /=10;
}
returnstring(buffer);
}
/**
* @dev Converts a `uint256` to its ASCII `string` hexadecimal representation.
*/functiontoHexString(uint256 value) internalpurereturns (stringmemory) {
if (value ==0) {
return"0x00";
}
uint256 temp = value;
uint256 length =0;
while (temp !=0) {
length++;
temp >>=8;
}
return toHexString(value, length);
}
/**
* @dev Converts a `uint256` to its ASCII `string` hexadecimal representation with fixed length.
*/functiontoHexString(uint256 value, uint256 length)
internalpurereturns (stringmemory)
{
bytesmemory buffer =newbytes(2* length +2);
buffer[0] ="0";
buffer[1] ="x";
for (uint256 i =2* length +1; i >1; --i) {
buffer[i] = _HEX_SYMBOLS[value &0xf];
value >>=4;
}
require(value ==0, "Strings: hex length insufficient");
returnstring(buffer);
}
}