// SPDX-License-Identifier: GPL-3.0-or-laterpragmasolidity 0.8.6;import {ERC721} from"../../../external/ERC721.sol";
import {ICrowdfundWithPodiumEditions} from"./interface/ICrowdfundWithPodiumEditions.sol";
/**
* @title CrowdfundWithPodiumEditions
* @author MirrorXYZ
*/contractCrowdfundWithPodiumEditionsisERC721, ICrowdfundWithPodiumEditions{
// ============ Constants ============stringpublicconstant name ="Crowdfunded Mirror Editions";
stringpublicconstant symbol ="CROWDFUND_EDITIONS";
bytes32publicconstant PRODUCER_TYPE ="0x123123";
uint256internalconstant REENTRANCY_NOT_ENTERED =1;
uint256internalconstant REENTRANCY_ENTERED =2;
// ============ Setup Storage ============// The CrowdfundFactory that is able to create editions.addresspublic editionCreator;
// ============ Mutable Storage ============// Mapping of edition id to descriptive data.mapping(uint256=> Edition) public editions;
// Mapping of token id to edition id.mapping(uint256=>uint256) public tokenToEdition;
// The contract that is able to mint.mapping(uint256=>address) public editionToMinter;
// `nextTokenId` increments with each token purchased, globally across all editions.uint256private nextTokenId;
// Editions start at 1, in order that unsold tokens don't map to the first edition.uint256private nextEditionId =1;
// Reentrancyuint256internal reentrancyStatus;
// Administrationaddresspublic owner;
addresspublic nextOwner;
// Base URI can be modified by multisig owner, for intended future// migration of API domain to a decentralized one.stringpublic baseURI;
// ============ Events ============eventEditionCreated(uint256 quantity,
uint256 price,
address fundingRecipient,
uint256indexed editionId
);
eventEditionPurchased(uint256indexed editionId,
uint256indexed tokenId,
// `numSold` at time of purchase represents the "serial number" of the NFT.uint256 numSold,
uint256 amountPaid,
// The account that paid for and received the NFT.address buyer,
address receiver
);
eventOwnershipTransferred(addressindexed previousOwner,
addressindexed newOwner
);
eventEditionCreatorChanged(addressindexed previousCreator,
addressindexed newCreator
);
// ============ Modifiers ============modifieronlyOwner() {
require(isOwner(), "caller is not the owner.");
_;
}
modifieronlyNextOwner() {
require(isNextOwner(), "current owner must set caller as next owner.");
_;
}
modifieronlyMinter(uint256 editionId) {
// Only the minter can call this function.// This allows us to mint through another contract, and// there not have to transfer funds into this contract to purchase.require(
msg.sender== editionToMinter[editionId],
"sender not allowed minter"
);
_;
}
// ============ Constructor ============constructor(stringmemory baseURI_, address owner_) {
baseURI = baseURI_;
owner = owner_;
}
// ============ Setup ============functionsetEditionCreator(address editionCreator_) external{
require(editionCreator ==address(0), "already set");
editionCreator = editionCreator_;
emit EditionCreatorChanged(address(0), editionCreator_);
}
// ============ Edition Methods ============functioncreateEditions(
EditionTier[] memory tiers,
// The account that should receive the revenue.addresspayable fundingRecipient,
// The address (e.g. crowdfund proxy) that is allowed to mint// tokens in this edition.address minter
) externaloverride{
// Only the crowdfund factory can create editions.require(msg.sender== editionCreator);
// Copy the next edition id, which we reference in the loop.uint256 firstEditionId = nextEditionId;
// Update the next edition id to what we expect after the loop.
nextEditionId += tiers.length;
// Execute a loop that created editions.for (uint8 x =0; x < tiers.length; x++) {
uint256 id = firstEditionId + x;
uint256 quantity = tiers[x].quantity;
uint256 price = tiers[x].price;
bytes32 contentHash = tiers[x].contentHash;
editions[id] = Edition({
quantity: quantity,
price: price,
fundingRecipient: fundingRecipient,
numSold: 0,
contentHash: contentHash
});
editionToMinter[id] = minter;
emit EditionCreated(quantity, price, fundingRecipient, id);
}
}
functionbuyEdition(uint256 editionId, address recipient)
externalpayableoverrideonlyMinter(editionId)
returns (uint256 tokenId)
{
return _buyEdition(editionId, recipient);
}
function_buyEdition(uint256 editionId, address recipient)
internalreturns (uint256 tokenId)
{
// Track and update token id.
tokenId = nextTokenId;
nextTokenId++;
// Check that the edition exists. Note: this is redundant// with the next check, but it is useful for clearer error messaging.require(editions[editionId].quantity >0, "Edition does not exist");
// Check that there are still tokens available to purchase.require(
editions[editionId].numSold < editions[editionId].quantity,
"This edition is already sold out."
);
// Increment the number of tokens sold for this edition.
editions[editionId].numSold++;
// Mint a new token for the sender, using the `tokenId`.
_mint(recipient, tokenId);
// Store the mapping of token id to the edition being purchased.
tokenToEdition[tokenId] = editionId;
emit EditionPurchased(
editionId,
tokenId,
editions[editionId].numSold,
msg.value,
msg.sender,
recipient
);
return tokenId;
}
// ============ NFT Methods ============// Returns e.g. https://mirror-api.com/editions/[editionId]/[tokenId]functiontokenURI(uint256 tokenId)
publicviewoverridereturns (stringmemory)
{
// If the token does not map to an edition, it'll be 0.require(tokenToEdition[tokenId] >0, "Token has not been sold yet");
// Concatenate the components, baseURI, editionId and tokenId, to create URI.returnstring(
abi.encodePacked(
baseURI,
_toString(tokenToEdition[tokenId]),
"/",
_toString(tokenId)
)
);
}
// Returns e.g. https://mirror-api.com/editions/metadatafunctioncontractURI() publicviewoverridereturns (stringmemory) {
// Concatenate the components, baseURI, editionId and tokenId, to create URI.returnstring(abi.encodePacked(baseURI, "metadata"));
}
// Given an edition's ID, returns its price.functioneditionPrice(uint256 editionId)
externalviewoverridereturns (uint256)
{
return editions[editionId].price;
}
// The hash of the given content for the NFT. Can be used// for IPFS storage, verifying authenticity, etc.functiongetContentHash(uint256 tokenId) publicviewreturns (bytes32) {
// If the token does not map to an edition, it'll be 0.require(tokenToEdition[tokenId] >0, "Token has not been sold yet");
// Concatenate the components, baseURI, editionId and tokenId, to create URI.return editions[tokenToEdition[tokenId]].contentHash;
}
functiongetRoyaltyRecipient(uint256 tokenId)
publicviewreturns (address)
{
require(tokenToEdition[tokenId] >0, "Token has not been minted yet");
return editions[tokenToEdition[tokenId]].fundingRecipient;
}
functionsetRoyaltyRecipient(uint256 editionId,
addresspayable newFundingRecipient
) public{
require(
editions[editionId].fundingRecipient ==msg.sender,
"Only current fundingRecipient can modify its value"
);
editions[editionId].fundingRecipient = newFundingRecipient;
}
// ============ Admin Methods ============functionchangeBaseURI(stringmemory baseURI_) publiconlyOwner{
baseURI = baseURI_;
}
// Allows the creator contract to be swapped out for an upgraded one.// NOTE: This does not affect existing editions already minted.functionchangeEditionCreator(address editionCreator_) publiconlyOwner{
emit EditionCreatorChanged(editionCreator, editionCreator_);
editionCreator = editionCreator_;
}
functionisOwner() publicviewreturns (bool) {
returnmsg.sender== owner;
}
functionisNextOwner() publicviewreturns (bool) {
returnmsg.sender== nextOwner;
}
functiontransferOwnership(address nextOwner_) externalonlyOwner{
require(nextOwner_ !=address(0), "Next owner is the zero address.");
nextOwner = nextOwner_;
}
functioncancelOwnershipTransfer() externalonlyOwner{
delete nextOwner;
}
functionacceptOwnership() externalonlyNextOwner{
delete nextOwner;
emit OwnershipTransferred(owner, msg.sender);
owner =msg.sender;
}
functionrenounceOwnership() externalonlyOwner{
emit OwnershipTransferred(owner, address(0));
owner =address(0);
}
// ============ Private Methods ============// From https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/Strings.solfunction_toString(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);
}
}
Contract Source Code
File 2 of 4: ERC721.sol
// SPDX-License-Identifier: GPL-3.0-or-laterpragmasolidity 0.8.6;import {IERC721, IERC721Metadata, IERC721Receiver, IERC165} from"./interface/IERC721.sol";
abstractcontractERC165isIERC165{
functionsupportsInterface(bytes4 interfaceId)
publicviewvirtualoverridereturns (bool)
{
return interfaceId ==type(IERC165).interfaceId;
}
}
/**
* Based on: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/ERC721.sol
*/contractERC721isERC165, IERC721{
mapping(uint256=>address) private _owners;
mapping(address=>uint256) private _balances;
mapping(uint256=>address) private _tokenApprovals;
mapping(address=>mapping(address=>bool)) private _operatorApprovals;
functionsupportsInterface(bytes4 interfaceId)
publicviewvirtualoverridereturns (bool)
{
return
interfaceId ==type(IERC721).interfaceId||
interfaceId ==type(IERC721Metadata).interfaceId||super.supportsInterface(interfaceId);
}
functionbalanceOf(address owner)
publicviewvirtualoverridereturns (uint256)
{
require(
owner !=address(0),
"ERC721: balance query for the zero address"
);
return _balances[owner];
}
functionownerOf(uint256 tokenId)
publicviewvirtualoverridereturns (address)
{
address owner = _owners[tokenId];
require(
owner !=address(0),
"ERC721: owner query for nonexistent token"
);
return owner;
}
functiontokenURI(uint256 tokenId)
publicviewvirtualreturns (stringmemory)
{
require(
_exists(tokenId),
"ERC721Metadata: URI query for nonexistent token"
);
stringmemory baseURI = _baseURI();
returnbytes(baseURI).length>0
? string(abi.encodePacked(baseURI, tokenId))
: "";
}
/**
* @dev Base URI for computing {tokenURI}. Empty by default, can be overriden
* in child contracts.
*/function_baseURI() internalviewvirtualreturns (stringmemory) {
return"";
}
functionapprove(address to, uint256 tokenId) publicvirtualoverride{
address owner = ERC721.ownerOf(tokenId);
require(to != owner, "ERC721: approval to current owner");
require(
msg.sender== owner || isApprovedForAll(owner, msg.sender),
"ERC721: approve caller is not owner nor approved for all"
);
_approve(to, tokenId);
}
functiongetApproved(uint256 tokenId)
publicviewvirtualoverridereturns (address)
{
require(
_exists(tokenId),
"ERC721: approved query for nonexistent token"
);
return _tokenApprovals[tokenId];
}
functionsetApprovalForAll(address operator, bool approved)
publicvirtualoverride{
require(operator !=msg.sender, "ERC721: approve to caller");
_operatorApprovals[msg.sender][operator] = approved;
emit ApprovalForAll(msg.sender, operator, approved);
}
functionisApprovedForAll(address owner, address operator)
publicviewvirtualoverridereturns (bool)
{
return _operatorApprovals[owner][operator];
}
functiontransferFrom(addressfrom,
address to,
uint256 tokenId
) publicvirtualoverride{
//solhint-disable-next-line max-line-lengthrequire(
_isApprovedOrOwner(msg.sender, tokenId),
"ERC721: transfer caller is not owner nor approved"
);
_transfer(from, to, tokenId);
}
functionsafeTransferFrom(addressfrom,
address to,
uint256 tokenId
) publicvirtualoverride{
safeTransferFrom(from, to, tokenId, "");
}
functionsafeTransferFrom(addressfrom,
address to,
uint256 tokenId,
bytesmemory _data
) publicvirtualoverride{
require(
_isApprovedOrOwner(msg.sender, tokenId),
"ERC721: transfer caller is not owner nor approved"
);
_safeTransfer(from, to, tokenId, _data);
}
function_safeTransfer(addressfrom,
address to,
uint256 tokenId,
bytesmemory _data
) internalvirtual{
_transfer(from, to, tokenId);
require(
_checkOnERC721Received(from, to, tokenId, _data),
"ERC721: transfer to non ERC721Receiver implementer"
);
}
function_exists(uint256 tokenId) internalviewvirtualreturns (bool) {
return _owners[tokenId] !=address(0);
}
function_isApprovedOrOwner(address spender, uint256 tokenId)
internalviewvirtualreturns (bool)
{
require(
_exists(tokenId),
"ERC721: operator query for nonexistent token"
);
address owner = ERC721.ownerOf(tokenId);
return (spender == owner ||
getApproved(tokenId) == spender ||
isApprovedForAll(owner, spender));
}
function_safeMint(address to, uint256 tokenId) internalvirtual{
_safeMint(to, tokenId, "");
}
function_safeMint(address to,
uint256 tokenId,
bytesmemory _data
) internalvirtual{
_mint(to, tokenId);
require(
_checkOnERC721Received(address(0), to, tokenId, _data),
"ERC721: transfer to non ERC721Receiver implementer"
);
}
function_mint(address to, uint256 tokenId) internalvirtual{
require(to !=address(0), "ERC721: mint to the zero address");
require(!_exists(tokenId), "ERC721: token already minted");
_balances[to] +=1;
_owners[tokenId] = to;
emit Transfer(address(0), to, tokenId);
}
function_burn(uint256 tokenId) internalvirtual{
address owner = ERC721.ownerOf(tokenId);
// Clear approvals
_approve(address(0), tokenId);
_balances[owner] -=1;
delete _owners[tokenId];
emit Transfer(owner, address(0), tokenId);
}
function_transfer(addressfrom,
address to,
uint256 tokenId
) internalvirtual{
require(
ERC721.ownerOf(tokenId) ==from,
"ERC721: transfer of token that is not own"
);
require(to !=address(0), "ERC721: transfer to the zero address");
// Clear approvals from the previous owner
_approve(address(0), tokenId);
_balances[from] -=1;
_balances[to] +=1;
_owners[tokenId] = to;
emit Transfer(from, to, tokenId);
}
function_approve(address to, uint256 tokenId) internalvirtual{
_tokenApprovals[tokenId] = to;
emit Approval(ERC721.ownerOf(tokenId), to, tokenId);
}
function_checkOnERC721Received(addressfrom,
address to,
uint256 tokenId,
bytesmemory _data
) privatereturns (bool) {
if (isContract(to)) {
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;
}
}
// https://github.com/OpenZeppelin/openzeppelin-contracts/blob/7f6a1666fac8ecff5dd467d0938069bc221ea9e0/contracts/utils/Address.solfunctionisContract(address account) internalviewreturns (bool) {
uint256 size;
// solhint-disable-next-line no-inline-assemblyassembly {
size :=extcodesize(account)
}
return size >0;
}
}
Contract Source Code
File 3 of 4: ICrowdfundWithPodiumEditions.sol
// SPDX-License-Identifier: GPL-3.0-or-laterpragmasolidity 0.8.6;interfaceICrowdfundWithPodiumEditions{
structEdition {
// The maximum number of tokens that can be sold.uint256 quantity;
// The price at which each token will be sold, in ETH.uint256 price;
// The account that will receive sales revenue.addresspayable fundingRecipient;
// The number of tokens sold so far.uint256 numSold;
bytes32 contentHash;
}
structEditionTier {
// The maximum number of tokens that can be sold.uint256 quantity;
// The price at which each token will be sold, in ETH.uint256 price;
bytes32 contentHash;
}
functionbuyEdition(uint256 editionId, address recipient)
externalpayablereturns (uint256 tokenId);
functioneditionPrice(uint256 editionId) externalviewreturns (uint256);
functioncreateEditions(
EditionTier[] memory tier,
// The account that should receive the revenue.addresspayable fundingRecipient,
address minter
) external;
functioncontractURI() externalviewreturns (stringmemory);
}