Learn
Alex Langshur
August 30, 2022

Building with Transpose: Create your own NFT Portfolio Tracker

The Transpose beta is finally here, which means widespread access to Web3’s most powerful suite of data APIs. Whether you need NFT, token, ENS, or transaction-level data, Transpose has got you covered with the industry’s most simple and complete offering.

In this first blog post, we’re going to explore how to quickly sign-up and get started on the Transpose API suite. We’ll show how several endpoints in the NFT API can be used together to easily create powerful views of blockchain data. In future blogs, we’ll dig into more advanced use cases.

Background

Since the dawn of the NFT era, NFT data has been incredibly difficult to work with. Performing high-level queries, such as pulling all NFTs owned by a user or retrieving a user’s recent NFT mints, transfers, and sales, has been a virtually impossible task.

Previously, to acquire this data, you would need to painstakingly track all NFT transfer events directly from a node and manually piece together a comprehensive story of NFT ownership. Today, better solutions exist for the job but even these are far from being truly developer-friendly.

We’ll demonstrate how Transpose can be used to quickly render incredibly powerful views of an account’s recent NFT activity. Specifically, given an account address or ENS name, we’ll derive a comprehensive view of every NFT the account has interacted with over the last month, including their history of mints, transfers, burns, and sales for each of these NFTs. We’ll call this the “NFT Resume”.

Getting Started

First things first, we need to sign-up for Transpose and claim ourselves a free API key. The Transpose free tier provides a whopping 100k requests per month, so we’ll have more than enough for this demo.

Head to app.transpose.io and click “Continue with Google” to get started. Once logged in with Google, you’ll be directed to name your team (can be anything), invite team members (can click “Skip”), select your tier (choose “Free Tier”), and name your first API key (can be anything). You’ll then be sent to main API dashboard, where you can manage your team, modify tiers, add API keys, and track granular API usage.

View of the main Transpose API dashboard

That’s it! You’ve successfully signed up for the Transpose API. Simply click the “Copy” button next to your first API key and we’re good to go.

Connecting to the API

To promote the most simple development experience possible, the Transpose API follows a straightforward RESTful architecture. Every combination of query parameter is fully indexed for every endpoint and will generate near-instantaneous response times (averaging 50ms up to 200ms in the worst-case). To peruse the full university of functionality that the API suite has to offer, check out the docs at docs.transpose.io.

In the pursuit of further simplicity, Transpose provides a Python SDK that wraps the REST API endpoints. In this post, we’ll leverage the Python SDK to build our NFT resume with the fewest possible lines of code.

To install the Transpose Python SDK, simply run the following command to install with Pip, Python’s package manager (this will also work in a virtual environment):

In a Python file or a Jupyter notebook, connect to the Transpose API through the Python SDK with the following lines:

If these two lines ran without error, you’ve successfully connected to the Transpose API! To dive deeper into the Transpose Python SDK, you can find all the documentation here.

Retrieving All Your NFTs

Let’s start by pulling a list of every single NFT that an account owns. This can be done in a single paginated query to the Transpose API and supports both Ethereum addresses and ENS names:

In the lines above, transpose references the Transpose API object we instantiated before, nft references the Transpose NFT API (as opposed to block, ens, and token), and nfts_by_owner references the endpoint we’re calling. We use to_dict on the response to convert it to a simple list of dictionaries.

This call with produce a response with the following format:

Retrieving All Your NFT Mints, Sends, and Burns

Following a similar approach above, let’s use a single call to pull every NFT you minted, sent, or burned over the last month.

First, let’s get a Python datetime corresponding to exactly a month ago from today. We’ll use this to tell the Python SDK that we only want NFT activity after this date.

Now, let’s pull all NFT activity with a single call to the Transpose Python SDK, telling the endpoint we only want activity performed by the target account after the date we calculated above.

The response is once again a list of Python dictionaries, but now with the following response format (where the category field is one of mint, send, and burn).

Retrieving All Your NFT Sales

Once again, we can use a single endpoint from the NFT API to retrieve an accounts sales across all major Ethereum NFT exchanges. Transpose provides the most complete view of NFT sales data, fully standardized to a single simple data model.

The response format for NFT sales is the same easy-to-understand data model across every. single. exchange. The following snippet demonstrates this.

Putting It All Together

Let’s put it all together!

Our first step is to convert both NFT transfers (i.e. mints, sends, and burns) and NFT sales into a single, standardized sentence for all activity types. Here are some examples:

Let’s start by doing so with NFT transfers. We’ll simply iterate over all of our NFT transfers and sort them into a dictionary that maps the NFT ID (a tuple of the NFT contract address and token ID) to a list of all the standardized activity sentences. We bucket each NFT transfer into an activity type of either minted, burned, sent, or received.

We can do the exact same process with NFT sales to standardize each sale into an activity type of either bought or sold.

Our final step is to sort all the activity by time for each individual NFT.

Outputting Results

Now, all we need is some simple code for formatting all the data we’ve aggregated above and outputting a cleaned NFT resume. The code below will do so by first printing the target wallet address, their entire ownership (NFT portfolio), and then any activity for any of the NFTs they’ve touched in the last month.

We also pull in NFT names by pinging the nfts_by_token_id endpoint for each NFT that has been added to the nft_activity_map:

This produces a clean, simple output such as the one below:

Conclusion

And there we have it. Our wallet resume! If you would like to look at the final code, there is a Jupyter notebook here.

There are many, many ways you can improve on this initial implementation, and I’d love to see you do so. Here are some ideas:

  • Support for wallets that contain more than 500 NFTs or have more than 500 items of activity in the last month (this can be done using pagination with the Transpose API)
  • Removing transfers that have corresponding sales so we don’t double count items of activity (can be done by checking overlapping block_number and log_index values on activity)
  • Adding profit calculations for NFTs that are bought and sold within the last month. Would also be interesting to just pull purchases for all NFTs that were sold within the last month and calculate the total USD-revenue for the month.
  • Calculate mint prices by looking at the transactions that mints occurred within (can be done with the Transpose Block API) and calculate revenue from flipping minted NFTs in secondary markets.
  • Build a front-end UI and interesting activity graphs for all this data! This is a product that many would use!

Return