We have all heard about smart contracts but what is their role in NFTs? In Part I of this blog series, we are going to attempt to illustrate how smart contracts (in general) work, get a little insight into the coding logic, and explore some of the functionalities enforced by the coding. In Part II, we will dive further on the coding conventions that are specific to NFTs.
Just like standard software programs, smart contracts are programs that are stored on a blockchain that are executed when specific conditions are met. For example, they can be used to automate conditions present in an agreement such that all users’ requirements are satisfied and without the requirement of validation from any central authority.
A quick note – for smart contracts to run, the blockchain in question has to be Turing–complete, i.e. capable of recognizing conditions that require data-manipulation. Examples of these are Ethereum and Solana, but not the Bitcoin blockchain.
In order to understand the smart contracts role in NFTs, we will dive a little into how they function.
To dive a bit deeper, let’s examine a widely used set of NFT functions written in solidity (the programming language of Ethereum) called the ERC 721 token standard on the Ethereum blockchain. This list of functions are well audited and thus accepted by the community of developers.
To illustrate, let’s examine the IERC721.sol contract stored on the Ethereum blockchain (sol here stands for Solidity). In the contract, we start out defining the required version of Solidity, as pragma solidity. Next, the interface call outlines all the required functions (without defining the function’s actual behavior). This ‘skeleton’ enables contracts written by other developers to be compatible without much effort or requirement to audit the code base. In short, we ‘inherit’ these functions as they are essential to any NFT contract, such that they can be ‘classified’ as an NFT smart contract. Of course, other custom functions can be added above and beyond these to supplement new functionality.
Note that the ERC721 protocol includes many functions that are originally used by fungible tokens like Ether (known as the ERC20 standard).
For the purpose of illustration let’s examine a couple of individual functions, for example, ‘transferFrom’ taken out of OpenZeppelin’s (a library of secure smart contracts) implementation of ERC20 functions. Three key variables are defined, the ‘To’ and ‘From’ addresses and the amount (as an integer). The two sub-functions _spendAllowance and _transfer then facilitate the transfer from the wallet address that is calling this contract (i.e. ‘From’) to the receiver address.
We are going to avoid going through the details of all of these functions as it’s out of this scope (however, if you are really interested we recommend exploring our smart contract course available here). Instead, let’s examine the high level functionality of some of the key functions:
In Part II, we are going to extend on the above functions and see how these are going to be used to create an actual NFT.
Let’s talk Web3! Contact us through [email protected]
©2022. Hyperglade PTE. LTD. All Rights Reserved.