How NFTs work

hey guys can anyone help me? I have a question, it may seem very silly to you but I don't really understand how a contract for an nft collection works in the part where the nft is acquired.

I know that mint function will verify some conditions and call _safeMint

`function mint(uint256 _mintAmount) public payable {`
`uint256 supply = totalSupply();`
`require(!paused);`
`require(_mintAmount > 0);`
`require(_mintAmount <= maxMintAmount);`
`require(supply + _mintAmount <= maxSupply);`
`if (msg.sender != owner()) {`
`require(msg.value >= cost * _mintAmount);`
`}`
`for(uint256 i = 1; i <= _mintAmount; i++) {`
`_safeMint(msg.sender, supply + i);`
`}`
`}`

safe mint will call _mint

f

unction _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" ); }

mint will update the balance of the address that called mint function 

`function _mint(address to, uint256 tokenId) internal virtual { require(to != address(0), "ERC721: mint to the zero address"); 
require(!_exists(tokenId), "ERC721: token already minted"); _beforeTokenTransfer(address(0), to, tokenId); _balances[to] += 1; _owners[tokenId] = to; emit Transfer(address(0), to, tokenId); _afterTokenTransfer(address(0), to, tokenId); }`

I know that now in the contract my address will be pointing to the token id, but I don't understand how for example open Sea can know that I have this token. because the function that I called only changed the state of the contract, it didn't give me anything back.

For example if a  mint a nft and go to open sea I can see my nft on my wallet, but how it works???

You are touching the most essential yet probably the most confusing concept about blockchains, token. A token is a metaphorical way of describing a certain type of ownership, for example, in the ERC20 standard, a token is fungile or what is being owned is fungible, and in ERC721, a token is non-fungile or what is being owned is non-fungible. So what is exactly being owned? As you asked, the function didn't give you anything back. The ownership is simply recorded in two mappings _balances and _owners in the smart contract. Updating these two mappings is sufficient to and equivalent with giving the ownership of a token to an owner, but there was never a token or a token-ish thing given to anyone. Token is simply used as an understandable concept to describe the net effect of updating state variables in the contract.

2 Likes

hey maxareo, thank you It looks very clear for me now.

could you answer me another question?

How does Opensea work behind the scenes?

like, how do they change the address that owns the nft when it is sold?