Introduction

Hi, I'm cardene, a software engineer and technical writer focused on blockchain and AI.

I've also authored Japanese technical books on specification-driven development with LLMs and decentralized application (DApp) development.

I also build and publish a variety of tools, libraries, and open-source projects.

In this article, I use ChatGPT's Code Interpreter, a feature that has recently received a lot of attention, to analyze NFT trading data. I wanted to see whether it could be useful for analyzing NFTs, cryptocurrencies, or other on-chain data, so I tried it out and documented the steps and results.

This article demonstrates the workflow rather than presenting a substantive analysis of the data. The interface and documentation described here reflect the tools as they were in July 2023.

Preparing the data

Prerequisites

To analyze data with Code Interpreter, you need to provide a file such as a CSV. Here, we will use Dune Analytics to retrieve the data.

Dune Analytics is a free-to-use blockchain analytics tool. You can query its datasets to retrieve specific data or visualize it in charts. Many users also publish their queries and charts in public dashboards.

It would certainly be faster to visualize and analyze the data directly in Dune Analytics. Still, if we can do the same through text prompts alone, it is worth trying. Let's prepare the data first.

Sign up

Go to the following page.

https://dune.com/browse/dashboards

Click Sign up in the top-right corner. The following screen will appear.

Dune sign-up screen on July 12, 2023, at 22:06:28

Enter the required information and click Create account.

Dune dashboard on July 12, 2023, at 22:10:42

After signing in, click the + button at the top of the screen and select New query.

Dune query editor on July 12, 2023, at 22:13:24

You should see a screen like the one above. The setup is complete.

Running the query

Let's run a query. I prepared the following query to retrieve NFT trading data for Crypto Spells, a blockchain game developed and operated by the company I worked for at the time.

sql
select
  *
FROM
  nft.trades
where
  nft_contract_address = 0x67cBBb366a51FFf9ad869d027E496Ba49f5f6D55

Paste the query into the black area outlined in red below.

Running the NFT trading query in Dune on July 12, 2023, at 22:16:52

After pasting it, click Run, outlined in orange. After about 30 seconds, the results appear in the area outlined in blue. Finally, click the button outlined in green to export the results as a CSV (apologies for all the different colors).

The data is now ready. In the next section, we will analyze it using Code Interpreter.

Analyzing data with Code Interpreter

Let's use Code Interpreter to analyze the data and display it in charts.

Selecting Code Interpreter in ChatGPT on July 12, 2023, at 22:25:23

First, open ChatGPT and enable Code Interpreter, as shown above.

Creating tables

Start by creating tables. Send the following prompt to ChatGPT. The original article included both a Japanese prompt (translated here) and an English version.

This is NFT trading data for the blockchain game Crypto Spells. For this dataset, show the daily trading volume, trading volume by time of day, trading volume by day of the week, the times of day when high-value trades occur, the days of the week when high-value trades occur, and the daily data for days with high-value trades.

This is the transaction data for Crypto Spells, an NFT in the Block chain game. Please give me the "daily trading volume", "trading volume by time of day", "trading volume by day of the week", "time of day when high prices are traded", "day of week when high prices are traded", and "data for each day when high prices are traded" for this data.

Output

It appears that there isn't a 'timestamp' column in the data. In order to provide the information you're asking for, we'll need a timestamp for each transaction. Let's look at the column names in the dataset to identify the right one.

The 'block_time' column appears to be the right one for our purposes, as it likely represents the time each transaction was made on the blockchain. Let's convert this column to datetime format and proceed with the analysis.

The data has been loaded successfully and the 'block_time' column has been converted to datetime format. Now, let's proceed with your requests.

  1. Daily trading volume
  2. Trading volume by time of day
  3. Trading volume by day of the week
  4. Time of day when high prices are traded
  5. Day of the week when high prices are traded
  6. Data for each day when high prices are traded

