Skip to content

Commit

Permalink
DEXs: Curve, Pancakeswap (#212)
Browse files Browse the repository at this point in the history
  • Loading branch information
SebMelendez01 authored Jun 20, 2024
1 parent e311d6e commit f5bd32f
Show file tree
Hide file tree
Showing 42 changed files with 75 additions and 78 deletions.
29 changes: 13 additions & 16 deletions macros/decentralized_exchanges/fact_curve_dex_swaps.sql
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@
where
event_name in ('TokenExchange', 'TokenExchangeUnderlying')
{% if is_incremental() %}
where t1.block_timestamp
>= (select max(block_timestamp) from {{ this }})
and t1.block_timestamp >= (select dateadd('day', -3, max(block_timestamp)) from {{ this }})
{% endif %}
),
average_token_price_per_day as (
Expand Down Expand Up @@ -118,20 +117,16 @@

t1.token_out,
t2.symbol as token_out_symbol,
coalesce(t1.amount_out, 0) as amount_out,
t1.amount_out, 0 as amount_out,
t2.decimals as token_out_decimals,
coalesce(t2.price, t3.price) as token_out_price,
coalesce(
(amount_out / pow(10, token_out_decimals)) * token_out_price, 0
) as amount_out_usd,
t2.price as token_out_price,
amount_out / pow(10, token_out_decimals) * token_out_price as amount_out_usd,
t1.token_in,
t3.symbol as token_in_symbol,
coalesce(t1.amount_in, 0) as amount_in,
t1.amount_in as amount_in,
t3.decimals as token_in_decimals,
coalesce(t3.price, 0) as token_in_price,
coalesce(
(amount_in / pow(10, token_in_decimals)) * token_in_price, 0
) as amount_in_usd,
t3.price as token_in_price,
amount_in / pow(10, token_in_decimals) * token_in_price as amount_in_usd,
amount_out_usd * t1.swap_fee as trading_fee,
trading_fee * admin_fee as revenue
from pool_events t1
Expand All @@ -145,7 +140,7 @@
and lower(t1.token_in) = lower(t3.token_address)
),
events as (
select
select
block_timestamp,
tx_hash,
event_index,
Expand All @@ -156,7 +151,7 @@
token_out_symbol,
token_in,
token_in_symbol,
least(amount_in_usd, amount_out_usd) as trading_volume,
coalesce(least(amount_in_usd, amount_out_usd), 0) as trading_volume,
trading_fee as trading_fees,
revenue as trading_revenue,
ROW_NUMBER() OVER (PARTITION by tx_hash, pool ORDER BY event_index) AS row_number
Expand All @@ -178,8 +173,7 @@
and substr(t1.input, 0, 10) in ('0x3df02124', '0xa6417ed6')
left join {{ _chain }}_flipside.core.fact_transactions t3 on t1.tx_hash = t3.tx_hash
{% if is_incremental() %}
where t1.block_timestamp
>= (select max(block_timestamp) from {{ this }})
where t1.block_timestamp >= (select dateadd('day', -3, max(block_timestamp)) from {{ this }})
{% endif %}
)

