'Optimal way of handling NFT minting queue
I am doing a generative NFT project where the user purchases an NFT pack from (1 to 10) and there will be a service that generates up to 10 NFTs, uploads it to IPFS then returns it to the frontend. Frontend then prompts the user to approve the transaction which then mints the NFT to the user.
This flows takes some time especially at the generating part,
so I was thinking of creating a different flow that has a smart contract that holds a queue array. Instead of generating the NFT first, the user will join the queue in the smart contract, then a service that polls the queue array will start generating and minting anything in the queue.
However, if I do this flow, the service will have to be the one that pays the gas fee when minting,
so I was wondering if anyone could advise me on how to create a queue system in blockchain for this case?
Solution 1:[1]
contract YourContract is Ownable {
uint256 public maxAmount = 10;
uint256 public price = 10 ether;
function mint(address _to, uint256 _mintAmount) public payable {
// you can add more logic
require(_mintAmount > 0);
// Maybe currently user has no enough fund to mint all
// so it mints some now, will do later, depends on your contract logic
require(_mintAmount <= maxAmount);
// owner() is if your contract is inheriting from Ownable
// if you set the owner on your own inside constructor, use `owner`
// you are forcing the callee of the function to pay this amount
if (msg.sender != owner()) {
require(msg.value >= price * _mintAmount);
}
// after callee has paid, run the minting
for (uint256 i = 1; i <= _mintAmount; i++) {
_safeMint(_to, i);
}
}
}
this is the _safeMint in ERC721
function _safeMint(address to, uint256 tokenId) internal virtual {
_safeMint(to, tokenId, "");
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Yilmaz |
