• Stars
    star
    131
  • Rank 275,867 (Top 6 %)
  • Language PLpgSQL
  • License
    MIT License
  • Created over 4 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

dune snippets is a collection of sql queries for duneanalytics.com / Google BigQuery

Dune Snippets

Warning
Abstractions are now 'deprecated', We encourage users to start making the transition to v2 abstractions aka spellbook. see https://github.com/duneanalytics/spellbook/pull/1424

pr/1424 Move Dune's v1 abstractions into the archive folder deprecated-dune-v1-abstractions. Intermediate datasets created by v1 abstractions are still accessible on dune.com. We encourage users to start making the transition to v2 abstractions aka spellbook.

Rename repo from abstractions to spellbook



Autoformat SQL

Snippets of both Dune user submissions and my own. All queries are linted through a GitHub Actions CI workflow


source from Dune Analytics HackMD

Contents

source from Dune Analytics HackMD

πŸ“‘ Dune Documentation

Here are some tips and tricks on how to get started with the data and interface.

πŸ‘‡ Top links

πŸ“š Need some help getting started with queries?

Many non-technical users have mastered Dune with no prior codeing experience. Dune is powered by the very common database PostgreSQL and you can find a ton of resources by searching online, both for getting started with SQL (udemy, codecademy etc.) and for any query specific question 🧠

Dune Analytics TLDR

1. Query human-readable smart contract data with PostgreSQL πŸ”

2. Visualize your findings πŸ“Š

3. Create dashboards and share them with public links 🌎

4. Explore analysis created by other community members. You can fork any query by the click of a button. πŸ‘¨β€πŸ‘©β€πŸ‘¦β€πŸ‘¦

πŸ‘‰ Create a user for free at duneanalytics.com

Table of contents

[TOC]

πŸ—‚ Data tables

You can currently query data from Ethereum mainnet and xdai.

To query xDai data change the data source in the dropdown list above the data table list on the query page.

The most commonly used tables are

  • Event and call data for each project where you typically get the action that occured in the smart contract. Simply search for project name to find the relevant tables.
  • Abstractions/view tables containing abstractions that makes various data very straight forward to query. Examples
    • dex.trades
    • lending.borrow, lending.collateral_change, lending.repay
    • stablecoin.transfer, stablecoin.mint, stablecoin.burn
    • See the full list and add your own via our Github
  • prices.usd which gives you USD price of various assets per minute. Typically joined on minute with the event data and multiplied by the on-chain value (trade, transfer etc).
  • erc20.tokens gives you contract address, symbol and number of decimals for popular tokens. Token value transfers are then divided by 10 to the power of decimals from this table.
  • Ethereum transactions to get ETH transfers. Typically join with event on ethereum.transactions.hash = evt_tx_hash.

Decoded smart contract data

Instead of working with the traces, logs, and receipts, Dune decodes smart contract activity into nice human-readable tables. See the this section for more info.

Submit contracts for decoding at duneanalytics.com/decode.

Abstractions/table views

These are the cleanest and easiest to use tables on Dune. We have abstractions for dex trades in the dex.trades table.

You can also search for view in our table list to find all the various views.

Together with various teams and community members we've created table views that make the blockchain data even easier to work with. This typically entails removing decimals for ETH and token transfers, adding human readable symbols, joining relevant tables together, adding USD value of events and more.

You can always see the underlying tables derived directly from the blockchain if you need more details or are curious about how the views are created: check out our Github, you can even do a pull request to create your own abstractions.

Raw Ethereum data

  • Blocks
  • Logs
  • Transactions
  • Receipts
  • Traces

You probably won't use this too much when doing analysis with Dune (see decoded data), but it's always nice to have just in case.

You can click here to learn more about Ethereum's data structure, but again it's not really needed for using Dune.

Centralised exchanges trading data

Token volume is great, but more often than not you want to know the USD value of smart contract activity.

You can easily get and join that information with onchain data using the data we pull from the coincap API. See how to use it below.

  • prices.usd - assets on Ethereum
    • contract_address
    • price
    • minute
    • symbol (ticker)

Note that WETH can be used for ETH price.

  • prices.layer_1usd - native layer 1 assets
    • price
    • minute
    • symbol (ticker)

Price is the volume-weighted price based on real-time market data every minute, translated to USD.

The data is fetched from the Coinpaprika API.

πŸ‘¨β€πŸ« Tips for querying the data

You can interact with the data tables through our interface at duneanalytics.com.

To create a new query you simply click Create -> Query

On your left you can select which database you want to use in the dropdown list and then see the data tables in the window. Just search for the project you are interested in working with.

