-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
45 changed files
with
1,099 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,6 +65,7 @@ OPTIMISM_FLIPSIDE | |
OSMOSIS | ||
OSMOSIS_FLIPSIDE | ||
PC_DBT_DB | ||
PENDLE | ||
POLKADOT | ||
POLYGON | ||
POLYGON_FLIPSIDE | ||
|
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
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 |
---|---|---|
|
@@ -148,7 +148,7 @@ SELECT | |
FROM | ||
full_table | ||
WHERE | ||
USD_BALANCE > 1 | ||
USD_BALANCE > 100 | ||
GROUP BY | ||
1 | ||
, 2 | ||
|
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,32 @@ | ||
{% macro get_pendle_daus_txns_for_chain(chain)%} | ||
|
||
with | ||
swap_logs as ( | ||
SELECT | ||
block_timestamp, | ||
tx_hash, | ||
DECODED_LOG:caller::STRING AS caller, | ||
TRY_TO_NUMBER(DECODED_LOG:netPtOut::STRING) / 1e18 AS netPtOut, | ||
TRY_TO_NUMBER(DECODED_LOG:netSyFee::STRING) / 1e18 AS netSyFee, | ||
TRY_TO_NUMBER(DECODED_LOG:netSyOut::STRING) / 1e18 AS netSyOut, | ||
TRY_TO_NUMBER(DECODED_LOG:netSyToReserve::STRING) / 1e18 AS netSyToReserve, | ||
DECODED_LOG:receiver::STRING AS receiver | ||
, contract_address as market_address | ||
, origin_from_address as user | ||
FROM {{ chain }}_flipside.core.ez_decoded_event_logs | ||
WHERE event_name = 'Swap' | ||
{% if is_incremental() %} | ||
AND block_timestamp > (select max(date) from {{ this }}) | ||
{% endif %} | ||
AND DECODED_LOG:netSyFee IS NOT NULL | ||
AND DECODED_LOG:netSyToReserve IS NOT NULL | ||
) | ||
SELECT | ||
date(block_timestamp) as date | ||
, '{{ chain }}' as chain | ||
, count(distinct user) as DAU | ||
, count(distinct tx_hash) as daily_txns | ||
FROM swap_logs | ||
GROUP BY 1 | ||
|
||
{% endmacro %} |
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,34 @@ | ||
{% macro get_pendle_deposit_redeem_txns(chain) %} | ||
|
||
WITH sy_addresses AS ( | ||
SELECT sy_address | ||
FROM {{ref('dim_pendle_' ~ chain ~ '_market_metadata')}} | ||
) | ||
, decoded_events as ( | ||
SELECT | ||
* | ||
FROM ethereum_flipside.core.ez_decoded_event_logs | ||
WHERE contract_address IN (SELECT DISTINCT(sy_address) FROM sy_addresses) | ||
AND event_name IN ('Deposit', 'Redeem') | ||
{% if is_incremental() %} | ||
AND block_timestamp > (SELECT DATEADD(day, -1, MAX(block_timestamp)) FROM {{this}}) | ||
{% endif %} | ||
) | ||
SELECT | ||
DECODED_LOG:tokenIn::STRING as token_address, | ||
DECODED_LOG:amountDeposited::number as amount, | ||
contract_address as sy_address, | ||
* | ||
FROM decoded_events | ||
WHERE event_name = 'Deposit' | ||
UNION ALL | ||
SELECT | ||
DECODED_LOG:tokenOut::STRING as token_address, | ||
- DECODED_LOG:amountTokenOut::number as amount, | ||
contract_address as sy_address, | ||
* | ||
FROM decoded_events | ||
WHERE event_name = 'Redeem' | ||
|
||
|
||
{% endmacro %} |
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 @@ | ||
{% macro get_pendle_markets_for_chain(chain) %} | ||
|
||
with | ||
-- Filter to get only new 'CreateNewMarket' logs after the last run date | ||
pt_addresses as ( | ||
SELECT | ||
DECODED_LOG:PT::STRING as pt_address, | ||
DECODED_LOG:market::STRING as market_address, | ||
block_timestamp | ||
FROM | ||
{{ chain }}_flipside.core.ez_decoded_event_logs | ||
WHERE | ||
event_name = 'CreateNewMarket' | ||
{% if is_incremental() %} | ||
AND block_timestamp > dateadd(day, -3, to_date(sysdate())) | ||
{% endif %} | ||
), | ||
|
||
-- Filter to get new SY addresses related to new PTs | ||
sy_addresses as ( | ||
SELECT | ||
'0x' || SUBSTR(input, 35, 40) as sy_address, | ||
'0x' || SUBSTR(OUTPUT, 27, 40) as pt_address | ||
FROM | ||
{{ chain }}_flipside.core.fact_traces | ||
WHERE | ||
SUBSTR(input, 0, 10) = '0xe28a68b6' | ||
AND SUBSTR(input, 35, 40) is not null | ||
AND pt_address IN (SELECT pt_address FROM pt_addresses) | ||
{% if is_incremental() %} | ||
AND block_timestamp > dateadd(day, -3, to_date(sysdate())) | ||
{% endif %} | ||
), | ||
|
||
-- Identify new underlying addresses based on deposits related to new SYs | ||
deposits as ( | ||
SELECT | ||
contract_address as sy_address, | ||
DECODED_LOG:tokenIn::STRING as underlying_address, | ||
COUNT(*) as deposit_count | ||
FROM | ||
{{ chain }}_flipside.core.ez_decoded_event_logs | ||
WHERE | ||
event_name = 'Deposit' | ||
AND DECODED_LOG:tokenIn::STRING <> '0x0000000000000000000000000000000000000000' | ||
AND contract_address IN (SELECT sy_address FROM sy_addresses) | ||
{% if is_incremental() %} | ||
AND block_timestamp > dateadd(day, -3, to_date(sysdate())) | ||
{% endif %} | ||
GROUP BY | ||
contract_address, DECODED_LOG:tokenIn::STRING | ||
), | ||
|
||
-- Rank new underlying addresses by deposit count to get the most frequent one | ||
ranked_underlyings as ( | ||
SELECT | ||
sy_address, | ||
underlying_address, | ||
ROW_NUMBER() OVER (PARTITION BY sy_address ORDER BY deposit_count DESC) as rank | ||
FROM | ||
deposits | ||
), | ||
|
||
-- Filter to get only the top-ranked underlying address for each SY | ||
underlyings as ( | ||
SELECT | ||
sy_address, | ||
underlying_address | ||
FROM | ||
ranked_underlyings | ||
WHERE | ||
rank = 1 | ||
) | ||
|
||
-- Final query to combine all the information into the dim_pendle_markets_data table | ||
SELECT | ||
p.market_address, | ||
p.pt_address, | ||
s.sy_address, | ||
u.underlying_address, | ||
MAX(block_timestamp) as block_timestamp | ||
FROM | ||
pt_addresses p | ||
LEFT JOIN | ||
sy_addresses s ON p.pt_address = s.pt_address | ||
LEFT JOIN | ||
underlyings u ON s.sy_address = u.sy_address | ||
WHERE | ||
u.underlying_address IS NOT NULL | ||
GROUP BY | ||
p.market_address, | ||
p.pt_address, | ||
s.sy_address, | ||
u.underlying_address | ||
{% endmacro %} |
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,67 @@ | ||
{% macro get_pendle_swap_fees_for_chain_by_token(chain, blacklist=(''))%} | ||
|
||
with | ||
swap_logs as ( | ||
SELECT | ||
block_timestamp, | ||
tx_hash, | ||
DECODED_LOG:caller::STRING AS caller, | ||
TRY_TO_NUMBER(DECODED_LOG:netPtOut::STRING) / 1e18 AS netPtOut, | ||
TRY_TO_NUMBER(DECODED_LOG:netSyFee::STRING) / 1e18 AS netSyFee, | ||
TRY_TO_NUMBER(DECODED_LOG:netSyOut::STRING) / 1e18 AS netSyOut, | ||
TRY_TO_NUMBER(DECODED_LOG:netSyToReserve::STRING) / 1e18 AS netSyToReserve, | ||
DECODED_LOG:receiver::STRING AS receiver | ||
, contract_address as market_address | ||
FROM {{ chain }}_flipside.core.ez_decoded_event_logs | ||
WHERE event_name = 'Swap' | ||
AND DECODED_LOG:netSyFee IS NOT NULL | ||
AND DECODED_LOG:netSyToReserve IS NOT NULL | ||
{% if is_incremental() %} | ||
AND block_timestamp > (select max(date)-1 from {{ this }}) | ||
{% endif %} | ||
{% if blacklist is string %} AND lower(contract_address) != '{{ blacklist }}' | ||
{% elif blacklist | length > 1 %} AND lower(contract_address) not in {{ blacklist }} | ||
{% endif %} | ||
) | ||
, market_metadata as ( | ||
SELECT | ||
market_address, | ||
pt_address, | ||
sy_address, | ||
underlying_address | ||
FROM | ||
{{ ref("dim_pendle_" ~ chain ~ "_market_metadata") }} | ||
) | ||
, swaps_with_meta_data as ( | ||
SELECT | ||
l.block_timestamp | ||
, p.symbol | ||
, p.price | ||
, l.netSyFee * p.price as fee_usd | ||
, l.netSyFee as fee_native | ||
, l.netSyOut * p.price as volume_usd | ||
, l.netSyOut as volume_native | ||
, l.netSyToReserve * p.price as revenue_usd | ||
, l.netSyToReserve as revenue_native | ||
, m.market_address | ||
, m.underlying_address | ||
FROM | ||
swap_logs l | ||
LEFT JOIN market_metadata m on m.market_address = l.market_address | ||
LEFT JOIN {{ chain }}_flipside.price.ez_prices_hourly p on p.hour = date_trunc('hour', l.block_timestamp) AND lower(p.token_address) = lower(m.underlying_address) | ||
) | ||
|
||
SELECT | ||
date(block_timestamp) as date | ||
, '{{ chain }}' as chain | ||
, symbol | ||
, SUM(fee_usd) as fee_usd | ||
, SUM(fee_native) as fee_native | ||
, SUM(volume_usd) as volume_usd | ||
, SUM(volume_native) as volume_native | ||
, SUM(revenue_usd) as revenue_usd | ||
, SUM(revenue_native) as revenue_native | ||
FROM swaps_with_meta_data | ||
GROUP BY 1, 2, 3 | ||
|
||
{% endmacro %} |
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,64 @@ | ||
{% macro get_pendle_tvl_for_chain_by_token(chain)%} | ||
|
||
with | ||
distinct_sy_underlyings as ( | ||
SELECT distinct sy_address, underlying_address FROM {{ref('dim_pendle_' ~ chain ~ '_market_metadata')}} | ||
{% if chain == 'arbitrum' %} | ||
WHERE sy_address not in ('0xc79d8a2aa6d769138e599d4dbc30569c9870a6ee', '0x318eec91f653ca72fafb038f9ad792a6bc0d644c') | ||
{% endif %} | ||
) | ||
, prices as ( | ||
SELECT | ||
date(hour) as date | ||
, symbol | ||
, token_address | ||
, avg(price) as price | ||
FROM | ||
{{chain}}_flipside.price.ez_prices_hourly | ||
where token_address in (select underlying_address from distinct_sy_underlyings) | ||
and hour > date('2022-11-23') | ||
group by 1, 2, 3 | ||
) | ||
, all_combinations as ( | ||
SELECT p.date, u.underlying_address | ||
FROM prices p | ||
CROSS JOIN distinct_sy_underlyings u | ||
) | ||
, cum as ( | ||
select | ||
date(f.block_timestamp) as date | ||
, m.underlying_address | ||
, sum(f.amount / pow(10, 18)) OVER (PARTITION BY m.underlying_address ORDER BY f.block_timestamp) as cum_sum | ||
from | ||
{{ref('fact_pendle_' ~ chain ~ '_deposit_redeem_txns')}} f | ||
left join distinct_sy_underlyings m on m.sy_address = f.sy_address | ||
{% if chain == 'ethereum' %} | ||
where f.sy_address <> lower('0x065347C1Dd7A23Aa043e3844B4D0746ff7715246') | ||
{% endif %} | ||
), | ||
filled_cum AS ( | ||
SELECT | ||
ac.date, | ||
ac.underlying_address, | ||
LAST_VALUE(c.cum_sum IGNORE NULLS) OVER ( | ||
PARTITION BY ac.underlying_address | ||
ORDER BY ac.date | ||
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW | ||
) AS cum_sum | ||
FROM all_combinations ac | ||
LEFT JOIN cum c ON ac.date = c.date AND ac.underlying_address = c.underlying_address | ||
) | ||
SELECT | ||
fc.date as date, | ||
'{{chain}}' AS chain, | ||
p.symbol as symbol, | ||
p.token_address as token_address, | ||
AVG(fc.cum_sum) AS amount_native, | ||
AVG(fc.cum_sum * p.price) AS amount_usd | ||
FROM filled_cum fc | ||
LEFT JOIN prices p ON p.token_address = fc.underlying_address AND fc.date = p.date | ||
WHERE fc.cum_sum is not null | ||
GROUP BY 1, 2, 3, 4 | ||
ORDER BY 1, 3 | ||
|
||
{% endmacro %} |
50 changes: 50 additions & 0 deletions
50
macros/pendle/get_pendle_yield_fees_for_chain_by_token.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,50 @@ | ||
{% macro get_pendle_yield_fees_for_chain_by_token(chain)%} | ||
|
||
with | ||
yt_addresses as ( | ||
SELECT | ||
DECODED_LOG:YT::STRING as yt_address | ||
, DECODED_LOG:SY::STRING as sy_address | ||
FROM | ||
{{ chain }}_flipside.core.ez_decoded_event_logs | ||
WHERE event_name = 'CreateYieldContract' | ||
) | ||
, fees as ( | ||
SELECT | ||
block_timestamp | ||
, tx_hash | ||
, contract_address as yt_address | ||
, yt.sy_address | ||
, decoded_log:amountInterestFee::number /1e18 as fee_sy_amount | ||
FROM | ||
{{ chain }}_flipside.core.ez_decoded_event_logs l | ||
LEFT JOIN yt_addresses yt ON yt.yt_address = l.contract_address | ||
WHERE event_name = 'CollectInterestFee' | ||
and contract_address in (SELECT distinct yt_address FROM yt_addresses) | ||
{% if is_incremental() %} | ||
AND block_timestamp > (select max(date) from {{ this }}) | ||
{% endif %} | ||
) | ||
, market_metadata as ( | ||
SELECT | ||
market_address, | ||
pt_address, | ||
sy_address, | ||
underlying_address | ||
FROM | ||
{{ ref("dim_pendle_" ~ chain ~ "_market_metadata") }} | ||
) | ||
SELECT | ||
distinct | ||
date(block_timestamp) as date | ||
, tx_hash | ||
, underlying_address as token_address | ||
, p.symbol as token | ||
, fee_sy_amount * p.price as yield_fee_usd | ||
, fee_sy_amount as yield_fee_native | ||
FROM | ||
fees f | ||
LEFT JOIN market_metadata m ON f.sy_address = m.sy_address | ||
LEFT JOIN {{ chain }}_flipside.price.ez_prices_hourly p ON p.hour = date_trunc('hour', f.block_timestamp) AND lower(p.token_address) = lower(m.underlying_address) | ||
|
||
{% endmacro %} |
Oops, something went wrong.