Certainly! Below is an SQL query that will retrieve the list of unique tokens traded by a specified list of Ethereum wallets within a given time range using the dex.trades table from Dune Analytics.
Please replace:
('0xWalletAddress1', '0xWalletAddress2', ...) with your list of wallet addresses.
'2023-09-01' and '2023-10-31' with your desired start and end dates.
-- Replace these with your actual list of wallet addresses
WITH wallet_list AS (
SELECT unnest(ARRAY[
'0xWalletAddress1',
'0xWalletAddress2',
-- Add more wallet addresses as needed
'0xWalletAddressN'
]) AS wallet_address
)
-- Main query to get the list of tokens traded by the wallets
SELECT DISTINCT token_address
FROM (
-- Tokens where the wallets are the buyer/trader
SELECT t.token_a_address AS token_address
FROM dex.trades t
INNER JOIN wallet_list w ON t.trader_a = w.wallet_address
WHERE t.time BETWEEN '2023-09-01' AND '2023-10-31'
UNION
-- Tokens where the wallets are the seller/counterparty
SELECT t.token_b_address AS token_address
FROM dex.trades t
INNER JOIN wallet_list w ON t.trader_b = w.wallet_address
WHERE t.time BETWEEN '2023-09-01' AND '2023-10-31'
) AS tokens_traded
ORDER BY token_address;
Explanation:
-
Wallet List CTE (wallet_list):
- We create a Common Table Expression (CTE) named
wallet_list that contains the list of wallet addresses you're interested in.
- Replace
'0xWalletAddress1', '0xWalletAddress2', ..., '0xWalletAddressN' with your actual wallet addresses.
-
Main Query:
- We select the distinct
token_address from a subquery that unifies the tokens from both sides of the trades involving the specified wallets.
- First Part (Buyer Side):
- We select
token_a_address (the token being bought) from dex.trades where trader_a matches one of our wallets.
- Second Part (Seller Side):
- We select
token_b_address (the token being sold) from dex.trades where trader_b matches one of our wallets.
- Time Range Filter:
- We apply a
WHERE clause to filter trades that occurred between your specified start and end dates.
- UNION Operator:
- We use
UNION to combine the tokens from both buyer and seller sides, ensuring we get unique tokens.
-
Ordering:
- We order the results by
token_address for readability.
Notes:
- Column Names:
- Make sure the column names (
trader_a, trader_b, token_a_address, token_b_address, and time) match those in the actual dex.trades table in Dune Analytics. Adjust them if necessary.
- Time Format:
- Ensure the date strings match the format used in your database (
YYYY-MM-DD). Include time if needed (YYYY-MM-DD HH24:MI:SS).
- Wallet Addresses:
- Wallet addresses should be in lowercase and checksummed if required by your database.
- Performance Considerations:
- For large datasets, consider indexing the
trader_a, trader_b, and time columns to improve query performance.
- Additional Filters:
- You can add more filters to the
WHERE clauses if needed (e.g., filtering by a specific DEX or token).
Example with Specific Wallets and Dates:
WITH wallet_list AS (
SELECT unnest(ARRAY[
'0xabc123...',
'0xdef456...'
]) AS wallet_address
)
SELECT DISTINCT token_address
FROM (
SELECT t.token_a_address AS token_address
FROM dex.trades t
INNER JOIN wallet_list w ON t.trader_a = w.wallet_address
WHERE t.time BETWEEN '2023-10-01' AND '2023-10-31'
UNION
SELECT t.token_b_address AS token_address
FROM dex.trades t
INNER JOIN wallet_list w ON t.trader_b = w.wallet_address
WHERE t.time BETWEEN '2023-10-01' AND '2023-10-31'
) AS tokens_traded
ORDER BY token_address;
Feel free to adjust the query according to your specific requirements and the exact schema of your dex.trades table.