|
| 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 | + |
0 commit comments