Use view abstractions and tables

The easiest way to do great analysis with Dune Analytics is to use prepared views named namespace.view_ or abstraction tables like dex.trades. All the view tables are cleaned and contains data and metadata (like human readable token symbols) that make them very straight forward to query.

Using Inline Ethereum addresses

In Dune Ethereum addresses are stored as postgres bytearrays which are encoded with the \x prefix. This differs from the customary 0x prefix. If you'd like to use an inline address, say to filter for a given token, you would do

WHERE token = '\x6b175474e89094c44da98b954eedeac495271d0f'

which is simply short for

WHERE token = '\x6b175474e89094c44da98b954eedeac495271d0f'::bytea

Quote camel case column and table names

Column and table names are mostly taken directly from smart contract ABIs, with no modification. Since most smart contracts are written in Solidity, and written with a camelCased naming convention, so is many of Dune's table and column names. Postgres requires you to quote columns and tablenames that are case sensitive:

SELECT β€œcolumnName”
FROM projectname.”contractName_evt_EventName”
LIMIT 10

In Postgres, double quotes are reserved for tables and columns, whereas single quotes are reserved for values:

SELECT β€œcolumnName”
FROM projectname.”contratName_evt_eventName”
WHERE contract_address = '\x6B175474E89094C44Da98b954EedeAC495271d0F'
LIMIT 10

Schemas are always lowercase in Dune.

Remove decimals

Ether transfers and most ERC-20 tokens have 18 decimal places. To get a more human readable number you need to remove all the decimals. The table erc20.tokens gives you contract address, symbol and number of decimals for popular tokens. Token value transfers are then divided by 10 to the power of decimals from this table:

transfer_value / 10^erc20.tokens.decimals

Use date_trunc to get time

We've added evt_block_time to decoded event tables for your convenience. A neat way to use it is with the date_trunc function like this

SELECT date_trunc('week', evt_block_time) AS time

Here you can use minute, day, week, month.

How to get USD price

To get the USD volume of onchain activity you typically want to join the smart contract event you are looking at with the usd price and join on minute. Also make sure that asset matches asset.

LEFT JOIN prices.usd p
ON p.minute = date_trunc('minute', evt_block_time)
AND event."asset" = p.contract_address

Then you can simply multiply the value or amount from the smart contract event with the usd price in your SELECT statement: * p.price.

Token symbols

You often want to group your results by token address. For instance you want to see volume on a DEX grouped by token. However, a big list of token addresses are abstract and hard to digest.

Therefore you often want to use the token symbol instead. Simply join the table erc20.tokens with your event table where asset = token address. You then select symbol in your select statement instead of token address.

NB The erc20.tokens table cointains a selection of popular tokens. If you are working with more obscure tokens you should be careful with joining with this table because tokens that are not in the coincap table might be excluded from your results.

🏷 Address Labels

Have you ever made a query on Dune where you get a list of addresses, only to stop and wonder what's behind these beautiful, random hexadecimal encoded strings? So have we.

Address labels is a new feature on Dune where you as a user can add, update and query labels for any address. All for free!

πŸͺ§ What is a label?

A label is a piece of metadata about an address, a tag or metadata if you will. It comes in the form of a key-value pair. The key is the label type, and the value the label name.

Browse addresses and and labels at the labels page.

πŸ–Ό What labels looks like

Check out this dashboard for examples on what can be created with labels.

Address label examples

The address 0xD551234Ae421e3BCBA99A0Da6d736074f22192FF can be labeled

type name
owner binance
wallet type exchange

Because the address is controlled by the exchange Binance.

The address 0xe65040f61701940b62e18da7a53126a58525588b can be labeled

type name
dapp usage uniswap user
activity dex trader

Because the address in the past interacted with Uniswap.

You are free to come up with both new types and label names, as labels on Dune are open ended and crowdsourced πŸŽ‰.

πŸ“₯ Adding labels

There are two ways to add labels:

1. Directly to an address via our labels page

Good for specific labels like "this is a binance wallet"

2. Via a Dune query

Use Dune queries to label addresses 🀯 A very powerful and scalable way to add labels like "all these addresses used Uniswap", and much much more.

Please see our Github for examples of labels created with queries and PR in your own!

Examples of what you can do:

  • Label all addresses that used a certain dapp
  • Label all addresses that hold a certain amount of a token
  • Label all addresses that use a dapp more than X times per month
  • Label all addresses that sent money to Binance

You could also do more novel and involved things around user patterns like who did arbitrage trades or profited from flash loans and so much more.

