Is there a way to avoid trading NFTs on standard marketplaces like OpenSea without breaking the erc721 standard? If so, how would you go about it? It is about an NFT that is something like a voucher that can be used 5 times. Over 5 years, once per year. I would like to prevent that someone unknowingly buys a redeemed voucher (for the current year).
2 Answers
You can include checks in your transfer function.
Keep a global map counter with token IDs pointing to the number of transactions per token
mapping(uint256=> uint256) private _tokenTx;
Now, in your transfer function you can use the NFT id, check in the map to see if it's lower than 5, if it is, you fail the tx, otherwise you continue and increase the number
function _transfer(
address from,
address to,
uint256 tokenId
) internal virtual {
require(ERC721.ownerOf(tokenId) == from, "ERC721: transfer from incorrect owner");
require(to != address(0), "ERC721: transfer to the zero address");
**require(_tokenTx[tokenId] <6, "ERC721: can\'t transfer more than 5 times");**
_approve(address(0), tokenId);
_balances[from] -= 1;
_balances[to] += 1;
_owners[tokenId] = to;
**_tokenTx[tokenId] = _tokenTx[tokenId]+1;**
emit Transfer(from, to, tokenId);
}
As for filtering exchanges transfers, you can either keep a dynamic list with the addresses they use, or block the approval processes altogether. The first keeps the standard better but is harder and more expensive to keep up, the second one is a bit more aggressive but will work for all popular exchanges out there

- 145
- 5
-
Thank you! I have already thought of something like that. In my case, an allowlisting for a private marketplace would be more interesting. Then I would not have to maintain the endless list of marketplaces. What does the whole thing look like on the marketplaces? If, for example, OpenSea has the purchase or auction function activated, it fails only at the transfer. The whole thing becomes pretty weird, isn't it? – SLTN98 Mar 24 '22 at 11:26
-
Marketplaces use the transferFrom call, if you want to use a whitelist process you'll have to do it for all your users. Their transfer and payment is done on the same step, it's atomic, so everyone would be notified that the tx failed and no assets would be transferred – Enrique Alcázar Garzás Mar 24 '22 at 14:36
-
@SLTN98 please remember liking/accepting answers that were helpful :) – Enrique Alcázar Garzás Mar 31 '22 at 11:44
Or, if you're using an external link to redirect buyers/traders to the text file that lists the voucher code, all you have to do is replace the voucher code(s) with a message saying that all the vouchers have been redeemed and then save the file. That way, the next time the NFT gets traded and they unlock the link, they'll see the message.
I sure as hell ain't going to waste my time trying to figure out all that coding nonesense...lol.

- 1