We can calculate the trading volume by summing the 'amount_usd' column. High prices can be defined as prices above a certain percentile, for example, the 95th percentile.

Let's start with the daily trading volume.

The daily trading volume of Crypto Spells is as follows:

(Note: The dates are in UTC.)

text
Date                     Daily Trading Volume (USD)
2019-07-22 00:00:00+00:00     317.812
2019-07-23 00:00:00+00:00     185.166
2019-07-24 00:00:00+00:00     428.393
2019-07-25 00:00:00+00:00     136.793
2019-07-26 00:00:00+00:00     151.186
...
2023-03-02 00:00:00+00:00       0.000
2023-03-03 00:00:00+00:00       0.000
2023-03-04 00:00:00+00:00       0.000
2023-03-05 00:00:00+00:00       0.000
2023-03-06 00:00:00+00:00    2022.310

Please note that the daily trading volume is the sum of the 'amount_usd' column for each day, and the dates are shown in UTC timezone.

Let's now calculate the trading volume by time of day.

The trading volume by time of day (UTC) is as follows:

text
Hour of Day (UTC)  Trading Volume (USD)
0                    7279.47
1                   18839.87
2                   14098.29
3                   32830.58
4                   30887.26
5                    6311.93
6                   10741.55
7                   12480.51
8                   28290.60
9                   17807.53
10                  24638.26
11                  19830.60
12                  22674.81
13                  16133.86
14                  14193.62
15                  16090.11
16                   2998.62
17                   2555.36
18                    724.34
19                    512.76
20                    500.37
21                  10991.49
22                   5447.27
23                   7654.76

Please note that the time of day is in 24-hour format and is given in UTC. The trading volume is the sum of the 'amount_usd' column for each hour of the day.

Next, let's calculate the trading volume by day of the week.

The trading volume by day of the week (UTC) is as follows:

text
Day of the Week   Trading Volume (USD)
Monday (0)              56962.54
Tuesday (1)             54042.68
Wednesday (2)           46192.10
Thursday (3)            37257.62
Friday (4)              37950.83
Saturday (5)            44206.30
Sunday (6)              47901.73

Please note that the day of the week is represented with Monday as 0 and Sunday as 6. The trading volume is the sum of the 'amount_usd' column for each day of the week.

Next, let's identify the time of day when high prices are traded. As mentioned before, we can define high prices as prices above a certain percentile, for example, the 95th percentile.

The time of day (UTC) when high prices are traded is as follows:

text
Hour of Day (UTC)  Number of High Price Trades
0                           4
1                           7
2                           7
3                          12
4                           9
5                           1
6                           3
7                           5
8                           9
9                           4
10                          8
11                         11
12                          9
13                          5
14                          3
15                          4
16                          1
17                          1
21                          2
22                          2
23                          3

High prices are defined as prices above the 95th percentile. The time of day is in 24-hour format and is given in UTC.

Next, let's identify the day of the week when high prices are traded.

The day of the week when high prices are traded is as follows:

text
Day of the Week   Number of High Price Trades
Monday (0)                     21
Tuesday (1)                    19
Wednesday (2)                  15
Thursday (3)                   13
Friday (4)                     10
Saturday (5)                   17
Sunday (6)                     15

Please note that the day of the week is represented with Monday as 0 and Sunday as 6.

Finally, let's get the data for each day when high prices are traded.

The data for each day when high prices are traded is as follows:

text
Date                     Token ID   Amount (USD)   Number of Items   Amount Original   Amount Raw   Block Number
2019-07-29 00:00:00+00:00  110950004  1037.362           1              4.103           4.103e+18   8243788
2019-08-05 00:00:00+00:00  110700010  1159.000           1              5.000           5.000e+18   8290506
2019-08-06 00:00:00+00:00  110950009  1147.900           1              5.000           5.000e+18   8296732
2019-08-16 00:00:00+00:00  221900016  1957.515           2             10.500           1.050e+19  16718312
2019-08-19 00:00:00+00:00  110700005  1017.300           1              5.000           5.000e+18   8380034
...
2021-11-10 00:00:00+00:00  113400079  1140.583           1              0.240           2.400e+17  13585893
2022-02-01 00:00:00+00:00  133100006  6866.900           1              2.500           2.500e+18  14118037
2022-08-29 00:00:00+00:00  111000013  2910.840           1              2.000           2.000e+18  15432151
2022-10-16 00:00:00+00:00  111100008  5134.800           1              4.000           4.000e+18  15757486
2023-03-06 00:00:00+00:00  221800570  2022.310           2              1.290           1.290e+18  33538143

High prices are defined as prices above the 95th percentile.

Please note that the dates are shown in UTC timezone, the amount is in USD, the number of items represents the number of transactions made on that day, the amount original represents the original transaction amount in the transaction currency, the amount raw represents the raw transaction amount in the lowest denomination of the transaction currency (for example, Wei for Ethereum), and the block number is the blockchain block number for the transaction.

Creating charts

Now let's generate charts.

This is NFT trading data for the blockchain game Crypto Spells. Plot the daily trading volume, trading volume by time of day, trading volume by day of the week, the times of day when high-value trades occur, the days of the week when high-value trades occur, and the daily data for days with high-value trades.

This is the transaction data for Crypto Spells, the NFT in the Block chain game. Please graph the "daily trading volume", "trading volume by time of day", "trading volume by day of week", "time of day when high prices are traded", "day of week when high prices are traded", and "data for each day when high prices are traded" for this data.

Output

Here are the graphs for the requested metrics:

  1. Daily Trading Volume: This graph shows the daily trading volume in USD. The x-axis represents the date, and the y-axis represents the trading volume.

image.png

  1. Trading Volume by Time of Day: This graph shows the trading volume by time of day in UTC. The x-axis represents the hour of the day, and the y-axis represents the trading volume.

  2. Trading Volume by Day of Week: This graph shows the trading volume by day of the week. The x-axis represents the day of the week with Monday=0 and Sunday=6, and the y-axis represents the trading volume.

image.png

  1. Time of Day When High Prices are Traded: This graph shows the number of high price trades by time of day in UTC. The x-axis represents the hour of the day, and the y-axis represents the number of high price trades.

image.png

  1. Day of Week When High Prices are Traded: This graph shows the number of high price trades by day of the week. The x-axis represents the day of the week with Monday=0 and Sunday=6, and the y-axis represents the number of high price trades.

image.png

  1. Data for Each Day When High Prices are Traded: This graph shows the trading volume in USD for each day when high prices were traded. The x-axis represents the date, and the y-axis represents the trading volume.

image.png

Please note that high prices are defined as prices above the 95th percentile.

The charts came out nicely.

Generating more data

Let's ask for some additional results.

This is NFT trading data for the blockchain game Crypto Spells. Generate charts for the following data:

  • Daily trading volume, with the time axis displayed in monthly intervals
  • Trading volume by time of day
  • Trading volume by day of the week
  • Times of day when high-value trades occur
  • Days of the week when high-value trades occur
  • Daily data for days with high-value trades, with the time axis displayed in monthly intervals

Define high-value trades as the top 80% of the data.

Also display the following:

  • The addresses and trading volumes of the top 5% of high-value trades.
  • The earliest transaction date among the top 10% of high-value trades.
Output

Below is an overview of the requested charts and data:

  1. Daily trading volume, with the time axis displayed in monthly intervals: See the first chart below.
  2. Trading volume by time of day: See the second chart below.
  3. Trading volume by day of the week: See the third chart below.
  4. Times of day when high-value trades occur: See the fourth chart below.
  5. Days of the week when high-value trades occur: See the fifth chart below.
  6. Daily data for days with high-value trades, with the time axis displayed in monthly intervals: See the sixth chart below.

