Skip to content

Commit f52de49

Browse files
0xBoxergitbook-bot
authored andcommitted
GitBook: [dev] 43 pages modified
1 parent fd79b1a commit f52de49

File tree

4 files changed

+168
-2
lines changed

4 files changed

+168
-2
lines changed

SUMMARY.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* [xDai Data](data-tables/data-tables/raw-data/xdai-data.md)
2323
* [Decoded Data](data-tables/data-tables/decoded-data.md)
2424
* [Token standards](data-tables/data-tables/special-tables.md)
25+
* [ERC-20 balances](data-tables/data-tables/erc-20-balances.md)
2526
* [Abstractions](data-tables/data-tables/abstractions.md)
2627
* [User Generated](data-tables/data-tables/user-generated.md)
2728
* [Prices](data-tables/data-tables/prices.md)
@@ -34,7 +35,7 @@
3435
* [raw transactions per wallet](working-on-dune/queries/tx-wallet.md)
3536
* [gas metrics per wallet](working-on-dune/queries/gas-metrics-per-wallet.md)
3637
* [price queries](working-on-dune/queries/price-queries.md)
37-
* [circulating supply over time of a token](working-on-dune/queries/circ-supply.md)
38+
* [total supply over time of a token](working-on-dune/queries/supply.md)
3839
* [Users and amount over a trailing period](working-on-dune/queries/untitled-6.md)
3940
* [USD value of token utilised for an event](working-on-dune/queries/untitled-3.md)
4041
* [USD price for a token from Uniswap](working-on-dune/queries/untitled-2.md)
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# ERC-20 balances
2+
3+
### No more struggles in querying for erc20 tokens.
4+
5+
On a raw data level it's pretty hard to work with erc20 tokens since you need to sum all transfers for all addresses over time. This unnecessarily bloats queries and quickly leads to human errors. To prevent that from happening we have constructed several views and tables that will help you query for erc20 data with ease.
6+
7+
These tables/views either tell you the latest distribution/allocation or the distribution over time on a hourly or daily basis. These tables can be used for all kinds of interesting analysis, but you still need to watch out for a few things while working with them:
8+
9+
10+
* the mint/burn address is not standardized, so you need to find out those addresses and manually apply a fix in your queries. In most cases it will be `x0000000000000000000000000000000000000000`for minting and burning, but always make sure that that is indeed the case. In the example given that's exactly not the case.
11+
12+
13+
14+
**example:**
15+
16+
```sql
17+
Select
18+
wallet_address,
19+
amount,
20+
day,
21+
token_symbol
22+
from erc20."view_token_balances_daily"
23+
where token_address = '\x429881672B9AE42b8EbA0E26cD9C73711b891Ca5'
24+
and wallet_address != '\x0000000000000000000000000000000000000000' --mint address
25+
and wallet_address != '\x000000000000000000000000000000000000dead' --burn address
26+
```
27+
28+
* working with these tables quickly leads to a lot of individual data points that our visualization engine is not always able to handle perfectly. Instead of trying to display every unique holder it makes sense to group them by certain criteria and display the dataset that way.
29+
30+
```sql
31+
Select
32+
33+
CASE WHEN wallet_address = '\xbBCf169eE191A1Ba7371F30A1C344bFC498b29Cf' then 'dill'
34+
WHEN wallet_address = '\xdc98556Ce24f007A5eF6dC1CE96322d65832A819' then 'uniswap'
35+
WHEN wallet_address = '\xC52139a20A57c9002e9F5188901EF0ffC63c7205' then 'smart_treasury'
36+
WHEN wallet_address = '\x40ec5b33f54e0e8a33a975908c5ba1c14e5bbbdf' then 'polygon'
37+
WHEN wallet_address = '\x6cc5f688a315f3dc28a7781717a9a798a59fda7b' then 'OKEX'
38+
WHEN amount between 0 and 10 then 'Plankton(0-10)'
39+
WHEN amount between 10 and 100 then 'shrimp(10-100)'
40+
WHEN amount between 100 and 1000 then 'fish(100-1,000)'
41+
WHEN amount between 1000 and 10000 then 'dolphin(1,000-10,000)'
42+
WHEN amount > 10000 then 'whale (>10000)'
43+
--note that the order of case statements matters here
44+
end as classification,
45+
46+
sum(amount) as amount,
47+
token_symbol
48+
from erc20."view_token_balances_latest"
49+
where token_address = '\x429881672B9AE42b8EbA0E26cD9C73711b891Ca5'
50+
and wallet_address != '\x0000000000000000000000000000000000000000'
51+
and wallet_address != '\x000000000000000000000000000000000000dead'
52+
and amount > 0.1
53+
group by 1,3
54+
55+
```
56+
57+
### Dashboard example:
58+
59+
This dashboard contains the most important use cases related to a single erc20 token that is used as gov token.
60+
61+
[https://duneanalytics.com/0xBoxer/pickle-finance\_1](https://duneanalytics.com/0xBoxer/pickle-finance_1)
62+
63+
### erc20.token\_balances
64+
65+
This table contains the hourly balance of all erc20 tokens over the entire existence of these tokens.
66+
67+
It's very useful to query for all kinds of erc20 related data. You can view which and how much tokens are in a specific wallet over a period of time, query for the distribution of a specific token over time or create snapshots.
68+
69+
| column name | data type | description |
70+
| :--- | :--- | :--- |
71+
| amount | numeric | the correct display format for that token |
72+
| amount\_raw | numeric | the raw amount of that token \(need to divide by decimals!\) |
73+
| timestamp | timestamptz | the time in the resolution of hours |
74+
| token\_address | bytea | the address of the token |
75+
| token\_symbol | text | the symbol of the token |
76+
| wallet\_address | bytea | the address of the wallet holding this token |
77+
78+
### erc20.view\_token\_balances\_latest
79+
80+
This view depends on the erc20.token\_balances table and gives you the information of the latest distribution of that token.
81+
82+
| column name | data type | description |
83+
| :--- | :--- | :--- |
84+
| amount | numeric | the correct display format for that token |
85+
| amount\_raw | numeric | the raw amount of that token \(need to divide by decimals!\) |
86+
| amount\_usd | float8 | the current price \(if we have data on the price\) |
87+
| last\_transfer_\__timestamp | timestamptz | the date on which the balance of this token last changed in this particular wallet address |
88+
| token\_address | bytea | the address of the token |
89+
| token\_symbol | text | the symbol of the token |
90+
| wallet\_address | bytea | the address of the wallet holding this token |
91+
92+
### erc20.view\_token\_balances\_hourly
93+
94+
This view depends on the erc20.token\_balances table and already has prices, so it's very convenient to use.
95+
96+
| column name | data type | description |
97+
| :--- | :--- | :--- |
98+
| amount | numeric | the correct display format for that token |
99+
| amount\_raw | numeric | the raw amount of that token \(need to divide by decimals!\) |
100+
| amount\_usd | float8 | the current price \(if we have data on the price\) |
101+
| timestamp | timestamptz | the time in the resolution of hours |
102+
| token\_address | bytea | the address of the token |
103+
| token\_symbol | text | the symbol of the token |
104+
| wallet\_address | bytea | the address of the wallet holding this token |
105+
106+
### erc20.view\_token\_balances\_daily
107+
108+
This view depends on the `erc20.token_balances` table and already has prices, so it's very convenient to use. **This table will perform much better than `erc20.view_token_balances_hourly` since it's only querying for data on a daily basis**. If you want to make high level analysis, this is your way to go.
109+
110+
| column name | data type | description |
111+
| :--- | :--- | :--- |
112+
| amount | numeric | the correct display format for that token |
113+
| amount\_raw | numeric | the raw amount of that token \(need to divide by decimals!\) |
114+
| amount\_usd | float8 | the current price \(if we have data on the price\) |
115+
| timestamp | timestamptz | the time in the resolution of days |
116+
| token\_address | bytea | the address of the token |
117+
| token\_symbol | text | the symbol of the token |
118+
| wallet\_address | bytea | the address of the wallet holding this token |
119+

working-on-dune/queries/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Seeing the queries live and in action does allow you to more intuitively engage
2121

2222
{% page-ref page="untitled-6.md" %}
2323

24-
{% page-ref page="circ-supply.md" %}
24+
{% page-ref page="supply.md" %}
2525

2626
{% page-ref page="untitled-3.md" %}
2727

working-on-dune/queries/supply.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# total supply over time of a token
2+
3+
4+
5+
You can either use that token's specific tables as long as they are [decoded](../../data-tables/data-tables/decoded-data.md):
6+
7+
```sql
8+
SELECT
9+
week,
10+
SUM(transfer) over (order by week)
11+
FROM
12+
(
13+
SELECT
14+
date_trunc('week', evt_block_time) as week,
15+
sum(amount/1e18) as transfer
16+
FROM ptokens."pBTC_evt_Minted" tr
17+
GROUP BY 1
18+
UNION
19+
SELECT
20+
date_trunc('week', evt_block_time) as week,
21+
sum(-amount/1e18) as transfer
22+
FROM ptokens."pBTC_evt_Burned" tr
23+
GROUP BY 1
24+
) as net;
25+
```
26+
27+
Or you can use a more general purpose query like this one:
28+
29+
\(Please note that while this works for most tokens, some tokens do have slight changes in their structure that break this query\)
30+
31+
```sql
32+
Select
33+
sum(amount),
34+
day
35+
36+
from erc20."view_token_balances_daily"
37+
where token_address = '\x429881672B9AE42b8EbA0E26cD9C73711b891Ca5'
38+
and wallet_address != '\x0000000000000000000000000000000000000000' --mint address
39+
and wallet_address != '\x000000000000000000000000000000000000dead' --burn address
40+
group by 2
41+
42+
--mint and burn address are not standardized, make sure to query for the right ones
43+
```
44+
45+
46+

0 commit comments

Comments
 (0)