Expand Down Expand Up @@ -209,4 +203,7 @@
and events.pool = traces.to_address
and events.row_number = traces.row_number
where events.block_timestamp is not null
{% if is_incremental() %}
and events.block_timestamp >= (select dateadd('day', -3, max(block_timestamp)) from {{ this }})
{% endif %}
{% endmacro %}
4 changes: 2 additions & 2 deletions models/projects/curve/core/ez_curve_metrics_by_chain.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{
config(
materialized="table",
snowflake_warehouse="CURVE_SM",
snowflake_warehouse="CURVE",
database="curve",
schema="core",
alias="ez_metrics_by_chain",
Expand All @@ -28,7 +28,7 @@ with
trading_volume_by_pool.chain,
sum(trading_volume_by_pool.trading_volume) as trading_volume,
sum(trading_volume_by_pool.trading_fees) as trading_fees,
sum(trading_volume_by_pool.trading_revenue) as unique_traders,
sum(trading_volume_by_pool.trading_revenue) as trading_revenue,
sum(trading_volume_by_pool.unique_traders) as unique_traders,
sum(trading_volume_by_pool.gas_cost_native) as gas_cost_native,
sum(trading_volume_by_pool.gas_cost_usd) as gas_cost_usd
Expand Down
5 changes: 2 additions & 3 deletions models/projects/curve/core/ez_curve_metrics_by_pool.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{
config(
materialized="table",
snowflake_warehouse="CURVE_SM",
snowflake_warehouse="CURVE",
database="curve",
schema="core",
alias="ez_metrics_by_pool",
Expand Down Expand Up @@ -40,7 +40,6 @@ select
'curve' as app,
'DeFi' as category,
tvl_by_pool.chain,
tvl_by_pool.version,
tvl_by_pool.pool,
tvl_by_pool.tvl,
trading_volume_pool.trading_volume,
Expand All @@ -49,5 +48,5 @@ select
trading_volume_pool.gas_cost_native,
trading_volume_pool.gas_cost_usd
from tvl_by_pool
left join trading_volume_pool using(date, chain, version, pool)
left join trading_volume_pool using(date, chain, pool)
where tvl_by_pool.date < to_date(sysdate())
2 changes: 1 addition & 1 deletion models/projects/curve/raw/ez_curve_dex_swaps.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{
config(
materialized="table",
snowflake_warehouse="CURVE_SM",
snowflake_warehouse="CURVE",
database="curve",
schema="raw",
alias="ez_dex_swaps",
Expand Down
39 changes: 20 additions & 19 deletions models/staging/curve/dim_curve_pools.sql
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,26 @@ with

select
chain,
app,
max(app) as app,
'DeFi' as category,
pool_type,
registration_address,
max(pool_type) as pool_type,
max(registration_address) as registration_address,
pool_address,
token,
amplification_coefficient,
name,
symbol,
swap_fee,
admin_fee,
mid_fee,
out_fee,
coin_0,
coin_1,
coin_2,
coin_3,
underlying_coin_0,
underlying_coin_1,
underlying_coin_2,
underlying_coin_3
max(token) as token,
max(amplification_coefficient) as amplification_coefficient,
max(name) as name,
max(symbol) as symbol,
max(swap_fee) as swap_fee,
max(admin_fee) as admin_fee,
max(mid_fee) as mid_fee,
max(out_fee) as out_fee,
max(coin_0) as coin_0,
max(coin_1) as coin_1,
max(coin_2) as coin_2,
max(coin_3) as coin_3,
max(underlying_coin_0) as underlying_coin_0,
max(underlying_coin_1) as underlying_coin_1,
max(underlying_coin_2) as underlying_coin_2,
max(underlying_coin_3) as underlying_coin_3
from latest_entry
group by pool_address, chain
2 changes: 1 addition & 1 deletion models/staging/curve/dim_curve_pools_gold.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{ config(materialized="table", snowflake_warehouse="CURVE_SM") }}
{{ config(materialized="table", snowflake_warehouse="CURVE") }}

select
chain,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

{{
config(
materialized="table", snowflake_warehouse="CURVE_SM"
materialized="table", snowflake_warehouse="CURVE"
)
}}
{{ fact_daily_curve_trading_vol_fees_traders_by_pool("arbitrum", "fact_curve_arbitrum_dex_swaps") }}
2 changes: 1 addition & 1 deletion models/staging/curve/fact_curve_arbitrum_dex_swaps.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{
config(
materialized="incremental", unique_key="date", snowflake_warehouse="CURVE_SM"
materialized="incremental", unique_key=["tx_hash", "event_index"], snowflake_warehouse="CURVE"
)
}}
{{ fact_curve_dex_swaps("arbitrum") }}
2 changes: 1 addition & 1 deletion models/staging/curve/fact_curve_arbitrum_tvl_by_pool.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{
config(
materialized="table", unique_key="date", snowflake_warehouse="CURVE_SM"
materialized="table", unique_key="date", snowflake_warehouse="CURVE"
)
}}
{{ fact_curve_tvl_by_pool("arbitrum", "0x912ce59144191c1204e64559fe8253a0e49e6548") }}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

{{
config(
materialized="table", snowflake_warehouse="CURVE_SM"
materialized="table", snowflake_warehouse="CURVE"
)
}}
{{ fact_daily_curve_trading_vol_fees_traders_by_pool("avalanche", "fact_curve_avalanche_dex_swaps") }}
2 changes: 1 addition & 1 deletion models/staging/curve/fact_curve_avalanche_dex_swaps.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{
config(
materialized="incremental", unique_key="date", snowflake_warehouse="CURVE_SM"
materialized="incremental", unique_key=["tx_hash", "event_index"], snowflake_warehouse="CURVE"
)
}}
{{ fact_curve_dex_swaps("avalanche") }}
2 changes: 1 addition & 1 deletion models/staging/curve/fact_curve_avalanche_tvl_by_pool.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{
config(
materialized="table", unique_key="date", snowflake_warehouse="CURVE_SM"
materialized="table", unique_key="date", snowflake_warehouse="CURVE"
)
}}
{{ fact_curve_tvl_by_pool("avalanche", "0xB31f66AA3C1e785363F0875A1B74E27b85FD66c7") }}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

{{
config(
materialized="table", snowflake_warehouse="CURVE_SM"
materialized="table", snowflake_warehouse="CURVE"
)
}}
{{ fact_daily_curve_trading_vol_fees_traders_by_pool("ethereum", "fact_curve_ethereum_dex_swaps") }}
2 changes: 1 addition & 1 deletion models/staging/curve/fact_curve_ethereum_dex_swaps.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{
config(
materialized="incremental", unique_key="date", snowflake_warehouse="CURVE_SM"
materialized="incremental", unique_key=["tx_hash", "event_index"], snowflake_warehouse="CURVE"
)
}}
{{ fact_curve_dex_swaps("ethereum") }}
2 changes: 1 addition & 1 deletion models/staging/curve/fact_curve_ethereum_tvl_by_pool.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{
config(
materialized="table", unique_key="date", snowflake_warehouse="CURVE_SM"
materialized="table", unique_key="date", snowflake_warehouse="CURVE"
)
}}
{{ fact_curve_tvl_by_pool("ethereum", "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE") }}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