The requested addresses and trading volumes for the top 5% of high-value trades, and the earliest transaction date among the top 10% of high-value trades, are also shown below.

Addresses and trading volumes of the top 5% of high-value trades:

Address Trading volume (USD)

text
0x7d238bb0b5b3bf494144fa261d20522f6131b125    13706.963464
0x5df4660295bed7abc783480c7d28d8c015cfb37a    11912.422901
0xaeb92050a7cbe1704c6e299e96712675e21c728d    11573.334000
...
(以下省略)

Earliest transaction date among the top 10% of high-value trades: 2019-07-22 14:36:04 UTC

Additional NFT trading data and charts

Although the session timed out partway through, I managed to obtain some results.

Can we generate SQL automatically?

After trying all this, I started wondering whether we could generate the SQL query automatically. The workflow I had in mind was roughly as follows:

  1. Upload some documentation to Code Interpreter.
  2. Ask it to generate code based on the documentation.
  3. Run the generated code in Dune Analytics.

It seemed plausible, so I gave it a try.

Finding documentation

First, I looked for documentation to upload to Code Interpreter. I found it fairly quickly.

https://github.com/duneanalytics/docs/tree/master

However, at the time, Code Interpreter could only accept one uploaded file at a time. One idea was to combine several Markdown files into one. I tried an online service that could merge files, but I couldn't tell whether it had worked properly, so I abandoned that approach for the time being. I plan to build my own tool for merging them later.

My next idea was to upload only the documentation relevant to the query I wanted to run.

While looking for NFT-related files, I found one explaining how to use the SQL table from the first half of this article.

https://github.com/duneanalytics/docs/blob/master/docs/data-tables/spellbook/top-tables/nft.trades.md

Generating code

This looked promising, so I uploaded the file and tried the following prompt.

Read the contents of the uploaded file. 0x67cBBb366a51FFf9ad869d027E496Ba49f5f6D55 Using the uploaded file as a reference, provide SQL code that retrieves trading data for the contract address above.

Here is the output.

The uploaded file describes the nft.trades database provided by Dune Analytics, which provides standardized NFT trading data. The database aggregates data from various platforms, including OpenSea, Rarible, SuperRare, CryptoPunks, Foundation, and LooksRare.

Based on this information, I suggest the following SQL query to retrieve trading data for the specified contract address.

sql
SELECT *
FROM nft.trades
WHERE nft_contract_address = '0x67cBBb366a51FFf9ad869d027E496Ba49f5f6D55';

The query above retrieves all trading data associated with the specified NFT contract address. This includes the trade timestamp, buyer and seller, trading platform, transaction amount in USD, transaction hash, and other information.

However, this SQL query can only be executed in the Dune Analytics environment and cannot be run directly here. You need to create a Dune Analytics account and execute it on that platform.

The generated query looked good. When I ran it in Dune Analytics, however, I got an error.

Dune indicated that the contract address should not be quoted. I removed the quotation marks and ran the following code again, which successfully returned results.

sql
SELECT *
FROM nft.trades
WHERE nft_contract_address = 0x67cBBb366a51FFf9ad869d027E496Ba49f5f6D55;

Successful query after removing quotation marks from the contract address on July 13, 2023, at 00:49:37

While investigating why the generated code was not fully correct, I noticed that the Dune Analytics documentation appeared to be out of date. Even the SQL in the documentation returned errors, which meant the file I had uploaded to Code Interpreter was not an accurate reference to begin with.

Generating another query

Rather than stopping after generating a single query, I tried another one.

Here is the prompt.

0x67cBBb366a51FFf9ad869d027E496Ba49f5f6D55 Using the uploaded file as a reference, provide SQL code that retrieves trading data for the contract address above over the past year.

Here is the output.

The SQL query to retrieve trading data over the past year for the contract address 0x67cBBb366a51FFf9ad869d027E496Ba49f5f6D55 is as follows:

