[Ethereum] Check owner of NFT-Token

erc-721nftopenzeppelinsolidity

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 😉

Best Answer

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.

Related Topic