#9 Useful queries part1
In daily data analysis, We usually encounter some common queries, such as tracking the price changes of an ERC20 token, querying the balance of various ERC20 tokens held by a certain address, etc. In the help documentation of the Dune platform, some helpful data dashboards and utility queries sections give some examples, you can refer to them. In this tutorial, we combine some typical needs that we encounter in our daily life, and sort out some query cases for you.
Query the latest price of a single ERC20 token
ERC20 tokens are used in a wide variety of blockchain applications. DeFi initiatives facilitate the trading of ERC20 tokens. Other projects reward their backers, early adopters, and development teams through distribution plans and airdrops in exchange for ERC20 tokens. Price data for several ERC20 tokens may be found on sites like CoinGecko. The 'prices.usd' and 'prices.usd_latest' tables in Dune make it easy for data analysts to retrieve the current market value of the most popular ERC20 tokens on each blockchain. There is a table called prices.usd that keeps track of the minute-by-minute prices of different ERC20 tokens. To facilitate activities like summarization and comparison while researching ERC20 token-related projects, we may pool the pricing data to convert the quantity of different tokens into the amount stated in US dollars.
Get the latest price of a single ERC20 token
The prices in the prices.usd
table are recorded on a minute-by-minute basis. The retrieval of the most recent record is contingent upon the token's symbol and the corresponding blockchain it is associated with. In the event that a contract address is available, it is also possible to use such contract address for querying purposes. The usd_latest
database is designed to store the most recent price of each token. Each token is represented by a single row in the table. The below techniques may be used to get the most recent price of an individual token, using WETH as an illustrative instance. In order to enhance query performance, we restrict the retrieval of the most recent portion of the data, since the pricing information is stored in a single record per token each minute, resulting in a substantial number of records for each token. Intermittently, there may exist a specific temporal lag. In the present case, we retrieve the most recent data entry during the preceding six-hour timeframe to ascertain the obtainability of the pricing.
Use the token value to read the latest price information in the prices.usd
table:
Use the smart contract address of the token to read the latest price in the prices.usd
table:
**Read the latest price information from the tables of prices.usd_latest
: **
The query is simpler to read from the prices.usd_latest
table, but since it is actually a view of the prices.usd
table, it is slightly less efficient to execute. reference source code: prices_usd _latest
Check the latest prices of multiple ERC20 tokens
When we need to read the latest prices of multiple tokens at the same time, the convenience of the prices.usd_latest
table is reflected. Here we take the latest price query of WETH, WBTC and USDC as an example.
Read the latest price information for multiple tokens from the prices.usd_latest
table:
Read the latest price information for multiple tokens from the prices.usd
table:
Because we want to read the latest prices of multiple tokens at the same time, we cannot simply use the limit
clause to limit the number of results to get the desired results. What we actually need to return is to take the first record after each different token is sorted in descending order by the minute
field. In the above query, we used row_number() over (partition by symbol order by minute desc) as row_num
to generate a new column. The values in this column are grouped by symbol
and sorted in descending order by the minute
field - that is, each different token will generate its own row number sequence value such as 1, 2, 3, 4, etc. We put it into a subquery, and filter the record of where row_num = 1
in the outer query, which is the latest record of each token. This method seems a little complicated, but similar queries are often used in practical applications, and new columns are generated through the row_number()
function and then used to filter data.
Query the daily average price of a single ERC20 token
When we need to query the average price of an ERC20 token every day, we can only use the prices.usd
table. By setting the date range of the price to be queried (or taking the data of all dates without adding the date range), summarizing by day, and using the avg()
function to obtain the average value, the price data by day can be obtained. The SQL statement is as follows:
If we need to return other fields at the same time, we can add them to the SELECT list and add them to the GROUP BY at the same time. This is because, when using the group by
clause, fields that appear in the SELECT list must also appear in the GROUP BY clause if they are not aggregate functions. The modified SQL statement is as follows:
Query the daily average price of multiple ERC20 tokens
Similarly, we can query the average price of a group of ERC20 tokens every day at the same time, just put the symbol of the token to be queried into the in ()
conditional clause. The SQL statement is as follows:
Calculate price from DeFi swap records
The price data table prices.usd
on Dune is maintained through spellbook, which does not include price information for all tokens on all supported blockchains. Especially when a new ERC20 token is newly issued and listed on the DEX (such as XEN), Dune's price list will not automatically display this token's data. At this point, we can read the swap data in the DeFi project, such as the Swap data in Uniswap, calculate the exchange price between the corresponding token and USDC (or WETH), and then convert the USDC or WETH price data to get the US dollar price. A sample query is as follows:
The above query is a practical application in the data dashboard of the XEN Crypto project. The reference link is as follows:
data dashboard: XEN Crypto Overview
Query: XEN - price trend
Calculate price from DeFi transaction spells table
If the corresponding DeFi transaction data is already integrated into the dex.trades
table, it will be easier to use this table to calculate the price. We can divide amount_usd
by token_bought_amount
or token_sold_amount
to get the USD price of the corresponding token. Taking USDC-WETH 0.30% under Uniswap V3 as an example, the SQL statement to calculate the latest price of WETH is as follows:
Calculate the price of the native token (ETH)
Taking Ethereum as an example, its native token ETH is not an ERC20 token, so there is no price information of ETH itself in the prices.usd
table. However, WETH tokens (Wrapped ETH) are equivalent to ETH, so we can directly use WETH price data.
Use price data from other blockchains
There is also a trick that can work around when the token price data of the blockchain we want to analyze cannot be found in prices.usd
. For example, the Avalanche-C chain also provides transactions of tokens such as USDC, WETH, WBTC, and AAVE, but they have different token addresses compared to the Ethereum chain. If prices.usd
does not provide the price data of the Avalache-C chain (it should already be supported), we can customize a CTE to map the token addresses on different chains, and then query to obtain the price.
Here's an example query: https://dune.com/queries/1042456
Calculate price from logs
Tip: the content of this section is relatively complicated. If you find it difficult, you can skip it directly.
A special case is when analyzing a new DeFi project or a blockchain newly supported by Dune. At this point, there is no corresponding prices.usd
data, the smart contract of the corresponding project has not been submitted for analysis, and the transaction records have not been integrated into the magic table like dex.trades
. The only thing we can access is the raw data tables such as transactions
and logs
. Therefore, we can first find several transaction records, analyze the detailed information of the event log displayed on the blockchain, determine the data type and relative position contained in the data
value of the event, and then manually analyze the data based on this information to convert the price.
For example, say we need to calculate the price of the $OP token on the Optimism chain, and assuming that all the aforementioned conditions are met, the price must be calculated from the original table of the transaction event log. We first find an exchange transaction record based on the clues provided by the project team (contract address, case hash, etc.): https://optimistic.etherscan.io/tx/0x1df6dda6a4cffdbc9e477e6682b982ca096ea747019e1c0dacf4aceac3fc532f. This is a swap transaction, where the topic1
value of the last logs
log "0xd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822" corresponds to "Swap(address,uint256,uint256,uint256,uint256,address)" method. This can be further verified by querying the decoding.evm_signatures
table (this is because Optimism is an EVM-compatible blockchain that uses the same related functions as Ethereum).
A screenshot of the logs on the blockchain browser is as follows:
The screenshot of evm_signatures signature data query is as follows:
When querying evm_signatures
in the above figure, we did some processing so that the relevant columns of data are displayed from top to bottom. The corresponding SQL statement is:
Combining the above relevant information, we can convert the price by analyzing the Swap records in the event log. In the query below, we take the latest 1000 transaction records to calculate the average price. Since the exchange is bidirectional, it may be exchanged from token0
to token1
or vice versa, we use a case statement to take out different values accordingly to calculate the transaction price. In addition, we did not further obtain the price of USDC for conversion. After all, it is a stable currency and its price fluctuates less. When you need more accurate data, you can refer to the previous example to convert through USDC price information.
Here is an actual case: https://dune.com/queries/1130354
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