Note that there might be a few minutes delay from adding the label on duneanalytics.com until you can query it in SQL.

πŸ—„ The labels table

Labels are stored in the new labels.labels table which has the following schema:

CREATE TABLE IF NOT EXISTS labels.labels (
    -- the id of the label is just an incrementing integer (not useful for querying)
    id integer PRIMARY KEY,
    -- the address this label describes
    address bytea NOT NULL,
    -- the actual label
    name text NOT NULL,
    -- the label type
    type text NOT NULL,
    -- the username of the Dune user who created this label
    author text NOT NULL,
    -- the source of this label, autopopulated by dune
    source text NOT NULL,
    -- the last time this label was changed
    updated_at timestamptz NOT NULL
);

πŸ§‘β€πŸ”§ Using labels

Note that this table holds multiple rows per address, and therefore joins against it can be tricky to get right. For that reason we've made the convenient function:

labels.get(address bytea, type text default null) RETURNS text[]

which we anticipate will be the primary way to use labels. See examples below.

Typically if you do a query that returns address you can use labels.get(address) to get all labels for that address independent of label type. If you want to see labels of the type owner you can do labels.get(address, 'owner'). You can also pass this function several label types you want included like: labels.get(address, 'owner', 'project').

We've also added the function labels.url(address bytea). Pass that function an address from your query and your results table will contain a clickable link to for instance:

https://duneanalytics.com/ethereum/address/0xD551234Ae421e3BCBA99A0Da6d736074f22192FF

πŸ“œ Usecase: I want to display labels for a list of addresses

We encourage you to run these queries in Dune while you read this

Say you're looking at the top 10 traders of DAI across all dexes last 24 hours:

SELECT trader_a, SUM(token_a_amount)
FROM dex.trades
WHERE token_a_symbol = 'DAI'
AND block_time > now() - interval '24 hours'
GROUP BY 1
ORDER BY 3 DESC
LIMIT 10;

If you want to have labels for these addresses simply alter the trader_a column to labels.get(trader_a).

Note: In the examples below --- represents lines removed, and +++ lines added.

--- SELECT trader_a, SUM(token_a_amount)
+++ SELECT labels.get(trader_a), SUM(token_a_amount)
    FROM dex.trades
    WHERE token_a_symbol = 'DAI'
    AND block_time > now() - interval '24 hours'
    GROUP BY 1
    ORDER BY 3 DESC
    LIMIT 10;

Now you've replaced the addresses with lists of all labels for trader_a. Sometimes you're only interested in a subset of labels: labels.get accepts an optional list of type names which filter the type of labels you get. Say you're only interested in 'activity' labels:

--- SELECT labels.get(trader_a), SUM(token_a_amount)
+++ SELECT labels.get(trader_a, 'activity'), SUM(token_a_amount)
    FROM dex.trades
    WHERE token_a_symbol = 'DAI'
    AND block_time > now() - interval '24 hours'
    GROUP BY 1
    ORDER BY 3 DESC
    LIMIT 10;

Of course you can also show the address, and filter for multiple label types

--- SELECT labels.get(trader_a, 'activity'), SUM(token_a_amount)
+++ SELECT trader_a, labels.get(trader_a, 'activity', 'project', 'contract_name') as labels, SUM(token_a_amount)
    FROM dex.trades
    WHERE token_a_symbol = 'DAI'
    AND block_time > now() - interval '24 hours'
--- GROUP BY 1
+++ GROUP BY 1, 2
    ORDER BY 3 DESC
    LIMIT 10;

You can also use labels.url to make the addresses clickable:

--- SELECT trader_a, labels.get(trader_a, 'activity') as labels, SUM(token_a_amount)
+++ SELECT labels.url(trader_a), labels.get(trader_a, 'activity') as labels, SUM(token_a_amount)
    FROM dex.trades
    WHERE token_a_symbol = 'DAI'
    AND block_time > now() - interval '24 hours'
--- GROUP BY 1
+++ GROUP BY 1, 2
    ORDER BY 3 DESC
    LIMIT 10;

This way people who look at your dashboard can easily contribute even better labels to it!

🧼 Usecase: I want to filter my query by labels that exist.

In this usecase you wouldn't want to use labels.get, because it can be slow to operate with. Instead you'll use the fantastic EXISTS function in SQL.

As an example: you're querying Uniswap, but are interested in the behavior of users who have traded previously on 1inch. Here's how you'd go about that:

SELECT "to"
FROM uniswap_v2."Pair_evt_Swap"
WHERE EXISTS(
            SELECT *
            FROM labels.labels
            WHERE address="to"
            AND type='dapp usage'
            AND name='1inch user'
            )
