Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

update top spellbook tables #372

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 85 additions & 24 deletions docs/data-tables/spellbook/top-tables/dex.trades.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,90 @@ This table standardizes and normalizes the trading data across virtually all rel

The scripts that generate the table dex.trades can be found in this [public github](https://github.com/duneanalytics/spellbook/tree/main/models/dex) repo.

## Indexed Chains And Platforms

<iframe src="https://dune.com/embeds/2924534/4857165" width="100%" height="400" frameborder="0"></iframe>

## Column Data

| Column name | Data type | Description |
| - | :-: | - |
| `block_time` | _timestamptz_ | The timestamp of the block that included this transaction |
| `token_a_symbol` | _varchar_ | The symbol of one of the two tokens that got traded |
| `token_b_symbol` | _varchar_ | The symbol of one of the two tokens that got traded |
| `token_a_amount` | _numeric_ | The amount of token A that got traded |
| `token_b_amount` | _numeric_ | The amount of token B that got traded |
| `project` | _varchar_ | The dex on which this trade was executed |
| `version` | _varchar_ | Which version of the dex got used? |
| `blockchain` | _varchar_ | Which blockchain did this occur on |
| `taker` | _varbinary_ | Which contract called the dex contract? |
| `maker` | _varbinary_ | In some special cases there actually is a counter party to transactions, this party will get displayed here if applicable |
| `token_a_amount_raw` | _numeric_ | The raw amount of token A that got traded |
| `token_b_amount_raw` | _numeric_ | The raw amount of token B that got traded |
| `amount_usd` | _numeric_ | The USD value of this trade |
| `token_a_address` | _varbinary_ | The ERC-20 token contract address of token A |
| `token_b_address` | _varbinary_ | The ERC-20 token contract address of token B |
| `exchange_contract_address` | _varbinary_ | The address of the decentralized exchange contract that made this trade possible |
| `tx_hash` | _varbinary_ | The hash of the transaction that contained this trade |
| `tx_from` | _varbinary_ | Which address initiated this transaction? |
| `tx_to` | _varbinary_ | What was the first smart contract that got called during this tx? |
| `trace_address` | _ARRAY_ | Which position in the graph tree does the execution of the trade have? |
| `evt_index` | _integer_ | This logs index position in the block (cumulative amount of logs ordered by execution) |
| `trade_id` | _integer_ | Just for database magic |
| Column Name | Data Type | Description |
|-------------------------|-------------------|----------------------------------------------------------------------- |
| `amount_usd` | _double_ | The USD value of this trade |
| `block_date` | _timestamp_ | The truncated timestamp of the block including this transaction |
| `block_time` | _timestamp_ | The timestamp of the block including this transaction |
| `blockchain` | _varchar_ | The blockchain where this transaction was broadcasted |
| `evt_index` | _int_ | This log's index position in the block (cumulative amount of logs ordered by execution) |
| `maker` | _varbinary_ | In some special cases, the counterparty to transactions (if applicable) |
| `project` | _varchar_ | The decentralized exchange (DEX) on which this trade was executed |
| `project_contract_address` | _varbinary_ | The contract address of the project or DEX |
| `taker` | _varbinary_ | The contract that called the DEX contract |
| `token_bought_address` | _varbinary_ | The address of the token bought during this trade |
| `token_bought_amount` | _double_ | The amount of the token bought during this trade |
| `token_bought_amount_raw` | _decimal(38,0)_ | The raw amount of the token bought during this trade |
| `token_bought_symbol` | _varchar_ | The symbol of the token bought |
| `token_pair` | _varchar_ | The trading pair of tokens involved in this trade |
| `token_sold_address` | _varbinary_ | The address of the token sold during this trade |
| `token_sold_amount` | _double_ | The amount of the token sold during this trade |
| `token_sold_amount_raw` | _decimal(38,0)_ | The raw amount of the token sold during this trade |
| `token_sold_symbol` | _varchar_ | The symbol of the token sold |
| `trace_address` | _varchar_ | The position in the execution graph tree for this trade |
| `tx_from` | _varbinary_ | The address that initiated this transaction |
| `tx_hash` | _varbinary_ | The hash of the transaction containing this trade |
| `tx_to` | _varbinary_ | The address of the first smart contract called during this transaction |
| `version` | _varchar_ | The version of the DEX used for this trade |

#### Get all transactions of USDC swaps in the past 24 hours

```sql
select * from dex.trades
where (token_bought_address = 0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48
OR token_sold_address = 0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48)
AND blockchain = 'ethereum'
AND block_time >= NOW() - interval '24' hour
```

<iframe src="https://dune.com/embeds/2924567/4857210" width="100%" height="400" frameborder="0"></iframe>


#### Get top 100 uniswap pairs' volume from uniswap in the past 3 days

```sql
select token_pair,
SUM(amount_usd) as total_volume
from dex.trades
WHERE blockchain = 'ethereum'
AND project = 'uniswap'
AND block_time >= NOW() - interval '3' day
AND token_pair IS NOT NULL
GROUP BY 1
ORDER BY 2 DESC -- order by total_volume
limit 100 -- 100 rows
```

<iframe src="https://dune.com/embeds/2924570/4857215" width="100%" height="400" frameborder="0"></iframe>


#### Get top 100 traders' volume of erc20 token using dex.trades

```sql
-- top traders trading wBTC in the past 7 days
SELECT tx_from as address,
SUM(amount_usd) as total_volume,
SUM(CASE WHEN "token_bought_address" = 0x2260fac5e5542a773aa44fbcfedf7c193bc2c599 THEN 1 END) as buy_count,
SUM(CASE WHEN "token_sold_address" = 0x2260fac5e5542a773aa44fbcfedf7c193bc2c599 THEN 1 END) as sell_count,
SUM(CASE WHEN "token_bought_address" = 0x2260fac5e5542a773aa44fbcfedf7c193bc2c599 THEN amount_usd END) as total_buy_volume,
SUM(CASE WHEN "token_sold_address" = 0x2260fac5e5542a773aa44fbcfedf7c193bc2c599 THEN amount_usd END) as total_sell_volume
FROM dex.trades
WHERE ("token_bought_address" = 0x2260fac5e5542a773aa44fbcfedf7c193bc2c599 OR "token_sold_address" = 0x2260fac5e5542a773aa44fbcfedf7c193bc2c599)
AND blockchain = 'ethereum'
AND block_time >= NOW() - interval '7' day
GROUP BY 1
ORDER BY 2 DESC
limit 100
```

<iframe src="https://dune.com/embeds/2924573/4857218" width="100%" height="400" frameborder="0"></iframe>




102 changes: 87 additions & 15 deletions docs/data-tables/spellbook/top-tables/labels.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,93 @@ Note that there might be a few minutes delay from adding the label on [dune.com]

## The labels table

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

| Column name | Data type | Description |
| - | :-: | - |
| `id` | _int_ | incrementing integer |
| `address` | _varbinary_ | The address of a contract or wallet this label describes |
| `name` | _varchar_ | label name |
| `blockchain` | _varchar_ | the blockchain the label is meant for |
| `author` | _varchar_ | The username of the user who created this label |
| `source` | _varchar_ | The source of this label, autopopulated by Dune |
| `updated_at` | _timestamptz_ | The last time this label was changed |
| `label_type` | _varchar_ | The type of label, defined in the readme |
| `model_name` | _varchar_ | The name of the label model (filename) |
Labels are stored in the new `labels.all` table which has the following schema:

| Column name | Data type | Description |
|---------------|---------------|-------------------------------------------------------|
| `address` | _varbinary_ | The address of a contract or wallet this label describes |
| `category` | _varchar_ | The category of the label (e.g., dex/contracts/institution) |
| `blockchain` | _varchar_ | The blockchain of the address where the label is given |
| `contributor` | _varchar_ | The name of the contributor that created the label |
| `name` | _varchar_ | The name of the label |
| `source` | _varchar_ | The source reference of the label |
| `label_type` | _varchar_ | The type of label (e.g., persona/usage/identifier) |
| `model_name` | _varchar_ | The name of the model used to create the label |
| `created_at` | _timestamp_ | The time when the label was created |
| `updated_at` | _timestamp_ | The time when the label was last updated |

## Using labels

!!! warning
this section is currently under construction, stay tuned!
#### Identifying CRV Holders Using Labels

```sql
SELECT address,
ens,
ARRAY_AGG(DISTINCT name) as label_list,
symbol,
contract_address,
balance
FROM (
SELECT address,
symbol,
name as ens,
contract_address,
SUM(amount) as balance
FROM (
SELECT tr."from" AS address,
symbol,
contract_address,
-(tr.value/POW(10,decimals)) AS amount
FROM erc20_ethereum.evt_transfer tr JOIN tokens.erc20 USING (contract_address)
WHERE "from" != 0x0000000000000000000000000000000000000000 -- exclude mint/burn addresses
AND contract_address = 0xD533a949740bb3306d119CC777fa900bA034cd52 -- contract_address of the erc20 token
UNION ALL
SELECT tr."to" AS address,
symbol,
contract_address,
(tr.value/POW(10,decimals)) AS amount
FROM erc20_ethereum.evt_transfer tr JOIN tokens.erc20 USING (contract_address)
WHERE "to" != 0x0000000000000000000000000000000000000000 -- exclude mint/burn addresses
AND contract_address = 0xD533a949740bb3306d119CC777fa900bA034cd52 -- contract_address of the erc20 token
) x LEFT JOIN ens.reverse_latest USING (address)
GROUP BY 1,2,3,4
HAVING SUM(amount) > 0.1 -- having more than 0.1 balance
) p LEFT JOIN labels.all USING (address)
GROUP BY 1,2,4,5,6
ORDER BY 6 DESC
```

<iframe src="https://dune.com/embeds/2914955/4844332" width="100%" height="400" frameborder="0"></iframe>

#### Using addresses of labels.contract_deployers_ethereum to find the top 100 deployers

```sql
SELECT COUNT(*) as contracts_deployed,
SUM(COUNT(*)) OVER (ORDER BY block_time) as contracts_over_time
from ethereum.creation_traces
WHERE "from" IN (SELECT distinct address FROM labels.contract_deployers_ethereum)
AND block_time >= NOW() - interval '7' day
GROUP BY 1
ORDER BY 2 DESC
```

<iframe src="https://dune.com/embeds/2914898/4844304" width="100%" height="400" frameborder="0"></iframe>

#### Using smart_dex_traders label to find tokens traded by traders

```sql
SELECT COALESCE(token_bought_symbol,CAST(token_bought_address AS VARCHAR)) as token_traded,
COUNT(DISTINCT tx_from) as address_count,
ARRAY_AGG(tx_from) as address_list,
SUM(amount_usd) as total_volume
FROM dex.trades
WHERE tx_from IN (select from_hex(address) from labels.smart_dex_traders)
AND token_bought_address != 0x0000000000000000000000000000000000000000
AND block_time >= NOW() - interval '14' day
GROUP BY 1
HAVING SUM(amount_usd) >= 100000
ORDER BY 4 DESC
```

<iframe src="https://dune.com/embeds/2914807/4844172" width="100%" height="400" frameborder="0"></iframe>

Loading