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, 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:
- What is Smart Contract?
- Read general information
- Read details of a transaction
- Read Contract: query data from contract
- 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) 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.
NBA player Stephen Curry bought BAYC #7990 NFT. Let’s have a look at it:
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.
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 . Etherscan is a blockchain explorer of Ethereum.
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.
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.
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.
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.
BAYC Inventory of dingaling.eth: 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
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
Explain this transaction according to the transaction details:
- BAYC #7990 transfer from
0x399d07303275719fb83acb468249a9a26ae2f674
(ArtOfBlock) to0x3becf83939f34311b6bee143197872d877501b11
(Stephen Curry) onAug-28-2021 06:55:12 AM +UTC)
. - This transaction is interacted with an OpenSea contract (
0x7be8076f4ea4a4ad08075c2508e481d6c946d12b
) by calling its functionatomicMatch_
. - 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.
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.
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.
Let me explain it for you:
- Read function
balanceOf()
, the return ofbalanceOf(0x3becf83939f34311b6bee143197872d877501b11)
is1
. Stephen Curry only has one BAYC NFT. - Read function
ownerOf()
, the return ofownOf(7990)
is0x3becf83939f34311b6bee143197872d877501b11
. - Read function tokenOfOwnerByIndex, the return of
tokenOfOwnerByIndex(0x54be3a794282c030b15e43ae2bb182e14c409c5e,9)
returns978
.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.
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()
.
Link MLoot: 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) thread for inspiring this tutorial.
Top comments (2)
I really want to appreciate the technical efforts of ProAssetRecoveryExpert in helping me recover back my lost bitcoin stolen by scammers. You can reach-out to him via e-mail (proassetrecoveryexpert@gmail.com)
Good article to review our knowledge.
Some comments may only be visible to logged-in visitors. Sign in to view all comments. Some comments have been hidden by the post's author - find out more