LIMIT 10;

The above query will give you 10 address that has swapped on Uniswap and traded on 1inch.

Of course, you can use the two patterns in conjunction! If you are interested for labels on those addresses, go ahead and use labels.get in addition to the WHERE EXISTS pattern:

--- SELECT "to"
+++ SELECT "to", labels.get("to")
    FROM uniswap_v2."Pair_evt_Swap"
    WHERE EXISTS(SELECT * FROM labels.labels WHERE address="to" AND type='dapp usage' AND name='1inch user')
    LIMIT 10;

There you have it: you see addresses that traded on both Uniswap and 1inch and all associated address labels.

🧐 Understanding data decoding in Dune Analytics

This section contains a quick primer on how you can explore what decoded data we have and the methods we use to decode the data.

In Dune, there are tables for each event and function defined in the smart contract ABI. Subsequently, every event or function call on that contract is decoded and inserted as rows into these tables.

The tables are named accordingly

events:projectname."contractName_evt_eventName"

function calls: projectname."contractName_call_eventName"

As an example, decoded data for the AddLiquidity-event and addLiquidity-function of the uniswap exchange contract are found in tables

uniswap."Exchange_evt_AddLiquidity" and uniswap."Exchange_call_addLiquidity".

Using the event tables is usually sufficient, but in some cases you will want to use the call tables. For instance Maker DAO which don't give you too many events you can use tables like maker.SaiTub_call_draw.

What contracts have decoded data?

Decoded data

By querying ethereum.contracts, you can get an overview over which contracts are tracked by the Dune backend. The columns are

namespace -- Project/product name
name -- Contract name
ABI -- The ABI that will populate the relevant tables
address -- The contract address
dynamic -- True if all contracts with the same bytecode are decoded automatically
bytecode -- The bytecode for the decoded contract

Abstractions and views

On top of the decoded tables we have a growing number of views that make it even easier to work with the data.

Views are named namespace.view_event for instance. In general you can search for view in the table list to find the views we have.

A few handy queries to explore decoded tables

See all projects we have decoded data for

SELECT DISTINCT namespace FROM ethereum."contracts";

Do we have decoded data for a specific contract?

SELECT * FROM ethereum."contracts"
WHERE address = '\xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2';

Contracts that are "interface"-decoded

SELECT * FROM ethereum."contracts" WHERE address IS NULL;

If you are working with a an event or call table directly you can see if there are several instances of that contract with

SELECT DISTINCT contract_address FROM projectname."contractName_evt_eventName";

Scalable decoding across contracts

Many dApps have numerous smart contracts that are more or less similar, we have two main ways of handling this in a scalable way:

Contracts with the same bytecode

Dune can dynamically add contracts to track by comparing the bytecode of deployed contracts to known bytecodes. If a known contract is "dynamic", events and calls to a newly deployed contract with matching bytecode will find it's way into the same tables as were defined by the base contract. Essentially this covers all factory-pattern contract architectures. As a result,Β SELECT-ing from a single table might yield data from multiple contracts. In decoded tables, the columnΒ contract_address tells you which smart contract the event or call is on. If you want to look at only a single contract you can filter by its address.

For example:

SELECT DISTINCT contract_address FROM uniswap."Exchange_evt_TokenPurchase";

Will give you all the unique Uniswap exchanges with a Token Purchase event.

Interfaces

When we're interested in a subset of events fired regardless of the origin contract, Dune uses interface-decoding. Notable examples include erc20 and erc721 transfer events. This method is reserved for special cases.

How Dune handles Proxy contracts

Sometimes users interact with dApps through proxy contracts. The proxy contract throws the event, but there's an underlying contract that contains and performs the action. In those cases, we apply the ABI of the proxied contract (sometimes called "master copy") to the proxy contract address. We also usually apply the name of the proxied contract to the relevant table.

πŸ“¬ Get any smart contract decoded

We have decoded data for the most popular smart contract projects. Head to duneanalytics.com/decode if you have a request for decoding of data.

πŸ‘©β€πŸ­ Change log

August 2020 - USD prices for more assets, token decimals on prices.usd table

March 2020 - block_time denormalization, traces.success and more

January 2020 - ERC20 transfer table, Fallback decoding and more

October 2019 - New data structure

πŸ‘‰ Some sample queries

Growth rate

Assuming you have a query with a count of some event grouped by time (month for instance) you can add this snippet to you select statement to get growth rate.

(count(distinct event) - lag(count(distinct event), 1)
over (order by date_trunc('month', evt_block_time))) / lag(count(distinct action), 1)
over (order by date_trunc('month', evt_block_time)) as "Growth rate"

