Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimism: velodrome pools dex balances #7375

Draft
wants to merge 19 commits into
base: main
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
version: 2

models:
- name: velodrome_pools_optimism_balances
description: "Tracks OP token balances in Velodrome pools on Optimism."
meta:
blockchain: optimism
sector: DEX
project: velodrome
contributors: jason
config:
tags: ['optimism', 'op_token', 'balances', 'velodrome']
data_tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- pool_address
- snapshot_day
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{{
config(
schema = 'velodrome_pools_optimism',
alias = 'balances',
materialized = 'incremental',
file_format = 'delta',
incremental_strategy = 'merge',
unique_key = ['pool_address', 'snapshot_day'],
incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.snapshot_day')]
)
}}

WITH velo_pools AS (
SELECT
pool AS pool_address,
token0,
token1,
evt_block_time AS creation_time
FROM
{{ source('velodrome_v2_optimism', 'PoolFactory_evt_PoolCreated') }}
WHERE
(token0 = 0x4200000000000000000000000000000000000042
OR token1 = 0x4200000000000000000000000000000000000042)
),
filtered_balances AS (
SELECT
address AS pool_address,
balance AS op_balance,
day AS snapshot_day
FROM
{{ source('tokens_optimism', 'balances_daily') }}
WHERE
token_address = 0x4200000000000000000000000000000000000042
{% if is_incremental() %}
and {{ incremental_predicate('day') }}
{% else %}
and day >= date '2023-06-22' --first pool initiated
{% endif %}
)
SELECT
p.pool_address,
p.token0,
p.token1,
p.creation_time,
COALESCE(b.op_balance, 0) AS op_balance,
COALESCE(b.snapshot_day, CURRENT_DATE) AS snapshot_day
FROM
velo_pools p
LEFT JOIN
filtered_balances b ON p.pool_address = b.pool_address
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
version: 3

models:
- name: aave_pools_optimism_balances
description: "Tracks OP token balances in Aave pools on Optimism."
meta:
blockchain: optimism
sector: DEX
project: aave
contributors: jason
config:
tags: ['optimism', 'op_token', 'balances', 'aave']
data_tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- pool_address
- snapshot_day
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
{{
config(
schema = 'aave_pools_optimism',
alias = 'balances',
materialized = 'incremental',
file_format = 'delta',
incremental_strategy = 'merge',
unique_key = ['pool_address', 'snapshot_day'],
incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.snapshot_day')]
)
}}

WITH supply_events AS (
SELECT
depositor AS pool_address,
DATE(evt_block_time) AS snapshot_day,
evt_block_time,
evt_block_number,
evt_tx_hash,
amount AS supply_amount,
'supply' as event_type
FROM {{ source('aave_v3_optimism', 'supply') }}
WHERE token_address = 0x4200000000000000000000000000000000000042
),

borrow_events AS (
SELECT
borrower AS pool_address,
DATE(evt_block_time) AS snapshot_day,
evt_block_time,
evt_block_number,
evt_tx_hash,
amount AS borrow_amount,
'borrow' as event_type
FROM {{ source('aave_v3_optimism', 'borrow') }}
WHERE token_address = 0x4200000000000000000000000000000042
),

combined_events AS (
SELECT
pool_address,
snapshot_day,
evt_block_time,
evt_block_number,
evt_tx_hash,
supply_amount AS amount,
event_type
FROM supply_events

UNION ALL

SELECT
pool_address,
snapshot_day,
evt_block_time,
evt_block_number,
evt_tx_hash,
-borrow_amount AS amount, -- Negative for borrows
event_type
FROM borrow_events
),

-- Calculate running balance per day
daily_balances AS (
SELECT
pool_address,
snapshot_day,
SUM(amount) as net_balance
FROM combined_events
GROUP BY 1, 2
),

-- Final output with metadata
final_balances AS (
SELECT DISTINCT
pool_address,
'aave' AS protocol_name,
'v3' AS protocol_version,
snapshot_day,
net_balance as op_balance
FROM daily_balances
WHERE net_balance > 0
)

SELECT
pool_address,
protocol_name,
protocol_version,
snapshot_day,
op_balance
FROM final_balances
4 changes: 4 additions & 0 deletions sources/_subprojects_outputs/dex/_sources.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,7 @@ sources:
- name: beethoven_x
tables:
- name: trades
- name: aave_v3_optimism
tables:
- name: supply
- name: borrow
1 change: 1 addition & 0 deletions sources/_subprojects_outputs/tokens/balances.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ sources:
tables:
- name: balances_daily_agg
- name: balances_daily_agg_base
- name: balances_daily
- name: tokens_polygon
tables:
- name: balances_daily_agg
Expand Down
Loading