#16 Blockchain analysis - polygon
Dune platform has been developing rapidly and currently supports 10 mainstream blockchains, including Layer 1 public chains such as Ethereum, BNB, Polygon, Fantom, and Layer 2 blockchains such as Arbitrum and Optimism that are dedicated to expanding Ethereum. In this tutorial, we will explore how to start analyzing the overview of a blockchain, taking the Polygon blockchain as an example.
Polygon's motto is "Bringing Ethereum to Everyone." Polygon believes that everyone can use Web3. It is a decentralized Ethereum scaling platform that enables developers to build scalable and user-friendly DApps with low transaction fees without compromising security.
Dashboard for this tutorial: Polygon Chain Overview
Contents of the Blockchain Overview Analysis
Our goal is to comprehensively analyze the entire Polygon Chain to understand its current development status. The analysis includes:
Block Analysis: total number of blocks, blocks mined per minute, total gas consumption, average gas consumption, daily (monthly) trend of block generation quantity, etc.
Transaction and User Analysis: total transaction volume, total number of users, transaction quantity per block, comparison of successful and failed transactions, daily (monthly) trend of transaction quantity, daily (monthly) trend of active users, daily (monthly) trend of new users, comparison of new users and active users, etc.
Native Token MATIC Analysis: total circulation supply, holder analysis, top holders, price trend, etc.
Smart Contract Analysis: total deployed smart contracts, daily (monthly) trend of new contract deployments, comparison of transaction volume for the most popular smart contracts, and analysis of development trends.
Block and Gas Consumption Analysis
Total Number of Blocks and Gas Consumption
To understand the total number of blocks and gas consumption in the Polygon Chain, we can write a simple SQL to retrieve the following information: the total number of blocks, the timestamp of the genesis block, the average number of new blocks per minute, the total gas consumption, and the average gas consumption per block.
SQL explanation:
By using the
to_unixtime()
, we can convert date and time to Unix Timestamp values, which allows us to calculate the number of seconds between two date and time values. We can then use this to calculate the average number of new blocks per minute. The corresponding function isfrom_unixtime()
.gas_used
represents the amount of gas consumed, andbase_fee_per_gas
is the unit price per gas. Multiplying them together gives us the gas cost. The native token of Polygon, MATIC, has 18 decimal places, so dividing by 1e18 gives us the final MATIC amount.
The results of this query can be added as Counter-type visualizations and included in a dashboard. The display is as follows:
Query link:https://dune.com/queries/1835390
Daily (Monthly) New Block Generation Trend and Gas Consumption
We can aggregate by date to calculate the daily number of generated blocks and the corresponding gas consumption. To track the change, we first define a CTE to perform daily data statistics. Then, based on this CTE, we use a window function such as avg(blocks_count) over (order by rows between 6 preceding and current row)
to calculate the 7-day moving average. The SQL is as follows:
Add two Bar Chart for the query, displaying "Daily Block Count, 7-day Moving Average, and 30-day Moving Average Block Count" and "Daily Gas Consumption Total and 7-day Moving Average" values. Add them to the dashboard.
Make a Fork of the above query, and modify it slightly to calculate the monthly statistics. Also, change the moving average to consider a period of 12 months. This will give us the monthly new block generation trend.
The visualizations of the two SQL queries added to the dashboard will have the following display. We can observe that the number of new blocks generated remains relatively stable, but the gas fees have significantly increased since 2022, with a brief decline in between and currently approaching the previous high.
Query Link:
Transaction and User Analysis
Total Transaction Volume and User Count
We want to calculate the total number of transactions and the total number of unique user addresses. A CTE can be difined to combine the sender addresses from
and receiver addresses to
using the UNION ALL, and then count the distinct addresses. It's important to note that we're not excluding contract addresses in this analysis. If you wish to exclude contract addresses, you can add a subquery to exclude those addresses found in the polygon.creation_traces
table. Since the data volume is large, we'll represent the values in millions (M). Add a Counter visualization chart for each metric and include them in the dashboard.
Query Link:
Daily (Monthly) Transaction and Active User Analysis
Similarly, by grouping the data by date, we can generate reports for daily transaction volume and the number of active users. By summarizing the data on a monthly basis, we can obtain monthly insights. Below is the SQL query for daily aggregation:
Add Bar Chart for both daily and monthly transaction data, displaying transaction count and active user count. You can use a secondary Y-axis for the active user count, and choose either Line or Area chart. The resulting visualization on the dashboard would be the following:
Query Link:
Active User and New User Statistics Analysis
For a public blockchain, the growth trend of new users is a critical analysis that reflects the popularity of the chain. We can start by identifying the first transaction date for each address (users_initial_transaction
CTE in the query below) and then use it to calculate the number of new users per day. By associating the daily active user data with the daily new user data, we can create a comparative chart. The number of active users for a given day can be obtained by subtracting the number of new users on that day from the daily active user count. Considering the possibility of no new users on certain dates, we use a LEFT JOIN and the coalesce()
to handle potential null values. The SQL query is as follows:
FORK this daily user statistics query, adjust the date to monthly statistics using date_trunc('month', block_time)
. This will enable us to calculate the number of active users and new users per month.
For these two queries, we can add the following visualizations:
Bar Chart: display the daily (or monthly) count of active users and new users. Since the proportion of new users is relatively low, set it to use the secondary Y-axis.
Area Chart: compare the proportion of new users and existing users.
Adding these visualizations to the dashboard will result in the following display:
Query link:
Native Token Analysis
MATIC Price Trend
Dune's Spells prices.usd
provides price of Polygon chain tokens, including the native token MATIC. Therefore, we can directly calculate the average price on a daily basis.
Since the query results are sorted in ascending order by date, the last record represents the average price for the most recent date, which can be considered as the "current price". We can generate a Counter chart for it, setting the "Row Number" value to "-1" to retrieve the value from the last row. Additionally, we can add a Line to display the daily average price for the MATIC token. After adding these charts to the dashboard, the display will be as shown below:
Query link:
Addresses with the highest holdings of the MATIC token
Addresses with the highest holdings of the MATIC token are of interest to us, as they often have the potential to influence the token's price movements. The following query retrieves the top 1000 addresses. MATIC
is the native token of the Polygon chain and the details of its transfers are stored in the polygon.traces
table. Please note that we haven't differentiated between contract and non-contract addresses in this query. Due to the low transaction gas fees on Polygon, we have omitted the calculation of gas consumption for performance reasons.
Considerations in the above query: the value
in the polygon.traces
is of type uint256
, which is a custom type in Dune SQL. If you directly compare it with the numerical value 0, you will encounter a type mismatch error that prevents comparison. Therefore, we use syntax like uint256 '0'
to convert the value 0 into the same type for comparison. Alternatively, you can use type conversion functions like cast(0 as uint256)
. You can also convert the value
to double, decimal, bigint, or other types before comparison, but in such cases, be mindful of potential data overflow issues.
We can further analyze the distribution of MATIC token holdings among the top 1000 addresses based on the above query. We can fork the previous query and make slight modifications to achieve this.
Generate a Bar Chart and a Pie Chart for the above two queries respectively. Add them to the dashboard, and the display is as follows:
Query link:
Smart Contract Analysis
Number of Created and Suicided Contracts
Since we have restricted the values of the type
and specified the sorting order, we can ensure that two records are returned and their order is fixed. Therefore, we can generate Counter-type visualizations for the values in the first and second rows respectively.
Query link:
Daily (Monthly) Contract Created and Suicided Count
We can calculate the daily (monthly) count of newly created and suicided contracts by date. Considering the cumulative count is also valuable, we first use a CTE to calculate the daily count, and then use the window function sum() over (partition by type order by block_date)
to calculate the cumulative count by date. The partition by type
is used to specify separate aggregations based on the contract type.
Similarly, we can adjust the date to monthly and calculate the count of newly created and suicided contracts on a monthly basis.
The above queries generate Bar Chart and Area Chart respectively. After adding them to the dashboard, the resulting display is as follows:
Query link:
Transaction Count Statistics for Top Smart Contracts
The top smart contracts in each blockchain usually generate the majority of transaction counts. We can analyze the top 100 smart contracts with the highest transaction counts. In the output results, we have added a link field for convenience, allowing you to directly query the transaction list for each smart contract by clicking on the link.
Generating a Bar Chart and a Table Chart for this query. Adding them to the dashboard, the display is as follows:
Query link:
Analysis of Daily Transaction Volume for the Most Active Smart Contracts
We can analyze the daily transaction volume for the top smart contracts with the highest cumulative transaction count. This can provide insights into the popularity and lifespan of different smart contracts in different stages. Given the large amount of data, we will only analyze the top 20 contracts.
We first query the top 20 smart contracts with the highest historical transaction volume. Then, we calculate the daily transaction volume for these smart contracts. We add three different types of visualizations for the query:
Bar Chart: displays the daily transaction volume for different smart contracts, stacked together.
Area Chart: displays the daily transaction volume for different smart contracts, stacked together. We set "Normalize to percentage" to adjust the chart to display in percentages.
Pie Chart: compares the cumulative transaction volume percentages for these top 20 smart contracts.
After adding these charts to the dashboard, the result is shown in the following:
Query link:
The most active smart contracts in the last 30 days
In addition to analyzing all historical transaction data, we can also perform a simple analysis on the most active smart contracts in recent. For example, we can analyze the top 50 smart contracts that have been the most active in the last 30 days.
As it is a recent active projects, it may have been newly deployed and launched. Therefore, we have added hyperlinks to the query and created a Table. The display is as follows:
Query link: https://dune.com/queries/1838077
Summary
Above, we have conducted a preliminary analysis of the Polygon Chain from several aspects, including blocks, gas consumption, transactions, users, native tokens, and smart contracts. Through this dashboard, we can gain a general understanding of the Polygon chain. In particular, through the analysis of top smart contracts, we can identify popular projects. This allows us to choose specific projects of interest for further analysis.
So far, SixdegreeLab has completed overview analyses for multiple blockchains, which you can find here:
About Us
Sixdegree
is a professional onchain data analysis team Our mission is to provide users with accurate onchain data charts, analysis, and insights. We are committed to popularizing onchain data analysis. By building a community and writing tutorials, among other initiatives, we train onchain data analysts, output valuable analysis content, promote the community to build the data layer of the blockchain, and cultivate talents for the broad future of blockchain data applications. Welcome to the community exchange!
Website: sixdegree.xyz
Email: contact@sixdegree.xyz
Twitter: twitter.com/SixdegreeLab
Dune: dune.com/sixdegree
Github: https://github.com/SixdegreeLab
Last updated