Multiply the number by 100 to get percentage.

Users and amount over a trailing period

SELECT  date_trunc('day', evt_block_time),
        COUNT (DISTINCT buyer),
        SUM(eth_bought / 1e18)
FROM uniswap."Exchange_evt_EthPurchase" p
WHERE evt_block_time > now() - interval '7 days'
GROUP BY 1
ORDER BY 1;

Filter query by an address in the interface

If you use {{}} in a query an input field in the UI will appear and anyone looking at the query can easily input info in that field that will go into the query.

This is very useful when filtering for custom atributes like an Ethereum address or a token address.

When you query in Dune you use \x... while people commonly use 0x... (see more details here).

Using the below snippet will allow users to past addresses in the regular 0x... format and then convert it to \x... that will work in a query.

WHERE contract_address = CONCAT('\x', substring('{{Address}}' from 3))::bytea

Here's an example of this being applied in a query.

Circulating supply over time of a token with mint/burn functions

SELECT
week,
SUM(transfer) over (order by week)
FROM
 (
    SELECT
    date_trunc('week', evt_block_time) as week,
    sum(amount/1e18) as transfer
    FROM ptokens."pBTC_evt_Minted" tr
    GROUP BY 1
UNION
    SELECT
    date_trunc('week', evt_block_time) as week,
    sum(-amount/1e18) as transfer
    FROM ptokens."pBTC_evt_Burned" tr
    GROUP BY 1
) as net;

Circulating supply over time with mint/burn from 0x000... address

SELECT
week,
SUM(transfer) over (order by week)
FROM
 (
    SELECT
    date_trunc('week', evt_block_time) as week,
    sum(value/1e8) as transfer --Divide by relevant decimal number
    FROM erc20."ERC20_evt_Transfer"
    WHERE contract_address = '\x2260fac5e5542a773aa44fbcfedf7c193bc2c599' -- token contract address
    AND "from" = '\x0000000000000000000000000000000000000000'
    GROUP BY 1
UNION
    SELECT
    date_trunc('week', evt_block_time) as week,
    sum(-value/1e8) as transfer --Divide by relevant decimal number
    FROM erc20."ERC20_evt_Transfer"
    WHERE contract_address = '\x2260fac5e5542a773aa44fbcfedf7c193bc2c599' -- token contract address
    AND "to" = '\x0000000000000000000000000000000000000000'
    GROUP BY 1
) as net;

USD value of token utilised for an event

SELECT
date_trunc('week', evt_block_time),
SUM(amount/1e18 * p.price) AS staked
FROM numerai."SimpleGriefing_evt_StakeAdded" s --Replace with relevant event
LEFT JOIN prices.usd p ON p.minute = date_trunc('minute', evt_block_time)
WHERE p.symbol = 'NMR' --Replace with relevant token
-- WHERE p.contract_address = s.token_address --In case different tokens
GROUP BY 1;

USD trading volume per token over time

SELECT    price.symbol,
          date_trunc('week', evt_block_time) AS time,
          SUM(["FilledAmount"] /10^(erc.decimals) * price.price) AS usd_value
   FROM [table].["fill_event"] filled
   LEFT JOIN prices.usd price ON date_trunc('minute', evt_block_time) = price.minute AND [tokenAddress] = price.contract_address
   LEFT JOIN erc20.tokens erc ON erc.contract_address = "takerAsset"
    WHERE evt_block_time < date_trunc('week', current_date)::date
   GROUP BY 1,
            2;

USD price for a token from Uniswap

The most common and easiest way to get token USD prices on Dune Analytics is with the prices.usd table. However, this data is fetched from centralised exchanges so for a long tail of tokens the best approach is to get prices from Uniswap.

This query uses WETH pairs, which is used to map to USD price. The query can be modified to work with any token that has a price in prices.usd

You can find this query on Dune here.

WITH weth_pairs AS ( -- Get exchange contract address and "other token" for WETH
    SELECT cr."pair" AS contract,
        CASE WHEN cr."token0" = '\xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2' then '0' ELSE '1' END  AS eth_token,
        CASE WHEN cr."token1" = '\xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2' then cr."token0" ELSE cr."token1" END  AS other_token
    FROM uniswap_v2."Factory_evt_PairCreated" cr
    WHERE token0 = '\xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2' OR  token1 = '\xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2'
    )

