Advanced NFT — Learn to read and interact with contracts
Last week, we learnt about transactions and their different statuses. We even had a deep dive into what went down in a cancelled transaction and how it creates a difference in the gas fees to be paid when compared to a failed or dropped transaction. Transaction is a simple way to describe exchange of Ethereum from one party to another. However, transactions in Ethereum is much more complex than just a monetary exchange. One of Vitalik’s dream of Ethereum when he first considered the blockchain of Bitcoin was that it should be more than just a financial exchange.
“What if transactions have ways of interacting with applications to store and manipulate other kinds of data on top of digital money?” — his dream did come alive in the Ethereum blockchain, enter the smart contract. Smart contracts are programs that are stored on the blockchain which will run it’s program functions when the conditions are met. Smart contracts are also the defining difference between Bitcoin and Ethereum. Bitcoin was not created with the ability to store programs within their blockchain.
Almost every action with an application in the Ethereum blockchain is interaction with a smart contract. Remember minting an NFT? or listing a NFT on OpenSea? or staking an NFT? All these are interactions with the smart contract. Perhaps seeing is believing.
Smart contract in full view
The best place for beginners to start finding and reading contracts is to go to Etherscan. This website returns you information for any Ethereum addresses you have. You can enter your own wallet address in the search bar and you will see everything you have interacted and transacted. Yeah, everything. It is the place that I return to quite often if I needed to CSI someone’s transaction or to find out how the project is doing.
Kind of scary isn’t it? This is because the nature of a public blockchain is that everyone is allowed to see everything on a pseudo-anonymous level. This means that if someone knows who you are based on your address, they can know all of your transactions, transparent for scrutiny. However, if people could not associate you to your address, then you remain anonymous. This is why NFT influencers have multiple wallet addresses, some not publicized because most people might already know main wallet address and follow their buy/sell causing the market to be really speculative, so it makes it difficult when they want to make private plays unaffected by their own influence.
Therefore, it also made sense when you submit a contract’s address, it will return you everything regarding the contract — the code, the transactions that interacted with it, everything.
For NFT projects, you could easily find contract addresses from icy.tools. There are many more advanced methods to scour for the addresses, but this is the most straightforward and simple for now.
I’ve taken the liberty of choosing a contract for us to study together, and so, this is how a contract looks like, fully bare for the world to see.
Once you’ve keyed in the contract address, you will be able to navigate to the “contract” tab. There, you will see the contract in it’s rawest code form. Transactions interact with this.
Interaction with smart contracts
It may look intimidating, but we will show you how it interacts with a more friendly interface. On the contracts tab, you could see 3 subtabs called “Code”, “Read Contract” and “Write Contract”.
The disgustingly cryptic writings you see are the raw codes, therefore in the “Code” section. Trust me, it’s still readable even to those who don’t know how to code. Etherscan has made it simple for it’s users by taking the most essential parts of the code and make them into interactable buttons stored in “Read Contract” and “Write Contract”.
Read Contract
Interactable buttons found in “Read Contract” are simply reading from the contract, these do not require a change in data within the contract so you don’t have to send a transaction — which also means that “Read Contract” functions does not come with a cost to use.
Notice how the functions are readable almost understandable English — name, owner, price, etc. Also, most of the functions in “Read Contract” are simply reading something off the contract, no data is changed, that’s why it does not require you to send a transaction, therefore no cost.
Write Contract
Interactable buttons over at “Write Contract” at the other hand comes with a cost because it requires a change of data as you are writing over data in the contract.
Now, see how the functions are all write-related — approve, addFreeMinter, mintReservedGods, etc. All of which requires some kind of adding or modifying, which is why you will need to send a transaction to interact with the contract and it’s data, therefore incurring the cost of sending a transaction.
So, is minting a read or write? If you’ve chosen write, then you are right. Although the act of minting is to discover or in a wider stretch, to “read”, what NFT we got, in essence it is to “write” a new owner to the digital good be it jpegs or gifs and to transfer it to the new owner. Thus, it’s irrevocably a write function.
What does this method do?
Well, knowing the barely understandable names does not make it any more easy to understand what it actually does. I’ll teach you a way which helped me to understand contracts.
As a developer, I could understand almost immediately from the “Read Contract” and “Write Contract” that the interactable button names are direct references to the name used in the code because of the way it is written. Consider mintReservedGods for example, it does not have spacing, it follows a camel-case typing where it starts with a small-lettered initial, followed by capitalizing the first letter of the following words. This tells me that the code probably used the exact same name and I’ll be able to understand how it works if I just try to understand the code for this particular function. And here’s the important bit that you need to understand: You don’t need to understand the whole contract to know the contract. You only need to know what you need to know.
So let’s take mintReservedGods for example. Let’s go back to the “Code” subtab and look for the function from within the code.
What I did was to do a search (ctrl+f, cmd+f) on the contract code, and typed in the function I was interested keen to find out more — in this case, mintReservedGods.
The closest brackets you see beside the function name is often called the parameters — in simpler terms, it’s the input. In our example, in order to call mintReservedGods, we need to input a count.
What count does it mean? I would suspect that it’s the count of reservedGods that a user want to mint. But you can read the rest of the code to see where this count is called again.
Even if you do not understand code, you will see that count is being used to mint. With this information, it’s already enough for you to head back to “Write Contract” and use the function.
Now, you know what to key in for the count.
I would go deeper if this is an interested study that people want to learn. As far as this article goes, I will only demonstrate briefly and have basic interpretation over code.
Understanding code is useful when you want to know what goes on exactly after you’ve sent a transaction and when it’s mined successfully. My recommendation is to go through a beginners course to learn the basics of JavaScript and Solidity. You might not even need to write, just watch and hear out how some code keywords work. Else, you will always be relying on people’s interpretation of the code.
The most important property of smart contracts
Now that you’ve seen how it works and also learnt how to work it, I want to circle back and conclude this lesson by bringing up the most important property of a smart contract — immutability.
A smart contract is immutable, and immutable means it cannot be changed. If it is immutable, then why are we able to write into a contract? It is because the contract is immutable, but the data in the contract is not.
This means that the contract that is written in it’s code form, once submitted, will never be able to change. No one can change the code from within — it cannot be re-designed.
Let’s take a simple function, pause, to demonstrate.
This pause function is a write function. This means somewhere in the code, there’s a pause switch, possibly to pause something. When the contract is submitted, means the pause switch is now permanently in the contract. But the data of the pause switch is still modifiable. In this case, whether something is paused or not.
We can always flip the pause switch, but we can no longer add a new switch or to remove the pause switch entirely. This is what it means to be able to operate within the design of the contract, but not able to modify the contract itself.
What’s the implication of this? Contract becomes law, or more famously known, code is law. No one can hope to modify the contract once it’s in the blockchain. It becomes the law. Everyone has to abide to it. This is the reason why the blockchain becomes a trustless system, because no one can change it once something’s in it.
It works to preserve a system of trust. Once code dictate that funds cannot be withdrawn and can only be given back to a list of minters, it will be this way. However, it will also render careless code writing and exploits unstoppable since.. code is law. When unintentionally exposing exploits, exploits become law to be exploited as well.
It requires developers to be extremely careful when writing the contract, and probably why it is called a contract — to be developed with care.
Here’s a thought-provoking question to consider, and I hope it sheds light to why scams are running rampant in the NFT space: When project owners promised how the funds received from the NFT mint out is going to be distributed (charity, development, etc), yet the withdraw function in code only points all funds to a single account, is the contract really written to bolster trust?
Join Coinmonks Telegram Channel and Youtube Channel learn about crypto trading and investing
Also, Read
- SmithBot Review | 4 Best Free Open Source Trading Bots
- Leveraged Token | Best Crypto Exchange | Paxful Review
- Crypto arbitrage Guide | How to Short Bitcoin
- Binance Futures Trading | 3Commas vs Mudrex vs eToro
- How to buy Monero | IDEX Review | BitKan Trading Bot
- YouHodler vs CoinLoan vs Hodlnaut | Cryptohopper vs HaasBot