3

I have a smartcontract that generates NFTs. I can check who the owner of a NFT is, because by minting, the sender of the payment can be set to be the owner. address to will be the receiver and owner of the token.

function _safeMint(
        address to,
        uint256 tokenId,
        bytes memory _data
    ) internal virtual {
        _mint(to, tokenId);
        require(
            _checkOnERC721Received(address(0), to, tokenId, _data),
            "ERC721: transfer to non ERC721Receiver implementer"
        );
    }

A smartcontract tracks the owner who intially bought or minted the token. But now the owner who has the token in his wallet could send the token by himself to another wallet right? But how does the smartcontract change who the owner is, if the token is send from one wallet to another ? By change i mean the mapping tokenID -> OwnerAddress that is tracked by the smartcontract.

from oppenzeppelin ERC721 this mapping stores the mapping between tokenID and Owner address and the function to check it. How can this method work if the NFT is transfered independently ? or i got it wrong and the token is not send idependently.. and then it makes sense. Because transfering a NFT from one wallet to another wallet is always calling the transferto function of the smartcontract that created/minted the token. is that right?

 // Mapping from token ID to owner address
    mapping(uint256 => address) private _owners;

 /**
     * @dev See {IERC721-ownerOf}.
     */
    function ownerOf(uint256 tokenId) public view virtual override returns (address) {
        address owner = _owners[tokenId];
        require(owner != address(0), "ERC721: owner query for nonexistent token");
        return owner;
    }

So a NFT is just a state of a smartcontract? and i can just check who the owner of a tokenID is by calling the function on the smartcontract which created the Token? And the fact that a wallet can hold NFTs is just a state of a smartcontract that keeps track of the tokens which where created by it and it always knows where the tokens are because all tokens are just states of a smartcontract?! ahh ;)

4
  • 1
    Do you want to check from inside your NFT contract or from outside it? Oct 6, 2021 at 18:46
  • outside. I want to be able to check from a tokenID for example, who the owner of the token is. if i want to create a qr-code for example, and stick it on a artwork, i want to be able to see who the owner is
    – Khan
    Oct 6, 2021 at 21:38
  • i think after formulating the question multiple times i am understanding it better. I just want to be able to track via a QR-Code who the owner of a Token is. Then i was thinking what happens if a Token is transferred to a different wallet without informing the smartcontract, wouldnt the mapping then be wrong? But i guess my misunderstanding was, that a token is nothing idependent. The token is the smartcontract itself. Everytime i transfer a token, even if i do it with metamask, the transfer function from the smartcontract is called.then i understand i can always track the owner of a token
    – Khan
    Oct 6, 2021 at 22:00
  • 1
    Right, you can always track the owner Oct 7, 2021 at 16:39

1 Answer 1

1

if you check the NFT contract on openzeppelin or if you deploy a NFT contract on Remix IDE online, then you can see that there is a function named OwnerOf

 /**
     * @dev See {IERC721-ownerOf}.
     */
    function ownerOf(uint256 tokenId) public view virtual override returns (address) {
        address owner = _owners[tokenId];
        require(owner != address(0), "ERC721: owner query for nonexistent token");
        return owner;
    }

you can call this function with the ID of a token. Since you have to call this function on the contract itself and a smartcontract always knows who owns a Token, you can easily get the address of the token owner. Its basically the same way how your wallet knows what you have in our wallet. The wallet actually does not contain any tokens. You have to ask a specific smartcontract to tell you, if you own any tokens of that contract. Thats the reason why you have to import a smartcontract address in your wallet, if you want to see custom tokens.

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.