Tracking NFT Transfers using Astra DB and Web3.js
Author: Alex Leventer
--
This is the first post in our brand new Astra DB Crypto Blog Series where we’ll cover everything you need to know about Web3. For the debut, we’ll introduce you to NFTs and walk you through how you can load them into DataStax Astra DB — a multi-cloud database-as-service (DBaaS) built on Apache Cassandra.
In this first post, we’ll give you a quick introduction to NFTs and how you can use the serverless DataStax Astra DB to access your NFTs easily and at scale. You’ll learn exactly what NFTs are, the types of NFT smart contracts, and how to use the Astra DB data loader to load every NFT ever minted into Astra DB.
Let’s get started.
NFT Basics
NFTs stands for Non-Fungible Token. In short, NFTs are:
- Digital assets representing unique objects like art, tickets, music, and videos
- Purchased and sold using Cryptocurrencies (like Ether)
- Unique and cannot be replaced (no two NFTs are the same)
- Created through a process called minting
- Supported on multiple blockchains. Most NFTs are on the Ethereum blockchain, but other chains like Solana are also supported
- Follow two NFT standards (ERC-721 and ERC-1155)
Let’s dig a little deeper into that last point.
Meet the two NFT standards
Think of standards as a consistent set of APIs for developers to interact with NFTs. The two standards are:
ERC-721 (Single Token Standard)
- A single deployed contract to manage one token
- Example: Bored Ape Yacht Club
- Events: Transfer, Approval, ApprovalForAll
- Functions: balanceOf, ownerOf, safeTransferFrom, transferFrom, approve, setApprovalForAll, getApproved, isApprovedForAll, tokenURI
ERC-1155 (Multiple Token Standard)
- A single deployed contract to manage one or many tokens
- Example: Billionaire NFT Club
- Events: TransferSingle, TransferBatch, ApprovalForAll, URI
- Functions: safeTransferFrom, safeBatchTransferFrom, balanceOf, balanceOfBatch, setApprovalForAll, isApprovedForAll, uri
Now that we’re all on the same page, let’s go through the steps to pull NFT transfers and grab the metadata that you’ll store in Astra DB later on.
Pulling NFT Transfers
- Listen for new blocks to be mined, get the block number for the newly mined block.
2. Get the block details using the block number.
3. Grab the transaction receipts by iterating through the block transactions.
4. The first log of each transaction recipient will contain an event signature. NFTs can be transferred in three ways: TransferSingle, TransferBatch, Transfer. If the decoded log matches one of the three transfer event signatures, you’ve detected an NFT transfer.
You can use a tool like 4Byte.directory to decode signatures.
5. Once you’ve detected a NFT transfer, you can pull additional metadata on the NFT, using the uri
function: tokenUri
(for 721s) or uri
(for 1155s) function.
6. IPFS (the Interplanetary File System) is commonly used to store NFT metadata. You can convert IPFS links to JSON using the ipfs.io
gateway.
You’re done! Now you’re ready to store the NFT metadata with the Astra DB Document API.
You can create a free Astra account or simply clone the NFT dataset to your Astra account with the steps below.
Create a database in Astra DB
- First, head over to the dashboard and hit the button “Create Database”.
2. Enter the database name and keyspace name, then select a provider and region.
Database Name: NFT
Keyspace: eth
Provider: Google Cloud
Region: us-east1
3. Next, navigate to the Data Loader and load the sample dataset, which in this case is Ethereum NFTs.
4. Navigate to the CQL Console and run queries against the Ethereum NFT dataset you loaded earlier.
Now that’s all locked and loaded, we can move onto searching for an NFT in your database.
Search for an individual NFT
Here’s what we want to see when searching for an NFT.
Your output should look like this:
- Search for all NFTs in a block using storage attached indexes
This will return all the NFTs that were transacted during the block 13319231.
- Search for all NFTs with name = ‘Primate Social Society’
This returns the first two results in the dataset, as shown below.
And there you have it! You just loaded an entire dataset of NFTs into your Astra DB in just a few steps.
This is only the tip of the iceberg of how you can wield Astra DB to easily manage NFTs, so stay tuned for the next posts in this Astra DB Crypto Blog Series to learn much more. In the meantime, if you have any questions or need help with any of the instructions above, ping us at hello@datastax.com.
Follow DataStax on Medium for exclusive posts on all things open source, including Pulsar, Cassandra, streaming, Kubernetes, and more. To join a buzzing community of developers from around the world and stay in the data loop, follow DataStaxDevs on Twitter and LinkedIn.