{{
config(
materialized="table", snowflake_warehouse="CURVE_SM"
materialized="table", snowflake_warehouse="CURVE"
)
}}
{{ fact_daily_curve_trading_vol_fees_traders_by_pool("optimism", "fact_curve_optimism_dex_swaps") }}
2 changes: 1 addition & 1 deletion models/staging/curve/fact_curve_optimism_dex_swaps.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{
config(
materialized="incremental", unique_key="date", snowflake_warehouse="CURVE_SM"
materialized="incremental", unique_key=["tx_hash", "event_index"], snowflake_warehouse="CURVE"
)
}}
{{ fact_curve_dex_swaps("optimism") }}
2 changes: 1 addition & 1 deletion models/staging/curve/fact_curve_optimism_tvl_by_pool.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{
config(
materialized="table", unique_key="date", snowflake_warehouse="CURVE_SM"
materialized="table", unique_key="date", snowflake_warehouse="CURVE"
)
}}
{{ fact_curve_tvl_by_pool("optimism", "0x4200000000000000000000000000000000000042") }}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
--depends_on {{ ref("fact_curve_polygon_dex_swaps") }}
{{
config(
materialized="table", snowflake_warehouse="CURVE_SM"
materialized="table", snowflake_warehouse="CURVE"
)
}}
{{ fact_daily_curve_trading_vol_fees_traders_by_pool("polygon", "fact_curve_polygon_dex_swaps") }}
2 changes: 1 addition & 1 deletion models/staging/curve/fact_curve_polygon_dex_swaps.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{
config(
materialized="incremental", unique_key="date", snowflake_warehouse="CURVE_SM"
materialized="incremental", unique_key=["tx_hash", "event_index"], snowflake_warehouse="CURVE"
)
}}
{{ fact_curve_dex_swaps("polygon") }}
2 changes: 1 addition & 1 deletion models/staging/curve/fact_curve_polygon_tvl_by_pool.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{
config(
materialized="table", unique_key="date", snowflake_warehouse="CURVE_SM"
materialized="table", unique_key="date", snowflake_warehouse="CURVE"
)
}}
{{ fact_curve_tvl_by_pool("polygon", "0x0000000000000000000000000000000000001010") }}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{
config(
materialized="incremental", unique_key="date", snowflake_warehouse="CURVE_SM"
materialized="incremental", unique_key="date", snowflake_warehouse="CURVE"
)
}}
{{ fact_curve_trading_vol_trading_fees_trading_revenue("arbitrum") }}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{
config(
materialized="incremental", unique_key="date", snowflake_warehouse="CURVE_SM"
materialized="incremental", unique_key="date", snowflake_warehouse="CURVE"
)
}}
{{ fact_curve_trading_vol_trading_fees_trading_revenue("avalanche") }}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{
config(
materialized="incremental", unique_key="date", snowflake_warehouse="CURVE_SM"
materialized="incremental", unique_key="date", snowflake_warehouse="CURVE"
)
}}
{{ fact_curve_trading_vol_trading_fees_trading_revenue("ethereum") }}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{
config(
materialized="incremental", unique_key="date", snowflake_warehouse="CURVE_SM"
materialized="incremental", unique_key="date", snowflake_warehouse="CURVE"
)
}}
{{ fact_curve_trading_vol_trading_fees_trading_revenue("optimism") }}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{
config(
materialized="incremental", unique_key="date", snowflake_warehouse="CURVE_SM"
materialized="incremental", unique_key="date", snowflake_warehouse="CURVE"
)
}}
{{ fact_curve_trading_vol_trading_fees_trading_revenue("polygon") }}
2 changes: 1 addition & 1 deletion models/staging/curve/fact_curve_tvl_arbitrum.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{
config(
materialized="incremental", unique_key="date", snowflake_warehouse="CURVE_SM"
materialized="incremental", unique_key="date", snowflake_warehouse="CURVE"
)
}}
{{ fact_curve_tvl("arbitrum", "0x912ce59144191c1204e64559fe8253a0e49e6548") }}
2 changes: 1 addition & 1 deletion models/staging/curve/fact_curve_tvl_avalanche.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{
config(
materialized="incremental", unique_key="date", snowflake_warehouse="CURVE_SM"
materialized="incremental", unique_key="date", snowflake_warehouse="CURVE"
)
}}
{{ fact_curve_tvl("avalanche", "0xB31f66AA3C1e785363F0875A1B74E27b85FD66c7") }}
2 changes: 1 addition & 1 deletion models/staging/curve/fact_curve_tvl_ethereum.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{
config(
materialized="incremental", unique_key="date", snowflake_warehouse="CURVE_SM"
materialized="incremental", unique_key="date", snowflake_warehouse="CURVE"
)
}}
{{ fact_curve_tvl("ethereum", "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE") }}
2 changes: 1 addition & 1 deletion models/staging/curve/fact_curve_tvl_optimism.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{
config(
materialized="incremental", unique_key="date", snowflake_warehouse="CURVE_SM"
materialized="incremental", unique_key="date", snowflake_warehouse="CURVE"
)
}}
{{ fact_curve_tvl("optimism", "0x4200000000000000000000000000000000000042") }}
2 changes: 1 addition & 1 deletion models/staging/curve/fact_curve_tvl_polygon.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{
config(
materialized="incremental", unique_key="date", snowflake_warehouse="CURVE_SM"
materialized="incremental", unique_key="date", snowflake_warehouse="CURVE"
)
}}
{{ fact_curve_tvl("polygon", "0x0000000000000000000000000000000000001010") }}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{ config(materialized="table", snowflake_warehouse="CURVE_SM") }}
{{ config(materialized="table", snowflake_warehouse="CURVE") }}
select date, chain, category, app, trading_volume, fees, tvl, unique_traders
from {{ ref("fact_curve_trading_vol_trading_fees_trading_revenue_arbitrum") }}
full outer join {{ ref("fact_curve_tvl_arbitrum") }} using (date, chain, category, app)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{ config(materialized="table", snowflake_warehouse="CURVE_SM") }}
{{ config(materialized="table", snowflake_warehouse="CURVE") }}
select date, chain, category, app, trading_volume, fees, tvl, unique_traders
from {{ ref("fact_curve_trading_vol_trading_fees_trading_revenue_avalanche") }}
full outer join {{ ref("fact_curve_tvl_avalanche") }} using (date, chain, category, app)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{ config(materialized="table", snowflake_warehouse="CURVE_SM") }}
{{ config(materialized="table", snowflake_warehouse="CURVE") }}
select date, chain, category, app, trading_volume, fees, tvl, unique_traders
from {{ ref("fact_curve_trading_vol_trading_fees_trading_revenue_ethereum") }}
full outer join {{ ref("fact_curve_tvl_ethereum") }} using (date, chain, category, app)
Expand Down
Loading

0 comments on commit f5bd32f

Please sign in to comment.