How do NFTs work?
Tags: Blockchain, Nft, Smart Contracts, Ethereum

How do NFTs work?

With the recent NFT wave, it’s important to know where and how NFT images and data are stored. In this article, I’ll show you how to get the properties and image of an NFT yourself. We’ll be doing that using OpenSea and Etherscan and calling the contract ourselves. At the end of this tutorial you’ll be able to understand at a high level what goes on behind the scenes when you look at this page:

Overview

Here are the steps we will be going through to get the data for Cool Cat #1149:

1) What is a Token URI?

2) Getting the contract address and token ID from OpenSea

3) Calling the tokenURI function through etherscan

4) Looking at the metadata for Cool Cat #1149

5) Finding the "real" image for Cool Cat #1149

The ERC721 Standard

ERC721 is the name of the most common standard for NFTs. Standards are used so that NFT developers make NFTs that mostly behave the same way, and have certain simliar properties. Don’t worry about what those properties are yet, we’ll talk about those more in depth in another article. All you need to know is that some of these properties allow platforms like OpenSea or Rainbow to look at the NFTs in your wallet and know where to find the attributes and image that represent them.

Using the Token ID to get Image and Information

One of the common properties every NFT under the ERC721 standard must have is a function called tokenURI . Calling a function on a smart contract allows you to execute a task (like minting a token or getting some data). This tokenURI function only gets data, and requires a single input, the ID of a token:

tokenURI(uint256 tokenId)

The result from executing this function tells you where to go to see the info for your NFT.

For a real life example, we’ll use the Cool Cats contract. To find the address of the Cool Cats contract, you can check the OpenSea page for Cool Cat #1149, and go to the “Details” section:

For Cool Cat #1149, you can see the contract address is 0x1a92f7381b9f03921564a437210bb9396471050c , and the token ID is 1149 . The contract address you see on OpenSea is clickable, so click that to get to the etherscan page.

You can also get the contract address and token ID from the OpenSea URL:

Copying and pasting the address from the URL ( 0x1a92f7381b9f03921564a437210bb9396471050c ) into the etherscan search bar will also take you to the right page.

Now, let’s execute the tokenURI(uint256 tokenId) function on the Cool Cats contract. Open up the etherscan page for the Cool Cats contract using any method from above or by clicking this link:

Then, click on “Contract” and then “Read Contract”

The Read Contract section shows you some of the functions you can call, and has a lot of useful info there. The one that matters to us is the tokenURI function.

Scroll down until you see tokenURI on the page, then type in 1149 , since the token we care about is Cool Cat #1149:

The result we get back is https://api.coolcatsnft.com/cat/1149 .

Follow that link, and we can see all the info about Cool Cat number 1149:

The information under “attributes” is what OpenSea uses under the “properties” tab in OpenSea. We call this information the “metadata” for the NFT. You can double check to see that it matches up with the OpenSea page. You can also see the link to the image that gets displayed:

Click on that, and you see the image of Cool Cat #1149:

Great! Now you’ve gone from an opensea page, to the smart contract behind the NFT, and then to the underlying data. The image we see at the end is the “real” image for the NFT, and that is the image OpenSea uses as well. These same steps will work for any ERC721 NFT (art blocks, bored apes, etc…) out there, and you can use this to see where images + metadata are stored for all of your NFTs. All you need to do is replace the contract address with the contract address of your NFT, and plug in the ID of your token.

Thank you for reading! This article is meant to serve as a technological primer for my upcoming articles, so stay tuned to learn more about NFTs.

Got any questions? Feedback? Requests for further articles? DM me on twitter: https://twitter.com/hype_eth