Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
132 changes: 124 additions & 8 deletions models/ethereum/aave/aave__market_stats.sql
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,38 @@ WITH aave_reads AS (
Table(Flatten(aave_reads.coins))
WHERE value::STRING <> '0x0000000000000000000000000000000000000000'-- AND inputs:address IS NULL
),
-- cast to a wide format next putting one column for every field in every read
-- cast to a wide format next putting one column for every field in every read
wide_format AS (
SELECT *
FROM long_format
SELECT
blockhour,
aave_version,
lending_pool_add,
lending_pool_type,
reserve_token,
"'averagestableborrowrate'",
"'id'",
"'interestratestrategyaddress'",
"'stabledebttokenaddress'",
"'currentstableborrowrate'",
"'currentvariableborrowrate'",
"'totalliquidity'",
"'currentliquidityrate'",
"'variableborrowindex'",
"'variableborrowrate'",
"'atokenaddress'",
"'availableliquidity'",
"'version'",
"'liquidityrate'",
"'stableborrowrate'",
"'totalborrowsstable'",
"'totalborrowsvariable'",
"'lastupdatetimestamp'",
"'liquidityindex'",
"'utilizationrate'",
"'variabledebttokenaddress'",
"'totalstabledebt'",
"'totalvariabledebt'"
FROM long_format
pivot(max(value) for field_name
IN ('averagestableborrowrate', 'id', 'interestratestrategyaddress', 'stabledebttokenaddress',
'currentstableborrowrate', 'currentvariableborrowrate', 'totalliquidity', 'currentliquidityrate',
Expand Down Expand Up @@ -96,11 +124,65 @@ reads_parsed AS (
),
-- splitting these up for organization
lending_pools_v2 AS (
SELECT * FROM reads_parsed WHERE lending_pool_type = 'LendingPool' AND aave_version <> 'Aave V1'
SELECT
blockhour,
aave_version,
lending_pool_add,
lending_pool_type,
reserve_token,
atoken_address,
stable_debt_token_address,
variable_debt_token_address,
available_liquidity,
liquidity_rate,
stbl_borrow_rate,
variable_borrow_rate,
total_stable_debt,
total_variable_debt,
utilization_rate
FROM reads_parsed
WHERE lending_pool_type = 'LendingPool'
AND aave_version <> 'Aave V1'
), data_providers_v2 AS (
SELECT * FROM reads_parsed WHERE lending_pool_type = 'DataProvider' AND aave_version <> 'Aave V1'
SELECT
blockhour,
aave_version,
lending_pool_add,
lending_pool_type,
reserve_token,
atoken_address,
stable_debt_token_address,
variable_debt_token_address,
available_liquidity,
liquidity_rate,
stbl_borrow_rate,
variable_borrow_rate,
total_stable_debt,
total_variable_debt,
utilization_rate
FROM reads_parsed
WHERE lending_pool_type = 'DataProvider'
AND aave_version <> 'Aave V1'
), lending_pools_v1 AS (
SELECT * FROM reads_parsed WHERE lending_pool_type = 'LendingPool' AND aave_version = 'Aave V1'
SELECT
blockhour,
aave_version,
lending_pool_add,
lending_pool_type,
reserve_token,
atoken_address,
stable_debt_token_address,
variable_debt_token_address,
available_liquidity,
liquidity_rate,
stbl_borrow_rate,
variable_borrow_rate,
total_stable_debt,
total_variable_debt,
utilization_rate
FROM reads_parsed
WHERE lending_pool_type = 'LendingPool'
AND aave_version = 'Aave V1'
),
-- format v2/amm data. Need to combine reads from the lending pool and data provider
aave_v2 AS (
Expand Down Expand Up @@ -156,9 +238,43 @@ aave_v1 AS (
),
aave AS (

SELECT * FROM aave_v2
SELECT
blockhour,
reserve_token,
aave_version,
lending_pool_add,
data_provider,
atoken_address,
stable_debt_token_address,
variable_debt_token_address,
total_liquidity,
liquidity_rate,
stbl_borrow_rate,
variable_borrow_rate,
total_stable_debt,
total_variable_debt,
utilization_rate
FROM aave_v2

UNION
SELECT * FROM aave_v1

SELECT
blockhour,
reserve_token,
aave_version,
lending_pool_add,
data_provider,
atoken_address,
stable_debt_token_address,
variable_debt_token_address,
total_liquidity,
liquidity_rate,
stbl_borrow_rate,
variable_borrow_rate,
total_stable_debt,
total_variable_debt,
utilization_rate
FROM aave_v1

),
-- get decimals, preferably from contract reads but using the prices table as a fallback
Expand Down
12 changes: 11 additions & 1 deletion models/ethereum/compound/compound__market_stats.sql
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,17 @@ comptr AS (
p.token_price as comp_price,
comp_price * comp_speed as comp_speed_usd
FROM {{ref('ethereum__reads')}} erd
JOIN (SELECT * FROM prices WHERE symbol = 'COMP') p
JOIN (
SELECT
block_hour,
token_price,
token_decimals,
symbol,
token_contract,
address
FROM prices
WHERE symbol = 'COMP'
) p
ON date_trunc('hour',erd.block_timestamp) = p.block_hour
WHERE
contract_address = '0x3d9819210a31b4961b30ef54be2aed79b9c9cd3b'
Expand Down
80 changes: 69 additions & 11 deletions models/ethereum/dex/ethereum__dex_liquidity_pools.sql
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,15 @@ WITH v3_pools AS ( -- uni v3
FROM flipside_dev_db.dbt.sushi_liquidity_pools

), new_sushi AS (
SELECT s.* -- future proofing: once the eth backfill is done these manual write-ins will be dups
SELECT
s.creation_time,
s.creation_tx,
s.factory_address,
s.pool_name,
s.pool_address,
s.token0,
s.token1,
s.platform -- future proofing: once the eth backfill is done these manual write-ins will be dups
FROM sushi_write_in s
LEFT JOIN v2_pools v
ON s.pool_address = v.pool_address
Expand All @@ -131,26 +139,65 @@ WITH v3_pools AS ( -- uni v3

stack AS (
-- get pool info
SELECT * FROM
v2_pools
SELECT
creation_time,
creation_tx,
factory_address,
pool_name,
pool_address,
token0,
token1,
platform
FROM v2_pools

UNION

SELECT * FROM
v2_redshift
SELECT
creation_time,
creation_tx,
factory_address,
pool_name,
pool_address,
token0,
token1,
platform
FROM v2_redshift

UNION

SELECT * FROM
new_sushi
SELECT
creation_time,
creation_tx,
factory_address,
pool_name,
pool_address,
token0,
token1,
platform
FROM new_sushi

UNION

SELECT * FROM
v3_pools
SELECT
creation_time,
creation_tx,
factory_address,
pool_name,
pool_address,
token0,
token1,
platform
FROM v3_pools
), curve AS (
SELECT
*,
creation_time,
creation_tx,
factory_address,
pool_name,
pool_address,
token0,
token1,
platform,
ARRAY_CONSTRUCT(token0,token1) AS tokens
FROM stack
WHERE pool_address IS NOT NULL AND token0 IS NOT NULL AND token1 IS NOT NULL
Expand All @@ -171,7 +218,18 @@ stack AS (
)


SELECT DISTINCT * FROM
SELECT
DISTINCT
creation_time,
creation_tx,
factory_address,
pool_name,
pool_address,
token0,
token1,
platform,
tokens
FROM
curve
WHERE pool_address IS NOT NULL

15 changes: 14 additions & 1 deletion models/ethereum/dex/ethereum__dex_swaps.sql
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,20 @@ prices_daily_backup AS(

UNION

SELECT *
SELECT
block_timestamp,
pool_address,
pool_name,
token_address,
tx_id,
amount_in,
amount_out,
from_address,
to_address,
amount_usd,
platform,
event_index,
direction
FROM v3_swaps

UNION
Expand Down
17 changes: 16 additions & 1 deletion models/ethereum/ethereum__erc20_balances.sql
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,19 @@ balances AS (
balance_date >= getdate() - interval '9 months'
{% endif %}
)
SELECT * FROM balances ORDER BY balance_date DESC
SELECT
balance_date,
user_address,
label,
address_name,
label_type,
label_subtype,
contract_address,
contract_label,
symbol,
price,
balance,
-- Value of the token in USD
amount_usd
FROM balances
ORDER BY balance_date DESC
Loading