, swap AS ( -- Get all trades on the pair last 14 days
    SELECT
        CASE WHEN eth_token = '0' then sw."amount0In" + sw."amount0Out" ELSE sw."amount1In" + sw."amount1Out"
        END/1e18 AS eth_amt,
        CASE WHEN eth_token = '1' then sw."amount0In" + sw."amount0Out" ELSE sw."amount1In" + sw."amount1Out"
        END/power(10, tok."decimals") AS other_amt, -- If the token is not in the erc20.tokens list you can manually divide by 10^decimals
        tok."symbol",
        tok."contract_address",
        date_trunc('hour', sw."evt_block_time") AS hour
    FROM uniswap_v2."Pair_evt_Swap" sw
    JOIN weth_pairs ON sw."contract_address" = weth_pairs."contract"
    JOIN erc20."tokens" tok ON weth_pairs."other_token" = tok."contract_address"
    WHERE other_token = '\xeb4c2781e4eba804ce9a9803c67d0893436bb27d' --renBTC example
    -- To allow users to submit token address in the Dune UI you can use the below line:
    -- WHERE other_token = CONCAT('\x', substring('{{Token address}}' from 3))::bytea -- Allow user to input 0x... format and convert to \x... format
    AND sw.evt_block_time >= now() - interval '14 days'
    )

, eth_prcs AS (
    SELECT avg(price) eth_prc, date_trunc('hour', minute) AS hour
    FROM prices.layer1_usd_eth
    WHERE minute >= now() - interval '14 days'
    group by 2
    )

SELECT
    AVG((eth_amt/other_amt)*eth_prc) AS usd_price,
    swap."symbol" AS symbol,
    swap."contract_address" AS contract_address,
    eth_prcs."hour" AS hour
FROM swap JOIN eth_prcs ON swap."hour" = eth_prcs."hour"
GROUP BY 2,3,4
;

Token (and USD value) per token over time for an address

Note that this query can get very heavy when there are many tokens and transfers over a long period of time.

WITH transfers AS (

    SELECT  day,
            address,
            token_address,
            sum(amount) AS amount -- Net inflow or outflow per day
    FROM

    (
        SELECT  date_trunc('day', evt_block_time) AS day,
                "to" AS address,
                tr.contract_address AS token_address,
                value AS amount
        FROM erc20."ERC20_evt_Transfer" tr
        WHERE "to" = '\x70c730465dff5447a12bae37090446745c9edccc' --Filter for holding address
        -- AND contract_address = '\xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2' -- Filter for token address if you only want to see a specific token

        UNION ALL

        SELECT  date_trunc('day', evt_block_time) AS day,
                "from" AS address,
                tr.contract_address AS token_address,
                -value AS amount
        FROM erc20."ERC20_evt_Transfer" tr
        WHERE "from" = '\x70c730465dff5447a12bae37090446745c9edccc' --Filter for holding address
        -- AND contract_address = '\xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2' -- Filter for token address if you only want to see a specific token
    ) t
   GROUP BY 1, 2, 3
   )

, balances_with_gap_days AS (
    SELECT  t.day,
            address,
            t.token_address,
            SUM(amount) OVER (PARTITION BY token_address, address ORDER BY t.day) AS balance, -- balance per day with a transfer
            lead(day, 1, now()) OVER (PARTITION BY token_address, address ORDER BY t.day) AS next_day -- the day after a day with a transfer
    FROM transfers t
    )

 , days AS (
    SELECT generate_series('2020-07-01'::timestamp, date_trunc('day', NOW()), '1 day') AS day -- Generate all days since the first contract
    )

, balance_all_days AS (
    SELECT  d.day,
            address,
            erc.symbol,
            b.token_address,
            SUM(balance/10^decimals) AS balance
    FROM balances_with_gap_days b
    INNER JOIN days d ON b.day <= d.day AND d.day < b.next_day -- Yields an observation for every day after the first transfer until the next day with transfer
    INNER JOIN erc20.tokens erc ON b.token_address = erc.contract_address
    GROUP BY 1, 2, 3, 4
    ORDER BY 1, 2, 3, 4
    )

SELECT  b.day,
        b.symbol,
        b.token_address,
        SUM(balance) AS token_balance,
        SUM(balance*p.price) AS balance_usd_value
FROM balance_all_days b
LEFT JOIN  (
                SELECT  date_trunc('day', p.minute) as day,
                        contract_address,
                        symbol,
                        decimals,
                        AVG(p.price) as price
                FROM prices."usd" p
                GROUP BY 1, 2, 3, 4
            ) p ON b.day = p.day AND b.token_address = p.contract_address
GROUP BY 1,2,3
ORDER BY 1,2,3
;

πŸ€• Known issues

Function overloading

