How to “read” an NFT Smart Contract
Tags: blockchain, web3, nft
Published: 2022-01-12 08:13:24

Why do you want to "read" a smart contract or interact with it? 

You can find detailed data on chain when you "read" smart contracts. You can "write" smart contracts to change data in them if the front-end of a project is down. Having the ability to interact with smart contracts directly gives you a big advantage in web3.

Smart contracts are not business contracts. They are programs running on blockchain. As cantino.eth says in [a tweet thread](https://twitter.com/chriscantino/status/1480300076786274304), to read the smart contract code of a project “gives you insight into the popularity of a project, how distributed its ownership is, and what it’s capable of”.   

Let’s begin to interact with smart contract with a five-part how-to guide: 

1. What is Smart Contract?
2. Read general information
3. Read details of a transaction
4. Read Contract: query data from contract
5. Write Contract: call state-transition functions

---

## 1. What is Smart Contract?

### 1.1 Smart Contract

Ethererum.org explains:

> A "smart contract" is simply a program that runs on the Ethereum blockchain. 
>
> It's a collection of code (its functions) and data (its state) that resides at a specific address on the Ethereum blockchain.

Every token, Fungible token or Non-Fungible token, is backed by a smart contract. For example, [Boring Ape Yacht Club(BAYC)](https://boredapeyachtclub.com/#/home) NFT is based on a ERC721-standard smart contract on Ethereum blockchain.

### 1.2 Find the NFT

You can find BAYC on Opensea marketplace: [https://opensea.io/collection/boredapeyachtclub](https://opensea.io/collection/boredapeyachtclub). 

NBA player Stephen Curry bought BAYC #7990 NFT. Let’s have a look at it: 

![BAYC on opensea](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/awsanepdowzdv5pkeqb5.jpeg) 

[https://opensea.io/assets/0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d/7990](https://opensea.io/assets/0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d/7990)

On the Opensea item page, you can see the image, name, owner, property, and other information of the NFT item as well as its market data such as floor prices, price history and transaction history. 

In the box under the image, you can find information about the smart contract:

- Blockchain: Ethereum
- Smart Contract Address: `0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d`
- Token Standard: ERC-721

If you want to know more about NFT, Opensea provides a great [NFT bible](https://opensea.io/blog/guides/non-fungible-tokens/). 

### 1.3 Use blockchain explorer

Follow the contract address link to explore the BAYC ERC-721  smart contract on Etherscan: [https://etherscan.io/address/0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d](https://etherscan.io/address/0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d) . Etherscan is a blockchain explorer of Ethereum. 

![NFT contract on etherscan](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ohx11tclm34f2ciur0b8.jpeg) 

On Etherscan, we can see detailed data of the smart contract such as latest transactions as well as its source code if it is open source and has been validated on Etherscan. 

To read contract means that we get data from it without changing the data in it. 

To write we will change the data by calling a smart contract function from our wallet.

For smart contracts of ERC20 and ERC721 token, Etherscan provides token tracker pages. We will use token tracker page instead of contract page here. 

Token tracker page of BAYC can be found at: [https://etherscan.io/token/0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d](https://etherscan.io/token/0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d). 

---

## 2. Read  general information

### 2.1 Total Supply, Holders, Transfers

On the BAYC Token page on Etherscan, you can find general information about this NFT collection: 

- Total Supply: 10, 000  BAYC
- Holders: 6,095
- Transfers: 54,828

On the contract page, you can also find some useful analytics at: [https://etherscan.io/address/0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d#analytics](https://etherscan.io/address/0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d#analytics). 

### 2.2 Holders and Distribution

In the holder tab, you can see the top holder of the NFT. Click link "Token Holder Chart", we can see a pie chart of holders. 

![NFT holder](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x5am8yb2hpsgjxl67ftl.jpeg)
 

### 2.3 NFT Token of an address

The user dingaling.eth has 105 BAYC NFT items. Click the link of his name to open a new page and click Inventory tab, and you can see the full list of his 105 BAYC NFTs. In Etherscan, we can filter token by holder’s address.

![dingaling.eth's BAYC](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s27xpgi1r0l5z47ib3x5.jpeg)
 

BAYC Inventory of dingaling.eth: [https://etherscan.io/token/0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d?a=0x54be3a794282c030b15e43ae2bb182e14c409c5e#inventory](https://etherscan.io/token/0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d?a=0x54be3a794282c030b15e43ae2bb182e14c409c5e#inventory). 

---

## 3. Read details of a transaction

When we transfer a token to another's address, a transaction occurs on the blockchain. When we try to mint a NFT on a web page, we send a transaction to the smart contract.

### 3.1 Transfer/Transactions

On the token tracker page, you can see the transactions/transfers of the token. 

There are six rows in the table: 

- Transaction hash (Txn Hash)
- Method of contract we interact with (can be BAYC and others)
- Time Stamp (Age)
- Token Transfer From
- Token Transfer To
- Related Token ID

![token tracker page on etherscan](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tydfhxs9gvv61ygm4xr1.jpeg)
 
### 3.2 Details of a transaction

Let’s have a look at the transaction that Stephen Curry bought BAYC #7990: [https://etherscan.io/tx/0x905f9e8c58d94ca66f4d6d10d57aa612860e9b19ed08eece85862ff730494c27](https://etherscan.io/tx/0x905f9e8c58d94ca66f4d6d10d57aa612860e9b19ed08eece85862ff730494c27)

![transaction details](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/piimzps0g8igb3j03lm2.jpeg)
 

Explain this transaction according to the transaction details: 

- BAYC #7990 transfer from `0x399d07303275719fb83acb468249a9a26ae2f674`(ArtOfBlock) to `0x3becf83939f34311b6bee143197872d877501b11`(Stephen Curry) on `Aug-28-2021 06:55:12 AM +UTC)`.
- This transaction is interacted with an OpenSea contract (`0x7be8076f4ea4a4ad08075c2508e481d6c946d12b`) by calling its function `atomicMatch_`.
- By calling the contract's function `atomicMatch_`, Stephen as the buyer paid 55 ETH($162,584.95), and ArtOfBlock as the seller gave him BAYC #7990 NFT.

---

## 4. Read Contract: query data from contract

To read contract means that we query data from contract and blockchain. Under the hood, we call a function of the contract which does not change its data and blockchain state. To read contract, we don't need to connect to our blockchain wallet.

Go to the contract tab and click "Read Contract" Button. We are taken to: [https://etherscan.io/address/0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d#readContract](https://etherscan.io/address/0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d#readContract). 

We can see the callable functions of BAYC contract which is a standard ERC-721 smart contract. Other than ERC-721 standard functions, there are some customized functions for this smart contract. You can find more about ERC-721 Standard at: [https://docs.openzeppelin.com/contracts/2.x/api/token/erc721](https://docs.openzeppelin.com/contracts/2.x/api/token/erc721).

![read contract on etherscan](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n8uv33lrb2gbypzdxzwk.jpeg) 

### 4.1 Stable parameters

Some of the parameters are stable. When you click related column,  you can see the result directly: 

- MAX_APES: 10000
- REVEAL_TIMESTAMP: 1619820000
- baseURI: ipfs://QmeSjSinHpPnmXmspMjwiXyN6zS4E9zccariGR3jxcaWtq/
- name: BoredApeYachtClub
- symbol: BAYC
- totalSupply: 10000

### 4.2 Query data with parameters

Let's input the address of Stephen Curry(`0x3becf83939f34311b6bee143197872d877501b11`) into `balanceOf` and input the Token ID(#7990) into `ownerOf`.  We can see the results returned by contract functions.

![read NFT contract on etherscan](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p44yr6yqed0v8i6zahoz.jpeg) 

Let me explain it for you:

- Read function `balanceOf()`, the return of `balanceOf(0x3becf83939f34311b6bee143197872d877501b11)` is `1`. Stephen Curry only has one BAYC NFT.
- Read function `ownerOf()`, the return of `ownOf(7990)` is `0x3becf83939f34311b6bee143197872d877501b11` .
- Read function tokenOfOwnerByIndex, the return of `tokenOfOwnerByIndex(0x54be3a794282c030b15e43ae2bb182e14c409c5e,9)` returns `978`. `0x54be3a794282c030b15e43ae2bb182e14c409c5e` is the address of dingaling.eth who has 105 BAYC NFTs. His 10th BAYC NFT is #978.

---

## 5. Write Contract: call state-transition functions

Instead of writing solidity language,  to Write Contract means to interact with the contract by calling state-transition functions (some may prefer to call them "methods") from our wallet. State-transition functions can be called to change the state of the smart contract and the blockchain.

## 5.1 Connect Wallet, usually MetaMask

You can consider this page of etherscan as a DAPP. To write contract, we need to connect wallet to this DAPP. 

Click “Connect to Web3”, and choose MetaMask Wallet in the  popup dialog. You need to confirm wallet connecting in MetaMask. If you choose WalletConnect, you can scan the QR code using mobile wallet app such as imToken, Rainbow, Trust Wallet, etc.  

![Write contract on etherscan](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ojxb4p9hauk4dfnmw1jx.jpeg)
 

### 5.2 Write Contract (call state-change functions) such as MINT

Sometimes we use Etherscan's write contract feature to call state-change functions of a NFT smart contract. The most used function is mint NFT. In BAYC smart contract, the mint function is `mintApe()`.

NFT projects usually provide a web page for users to interact with. Under the hood, we call the mint function of the smart contract. On etherscan, we can call the mint function directly. 

Take Loot NFT for example. Loot and More Loot(MLoot) don't have a web page. Instead, Loot founder Dom Hoffmann asked users to mint on Etherscan. You can still mint MLoot now call `claim()`. 

![mint mloot](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o8nukfz0ya0mv1nu5t09.jpeg)
 

Link MLoot: [https://etherscan.io/token/0x1dfe7ca09e99d10835bf73044a23b73fc20623df#writeContract](https://etherscan.io/token/0x1dfe7ca09e99d10835bf73044a23b73fc20623df#writeContract)

If you have a related NFT token in your wallet, you can do more such as transfer with Write Contract.

Following this five-part tutorial, you may have learned how to read/write (interact with) a smart contract. Now you can start your Web3 journey with this new ability. Enjoy! 

---

Thanks for cantino.eth’s([@chriscantino](https://twitter.com/chriscantino)) thread for inspiring this tutorial.