DataStax News: Astra Streaming now GA with new built-in support for Kafka and RabbitMQ. Read the press release.
The DataStax Blog
ExploreBlog Home
Tracking NFT Transfers using Astra DB and Web3.js
This is the first post in our brand new Astra DB Crypto Blog Series - 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 further into that last point.
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
Pulling NFT Transfers
Now let’s go through the steps to pull NFT transfers and grab the metadata that you’ll store in Astra DB later on.
- Listen for new blocks to be mined, get the block number for the newly minted block.
|
- Get the block details using the block number.
|
- Grab the transaction receipts by iterating through the block transactions.
|
- 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.
|
- 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.
|
- 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”.
Figure 1: Screenshot of Astra DB dashboard option to create a database.
- Enter the database name and keyspace name, then select a provider and region.
Database Name: NFT
Keyspace: eth
Provider: Google Cloud
Region: us-east1
Figure 2: Screenshot of new database information.
- Next, navigate to the Data Loader and load the sample dataset, which in this case is Ethereum NFTs.
Figure 3: Screenshot of options to load a dataset.
- Select the partition key and clustering column, and finish loading the table. Use the following information:
Partition Key: contact_address
Clustering Column: token_id
Figure 4: Screenshot of key and clustering fields.
- Navigate to the CQL Console and run queries against the Ethereum NFT dataset you loaded earlier.
Figure 5: Screenshot of CQL Console option.
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.
Figure 6: Screenshot of an example NFT (Source).
|
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 14843498.
Search for all NFTs with name = ‘Okay Duck Yacht Club’
|
This returns 19 results in the dataset, as shown below.
|
And there you have it! You’ve just loaded an entire dataset of NFTs into your Astra DB in just a few steps.
Note that this is just 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.