We have a known issue with function overloading. There are a few cases where smart contract developers use function overloading, i.e. specify two functions with the same name but different parameters. In these cases, we will currently only have one of the implementations in our database. We’re working on a fix for this. One known case is the two approve implementations in the SAI contract.

More Repositories

1

q-evm

MEV Bot and Flashbots Bundler written in q using kdb+
q
385
star
2

compendium

The Greatest Collection of anything related to finance and crypto
Jupyter Notebook
170
star
3

mempool-shark

primitive and naive etheruem mempool monitor service
TypeScript
111
star
4

gas-reporting

Reference documentation on every gas price API and all the different formats
JavaScript
90
star
5

art-of-engineering

A polemic on software design
Python
61
star
6

Procedural-Approaches-Towards-MEV

Preprint, Procedural approaches towards Maximal Extracted Value
TeX
47
star
7

foundry-scripts

ergonomic devtooling for foundry
Shell
44
star
8

openevm

Open Ethereum Virtual Machine - Knowledgebase, Documentation, etc
HTML
41
star
9

prettier-config-solidity

Prettier config optimized to reduce AST churn & conform to solidity spec
Solidity
30
star
10

zen-foundry-template

zen minimal foundry template
Solidity
28
star
11

ethereum-api

Solidity API contract design information for utilizing RPC/RESTful Node Interaction and GraphQL
22
star
12

layerzero-contracts

LayerZero Contracts for Relay Endpoint
Solidity
19
star
13

nextjs-web3-template

NextJS Web3 Template
Shell
17
star
14

mev-docs

flashbots / MEV documents, papers, articles, etc
JavaScript
16
star
15

isdx

Introspection / Observability smart contracts
Solidity
13
star
16

libmev

TypeScript
11
star
17

dappspec

dappspec is an extended natspec and generator in one
CSS
11
star
18

gasevo

gasevo: embedded volumetric optionality for ethereum hedging
10
star
19

metamask-decloak

metamask and metaswap codebase introspection and tools
JavaScript
9
star
20

boilerplate-fintech-legal-templates

Boilerplate Legal Notices
Shell
9
star
21

foundry-docs

documentation for Ethereum Foundry Forge/Cast
Shell
9
star
22

CME-iLink3

CME iLINK3 Connectivity
JavaScript
8
star
23

solidity-parser-explorer

Solidity and Yul syntax-aware highlighting and AST explorer
C
8
star
24

ofac-list

OFAC List for blockchains
Python
8
star
25

yearn-finance-tokenlist

Yearn Finance Ecosystem Tokenlist
JavaScript
8
star
26

intro-to-mev

miner extracted value presentation
JavaScript
8
star
27

tor-server

TORnado Onion service via google cloud
HCL
7
star
28

typescript-cjs-esm-tests

ESM/CJS docs + testing & dist for cjs/esm - Establishing the best way to support cjs and esm and how to structure project to enable that
TypeScript
7
star
29

base64-svg

base64-svg solidity contract with helpful js utils
JavaScript
7
star
30

proxy-yul

transparent proxy in yul
Yul
7
star
31

yearn-protocol-nodejs

convenience script to use nodejs/npm insteald of brownie for yearn-protocol
Solidity
7
star
32

github-nft

Generate an NFT based off your github profile
Solidity
7
star
33

latex-solidity

LaTeX Solidity and Yul Highlighting via Pygments
TeX
6
star
34

rpc-web3-debugger

Web3 Provider Testing and Debugging Suite
CSS
6
star
35

forge-scope

improved remappings and deployments for foundry
TypeScript
6
star
36

safe-result

ensure transaction results are safe before sending
TypeScript
6
star
37

hardhat-spark

hardhat ignite
Solidity
6
star
38

parallelish

parallelish; run a GitHub Action jobs multiple times in parallel in a single pipeline
Shell
6
star
39

dgeth

a degenerate go-ethereum client
Go
6
star
40

ethereum2

Ethereum Execution/Consensus monorepo
Shell
5
star
41

solidity-testfiles

contracts for testing parsing and ast transforms for solidity
Solidity
5
star
42

simple-sushiswap-sdk

updated simple sushiswap sdk
TypeScript
5
star
43

libjq

a library of useful jq functions and scripts
jq
5
star
44

solidity-mac-osx-bin

solidity solc binaries for mac os x
Python
5
star
45

audit-format

Visual Studio Code Extension for inline Audit/Bookmarks
5
star
46

evo-whitepaper

EVO Protocol Whitepaper
TeX
5
star
47

foundry-badge

Foundry status/shield badges with branding attributes
TypeScript
5
star
48

