-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create Safe on Ethereum Balances (#6392)
* Create Safe on Ethereum Balances * Update safe_eth_balances.sql v2 * Update safe_eth_balances.sql v3 * Update safe_ethereum_schema.yml v1 * Update safe_ethereum_schema.yml v2 * removed schema * Update safe_eth_balances.sql v5 Added token balance * Update safe_eth_balances.sql v6 Changed Incremental command * Update safe_eth_balances.sql v7 * Update safe_eth_balances.sql v8 * reformat and finalize spell * Update dbt_subprojects/hourly_spellbook/models/_project/safe/ethereum/safe_ethereum_balances.sql Added jeff-dude suggestion Co-authored-by: jeff-dude <[email protected]> * Update safe_ethereum_balances.sql fewer columns in unique keys * Update dbt_subprojects/hourly_spellbook/models/_project/safe/ethereum/safe_ethereum_balances.sql Co-authored-by: jeff-dude <[email protected]> * Update safe_ethereum_balances.sql v11 Changed to day from timestamp * Update safe_ethereum_schema.yml added balances schema * Update safe_ethereum_schema.yml * fix schema file, cast as day --------- Co-authored-by: jeff-dude <[email protected]> Co-authored-by: jeff-dude <[email protected]>
- Loading branch information
1 parent
fe64976
commit 0a046ab
Showing
3 changed files
with
126 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
dbt_subprojects/hourly_spellbook/models/_project/safe/ethereum/safe_ethereum_balances.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
{{ | ||
config( | ||
schema = 'safe_ethereum', | ||
alias = 'balances', | ||
partition_by = ['day'], | ||
materialized = 'incremental', | ||
incremental_strategy = 'merge', | ||
file_format = 'delta', | ||
unique_key = ['day', 'blockchain', 'address', 'token_address'], | ||
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 date)) 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 date) as day, | ||
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 | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |