ERC-721 implementation in Blockchain / Solidity - Time & Space Complexity
When working with ERC-721 tokens, it is important to understand how the time to execute key functions changes as the number of tokens grows.
We want to know how the cost of minting, transferring, or checking ownership changes when more tokens exist.
Analyze the time complexity of the following simplified ERC-721 transfer function.
function transferFrom(address from, address to, uint256 tokenId) public {
require(ownerOf[tokenId] == from, "Not owner");
ownerOf[tokenId] = to;
balances[from] -= 1;
balances[to] += 1;
}
This code transfers ownership of a single token from one address to another and updates balances.
Look for loops or repeated steps that depend on input size.
- Primary operation: Direct access and update of token ownership and balances.
- How many times: Each transfer handles exactly one token, so operations happen once per call.
The function always does a fixed number of steps regardless of how many tokens exist.
| Input Size (number of tokens) | Approx. Operations |
|---|---|
| 10 | 4 |
| 100 | 4 |
| 1000 | 4 |
Pattern observation: The number of operations stays the same no matter how many tokens exist.
Time Complexity: O(1)
This means the transfer function runs in constant time, doing the same amount of work no matter how many tokens there are.
[X] Wrong: "Transferring a token takes longer as more tokens exist because the contract must check all tokens."
[OK] Correct: The contract uses direct lookups by token ID, so it does not check all tokens. It only updates the specific token involved.
Understanding how smart contract functions scale with data size is a key skill. It shows you can write efficient blockchain code that saves gas and runs smoothly.
"What if the transfer function needed to check a list of all tokens owned by the sender? How would the time complexity change?"
Practice
What is the main purpose of the ERC-721 standard in blockchain?
Solution
Step 1: Understand ERC-721 token type
ERC-721 defines a standard for unique tokens, unlike fungible tokens which are identical.Step 2: Identify the purpose of ERC-721
It is used to create NFTs, which are unique digital assets on the blockchain.Final Answer:
To create unique, non-fungible tokens (NFTs) -> Option CQuick Check:
ERC-721 = Unique NFTs [OK]
- Confusing ERC-721 with ERC-20 fungible tokens
- Thinking ERC-721 manages contract ownership
- Assuming ERC-721 speeds up transactions
Which of the following is the correct way to inherit OpenZeppelin's ERC721 contract in Solidity?
contract MyNFT is ? {}Solution
Step 1: Identify the correct base contract for NFTs
ERC721 is the OpenZeppelin contract for NFTs, ERC20 is for fungible tokens.Step 2: Confirm inheritance syntax
Usingis ERC721correctly inherits the NFT functionality.Final Answer:
ERC721 -> Option BQuick Check:
Inherit ERC721 for NFTs [OK]
- Using ERC20 instead of ERC721
- Confusing Ownable with ERC721
- Trying to inherit SafeMath for NFTs
What will be the output of the following Solidity function when called?
function mintToken(address to, uint256 tokenId) public {
_mint(to, tokenId);
}
// Assume _mint works as expected and tokenId = 1, to = 0x123...What happens after calling mintToken(0x123..., 1)?
Solution
Step 1: Understand the _mint function
_mint creates a new token with the given tokenId and assigns it to the 'to' address.Step 2: Analyze the function call
Calling mintToken with address 0x123... and tokenId 1 creates that NFT for 0x123....Final Answer:
A new NFT with tokenId 1 is assigned to address 0x123... -> Option AQuick Check:
mintToken creates NFT for 'to' address [OK]
- Thinking _mint burns tokens
- Assuming _mint transfers tokens
- Expecting syntax errors from correct code
Identify the error in this Solidity snippet for minting an ERC-721 token:
function mint(address to, uint256 tokenId) public {
_mint(tokenId, to);
}Solution
Step 1: Check _mint function parameter order
OpenZeppelin's _mint expects (address to, uint256 tokenId).Step 2: Compare with given code
The code uses _mint(tokenId, to), reversing the parameters.Final Answer:
_mint parameters are reversed; should be (to, tokenId) -> Option DQuick Check:
_mint(to, tokenId) correct order [OK]
- Swapping parameters of _mint
- Thinking _mint can't be public
- Confusing tokenId type
You want to create an ERC-721 contract that only allows the contract owner to mint new tokens. Which of these Solidity code snippets correctly enforces this?
contract MyNFT is ERC721, Ownable {
function mint(address to, uint256 tokenId) public {
?
_mint(to, tokenId);
}
}Solution
Step 1: Understand ownership check
Only the contract owner should mint, so check if msg.sender equals owner().Step 2: Analyze each require statement
require(msg.sender == owner(), "Not owner"); correctly checks sender is owner; others check wrong conditions.Final Answer:
require(msg.sender == owner(), "Not owner"); -> Option AQuick Check:
Owner check uses msg.sender == owner() [OK]
- Checking 'to' address instead of sender
- Allowing anyone to mint without checks
- Using wrong require conditions
Ready to prove it?
Pass the test and this pattern moves unknown
