Nansen Query Documentation
  • Introduction
  • Overview
    • 📍Getting Started
    • 📍Benefits of Curation
  • Data
    • 🗃️Data Sources
    • 🗃️Data Catalog
      • 🗃️Raw Events
      • 🗃️Aggregates
        • Entity Stats
        • Address Stats
        • Token Usage Stats
        • Transaction Stats
        • Address & Their Relationships
      • 🗃️Dex Trades
        • 🗃️Dex Trader P&L
        • 🗃️Dex Trader Use Cases
        • 🗃️Advanced Topics
        • 🗃️Trader P&L FAQ
      • 🗃️Decoded Contracts
      • 🗃️NFTs
      • 🗃️Governance
      • 🗃️Solana
        • 🗃️Solana Aggregates
    • 🗃️Repeated / Nested Fields
    • 🗃️Adding New Projects
    • 🗃️Data Marketplace
  • Web App
    • 🖥️Overview
    • 🖥️Getting Started: Web App
    • 🖥️My Queries
    • 🖥️Query Editor
    • 🖥️Visualizations
    • 🖥️Dashboards
    • 🖥️Dashboard Editor
    • 🖥️Refresh Schedules
    • ⚙️Custom Query APIs
      • ⚙️API Keys
      • ⚙️Execute A Query
      • ⚙️Get Query Results
  • Database Access
    • 📂Database Overview
    • 📂Getting Started: Database
    • 📂Sample Queries
      • 📂Wallet Examples
      • 📂Token Examples
      • 📂Trader P&L
      • 📂Project Examples
      • 📂Solana Queries
      • 📂Blockchain Examples
    • 📂Sample Python Code
      • 📂Wallet Examples
      • 📂Token Examples
    • 📂Cost Optimization
    • 📂Database FAQs
  • Other
    • ⁉️General FAQs
    • 🏷️Wallet Labels
Powered by GitBook
On this page
  1. Database Access
  2. Sample Queries

Token Examples

Replace token_address and the source table with the appropriate values

Top 10 ETH Balance By Wallet

#standardSQL
-- MIT License
-- Copyright (c) 2018 Evgeny Medvedev, evge.medvedev@gmail.com
WITH
double_entry_book AS (
    -- debits
    SELECT to_address AS address, value AS value
    FROM `bigquery-public-data.crypto_ethereum.traces`
    WHERE to_address IS NOT NULL
    AND status = 1
    AND (call_type NOT IN ('delegatecall', 'callcode', 'staticcall') OR call_type IS NULL)
    UNION ALL
    -- credits
    SELECT from_address AS address, -value AS value
    FROM `bigquery-public-data.crypto_ethereum.traces`
    WHERE from_address IS NOT NULL
    AND status = 1
    AND (call_type NOT IN ('delegatecall', 'callcode', 'staticcall') OR call_type IS NULL)
    UNION ALL
    -- transaction fees debits
    SELECT miner AS address, SUM(CAST(receipt_gas_used AS NUMERIC) * CAST(gas_price AS NUMERIC)) AS value
    FROM `bigquery-public-data.crypto_ethereum.transactions` AS transactions
    JOIN `bigquery-public-data.crypto_ethereum.blocks` AS blocks ON blocks.number = transactions.block_number
    GROUP BY blocks.miner
    UNION ALL
    -- transaction fees credits
    SELECT from_address AS address, -(CAST(receipt_gas_used AS NUMERIC) * CAST(gas_price AS NUMERIC)) AS value
    FROM `bigquery-public-data.crypto_ethereum.transactions`
)

SELECT
    address,
    SUM(value) AS balance
FROM double_entry_book
GROUP BY address
ORDER BY balance DESC
LIMIT 10

Result should look something like this:

All Historical Trades for a Token

SELECT
    token_address,
    from_address,
    to_address,
    value,
    DATE(block_timestamp) AS date
FROM
    `bigquery-public-data.crypto_ethereum.token_transfers`
WHERE
    token_address = '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48'
ORDER BY 
    DATE(block_timestamp)DESC
PreviousWallet ExamplesNextTrader P&L

Last updated 2 years ago

📂
📂
Source:
https://medium.com/google-cloud/how-to-query-balances-for-all-ethereum-addresses-in-bigquery-fb594e4034a7