sql
SELECT *
FROM nft.trades
WHERE nft_contract_address = '0x67cBBb366a51FFf9ad869d027E496Ba49f5f6D55'
AND block_time >= NOW() - INTERVAL '1 year';

This query returns all trades associated with the specified NFT contract address over the past year. The data includes the trade timestamp, buyer and seller, trading platform, transaction amount in USD, transaction hash, and other information.

However, this SQL query can only be executed in the Dune Analytics environment and cannot be run directly here. You need to create a Dune Analytics account and execute it on that platform.

It looked promising, so I ran it in Dune Analytics. The result was another error. What was wrong this time?

I opened ChatGPT in another tab, pasted the error message and the code, and asked for help. It produced the following SQL query.

sql
SELECT
  *
FROM
  nft.trades
WHERE
  nft_contract_address = 0x67cBBb366a51FFf9ad869d027E496Ba49f5f6D55
  AND block_time >= NOW() - INTERVAL '1' year;

I was skeptical, but when I ran it, it worked.

Successful query for the past year on July 13, 2023, at 00:50:18

The number of rows returned dropped considerably, so I may have needed to specify additional parameters. Here too, the issue appeared to stem from outdated official documentation. I had not expected ChatGPT to be more reliable than the official documentation in this case.

Trying again

I asked it to generate one more query.

Here is the prompt.

0x67cBBb366a51FFf9ad869d027E496Ba49f5f6D55 Using the uploaded file as a reference, provide SQL code that retrieves trading data for the contract address above in 2022.

Here is the output.

The SQL query to retrieve trading data for 2022 for the contract address 0x67cBBb366a51FFf9ad869d027E496Ba49f5f6D55 is as follows:

sql
SELECT *
FROM nft.trades
WHERE nft_contract_address = '0x67cBBb366a51FFf9ad869d027E496Ba49f5f6D55'
AND block_time >= '2022-01-01' AND block_time < '2023-01-01';

This query returns all trades associated with the specified NFT contract address in 2022. The data includes the trade timestamp, buyer and seller, trading platform, transaction amount in USD, transaction hash, and other information.

However, this SQL query can only be executed in the Dune Analytics environment and cannot be run directly here. You need to create a Dune Analytics account and execute it on that platform.

Running the query as generated produced another error. I asked ChatGPT about it, and it suggested the following SQL.

sql
SELECT
  *
FROM
  nft.trades
WHERE
  nft_contract_address = 0x67cBBb366a51FFf9ad869d027E496Ba49f5f6D55
  AND block_time >= CAST('2022-01-01' AS timestamp)
  AND block_time < CAST('2023-01-01' AS timestamp);

I ran it, and it worked.

Successful query for trades in 2022 on July 13, 2023, at 00:51:30

Again, the number of rows returned dropped considerably, so additional parameters may have been necessary.

In this case, the official documentation did not contain sample code, and I suspect ChatGPT interpreted part of the documentation differently from what was intended. ChatGPT ended up correcting code that ChatGPT itself had generated. The more I used it, the more it surprised me.

Conclusion

My takeaway from this experiment was how useful ChatGPT could be. I was particularly surprised that it could work around errors in the official documentation.

There are still some issues I would like to revisit, particularly the occasions when it failed to generate charts or tables correctly. Also, without a sufficiently specific prompt, Code Interpreter may try to search the internet instead. It is important to tell it explicitly to use the uploaded file.

Some readers may be wondering whether it would be faster to analyze the data directly in Dune Analytics. Depending on the person, that may indeed be true. My aim here, however, was to demonstrate what Code Interpreter could do through a concrete example of its use.

Final thoughts

This article covered analyzing NFT trading data with ChatGPT's Code Interpreter and using it to generate code automatically.

At the time of writing, Code Interpreter was a new feature. I encourage readers to try it out. There is still a lot to explore, and I intend to keep experimenting with it.

If you have questions, feel free to send me a direct message on Twitter.

Twitter @cardene777