0

In an ERC721 contract how to create a status variable that saves -1 to indicate that an nft owner address no longer owns it because it has decided to transfer it to another address.

In order to achieve this result, I tried to replicate an approach found here

I used as follow.

  1. create two mappings

    mapping(address => uint256[]) public userOwnedTokens;
    mapping(uint256 => int256)    public tokenIsAtIndex;
  1. then create a function that update these mapping when a nft token is minted
 
  function createNft(string memory _tokenURI, address to) onlyOwner public returns (uint256) 
    {
        _tokenIds.increment();
        uint256 newTokenId = _tokenIds.current();
        _mint(msg.sender, newTokenId);     
        _setTokenURI(newTokenId, _tokenURI);

        userOwnedTokens[msg.sender].push(newTokenId); 

        int256 arrayLength = int(userOwnedTokens[msg.sender].length);
        tokenIsAtIndex[newTokenId] = arrayLength;
       
        return newTokenId;
    }

  1. So I need to save -1 to indicate that the user moved the nft in the following way:
    // Inside a transfer logic
    uint256 tokenIndex = tokenIsAtIndex[tokenId];
    userOwnedTokens[from][tokenIndex] = -1; // TO DENOTE THAT THE TOKEN HAS BEEN TRANSFERRED, YOU CAN USE ANY OTHER NUMBER

So the the question here is how can I override in example transferFrom or safeTransferFrom in order to set -1 every time someone transfer the nft.

1 Answer 1

0

Would something like that help you?

contract Test {
    mapping(address => mapping(uint256 => bool)) public addTokenButTransferedItOut;

    function transferOut(uint tokenIndex) external {
        addTokenButTransferedItOut[msg.sender][tokenIndex] = true;
    }

    function get(uint tokenIndex) external returns(bool){
        return addTokenButTransferedItOut[msg.sender][tokenIndex];
    }
}

Just using a mapping address to mapping uint to boolean So address -> to tokenId -> to true if that address had the token and transfered it out?

Just an idea, you could also do mapping address to mapping uint to int so you can set it to -1.

Up to you

1
  • hi oliver, thanks for the response. The point here is : if someone transfer the nft using Opensea, how can I save somewhere the fact that the transfer happened. If I use my function, i agree with you...is simple just mapping the transfer out. But if Opensea start a transfer process , I cannot see that transfer. Maybe I have to override erc20 function transferFrom ? Commented Nov 10, 2022 at 12:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.