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

Create Safe on Ethereum Balances #6392

Merged
merged 22 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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,96 @@
{{
config(
schema = 'safe_ethereum',
alias = 'balances',
partition_by = ['day'],
materialized = 'incremental',
incremental_strategy = 'merge',
file_format = 'delta',
unique_key = ['day', 'blockchain', 'address', 'token_address'],
on_schema_change = 'sync_all_columns',
safeintern marked this conversation as resolved.
Show resolved Hide resolved
post_hook = '{{ expose_spells(\'["ethereum"]\',
"project",
"safe",
\'["safeintern"]\') }}'
)
}}

with changed_balances as (
select
a.blockchain,
day,
a.address,
token_symbol,
token_address,
token_standard,
token_id,
balance,
lead(cast(day as timestamp)) over (partition by token_address, a.address, token_id order by day asc) as next_update_day
from {{ source('tokens_ethereum', 'balances_daily_agg') }} a
join (
select
address
, blockchain
from {{ ref('safe_ethereum_safes') }} s
where blockchain = 'ethereum'
) q on q.address = a.address
where day >= date('2021-07-01')
and token_standard in ('native', 'erc20')
{% if is_incremental() %}
and {{ incremental_predicate('day') }}
{% endif %}
),
days as (
select *
from unnest(
sequence(cast('2021-07-01' as date), date(date_trunc('day', now())), interval '1' day)
) as foo(day)
),
forward_fill as (
select
blockchain,
cast(d.day as timestamp) as day,
jeff-dude marked this conversation as resolved.
Show resolved Hide resolved
address,
token_symbol,
token_address,
token_standard,
token_id,
balance
from days d
left join changed_balances b
on d.day >= b.day
and (b.next_update_day is null OR d.day < b.next_update_day)
where d.day >= cast('2021-07-01' as date)
{% if is_incremental() %}
and {{ incremental_predicate('d.day') }}
{% endif %}
)
select
b.day,
b.blockchain,
b.address,
b.token_address,
b.token_standard,
b.token_id,
b.token_symbol,
sum(b.balance) as token_balance,
sum(b.balance * p.price) as balance_usd
from (
select * from forward_fill
where balance > 0
) b
left join {{ ref('prices_usd_daily') }} p
jeff-dude marked this conversation as resolved.
Show resolved Hide resolved
on (
token_standard = 'erc20'
and b.blockchain = p.blockchain
and b.token_address = p.contract_address
and b.day = p.day
)
or (
token_standard = 'native'
and p.blockchain is null
and p.contract_address is null
and p.symbol = 'ETH'
and b.day = p.day
)
group by 1, 2, 3, 4, 5, 6, 7
5 changes: 5 additions & 0 deletions sources/_subprojects_outputs/tokens/balances.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
version: 2
sources:
- name: tokens_ethereum
tables:
- name: balances_daily_agg
Loading