Smart Contracts in NFTs: Part I

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.

Trustless Execution

Any smart contract follows a certain set of functions and rules as ordered below:

  • Two or more parties agree to the conditions and sign of using electronic signature
  • Terms & conditions – the programming conditions for which the contract will execute
  • Contents of the contract – exchange currency (i.e. tokens), metadata (i.e. files pointed to or contained inside the contract)
hyperglade marketplace buy and sell easy nft

Examining basic protocol functions 

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:

  • name and symbol are self explanatory, e.g. ‘Ethererum’ and ‘ETH’, respectively
  • totalSupply is the total number of tokens
  • balanceOf is the balance of the provided address also measured in the smallest unit
  • transferFrom is the function transferring the provided value (again measured in the smallest units) from the sender to the address. Let’s skip the more detailed execution like safeTransferFrom
  • tokenURI — the path to the token metadata (like the image, qualities of the token, etc.)
  • tokenByIndex — returns tokenId of the token at the specified index
  • tokenOfOwnerByIndex — same as tokenByIndex but for the specified owner

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.