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: uniswap pools balances #7434

Draft
wants to merge 8 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
25 changes: 25 additions & 0 deletions dbt_macros/shared/balances_subset_daily.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{%- macro balances_subset_daily(
blockchain,
token_address,
start_date
)
%}

with
filtered_balances as (
select
address as pool_address,
balance as token_balance,
day as snapshot_day
from {{ source('tokens_' ~ blockchain, 'balances_daily') }}
where
token_address = {{ token_address }}
{% if is_incremental() %}
and {{ incremental_predicate('day') }}
{% else %}
and day >= cast('{{ start_date }}' as date)
{% endif %}
)

select * from filtered_balances
{% endmacro %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
version: 2

models:
- name: uniswap_pools_optimism_balances
description: "Tracks OP token balances in Uniswap pools on Optimism."
meta:
blockchain: optimism
sector: DEX
project: uniswap
contributors: jason
config:
tags: ['optimism', 'op_token', 'balances', 'uniswap','pools']
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,46 @@
{{
config(
schema = 'uniswap_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 op_pools as (
select
pool as pool_address,
token0,
token1,
fee as fee_tier,
creation_block_time as creation_time
from
{{ source('uniswap_v3_optimism', 'pools') }}
where
token0 = from_hex('0x4200000000000000000000000000000000000042')
or token1 = from_hex('0x4200000000000000000000000000000000000042')
)

, filtered_balances as (
{{ balances_subset_daily(
blockchain='optimism',
token_address="from_hex('0x4200000000000000000000000000000000000042')",
start_date='2021-11-11'
) }}
)

select
p.pool_address,
p.token0,
p.token1,
p.fee_tier,
p.creation_time,
coalesce(b.token_balance, 0) as op_balance,
coalesce(b.snapshot_day, current_date) as snapshot_day
from
filtered_balances b
right join
op_pools p on p.pool_address = b.pool_address
2 changes: 2 additions & 0 deletions sources/_sector/dex/trades/optimism/_sources.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ version: 2

sources:
- name: uniswap_v3_optimism
tables:
- name: pools
- name: sushi_optimism
tables:
- name: ConstantProductPool_evt_Swap
Expand Down
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