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

Arbitrum RWA user balances #6608

Merged
merged 34 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
c70613f
batman
darvinrio Aug 26, 2024
cf648c7
adding template assets table
darvinrio Aug 26, 2024
3a6e109
adding clarity to the macros info
darvinrio Aug 26, 2024
0917787
macro patches
darvinrio Aug 26, 2024
22e1e6b
assets table column name changes
darvinrio Aug 26, 2024
693f610
init balances table
darvinrio Aug 26, 2024
9329340
macros name fix
darvinrio Aug 26, 2024
a9c10ee
move to tokens subproject
darvinrio Aug 26, 2024
c90339b
add schema and add balance to macro
darvinrio Aug 26, 2024
27568ea
remoove junk code and fix schema, compile fixes
darvinrio Aug 26, 2024
e714438
add day to schema
darvinrio Aug 26, 2024
3c42fb3
fix schema
darvinrio Aug 26, 2024
5f9fa27
schema fix
darvinrio Aug 26, 2024
d82f2cb
fix incremental
darvinrio Aug 26, 2024
67669bd
fix incremental
darvinrio Aug 26, 2024
941b1c8
incremental fixes
darvinrio Aug 26, 2024
bf6d5de
add dex balances table
darvinrio Aug 26, 2024
abff240
add pools
darvinrio Aug 26, 2024
67d52e2
fix column name ambiguity
darvinrio Aug 26, 2024
8dde263
adding pyor
darvinrio Aug 29, 2024
2ecba38
move it to one macro
darvinrio Aug 29, 2024
950ba4c
fix
darvinrio Aug 29, 2024
6f61642
remove redundant query
darvinrio Aug 29, 2024
932921c
log last update date and next update for context on rebase forward fi…
darvinrio Aug 29, 2024
643698f
Merge branch 'main' into feat/rwa-tvl
darvinrio Aug 29, 2024
54d7495
adding info context
darvinrio Aug 29, 2024
e5502ab
test incremental setup
0xRobin Sep 4, 2024
1686475
fix errors
0xRobin Sep 4, 2024
e788708
fix columns
0xRobin Sep 4, 2024
af87384
fix columns
0xRobin Sep 4, 2024
6840c98
this should be a bit faster
0xRobin Sep 4, 2024
4a7dde0
fix value expression
0xRobin Sep 4, 2024
795b2a1
check runtime of inner join
0xRobin Sep 4, 2024
6031306
Merge branch 'main' into feat/rwa-tvl
0xRobin Sep 5, 2024
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,162 @@
{# @DEV here

@NOTICE this macro constructs the address level token balances table for given input table
@NOTICE aka, you give lists of tokens and/or address, it generates table with daily balances of the address-token pair

@PARAM blockchain -- blockchain name
@PARAM address_list -- must have an address column, can be none if only filtering on tokens
@PARAM token_list -- must have a token_address column, can be none if only filtering on tokens
@PARAM address_token_list -- for advanced usage, must have both (address, token_address) columns, can be none
@PARAM start_date -- the start_date, used to generate the daily timeseries

#}

{%- macro balances_incremental_subset_daily(
blockchain,
start_date,
address_list = none,
token_list = none,
address_token_list = none
)
%}

WITH
filtered_daily_agg_balances as (
select
b.blockchain,
b.day,
b.block_number,
b.block_time,
b.address,
b.token_address,
b.token_standard,
b.balance_raw,
CASE
WHEN b.token_standard = 'erc20' THEN b.balance_raw / power(10, erc20_tokens.decimals)
WHEN b.token_standard = 'native' THEN b.balance_raw / power(10, 18)
ELSE b.balance_raw
END as balance,
erc20_tokens.symbol as token_symbol,
token_id
from {{ref('tokens_'~blockchain~'_balances_daily_agg_base')}} b
{% if address_list is not none %}
inner join (select distinct address from {{address_list}}) f1
on f1.address = b.address
{% endif %}
{% if token_list is not none %}
inner join (select distinct token_address from {{token_list}}) f2
on f2.token_address = b.token_address
{% endif %}
{% if address_token_list is not none %}
inner join (select distinct address, token_address from {{address_token_list}}) f3
on f3.token_address = b.token_address
and f3.address = b.address
{% endif %}
left join {{ source('tokens', 'erc20') }} erc20_tokens on
erc20_tokens.blockchain = '{{blockchain}}'
AND erc20_tokens.contract_address = b.token_address
AND b.token_standard = 'erc20'
where day >= cast('{{start_date}}' as date)

)
,changed_balances as (
select *
, lead(cast(day as timestamp)) over (partition by token_address,address,token_id order by day asc) as next_update_day
from (
select * from (
select
blockchain
,day
,address
,token_symbol
,token_address
,token_standard
,token_id
,balance
from filtered_daily_agg_balances
where day >= cast('{{start_date}}' as date)
{% if is_incremental() %}
and {{ incremental_predicate('day') }}
{% endif %}
)
-- if we're running incremental, we need to retrieve the last known balance updates from before the current window
-- so we can correctly populate the forward fill.
{% if is_incremental() %}
UNION ALL
select * from (
select
blockchain
,max(day) as day
,address
,token_symbol
,token_address
,token_standard
,token_id
,max_by(balance, day) as balance
from filtered_daily_agg_balances
where day >= cast('{{start_date}}' as date)
and not {{ incremental_predicate('day') }}
group by 1,3,4,5,6,7
)
{% endif %}
)
),

days as (
select *
from unnest(
sequence(cast('{{start_date}}' 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,
address,
token_symbol,
token_address,
token_standard,
token_id,
balance,
b.day as last_updated,
b.next_update_day as next_update
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) -- perform forward fill
)

select
b.blockchain,
b.day,
b.address,
b.token_symbol,
b.token_address,
b.token_standard,
b.token_id,
b.balance,
b.balance * p.price as balance_usd,
b.last_updated,
b.next_update
from(
select * from forward_fill
where balance > 0
{% if is_incremental() %}
and {{ incremental_predicate('day') }}
{% endif %}

) b
left join {{source('prices','usd')}} p
on (token_standard = 'erc20'
and p.blockchain = '{{blockchain}}'
and b.token_address = p.contract_address
and b.day = p.minute)
or (token_standard = 'native'
and p.blockchain is null
and p.contract_address is null
and p.symbol = (select native_token_symbol from {{source('evms','info')}} where blockchain = '{{blockchain}}')
and b.day = p.minute)
{% endmacro %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{{
config(
schema = 'rwa_arbitrum',
alias = 'balances',
materialized = 'incremental',
file_format = 'delta',
incremental_strategy = 'merge',
unique_key = ['day', 'address', 'token_address'],
incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.day')]
)
}}

with
rwa_tokens as (
select
project,
token_address
from {{ref('rwa_arbitrum_tokens')}}
where type = 'RWA'
)

,balances as (
{{
balances_incremental_subset_daily(
blockchain = 'arbitrum',
token_list = 'rwa_tokens',
start_date = '2023-11-17'
)
}}
)

select
t.project
,b.*
from balances b
left join rwa_tokens t
on b.token_address = t.token_address
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{{
config(
schema = 'rwa_arbitrum',
alias = 'dex_balances',
materialized = 'incremental',
file_format = 'delta',
incremental_strategy = 'merge',
unique_key = ['day', 'address', 'token_address'],
incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.day')]
)
}}

with dex_pools as (
select
project,
version,
pool as address,
token_address
from {{ref('rwa_arbitrum_dex_pools')}}
)

,balances as (
{{
balances_incremental_subset_daily(
blockchain = 'arbitrum',
address_token_list = 'dex_pools',
start_date = '2023-11-17',
)
}}
)

select
p.project
,p.version
,b.*
from balances b
left join dex_pools p
on b.address = p.address
and b.token_address = p.token_address


Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

{{
config(
schema = 'rwa_arbitrum'
,alias = 'dex_pools'
,materialized = 'table'
,post_hook='{{ expose_spells(\'["arbitrum"]\',
"sector",
"rwa",
\'["maybeYonas", "pyor_xyz"]\') }}'
)
}}

select
project,
version,
pool,
token_address,
token_is_rwa,
symbol,
protocol,
type
from (values
('curve', 'stableswap-ng', 0x4bD135524897333bec344e50ddD85126554E58B4, 0xaf88d065e77c8cC2239327C5EDb3A432268e5831, 0, 'USDC', 'Mountain Protocol', 'RWA'),
('curve', 'stableswap-ng', 0x4bD135524897333bec344e50ddD85126554E58B4, 0x59D9356E565Ab3A36dD77763Fc0d87fEaf85508C, 0, 'USDM', 'Mountain Protocol', 'RWA'),
('uniswap', '3', 0xfEAA137F43f88b7F767F5A67978FfF8EC11Cc6Ef, 0x82aF49447D8a07e3bd95BD0d56f35241523fBab1, 0, 'WETH', 'Frax Finance', 'Governance'),
('uniswap', '3', 0xfEAA137F43f88b7F767F5A67978FfF8EC11Cc6Ef, 0x9d2F299715D94d8A7E6F5eaa8E654E8c74a988A7, 0, 'FXS', 'Frax Finance', 'Governance'),
('ramses', '3', 0xa45Cbd521ED745F6C856C89F8dBB583bD1591fC2, 0x82aF49447D8a07e3bd95BD0d56f35241523fBab1, 0, 'WETH', 'Frax Finance', 'Governance'),
('ramses', '3', 0xa45Cbd521ED745F6C856C89F8dBB583bD1591fC2, 0x9d2F299715D94d8A7E6F5eaa8E654E8c74a988A7, 0, 'FXS', 'Frax Finance', 'Governance')
) as t(
project,
version,
pool,
token_address,
token_is_rwa,
symbol,
protocol,
type
)
Loading
Loading