I need help building a claim function that lets each wallet address mint exactly one NFT for free (only paying gas). This is for a promotional campaign where users can get their free token directly.
Right now my contract only allows the owner to distribute tokens, but I want regular users to be able to claim their own NFT. This would save me from paying all the gas fees when there are many tokens to distribute.
Here’s my current code using OpenZeppelin:
contract FreeNFTContract is ERC721, ERC721Enumerable, Ownable {
bool public giveawayEnabled = false;
uint256 public remainingFreeTokens = 5;
function distributeFreeNFT(uint qty, address recipient) public onlyOwner {
require(giveawayEnabled, "Giveaway is not enabled yet.");
require(qty <= remainingFreeTokens, "Not enough tokens left.");
require(qty > 0, "Quantity must be at least 1.");
for (uint j = 0; j < qty; j++) {
uint256 newTokenId = totalSupply();
if (newTokenId < remainingFreeTokens) {
_safeMint(recipient, newTokenId);
}
}
remainingFreeTokens = remainingFreeTokens.sub(qty);
}
}
Is there a way to modify this so users can call the function themselves while still limiting one NFT per address?