homoglyph-attack

5
star
49

Mechanism-Design-Literature

Mechanism Design Literature
4
star
50

gitxxx

Git utils and Addons
Python
4
star
51

foundryup-tests

Foundryup Extended for Foundry
Shell
4
star
52

web3-rpc-failover

web3 rpc provider connectivity failover
TypeScript
4
star
53

generator-yyip

yeoman generator to create YIPs / yearn finance improvement proposals
JavaScript
4
star
54

echidna-forge-container

echidna + forge fuzzing container env
Shell
4
star
55

solidity-hardhat-template

solidity hardhat template bootstrap
TypeScript
4
star
56

forge-nix

Foundry git submodule / nix dependency manager
Shell
4
star
57

notebooks

JavaScript
4
star
58

foundry-tutorial

Foundry Solidity Tutorial
Solidity
4
star
59

foundry-prettier-fuzz

Shell
4
star
60

nix-linguist

Legacy GitHub Linguist Grammar for Nix
TypeScript
4
star
61

iconography

performant and minimalist svg icon library for web3
JavaScript
4
star
62

disco-ecdsa

Gas and Size Optimized ECDSA contracts from Open Zeppelin
Solidity
4
star
63

turbokeeper-beta

JavaScript
4
star
64

solidity-osx

Complete Binaries for Solidity on OSX
Dockerfile
4
star
65

geth-instrument

Go
4
star
66

docker-remixd

Ethereum Remix IDE Container Environment
Dockerfile
3
star
67

boilerplate-certificate-practice-statement

Certificate Practice Statement Boilerplate for providing an official CSP for code signing/pki
CSS
3
star
68

solidity-lld64

solidity compiler for macOS M1/M2 using performant LLVM Linker
3
star
69

foundry-tags

Shell
3
star
70

baseline-truffle-box

Baseline Protocol Truffle Box
Solidity
3
star
71

defi-threat-matrix

MITRE ATT&CK Adapted for Decentralized Finance
CSS
3
star
72

hexium-font

a font for transaction hashes / hex encoded / checksum data
HTML
3
star
73

dappspec-css

css library for generating and publishing natspec documentation from solidity
Sass
3
star
74

caddyflask

flask + caddyserver + docker-compose
Shell
3
star
75

solc-osx-aarch64

see wiki
Shell
3
star
76

solidity-pegjs-parser

Solidity Parser for Coverage Testing utilizing new Peggy PEG.js Parser
PEG.js
3
star
77

dotfiles2

~/.dotfiles2 - sane shell configuration
Shell
3
star
78

vyp4r

Vyp4r - ANTLR4 Parser for Vyper Language
JavaScript
3
star
79

minimal-jsonrpc-dapp-methods

minimal JSON RPC methods required for a node to service a DApp
JavaScript
3
star
80

badge-1-tutorial

Example Badge Tutorial process for Acclaim NFT's
HTML
3
star
81

gh-label-template-ethereum

GitHub Repo pre-configured for Ethereum projects based off of Spearbit
3
star
82

metamask-exploit

CSS
3
star
83

custom-rpc-methods

List of Non-Standard/Custom JSON-RPC Methods for Ethereum EVM Clients
HTML
3
star
84

wallet-pane

TypeScript
3
star
85

pqcm

PQCM - a linked-list priority queue for call markets and decentralized exchanges. Implemented in solidity
Solidity
3
star
86

4626-docs

ERC4626 Spec
HTML
3
star
87

openmev-tornadocash-relay

TypeScript
3
star
88

brownie-ci-docker

Ethereum Brownie as a CI service - See the packages URL for more info
Shell
3
star
89

yearn-monorepo

Solidity
3
star
90

gethulent

Geth JSON-RPC client for proxy/MEV.mocking transactions/calls
Go
3
star
91

safe-result-boundry

react hook for automated fallback/redirection based on fast-lev
TypeScript
3
star
92

blockchain-time

Papers and Resources for utilizing time on-chain and off-chain
Perl
3
star
93

wallet-rpc-method

TypeScript
2
star
94

dns-over-ens

Solidity
2
star
95

wall-e_th

walleth-derived android ethereum wallet for accessing pre 1.8-firmware Trezor Hardware Wallets
Kotlin
2
star
96

nix-template

template for dapptools/nix
Nix
2
star
97

create-subgraph-app

cra-subgraph - web3/subgraph template
TypeScript
2
star
98

zola-solidity

HTML
2
star
99

typesense-doc-search

Shell
2
star
100

solidity-handlebars

solidity interface and natspec handlebar templates
Handlebars
2
star