Skip to content

Commit

Permalink
Add Safe Optimism Balances (#6498)
Browse files Browse the repository at this point in the history
* Create safe_optimism_balances.sql

* Update safe_optimism_schema.yml

* Update safe_optimism_schema.yml v2

* Update balances.yml

Added sources for Safe's balances on Polygon and Optimism

---------

Co-authored-by: jeff-dude <102681548+jeff-dude@users.noreply.github.com>
  • Loading branch information
safeintern and jeff-dude authored Aug 9, 2024
1 parent ee2bbc6 commit 12f01f8
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
{{
config(
schema = 'safe_optimism',
alias = 'balances',
partition_by = ['day'],
materialized = 'incremental',
incremental_strategy = 'merge',
file_format = 'delta',
unique_key = ['day', 'blockchain', 'address', 'token_address'],
post_hook = '{{ expose_spells(\'["optimism"]\',
"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_optimism', 'balances_daily_agg') }} a
join (
select
address
, blockchain
from {{ ref('safe_optimism_safes') }} s
where blockchain = 'optimism'
) 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
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,29 @@ models:
name: amount_usd
description: "USD amount of transferred ETH"

- name: safe_optimism_balances
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- day
- blockchain
- address
- token_address
meta:
blockchain: optimism
project: safe
contributors: safeintern
config:
tags: ['safe', 'optimism']
description: “Safe addresses balances”
columns:
- name: day
- name: blockchain
- name: address
- name: token_address
- name: token_standard
- name: token_id
- name: token_symbol
- name: token_balance
- name: balance_usd

9 changes: 8 additions & 1 deletion sources/_subprojects_outputs/tokens/balances.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,11 @@ version: 2
sources:
- name: tokens_ethereum
tables:
- name: balances_daily_agg
- name: balances_daily_agg
- name: tokens_optimism
tables:
- name: balances_daily_agg
- name: tokens_polygon
tables:
- name: balances_daily_agg

0 comments on commit 12f01f8

Please sign in to comment.