How To Read A Smart Contract On Etherscan
What is a smart contract? A basic definition is: A smart contract is a tamper-proof program that runs on a blockchain network when certain predefined conditions are satisfied.
What does that mean? Smart contracts, simply put, are programs. The code is transparent and often immutable or unchangeable. Smart contracts are hosted and executed on blockchain networks. Knowing what smart contracts are is great, but how do you read a smart contract to see what it does?
How To Read a Smart Contract
In this guide, we will look at the contracts for a popular NFT project, Doodles, and how to read these smart contracts on Etherscan. By looking at the project on OpenSea, we can investigate one of the NFTs to find the contract powering the NFT project. There are a few steps to get to the contract and read it.
Finding the Project’s Contract
Within an NFT project on OpenSea, you can view a single item. Then, under “Details,” you’ll find a link to the contract address. This link will take you to Etherscan, which is a blockchain explorer. Essentially, it allows you to view information about the Ethereum blockchain.
Checking For Verified Contracts
Once you know the contract’s address, you can view it on Etherscan. The contract address for the Doodles contract is 0x8a90CAb2b38dba80c64b7734e58Ee1dB38B8992e
. If the contract’s code isn’t verified, there isn’t much you can do on Etherscan. While the byte code for the contract is available there and it is possible to decompile it, that is outside the scope of this tutorial. If you don’t see the green checkmark next to “Contract,” the code hasn’t been verified.
Reading a Verified Contract
If the contract is verified, you should be able to look at the code in a more human-readable format. If you click on the “Contract” tab, you will be taken to the code for the contract.
Depending on the method of verification the contract’s author used, you may see one big file, which is a concatenation of all the contracts used by the smart contract or, as is the case with Doodles, separate files. Multiple contracts are seen here because a single smart contract often imports other contracts. This allows contracts to reuse verified contracts such as the OpenZeppelin ERC-721 and the Ownable contracts we see in the Doodles project.
At this point, you can read through all of the code that powers the Doodles NFT project to ensure that it functions as expected.
Interacting With the Contract
If you’d like to interact with a contract through Etherscan, that’s also possible if the contract is verified. While mainly outside the scope of this post, the “Read Contract” and “Write Contract” tabs provide access to the functions of the smart contract. Anyone has access to read functions and can execute them for free. Write functions change the blockchain, which requires gas and may come with other requirements such as ownership.
Why Reading Smart Contracts Is Important
The fact that smart contracts are publicly available to read is one of the advantages of using them. The average user can see the code powering the contract, meaning they can ensure the contract does what it says it does. This reduces the need to trust in the developer.
Finding the contract code and reading it is just the start. To better understand what is happening within the contract, you will need a basic understanding of Solidity.