Skip to content
This repository has been archived by the owner on Sep 29, 2023. It is now read-only.

Latest commit

 

History

History
29 lines (17 loc) · 2.84 KB

ERC721Concepts.md

File metadata and controls

29 lines (17 loc) · 2.84 KB

Getting your head into the code

Feel free to skip this section if you've already read about ERC-721 or perused the code on your own.

There's nothing like going straight to the source: https://eips.ethereum.org/EIPS/eip-721#specification. The ERC-721 spec outlines the bare minimum interface and behavior that a contract needs to implement in order to be recognized and treated as an ERC-721 contract by the rest of the web 3 ecosystem (such as OpenSea, block explorers, etc.). There are only a half dozen operative "MUST"s in there, so we've got a lot of leeway. Eventually, we're going to deploy a snazzy, gas-optimized ERC721 with some bonus features. But for now, let's take a look at the stock version, OpenZeppelin's example ERC721

OpenZeppelin's contracts show a straightforward, battle-tested, minimal implementation of the ERC-721 spec. For example, the ERC-721 spec states that safeTransferFrom "Throws if _from is not the current owner." And we can see that the required functionality is implemented on lines 148-150 of OpenZeppelin's ERC721:

        if (previousOwner != from) {
            revert ERC721IncorrectOwner(from, tokenId, previousOwner);
        }

Take a pass through the spec and identify in the OZ code where each requirement is met. It's elegant, simple, and ingenious. NFTs are great!

But we can build something even better.

EVM based blockchains use the concept of gas to address spam and infinite loops. This means that every operation on chain costs real money. So, by making changes at the level of implementation details in your smart contract, you can save your users real money. A penny here and a buck there add up to a lot!

Compare Solady's _ownerOf with OpenZeppelin's. OZ's relies on the functionality that comes for free with solidity, which is convenient, but less gas efficient. Solady's implementation uses assembly to trim the gas costs down, but it comes at the expense of readability.

But don't get nervous! This is just a quick tour through the code we're going to be working on top of. You'll be able to make a custom, interesting NFT with maximally optimized guts without having to get elbows deep in mstores. You won't even have to twiddle any bits by hand unless you want to.

Let's get to it.

Next up:

Setting up your environment