Navigating Smart Contracts on Etherscan
What is a block explorer? How do you read and write smart contract variables?
TLDR:
Etherscan is an Ethereum block explorer, ie. an online navigator for absolutely all the on-chain data that lives on the Ethereum blockchain.
Navigating a Smart Contract is as simple as selecting an etherscan URL. You can see its transaction history, contract data, and more.
Reading and writing to variables in a Smart Contract can be done in the “contract” tab. Connect your wallet and interact.
Every Thursday I am now releasing one of these Substack posts, and with every post I release an accompanying Twitter thread that summarises what was written in the post. For example last week’s post on “Web1 to Web3, a brief history” turned into the following thread:
I’ve been asking the people on Twitter who interact with my threads about what topic to cover each following week. This time around the resounding reply was to cover “Navigating Smart Contracts on Etherscan”.
This is quite a large topic. Previously some of my posts were too long and it would be hard to read, such as “Web3 Security 101”. My goal now is to keep posts to a short 4-5 minute read so I’m going to break up today’s topic into parts.
This week we’ll keep it simple covering the basic concepts, and in some future weeks we’ll get more advanced and dive into some of Etherscan’s other features, plus peak into the smart contracts of some of the most famous NFTs.
What is a block explorer?
To begin with, we need to remember that blockchains are distributed ledgers that store signed and validated transaction data of a cryptocurrency network. In layman’s terms blockchains are essentially the databases storing all the history of transactions within a network like Bitcoin or Ethereum.
Block Explorers are the way for us to view that database, they are the interface that we can use to “explore” the data on the “block”chain - hence the name. They act as navigators for us to traverse the entire blockchain history. With a block explorer you can see everything that’s been written and being written into the blockchain live. Literally every single transaction that’s happened, all the tokens that have been created, all the smart contracts that live on-chain, everything!
On Bitcoin there are a lot of successful block explorers like blockstream.info, bitcoinexplorer.org, blockchain.com, and honestly many more. However, in Ethereum one block explorer has dominated and become synonymous with the network, and that is etherscan.io. It’s worth noting that Etherscan rules the entire EVM block explorer category. All EVM based chains (ie. chains that are built using the same fundamental pieces as Ethereum) have an Etherscan spin-off for them. For example Avalanche and Polygon both have snowtrace.io and polygonscan.com respectively, that were both built by Etherscan.
Blockchains of networks like Ethereum are entirely public so literally all the data is available for anyone with an Internet connection. Etherscan makes it super easy for you to traverse this data and fall down the rabbit hole that is on-chain data.
Above is the front-page of Etherscan where you can see that it shows some of the more basic metrics like gas price, the current price of ETH and its total market-cap. But it also shows more complex data like the latest blocks validated on-chain, the speed of transactions per second, and there’s a wealth of more data hidden behind the tabs in the top-right. There is an insane amount of data available.
There’s simply too much to cover and either way our focus today is on understanding the smart contract interface within Etherscan.
Navigating a Smart Contract
As a quick recap, smart contracts are the code we interact with in a network like Ethereum that creates tokens and NFTs, these pieces of code do most of the magic in this space. Every single smart contract that has been uploaded to the Ethereum network can be found on Etherscan, and assuming the author has uploaded the contract’s metadata you can view the code in a human readable way.
To narrow down the scope from literally every smart contract to a single one, we’ll navigate to the smart contract for our very first project in the Web3 space milliondollartokenpage.com. We built it inspired by milliondollarhomepage.com as a Web3 version for this Web1 site and launched it back in Autumn 2021.
There were two smart contracts as the first one had a few things we wanted to change so we upgraded it, hence we have a version 1 and version 2. Today we’ll look just at v2: https://etherscan.io/address/0x8e720F90014fA4De02627f4A4e217B7e3942d5e8.
When you open v2 the first thing you see in the top-left overview is that this contract holds no tokens or ETH in it. This is because we’ve withdrawn all the ETH out of it but if someone were to mint a new NFT today, the ETH sent to the contract would show up. You’ve also got a link in the middle to who created the contract and the transaction that created it.
The tabs below will default to “Transactions” and if you scroll down you can literally see all the the transactions that have been performed, ie. all the functions that have been called on the contract. The “method” field shows in each case what specific function was called. So for example you can see in the above case from the 2nd line, that the 2nd most recent transaction was a “withdraw” transaction where we withdrew the money from the contract, which happened a full 144 days ago. It also shows the transaction hash (ie. a number that uniquely identifies that transaction) and the block number it was validated in (ie. the block it got saved into the blockchain at), in this case it was block number 15,747,112.
Below that on the 3rd line you can see that “Mint Token Group” function was called from my wallet afox.eth. This function was written in the code such that you can mint several NFTs in one batch. On the end of the line you can see that I sent 0.25 ETH in this transaction, this is because I minted 25 NFTs in that batch that cost 0.01 ETH each. Plus right at the end you can see the gas I spent to execute the transaction was a little over 0.015 ETH.
Selecting the “Contract” tab as above you’ll see the code. Solidity is the language that pretty much every contract you’ll come across in Ethereum has been written in, but the way the language works in itself will be covered in a separate post in the future. For now you can just understand that this view shows all the code that’s been uploaded for that smart contract.
Worth noting is that most smart contracts are a composition of one main contract along with several other battle tested contracts. For example you can see that it says “File 1 of 16” because there are other pieces of code that have been written before which we imported in order to avoid wasting time re-inventing the wheel, or even worse creating unnecessary accidental bugs.
Below is just an example of the many minting functions available in this specific smart contract. You can see all of them call the underlying “_safeMint” function, which is a more complex function that you can take a look at in the code if interested.
Read Smart Contract variables
Most people are not developers so reading the code itself is less interesting. However, even non-coders might want to understand how to interact with the code to see what the current variables are for a given smart contract.
To do this you must first select the “Read contract” tab on the left hand side. That will show you all the functions that have been exposed for others to interact with. In the above example you can see a list of 11 functions, although this contract has 36 functions in total if you scroll down. It’s a pretty complex contract as they come, most basic ERC721s have a lot less functions.
A quick reminder that ERC721 is one of the standards that defines an NFT contract. BAYC and Moonbirds are famous examples of NFTs built on top of the ERC721 standard. Simply put the standard defines a set of functions that the smart contract must implement, the exact ERC721 functions are shown below.
Just by comparing both of the previous images you can already see some of the functions from the standard within the list. For example you can see “balanceOf” and “getApproved” in the top and bottom image, and if you scroll on that page you’ll find the rest.
If we do scroll down to the bottom of this view we can find interesting fixed variables like “totalSupply”, that when you click on will show that only 10,000 NFTs will ever exist in this collection as defined by the code. Plus you can also read the “tokenURI” field for a specific NFT. For example if we read it for NFT number 1 as shown above, you can see that it stores its metadata at an IPFS address:
ipfs://QmZbMDBzhpiK4nHYkbk9ksAmaZt4oB4Ukk9xJid3jzeLiT/1.json. Understanding IPFS itself will be a lesson for another day though.
Write Smart Contract variables
To write to the contract we can go back to the top and select “Write Contract”, which will show a new list of functions. These are the functions that when you call will change the data within the blockchain itself.
In order to call any of these you will need to connect a wallet like Metamask and then sign the transaction generated. When reading a variable, you are just reading what’s already saved into the blockchain, but when writing you need that transaction to be validated and processed by a the network in order for it to actively change what is saved on the blockchain.
If we wanted to change the NFT’s price, which is currently 0.01 ETH, then we could go in and set a new mint price by calling the “setMintPrice” function with a new parameter like 0.1. This would generate a new transaction that you would need to sign and be processed by the network.
However, this specific function was written such that only the contract’s owner can set that variable. So if you did try to call it from any other random account, the transaction would be processed and you would pay gas, but the function would have failed and you would not change the underlying price variable in the blockchain.
Moreover if we want for example to take money out of the contract we can call the “withdraw” function further down. Any money earned by the contract through minting will need to be extracted by calling this. But once again this function has been written such that only the owner of the contract can call it.
Finally, other functions like the ones in the standard that allow transfer of ownership will also have similar limitations that mean only the owner of the specific NFT can call them. Usually when interacting with marketplace contracts like Opensea’s, we will call the “setApprovalForAll” function on an NFT contract and allow Opensea to take ownership of the NFT away from us as soon as there’s a buyer for that NFT.
Well that’s it for today and we’ve barely scratched the surface of what’s possible in Etherscan. This post is probably going to take you more than 4-5 minutes to read, just imagine if we’d gone deeper!
Now though you understand the basics and can confidently navigate a smart contract and read and write to it. Whenever sites like Opensea interact with an NFT they are under the hood just calling these functions like you can do here, so now you’re basically just as powerful as Opensea!
Keep your eyes peeled in coming weeks as we dive into some famous NFT contracts.