ethereumsoliditynfterc721

Block OpenSea trading


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).


Solution

  • 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