From ccdc9652907d522dea6b9b94d4f99110642afb81 Mon Sep 17 00:00:00 2001 From: Matt Romano <42412983+mattromano@users.noreply.github.com> Date: Mon, 29 Jan 2024 14:28:05 -0800 Subject: [PATCH] AN-3831/add-evm-lending-protocols (#237) * add sonne and aave protocols * add granary finance * all exactly but liquidations * fix liquidations * tarot models * fix tarot liq * test fixes * add complete models template * add gold layer + docs * missing inc tag * test fixes * renames * event name fix * doc updates * push back recency tests * overview fix * gen doc fix * pr comment fixes * consolidate docs * tarot asset detail fix --- .../complete_lending_column_docs.md | 381 ++++++++++++++++++ .../complete_lending_table_docs.md | 36 ++ .../doc_descriptions/general/__overview__.md | 6 + .../defi/lending/defi__ez_lending_borrows.sql | 48 +++ .../defi/lending/defi__ez_lending_borrows.yml | 46 +++ .../lending/defi__ez_lending_deposits.sql | 48 +++ .../lending/defi__ez_lending_deposits.yml | 46 +++ .../lending/defi__ez_lending_flashloans.sql | 52 +++ .../lending/defi__ez_lending_flashloans.yml | 54 +++ .../lending/defi__ez_lending_liquidations.sql | 51 +++ .../lending/defi__ez_lending_liquidations.yml | 52 +++ .../lending/defi__ez_lending_repayments.sql | 49 +++ .../lending/defi__ez_lending_repayments.yml | 48 +++ .../lending/defi__ez_lending_withdraws.sql | 48 +++ .../lending/defi__ez_lending_withdraws.yml | 46 +++ .../lending/aave/silver__aave_borrows.sql | 116 ++++++ .../lending/aave/silver__aave_borrows.yml | 52 +++ .../lending/aave/silver__aave_deposits.sql | 104 +++++ .../lending/aave/silver__aave_deposits.yml | 48 +++ .../lending/aave/silver__aave_flashloans.sql | 111 +++++ .../lending/aave/silver__aave_flashloans.yml | 49 +++ .../aave/silver__aave_liquidations.sql | 109 +++++ .../aave/silver__aave_liquidations.yml | 53 +++ .../lending/aave/silver__aave_repayments.sql | 99 +++++ .../lending/aave/silver__aave_repayments.yml | 52 +++ .../defi/lending/aave/silver__aave_tokens.sql | 123 ++++++ .../lending/aave/silver__aave_withdraws.sql | 96 +++++ .../lending/aave/silver__aave_withdraws.yml | 46 +++ .../silver__complete_lending_borrows.sql | 259 ++++++++++++ .../silver__complete_lending_borrows.yml | 55 +++ .../silver__complete_lending_deposits.sql | 256 ++++++++++++ .../silver__complete_lending_deposits.yml | 54 +++ .../silver__complete_lending_flashloans.sql | 151 +++++++ .../silver__complete_lending_flashloans.yml | 54 +++ .../silver__complete_lending_liquidations.sql | 316 +++++++++++++++ .../silver__complete_lending_liquidations.yml | 58 +++ .../silver__complete_lending_repayments.sql | 265 ++++++++++++ .../silver__complete_lending_repayments.yml | 58 +++ .../silver__complete_lending_withdraws.sql | 272 +++++++++++++ .../silver__complete_lending_withdraws.yml | 56 +++ .../exactly/silver__exactly_asset_details.sql | 98 +++++ .../exactly/silver__exactly_borrows.sql | 116 ++++++ .../exactly/silver__exactly_borrows.yml | 42 ++ .../exactly/silver__exactly_deposits.sql | 123 ++++++ .../exactly/silver__exactly_deposits.yml | 50 +++ .../exactly/silver__exactly_liquidations.sql | 115 ++++++ .../exactly/silver__exactly_liquidations.yml | 56 +++ .../exactly/silver__exactly_repayments.sql | 117 ++++++ .../exactly/silver__exactly_repayments.yml | 44 ++ .../exactly/silver__exactly_withdraws.sql | 117 ++++++ .../exactly/silver__exactly_withdraws.yml | 47 +++ .../granary/silver__granary_borrows.sql | 116 ++++++ .../granary/silver__granary_borrows.yml | 52 +++ .../granary/silver__granary_deposits.sql | 104 +++++ .../granary/silver__granary_deposits.yml | 48 +++ .../granary/silver__granary_flashloans.sql | 111 +++++ .../granary/silver__granary_flashloans.yml | 49 +++ .../granary/silver__granary_liquidations.sql | 109 +++++ .../granary/silver__granary_liquidations.yml | 53 +++ .../granary/silver__granary_repayments.sql | 99 +++++ .../granary/silver__granary_repayments.yml | 52 +++ .../granary/silver__granary_tokens.sql | 123 ++++++ .../granary/silver__granary_withdraws.sql | 96 +++++ .../granary/silver__granary_withdraws.yml | 46 +++ .../sonne/silver__sonne_asset_details.sql | 97 +++++ .../lending/sonne/silver__sonne_borrows.sql | 120 ++++++ .../lending/sonne/silver__sonne_borrows.yml | 42 ++ .../lending/sonne/silver__sonne_deposits.sql | 123 ++++++ .../lending/sonne/silver__sonne_deposits.yml | 50 +++ .../sonne/silver__sonne_liquidations.sql | 114 ++++++ .../sonne/silver__sonne_liquidations.yml | 56 +++ .../sonne/silver__sonne_repayments.sql | 117 ++++++ .../sonne/silver__sonne_repayments.yml | 44 ++ .../lending/sonne/silver__sonne_withdraws.sql | 123 ++++++ .../lending/sonne/silver__sonne_withdraws.yml | 47 +++ .../lending/tarot/silver__tarot_borrows.sql | 101 +++++ .../lending/tarot/silver__tarot_borrows.yml | 42 ++ .../lending/tarot/silver__tarot_deposits.sql | 99 +++++ .../lending/tarot/silver__tarot_deposits.yml | 47 +++ .../tarot/silver__tarot_liquidations.sql | 117 ++++++ .../tarot/silver__tarot_liquidations.yml | 56 +++ .../tarot/silver__tarot_liquidity_pools.sql | 103 +++++ .../tarot/silver__tarot_repayments.sql | 103 +++++ .../tarot/silver__tarot_repayments.yml | 44 ++ .../lending/tarot/silver__tarot_withdraws.sql | 94 +++++ .../lending/tarot/silver__tarot_withdraws.yml | 47 +++ 86 files changed, 7692 insertions(+) create mode 100644 models/doc_descriptions/complete_lending/complete_lending_column_docs.md create mode 100644 models/doc_descriptions/complete_lending/complete_lending_table_docs.md create mode 100644 models/gold/defi/lending/defi__ez_lending_borrows.sql create mode 100644 models/gold/defi/lending/defi__ez_lending_borrows.yml create mode 100644 models/gold/defi/lending/defi__ez_lending_deposits.sql create mode 100644 models/gold/defi/lending/defi__ez_lending_deposits.yml create mode 100644 models/gold/defi/lending/defi__ez_lending_flashloans.sql create mode 100644 models/gold/defi/lending/defi__ez_lending_flashloans.yml create mode 100644 models/gold/defi/lending/defi__ez_lending_liquidations.sql create mode 100644 models/gold/defi/lending/defi__ez_lending_liquidations.yml create mode 100644 models/gold/defi/lending/defi__ez_lending_repayments.sql create mode 100644 models/gold/defi/lending/defi__ez_lending_repayments.yml create mode 100644 models/gold/defi/lending/defi__ez_lending_withdraws.sql create mode 100644 models/gold/defi/lending/defi__ez_lending_withdraws.yml create mode 100644 models/silver/defi/lending/aave/silver__aave_borrows.sql create mode 100644 models/silver/defi/lending/aave/silver__aave_borrows.yml create mode 100644 models/silver/defi/lending/aave/silver__aave_deposits.sql create mode 100644 models/silver/defi/lending/aave/silver__aave_deposits.yml create mode 100644 models/silver/defi/lending/aave/silver__aave_flashloans.sql create mode 100644 models/silver/defi/lending/aave/silver__aave_flashloans.yml create mode 100644 models/silver/defi/lending/aave/silver__aave_liquidations.sql create mode 100644 models/silver/defi/lending/aave/silver__aave_liquidations.yml create mode 100644 models/silver/defi/lending/aave/silver__aave_repayments.sql create mode 100644 models/silver/defi/lending/aave/silver__aave_repayments.yml create mode 100644 models/silver/defi/lending/aave/silver__aave_tokens.sql create mode 100644 models/silver/defi/lending/aave/silver__aave_withdraws.sql create mode 100644 models/silver/defi/lending/aave/silver__aave_withdraws.yml create mode 100644 models/silver/defi/lending/complete_lending/silver__complete_lending_borrows.sql create mode 100644 models/silver/defi/lending/complete_lending/silver__complete_lending_borrows.yml create mode 100644 models/silver/defi/lending/complete_lending/silver__complete_lending_deposits.sql create mode 100644 models/silver/defi/lending/complete_lending/silver__complete_lending_deposits.yml create mode 100644 models/silver/defi/lending/complete_lending/silver__complete_lending_flashloans.sql create mode 100644 models/silver/defi/lending/complete_lending/silver__complete_lending_flashloans.yml create mode 100644 models/silver/defi/lending/complete_lending/silver__complete_lending_liquidations.sql create mode 100644 models/silver/defi/lending/complete_lending/silver__complete_lending_liquidations.yml create mode 100644 models/silver/defi/lending/complete_lending/silver__complete_lending_repayments.sql create mode 100644 models/silver/defi/lending/complete_lending/silver__complete_lending_repayments.yml create mode 100644 models/silver/defi/lending/complete_lending/silver__complete_lending_withdraws.sql create mode 100644 models/silver/defi/lending/complete_lending/silver__complete_lending_withdraws.yml create mode 100644 models/silver/defi/lending/exactly/silver__exactly_asset_details.sql create mode 100644 models/silver/defi/lending/exactly/silver__exactly_borrows.sql create mode 100644 models/silver/defi/lending/exactly/silver__exactly_borrows.yml create mode 100644 models/silver/defi/lending/exactly/silver__exactly_deposits.sql create mode 100644 models/silver/defi/lending/exactly/silver__exactly_deposits.yml create mode 100644 models/silver/defi/lending/exactly/silver__exactly_liquidations.sql create mode 100644 models/silver/defi/lending/exactly/silver__exactly_liquidations.yml create mode 100644 models/silver/defi/lending/exactly/silver__exactly_repayments.sql create mode 100644 models/silver/defi/lending/exactly/silver__exactly_repayments.yml create mode 100644 models/silver/defi/lending/exactly/silver__exactly_withdraws.sql create mode 100644 models/silver/defi/lending/exactly/silver__exactly_withdraws.yml create mode 100644 models/silver/defi/lending/granary/silver__granary_borrows.sql create mode 100644 models/silver/defi/lending/granary/silver__granary_borrows.yml create mode 100644 models/silver/defi/lending/granary/silver__granary_deposits.sql create mode 100644 models/silver/defi/lending/granary/silver__granary_deposits.yml create mode 100644 models/silver/defi/lending/granary/silver__granary_flashloans.sql create mode 100644 models/silver/defi/lending/granary/silver__granary_flashloans.yml create mode 100644 models/silver/defi/lending/granary/silver__granary_liquidations.sql create mode 100644 models/silver/defi/lending/granary/silver__granary_liquidations.yml create mode 100644 models/silver/defi/lending/granary/silver__granary_repayments.sql create mode 100644 models/silver/defi/lending/granary/silver__granary_repayments.yml create mode 100644 models/silver/defi/lending/granary/silver__granary_tokens.sql create mode 100644 models/silver/defi/lending/granary/silver__granary_withdraws.sql create mode 100644 models/silver/defi/lending/granary/silver__granary_withdraws.yml create mode 100644 models/silver/defi/lending/sonne/silver__sonne_asset_details.sql create mode 100644 models/silver/defi/lending/sonne/silver__sonne_borrows.sql create mode 100644 models/silver/defi/lending/sonne/silver__sonne_borrows.yml create mode 100644 models/silver/defi/lending/sonne/silver__sonne_deposits.sql create mode 100644 models/silver/defi/lending/sonne/silver__sonne_deposits.yml create mode 100644 models/silver/defi/lending/sonne/silver__sonne_liquidations.sql create mode 100644 models/silver/defi/lending/sonne/silver__sonne_liquidations.yml create mode 100644 models/silver/defi/lending/sonne/silver__sonne_repayments.sql create mode 100644 models/silver/defi/lending/sonne/silver__sonne_repayments.yml create mode 100644 models/silver/defi/lending/sonne/silver__sonne_withdraws.sql create mode 100644 models/silver/defi/lending/sonne/silver__sonne_withdraws.yml create mode 100644 models/silver/defi/lending/tarot/silver__tarot_borrows.sql create mode 100644 models/silver/defi/lending/tarot/silver__tarot_borrows.yml create mode 100644 models/silver/defi/lending/tarot/silver__tarot_deposits.sql create mode 100644 models/silver/defi/lending/tarot/silver__tarot_deposits.yml create mode 100644 models/silver/defi/lending/tarot/silver__tarot_liquidations.sql create mode 100644 models/silver/defi/lending/tarot/silver__tarot_liquidations.yml create mode 100644 models/silver/defi/lending/tarot/silver__tarot_liquidity_pools.sql create mode 100644 models/silver/defi/lending/tarot/silver__tarot_repayments.sql create mode 100644 models/silver/defi/lending/tarot/silver__tarot_repayments.yml create mode 100644 models/silver/defi/lending/tarot/silver__tarot_withdraws.sql create mode 100644 models/silver/defi/lending/tarot/silver__tarot_withdraws.yml diff --git a/models/doc_descriptions/complete_lending/complete_lending_column_docs.md b/models/doc_descriptions/complete_lending/complete_lending_column_docs.md new file mode 100644 index 00000000..47abca4f --- /dev/null +++ b/models/doc_descriptions/complete_lending/complete_lending_column_docs.md @@ -0,0 +1,381 @@ +{% docs complete_lending_borrow_rate_mode %} + +The rate mode the user is swapping from. Stable: 1, Variable: 2. Borrowers can switch between the stable and variable rate at any time. Stable rates act as a fixed rate in the short-term, but can be re-balanced in the long-term in response to changes in market conditions. The variable rate is the rate based on the offer and demand. The stable rate, as its name indicates, will remain pretty stable and its the best option to plan how much interest you will have to pay. The variable rate will change over time and could be the optimal rate depending on market conditions. + +{% enddocs %} + +{% docs complete_lending_borrow_rate_stable %} + +The stable interest rate for borrowing assets. + +{% enddocs %} + +{% docs complete_lending_borrow_rate_variable %} + +The variable interest rate for borrowing assets. + +{% enddocs %} + +{% docs complete_lending_collateral_complete_lending_token %} + +The interest bearing token that's burned when a liquidation occurs. + +{% enddocs %} + +{% docs complete_lending_collateral_asset %} + +The asset provided as collateral, which can be liquidated. + +{% enddocs %} + +{% docs complete_lending_data_provider %} + +The protocol data provider contract address. + +{% enddocs %} + +{% docs complete_lending_debt_complete_lending_token %} + +The interest bearing token representing the debt. + +{% enddocs %} + +{% docs complete_lending_debt_asset %} + +The debt asset, which the user borrowed. + +{% enddocs %} + +{% docs complete_lending_debt_to_cover_amount %} + +The amount of debt the user must cover. + +{% enddocs %} + +{% docs complete_lending_debt_to_cover_amount_usd %} + +The amount of debt the user must cover, valued in USD. + +{% enddocs %} + +{% docs complete_lending_depositor_address %} + +The depositor's address. + +{% enddocs %} + +{% docs complete_lending_end_voting_period %} + +The block number in which the voting period ends. + +{% enddocs %} + +{% docs complete_lending_flashloan_amount %} + +The amount of assets flash loaned. + +{% enddocs %} + +{% docs complete_lending_flashloan_amount_usd %} + +The value of the flash loan amount, in USD. + +{% enddocs %} + +{% docs complete_lending_governance_contract %} + +The governance contract address. + +{% enddocs %} + +{% docs complete_lending_initiator_address %} + +The address that initiated the flash loan. + +{% enddocs %} + +{% docs complete_lending_issued_tokens %} + +The amount of tokens that the user is depositing. + +{% enddocs %} + +{% docs complete_lending_lending_pool_contract %} + +The address of the lending pool. + +{% enddocs %} + +{% docs complete_lending_liquidated_amount %} + +The amount of asset liquidated. + +{% enddocs %} + +{% docs complete_lending_liquidated_amount_usd %} + +The value of the liquidated asset, in USD. + +{% enddocs %} + +{% docs complete_lending_liquidator %} + +The address that initiated the liquidation call. + +{% enddocs %} + +{% docs complete_lending_market %} + +The asset contract for the applicable market. + +{% enddocs %} + +{% docs complete_lending_payer %} + +The address that initiated the repayment. + +{% enddocs %} + +{% docs complete_lending_premium_amount %} + +The flash loan fee, changeable via the normal governance process, decimal adjusted. + +{% enddocs %} + +{% docs complete_lending_premium_amount_usd %} + +The flash loan fee, valued in USD. + +{% enddocs %} + +{% docs complete_lending_proposal_id %} + +The unique ID representing a proposal. + +{% enddocs %} + +{% docs complete_lending_proposal_tx %} + +The transaction confirming a proposal submission. + +{% enddocs %} + +{% docs complete_lending_proposer %} + +The user's address that submitted the proposal. + +{% enddocs %} + +{% docs complete_lending_repayed_tokens %} + +The amount of tokens repaid. + +{% enddocs %} + +{% docs complete_lending_repayed_usd %} + +The value of repaid tokens, in USD. + +{% enddocs %} + +{% docs complete_lending_stable_debt_token_address %} + +Debt tokens are interest-accruing tokens that are minted and burned on borrow and repay, representing a debt to the protocol with a stable interest rate. + +{% enddocs %} + +{% docs complete_lending_start_voting_period %} + +The block number in which the voting period begins. + +{% enddocs %} + +{% docs complete_lending_status %} + +The proposal's status. + +{% enddocs %} + +{% docs complete_lending_supplied_usd %} + +The value of the asset in USD that the user is depositing. + +{% enddocs %} + +{% docs complete_lending_supply_rate %} + +The interest rate for supplying assets to the protocol. + +{% enddocs %} + +{% docs complete_lending_support %} + +A value indicating their vote (For: true, Against: false). + +{% enddocs %} + +{% docs complete_lending_target_address %} + +The address receiving the flash loan. + +{% enddocs %} + +{% docs complete_lending_targets %} + +List of the targeted addresses by proposal transactions. + +{% enddocs %} + +{% docs complete_lending_token %} + +The interest bearing token contract. + +{% enddocs %} + +{% docs complete_lending_total_liquidity_token %} + +The total supply of liquidity tokens. + +{% enddocs %} + +{% docs complete_lending_total_liquidity_usd %} + +The total value of liquidity tokens, in USD. + +{% enddocs %} + +{% docs complete_lending_total_stable_debt_token %} + +The total supply of debt tokens, representing a debt to the protocol with a stable interest rate. + +{% enddocs %} + +{% docs complete_lending_total_stable_debt_usd %} + +The total USD value of debt tokens, representing a debt to the protocol with a stable interest rate. +{% enddocs %} + +{% docs complete_lending_total_variable_debt_token %} + +The total supply of debt tokens, representing a debt to the protocol with a variable interest rate. + +{% enddocs %} + +{% docs complete_lending_total_variable_debt_usd %} + +The total USD value of debt tokens, representing a debt to the protocol with a variable interest rate. +{% enddocs %} + +{% docs complete_lending_utilization_rate %} + +The percentage of assets loaned out. + +{% enddocs %} + +{% docs complete_lending_variable_debt_token_address %} + +Debt tokens are interest-accruing tokens that are minted and burned on borrow and repay, representing a debt to the protocol with a variable interest rate. + +{% enddocs %} + +{% docs complete_lending_version %} + +The contract version. Example: Aave AMM, Aave v1, Aave v2 + +{% enddocs %} + +{% docs complete_lending_withdrawn_tokens %} + +The amount of tokens withdrawn. + +{% enddocs %} + +{% docs complete_lending_withdrawn_usd %} + +The value of withdrawn tokens, in USD. + +{% enddocs %} + +{% docs complete_lending_platform %} + +The specific protocol where lending event occurred. + +{% enddocs %} + +{% docs complete_lending_protocol_token %} + +The protocol's specific lending asset token, ie cWBTC or aETHUni. + +{% enddocs %} + +{% docs complete_lending_borrower %} + +Address that initiated the borrow event. + +{% enddocs %} + +{% docs complete_lending_amount %} + +The decimal adjusted amount of tokens involved in the lending transaction, where available. + +{% enddocs %} + +{% docs complete_lending_amount_usd %} + +The value of the tokens in USD at the time of the lending transaction, where available. + +{% enddocs %} + +{% docs complete_lending_token_address %} + +The address of the token associated with the lending action. + +{% enddocs %} + +{% docs complete_lending_token_symbol %} + +The symbol of the token associated with the lending action. + +{% enddocs %} + +{% docs complete_lending_depositor %} + +Address that initiated a deposit event. + +{% enddocs %} + +{% docs complete_lending_amount_unadj %} + +The non-decimal adjusted amount of tokens involved in the lending transaction. + +{% enddocs %} + +{% docs complete_lending_premium_amount_unadj %} + +The flash loan fee, changeable via the normal governance process, non-decimal adjusted. + +{% enddocs %} + +{% docs complete_lending_flashloan_amount_unadj %} + +The amount of assets flash loaned, non-decimal adjusted. + +{% enddocs %} + +{% docs complete_lending_flashloan_token %} + +The flashloaned token address. + +{% enddocs %} + +{% docs complete_lending_flashloan_token_symbol %} + +The flashloaned token symbol + +{% enddocs %} + +{% docs borrower %} + +Its the address of the user who is Borrowing or repaying the loan, depending on the action. + +{% enddocs %} \ No newline at end of file diff --git a/models/doc_descriptions/complete_lending/complete_lending_table_docs.md b/models/doc_descriptions/complete_lending/complete_lending_table_docs.md new file mode 100644 index 00000000..66c1ee80 --- /dev/null +++ b/models/doc_descriptions/complete_lending/complete_lending_table_docs.md @@ -0,0 +1,36 @@ +{% docs complete_lending_borrows_table_doc %} + +This table contains transactions where users borrowed assets across AAVE, COMPOUND, SILO, DFORCE, RADIANT, and LODESTAR protocols. In order to borrow assets, a user must first deposit their preferred asset and amount as collateral. + + +{% enddocs %} + +{% docs complete_lending_deposits_table_doc %} + +This table contains deposit transactions across AAVE, COMPOUND, SILO, DFORCE, RADIANT, and LODESTAR protocols. A user deposits their preferred asset and amount. After depositing, users earn passive income based on the market borrowing demand. Additionally, depositing allows users to borrow by using their deposited assets as a collateral. Any interest earned by depositing funds helps offset the interest rate accumulated by borrowing. + +{% enddocs %} + +{% docs complete_lending_flashloans_table_doc %} + +This table contains flash loan transactions across AAVE and RADIANT protocols. Flash loans are a feature designed for developers, due to the technical knowledge required to execute one. Flash Loans allow you to borrow any available amount of assets without providing any collateral, as long as the liquidity is returned to the protocol within one block transaction. + +{% enddocs %} + +{% docs complete_lending_liquidations_table_doc %} + +This table contains transactions in which a borrower's collateral asset is liquidated across AAVE, COMPOUND, SILO, DFORCE, RADIANT, and LODESTAR protocols. Liquidations occur when a borrower's health factor goes below 1 due to their collateral value not properly covering their loan/debt value. This might happen when the collateral decreases in value or the borrowed debt increases in value against each other. This collateral vs loan value ratio is shown in the health factor. In a liquidation, up to 50% of a borrower's debt is repaid and that value + liquidation fee is taken from the collateral available, so after a liquidation the amount liquidated from one's debt is repaid. + +{% enddocs %} + +{% docs complete_lending_repayments_table_doc %} + +This table contains transactions in which a borrower repays their loan (debt) across the AAVE, COMPOUND, SILO, DFORCE, RADIANT, and LODESTAR protocols. Loans are repaid in the same asset borrowed, plus accrued interest. Borrowers can pay back their loan based on the USD price as they can borrow any of the available stable coins (USDC, DAI, USDT, etc.). + +{% enddocs %} + +{% docs complete_lending_withdraws_table_doc %} + +This table contains transactions in which a user withdraws liquidity across the AAVE, COMPOUND, SILO, DFORCE, RADIANT, and LODESTAR protocols. Users need to make sure there is enough liquidity (not borrowed) in order to withdraw, if this is not the case, users need to wait for more liquidity from depositors or borrowers repaying. + +{% enddocs %} \ No newline at end of file diff --git a/models/doc_descriptions/general/__overview__.md b/models/doc_descriptions/general/__overview__.md index 4b06d1fa..b04e1900 100644 --- a/models/doc_descriptions/general/__overview__.md +++ b/models/doc_descriptions/general/__overview__.md @@ -50,6 +50,12 @@ There is more information on how to use dbt docs in the last section of this doc - [ez_bridge_activity](https://flipsidecrypto.github.io/optimism-models/#!/model/model.optimism_models.defi__ez_bridge_activity) - [dim_dex_liquidity_pools](https://flipsidecrypto.github.io/optimism-models/#!/model/model.optimism_models.defi__dim_dex_liquidity_pools) - [ez_dex_swaps](https://flipsidecrypto.github.io/optimism-models/#!/model/model.optimism_models.defi__ez_dex_swaps) +- [ez_borrows](https://flipsidecrypto.github.io/optimism-models/#!/model/model.optimism_models.defi__ez_borrows) +- [ez_deposits](https://flipsidecrypto.github.io/optimism-models/#!/model/model.optimism_models.defi__ez_deposits) +- [ez_flashloans](https://flipsidecrypto.github.io/optimism-models/#!/model/model.optimism_models.defi__ez_flashloans) +- [ez_liquidations](https://flipsidecrypto.github.io/optimism-models/#!/model/model.optimism_models.defi__ez_liquidations) +- [ez_repayments](https://flipsidecrypto.github.io/optimism-models/#!/model/model.optimism_models.defi__ez_repayments) +- [ez_withdraws](https://flipsidecrypto.github.io/optimism-models/#!/model/model.optimism_models.defi__ez_withdraws) **Velodrome** - [ez_claimed_rewards](https://flipsidecrypto.github.io/optimism-models/#!/model/model.optimism_models.velodrome__ez_claimed_rewards) diff --git a/models/gold/defi/lending/defi__ez_lending_borrows.sql b/models/gold/defi/lending/defi__ez_lending_borrows.sql new file mode 100644 index 00000000..4859b75f --- /dev/null +++ b/models/gold/defi/lending/defi__ez_lending_borrows.sql @@ -0,0 +1,48 @@ + {{ config( + materialized = 'view', + persist_docs ={ "relation": true, + "columns": true }, + meta={ + 'database_tags':{ + 'table': { + 'PROTOCOL': 'SONNE, AAVE, GRANARY, TAROT, EXACTLY', + 'PURPOSE': 'LENDING, BORROWS' + } + } + } +) }} + +SELECT + block_number, + block_timestamp, + tx_hash, + contract_address, + event_name, + event_index, + origin_function_signature, + origin_from_address, + origin_to_address, + platform, + protocol_market, + borrower, + token_address, + token_symbol, + amount_unadj, + amount, + amount_usd, + COALESCE ( + complete_lending_borrows_id, + {{ dbt_utils.generate_surrogate_key( + ['tx_hash', 'event_index'] + ) }} + ) AS ez_lending_borrows_id, + COALESCE( + inserted_timestamp, + '2000-01-01' + ) AS inserted_timestamp, + COALESCE( + modified_timestamp, + '2000-01-01' + ) AS modified_timestamp +FROM + {{ ref('silver__complete_lending_borrows') }} \ No newline at end of file diff --git a/models/gold/defi/lending/defi__ez_lending_borrows.yml b/models/gold/defi/lending/defi__ez_lending_borrows.yml new file mode 100644 index 00000000..091aceed --- /dev/null +++ b/models/gold/defi/lending/defi__ez_lending_borrows.yml @@ -0,0 +1,46 @@ +version: 2 +models: + - name: defi__ez_lending_borrows + description: '{{ doc("complete_lending_borrows_table_doc") }}' + + columns: + - name: BLOCK_NUMBER + description: '{{ doc("opt_block_number") }}' + - name: BLOCK_TIMESTAMP + description: '{{ doc("opt_block_timestamp") }}' + - name: TX_HASH + description: '{{ doc("opt_logs_tx_hash") }}' + - name: CONTRACT_ADDRESS + description: '{{ doc("opt_logs_contract_address") }}' + - name: EVENT_NAME + description: '{{ doc("opt_event_name") }}' + - name: EVENT_INDEX + description: '{{ doc("opt_event_index") }}' + - name: ORIGIN_FUNCTION_SIGNATURE + description: '{{ doc("nft_origin_sig") }}' + - name: ORIGIN_FROM_ADDRESS + description: '{{ doc("opt_origin_from") }}' + - name: ORIGIN_TO_ADDRESS + description: '{{ doc("opt_origin_to") }}' + - name: PLATFORM + description: '{{ doc("complete_lending_platform") }}' + - name: PROTOCOL_MARKET + description: '{{ doc("complete_lending_protocol_token") }}' + - name: BORROWER + description: '{{ doc("complete_lending_borrower") }}' + - name: TOKEN_ADDRESS + description: '{{ doc("complete_lending_token_address") }}' + - name: TOKEN_SYMBOL + description: '{{ doc("opt_symbol") }}' + - name: AMOUNT_UNADJ + description: '{{ doc("complete_lending_amount_unadj") }}' + - name: AMOUNT + description: '{{ doc("complete_lending_amount") }}' + - name: AMOUNT_USD + description: '{{ doc("complete_lending_amount_usd") }}' + - name: EZ_LENDING_BORROWS_ID + description: '{{ doc("pk") }}' + - name: INSERTED_TIMESTAMP + description: '{{ doc("inserted_timestamp") }}' + - name: MODIFIED_TIMESTAMP + description: '{{ doc("modified_timestamp") }}' \ No newline at end of file diff --git a/models/gold/defi/lending/defi__ez_lending_deposits.sql b/models/gold/defi/lending/defi__ez_lending_deposits.sql new file mode 100644 index 00000000..2dfa1df4 --- /dev/null +++ b/models/gold/defi/lending/defi__ez_lending_deposits.sql @@ -0,0 +1,48 @@ +{{ config( + materialized = 'view', + persist_docs ={ "relation": true, + "columns": true }, + meta={ + 'database_tags':{ + 'table': { + 'PROTOCOL': 'SONNE, AAVE, GRANARY, TAROT, EXACTLY', + 'PURPOSE': 'LENDING, DEPOSITS' + } + } + } +) }} + +SELECT + block_number, + block_timestamp, + tx_hash, + contract_address, + event_name, + event_index, + origin_function_signature, + origin_from_address, + origin_to_address, + platform, + protocol_market, + depositor, + token_address, + token_symbol, + amount_unadj, + amount, + amount_usd, + COALESCE ( + complete_lending_deposits_id, + {{ dbt_utils.generate_surrogate_key( + ['tx_hash', 'event_index'] + ) }} + ) AS ez_lending_deposits_id, + COALESCE( + inserted_timestamp, + '2000-01-01' + ) AS inserted_timestamp, + COALESCE( + modified_timestamp, + '2000-01-01' + ) AS modified_timestamp +FROM + {{ ref('silver__complete_lending_deposits') }} \ No newline at end of file diff --git a/models/gold/defi/lending/defi__ez_lending_deposits.yml b/models/gold/defi/lending/defi__ez_lending_deposits.yml new file mode 100644 index 00000000..e458b755 --- /dev/null +++ b/models/gold/defi/lending/defi__ez_lending_deposits.yml @@ -0,0 +1,46 @@ +version: 2 +models: + - name: defi__ez_lending_deposits + description: '{{ doc("complete_lending_deposits_table_doc") }}' + + columns: + - name: BLOCK_NUMBER + description: '{{ doc("opt_block_number") }}' + - name: BLOCK_TIMESTAMP + description: '{{ doc("opt_block_timestamp") }}' + - name: TX_HASH + description: '{{ doc("opt_logs_tx_hash") }}' + - name: CONTRACT_ADDRESS + description: '{{ doc("opt_logs_contract_address") }}' + - name: EVENT_NAME + description: '{{ doc("opt_event_name") }}' + - name: EVENT_INDEX + description: '{{ doc("opt_event_index") }}' + - name: ORIGIN_FUNCTION_SIGNATURE + description: '{{ doc("nft_origin_sig") }}' + - name: ORIGIN_FROM_ADDRESS + description: '{{ doc("opt_origin_from") }}' + - name: ORIGIN_TO_ADDRESS + description: '{{ doc("opt_origin_to") }}' + - name: PLATFORM + description: '{{ doc("complete_lending_platform") }}' + - name: PROTOCOL_MARKET + description: '{{ doc("complete_lending_protocol_token") }}' + - name: DEPOSITOR + description: '{{ doc("complete_lending_depositor") }}' + - name: TOKEN_ADDRESS + description: '{{ doc("complete_lending_token_address") }}' + - name: TOKEN_SYMBOL + description: '{{ doc("opt_symbol") }}' + - name: AMOUNT_UNADJ + description: '{{ doc("complete_lending_amount_unadj") }}' + - name: AMOUNT + description: '{{ doc("complete_lending_amount") }}' + - name: AMOUNT_USD + description: '{{ doc("complete_lending_amount_usd") }}' + - name: EZ_LENDING_DEPOSITS_ID + description: '{{ doc("pk") }}' + - name: INSERTED_TIMESTAMP + description: '{{ doc("inserted_timestamp") }}' + - name: MODIFIED_TIMESTAMP + description: '{{ doc("modified_timestamp") }}' \ No newline at end of file diff --git a/models/gold/defi/lending/defi__ez_lending_flashloans.sql b/models/gold/defi/lending/defi__ez_lending_flashloans.sql new file mode 100644 index 00000000..658454a3 --- /dev/null +++ b/models/gold/defi/lending/defi__ez_lending_flashloans.sql @@ -0,0 +1,52 @@ + {{ config( + materialized = 'view', + persist_docs ={ "relation": true, + "columns": true }, + meta={ + 'database_tags':{ + 'table': { + 'PROTOCOL': 'AAVE, GRANARY', + 'PURPOSE': 'LENDING, FLASHLOANS' + } + } + } +) }} + +SELECT + block_number, + block_timestamp, + tx_hash, + contract_address, + event_name, + event_index, + origin_function_signature, + origin_from_address, + origin_to_address, + platform, + initiator, + target, + protocol_market, + flashloan_token, + flashloan_token_symbol, + flashloan_amount_unadj, + flashloan_amount, + flashloan_amount_usd, + premium_amount_unadj, + premium_amount, + premium_amount_usd, + COALESCE ( + complete_lending_flashloans_id, + {{ dbt_utils.generate_surrogate_key( + ['tx_hash', 'event_index'] + ) }} + ) AS ez_lending_flashloans_id, + COALESCE( + inserted_timestamp, + '2000-01-01' + ) AS inserted_timestamp, + COALESCE( + modified_timestamp, + '2000-01-01' + ) AS modified_timestamp +FROM + {{ ref('silver__complete_lending_flashloans') }} \ No newline at end of file diff --git a/models/gold/defi/lending/defi__ez_lending_flashloans.yml b/models/gold/defi/lending/defi__ez_lending_flashloans.yml new file mode 100644 index 00000000..b9f0be98 --- /dev/null +++ b/models/gold/defi/lending/defi__ez_lending_flashloans.yml @@ -0,0 +1,54 @@ +version: 2 +models: + - name: defi__ez_lending_flashloans + description: '{{ doc("complete_lending_flashloans_table_doc") }}' + + columns: + - name: BLOCK_NUMBER + description: '{{ doc("opt_block_number") }}' + - name: BLOCK_TIMESTAMP + description: '{{ doc("opt_block_timestamp") }}' + - name: TX_HASH + description: '{{ doc("opt_logs_tx_hash") }}' + - name: CONTRACT_ADDRESS + description: '{{ doc("opt_logs_contract_address") }}' + - name: EVENT_NAME + description: '{{ doc("opt_event_name") }}' + - name: EVENT_INDEX + description: '{{ doc("opt_event_index") }}' + - name: ORIGIN_FUNCTION_SIGNATURE + description: '{{ doc("nft_origin_sig") }}' + - name: ORIGIN_FROM_ADDRESS + description: '{{ doc("opt_origin_from") }}' + - name: ORIGIN_TO_ADDRESS + description: '{{ doc("opt_origin_to") }}' + - name: PLATFORM + description: '{{ doc("complete_lending_platform") }}' + - name: INITIATOR + description: '{{ doc("complete_lending_initiator_address") }}' + - name: TARGET + description: '{{ doc("complete_lending_target_address") }}' + - name: PROTOCOL_MARKET + description: '{{ doc("complete_lending_protocol_token") }}' + - name: FLASHLOAN_TOKEN + description: '{{ doc("complete_lending_flashloan_token") }}' + - name: FLASHLOAN_TOKEN_SYMBOL + description: '{{ doc("opt_symbol") }}' + - name: FLASHLOAN_AMOUNT_UNADJ + description: '{{ doc("complete_lending_flashloan_amount_unadj") }}' + - name: FLASHLOAN_AMOUNT + description: '{{ doc("complete_lending_flashloan_amount") }}' + - name: FLASHLOAN_AMOUNT_USD + description: '{{ doc("complete_lending_flashloan_amount_usd") }}' + - name: PREMIUM_AMOUNT_UNADJ + description: '{{ doc("complete_lending_premium_amount_unadj") }}' + - name: PREMIUM_AMOUNT + description: '{{ doc("complete_lending_premium_amount") }}' + - name: PREMIUM_AMOUNT_USD + description: '{{ doc("complete_lending_premium_amount_usd") }}' + - name: EZ_LENDING_FLASHLOANS_ID + description: '{{ doc("pk") }}' + - name: INSERTED_TIMESTAMP + description: '{{ doc("inserted_timestamp") }}' + - name: MODIFIED_TIMESTAMP + description: '{{ doc("modified_timestamp") }}' \ No newline at end of file diff --git a/models/gold/defi/lending/defi__ez_lending_liquidations.sql b/models/gold/defi/lending/defi__ez_lending_liquidations.sql new file mode 100644 index 00000000..ec472c3a --- /dev/null +++ b/models/gold/defi/lending/defi__ez_lending_liquidations.sql @@ -0,0 +1,51 @@ + {{ config( + materialized = 'view', + persist_docs ={ "relation": true, + "columns": true }, + meta={ + 'database_tags':{ + 'table': { + 'PROTOCOL': 'SONNE, AAVE, GRANARY, TAROT, EXACTLY', + 'PURPOSE': 'LENDING, LIQUIDATIONS' + } + } + } +) }} + +SELECT + block_number, + block_timestamp, + tx_hash, + contract_address, + event_name, + event_index, + origin_function_signature, + origin_from_address, + origin_to_address, + platform, + liquidator, + borrower, + protocol_market, + collateral_token, + collateral_token_symbol, + amount_unadj, + amount, + amount_usd, + debt_token, + debt_token_symbol, + COALESCE ( + complete_lending_liquidations_id, + {{ dbt_utils.generate_surrogate_key( + ['tx_hash', 'event_index'] + ) }} + ) AS ez_lending_liquidations_id, + COALESCE( + inserted_timestamp, + '2000-01-01' + ) AS inserted_timestamp, + COALESCE( + modified_timestamp, + '2000-01-01' + ) AS modified_timestamp +FROM + {{ ref('silver__complete_lending_liquidations') }} \ No newline at end of file diff --git a/models/gold/defi/lending/defi__ez_lending_liquidations.yml b/models/gold/defi/lending/defi__ez_lending_liquidations.yml new file mode 100644 index 00000000..916869be --- /dev/null +++ b/models/gold/defi/lending/defi__ez_lending_liquidations.yml @@ -0,0 +1,52 @@ +version: 2 +models: + - name: defi__ez_lending_liquidations + description: '{{ doc("complete_lending_liquidations_table_doc") }}' + + columns: + - name: BLOCK_NUMBER + description: '{{ doc("opt_block_number") }}' + - name: BLOCK_TIMESTAMP + description: '{{ doc("opt_block_timestamp") }}' + - name: TX_HASH + description: '{{ doc("opt_logs_tx_hash") }}' + - name: CONTRACT_ADDRESS + description: '{{ doc("opt_logs_contract_address") }}' + - name: EVENT_NAME + description: '{{ doc("opt_event_name") }}' + - name: EVENT_INDEX + description: '{{ doc("opt_event_index") }}' + - name: ORIGIN_FUNCTION_SIGNATURE + description: '{{ doc("nft_origin_sig") }}' + - name: ORIGIN_FROM_ADDRESS + description: '{{ doc("opt_origin_from") }}' + - name: ORIGIN_TO_ADDRESS + description: '{{ doc("opt_origin_to") }}' + - name: PLATFORM + description: '{{ doc("complete_lending_platform") }}' + - name: LIQUIDATOR + description: '{{ doc("complete_lending_liquidator") }}' + - name: BORROWER + description: '{{ doc("complete_lending_borrower") }}' + - name: PROTOCOL_MARKET + description: '{{ doc("complete_lending_protocol_token") }}' + - name: COLLATERAL_TOKEN + description: '{{ doc("complete_lending_collateral_asset") }}' + - name: COLLATERAL_TOKEN_SYMBOL + description: '{{ doc("opt_symbol") }}' + - name: LIQUIDATED_AMOUNT_UNADJU + description: '{{ doc("complete_lending_amount_unadj") }}' + - name: LIQUIDATED_AMOUNT + description: '{{ doc("complete_lending_amount") }}' + - name: LIQUIDATED_AMOUNT_USD + description: '{{ doc("complete_lending_amount_usd") }}' + - name: DEBT_TOKEN + description: '{{ doc("complete_lending_debt_asset") }}' + - name: DEBT_TOKEN_SYMBOL + description: '{{ doc("opt_symbol") }}' + - name: EZ_LENDING_LIQUDATIONS_ID + description: '{{ doc("pk") }}' + - name: INSERTED_TIMESTAMP + description: '{{ doc("inserted_timestamp") }}' + - name: MODIFIED_TIMESTAMP + description: '{{ doc("modified_timestamp") }}' \ No newline at end of file diff --git a/models/gold/defi/lending/defi__ez_lending_repayments.sql b/models/gold/defi/lending/defi__ez_lending_repayments.sql new file mode 100644 index 00000000..8680f11a --- /dev/null +++ b/models/gold/defi/lending/defi__ez_lending_repayments.sql @@ -0,0 +1,49 @@ + {{ config( + materialized = 'view', + persist_docs ={ "relation": true, + "columns": true }, + meta={ + 'database_tags':{ + 'table': { + 'PROTOCOL': 'SONNE, AAVE, GRANARY, TAROT, EXACTLY', + 'PURPOSE': 'LENDING, REPAYMENTS' + } + } + } +) }} + +SELECT + block_number, + block_timestamp, + origin_from_address, + origin_to_address, + origin_function_signature, + tx_hash, + event_index, + contract_address, + event_name, + platform, + payer, + borrower, + protocol_market, + token_address, + token_symbol, + amount_unadj, + amount, + amount_usd, + COALESCE ( + complete_lending_repayments_id, + {{ dbt_utils.generate_surrogate_key( + ['tx_hash', 'event_index'] + ) }} + ) AS ez_lending_repayments_id, + COALESCE( + inserted_timestamp, + '2000-01-01' + ) AS inserted_timestamp, + COALESCE( + modified_timestamp, + '2000-01-01' + ) AS modified_timestamp +FROM + {{ ref('silver__complete_lending_repayments') }} \ No newline at end of file diff --git a/models/gold/defi/lending/defi__ez_lending_repayments.yml b/models/gold/defi/lending/defi__ez_lending_repayments.yml new file mode 100644 index 00000000..17be1345 --- /dev/null +++ b/models/gold/defi/lending/defi__ez_lending_repayments.yml @@ -0,0 +1,48 @@ +version: 2 +models: + - name: defi__ez_lending_repayments + description: '{{ doc("complete_lending_repayments_table_doc") }}' + + columns: + - name: BLOCK_NUMBER + description: '{{ doc("opt_block_number") }}' + - name: BLOCK_TIMESTAMP + description: '{{ doc("opt_block_timestamp") }}' + - name: TX_HASH + description: '{{ doc("opt_logs_tx_hash") }}' + - name: CONTRACT_ADDRESS + description: '{{ doc("opt_logs_contract_address") }}' + - name: EVENT_NAME + description: '{{ doc("opt_event_name") }}' + - name: EVENT_INDEX + description: '{{ doc("opt_event_index") }}' + - name: ORIGIN_FUNCTION_SIGNATURE + description: '{{ doc("nft_origin_sig") }}' + - name: ORIGIN_FROM_ADDRESS + description: '{{ doc("opt_origin_from") }}' + - name: ORIGIN_TO_ADDRESS + description: '{{ doc("opt_origin_to") }}' + - name: PLATFORM + description: '{{ doc("complete_lending_platform") }}' + - name: PAYER + description: '{{ doc("complete_lending_payer") }}' + - name: BORROWER + description: '{{ doc("complete_lending_borrower") }}' + - name: PROTOCOL_MARKET + description: '{{ doc("complete_lending_protocol_token") }}' + - name: TOKEN_ADDRESS + description: '{{ doc("complete_lending_token_address") }}' + - name: TOKEN_SYMBOL + description: '{{ doc("complete_lending_token_symbol") }}' + - name: AMOUNT_UNADJ + description: '{{ doc("complete_lending_amount_unadj") }}' + - name: AMOUNT + description: '{{ doc("complete_lending_amount") }}' + - name: AMOUNT_USD + description: '{{ doc("complete_lending_amount_usd") }}' + - name: EZ_LENDING_REPAYMENTS_ID + description: '{{ doc("pk") }}' + - name: INSERTED_TIMESTAMP + description: '{{ doc("inserted_timestamp") }}' + - name: MODIFIED_TIMESTAMP + description: '{{ doc("modified_timestamp") }}' \ No newline at end of file diff --git a/models/gold/defi/lending/defi__ez_lending_withdraws.sql b/models/gold/defi/lending/defi__ez_lending_withdraws.sql new file mode 100644 index 00000000..3fea8777 --- /dev/null +++ b/models/gold/defi/lending/defi__ez_lending_withdraws.sql @@ -0,0 +1,48 @@ + {{ config( + materialized = 'view', + persist_docs ={ "relation": true, + "columns": true }, + meta={ + 'database_tags':{ + 'table': { + 'PROTOCOL': 'SONNE, AAVE, GRANARY, TAROT, EXACTLY', + 'PURPOSE': 'LENDING, WITHDRAWS' + } + } + } +) }} + +SELECT + block_number, + block_timestamp, + tx_hash, + contract_address, + event_name, + event_index, + origin_function_signature, + origin_from_address, + origin_to_address, + platform, + depositor, + protocol_market, + token_address, + token_symbol, + amount_unadj, + amount, + amount_usd, + COALESCE ( + complete_lending_withdraws_id, + {{ dbt_utils.generate_surrogate_key( + ['tx_hash', 'event_index'] + ) }} + ) AS ez_lending_withdraws_id, + COALESCE( + inserted_timestamp, + '2000-01-01' + ) AS inserted_timestamp, + COALESCE( + modified_timestamp, + '2000-01-01' + ) AS modified_timestamp +FROM + {{ ref('silver__complete_lending_withdraws') }} \ No newline at end of file diff --git a/models/gold/defi/lending/defi__ez_lending_withdraws.yml b/models/gold/defi/lending/defi__ez_lending_withdraws.yml new file mode 100644 index 00000000..887c5e51 --- /dev/null +++ b/models/gold/defi/lending/defi__ez_lending_withdraws.yml @@ -0,0 +1,46 @@ +version: 2 +models: + - name: defi__ez_lending_withdraws + description: '{{ doc("complete_lending_withdraws_table_doc") }}' + + columns: + - name: BLOCK_NUMBER + description: '{{ doc("opt_block_number") }}' + - name: BLOCK_TIMESTAMP + description: '{{ doc("opt_block_timestamp") }}' + - name: TX_HASH + description: '{{ doc("opt_logs_tx_hash") }}' + - name: CONTRACT_ADDRESS + description: '{{ doc("opt_logs_contract_address") }}' + - name: EVENT_NAME + description: '{{ doc("opt_event_name") }}' + - name: EVENT_INDEX + description: '{{ doc("opt_event_index") }}' + - name: ORIGIN_FUNCTION_SIGNATURE + description: '{{ doc("nft_origin_sig") }}' + - name: ORIGIN_FROM_ADDRESS + description: '{{ doc("opt_origin_from") }}' + - name: ORIGIN_TO_ADDRESS + description: '{{ doc("opt_origin_to") }}' + - name: PLATFORM + description: '{{ doc("complete_lending_platform") }}' + - name: DEPOSITOR + description: '{{ doc("borrower") }}' + - name: PROTOCOL_MARKET + description: '{{ doc("complete_lending_protocol_token") }}' + - name: TOKEN_ADDRESS + description: '{{ doc("complete_lending_token_address") }}' + - name: TOKEN_SYMBOL + description: '{{ doc("complete_lending_token_symbol") }}' + - name: AMOUNT_UNADJ + description: '{{ doc("complete_lending_amount_unadj") }}' + - name: AMOUNT + description: '{{ doc("complete_lending_amount") }}' + - name: AMOUNT_USD + description: '{{ doc("complete_lending_amount_usd") }}' + - name: EZ_LENDING_WITHDRAWS_ID + description: '{{ doc("pk") }}' + - name: INSERTED_TIMESTAMP + description: '{{ doc("inserted_timestamp") }}' + - name: MODIFIED_TIMESTAMP + description: '{{ doc("modified_timestamp") }}' \ No newline at end of file diff --git a/models/silver/defi/lending/aave/silver__aave_borrows.sql b/models/silver/defi/lending/aave/silver__aave_borrows.sql new file mode 100644 index 00000000..b6d0ad0a --- /dev/null +++ b/models/silver/defi/lending/aave/silver__aave_borrows.sql @@ -0,0 +1,116 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = "block_number", + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} + +WITH --borrows from Aave LendingPool contracts +borrow AS ( + + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + regexp_substr_all(SUBSTR(DATA, 3, len(DATA)), '.{64}') AS segmented_data, + CONCAT('0x', SUBSTR(topics [1] :: STRING, 27, 40)) AS aave_market, + CONCAT('0x', SUBSTR(topics [2] :: STRING, 27, 40)) AS onBehalfOf, + utils.udf_hex_to_int( + topics [3] :: STRING + ) :: INTEGER AS refferal, + CONCAT('0x', SUBSTR(segmented_data [0] :: STRING, 25, 40)) AS userAddress, + utils.udf_hex_to_int( + segmented_data [1] :: STRING + ) :: INTEGER AS borrow_quantity, + utils.udf_hex_to_int( + segmented_data [2] :: STRING + ) :: INTEGER AS borrow_rate_mode, + utils.udf_hex_to_int( + segmented_data [3] :: STRING + ) :: INTEGER AS borrowrate, + CASE + WHEN contract_address = LOWER('0x794a61358D6845594F94dc1DB02A252b5b4814aD') THEN 'Aave V3' + ELSE 'ERROR' + END AS aave_version, + origin_from_address AS borrower_address, + COALESCE( + origin_to_address, + contract_address + ) AS lending_pool_contract, + _inserted_timestamp, + _log_id + FROM + {{ ref('silver__logs') }} + WHERE + topics [0] :: STRING = '0xb3d084820fb1a9decffb176436bd02558d15fac9b0ddfed8c465bc7359d7dce0' + +{% if is_incremental() %} +AND _inserted_timestamp >= ( + SELECT + MAX( + _inserted_timestamp + ) - INTERVAL '12 hours' + FROM + {{ this }} +) +{% endif %} +AND contract_address = LOWER('0x794a61358D6845594F94dc1DB02A252b5b4814aD') +AND tx_status = 'SUCCESS' --excludes failed txs +), +atoken_meta AS ( + SELECT + atoken_address, + atoken_symbol, + atoken_name, + atoken_decimals, + underlying_address, + underlying_symbol, + underlying_name, + underlying_decimals, + atoken_version, + atoken_created_block, + atoken_stable_debt_address, + atoken_variable_debt_address + FROM + {{ ref('silver__aave_tokens') }} +) +SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + aave_market, + atoken_meta.atoken_address AS aave_token, + borrow_quantity AS amount_unadj, + borrow_quantity / pow( + 10, + atoken_meta.underlying_decimals + ) AS amount, + borrower_address, + CASE + WHEN borrow_rate_mode = 2 THEN 'Variable Rate' + ELSE 'Stable Rate' + END AS borrow_rate_mode, + lending_pool_contract, + aave_version AS platform, + atoken_meta.underlying_symbol AS symbol, + atoken_meta.underlying_decimals AS underlying_decimals, + 'optimism' AS blockchain, + _log_id, + _inserted_timestamp +FROM + borrow + LEFT JOIN atoken_meta + ON borrow.aave_market = atoken_meta.underlying_address qualify(ROW_NUMBER() over(PARTITION BY _log_id +ORDER BY + _inserted_timestamp DESC)) = 1 diff --git a/models/silver/defi/lending/aave/silver__aave_borrows.yml b/models/silver/defi/lending/aave/silver__aave_borrows.yml new file mode 100644 index 00000000..06f62b16 --- /dev/null +++ b/models/silver/defi/lending/aave/silver__aave_borrows.yml @@ -0,0 +1,52 @@ +version: 2 +models: + - name: silver__aave_borrows + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - TX_HASH + - EVENT_INDEX + columns: + - name: TX_HASH + tests: + - not_null + - name: BLOCK_NUMBER + tests: + - not_null + - name: BLOCK_TIMESTAMP + tests: + - not_null + - name: EVENT_INDEX + tests: + - not_null + - name: AAVE_MARKET + tests: + - not_null + - name: AMOUNT_UNADJ + tests: + - not_null + - name: AMOUNT + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: BORROWER_ADDRESS + tests: + - not_null + - name: BORROW_RATE_MODE + tests: + - not_null + - name: LENDING_POOL_CONTRACT + tests: + - not_null + - name: PLATFORM + tests: + - not_null + - name: SYMBOL + tests: + - not_null + - name: BLOCKCHAIN + tests: + - not_null \ No newline at end of file diff --git a/models/silver/defi/lending/aave/silver__aave_deposits.sql b/models/silver/defi/lending/aave/silver__aave_deposits.sql new file mode 100644 index 00000000..3dc73539 --- /dev/null +++ b/models/silver/defi/lending/aave/silver__aave_deposits.sql @@ -0,0 +1,104 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = "block_number", + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} + +WITH deposits AS( + + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + regexp_substr_all(SUBSTR(DATA, 3, len(DATA)), '.{64}') AS segmented_data, + CONCAT('0x', SUBSTR(topics [1] :: STRING, 27, 40)) AS aave_market, + CONCAT('0x', SUBSTR(topics [2] :: STRING, 27, 40)) AS onBehalfOf, + utils.udf_hex_to_int( + topics [3] :: STRING + ) :: INTEGER AS refferal, + CONCAT('0x', SUBSTR(topics [2] :: STRING, 27, 42)) AS userAddress, + utils.udf_hex_to_int( + segmented_data [1] :: STRING + ) :: INTEGER AS deposit_quantity, + CASE + WHEN contract_address = LOWER('0x794a61358D6845594F94dc1DB02A252b5b4814aD') THEN 'Aave V3' + ELSE 'ERROR' + END AS aave_version, + origin_from_address AS depositor_address, + COALESCE( + origin_to_address, + contract_address + ) AS lending_pool_contract, + _inserted_timestamp, + _log_id + FROM + {{ ref('silver__logs') }} + WHERE + topics [0] :: STRING = '0x2b627736bca15cd5381dcf80b0bf11fd197d01a037c52b927a881a10fb73ba61' + +{% if is_incremental() %} +AND _inserted_timestamp >= ( + SELECT + MAX( + _inserted_timestamp + ) - INTERVAL '12 hours' + FROM + {{ this }} +) +{% endif %} +AND contract_address = LOWER('0x794a61358D6845594F94dc1DB02A252b5b4814aD') +AND tx_status = 'SUCCESS' --excludes failed txs +), +atoken_meta AS ( + SELECT + atoken_address, + atoken_symbol, + atoken_name, + atoken_decimals, + underlying_address, + underlying_symbol, + underlying_name, + underlying_decimals, + atoken_version, + atoken_created_block, + atoken_stable_debt_address, + atoken_variable_debt_address + FROM + {{ ref('silver__aave_tokens') }} +) +SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + aave_market, + atoken_meta.atoken_address AS aave_token, + deposit_quantity AS amount_unadj, + deposit_quantity / pow( + 10, + atoken_meta.underlying_decimals + ) AS amount, + depositor_address, + lending_pool_contract, + aave_version AS platform, + atoken_meta.underlying_symbol AS symbol, + 'optimism' AS blockchain, + _log_id, + _inserted_timestamp +FROM + deposits + LEFT JOIN atoken_meta + ON deposits.aave_market = atoken_meta.underlying_address qualify(ROW_NUMBER() over(PARTITION BY _log_id +ORDER BY + _inserted_timestamp DESC)) = 1 diff --git a/models/silver/defi/lending/aave/silver__aave_deposits.yml b/models/silver/defi/lending/aave/silver__aave_deposits.yml new file mode 100644 index 00000000..e35acbd6 --- /dev/null +++ b/models/silver/defi/lending/aave/silver__aave_deposits.yml @@ -0,0 +1,48 @@ +version: 2 +models: + - name: silver__aave_deposits + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - TX_HASH + - EVENT_INDEX + columns: + - name: BLOCKCHAIN + tests: + - not_null + - name: BLOCK_NUMBER + tests: + - not_null + - name: BLOCK_TIMESTAMP + tests: + - not_null + - name: TX_HASH + tests: + - not_null + - name: AMOUNT_UNADJ + tests: + - not_null + - name: AMOUNT + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: EVENT_INDEX + tests: + - not_null + - name: AAVE_MARKET + tests: + - not_null + - name: DEPOSITOR_ADDRESS + tests: + - not_null + - name: LENDING_POOL_CONTRACT + tests: + - not_null + - name: PLATFORM + tests: + - not_null + - name: SYMBOL + tests: + - not_null \ No newline at end of file diff --git a/models/silver/defi/lending/aave/silver__aave_flashloans.sql b/models/silver/defi/lending/aave/silver__aave_flashloans.sql new file mode 100644 index 00000000..a5a74458 --- /dev/null +++ b/models/silver/defi/lending/aave/silver__aave_flashloans.sql @@ -0,0 +1,111 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = "block_number", + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} + +WITH flashloan AS ( + + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + regexp_substr_all(SUBSTR(DATA, 3, len(DATA)), '.{64}') AS segmented_data, + CONCAT('0x', SUBSTR(topics [1] :: STRING, 27, 40)) AS target_address, + origin_to_address AS initiator_address, + CONCAT('0x', SUBSTR(topics [2] :: STRING, 27, 40)) AS aave_market, + utils.udf_hex_to_int( + segmented_data [1] :: STRING + ) :: INTEGER AS flashloan_quantity, + utils.udf_hex_to_int( + segmented_data [3] :: STRING + ) :: INTEGER AS premium_quantity, + utils.udf_hex_to_int( + topics [3] :: STRING + ) :: INTEGER AS refferalCode, + COALESCE( + origin_to_address, + contract_address + ) AS lending_pool_contract, + CASE + WHEN contract_address = LOWER('0x794a61358D6845594F94dc1DB02A252b5b4814aD') THEN 'Aave V3' + ELSE 'ERROR' + END AS aave_version, + _inserted_timestamp, + _log_id + FROM + {{ ref('silver__logs') }} + WHERE + topics [0] :: STRING = '0xefefaba5e921573100900a3ad9cf29f222d995fb3b6045797eaea7521bd8d6f0' + +{% if is_incremental() %} +AND _inserted_timestamp >= ( + SELECT + MAX( + _inserted_timestamp + ) - INTERVAL '12 hours' + FROM + {{ this }} +) +{% endif %} +AND contract_address = LOWER('0x794a61358D6845594F94dc1DB02A252b5b4814aD') +AND tx_status = 'SUCCESS' --excludes failed txs +), +atoken_meta AS ( + SELECT + atoken_address, + atoken_symbol, + atoken_name, + atoken_decimals, + underlying_address, + underlying_symbol, + underlying_name, + underlying_decimals, + atoken_version, + atoken_created_block, + atoken_stable_debt_address, + atoken_variable_debt_address + FROM + {{ ref('silver__aave_tokens') }} +) +SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + aave_market, + atoken_meta.atoken_address AS aave_token, + flashloan_quantity AS flashloan_amount_unadj, + flashloan_quantity / pow( + 10, + atoken_meta.underlying_decimals + ) AS flashloan_amount, + premium_quantity AS premium_amount_unadj, + premium_quantity / pow( + 10, + atoken_meta.underlying_decimals + ) AS premium_amount, + initiator_address AS initiator_address, + target_address AS target_address, + aave_version AS platform, + atoken_meta.underlying_symbol AS symbol, + 'optimism' AS blockchain, + _log_id, + _inserted_timestamp +FROM + flashloan + LEFT JOIN atoken_meta + ON flashloan.aave_market = atoken_meta.underlying_address qualify(ROW_NUMBER() over(PARTITION BY _log_id +ORDER BY + _inserted_timestamp DESC)) = 1 diff --git a/models/silver/defi/lending/aave/silver__aave_flashloans.yml b/models/silver/defi/lending/aave/silver__aave_flashloans.yml new file mode 100644 index 00000000..e0345424 --- /dev/null +++ b/models/silver/defi/lending/aave/silver__aave_flashloans.yml @@ -0,0 +1,49 @@ +version: 2 +models: + - name: silver__aave_flashloans + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - TX_HASH + - EVENT_INDEX + columns: + - name: BLOCKCHAIN + tests: + - not_null + - name: BLOCK_NUMBER + tests: + - not_null + - name: BLOCK_TIMESTAMP + tests: + - not_null + - name: EVENT_INDEX + tests: + - not_null + - name: TX_HASH + tests: + - not_null + - name: AAVE_MARKET + tests: + - not_null + - name: AAVE_TOKEN + - name: FLASHLOAN_AMOUNT_UNADJ + tests: + - not_null + - name: FLASHLOAN_AMOUNT + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: INITIATOR_ADDRESS + tests: + - not_null + - name: TARGET_ADDRESS + tests: + - not_null + - name: PLATFORM + tests: + - not_null + - name: SYMBOL + tests: + - not_null diff --git a/models/silver/defi/lending/aave/silver__aave_liquidations.sql b/models/silver/defi/lending/aave/silver__aave_liquidations.sql new file mode 100644 index 00000000..e68be4dc --- /dev/null +++ b/models/silver/defi/lending/aave/silver__aave_liquidations.sql @@ -0,0 +1,109 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = "block_number", + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} + +WITH liquidation AS( + + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + regexp_substr_all(SUBSTR(DATA, 3, len(DATA)), '.{64}') AS segmented_data, + CONCAT('0x', SUBSTR(topics [1] :: STRING, 27, 40)) AS collateral_asset, + CONCAT('0x', SUBSTR(topics [2] :: STRING, 27, 40)) AS debt_asset, + CONCAT('0x', SUBSTR(topics [3] :: STRING, 27, 40)) AS borrower_address, + utils.udf_hex_to_int( + segmented_data [0] :: STRING + ) :: INTEGER AS debt_to_cover_amount, + utils.udf_hex_to_int( + segmented_data [1] :: STRING + ) :: INTEGER AS liquidated_amount, + CONCAT('0x', SUBSTR(segmented_data [2] :: STRING, 25, 40)) AS liquidator_address, + CASE + WHEN contract_address = LOWER('0x794a61358D6845594F94dc1DB02A252b5b4814aD') THEN 'Aave V3' + ELSE 'ERROR' + END AS aave_version, + COALESCE( + origin_to_address, + contract_address + ) AS lending_pool_contract, + _inserted_timestamp, + _log_id + FROM + {{ ref('silver__logs') }} + WHERE + topics [0] :: STRING = '0xe413a321e8681d831f4dbccbca790d2952b56f977908e45be37335533e005286' + +{% if is_incremental() %} +AND _inserted_timestamp >= ( + SELECT + MAX( + _inserted_timestamp + ) - INTERVAL '12 hours' + FROM + {{ this }} +) +{% endif %} +AND contract_address = LOWER('0x794a61358D6845594F94dc1DB02A252b5b4814aD') +AND tx_status = 'SUCCESS' --excludes failed txs +), +atoken_meta AS ( + SELECT + atoken_address, + atoken_symbol, + atoken_name, + atoken_decimals, + underlying_address, + underlying_symbol, + underlying_name, + underlying_decimals, + atoken_version, + atoken_created_block, + atoken_stable_debt_address, + atoken_variable_debt_address + FROM + {{ ref('silver__aave_tokens') }} +) +SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + collateral_asset, + amc.atoken_address AS collateral_aave_token, + liquidated_amount AS amount_unadj, + liquidated_amount / pow( + 10, + amc.atoken_decimals + ) AS amount, + debt_asset, + amd.atoken_address AS debt_aave_token, + liquidator_address AS liquidator, + borrower_address AS borrower, + aave_version AS platform, + amc.underlying_symbol AS collateral_token_symbol, + amd.underlying_symbol AS debt_token_symbol, + 'optimism' AS blockchain, + _log_id, + _inserted_timestamp +FROM + liquidation + LEFT JOIN atoken_meta amc + ON liquidation.collateral_asset = amc.underlying_address + LEFT JOIN atoken_meta amd + ON liquidation.debt_asset = amd.underlying_address qualify(ROW_NUMBER() over(PARTITION BY _log_id +ORDER BY + _inserted_timestamp DESC)) = 1 diff --git a/models/silver/defi/lending/aave/silver__aave_liquidations.yml b/models/silver/defi/lending/aave/silver__aave_liquidations.yml new file mode 100644 index 00000000..9a3c8028 --- /dev/null +++ b/models/silver/defi/lending/aave/silver__aave_liquidations.yml @@ -0,0 +1,53 @@ +version: 2 +models: + - name: silver__aave_liquidations + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - TX_HASH + - EVENT_INDEX + columns: + - name: BLOCKCHAIN + tests: + - not_null + - name: BLOCK_NUMBER + tests: + - not_null + - name: BLOCK_TIMESTAMP + tests: + - not_null + - name: EVENT_INDEX + tests: + - not_null + - name: TX_HASH + tests: + - not_null + - name: COLLATERAL_ASSET + tests: + - not_null + - name: COLLATERAL_AAVE_TOKEN + - name: AMOUNT_UNADJ + tests: + - not_null + - name: AMOUNT + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: DEBT_AAVE_TOKEN + - name: LIQUIDATOR + tests: + - not_null + - name: BORROWER + tests: + - not_null + - name: PLATFORM + tests: + - not_null + - name: COLLATERAL_TOKEN_SYMBOL + tests: + - not_null + - name: DEBT_TOKEN_SYMBOL + tests: + - not_null diff --git a/models/silver/defi/lending/aave/silver__aave_repayments.sql b/models/silver/defi/lending/aave/silver__aave_repayments.sql new file mode 100644 index 00000000..977be269 --- /dev/null +++ b/models/silver/defi/lending/aave/silver__aave_repayments.sql @@ -0,0 +1,99 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = "block_number", + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} + +WITH repay AS( + + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + regexp_substr_all(SUBSTR(DATA, 3, len(DATA)), '.{64}') AS segmented_data, + CONCAT('0x', SUBSTR(topics [1] :: STRING, 27, 40)) AS aave_market, + CONCAT('0x', SUBSTR(topics [2] :: STRING, 27, 40)) AS borrower_address, + CONCAT('0x', SUBSTR(topics [3] :: STRING, 27, 40)) AS repayer, + utils.udf_hex_to_int( + segmented_data [0] :: STRING + ) :: INTEGER AS repayed_amount, + CASE + WHEN contract_address = LOWER('0x794a61358D6845594F94dc1DB02A252b5b4814aD') THEN 'Aave V3' + ELSE 'ERROR' + END AS aave_version, + origin_to_address AS lending_pool_contract, + origin_from_address AS repayer_address, + _log_id, + _inserted_timestamp + FROM + {{ ref('silver__logs') }} + WHERE + topics [0] :: STRING = '0xa534c8dbe71f871f9f3530e97a74601fea17b426cae02e1c5aee42c96c784051' + +{% if is_incremental() %} +AND _inserted_timestamp >= ( + SELECT + MAX( + _inserted_timestamp + ) - INTERVAL '12 hours' + FROM + {{ this }} +) +{% endif %} +AND contract_address = LOWER('0x794a61358D6845594F94dc1DB02A252b5b4814aD') +AND tx_status = 'SUCCESS' --excludes failed txs +), +atoken_meta AS ( + SELECT + atoken_address, + atoken_symbol, + atoken_name, + atoken_decimals, + underlying_address, + underlying_symbol, + underlying_name, + underlying_decimals, + atoken_version, + atoken_created_block, + atoken_stable_debt_address, + atoken_variable_debt_address + FROM + {{ ref('silver__aave_tokens') }} +) +SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + aave_market, + atoken_meta.atoken_address AS aave_token, + repayed_amount AS amount_unadj, + repayed_amount / pow( + 10, + atoken_meta.underlying_decimals + ) AS amount, + repayer_address AS payer, + borrower_address AS borrower, + lending_pool_contract, + aave_version AS platform, + atoken_meta.underlying_symbol AS symbol, + 'optimism' AS blockchain, + _log_id, + _inserted_timestamp +FROM + repay + LEFT JOIN atoken_meta + ON repay.aave_market = atoken_meta.underlying_address qualify(ROW_NUMBER() over(PARTITION BY _log_id +ORDER BY + _inserted_timestamp DESC)) = 1 diff --git a/models/silver/defi/lending/aave/silver__aave_repayments.yml b/models/silver/defi/lending/aave/silver__aave_repayments.yml new file mode 100644 index 00000000..bdb85561 --- /dev/null +++ b/models/silver/defi/lending/aave/silver__aave_repayments.yml @@ -0,0 +1,52 @@ +version: 2 +models: + - name: silver__aave_repayments + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - TX_HASH + - EVENT_INDEX + columns: + - name: BLOCKCHAIN + tests: + - not_null + - name: BLOCK_NUMBER + tests: + - not_null + - name: TX_HASH + tests: + - not_null + - name: BLOCK_TIMESTAMP + tests: + - not_null + - name: EVENT_INDEX + tests: + - not_null + - name: AAVE_MARKET + tests: + - not_null + - name: AAVE_TOKEN + - name: AMOUNT_UNADJ + tests: + - not_null + - name: AMOUNT + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: PAYER + tests: + - not_null + - name: BORROWER + tests: + - not_null + - name: LENDING_POOL_CONTRACT + tests: + - not_null + - name: PLATFORM + tests: + - not_null + - name: SYMBOL + tests: + - not_null diff --git a/models/silver/defi/lending/aave/silver__aave_tokens.sql b/models/silver/defi/lending/aave/silver__aave_tokens.sql new file mode 100644 index 00000000..c667e132 --- /dev/null +++ b/models/silver/defi/lending/aave/silver__aave_tokens.sql @@ -0,0 +1,123 @@ +{{ config( + materialized = 'incremental', + tags = ['curated'] +) }} + +WITH DECODE AS ( + + SELECT + block_number AS atoken_created_block, + contract_address AS a_token_address, + regexp_substr_all(SUBSTR(DATA, 3, len(DATA)), '.{64}') AS segmented_data, + CONCAT('0x', SUBSTR(topics [1] :: STRING, 27, 40)) AS underlying_asset, + CONCAT('0x', SUBSTR(topics [2] :: STRING, 27, 40)) AS aave_version_pool, + utils.udf_hex_to_int( + SUBSTR( + segmented_data [2] :: STRING, + 27, + 40 + ) + ) :: INTEGER AS atoken_decimals, + utils.udf_hex_to_string ( + segmented_data [7] :: STRING + ) :: STRING AS atoken_name, + utils.udf_hex_to_string ( + segmented_data [9] :: STRING + ) :: STRING AS atoken_symbol, + l._inserted_timestamp, + l._log_id + FROM + {{ ref('silver__logs') }} + l + WHERE + topics [0] = '0xb19e051f8af41150ccccb3fc2c2d8d15f4a4cf434f32a559ba75fe73d6eea20b' + +{% if is_incremental() %} +AND _inserted_timestamp >= ( + SELECT + MAX( + _inserted_timestamp + ) - INTERVAL '12 hours' + FROM + {{ this }} +) +{% endif %} +), +a_token_step_1 AS ( + SELECT + atoken_created_block, + a_token_address, + segmented_data, + underlying_asset, + aave_version_pool, + atoken_decimals, + atoken_name, + atoken_symbol, + _inserted_timestamp, + _log_id + FROM + DECODE + WHERE + atoken_name LIKE '%Aave%' +), +debt_tokens AS ( + SELECT + block_number AS atoken_created_block, + contract_address AS a_token_address, + regexp_substr_all(SUBSTR(DATA, 3, len(DATA)), '.{64}') AS segmented_data, + CONCAT('0x', SUBSTR(topics [1] :: STRING, 27, 40)) AS underlying_asset, + CONCAT('0x', SUBSTR(topics [2] :: STRING, 27, 40)) AS atoken_address, + CONCAT('0x', SUBSTR(segmented_data [0] :: STRING, 27, 40)) :: STRING AS atoken_stable_debt_address, + CONCAT('0x', SUBSTR(segmented_data [1] :: STRING, 27, 40)) :: STRING AS atoken_variable_debt_address, + _inserted_timestamp, + _log_id + FROM + {{ ref('silver__logs') }} + WHERE + topics [0] = '0x3a0ca721fc364424566385a1aa271ed508cc2c0949c2272575fb3013a163a45f' + AND CONCAT('0x', SUBSTR(topics [2] :: STRING, 27, 40)) IN ( + SELECT + a_token_address + FROM + a_token_step_1 + ) +), +a_token_step_2 AS ( + SELECT + atoken_created_block, + a_token_address, + segmented_data, + underlying_asset, + aave_version_pool, + atoken_decimals, + atoken_name, + atoken_symbol, + _inserted_timestamp, + _log_id, + 'Aave V3' AS protocol + FROM + a_token_step_1 + WHERE + aave_version_pool = LOWER('0x794a61358D6845594F94dc1DB02A252b5b4814aD') +) +SELECT + A.atoken_created_block, + A.atoken_symbol AS atoken_symbol, + A.a_token_address AS atoken_address, + b.atoken_stable_debt_address, + b.atoken_variable_debt_address, + A.atoken_decimals AS atoken_decimals, + A.protocol AS atoken_version, + atoken_name AS atoken_name, + C.token_symbol AS underlying_symbol, + A.underlying_asset AS underlying_address, + C.token_decimals AS underlying_decimals, + C.token_name AS underlying_name, + A._inserted_timestamp, + A._log_id +FROM + a_token_step_2 A + INNER JOIN debt_tokens b + ON A.a_token_address = b.atoken_address + INNER JOIN {{ ref('silver__contracts') }} C + ON contract_address = A.underlying_asset diff --git a/models/silver/defi/lending/aave/silver__aave_withdraws.sql b/models/silver/defi/lending/aave/silver__aave_withdraws.sql new file mode 100644 index 00000000..e87077ba --- /dev/null +++ b/models/silver/defi/lending/aave/silver__aave_withdraws.sql @@ -0,0 +1,96 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = "block_number", + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} + +WITH withdraw AS( + + SELECT + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + regexp_substr_all(SUBSTR(DATA, 3, len(DATA)), '.{64}') AS segmented_data, + CONCAT('0x', SUBSTR(topics [1] :: STRING, 27, 40)) AS aave_market, + CONCAT('0x', SUBSTR(topics [2] :: STRING, 27, 40)) AS useraddress, + CONCAT('0x', SUBSTR(topics [3] :: STRING, 27, 40)) AS depositor, + utils.udf_hex_to_int( + segmented_data [0] :: STRING + ) :: INTEGER AS withdraw_amount, + tx_hash, + CASE + WHEN contract_address = LOWER('0x794a61358D6845594F94dc1DB02A252b5b4814aD') THEN 'Aave V3' + ELSE 'ERROR' + END AS aave_version, + origin_to_address AS lending_pool_contract, + _inserted_timestamp, + _log_id + FROM + {{ ref('silver__logs') }} + WHERE + topics [0] :: STRING = '0x3115d1449a7b732c986cba18244e897a450f61e1bb8d589cd2e69e6c8924f9f7' + +{% if is_incremental() %} +AND _inserted_timestamp >= ( + SELECT + MAX( + _inserted_timestamp + ) - INTERVAL '12 hours' + FROM + {{ this }} +) +{% endif %} +AND contract_address = LOWER('0x794a61358D6845594F94dc1DB02A252b5b4814aD') +AND tx_status = 'SUCCESS' --excludes failed txs +), +atoken_meta AS ( + SELECT + atoken_address, + atoken_symbol, + atoken_name, + atoken_decimals, + underlying_address, + underlying_symbol, + underlying_name, + underlying_decimals, + atoken_version, + atoken_created_block, + atoken_stable_debt_address, + atoken_variable_debt_address + FROM + {{ ref('silver__aave_tokens') }} +) +SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + aave_market, + atoken_meta.atoken_address AS aave_token, + withdraw_amount AS amount_unadj, + withdraw_amount / pow( + 10, + atoken_meta.underlying_decimals + ) AS amount, + depositor depositor_address, + aave_version AS platform, + atoken_meta.underlying_symbol AS symbol, + 'optimism' AS blockchain, + _log_id, + _inserted_timestamp +FROM + withdraw + LEFT JOIN atoken_meta + ON withdraw.aave_market = atoken_meta.underlying_address qualify(ROW_NUMBER() over(PARTITION BY _log_id +ORDER BY + _inserted_timestamp DESC)) = 1 diff --git a/models/silver/defi/lending/aave/silver__aave_withdraws.yml b/models/silver/defi/lending/aave/silver__aave_withdraws.yml new file mode 100644 index 00000000..c806a1fc --- /dev/null +++ b/models/silver/defi/lending/aave/silver__aave_withdraws.yml @@ -0,0 +1,46 @@ +version: 2 +models: + - name: silver__aave_withdraws + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - TX_HASH + - EVENT_INDEX + columns: + - name: BLOCKCHAIN + tests: + - not_null + - name: TX_HASH + tests: + - not_null + - name: BLOCK_NUMBER + tests: + - not_null + - name: BLOCK_TIMESTAMP + tests: + - not_null + - name: EVENT_INDEX + tests: + - not_null + - name: AAVE_MARKET + tests: + - not_null + - name: AAVE_TOKEN + - name: AMOUNT_UNADJ + tests: + - not_null + - name: AMOUNT + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: DEPOSITOR_ADDRESS + tests: + - not_null + - name: PLATFORM + tests: + - not_null + - name: SYMBOL + tests: + - not_null diff --git a/models/silver/defi/lending/complete_lending/silver__complete_lending_borrows.sql b/models/silver/defi/lending/complete_lending/silver__complete_lending_borrows.sql new file mode 100644 index 00000000..ee50a2a5 --- /dev/null +++ b/models/silver/defi/lending/complete_lending/silver__complete_lending_borrows.sql @@ -0,0 +1,259 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = ['block_number','platform'], + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} + +WITH aave AS ( + + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + borrower_address AS borrower, + aave_token AS protocol_market, + aave_market AS token_address, + symbol AS token_symbol, + amount_unadj, + amount, + platform, + 'optimism' AS blockchain, + A._LOG_ID, + A._INSERTED_TIMESTAMP + FROM + {{ ref('silver__aave_borrows') }} A + +{% if is_incremental() and 'aave' not in var('HEAL_CURATED_MODEL') %} +WHERE + A._inserted_timestamp >= ( + SELECT + MAX( + _inserted_timestamp + ) - INTERVAL '36 hours' + FROM + {{ this }} + ) +{% endif %} +), +granary AS ( + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + borrower_address AS borrower, + granary_token AS protocol_market, + granary_market AS token_address, + symbol AS token_symbol, + amount_unadj, + amount, + platform, + 'optimism' AS blockchain, + A._LOG_ID, + A._INSERTED_TIMESTAMP + FROM + {{ ref('silver__granary_borrows') }} A + +{% if is_incremental() and 'granary' not in var('HEAL_CURATED_MODEL') %} +WHERE + A._inserted_timestamp >= ( + SELECT + MAX( + _inserted_timestamp + ) - INTERVAL '36 hours' + FROM + {{ this }} + ) +{% endif %} +), +exactly AS ( + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + borrower, + token_address AS protocol_market, + borrows_contract_address AS token_address, + borrows_contract_symbol AS token_symbol, + amount_unadj, + amount, + platform, + 'optimism' AS blockchain, + A._LOG_ID, + A._INSERTED_TIMESTAMP + FROM + {{ ref('silver__exactly_borrows') }} A + +{% if is_incremental() and 'exactly' not in var('HEAL_CURATED_MODEL') %} +WHERE + A._inserted_timestamp >= ( + SELECT + MAX( + _inserted_timestamp + ) - INTERVAL '36 hours' + FROM + {{ this }} + ) +{% endif %} +), +sonne AS ( + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + borrower, + token_address AS protocol_market, + borrows_contract_address AS token_address, + borrows_contract_symbol AS token_symbol, + amount_unadj, + amount, + platform, + 'optimism' AS blockchain, + A._LOG_ID, + A._INSERTED_TIMESTAMP + FROM + {{ ref('silver__sonne_borrows') }} A + +{% if is_incremental() and 'sonne' not in var('HEAL_CURATED_MODEL') %} +WHERE + A._inserted_timestamp >= ( + SELECT + MAX( + _inserted_timestamp + ) - INTERVAL '36 hours' + FROM + {{ this }} + ) +{% endif %} +), +tarot AS ( + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + borrower, + token_address AS protocol_market, + borrows_contract_address AS token_address, + borrows_contract_symbol AS token_symbol, + amount_unadj, + amount, + platform, + 'optimism' AS blockchain, + A._LOG_ID, + A._INSERTED_TIMESTAMP + FROM + {{ ref('silver__tarot_borrows') }} A + +{% if is_incremental() and 'tarot' not in var('HEAL_CURATED_MODEL') %} +WHERE + A._inserted_timestamp >= ( + SELECT + MAX( + _inserted_timestamp + ) - INTERVAL '36 hours' + FROM + {{ this }} + ) +{% endif %} +), +borrow_union AS ( + SELECT + * + FROM + aave + UNION ALL + SELECT + * + FROM + granary + UNION ALL + SELECT + * + FROM + exactly + UNION ALL + SELECT + * + FROM + sonne + UNION ALL + SELECT + * + FROM + tarot +), +FINAL AS ( + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + b.contract_address, + 'Borrow' AS event_name, + borrower, + protocol_market, + b.token_address, + b.token_symbol, + amount_unadj, + amount, + ROUND( + amount * price, + 2 + ) AS amount_usd, + platform, + blockchain, + b._LOG_ID, + b._INSERTED_TIMESTAMP + FROM + borrow_union b + LEFT JOIN {{ ref('price__ez_hourly_token_prices') }} + p + ON b.token_address = p.token_address + AND DATE_TRUNC( + 'hour', + block_timestamp + ) = p.hour + LEFT JOIN {{ ref('silver__contracts') }} C + ON b.token_address = C.contract_address +) +SELECT + *, + {{ dbt_utils.generate_surrogate_key( + ['tx_hash','event_index'] + ) }} AS complete_lending_borrows_id, + SYSDATE() AS inserted_timestamp, + SYSDATE() AS modified_timestamp, + '{{ invocation_id }}' AS _invocation_id +FROM + FINAL qualify(ROW_NUMBER() over(PARTITION BY _log_id +ORDER BY + _inserted_timestamp DESC)) = 1 diff --git a/models/silver/defi/lending/complete_lending/silver__complete_lending_borrows.yml b/models/silver/defi/lending/complete_lending/silver__complete_lending_borrows.yml new file mode 100644 index 00000000..175a0028 --- /dev/null +++ b/models/silver/defi/lending/complete_lending/silver__complete_lending_borrows.yml @@ -0,0 +1,55 @@ +version: 2 +models: + - name: silver__complete_lending_borrows + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - TX_HASH + - EVENT_INDEX + columns: + - name: TX_HASH + tests: + - not_null + - name: BLOCK_NUMBER + tests: + - not_null + - name: BLOCK_TIMESTAMP + tests: + - not_null + - dbt_expectations.expect_row_values_to_have_recent_data: + datepart: day + interval: 30 + - name: EVENT_INDEX + tests: + - not_null + - name: BORROWER + tests: + - not_null + - name: PROTOCOL_MARKET + tests: + - not_null + - name: TOKEN_ADDRESS + tests: + - not_null + - name: TOKEN_SYMBOL + tests: + - not_null + - name: AMOUNT_UNADJ + tests: + - not_null + - name: AMOUNT + tests: + - not_null + - name: AMOUNT_USD + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: PLATFORM + tests: + - not_null + - name: BLOCKCHAIN + tests: + - not_null + \ No newline at end of file diff --git a/models/silver/defi/lending/complete_lending/silver__complete_lending_deposits.sql b/models/silver/defi/lending/complete_lending/silver__complete_lending_deposits.sql new file mode 100644 index 00000000..2a3a6fb8 --- /dev/null +++ b/models/silver/defi/lending/complete_lending/silver__complete_lending_deposits.sql @@ -0,0 +1,256 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = ['block_number','platform'], + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} + +WITH aave AS ( + + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + depositor_address, + aave_token AS protocol_market, + aave_market AS token_address, + symbol AS token_symbol, + amount_unadj, + amount, + platform, + 'optimism' AS blockchain, + _LOG_ID, + _INSERTED_TIMESTAMP + FROM + {{ ref('silver__aave_deposits') }} + +{% if is_incremental() and 'aave' not in var('HEAL_CURATED_MODEL') %} +WHERE + _inserted_timestamp >= ( + SELECT + MAX(_inserted_timestamp) - INTERVAL '36 hours' + FROM + {{ this }} + ) +{% endif %} +), +granary AS ( + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + depositor_address, + granary_token AS protocol_market, + granary_market AS token_address, + symbol AS token_symbol, + amount_unadj, + amount, + platform, + 'optimism' AS blockchain, + _LOG_ID, + _INSERTED_TIMESTAMP + FROM + {{ ref('silver__granary_deposits') }} + +{% if is_incremental() and 'granary' not in var('HEAL_CURATED_MODEL') %} +WHERE + _inserted_timestamp >= ( + SELECT + MAX(_inserted_timestamp) - INTERVAL '36 hours' + FROM + {{ this }} + ) +{% endif %} +), +exactly AS ( + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + supplier AS depositor_address, + token_address AS protocol_market, + supplied_contract_addr AS token_address, + supplied_symbol AS token_symbol, + amount_unadj, + amount, + platform, + 'optimism' AS blockchain, + _LOG_ID, + _INSERTED_TIMESTAMP + FROM + {{ ref('silver__exactly_deposits') }} + +{% if is_incremental() and 'exactly' not in var('HEAL_CURATED_MODEL') %} +WHERE + _inserted_timestamp >= ( + SELECT + MAX(_inserted_timestamp) - INTERVAL '36 hours' + FROM + {{ this }} + ) +{% endif %} +), +sonne AS ( + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + supplier AS depositor_address, + token_address AS protocol_market, + supplied_contract_addr AS token_address, + supplied_symbol AS token_symbol, + amount_unadj, + amount, + platform, + 'optimism' AS blockchain, + _LOG_ID, + _INSERTED_TIMESTAMP + FROM + {{ ref('silver__sonne_deposits') }} + +{% if is_incremental() and 'sonne' not in var('HEAL_CURATED_MODEL') %} +WHERE + _inserted_timestamp >= ( + SELECT + MAX(_inserted_timestamp) - INTERVAL '36 hours' + FROM + {{ this }} + ) +{% endif %} +), +tarot AS ( + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + supplier AS depositor_address, + token_address AS protocol_market, + supplied_contract_addr AS token_address, + supplied_symbol AS token_symbol, + amount_unadj, + amount, + platform, + 'optimism' AS blockchain, + _LOG_ID, + _INSERTED_TIMESTAMP + FROM + {{ ref('silver__tarot_deposits') }} + +{% if is_incremental() and 'tarot' not in var('HEAL_CURATED_MODEL') %} +WHERE + _inserted_timestamp >= ( + SELECT + MAX(_inserted_timestamp) - INTERVAL '36 hours' + FROM + {{ this }} + ) +{% endif %} +), +deposit_union AS ( + SELECT + * + FROM + aave + UNION ALL + SELECT + * + FROM + granary + UNION ALL + SELECT + * + FROM + exactly + UNION ALL + SELECT + * + FROM + sonne + UNION ALL + SELECT + * + FROM + tarot +), +FINAL AS ( + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + A.contract_address, + CASE + WHEN platform IN ( + 'Tarot', + 'Sonne' + ) THEN 'Mint' + WHEN platform = 'Aave V3' THEN 'Supply' + ELSE 'Deposit' + END AS event_name, + protocol_market, + depositor_address AS depositor, + A.token_address, + A.token_symbol, + amount_unadj, + amount, + ROUND( + amount * price, + 2 + ) AS amount_usd, + platform, + blockchain, + A._LOG_ID, + A._INSERTED_TIMESTAMP + FROM + deposit_union A + LEFT JOIN {{ ref('price__ez_hourly_token_prices') }} + p + ON A.token_address = p.token_address + AND DATE_TRUNC( + 'hour', + block_timestamp + ) = p.hour + LEFT JOIN {{ ref('silver__contracts') }} C + ON A.token_address = C.contract_address +) +SELECT + *, + {{ dbt_utils.generate_surrogate_key( + ['tx_hash','event_index'] + ) }} AS complete_lending_deposits_id, + SYSDATE() AS inserted_timestamp, + SYSDATE() AS modified_timestamp, + '{{ invocation_id }}' AS _invocation_id +FROM + FINAL qualify(ROW_NUMBER() over(PARTITION BY _log_id +ORDER BY + _inserted_timestamp DESC)) = 1 diff --git a/models/silver/defi/lending/complete_lending/silver__complete_lending_deposits.yml b/models/silver/defi/lending/complete_lending/silver__complete_lending_deposits.yml new file mode 100644 index 00000000..235bbfff --- /dev/null +++ b/models/silver/defi/lending/complete_lending/silver__complete_lending_deposits.yml @@ -0,0 +1,54 @@ +version: 2 +models: + - name: silver__complete_lending_deposits + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - TX_HASH + - EVENT_INDEX + columns: + - name: TX_HASH + tests: + - not_null + - name: BLOCK_NUMBER + tests: + - not_null + - name: BLOCK_TIMESTAMP + tests: + - not_null + - dbt_expectations.expect_row_values_to_have_recent_data: + datepart: day + interval: 30 + - name: EVENT_INDEX + tests: + - not_null + - name: PROTOCOL_MARKET + tests: + - name: DEPOSITOR + tests: + - not_null + - name: TOKEN_ADDRESS + tests: + - not_null + - name: TOKEN_SYMBOL + tests: + - not_null + - name: AMOUNT_UNADJ + tests: + - not_null + - name: AMOUNT + tests: + - not_null + - name: AMOUNT_USD + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: PLATFORM + tests: + - not_null + - name: BLOCKCHAIN + tests: + - not_null + \ No newline at end of file diff --git a/models/silver/defi/lending/complete_lending/silver__complete_lending_flashloans.sql b/models/silver/defi/lending/complete_lending/silver__complete_lending_flashloans.sql new file mode 100644 index 00000000..9f85428e --- /dev/null +++ b/models/silver/defi/lending/complete_lending/silver__complete_lending_flashloans.sql @@ -0,0 +1,151 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = ['block_number','platform'], + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} + +WITH aave as ( + + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + aave_market AS token_address, + aave_token AS protocol_token, + flashloan_amount_unadj, + flashloan_amount, + premium_amount_unadj, + premium_amount, + initiator_address, + target_address, + 'Aave V3' AS platform, + symbol, + blockchain, + _LOG_ID, + _INSERTED_TIMESTAMP + FROM + {{ ref('silver__aave_flashloans') }} + +{% if is_incremental() and 'aave' not in var('HEAL_CURATED_MODEL') %} +WHERE + _inserted_timestamp >= ( + SELECT + MAX(_inserted_timestamp) - INTERVAL '36 hours' + FROM + {{ this }} + ) +{% endif %} +), + +granary as ( + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + granary_market AS token_address, + granary_token AS protocol_token, + flashloan_amount_unadj, + flashloan_amount, + premium_amount_unadj, + premium_amount, + initiator_address, + target_address, + platform, + symbol AS token_symbol, + blockchain, + _LOG_ID, + _INSERTED_TIMESTAMP + FROM + {{ ref('silver__granary_flashloans') }} + +{% if is_incremental() and 'granary' not in var('HEAL_CURATED_MODEL') %} +WHERE + _inserted_timestamp >= ( + SELECT + MAX(_inserted_timestamp) - INTERVAL '36 hours' + FROM + {{ this }} + ) +{% endif %} +), + +flashloan_union as ( + SELECT + * + FROM + aave + UNION ALL + SELECT + * + FROM + granary +), + +FINAL AS ( + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + f.contract_address, + 'FlashLoan' AS event_name, + protocol_token AS protocol_market, + initiator_address AS initiator, + target_address AS target, + f.token_address AS flashloan_token, + token_symbol AS flashloan_token_symbol, + flashloan_amount_unadj, + flashloan_amount, + ROUND( + flashloan_amount * price, + 2 + ) AS flashloan_amount_usd, + premium_amount_unadj, + premium_amount, + ROUND( + premium_amount * price, + 2 + ) AS premium_amount_usd, + platform, + blockchain, + f._LOG_ID, + f._INSERTED_TIMESTAMP + FROM + flashloan_union f + LEFT JOIN {{ ref('price__ez_hourly_token_prices') }} + p + ON f.token_address = p.token_address + AND DATE_TRUNC( + 'hour', + block_timestamp + ) = p.hour + LEFT JOIN {{ ref('silver__contracts') }} C + ON f.token_address = C.contract_address +) +SELECT + *, + {{ dbt_utils.generate_surrogate_key( + ['tx_hash','event_index'] + ) }} AS complete_lending_flashloans_id, + SYSDATE() AS inserted_timestamp, + SYSDATE() AS modified_timestamp, + '{{ invocation_id }}' AS _invocation_id +FROM + FINAL qualify(ROW_NUMBER() over(PARTITION BY _log_id +ORDER BY + _inserted_timestamp DESC)) = 1 diff --git a/models/silver/defi/lending/complete_lending/silver__complete_lending_flashloans.yml b/models/silver/defi/lending/complete_lending/silver__complete_lending_flashloans.yml new file mode 100644 index 00000000..8da9abd7 --- /dev/null +++ b/models/silver/defi/lending/complete_lending/silver__complete_lending_flashloans.yml @@ -0,0 +1,54 @@ +version: 2 +models: + - name: silver__complete_lending_flashloans + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - TX_HASH + - EVENT_INDEX + columns: + - name: BLOCK_NUMBER + tests: + - not_null + - name: BLOCK_TIMESTAMP + tests: + - not_null + - dbt_expectations.expect_row_values_to_have_recent_data: + datepart: day + interval: 60 + - name: EVENT_INDEX + tests: + - not_null + - name: TX_HASH + tests: + - not_null + - name: PROTOCOL_MARKET + tests: + - not_null + - name: INITIATOR + tests: + - not_null + - name: TARGET + tests: + - not_null + - name: FLASHLOAN_AMOUNT_UNADJ + tests: + - not_null + - name: FLASHLOAN_AMOUNT + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: FLASHLOAN_AMOUNT_USD + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: PLATFORM + tests: + - not_null + - name: BLOCKCHAIN + tests: + - not_null diff --git a/models/silver/defi/lending/complete_lending/silver__complete_lending_liquidations.sql b/models/silver/defi/lending/complete_lending/silver__complete_lending_liquidations.sql new file mode 100644 index 00000000..8389742e --- /dev/null +++ b/models/silver/defi/lending/complete_lending/silver__complete_lending_liquidations.sql @@ -0,0 +1,316 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = ['block_number','platform'], + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} + +WITH aave as ( +SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + liquidator, + borrower, + amount_unadj, + amount AS liquidated_amount, + NULL AS liquidated_amount_usd, + collateral_aave_token AS protocol_collateral_asset, + collateral_asset, + collateral_token_symbol AS collateral_asset_symbol, + debt_asset, + debt_token_symbol AS debt_asset_symbol, + 'Aave V3' AS platform, + 'optimism' AS blockchain, + _LOG_ID, + _INSERTED_TIMESTAMP +FROM + {{ ref('silver__aave_liquidations') }} + +{% if is_incremental() and 'aave' not in var('HEAL_CURATED_MODEL') %} +WHERE + _inserted_timestamp >= ( + SELECT + MAX(_inserted_timestamp) - INTERVAL '36 hours' + FROM + {{ this }} + ) +{% endif %} +), + +granary as ( +SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + liquidator, + borrower, + amount_unadj, + amount AS liquidated_amount, + NULL AS liquidated_amount_usd, + collateral_granary_token AS protocol_collateral_asset, + collateral_asset, + collateral_token_symbol AS collateral_asset_symbol, + debt_asset, + debt_token_symbol AS debt_asset_symbol, + 'Granary' AS platform, + 'optimism' AS blockchain, + _LOG_ID, + _INSERTED_TIMESTAMP +FROM + {{ ref('silver__granary_liquidations') }} + +{% if is_incremental() and 'granary' not in var('HEAL_CURATED_MODEL') %} +WHERE + _inserted_timestamp >= ( + SELECT + MAX(_inserted_timestamp) - INTERVAL '36 hours' + FROM + {{ this }} + ) +{% endif %} +), + +exactly as ( + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + liquidator, + borrower, + amount_unadj, + amount AS liquidated_amount, + NULL AS liquidated_amount_usd, + token AS protocol_collateral_asset, + liquidation_contract_address AS collateral_asset, + liquidation_contract_symbol AS collateral_asset_symbol, + collateral_token AS debt_asset, + collateral_symbol AS debt_asset_symbol, + platform, + 'optimism' AS blockchain, + l._LOG_ID, + l._INSERTED_TIMESTAMP + FROM + {{ ref('silver__exactly_liquidations') }} + l + +{% if is_incremental() and 'exactly' not in var('HEAL_CURATED_MODEL') %} +WHERE + l._inserted_timestamp >= ( + SELECT + MAX(_inserted_timestamp) - INTERVAL '36 hours' + FROM + {{ this }} + ) +{% endif %} +), + +sonne as ( + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + liquidator, + borrower, + amount_unadj, + amount AS liquidated_amount, + NULL AS liquidated_amount_usd, + token AS protocol_collateral_asset, + liquidation_contract_address AS collateral_asset, + liquidation_contract_symbol AS collateral_asset_symbol, + collateral_token AS debt_asset, + collateral_symbol AS debt_asset_symbol, + platform, + 'optimism' AS blockchain, + l._LOG_ID, + l._INSERTED_TIMESTAMP + FROM + {{ ref('silver__sonne_liquidations') }} + l + +{% if is_incremental() and 'sonne' not in var('HEAL_CURATED_MODEL') %} +WHERE + l._inserted_timestamp >= ( + SELECT + MAX(_inserted_timestamp) - INTERVAL '36 hours' + FROM + {{ this }} + ) +{% endif %} +), + +tarot as ( + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + liquidator, + borrower, + amount_unadj, + amount AS liquidated_amount, + NULL AS liquidated_amount_usd, + token AS protocol_collateral_asset, + liquidation_contract_address AS collateral_asset, + liquidation_contract_symbol AS collateral_asset_symbol, + collateral_token AS debt_asset, + collateral_symbol AS debt_asset_symbol, + platform, + 'optimism' AS blockchain, + l._LOG_ID, + l._INSERTED_TIMESTAMP + FROM + {{ ref('silver__tarot_liquidations') }} + l + +{% if is_incremental() and 'tarot' not in var('HEAL_CURATED_MODEL') %} +WHERE + l._inserted_timestamp >= ( + SELECT + MAX(_inserted_timestamp) - INTERVAL '36 hours' + FROM + {{ this }} + ) +{% endif %} +), + +liquidation_union as ( + SELECT + * + FROM + aave + UNION ALL + SELECT + * + FROM + granary + UNION ALL + SELECT + * + FROM + exactly + UNION ALL + SELECT + * + FROM + sonne + UNION ALL + SELECT + * + FROM + tarot +), + +contracts AS ( + SELECT + * + FROM + {{ ref('silver__contracts') }} C + WHERE + C.contract_address IN ( + SELECT + DISTINCT(collateral_asset) AS asset + FROM + liquidation_union + ) +), +prices AS ( + SELECT + * + FROM + {{ ref('price__ez_hourly_token_prices') }} + p + WHERE + token_address IN ( + SELECT + DISTINCT(collateral_asset) AS asset + FROM + liquidation_union + ) + AND HOUR > ( + SELECT + MIN(block_timestamp) + FROM + liquidation_union + ) +), +FINAL AS ( + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + A.contract_address, + CASE + WHEN platform = 'Sonne' THEN 'LiquidateBorrow' + WHEN platform IN ('Tarot','Exactly') THEN 'Liquidate' + ELSE 'LiquidationCall' + END AS event_name, + liquidator, + borrower, + protocol_collateral_asset AS protocol_market, + collateral_asset AS collateral_token, + collateral_asset_symbol AS collateral_token_symbol, + amount_unadj, + liquidated_amount AS amount, + ROUND( + liquidated_amount * p.price, + 2 + ) AS amount_usd, + debt_asset AS debt_token, + debt_asset_symbol AS debt_token_symbol, + platform, + A.blockchain, + A._LOG_ID, + A._INSERTED_TIMESTAMP + FROM + liquidation_union A + LEFT JOIN prices p + ON collateral_asset = p.token_address + AND DATE_TRUNC( + 'hour', + block_timestamp + ) = p.hour + LEFT JOIN contracts C + ON collateral_asset = C.contract_address +) +SELECT + *, + {{ dbt_utils.generate_surrogate_key( + ['tx_hash','event_index'] + ) }} AS complete_lending_liquidations_id, + SYSDATE() AS inserted_timestamp, + SYSDATE() AS modified_timestamp, + '{{ invocation_id }}' AS _invocation_id +FROM + FINAL qualify(ROW_NUMBER() over(PARTITION BY _log_id +ORDER BY + _inserted_timestamp DESC)) = 1 diff --git a/models/silver/defi/lending/complete_lending/silver__complete_lending_liquidations.yml b/models/silver/defi/lending/complete_lending/silver__complete_lending_liquidations.yml new file mode 100644 index 00000000..2ae699f0 --- /dev/null +++ b/models/silver/defi/lending/complete_lending/silver__complete_lending_liquidations.yml @@ -0,0 +1,58 @@ +version: 2 +models: + - name: silver__complete_lending_liquidations + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - TX_HASH + - EVENT_INDEX + columns: + - name: TX_HASH + tests: + - not_null + - name: BLOCK_NUMBER + tests: + - not_null + - name: BLOCK_TIMESTAMP + tests: + - not_null + - dbt_expectations.expect_row_values_to_have_recent_data: + datepart: day + interval: 60 + - name: EVENT_INDEX + tests: + - not_null + - name: LIQUIDATOR + tests: + - not_null + - name: BORROWER + tests: + - not_null + - name: PROTOCOL_MARKET + tests: + - not_null + - name: COLLATERAL_TOKEN + tests: + - not_null + - name: COLLATERAL_TOKEN_SYMBOL + tests: + - not_null + - name: AMOUNT_UNADJ + tests: + - not_null + - name: AMOUNT + tests: + - not_null + - name: AMOUNT_USD + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: PLATFORM + tests: + - not_null + - name: BLOCKCHAIN + tests: + - not_null + \ No newline at end of file diff --git a/models/silver/defi/lending/complete_lending/silver__complete_lending_repayments.sql b/models/silver/defi/lending/complete_lending/silver__complete_lending_repayments.sql new file mode 100644 index 00000000..295fcebf --- /dev/null +++ b/models/silver/defi/lending/complete_lending/silver__complete_lending_repayments.sql @@ -0,0 +1,265 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = ['block_number','platform'], + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} + +WITH aave as ( + + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + aave_market AS token_address, + aave_token AS protocol_market, + amount_unadj, + amount, + symbol AS token_symbol, + payer AS payer_address, + borrower, + 'Aave V3' AS platform, + 'optimism' AS blockchain, + _LOG_ID, + _INSERTED_TIMESTAMP + FROM + {{ ref('silver__aave_repayments') }} +{% if is_incremental() and 'aave' not in var('HEAL_CURATED_MODEL') %} +WHERE + _inserted_timestamp >= ( + SELECT + MAX(_inserted_timestamp) - INTERVAL '36 hours' + FROM + {{ this }} + ) +{% endif %} +), + +granary as ( + + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + granary_market AS token_address, + granary_token AS protocol_market, + amount_unadj, + amount, + symbol AS token_symbol, + payer AS payer_address, + borrower, + platform, + 'optimism' AS blockchain, + _LOG_ID, + _INSERTED_TIMESTAMP + FROM + {{ ref('silver__granary_repayments') }} + + {% if is_incremental() and 'granary' not in var('HEAL_CURATED_MODEL') %} + WHERE + _inserted_timestamp >= ( + SELECT + MAX(_inserted_timestamp) - INTERVAL '36 hours' + FROM + {{ this }} + ) + {% endif %} +), + +exactly as ( + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + repay_contract_address AS token_address, + token_address AS protocol_market, + amount_unadj, + amount, + repay_contract_symbol AS token_symbol, + payer AS payer_address, + borrower, + platform, + 'optimism' AS blockchain, + _LOG_ID, + _INSERTED_TIMESTAMP + FROM + {{ ref('silver__exactly_repayments') }} + + {% if is_incremental() and 'exactly' not in var('HEAL_CURATED_MODEL') %} + WHERE + _inserted_timestamp >= ( + SELECT + MAX(_inserted_timestamp) - INTERVAL '36 hours' + FROM + {{ this }} + ) + {% endif %} +), + +sonne as ( + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + repay_contract_address AS token_address, + token_address AS protocol_market, + amount_unadj, + amount, + repay_contract_symbol AS token_symbol, + payer AS payer_address, + borrower, + platform, + 'optimism' AS blockchain, + _LOG_ID, + _INSERTED_TIMESTAMP + FROM + {{ ref('silver__sonne_repayments') }} + + {% if is_incremental() and 'sonne' not in var('HEAL_CURATED_MODEL') %} + WHERE + _inserted_timestamp >= ( + SELECT + MAX(_inserted_timestamp) - INTERVAL '36 hours' + FROM + {{ this }} + ) + {% endif %} +), + +tarot as ( + + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + repay_contract_address AS token_address, + token_address AS protocol_market, + amount_unadj, + amount, + repay_contract_symbol AS token_symbol, + payer AS payer_address, + borrower, + platform, + 'optimism' AS blockchain, + _LOG_ID, + _INSERTED_TIMESTAMP + FROM + {{ ref('silver__tarot_repayments') }} + +{% if is_incremental() and 'tarot' not in var('HEAL_CURATED_MODEL') %} + WHERE + _inserted_timestamp >= ( + SELECT + MAX(_inserted_timestamp) - INTERVAL '36 hours' + FROM + {{ this }} + ) + {% endif %} + +), + +repayments_union as ( + SELECT + * + FROM + aave + UNION ALL + SELECT + * + FROM + granary + UNION ALL + SELECT + * + FROM + exactly + UNION ALL + SELECT + * + FROM + sonne + UNION ALL + SELECT + * + FROM + tarot +), +FINAL AS ( + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + A.contract_address, + CASE + WHEN platform = 'Sonne' THEN 'RepayBorrow' + ELSE 'Repay' + END AS event_name, + protocol_market, + payer_address AS payer, + borrower, + A.token_address, + A.token_symbol, + amount_unadj, + amount, + ROUND( + amount * price, + 2 + ) AS amount_usd, + platform, + blockchain, + A._LOG_ID, + A._INSERTED_TIMESTAMP + FROM + repayments_union A + LEFT JOIN {{ ref('price__ez_hourly_token_prices') }} + p + ON A.token_address = p.token_address + AND DATE_TRUNC( + 'hour', + block_timestamp + ) = p.hour + LEFT JOIN {{ ref('silver__contracts') }} C + ON A.token_address = C.contract_address +) +SELECT + *, + {{ dbt_utils.generate_surrogate_key( + ['tx_hash','event_index'] + ) }} AS complete_lending_repayments_id, + SYSDATE() AS inserted_timestamp, + SYSDATE() AS modified_timestamp, + '{{ invocation_id }}' AS _invocation_id +FROM + FINAL qualify(ROW_NUMBER() over(PARTITION BY _log_id +ORDER BY + _inserted_timestamp DESC)) = 1 diff --git a/models/silver/defi/lending/complete_lending/silver__complete_lending_repayments.yml b/models/silver/defi/lending/complete_lending/silver__complete_lending_repayments.yml new file mode 100644 index 00000000..759e317a --- /dev/null +++ b/models/silver/defi/lending/complete_lending/silver__complete_lending_repayments.yml @@ -0,0 +1,58 @@ +version: 2 +models: + - name: silver__complete_lending_repayments + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - TX_HASH + - EVENT_INDEX + columns: + - name: TX_HASH + tests: + - not_null + - name: BLOCK_NUMBER + tests: + - not_null + - name: BLOCK_TIMESTAMP + tests: + - not_null + - dbt_expectations.expect_row_values_to_have_recent_data: + datepart: day + interval: 30 + - name: EVENT_INDEX + tests: + - not_null + - name: PROTOCOL_MARKET + tests: + - not_null + - name: PAYER + tests: + - not_null: + where: PLATFORM <> 'Silo' + - name: BORROWER + tests: + - not_null + - name: TOKEN_ADDRESS + tests: + - not_null + - name: TOKEN_SYMBOL + tests: + - not_null + - name: AMOUNT_UNADJ + tests: + - not_null + - name: AMOUNT + tests: + - not_null + - name: AMOUNT_USD + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: PLATFORM + tests: + - not_null + - name: BLOCKCHAIN + tests: + - not_null \ No newline at end of file diff --git a/models/silver/defi/lending/complete_lending/silver__complete_lending_withdraws.sql b/models/silver/defi/lending/complete_lending/silver__complete_lending_withdraws.sql new file mode 100644 index 00000000..958394b2 --- /dev/null +++ b/models/silver/defi/lending/complete_lending/silver__complete_lending_withdraws.sql @@ -0,0 +1,272 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = ['block_number','platform'], + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} + +WITH aave as ( + + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + aave_token AS protocol_market, + aave_market AS token_address, + symbol AS token_symbol, + amount_unadj, + amount, + depositor_address, + 'Aave V3' AS platform, + 'optimism' AS blockchain, + _LOG_ID, + _INSERTED_TIMESTAMP + FROM + {{ ref('silver__aave_withdraws') }} + +{% if is_incremental() and 'aave' not in var('HEAL_CURATED_MODEL') %} +WHERE + _inserted_timestamp >= ( + SELECT + MAX( + _inserted_timestamp + ) - INTERVAL '36 hours' + FROM + {{ this }} + ) +{% endif %} +), + +granary as ( + + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + granary_token AS protocol_market, + granary_market AS token_address, + symbol AS token_symbol, + amount_unadj, + amount, + depositor_address, + platform, + 'optimism' AS blockchain, + _LOG_ID, + _INSERTED_TIMESTAMP + FROM + {{ ref('silver__granary_withdraws') }} + + {% if is_incremental() and 'granary' not in var('HEAL_CURATED_MODEL') %} + WHERE + _inserted_timestamp >= ( + SELECT + MAX( + _inserted_timestamp + ) - INTERVAL '36 hours' + FROM + {{ this }} + ) + {% endif %} +), + +exactly as ( + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + token_address AS protocol_market, + received_contract_address AS token_address, + received_contract_symbol token_symbol, + amount_unadj, + amount, + redeemer AS depositor_address, + platform, + 'optimism' AS blockchain, + _LOG_ID, + _INSERTED_TIMESTAMP + FROM + {{ ref('silver__exactly_withdraws') }} + + {% if is_incremental() and 'exactly' not in var('HEAL_CURATED_MODEL') %} + WHERE + _inserted_timestamp >= ( + SELECT + MAX( + _inserted_timestamp + ) - INTERVAL '36 hours' + FROM + {{ this }} + ) + {% endif %} +), + +sonne as ( + + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + token_address AS protocol_market, + received_contract_address AS token_address, + received_contract_symbol token_symbol, + amount_unadj, + amount, + redeemer AS depositor_address, + platform, + 'optimism' AS blockchain, + _LOG_ID, + _INSERTED_TIMESTAMP + FROM + {{ ref('silver__sonne_withdraws') }} + + {% if is_incremental() and 'sonne' not in var('HEAL_CURATED_MODEL') %} + WHERE + _inserted_timestamp >= ( + SELECT + MAX( + _inserted_timestamp + ) - INTERVAL '36 hours' + FROM + {{ this }} + ) + {% endif %} +), + +tarot as ( + + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + token_address AS protocol_market, + received_contract_address AS token_address, + received_contract_symbol token_symbol, + amount_unadj, + amount, + redeemer AS depositor_address, + platform, + 'optimism' AS blockchain, + _LOG_ID, + _INSERTED_TIMESTAMP + FROM + {{ ref('silver__tarot_withdraws') }} + +{% if is_incremental() and 'tarot' not in var('HEAL_CURATED_MODEL') %} + WHERE + _inserted_timestamp >= ( + SELECT + MAX( + _inserted_timestamp + ) - INTERVAL '36 hours' + FROM + {{ this }} + ) + {% endif %} + +), + +withdraws_union as ( + SELECT + * + FROM + aave + UNION ALL + SELECT + * + FROM + granary + UNION ALL + SELECT + * + FROM + exactly + UNION ALL + SELECT + * + FROM + sonne + UNION ALL + SELECT + * + FROM + tarot +), + +FINAL AS ( + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + A.contract_address, + CASE + WHEN platform IN ('Tarot','Sonne') THEN 'Redeem' + ELSE 'Withdraw' + END AS event_name, + protocol_market, + depositor_address AS depositor, + A.token_address, + A.token_symbol, + amount_unadj, + amount, + ROUND( + amount * price, + 2 + ) AS amount_usd, + platform, + blockchain, + A._log_id, + A._inserted_timestamp + FROM + withdraws_union A + LEFT JOIN {{ ref('price__ez_hourly_token_prices') }} + p + ON A.token_address = p.token_address + AND DATE_TRUNC( + 'hour', + block_timestamp + ) = p.hour + LEFT JOIN {{ ref('silver__contracts') }} C + ON A.token_address = C.contract_address +) +SELECT + *, + {{ dbt_utils.generate_surrogate_key( + ['tx_hash','event_index'] + ) }} AS complete_lending_withdraws_id, + SYSDATE() AS inserted_timestamp, + SYSDATE() AS modified_timestamp, + '{{ invocation_id }}' AS _invocation_id +FROM + FINAL qualify(ROW_NUMBER() over(PARTITION BY _log_id +ORDER BY + _inserted_timestamp DESC)) = 1 diff --git a/models/silver/defi/lending/complete_lending/silver__complete_lending_withdraws.yml b/models/silver/defi/lending/complete_lending/silver__complete_lending_withdraws.yml new file mode 100644 index 00000000..637c4191 --- /dev/null +++ b/models/silver/defi/lending/complete_lending/silver__complete_lending_withdraws.yml @@ -0,0 +1,56 @@ +version: 2 +models: + - name: silver__complete_lending_withdraws + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - TX_HASH + - EVENT_INDEX + columns: + - name: TX_HASH + tests: + - not_null + - name: BLOCK_NUMBER + tests: + - not_null + - name: BLOCK_TIMESTAMP + tests: + - not_null + - dbt_expectations.expect_row_values_to_have_recent_data: + datepart: day + interval: 30 + - name: EVENT_INDEX + tests: + - not_null + - name: PROTOCOL_MARKET + tests: + - not_null + - name: DEPOSITOR + tests: + - not_null + - name: TOKEN_ADDRESS + tests: + - not_null + - name: TOKEN_SYMBOL + tests: + - not_null + - name: AMOUNT_UNADJ + tests: + - not_null + - name: AMOUNT + tests: + - not_null + - name: AMOUNT_USD + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: PLATFORM + tests: + - not_null + - name: BLOCKCHAIN + tests: + - not_null + + \ No newline at end of file diff --git a/models/silver/defi/lending/exactly/silver__exactly_asset_details.sql b/models/silver/defi/lending/exactly/silver__exactly_asset_details.sql new file mode 100644 index 00000000..e4a92ab8 --- /dev/null +++ b/models/silver/defi/lending/exactly/silver__exactly_asset_details.sql @@ -0,0 +1,98 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = "block_number", + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} + +WITH log_pull AS ( + + SELECT + tx_hash, + block_number, + block_timestamp, + contract_address, + _inserted_timestamp, + _log_id + FROM + {{ ref('silver__logs') }} + WHERE + topics [0] :: STRING = '0x7902cd1307c545e3f5782172612372bf997a93698917ced12b2f83d86e347d0c' + AND origin_from_address = LOWER('0xe61bdef3fff4c3cf7a07996dcb8802b5c85b665a') + + +{% if is_incremental() %} +AND _inserted_timestamp >= ( + SELECT + MAX( + _inserted_timestamp + ) - INTERVAL '12 hours' + FROM + {{ this }} +) +{% endif %} +), +traces_pull AS ( + SELECT + from_address AS token_address, + to_address AS underlying_asset + FROM + {{ ref('silver__traces') }} + WHERE + tx_hash IN ( + SELECT + tx_hash + FROM + log_pull + ) + AND type = 'STATICCALL' +), +contracts AS ( + SELECT + * + FROM + {{ ref('silver__contracts') }} +), +contract_pull AS ( + SELECT + l.tx_hash, + l.block_number, + l.block_timestamp, + l.contract_address, + C.token_name, + C.token_symbol, + C.token_decimals, + t.underlying_asset, + l._inserted_timestamp, + l._log_id + FROM + log_pull l + LEFT JOIN traces_pull t + ON l.contract_address = t.token_address + LEFT JOIN contracts C + ON C.contract_address = l.contract_address qualify(ROW_NUMBER() over(PARTITION BY l.contract_address + ORDER BY + block_timestamp ASC)) = 1 +) +SELECT + l.tx_hash, + l.block_number, + l.block_timestamp, + l.contract_address AS token_address, + l.token_name, + l.token_symbol, + l.token_decimals, + l.underlying_asset AS underlying_asset_address, + C.token_name AS underlying_name, + C.token_symbol AS underlying_symbol, + C.token_decimals AS underlying_decimals, + l._inserted_timestamp, + l._log_id +FROM + contract_pull l + LEFT JOIN contracts C + ON C.contract_address = l.underlying_asset +WHERE + underlying_asset IS NOT NULL + AND l.token_name IS NOT NULL diff --git a/models/silver/defi/lending/exactly/silver__exactly_borrows.sql b/models/silver/defi/lending/exactly/silver__exactly_borrows.sql new file mode 100644 index 00000000..23dd71bb --- /dev/null +++ b/models/silver/defi/lending/exactly/silver__exactly_borrows.sql @@ -0,0 +1,116 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = "block_number", + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} +-- pull all token addresses and corresponding name +WITH asset_details AS ( + + SELECT + token_address, + token_symbol, + token_name, + token_decimals, + underlying_asset_address, + underlying_name, + underlying_symbol, + underlying_decimals + FROM + {{ ref('silver__exactly_asset_details') }} +), +sonne_borrows AS ( + SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + regexp_substr_all(SUBSTR(DATA, 3, len(DATA)), '.{64}') AS segmented_data, + CONCAT('0x', SUBSTR(topics [3] :: STRING, 27, 40)) AS borrower, + utils.udf_hex_to_int( + segmented_data [0] :: STRING + ) :: INTEGER AS loan_amount_raw, + 0 AS accountBorrows, + 0 AS totalBorrows, + contract_address AS token, + 'Exactly' AS platform, + _inserted_timestamp, + _log_id + FROM + {{ ref('silver__logs') }} + WHERE + contract_address IN ( + SELECT + token_address + FROM + asset_details + ) + AND topics [0] :: STRING = '0x96558a334f4759f0e7c423d68c84721860bd8fbf94ddc4e55158ecb125ad04b5' + +{% if is_incremental() %} +AND _inserted_timestamp >= ( + SELECT + MAX( + _inserted_timestamp + ) - INTERVAL '12 hours' + FROM + {{ this }} +) +{% endif %} +), +sonne_combine AS ( + SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + borrower, + loan_amount_raw, + C.underlying_asset_address AS borrows_contract_address, + C.underlying_symbol AS borrows_contract_symbol, + token, + C.token_symbol, + C.underlying_decimals, + b.platform, + b._log_id, + b._inserted_timestamp + FROM + sonne_borrows b + LEFT JOIN asset_details C + ON b.token = C.token_address +) +SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + borrower, + borrows_contract_address, + borrows_contract_symbol, + token as token_address, + token_symbol, + loan_amount_raw AS amount_unadj, + loan_amount_raw / pow( + 10, + underlying_decimals + ) AS amount, + platform, + _inserted_timestamp, + _log_id +FROM + sonne_combine qualify(ROW_NUMBER() over(PARTITION BY _log_id +ORDER BY + _inserted_timestamp DESC)) = 1 diff --git a/models/silver/defi/lending/exactly/silver__exactly_borrows.yml b/models/silver/defi/lending/exactly/silver__exactly_borrows.yml new file mode 100644 index 00000000..b3f1d374 --- /dev/null +++ b/models/silver/defi/lending/exactly/silver__exactly_borrows.yml @@ -0,0 +1,42 @@ +version: 2 +models: + - name: silver__exactly_borrows + + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - _log_id + columns: + - name: BLOCK_NUMBER + tests: + - not_null + - name: BLOCK_TIMESTAMP + tests: + - not_null + - name: BORROWER + tests: + - not_null + - name: BORROWS_CONTRACT_ADDRESS + tests: + - not_null + - name: BORROWS_CONTRACT_SYMBOL + tests: + - not_null + - name: TOKEN_ADDRESS + tests: + - not_null + - name: TOKEN_SYMBOL + tests: + - not_null + - name: AMOUNT + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: TX_HASH + tests: + - not_null + - name: EVENT_INDEX + tests: + - not_null \ No newline at end of file diff --git a/models/silver/defi/lending/exactly/silver__exactly_deposits.sql b/models/silver/defi/lending/exactly/silver__exactly_deposits.sql new file mode 100644 index 00000000..f3b7588c --- /dev/null +++ b/models/silver/defi/lending/exactly/silver__exactly_deposits.sql @@ -0,0 +1,123 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = "block_number", + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} +-- pull all token addresses and corresponding name +WITH asset_details AS ( + + SELECT + token_address, + token_symbol, + token_name, + token_decimals, + underlying_asset_address, + underlying_name, + underlying_symbol, + underlying_decimals + FROM + {{ ref('silver__exactly_asset_details') }} +), +exactly_deposits AS ( + SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + contract_address AS token_address, + regexp_substr_all(SUBSTR(DATA, 3, len(DATA)), '.{64}') AS segmented_data, + CONCAT('0x', SUBSTR(topics [2] :: STRING, 27, 40)) AS supplier, + utils.udf_hex_to_int( + segmented_data [1] :: STRING + ) :: INTEGER AS minttokens_raw, + utils.udf_hex_to_int( + segmented_data [0] :: STRING + ) :: INTEGER AS mintAmount_raw, + 'Exactly' AS platform, + _inserted_timestamp, + _log_id + FROM + {{ ref('silver__logs') }} + WHERE + contract_address IN ( + SELECT + token_address + FROM + asset_details + ) + AND topics [0] :: STRING = '0xdcbc1c05240f31ff3ad067ef1ee35ce4997762752e3a095284754544f4c709d7' + +{% if is_incremental() %} +AND _inserted_timestamp >= ( + SELECT + MAX( + _inserted_timestamp + ) - INTERVAL '12 hours' + FROM + {{ this }} +) +{% endif %} +), +exactly_combine AS ( + SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + supplier, + minttokens_raw, + mintAmount_raw, + C.underlying_asset_address AS supplied_contract_addr, + C.underlying_symbol AS supplied_symbol, + C.token_address, + C.token_symbol, + C.token_decimals, + C.underlying_decimals, + b.platform, + b._log_id, + b._inserted_timestamp + FROM + exactly_deposits b + LEFT JOIN asset_details C + ON b.token_address = C.token_address +) +SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + token_address, + token_symbol, + minttokens_raw / pow( + 10, + token_decimals + ) AS issued_tokens, + mintAmount_raw AS amount_unadj, + mintAmount_raw / pow( + 10, + underlying_decimals + ) AS amount, + supplied_contract_addr, + supplied_symbol, + supplier, + platform, + _inserted_timestamp, + _log_id +FROM + exactly_combine qualify(ROW_NUMBER() over(PARTITION BY _log_id +ORDER BY + _inserted_timestamp DESC)) = 1 diff --git a/models/silver/defi/lending/exactly/silver__exactly_deposits.yml b/models/silver/defi/lending/exactly/silver__exactly_deposits.yml new file mode 100644 index 00000000..f68266c6 --- /dev/null +++ b/models/silver/defi/lending/exactly/silver__exactly_deposits.yml @@ -0,0 +1,50 @@ +version: 2 +models: + - name: silver__exactly_deposits + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - _log_id + columns: + - name: BLOCK_NUMBER + tests: + - not_null + - name: BLOCK_TIMESTAMP + tests: + - not_null + - dbt_expectations.expect_row_values_to_have_recent_data: + datepart: day + interval: 3 + - name: TOKEN_ADDRESS + tests: + - not_null + - name: TOKEN_SYMBOL + tests: + - not_null + - name: ISSUED_TOKENS + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: AMOUNT + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: SUPPLIED_CONTRACT_ADDR + tests: + - not_null + - name: SUPPLIED_SYMBOL + tests: + - not_null + - name: SUPPLIER + tests: + - not_null + - name: TX_HASH + tests: + - not_null + - name: EVENT_INDEX + tests: + - not_null \ No newline at end of file diff --git a/models/silver/defi/lending/exactly/silver__exactly_liquidations.sql b/models/silver/defi/lending/exactly/silver__exactly_liquidations.sql new file mode 100644 index 00000000..b18fc705 --- /dev/null +++ b/models/silver/defi/lending/exactly/silver__exactly_liquidations.sql @@ -0,0 +1,115 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = "block_number", + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} +-- pull all token addresses and corresponding name +-- add the collateral liquidated here +WITH asset_details AS ( + + SELECT + token_address, + token_symbol, + token_name, + token_decimals, + underlying_asset_address, + underlying_name, + underlying_symbol, + underlying_decimals + FROM + {{ ref('silver__exactly_asset_details') }} +), +exactly_liquidations AS ( + SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + regexp_substr_all(SUBSTR(DATA, 3, len(DATA)), '.{64}') AS segmented_data, + CONCAT('0x', SUBSTR(topics [2] :: STRING, 27, 40)) AS borrower, + contract_address AS token, + CONCAT('0x', SUBSTR(topics [1] :: STRING, 27, 40)) AS liquidator, + utils.udf_hex_to_int( + segmented_data [2] :: STRING + ) :: INTEGER AS seizeTokens_raw, + utils.udf_hex_to_int( + segmented_data [1] :: STRING + ) :: INTEGER AS repayAmount_raw, + CONCAT('0x', SUBSTR(topics [3] :: STRING, 27, 40)) AS tokenCollateral, + 'Exactly' AS platform, + _inserted_timestamp, + _log_id + FROM + {{ ref('silver__logs') }} + WHERE + contract_address IN ( + SELECT + token_address + FROM + asset_details + ) + AND topics [0] :: STRING = '0x67bb48f97d82192848c24158abf58ec614777328e19655e0a219652b773fd1db' + +{% if is_incremental() %} +AND _inserted_timestamp >= ( + SELECT + MAX( + _inserted_timestamp + ) - INTERVAL '12 hours' + FROM + {{ this }} +) +{% endif %} +), +liquidation_union AS ( + SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + borrower, + token, + asd1.token_symbol AS token_symbol, + liquidator, + seizeTokens_raw / pow( + 10, + asd2.token_decimals + ) AS tokens_seized, + tokenCollateral AS protocol_market, + asd2.token_symbol AS collateral_token_symbol, + asd2.underlying_asset_address AS collateral_token, + asd2.underlying_symbol AS collateral_symbol, + repayAmount_raw AS amount_unadj, + repayAmount_raw / pow( + 10, + asd1.underlying_decimals + ) AS amount, + asd1.underlying_decimals, + asd1.underlying_asset_address AS liquidation_contract_address, + asd1.underlying_symbol AS liquidation_contract_symbol, + l.platform, + l._inserted_timestamp, + l._log_id + FROM + exactly_liquidations l + LEFT JOIN asset_details asd1 + ON l.token = asd1.token_address + LEFT JOIN asset_details asd2 + ON l.tokenCollateral = asd2.token_address +) +SELECT + * +FROM + liquidation_union qualify(ROW_NUMBER() over(PARTITION BY _log_id +ORDER BY + _inserted_timestamp DESC)) = 1 diff --git a/models/silver/defi/lending/exactly/silver__exactly_liquidations.yml b/models/silver/defi/lending/exactly/silver__exactly_liquidations.yml new file mode 100644 index 00000000..993a6ec9 --- /dev/null +++ b/models/silver/defi/lending/exactly/silver__exactly_liquidations.yml @@ -0,0 +1,56 @@ +version: 2 +models: + - name: silver__exactly_liquidations + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - _log_id + columns: + - name: BLOCK_NUMBER + tests: + - not_null + - name: BLOCK_TIMESTAMP + tests: + - not_null + - name: BORROWER + tests: + - not_null + - name: TOKEN + tests: + - not_null + - name: TOKEN_SYMBOL + tests: + - not_null + - name: LIQUIDATOR + tests: + - not_null + - name: TOKENS_SEIZED + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: AMOUNT + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: LIQUIDATION_CONTRACT_ADDRESS + tests: + - not_null + - name: LIQUIDATION_CONTRACT_SYMBOL + tests: + - not_null + - name: TX_HASH + tests: + - not_null + - name: COLLATERAL_TOKEN + tests: + - not_null + - name: COLLATERAL_SYMBOL + tests: + - not_null + - name: EVENT_INDEX + tests: + - not_null \ No newline at end of file diff --git a/models/silver/defi/lending/exactly/silver__exactly_repayments.sql b/models/silver/defi/lending/exactly/silver__exactly_repayments.sql new file mode 100644 index 00000000..133711f8 --- /dev/null +++ b/models/silver/defi/lending/exactly/silver__exactly_repayments.sql @@ -0,0 +1,117 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = "block_number", + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} +-- pull all token addresses and corresponding name +WITH asset_details AS ( + + SELECT + token_address, + token_symbol, + token_name, + token_decimals, + underlying_asset_address, + underlying_name, + underlying_symbol, + underlying_decimals + FROM + {{ ref('silver__exactly_asset_details') }} +), +exactly_repayments AS ( + SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + regexp_substr_all(SUBSTR(DATA, 3, len(DATA)), '.{64}') AS segmented_data, + CONCAT('0x', SUBSTR(topics [2] :: STRING, 27, 40)) AS borrower, + contract_address AS token, + CONCAT('0x', SUBSTR(topics [1] :: STRING, 27, 40)) AS payer, + utils.udf_hex_to_int( + segmented_data [1] :: STRING + ) :: INTEGER AS repayed_amount_raw, + 'Exactly' AS platform, + _inserted_timestamp, + _log_id + FROM + {{ ref('silver__logs') }} + WHERE + contract_address IN ( + SELECT + token_address + FROM + asset_details + ) + AND topics [0] :: STRING = '0xe4a1ae657f49cb1fb1c7d3a94ae6093565c4c8c0e03de488f79c377c3c3a24e0' + +{% if is_incremental() %} +AND _inserted_timestamp >= ( + SELECT + MAX( + _inserted_timestamp + ) - INTERVAL '12 hours' + FROM + {{ this }} +) +{% endif %} +), +exactly_combine AS ( + SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + borrower, + token, + C.token_symbol, + payer, + repayed_amount_raw, + C.underlying_asset_address AS repay_contract_address, + C.underlying_symbol AS repay_contract_symbol, + C.underlying_decimals, + b.platform, + b._log_id, + b._inserted_timestamp + FROM + exactly_repayments b + LEFT JOIN asset_details C + ON b.token = C.token_address +) +SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + borrower, + token, + token_symbol, + payer, + repay_contract_address, + repay_contract_symbol, + repayed_amount_raw AS amount_unadj, + repayed_amount_raw / pow( + 10, + underlying_decimals + ) AS amount, + platform, + _inserted_timestamp, + _log_id +FROM + exactly_combine qualify(ROW_NUMBER() over(PARTITION BY _log_id +ORDER BY + _inserted_timestamp DESC)) = 1 diff --git a/models/silver/defi/lending/exactly/silver__exactly_repayments.yml b/models/silver/defi/lending/exactly/silver__exactly_repayments.yml new file mode 100644 index 00000000..d482f9ae --- /dev/null +++ b/models/silver/defi/lending/exactly/silver__exactly_repayments.yml @@ -0,0 +1,44 @@ +version: 2 +models: + - name: silver__exactly_repayments + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - _log_id + columns: + - name: BLOCK_NUMBER + tests: + - not_null + - name: BLOCK_TIMESTAMP + tests: + - not_null + - name: BORROWER + tests: + - not_null + - name: TOKEN + tests: + - not_null + - name: TOKEN_SYMBOL + tests: + - not_null + - name: PAYER + tests: + - not_null + - name: REPAY_CONTRACT_ADDRESS + tests: + - not_null + - name: REPAY_CONTRACT_SYMBOL + tests: + - not_null + - name: AMOUNT + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: TX_HASH + tests: + - not_null + - name: EVENT_INDEX + tests: + - not_null \ No newline at end of file diff --git a/models/silver/defi/lending/exactly/silver__exactly_withdraws.sql b/models/silver/defi/lending/exactly/silver__exactly_withdraws.sql new file mode 100644 index 00000000..d942e43c --- /dev/null +++ b/models/silver/defi/lending/exactly/silver__exactly_withdraws.sql @@ -0,0 +1,117 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = "block_number", + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} +-- pull all token addresses and corresponding name +WITH asset_details AS ( + + SELECT + token_address, + token_symbol, + token_name, + token_decimals, + underlying_asset_address, + underlying_name, + underlying_symbol, + underlying_decimals + FROM + {{ ref('silver__exactly_asset_details') }} +), +exactly_redemptions AS ( + SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + contract_address AS token, + regexp_substr_all(SUBSTR(DATA, 3, len(DATA)), '.{64}') AS segmented_data, + utils.udf_hex_to_int( + segmented_data [0] :: STRING + ) :: INTEGER AS received_amount_raw, + 0 AS redeemed_token_raw, + CONCAT('0x', SUBSTR(topics [1] :: STRING, 27, 40)) AS redeemer, + 'Exactly' AS platform, + _inserted_timestamp, + _log_id + FROM + {{ ref('silver__logs') }} + WHERE + contract_address IN ( + SELECT + token_address + FROM + asset_details + ) + AND topics [0] :: STRING = '0xfbde797d201c681b91056529119e0b02407c7bb96a4a2c75c01fc9667232c8db' + +{% if is_incremental() %} +AND _inserted_timestamp >= ( + SELECT + MAX( + _inserted_timestamp + ) - INTERVAL '12 hours' + FROM + {{ this }} +) +{% endif %} +), +exactly_combine AS ( + SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + token, + redeemer, + received_amount_raw, + redeemed_token_raw, + C.underlying_asset_address AS received_contract_address, + C.underlying_symbol AS received_contract_symbol, + C.token_symbol, + C.token_decimals, + C.underlying_decimals, + b.platform, + b._log_id, + b._inserted_timestamp + FROM + exactly_redemptions b + LEFT JOIN asset_details C + ON b.token = C.token_address +) +SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + token as token_address, + token_symbol, + received_amount_raw AS amount_unadj, + received_amount_raw / pow( + 10, + underlying_decimals + ) AS amount, + received_contract_address, + received_contract_symbol, + redeemer, + platform, + _inserted_timestamp, + _log_id +FROM + exactly_combine ee qualify(ROW_NUMBER() over(PARTITION BY _log_id +ORDER BY + _inserted_timestamp DESC)) = 1 diff --git a/models/silver/defi/lending/exactly/silver__exactly_withdraws.yml b/models/silver/defi/lending/exactly/silver__exactly_withdraws.yml new file mode 100644 index 00000000..029b6e17 --- /dev/null +++ b/models/silver/defi/lending/exactly/silver__exactly_withdraws.yml @@ -0,0 +1,47 @@ +version: 2 +models: + - name: silver__exactly_withdraws + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - _log_id + columns: + - name: BLOCK_NUMBER + tests: + - not_null + - name: BLOCK_TIMESTAMP + tests: + - not_null + - name: TOKEN_ADDRESS + tests: + - not_null + - name: TOKEN_SYMBOL + tests: + - not_null + - name: AMOUNT + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: RECEIVED_CONTRACT_ADDRESS + tests: + - not_null + - name: RECEIVED_CONTRACT_SYMBOL + tests: + - not_null + - name: REDEEMED_TOKEN + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: REDEEMER + tests: + - not_null + - name: TX_HASH + tests: + - not_null + - name: EVENT_INDEX + tests: + - not_null \ No newline at end of file diff --git a/models/silver/defi/lending/granary/silver__granary_borrows.sql b/models/silver/defi/lending/granary/silver__granary_borrows.sql new file mode 100644 index 00000000..f74b0e45 --- /dev/null +++ b/models/silver/defi/lending/granary/silver__granary_borrows.sql @@ -0,0 +1,116 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = "block_number", + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} + +WITH --borrows from granary LendingPool contracts +borrow AS ( + + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + regexp_substr_all(SUBSTR(DATA, 3, len(DATA)), '.{64}') AS segmented_data, + CONCAT('0x', SUBSTR(topics [1] :: STRING, 27, 40)) AS granary_market, + CONCAT('0x', SUBSTR(topics [2] :: STRING, 27, 40)) AS onBehalfOf, + utils.udf_hex_to_int( + topics [3] :: STRING + ) :: INTEGER AS refferal, + CONCAT('0x', SUBSTR(segmented_data [0] :: STRING, 25, 40)) AS userAddress, + utils.udf_hex_to_int( + segmented_data [1] :: STRING + ) :: INTEGER AS borrow_quantity, + utils.udf_hex_to_int( + segmented_data [2] :: STRING + ) :: INTEGER AS borrow_rate_mode, + utils.udf_hex_to_int( + segmented_data [3] :: STRING + ) :: INTEGER AS borrowrate, + CASE + WHEN contract_address = LOWER('0x8FD4aF47E4E63d1D2D45582c3286b4BD9Bb95DfE') THEN 'Granary' + ELSE 'ERROR' + END AS granary_version, + origin_from_address AS borrower_address, + COALESCE( + origin_to_address, + contract_address + ) AS lending_pool_contract, + _inserted_timestamp, + _log_id + FROM + {{ ref('silver__logs') }} + WHERE + topics [0] :: STRING = '0xc6a898309e823ee50bac64e45ca8adba6690e99e7841c45d754e2a38e9019d9b' + +{% if is_incremental() %} +AND _inserted_timestamp >= ( + SELECT + MAX( + _inserted_timestamp + ) - INTERVAL '12 hours' + FROM + {{ this }} +) +{% endif %} +AND contract_address = LOWER('0x8FD4aF47E4E63d1D2D45582c3286b4BD9Bb95DfE') +AND tx_status = 'SUCCESS' --excludes failed txs +), +atoken_meta AS ( + SELECT + atoken_address, + atoken_symbol, + atoken_name, + atoken_decimals, + underlying_address, + underlying_symbol, + underlying_name, + underlying_decimals, + atoken_version, + atoken_created_block, + atoken_stable_debt_address, + atoken_variable_debt_address + FROM + {{ ref('silver__granary_tokens') }} +) +SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + granary_market, + atoken_meta.atoken_address AS granary_token, + borrow_quantity AS amount_unadj, + borrow_quantity / pow( + 10, + atoken_meta.underlying_decimals + ) AS amount, + borrower_address, + CASE + WHEN borrow_rate_mode = 2 THEN 'Variable Rate' + ELSE 'Stable Rate' + END AS borrow_rate_mode, + lending_pool_contract, + granary_version AS platform, + atoken_meta.underlying_symbol AS symbol, + atoken_meta.underlying_decimals AS underlying_decimals, + 'optimism' AS blockchain, + _log_id, + _inserted_timestamp +FROM + borrow + LEFT JOIN atoken_meta + ON borrow.granary_market = atoken_meta.underlying_address qualify(ROW_NUMBER() over(PARTITION BY _log_id +ORDER BY + _inserted_timestamp DESC)) = 1 diff --git a/models/silver/defi/lending/granary/silver__granary_borrows.yml b/models/silver/defi/lending/granary/silver__granary_borrows.yml new file mode 100644 index 00000000..302b7672 --- /dev/null +++ b/models/silver/defi/lending/granary/silver__granary_borrows.yml @@ -0,0 +1,52 @@ +version: 2 +models: + - name: silver__granary_borrows + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - TX_HASH + - EVENT_INDEX + columns: + - name: TX_HASH + tests: + - not_null + - name: BLOCK_NUMBER + tests: + - not_null + - name: BLOCK_TIMESTAMP + tests: + - not_null + - name: EVENT_INDEX + tests: + - not_null + - name: granary_MARKET + tests: + - not_null + - name: AMOUNT_UNADJ + tests: + - not_null + - name: AMOUNT + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: BORROWER_ADDRESS + tests: + - not_null + - name: BORROW_RATE_MODE + tests: + - not_null + - name: LENDING_POOL_CONTRACT + tests: + - not_null + - name: PLATFORM + tests: + - not_null + - name: SYMBOL + tests: + - not_null + - name: BLOCKCHAIN + tests: + - not_null \ No newline at end of file diff --git a/models/silver/defi/lending/granary/silver__granary_deposits.sql b/models/silver/defi/lending/granary/silver__granary_deposits.sql new file mode 100644 index 00000000..809c29d4 --- /dev/null +++ b/models/silver/defi/lending/granary/silver__granary_deposits.sql @@ -0,0 +1,104 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = "block_number", + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} + +WITH deposits AS( + + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + regexp_substr_all(SUBSTR(DATA, 3, len(DATA)), '.{64}') AS segmented_data, + CONCAT('0x', SUBSTR(topics [1] :: STRING, 27, 40)) AS granary_market, + CONCAT('0x', SUBSTR(topics [2] :: STRING, 27, 40)) AS onBehalfOf, + utils.udf_hex_to_int( + topics [3] :: STRING + ) :: INTEGER AS refferal, + CONCAT('0x', SUBSTR(topics [2] :: STRING, 27, 42)) AS userAddress, + utils.udf_hex_to_int( + segmented_data [1] :: STRING + ) :: INTEGER AS deposit_quantity, + CASE + WHEN contract_address = LOWER('0x8FD4aF47E4E63d1D2D45582c3286b4BD9Bb95DfE') THEN 'Granary' + ELSE 'ERROR' + END AS granary_version, + origin_from_address AS depositor_address, + COALESCE( + origin_to_address, + contract_address + ) AS lending_pool_contract, + _inserted_timestamp, + _log_id + FROM + {{ ref('silver__logs') }} + WHERE + topics [0] :: STRING = '0xde6857219544bb5b7746f48ed30be6386fefc61b2f864cacf559893bf50fd951' + +{% if is_incremental() %} +AND _inserted_timestamp >= ( + SELECT + MAX( + _inserted_timestamp + ) - INTERVAL '12 hours' + FROM + {{ this }} +) +{% endif %} +AND contract_address = LOWER('0x8FD4aF47E4E63d1D2D45582c3286b4BD9Bb95DfE') +AND tx_status = 'SUCCESS' --excludes failed txs +), +atoken_meta AS ( + SELECT + atoken_address, + atoken_symbol, + atoken_name, + atoken_decimals, + underlying_address, + underlying_symbol, + underlying_name, + underlying_decimals, + atoken_version, + atoken_created_block, + atoken_stable_debt_address, + atoken_variable_debt_address + FROM + {{ ref('silver__granary_tokens') }} +) +SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + granary_market, + atoken_meta.atoken_address AS granary_token, + deposit_quantity AS amount_unadj, + deposit_quantity / pow( + 10, + atoken_meta.underlying_decimals + ) AS amount, + depositor_address, + lending_pool_contract, + granary_version AS platform, + atoken_meta.underlying_symbol AS symbol, + 'optimism' AS blockchain, + _log_id, + _inserted_timestamp +FROM + deposits + LEFT JOIN atoken_meta + ON deposits.granary_market = atoken_meta.underlying_address qualify(ROW_NUMBER() over(PARTITION BY _log_id +ORDER BY + _inserted_timestamp DESC)) = 1 diff --git a/models/silver/defi/lending/granary/silver__granary_deposits.yml b/models/silver/defi/lending/granary/silver__granary_deposits.yml new file mode 100644 index 00000000..30c23118 --- /dev/null +++ b/models/silver/defi/lending/granary/silver__granary_deposits.yml @@ -0,0 +1,48 @@ +version: 2 +models: + - name: silver__granary_deposits + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - TX_HASH + - EVENT_INDEX + columns: + - name: BLOCKCHAIN + tests: + - not_null + - name: BLOCK_NUMBER + tests: + - not_null + - name: BLOCK_TIMESTAMP + tests: + - not_null + - name: TX_HASH + tests: + - not_null + - name: AMOUNT_UNADJ + tests: + - not_null + - name: AMOUNT + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: EVENT_INDEX + tests: + - not_null + - name: granary_MARKET + tests: + - not_null + - name: DEPOSITOR_ADDRESS + tests: + - not_null + - name: LENDING_POOL_CONTRACT + tests: + - not_null + - name: PLATFORM + tests: + - not_null + - name: SYMBOL + tests: + - not_null \ No newline at end of file diff --git a/models/silver/defi/lending/granary/silver__granary_flashloans.sql b/models/silver/defi/lending/granary/silver__granary_flashloans.sql new file mode 100644 index 00000000..482e9f34 --- /dev/null +++ b/models/silver/defi/lending/granary/silver__granary_flashloans.sql @@ -0,0 +1,111 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = "block_number", + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} + +WITH flashloan AS ( + + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + regexp_substr_all(SUBSTR(DATA, 3, len(DATA)), '.{64}') AS segmented_data, + CONCAT('0x', SUBSTR(topics [1] :: STRING, 27, 40)) AS target_address, + origin_to_address AS initiator_address, + CONCAT('0x', SUBSTR(topics [3] :: STRING, 27, 40)) AS granary_market, + utils.udf_hex_to_int( + segmented_data [0] :: STRING + ) :: INTEGER AS flashloan_quantity, + utils.udf_hex_to_int( + segmented_data [1] :: STRING + ) :: INTEGER AS premium_quantity, + utils.udf_hex_to_int( + topics [2] :: STRING + ) :: INTEGER AS refferalCode, + COALESCE( + origin_to_address, + contract_address + ) AS lending_pool_contract, + CASE + WHEN contract_address = LOWER('0x8FD4aF47E4E63d1D2D45582c3286b4BD9Bb95DfE') THEN 'Granary' + ELSE 'ERROR' + END AS granary_version, + _inserted_timestamp, + _log_id + FROM + {{ ref('silver__logs') }} + WHERE + topics [0] :: STRING = '0x631042c832b07452973831137f2d73e395028b44b250dedc5abb0ee766e168ac' + +{% if is_incremental() %} +AND _inserted_timestamp >= ( + SELECT + MAX( + _inserted_timestamp + ) - INTERVAL '12 hours' + FROM + {{ this }} +) +{% endif %} +AND contract_address = LOWER('0x8FD4aF47E4E63d1D2D45582c3286b4BD9Bb95DfE') +AND tx_status = 'SUCCESS' --excludes failed txs +), +atoken_meta AS ( + SELECT + atoken_address, + atoken_symbol, + atoken_name, + atoken_decimals, + underlying_address, + underlying_symbol, + underlying_name, + underlying_decimals, + atoken_version, + atoken_created_block, + atoken_stable_debt_address, + atoken_variable_debt_address + FROM + {{ ref('silver__granary_tokens') }} +) +SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + granary_market, + atoken_meta.atoken_address AS granary_token, + flashloan_quantity AS flashloan_amount_unadj, + flashloan_quantity / pow( + 10, + atoken_meta.underlying_decimals + ) AS flashloan_amount, + premium_quantity AS premium_amount_unadj, + premium_quantity / pow( + 10, + atoken_meta.underlying_decimals + ) AS premium_amount, + initiator_address AS initiator_address, + target_address AS target_address, + granary_version AS platform, + atoken_meta.underlying_symbol AS symbol, + 'optimism' AS blockchain, + _log_id, + _inserted_timestamp +FROM + flashloan + LEFT JOIN atoken_meta + ON flashloan.granary_market = atoken_meta.underlying_address qualify(ROW_NUMBER() over(PARTITION BY _log_id +ORDER BY + _inserted_timestamp DESC)) = 1 diff --git a/models/silver/defi/lending/granary/silver__granary_flashloans.yml b/models/silver/defi/lending/granary/silver__granary_flashloans.yml new file mode 100644 index 00000000..e69fc358 --- /dev/null +++ b/models/silver/defi/lending/granary/silver__granary_flashloans.yml @@ -0,0 +1,49 @@ +version: 2 +models: + - name: silver__granary_flashloans + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - TX_HASH + - EVENT_INDEX + columns: + - name: BLOCKCHAIN + tests: + - not_null + - name: BLOCK_NUMBER + tests: + - not_null + - name: BLOCK_TIMESTAMP + tests: + - not_null + - name: EVENT_INDEX + tests: + - not_null + - name: TX_HASH + tests: + - not_null + - name: granary_MARKET + tests: + - not_null + - name: granary_TOKEN + - name: FLASHLOAN_AMOUNT_UNADJ + tests: + - not_null + - name: FLASHLOAN_AMOUNT + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: INITIATOR_ADDRESS + tests: + - not_null + - name: TARGET_ADDRESS + tests: + - not_null + - name: PLATFORM + tests: + - not_null + - name: SYMBOL + tests: + - not_null diff --git a/models/silver/defi/lending/granary/silver__granary_liquidations.sql b/models/silver/defi/lending/granary/silver__granary_liquidations.sql new file mode 100644 index 00000000..460e44cc --- /dev/null +++ b/models/silver/defi/lending/granary/silver__granary_liquidations.sql @@ -0,0 +1,109 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = "block_number", + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} + +WITH liquidation AS( + + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + regexp_substr_all(SUBSTR(DATA, 3, len(DATA)), '.{64}') AS segmented_data, + CONCAT('0x', SUBSTR(topics [1] :: STRING, 27, 40)) AS collateral_asset, + CONCAT('0x', SUBSTR(topics [2] :: STRING, 27, 40)) AS debt_asset, + CONCAT('0x', SUBSTR(topics [3] :: STRING, 27, 40)) AS borrower_address, + utils.udf_hex_to_int( + segmented_data [0] :: STRING + ) :: INTEGER AS debt_to_cover_amount, + utils.udf_hex_to_int( + segmented_data [1] :: STRING + ) :: INTEGER AS liquidated_amount, + CONCAT('0x', SUBSTR(segmented_data [2] :: STRING, 25, 40)) AS liquidator_address, + CASE + WHEN contract_address = LOWER('0x8FD4aF47E4E63d1D2D45582c3286b4BD9Bb95DfE') THEN 'Granary' + ELSE 'ERROR' + END AS granary_version, + COALESCE( + origin_to_address, + contract_address + ) AS lending_pool_contract, + _inserted_timestamp, + _log_id + FROM + {{ ref('silver__logs') }} + WHERE + topics [0] :: STRING = '0xe413a321e8681d831f4dbccbca790d2952b56f977908e45be37335533e005286' + +{% if is_incremental() %} +AND _inserted_timestamp >= ( + SELECT + MAX( + _inserted_timestamp + ) - INTERVAL '12 hours' + FROM + {{ this }} +) +{% endif %} +AND contract_address = LOWER('0x8FD4aF47E4E63d1D2D45582c3286b4BD9Bb95DfE') +AND tx_status = 'SUCCESS' --excludes failed txs +), +atoken_meta AS ( + SELECT + atoken_address, + atoken_symbol, + atoken_name, + atoken_decimals, + underlying_address, + underlying_symbol, + underlying_name, + underlying_decimals, + atoken_version, + atoken_created_block, + atoken_stable_debt_address, + atoken_variable_debt_address + FROM + {{ ref('silver__granary_tokens') }} +) +SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + collateral_asset, + amc.atoken_address AS collateral_granary_token, + liquidated_amount AS amount_unadj, + liquidated_amount / pow( + 10, + amc.atoken_decimals + ) AS amount, + debt_asset, + amd.atoken_address AS debt_granary_token, + liquidator_address AS liquidator, + borrower_address AS borrower, + granary_version AS platform, + amc.underlying_symbol AS collateral_token_symbol, + amd.underlying_symbol AS debt_token_symbol, + 'optimism' AS blockchain, + _log_id, + _inserted_timestamp +FROM + liquidation + LEFT JOIN atoken_meta amc + ON liquidation.collateral_asset = amc.underlying_address + LEFT JOIN atoken_meta amd + ON liquidation.debt_asset = amd.underlying_address qualify(ROW_NUMBER() over(PARTITION BY _log_id +ORDER BY + _inserted_timestamp DESC)) = 1 diff --git a/models/silver/defi/lending/granary/silver__granary_liquidations.yml b/models/silver/defi/lending/granary/silver__granary_liquidations.yml new file mode 100644 index 00000000..f5eb6180 --- /dev/null +++ b/models/silver/defi/lending/granary/silver__granary_liquidations.yml @@ -0,0 +1,53 @@ +version: 2 +models: + - name: silver__granary_liquidations + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - TX_HASH + - EVENT_INDEX + columns: + - name: BLOCKCHAIN + tests: + - not_null + - name: BLOCK_NUMBER + tests: + - not_null + - name: BLOCK_TIMESTAMP + tests: + - not_null + - name: EVENT_INDEX + tests: + - not_null + - name: TX_HASH + tests: + - not_null + - name: COLLATERAL_ASSET + tests: + - not_null + - name: COLLATERAL_granary_TOKEN + - name: AMOUNT_UNADJ + tests: + - not_null + - name: AMOUNT + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: DEBT_granary_TOKEN + - name: LIQUIDATOR + tests: + - not_null + - name: BORROWER + tests: + - not_null + - name: PLATFORM + tests: + - not_null + - name: COLLATERAL_TOKEN_SYMBOL + tests: + - not_null + - name: DEBT_TOKEN_SYMBOL + tests: + - not_null diff --git a/models/silver/defi/lending/granary/silver__granary_repayments.sql b/models/silver/defi/lending/granary/silver__granary_repayments.sql new file mode 100644 index 00000000..b3591479 --- /dev/null +++ b/models/silver/defi/lending/granary/silver__granary_repayments.sql @@ -0,0 +1,99 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = "block_number", + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} + +WITH repay AS( + + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + regexp_substr_all(SUBSTR(DATA, 3, len(DATA)), '.{64}') AS segmented_data, + CONCAT('0x', SUBSTR(topics [1] :: STRING, 27, 40)) AS granary_market, + CONCAT('0x', SUBSTR(topics [2] :: STRING, 27, 40)) AS borrower_address, + CONCAT('0x', SUBSTR(topics [3] :: STRING, 27, 40)) AS repayer, + utils.udf_hex_to_int( + segmented_data [0] :: STRING + ) :: INTEGER AS repayed_amount, + CASE + WHEN contract_address = LOWER('0x8FD4aF47E4E63d1D2D45582c3286b4BD9Bb95DfE') THEN 'Granary' + ELSE 'ERROR' + END AS granary_version, + origin_to_address AS lending_pool_contract, + origin_from_address AS repayer_address, + _log_id, + _inserted_timestamp + FROM + {{ ref('silver__logs') }} + WHERE + topics [0] :: STRING = '0x4cdde6e09bb755c9a5589ebaec640bbfedff1362d4b255ebf8339782b9942faa' + +{% if is_incremental() %} +AND _inserted_timestamp >= ( + SELECT + MAX( + _inserted_timestamp + ) - INTERVAL '12 hours' + FROM + {{ this }} +) +{% endif %} +AND contract_address = LOWER('0x8FD4aF47E4E63d1D2D45582c3286b4BD9Bb95DfE') +AND tx_status = 'SUCCESS' --excludes failed txs +), +atoken_meta AS ( + SELECT + atoken_address, + atoken_symbol, + atoken_name, + atoken_decimals, + underlying_address, + underlying_symbol, + underlying_name, + underlying_decimals, + atoken_version, + atoken_created_block, + atoken_stable_debt_address, + atoken_variable_debt_address + FROM + {{ ref('silver__granary_tokens') }} +) +SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + granary_market, + atoken_meta.atoken_address AS granary_token, + repayed_amount AS amount_unadj, + repayed_amount / pow( + 10, + atoken_meta.underlying_decimals + ) AS amount, + repayer_address AS payer, + borrower_address AS borrower, + lending_pool_contract, + granary_version AS platform, + atoken_meta.underlying_symbol AS symbol, + 'optimism' AS blockchain, + _log_id, + _inserted_timestamp +FROM + repay + LEFT JOIN atoken_meta + ON repay.granary_market = atoken_meta.underlying_address qualify(ROW_NUMBER() over(PARTITION BY _log_id +ORDER BY + _inserted_timestamp DESC)) = 1 diff --git a/models/silver/defi/lending/granary/silver__granary_repayments.yml b/models/silver/defi/lending/granary/silver__granary_repayments.yml new file mode 100644 index 00000000..451b29a2 --- /dev/null +++ b/models/silver/defi/lending/granary/silver__granary_repayments.yml @@ -0,0 +1,52 @@ +version: 2 +models: + - name: silver__granary_repayments + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - TX_HASH + - EVENT_INDEX + columns: + - name: BLOCKCHAIN + tests: + - not_null + - name: BLOCK_NUMBER + tests: + - not_null + - name: TX_HASH + tests: + - not_null + - name: BLOCK_TIMESTAMP + tests: + - not_null + - name: EVENT_INDEX + tests: + - not_null + - name: granary_MARKET + tests: + - not_null + - name: granary_TOKEN + - name: AMOUNT_UNADJ + tests: + - not_null + - name: AMOUNT + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: PAYER + tests: + - not_null + - name: BORROWER + tests: + - not_null + - name: LENDING_POOL_CONTRACT + tests: + - not_null + - name: PLATFORM + tests: + - not_null + - name: SYMBOL + tests: + - not_null diff --git a/models/silver/defi/lending/granary/silver__granary_tokens.sql b/models/silver/defi/lending/granary/silver__granary_tokens.sql new file mode 100644 index 00000000..f9c7df93 --- /dev/null +++ b/models/silver/defi/lending/granary/silver__granary_tokens.sql @@ -0,0 +1,123 @@ +{{ config( + materialized = 'incremental', + tags = ['curated'] +) }} + +WITH DECODE AS ( + + SELECT + block_number AS atoken_created_block, + contract_address AS a_token_address, + regexp_substr_all(SUBSTR(DATA, 3, len(DATA)), '.{64}') AS segmented_data, + CONCAT('0x', SUBSTR(topics [1] :: STRING, 27, 40)) AS underlying_asset, + CONCAT('0x', SUBSTR(topics [2] :: STRING, 27, 40)) AS granary_version_pool, + utils.udf_hex_to_int( + SUBSTR( + segmented_data [2] :: STRING, + 27, + 40 + ) + ) :: INTEGER AS atoken_decimals, + utils.udf_hex_to_string ( + segmented_data [7] :: STRING + ) :: STRING AS atoken_name, + utils.udf_hex_to_string ( + segmented_data [9] :: STRING + ) :: STRING AS atoken_symbol, + l._inserted_timestamp, + l._log_id + FROM + {{ ref('silver__logs') }} + l + WHERE + topics [0] = '0xb19e051f8af41150ccccb3fc2c2d8d15f4a4cf434f32a559ba75fe73d6eea20b' + +{% if is_incremental() %} +AND _inserted_timestamp >= ( + SELECT + MAX( + _inserted_timestamp + ) - INTERVAL '12 hours' + FROM + {{ this }} +) +{% endif %} +), +a_token_step_1 AS ( + SELECT + atoken_created_block, + a_token_address, + segmented_data, + underlying_asset, + granary_version_pool, + atoken_decimals, + atoken_name, + atoken_symbol, + _inserted_timestamp, + _log_id + FROM + DECODE + WHERE + atoken_name LIKE '%Granary%' +), +debt_tokens AS ( + SELECT + block_number AS atoken_created_block, + contract_address AS a_token_address, + regexp_substr_all(SUBSTR(DATA, 3, len(DATA)), '.{64}') AS segmented_data, + CONCAT('0x', SUBSTR(topics [1] :: STRING, 27, 40)) AS underlying_asset, + CONCAT('0x', SUBSTR(topics [2] :: STRING, 27, 40)) AS atoken_address, + CONCAT('0x', SUBSTR(segmented_data [0] :: STRING, 27, 40)) :: STRING AS atoken_stable_debt_address, + CONCAT('0x', SUBSTR(segmented_data [1] :: STRING, 27, 40)) :: STRING AS atoken_variable_debt_address, + _inserted_timestamp, + _log_id + FROM + {{ ref('silver__logs') }} + WHERE + topics [0] = '0x3a0ca721fc364424566385a1aa271ed508cc2c0949c2272575fb3013a163a45f' + AND CONCAT('0x', SUBSTR(topics [2] :: STRING, 27, 40)) IN ( + SELECT + a_token_address + FROM + a_token_step_1 + ) +), +a_token_step_2 AS ( + SELECT + atoken_created_block, + a_token_address, + segmented_data, + underlying_asset, + granary_version_pool, + atoken_decimals, + atoken_name, + atoken_symbol, + _inserted_timestamp, + _log_id, + 'Granary' AS protocol + FROM + a_token_step_1 + WHERE + granary_version_pool = LOWER('0x8FD4aF47E4E63d1D2D45582c3286b4BD9Bb95DfE') +) +SELECT + A.atoken_created_block, + A.atoken_symbol AS atoken_symbol, + A.a_token_address AS atoken_address, + b.atoken_stable_debt_address, + b.atoken_variable_debt_address, + A.atoken_decimals AS atoken_decimals, + A.protocol AS atoken_version, + atoken_name AS atoken_name, + C.token_symbol AS underlying_symbol, + A.underlying_asset AS underlying_address, + C.token_decimals AS underlying_decimals, + C.token_name AS underlying_name, + A._inserted_timestamp, + A._log_id +FROM + a_token_step_2 A + INNER JOIN debt_tokens b + ON A.a_token_address = b.atoken_address + INNER JOIN {{ ref('silver__contracts') }} C + ON contract_address = A.underlying_asset diff --git a/models/silver/defi/lending/granary/silver__granary_withdraws.sql b/models/silver/defi/lending/granary/silver__granary_withdraws.sql new file mode 100644 index 00000000..896e7180 --- /dev/null +++ b/models/silver/defi/lending/granary/silver__granary_withdraws.sql @@ -0,0 +1,96 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = "block_number", + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} + +WITH withdraw AS( + + SELECT + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + regexp_substr_all(SUBSTR(DATA, 3, len(DATA)), '.{64}') AS segmented_data, + CONCAT('0x', SUBSTR(topics [1] :: STRING, 27, 40)) AS granary_market, + CONCAT('0x', SUBSTR(topics [2] :: STRING, 27, 40)) AS useraddress, + CONCAT('0x', SUBSTR(topics [3] :: STRING, 27, 40)) AS depositor, + utils.udf_hex_to_int( + segmented_data [0] :: STRING + ) :: INTEGER AS withdraw_amount, + tx_hash, + CASE + WHEN contract_address = LOWER('0x8FD4aF47E4E63d1D2D45582c3286b4BD9Bb95DfE') THEN 'Granary' + ELSE 'ERROR' + END AS granary_version, + origin_to_address AS lending_pool_contract, + _inserted_timestamp, + _log_id + FROM + {{ ref('silver__logs') }} + WHERE + topics [0] :: STRING = '0x3115d1449a7b732c986cba18244e897a450f61e1bb8d589cd2e69e6c8924f9f7' + +{% if is_incremental() %} +AND _inserted_timestamp >= ( + SELECT + MAX( + _inserted_timestamp + ) - INTERVAL '12 hours' + FROM + {{ this }} +) +{% endif %} +AND contract_address = LOWER('0x8FD4aF47E4E63d1D2D45582c3286b4BD9Bb95DfE') +AND tx_status = 'SUCCESS' --excludes failed txs +), +atoken_meta AS ( + SELECT + atoken_address, + atoken_symbol, + atoken_name, + atoken_decimals, + underlying_address, + underlying_symbol, + underlying_name, + underlying_decimals, + atoken_version, + atoken_created_block, + atoken_stable_debt_address, + atoken_variable_debt_address + FROM + {{ ref('silver__granary_tokens') }} +) +SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + granary_market, + atoken_meta.atoken_address AS granary_token, + withdraw_amount AS amount_unadj, + withdraw_amount / pow( + 10, + atoken_meta.underlying_decimals + ) AS amount, + depositor depositor_address, + granary_version AS platform, + atoken_meta.underlying_symbol AS symbol, + 'optimism' AS blockchain, + _log_id, + _inserted_timestamp +FROM + withdraw + LEFT JOIN atoken_meta + ON withdraw.granary_market = atoken_meta.underlying_address qualify(ROW_NUMBER() over(PARTITION BY _log_id +ORDER BY + _inserted_timestamp DESC)) = 1 diff --git a/models/silver/defi/lending/granary/silver__granary_withdraws.yml b/models/silver/defi/lending/granary/silver__granary_withdraws.yml new file mode 100644 index 00000000..92255e4d --- /dev/null +++ b/models/silver/defi/lending/granary/silver__granary_withdraws.yml @@ -0,0 +1,46 @@ +version: 2 +models: + - name: silver__granary_withdraws + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - TX_HASH + - EVENT_INDEX + columns: + - name: BLOCKCHAIN + tests: + - not_null + - name: TX_HASH + tests: + - not_null + - name: BLOCK_NUMBER + tests: + - not_null + - name: BLOCK_TIMESTAMP + tests: + - not_null + - name: EVENT_INDEX + tests: + - not_null + - name: granary_MARKET + tests: + - not_null + - name: granary_TOKEN + - name: AMOUNT_UNADJ + tests: + - not_null + - name: AMOUNT + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: DEPOSITOR_ADDRESS + tests: + - not_null + - name: PLATFORM + tests: + - not_null + - name: SYMBOL + tests: + - not_null diff --git a/models/silver/defi/lending/sonne/silver__sonne_asset_details.sql b/models/silver/defi/lending/sonne/silver__sonne_asset_details.sql new file mode 100644 index 00000000..9be32e7b --- /dev/null +++ b/models/silver/defi/lending/sonne/silver__sonne_asset_details.sql @@ -0,0 +1,97 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = "block_number", + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} + +WITH log_pull AS ( + + SELECT + tx_hash, + block_number, + block_timestamp, + contract_address, + _inserted_timestamp, + _log_id + FROM + {{ ref('silver__logs') }} + WHERE + topics [0] :: STRING = '0x7ac369dbd14fa5ea3f473ed67cc9d598964a77501540ba6751eb0b3decf5870d' + AND origin_from_address = LOWER('0xFb59Ce8986943163F14C590755b29dB2998F2322') + +{% if is_incremental() %} +AND _inserted_timestamp >= ( + SELECT + MAX( + _inserted_timestamp + ) - INTERVAL '12 hours' + FROM + {{ this }} +) +{% endif %} +), +traces_pull AS ( + SELECT + from_address AS token_address, + to_address AS underlying_asset + FROM + {{ ref('silver__traces') }} + WHERE + tx_hash IN ( + SELECT + tx_hash + FROM + log_pull + ) + AND identifier = 'STATICCALL_2' +), +contracts AS ( + SELECT + * + FROM + {{ ref('silver__contracts') }} +), +contract_pull AS ( + SELECT + l.tx_hash, + l.block_number, + l.block_timestamp, + l.contract_address, + C.token_name, + C.token_symbol, + C.token_decimals, + t.underlying_asset, + l._inserted_timestamp, + l._log_id + FROM + log_pull l + LEFT JOIN traces_pull t + ON l.contract_address = t.token_address + LEFT JOIN contracts C + ON C.contract_address = l.contract_address qualify(ROW_NUMBER() over(PARTITION BY l.contract_address + ORDER BY + block_timestamp ASC)) = 1 +) +SELECT + l.tx_hash, + l.block_number, + l.block_timestamp, + l.contract_address AS token_address, + l.token_name, + l.token_symbol, + l.token_decimals, + l.underlying_asset AS underlying_asset_address, + C.token_name AS underlying_name, + C.token_symbol AS underlying_symbol, + C.token_decimals AS underlying_decimals, + l._inserted_timestamp, + l._log_id +FROM + contract_pull l + LEFT JOIN contracts C + ON C.contract_address = l.underlying_asset +WHERE + underlying_asset IS NOT NULL + AND l.token_name IS NOT NULL diff --git a/models/silver/defi/lending/sonne/silver__sonne_borrows.sql b/models/silver/defi/lending/sonne/silver__sonne_borrows.sql new file mode 100644 index 00000000..50c993a7 --- /dev/null +++ b/models/silver/defi/lending/sonne/silver__sonne_borrows.sql @@ -0,0 +1,120 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = "block_number", + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} +-- pull all token addresses and corresponding name +WITH asset_details AS ( + + SELECT + token_address, + token_symbol, + token_name, + token_decimals, + underlying_asset_address, + underlying_name, + underlying_symbol, + underlying_decimals + FROM + {{ ref('silver__sonne_asset_details') }} +), +sonne_borrows AS ( + SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + regexp_substr_all(SUBSTR(DATA, 3, len(DATA)), '.{64}') AS segmented_data, + CONCAT('0x', SUBSTR(segmented_data [0] :: STRING, 25, 40)) AS borrower, + utils.udf_hex_to_int( + segmented_data [1] :: STRING + ) :: INTEGER AS loan_amount_raw, + utils.udf_hex_to_int( + segmented_data [2] :: STRING + ) :: INTEGER AS accountBorrows, + utils.udf_hex_to_int( + segmented_data [3] :: STRING + ) :: INTEGER AS totalBorrows, + contract_address AS token, + 'Sonne' AS platform, + _inserted_timestamp, + _log_id + FROM + {{ ref('silver__logs') }} + WHERE + contract_address IN ( + SELECT + token_address + FROM + asset_details + ) + AND topics [0] :: STRING = '0x13ed6866d4e1ee6da46f845c46d7e54120883d75c5ea9a2dacc1c4ca8984ab80' + +{% if is_incremental() %} +AND _inserted_timestamp >= ( + SELECT + MAX( + _inserted_timestamp + ) - INTERVAL '12 hours' + FROM + {{ this }} +) +{% endif %} +), +sonne_combine AS ( + SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + borrower, + loan_amount_raw, + C.underlying_asset_address AS borrows_contract_address, + C.underlying_symbol AS borrows_contract_symbol, + token, + C.token_symbol, + C.underlying_decimals, + b.platform, + b._log_id, + b._inserted_timestamp + FROM + sonne_borrows b + LEFT JOIN asset_details C + ON b.token = C.token_address +) +SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + borrower, + borrows_contract_address, + borrows_contract_symbol, + token as token_address, + token_symbol, + loan_amount_raw AS amount_unadj, + loan_amount_raw / pow( + 10, + underlying_decimals + ) AS amount, + platform, + _inserted_timestamp, + _log_id +FROM + sonne_combine qualify(ROW_NUMBER() over(PARTITION BY _log_id +ORDER BY + _inserted_timestamp DESC)) = 1 diff --git a/models/silver/defi/lending/sonne/silver__sonne_borrows.yml b/models/silver/defi/lending/sonne/silver__sonne_borrows.yml new file mode 100644 index 00000000..56fb295c --- /dev/null +++ b/models/silver/defi/lending/sonne/silver__sonne_borrows.yml @@ -0,0 +1,42 @@ +version: 2 +models: + - name: silver__sonne_borrows + + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - _log_id + columns: + - name: BLOCK_NUMBER + tests: + - not_null + - name: BLOCK_TIMESTAMP + tests: + - not_null + - name: BORROWER + tests: + - not_null + - name: BORROWS_CONTRACT_ADDRESS + tests: + - not_null + - name: BORROWS_CONTRACT_SYMBOL + tests: + - not_null + - name: TOKEN_ADDRESS + tests: + - not_null + - name: TOKEN_SYMBOL + tests: + - not_null + - name: AMOUNT + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: TX_HASH + tests: + - not_null + - name: EVENT_INDEX + tests: + - not_null \ No newline at end of file diff --git a/models/silver/defi/lending/sonne/silver__sonne_deposits.sql b/models/silver/defi/lending/sonne/silver__sonne_deposits.sql new file mode 100644 index 00000000..6c61225e --- /dev/null +++ b/models/silver/defi/lending/sonne/silver__sonne_deposits.sql @@ -0,0 +1,123 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = "block_number", + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} +-- pull all token addresses and corresponding name +WITH asset_details AS ( + + SELECT + token_address, + token_symbol, + token_name, + token_decimals, + underlying_asset_address, + underlying_name, + underlying_symbol, + underlying_decimals + FROM + {{ ref('silver__sonne_asset_details') }} +), +sonne_deposits AS ( + SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + contract_address AS token_address, + regexp_substr_all(SUBSTR(DATA, 3, len(DATA)), '.{64}') AS segmented_data, + utils.udf_hex_to_int( + segmented_data [3] :: STRING + ) :: INTEGER AS minttokens_raw, + utils.udf_hex_to_int( + segmented_data [2] :: STRING + ) :: INTEGER AS mintAmount_raw, + CONCAT('0x', SUBSTR(segmented_data [0] :: STRING, 25, 40)) AS supplier, + 'Sonne' AS platform, + _inserted_timestamp, + _log_id + FROM + {{ ref('silver__logs') }} + WHERE + contract_address IN ( + SELECT + token_address + FROM + asset_details + ) + AND topics [0] :: STRING = '0x4c209b5fc8ad50758f13e2e1088ba56a560dff690a1c6fef26394f4c03821c4f' + +{% if is_incremental() %} +AND _inserted_timestamp >= ( + SELECT + MAX( + _inserted_timestamp + ) - INTERVAL '12 hours' + FROM + {{ this }} +) +{% endif %} +), +sonne_combine AS ( + SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + supplier, + minttokens_raw, + mintAmount_raw, + C.underlying_asset_address AS supplied_contract_addr, + C.underlying_symbol AS supplied_symbol, + C.token_address, + C.token_symbol, + C.token_decimals, + C.underlying_decimals, + b.platform, + b._log_id, + b._inserted_timestamp + FROM + sonne_deposits b + LEFT JOIN asset_details C + ON b.token_address = C.token_address +) +SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + token_address, + token_symbol, + minttokens_raw / pow( + 10, + token_decimals + ) AS issued_tokens, + mintAmount_raw AS amount_unadj, + mintAmount_raw / pow( + 10, + underlying_decimals + ) AS amount, + supplied_contract_addr, + supplied_symbol, + supplier, + platform, + _inserted_timestamp, + _log_id +FROM + sonne_combine qualify(ROW_NUMBER() over(PARTITION BY _log_id +ORDER BY + _inserted_timestamp DESC)) = 1 diff --git a/models/silver/defi/lending/sonne/silver__sonne_deposits.yml b/models/silver/defi/lending/sonne/silver__sonne_deposits.yml new file mode 100644 index 00000000..1cca5511 --- /dev/null +++ b/models/silver/defi/lending/sonne/silver__sonne_deposits.yml @@ -0,0 +1,50 @@ +version: 2 +models: + - name: silver__sonne_deposits + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - _log_id + columns: + - name: BLOCK_NUMBER + tests: + - not_null + - name: BLOCK_TIMESTAMP + tests: + - not_null + - dbt_expectations.expect_row_values_to_have_recent_data: + datepart: day + interval: 3 + - name: TOKEN_ADDRESS + tests: + - not_null + - name: TOKEN_SYMBOL + tests: + - not_null + - name: ISSUED_TOKENS + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: AMOUNT + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: SUPPLIED_CONTRACT_ADDR + tests: + - not_null + - name: SUPPLIED_SYMBOL + tests: + - not_null + - name: SUPPLIER + tests: + - not_null + - name: TX_HASH + tests: + - not_null + - name: EVENT_INDEX + tests: + - not_null \ No newline at end of file diff --git a/models/silver/defi/lending/sonne/silver__sonne_liquidations.sql b/models/silver/defi/lending/sonne/silver__sonne_liquidations.sql new file mode 100644 index 00000000..32f5b730 --- /dev/null +++ b/models/silver/defi/lending/sonne/silver__sonne_liquidations.sql @@ -0,0 +1,114 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = "block_number", + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} + +WITH asset_details AS ( + + SELECT + token_address, + token_symbol, + token_name, + token_decimals, + underlying_asset_address, + underlying_name, + underlying_symbol, + underlying_decimals + FROM + {{ ref('silver__sonne_asset_details') }} +), +sonne_liquidations AS ( + SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + regexp_substr_all(SUBSTR(DATA, 3, len(DATA)), '.{64}') AS segmented_data, + CONCAT('0x', SUBSTR(segmented_data [1] :: STRING, 25, 40)) AS borrower, + contract_address AS token, + CONCAT('0x', SUBSTR(segmented_data [0] :: STRING, 25, 40)) AS liquidator, + utils.udf_hex_to_int( + segmented_data [4] :: STRING + ) :: INTEGER AS seizeTokens_raw, + utils.udf_hex_to_int( + segmented_data [2] :: STRING + ) :: INTEGER AS repayAmount_raw, + CONCAT('0x', SUBSTR(segmented_data [3] :: STRING, 25, 40)) AS tokenCollateral, + 'Sonne' AS platform, + _inserted_timestamp, + _log_id + FROM + {{ ref('silver__logs') }} + WHERE + contract_address IN ( + SELECT + token_address + FROM + asset_details + ) + AND topics [0] :: STRING = '0x298637f684da70674f26509b10f07ec2fbc77a335ab1e7d6215a4b2484d8bb52' + +{% if is_incremental() %} +AND _inserted_timestamp >= ( + SELECT + MAX( + _inserted_timestamp + ) - INTERVAL '12 hours' + FROM + {{ this }} +) +{% endif %} +), +liquidation_union AS ( + SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + borrower, + token, + asd1.token_symbol AS token_symbol, + liquidator, + seizeTokens_raw / pow( + 10, + asd2.token_decimals + ) AS tokens_seized, + tokenCollateral AS protocol_market, + asd2.token_symbol AS collateral_token_symbol, + asd2.underlying_asset_address AS collateral_token, + asd2.underlying_symbol AS collateral_symbol, + repayAmount_raw AS amount_unadj, + repayAmount_raw / pow( + 10, + asd1.underlying_decimals + ) AS amount, + asd1.underlying_decimals, + asd1.underlying_asset_address AS liquidation_contract_address, + asd1.underlying_symbol AS liquidation_contract_symbol, + l.platform, + l._inserted_timestamp, + l._log_id + FROM + sonne_liquidations l + LEFT JOIN asset_details asd1 + ON l.token = asd1.token_address + LEFT JOIN asset_details asd2 + ON l.tokenCollateral = asd2.token_address +) +SELECT + * +FROM + liquidation_union qualify(ROW_NUMBER() over(PARTITION BY _log_id +ORDER BY + _inserted_timestamp DESC)) = 1 diff --git a/models/silver/defi/lending/sonne/silver__sonne_liquidations.yml b/models/silver/defi/lending/sonne/silver__sonne_liquidations.yml new file mode 100644 index 00000000..03767666 --- /dev/null +++ b/models/silver/defi/lending/sonne/silver__sonne_liquidations.yml @@ -0,0 +1,56 @@ +version: 2 +models: + - name: silver__sonne_liquidations + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - _log_id + columns: + - name: BLOCK_NUMBER + tests: + - not_null + - name: BLOCK_TIMESTAMP + tests: + - not_null + - name: BORROWER + tests: + - not_null + - name: TOKEN + tests: + - not_null + - name: TOKEN_SYMBOL + tests: + - not_null + - name: LIQUIDATOR + tests: + - not_null + - name: TOKENS_SEIZED + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: AMOUNT + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: LIQUIDATION_CONTRACT_ADDRESS + tests: + - not_null + - name: LIQUIDATION_CONTRACT_SYMBOL + tests: + - not_null + - name: TX_HASH + tests: + - not_null + - name: COLLATERAL_TOKEN + tests: + - not_null + - name: COLLATERAL_SYMBOL + tests: + - not_null + - name: EVENT_INDEX + tests: + - not_null \ No newline at end of file diff --git a/models/silver/defi/lending/sonne/silver__sonne_repayments.sql b/models/silver/defi/lending/sonne/silver__sonne_repayments.sql new file mode 100644 index 00000000..4b7f3247 --- /dev/null +++ b/models/silver/defi/lending/sonne/silver__sonne_repayments.sql @@ -0,0 +1,117 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = "block_number", + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} +-- pull all token addresses and corresponding name +WITH asset_details AS ( + + SELECT + token_address, + token_symbol, + token_name, + token_decimals, + underlying_asset_address, + underlying_name, + underlying_symbol, + underlying_decimals + FROM + {{ ref('silver__sonne_asset_details') }} +), +sonne_repayments AS ( + SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + regexp_substr_all(SUBSTR(DATA, 3, len(DATA)), '.{64}') AS segmented_data, + CONCAT('0x', SUBSTR(segmented_data [1] :: STRING, 25, 40)) AS borrower, + contract_address AS token, + CONCAT('0x', SUBSTR(segmented_data [0] :: STRING, 25, 40)) AS payer, + utils.udf_hex_to_int( + segmented_data [2] :: STRING + ) :: INTEGER AS repayed_amount_raw, + 'Sonne' AS platform, + _inserted_timestamp, + _log_id + FROM + {{ ref('silver__logs') }} + WHERE + contract_address IN ( + SELECT + token_address + FROM + asset_details + ) + AND topics [0] :: STRING = '0x1a2a22cb034d26d1854bdc6666a5b91fe25efbbb5dcad3b0355478d6f5c362a1' + +{% if is_incremental() %} +AND _inserted_timestamp >= ( + SELECT + MAX( + _inserted_timestamp + ) - INTERVAL '12 hours' + FROM + {{ this }} +) +{% endif %} +), +sonne_combine AS ( + SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + borrower, + token, + C.token_symbol, + payer, + repayed_amount_raw, + C.underlying_asset_address AS repay_contract_address, + C.underlying_symbol AS repay_contract_symbol, + C.underlying_decimals, + b.platform, + b._log_id, + b._inserted_timestamp + FROM + sonne_repayments b + LEFT JOIN asset_details C + ON b.token = C.token_address +) +SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + borrower, + token as token_address, + token_symbol, + payer, + repay_contract_address, + repay_contract_symbol, + repayed_amount_raw AS amount_unadj, + repayed_amount_raw / pow( + 10, + underlying_decimals + ) AS amount, + platform, + _inserted_timestamp, + _log_id +FROM + sonne_combine qualify(ROW_NUMBER() over(PARTITION BY _log_id +ORDER BY + _inserted_timestamp DESC)) = 1 diff --git a/models/silver/defi/lending/sonne/silver__sonne_repayments.yml b/models/silver/defi/lending/sonne/silver__sonne_repayments.yml new file mode 100644 index 00000000..1c1420fb --- /dev/null +++ b/models/silver/defi/lending/sonne/silver__sonne_repayments.yml @@ -0,0 +1,44 @@ +version: 2 +models: + - name: silver__sonne_repayments + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - _log_id + columns: + - name: BLOCK_NUMBER + tests: + - not_null + - name: BLOCK_TIMESTAMP + tests: + - not_null + - name: BORROWER + tests: + - not_null + - name: TOKEN_ADDRESS + tests: + - not_null + - name: TOKEN_SYMBOL + tests: + - not_null + - name: PAYER + tests: + - not_null + - name: REPAY_CONTRACT_ADDRESS + tests: + - not_null + - name: REPAY_CONTRACT_SYMBOL + tests: + - not_null + - name: AMOUNT + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: TX_HASH + tests: + - not_null + - name: EVENT_INDEX + tests: + - not_null \ No newline at end of file diff --git a/models/silver/defi/lending/sonne/silver__sonne_withdraws.sql b/models/silver/defi/lending/sonne/silver__sonne_withdraws.sql new file mode 100644 index 00000000..4adb221f --- /dev/null +++ b/models/silver/defi/lending/sonne/silver__sonne_withdraws.sql @@ -0,0 +1,123 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = "block_number", + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} +-- pull all token addresses and corresponding name +WITH asset_details AS ( + + SELECT + token_address, + token_symbol, + token_name, + token_decimals, + underlying_asset_address, + underlying_name, + underlying_symbol, + underlying_decimals + FROM + {{ ref('silver__sonne_asset_details') }} +), +sonne_redemptions AS ( + SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + contract_address AS token, + regexp_substr_all(SUBSTR(DATA, 3, len(DATA)), '.{64}') AS segmented_data, + utils.udf_hex_to_int( + segmented_data [1] :: STRING + ) :: INTEGER AS received_amount_raw, + utils.udf_hex_to_int( + segmented_data [3] :: STRING + ) :: INTEGER AS redeemed_token_raw, + CONCAT('0x', SUBSTR(segmented_data [0] :: STRING, 25, 40)) AS redeemer, + 'Sonne' AS platform, + _inserted_timestamp, + _log_id + FROM + {{ ref('silver__logs') }} + WHERE + contract_address IN ( + SELECT + token_address + FROM + asset_details + ) + AND topics [0] :: STRING = '0xe5b754fb1abb7f01b499791d0b820ae3b6af3424ac1c59768edb53f4ec31a929' + +{% if is_incremental() %} +AND _inserted_timestamp >= ( + SELECT + MAX( + _inserted_timestamp + ) - INTERVAL '12 hours' + FROM + {{ this }} +) +{% endif %} +), +sonne_combine AS ( + SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + token, + redeemer, + received_amount_raw, + redeemed_token_raw, + C.underlying_asset_address AS received_contract_address, + C.underlying_symbol AS received_contract_symbol, + C.token_symbol, + C.token_decimals, + C.underlying_decimals, + b.platform, + b._log_id, + b._inserted_timestamp + FROM + sonne_redemptions b + LEFT JOIN asset_details C + ON b.token = C.token_address +) +SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + token as token_address, + token_symbol, + received_amount_raw AS amount_unadj, + received_amount_raw / pow( + 10, + underlying_decimals + ) AS amount, + received_contract_address, + received_contract_symbol, + redeemed_token_raw / pow( + 10, + token_decimals + ) AS redeemed_token, + redeemer, + platform, + _inserted_timestamp, + _log_id +FROM + sonne_combine ee qualify(ROW_NUMBER() over(PARTITION BY _log_id +ORDER BY + _inserted_timestamp DESC)) = 1 diff --git a/models/silver/defi/lending/sonne/silver__sonne_withdraws.yml b/models/silver/defi/lending/sonne/silver__sonne_withdraws.yml new file mode 100644 index 00000000..77483df4 --- /dev/null +++ b/models/silver/defi/lending/sonne/silver__sonne_withdraws.yml @@ -0,0 +1,47 @@ +version: 2 +models: + - name: silver__sonne_withdraws + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - _log_id + columns: + - name: BLOCK_NUMBER + tests: + - not_null + - name: BLOCK_TIMESTAMP + tests: + - not_null + - name: TOKEN_ADDRESS + tests: + - not_null + - name: TOKEN_SYMBOL + tests: + - not_null + - name: AMOUNT + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: RECEIVED_CONTRACT_ADDRESS + tests: + - not_null + - name: RECEIVED_CONTRACT_SYMBOL + tests: + - not_null + - name: REDEEMED_TOKEN + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: REDEEMER + tests: + - not_null + - name: TX_HASH + tests: + - not_null + - name: EVENT_INDEX + tests: + - not_null \ No newline at end of file diff --git a/models/silver/defi/lending/tarot/silver__tarot_borrows.sql b/models/silver/defi/lending/tarot/silver__tarot_borrows.sql new file mode 100644 index 00000000..14c5b79d --- /dev/null +++ b/models/silver/defi/lending/tarot/silver__tarot_borrows.sql @@ -0,0 +1,101 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = "block_number", + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} + +WITH asset_details AS ( + + SELECT + borrowable1 AS token_address, + token0 AS underlying_asset_address, + token0_name AS underlying_asset_name, + token0_symbol AS underlying_asset_symbol, + token0_decimals AS underlying_decimals + FROM + {{ ref('silver__tarot_liquidity_pools') }} + GROUP BY + ALL + UNION ALL + SELECT + borrowable2 AS token_address, + token1 AS underlying_asset_address, + token1_name AS underlying_asset_name, + token1_symbol AS underlying_asset_symbol, + token1_decimals AS underlying_decimals + FROM + {{ ref('silver__tarot_liquidity_pools') }} + GROUP BY + ALL +), +log_pull AS ( + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + regexp_substr_all(SUBSTR(DATA, 3, len(DATA)), '.{64}') AS segmented_data, + CONCAT('0x', SUBSTR(topics [2] :: STRING, 25, 40)) AS borrower, + utils.udf_hex_to_int( + segmented_data [0] :: STRING + ) :: INTEGER AS loan_amount_raw, + _inserted_timestamp, + _log_id + FROM + {{ ref('silver__logs') }} + l + WHERE + contract_address IN ( + SELECT + token_address + FROM + asset_details + ) + AND topics [0] :: STRING = '0x33f3048bd4e6af45e53afb722adfd57dbde82da7e93e44db921fb4b8c6a70c4b' + AND loan_amount_raw > 0 --borrow and repay in same log event, value in segmented data determines what kind of event + +{% if is_incremental() %} +AND _inserted_timestamp >= ( + SELECT + MAX( + _inserted_timestamp + ) - INTERVAL '12 hours' + FROM + {{ this }} +) +{% endif %} +) +SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + borrower, + underlying_asset_address AS borrows_contract_address, + underlying_asset_symbol AS borrows_contract_symbol, + a.token_address AS token_address, + 'bTAROT' AS token_symbol, + loan_amount_raw AS amount_unadj, + loan_amount_raw / pow( + 10, + underlying_decimals + ) AS amount, + 'Tarot' AS platform, + _inserted_timestamp, + _log_id +FROM + log_pull l + LEFT JOIN asset_details A + ON A.token_address = l.contract_address qualify(ROW_NUMBER() over(PARTITION BY _log_id +ORDER BY + _inserted_timestamp DESC)) = 1 diff --git a/models/silver/defi/lending/tarot/silver__tarot_borrows.yml b/models/silver/defi/lending/tarot/silver__tarot_borrows.yml new file mode 100644 index 00000000..4d24c579 --- /dev/null +++ b/models/silver/defi/lending/tarot/silver__tarot_borrows.yml @@ -0,0 +1,42 @@ +version: 2 +models: + - name: silver__tarot_borrows + + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - _log_id + columns: + - name: BLOCK_NUMBER + tests: + - not_null + - name: BLOCK_TIMESTAMP + tests: + - not_null + - name: BORROWER + tests: + - not_null + - name: BORROWS_CONTRACT_ADDRESS + tests: + - not_null + - name: BORROWS_CONTRACT_SYMBOL + tests: + - not_null + - name: TOKEN_ADDRESS + tests: + - not_null + - name: TOKEN_SYMBOL + tests: + - not_null + - name: AMOUNT + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: TX_HASH + tests: + - not_null + - name: EVENT_INDEX + tests: + - not_null \ No newline at end of file diff --git a/models/silver/defi/lending/tarot/silver__tarot_deposits.sql b/models/silver/defi/lending/tarot/silver__tarot_deposits.sql new file mode 100644 index 00000000..c089db2c --- /dev/null +++ b/models/silver/defi/lending/tarot/silver__tarot_deposits.sql @@ -0,0 +1,99 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = "block_number", + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} + +WITH asset_details AS ( + + SELECT + borrowable1 AS token_address, + token0 AS underlying_asset_address, + token0_symbol AS underlying_asset_symbol, + token0_decimals AS underlying_decimals + FROM + {{ ref('silver__tarot_liquidity_pools') }} + GROUP BY + ALL + UNION ALL + SELECT + borrowable2 AS token_address, + token1 AS underlying_asset_address, + token1_symbol AS underlying_asset_symbol, + token1_decimals AS underlying_decimals + FROM + {{ ref('silver__tarot_liquidity_pools') }} + GROUP BY + ALL +), +log_pull AS ( + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + regexp_substr_all(SUBSTR(DATA, 3, len(DATA)), '.{64}') AS segmented_data, + CONCAT('0x', SUBSTR(topics [1] :: STRING, 25, 40)) AS sender, + CONCAT('0x', SUBSTR(topics [2] :: STRING, 25, 40)) AS supplier, + utils.udf_hex_to_int( + segmented_data [0] :: STRING + ) :: INTEGER AS mintAmount_raw, + _inserted_timestamp, + _log_id + FROM + {{ ref('silver__logs') }} + l + WHERE + contract_address IN ( + SELECT + token_address + FROM + asset_details + ) + AND topics [0] :: STRING = '0x2f00e3cdd69a77be7ed215ec7b2a36784dd158f921fca79ac29deffa353fe6ee' + +{% if is_incremental() %} +AND _inserted_timestamp >= ( + SELECT + MAX( + _inserted_timestamp + ) - INTERVAL '12 hours' + FROM + {{ this }} +) +{% endif %} +) +SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + A.token_address, + 'bTAROT' AS token_symbol, + mintAmount_raw AS amount_unadj, + mintAmount_raw / pow( + 10, + underlying_decimals + ) AS amount, + underlying_asset_address AS supplied_contract_addr, + underlying_asset_symbol AS supplied_symbol, + supplier, + 'Tarot' AS platform, + _inserted_timestamp, + _log_id +FROM + log_pull l + LEFT JOIN asset_details A + ON A.token_address = l.contract_address qualify(ROW_NUMBER() over(PARTITION BY _log_id +ORDER BY + _inserted_timestamp DESC)) = 1 diff --git a/models/silver/defi/lending/tarot/silver__tarot_deposits.yml b/models/silver/defi/lending/tarot/silver__tarot_deposits.yml new file mode 100644 index 00000000..aa4a3a67 --- /dev/null +++ b/models/silver/defi/lending/tarot/silver__tarot_deposits.yml @@ -0,0 +1,47 @@ +version: 2 +models: + - name: silver__tarot_deposits + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - _log_id + columns: + - name: BLOCK_NUMBER + tests: + - not_null + - name: BLOCK_TIMESTAMP + tests: + - not_null + - name: TOKEN_ADDRESS + tests: + - not_null + - name: TOKEN_SYMBOL + tests: + - not_null + - name: ISSUED_TOKENS + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: AMOUNT + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: SUPPLIED_CONTRACT_ADDR + tests: + - not_null + - name: SUPPLIED_SYMBOL + tests: + - not_null + - name: SUPPLIER + tests: + - not_null + - name: TX_HASH + tests: + - not_null + - name: EVENT_INDEX + tests: + - not_null \ No newline at end of file diff --git a/models/silver/defi/lending/tarot/silver__tarot_liquidations.sql b/models/silver/defi/lending/tarot/silver__tarot_liquidations.sql new file mode 100644 index 00000000..9be5977e --- /dev/null +++ b/models/silver/defi/lending/tarot/silver__tarot_liquidations.sql @@ -0,0 +1,117 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = "block_number", + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} + +with asset_details as ( + + select + borrowable1 as token_address, + token0 as underlying_asset_address, + token0_symbol as underlying_asset_symbol, + token0_decimals as underlying_decimals, + ctarot, + lp_token_address, + lp_token_name, + lp_token_symbol, + lp_token_decimals + from + {{ ref('silver__tarot_liquidity_pools') }} + group by all + UNION ALL + select + borrowable2 as token_address, + token1 as underlying_asset_address, + token1_symbol as underlying_asset_symbol, + token1_decimals as underlying_decimals, + ctarot, + lp_token_address, + lp_token_name, + lp_token_symbol, + lp_token_decimals + from + {{ ref('silver__tarot_liquidity_pools') }} + group by all + +), +log_pull as ( + select + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + regexp_substr_all(SUBSTR(DATA, 3, len(DATA)), '.{64}') AS segmented_data, + CONCAT('0x', SUBSTR(topics[1] :: STRING, 27, 40)) AS sender, + CONCAT('0x', SUBSTR(topics[2] :: STRING, 27, 40)) AS borrower, + CONCAT('0x', SUBSTR(topics[3] :: STRING, 27, 40)) AS liquidator, + utils.udf_hex_to_int( + segmented_data [0] :: STRING + ) :: INTEGER AS seizeTokens_raw, + utils.udf_hex_to_int( + segmented_data [1] :: STRING + ) :: INTEGER AS repayAmount_raw, + _inserted_timestamp, + _log_id + from + {{ ref('silver__logs') }} l + WHERE + contract_address IN (SELECT TOKEN_ADDRESS FROM asset_details) + AND + topics [0] :: STRING = '0xb0dbe18c6ffdf0da655dd690e77211d379205c497be44c64447c3f5f021b5167' +{% if is_incremental() %} +AND _inserted_timestamp >= ( + SELECT + MAX( + _inserted_timestamp + ) - INTERVAL '12 hours' + FROM + {{ this }} +) +{% endif %} +) +select + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + borrower, + a.token_address as token, + 'bTarot' AS token_symbol, + liquidator, + seizeTokens_raw / pow( + 10, + 18 + ) AS tokens_seized,--cTarot amount, which is based on the amount of LP tokens deposited + contract_address as protocol_market, + 'cTarot' AS collateral_token_symbol, + a.lp_token_address AS collateral_token, + a.lp_token_symbol AS collateral_symbol, + repayAmount_raw AS amount_unadj, + repayAmount_raw / pow( + 10, + a.underlying_decimals + ) AS amount, + a.underlying_asset_address as liquidation_contract_address, + a.underlying_asset_symbol as liquidation_contract_symbol, + 'Tarot' as platform, + _inserted_timestamp, + _log_id +FROM + log_pull l +LEFT JOIN + asset_details a +ON + a.token_address = l.contract_address qualify(ROW_NUMBER() over(PARTITION BY _log_id +ORDER BY + _inserted_timestamp DESC)) = 1 \ No newline at end of file diff --git a/models/silver/defi/lending/tarot/silver__tarot_liquidations.yml b/models/silver/defi/lending/tarot/silver__tarot_liquidations.yml new file mode 100644 index 00000000..4bd45101 --- /dev/null +++ b/models/silver/defi/lending/tarot/silver__tarot_liquidations.yml @@ -0,0 +1,56 @@ +version: 2 +models: + - name: silver__tarot_liquidations + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - _log_id + columns: + - name: BLOCK_NUMBER + tests: + - not_null + - name: BLOCK_TIMESTAMP + tests: + - not_null + - name: BORROWER + tests: + - not_null + - name: TOKEN + tests: + - not_null + - name: TOKEN_SYMBOL + tests: + - not_null + - name: LIQUIDATOR + tests: + - not_null + - name: TOKENS_SEIZED + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: AMOUNT + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: LIQUIDATION_CONTRACT_ADDRESS + tests: + - not_null + - name: LIQUIDATION_CONTRACT_SYMBOL + tests: + - not_null + - name: TX_HASH + tests: + - not_null + - name: COLLATERAL_TOKEN + tests: + - not_null + - name: COLLATERAL_SYMBOL + tests: + - not_null + - name: EVENT_INDEX + tests: + - not_null \ No newline at end of file diff --git a/models/silver/defi/lending/tarot/silver__tarot_liquidity_pools.sql b/models/silver/defi/lending/tarot/silver__tarot_liquidity_pools.sql new file mode 100644 index 00000000..b5f18371 --- /dev/null +++ b/models/silver/defi/lending/tarot/silver__tarot_liquidity_pools.sql @@ -0,0 +1,103 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = "block_number", + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} + +WITH factory_pull AS ( + + SELECT + tx_hash, + block_number, + block_timestamp, + regexp_substr_all(SUBSTR(DATA, 3, len(DATA)), '.{64}') AS segmented_data, + CONCAT('0x', SUBSTR(topics [1] :: STRING, 27, 40)) AS vtarot, + CONCAT('0x', SUBSTR(topics [2] :: STRING, 27, 40)) AS token0, + CONCAT('0x', SUBSTR(topics [3] :: STRING, 27, 40)) AS token1, + CONCAT('0x', SUBSTR(segmented_data [0] :: STRING, 25, 40)) AS ctarot, + CONCAT('0x', SUBSTR(segmented_data [1] :: STRING, 25, 40)) AS borrowable1, + CONCAT('0x', SUBSTR(segmented_data [2] :: STRING, 25, 40)) AS borrowable2, + utils.udf_hex_to_int( + segmented_data [3] :: STRING + ) :: INTEGER AS lendingpoolId, + l._inserted_timestamp, + l._log_id + FROM + {{ ref('silver__logs') }} + l + WHERE + contract_address = LOWER('0xD7cABeF2c1fD77a31c5ba97C724B82d3e25fC83C') + AND topics [0] = '0x4c3ab495dc8ebd1b2f3232d7632e54411bc7e4d111475e7fbbd5547d9a28c495' + +{% if is_incremental() %} +AND _inserted_timestamp >= ( + SELECT + MAX( + _inserted_timestamp + ) - INTERVAL '12 hours' + FROM + {{ this }} +) +{% endif %} +), +lp_token_pull AS ( + SELECT + t.tx_hash, + C.* + FROM + {{ ref('silver__traces') }} + t + LEFT JOIN {{ ref('silver__contracts') }} C + ON to_address = contract_address + WHERE + block_timestamp >= ( + SELECT + MIN(block_timestamp) + FROM + factory_pull + ) + AND t.tx_hash IN ( + SELECT + tx_hash + FROM + factory_pull + ) + AND identifier = 'STATICCALL_3_1_0' +) +SELECT + l.tx_hash, + block_number, + block_timestamp, + lp.contract_address AS lp_token_address, + lp.token_name AS lp_token_name, + lp.token_symbol AS lp_token_symbol, + lp.token_decimals AS lp_token_decimals, + vtarot, + token0, + c1.token_name AS token0_name, + c1.token_symbol AS token0_symbol, + c1.token_decimals AS token0_decimals, + token1, + c2.token_name AS token1_name, + c2.token_symbol AS token1_symbol, + c2.token_decimals AS token1_decimals, + ctarot, + borrowable1, + borrowable2, + utils.udf_hex_to_int( + segmented_data [3] :: STRING + ) :: INTEGER AS lendingpoolId, + l._inserted_timestamp, + l._log_id +FROM + factory_pull l + LEFT JOIN {{ ref('silver__contracts') }} + c1 + ON l.token0 = c1.contract_address + LEFT JOIN {{ ref('silver__contracts') }} + c2 + ON l.token1 = c2.contract_address + LEFT JOIN lp_token_pull lp + ON l.tx_hash = lp.tx_hash diff --git a/models/silver/defi/lending/tarot/silver__tarot_repayments.sql b/models/silver/defi/lending/tarot/silver__tarot_repayments.sql new file mode 100644 index 00000000..8b75ce60 --- /dev/null +++ b/models/silver/defi/lending/tarot/silver__tarot_repayments.sql @@ -0,0 +1,103 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = "block_number", + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} + +WITH asset_details AS ( + + SELECT + borrowable1 AS token_address, + token0 AS underlying_asset_address, + token0_name AS underlying_asset_name, + token0_symbol AS underlying_asset_symbol, + token0_decimals AS underlying_decimals + FROM + {{ ref('silver__tarot_liquidity_pools') }} + GROUP BY + ALL + UNION ALL + SELECT + borrowable2 AS token_address, + token1 AS underlying_asset_address, + token1_name AS underlying_asset_name, + token1_symbol AS underlying_asset_symbol, + token1_decimals AS underlying_decimals + FROM + {{ ref('silver__tarot_liquidity_pools') }} + GROUP BY + ALL +), +log_pull AS ( + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + regexp_substr_all(SUBSTR(DATA, 3, len(DATA)), '.{64}') AS segmented_data, + CONCAT('0x', SUBSTR(topics [2] :: STRING, 25, 40)) AS borrower, + CONCAT('0x', SUBSTR(topics [3] :: STRING, 25, 40)) AS payer, + utils.udf_hex_to_int( + segmented_data [1] :: STRING + ) :: INTEGER AS repay_amount_raw, + _inserted_timestamp, + _log_id + FROM + {{ ref('silver__logs') }} + l + WHERE + contract_address IN ( + SELECT + token_address + FROM + asset_details + ) + AND topics [0] :: STRING = '0x33f3048bd4e6af45e53afb722adfd57dbde82da7e93e44db921fb4b8c6a70c4b' + AND repay_amount_raw > 0 --borrow and repay in same log event, value in segmented data determines what kind of event + +{% if is_incremental() %} +AND _inserted_timestamp >= ( + SELECT + MAX( + _inserted_timestamp + ) - INTERVAL '12 hours' + FROM + {{ this }} +) +{% endif %} +) +SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + borrower, + A.token_address AS token_address, + 'bTAROT' AS token_symbol, + underlying_asset_address AS repay_contract_address, + underlying_asset_symbol AS repay_contract_symbol, + repay_amount_raw AS amount_unadj, + repay_amount_raw / pow( + 10, + underlying_decimals + ) AS amount, + payer, + 'Tarot' AS platform, + _inserted_timestamp, + _log_id +FROM + log_pull l + LEFT JOIN asset_details A + ON A.token_address = l.contract_address qualify(ROW_NUMBER() over(PARTITION BY _log_id +ORDER BY + _inserted_timestamp DESC)) = 1 diff --git a/models/silver/defi/lending/tarot/silver__tarot_repayments.yml b/models/silver/defi/lending/tarot/silver__tarot_repayments.yml new file mode 100644 index 00000000..070d88b9 --- /dev/null +++ b/models/silver/defi/lending/tarot/silver__tarot_repayments.yml @@ -0,0 +1,44 @@ +version: 2 +models: + - name: silver__tarot_repayments + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - _log_id + columns: + - name: BLOCK_NUMBER + tests: + - not_null + - name: BLOCK_TIMESTAMP + tests: + - not_null + - name: BORROWER + tests: + - not_null + - name: TOKEN_ADDRESS + tests: + - not_null + - name: TOKEN_SYMBOL + tests: + - not_null + - name: PAYER + tests: + - not_null + - name: REPAY_CONTRACT_ADDRESS + tests: + - not_null + - name: REPAY_CONTRACT_SYMBOL + tests: + - not_null + - name: AMOUNT + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: TX_HASH + tests: + - not_null + - name: EVENT_INDEX + tests: + - not_null \ No newline at end of file diff --git a/models/silver/defi/lending/tarot/silver__tarot_withdraws.sql b/models/silver/defi/lending/tarot/silver__tarot_withdraws.sql new file mode 100644 index 00000000..10a96fc5 --- /dev/null +++ b/models/silver/defi/lending/tarot/silver__tarot_withdraws.sql @@ -0,0 +1,94 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = "block_number", + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} + +with asset_details as ( + + select + borrowable1 as token_address, + token0 as underlying_asset_address, + token0_symbol as underlying_asset_symbol, + token0_decimals as underlying_decimals + from + {{ ref('silver__tarot_liquidity_pools') }} + group by all + UNION ALL + select + borrowable2 as token_address, + token1 as underlying_asset_address, + token1_symbol as underlying_asset_symbol, + token1_decimals as underlying_decimals + from + {{ ref('silver__tarot_liquidity_pools') }} + group by all + +), +log_pull as ( + select + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + regexp_substr_all(SUBSTR(DATA, 3, len(DATA)), '.{64}') AS segmented_data, + CONCAT('0x', SUBSTR(topics[1] :: STRING, 25, 40)) AS sender, + CONCAT('0x', SUBSTR(topics[2] :: STRING, 25, 40)) AS redeemer, + utils.udf_hex_to_int( + segmented_data [0] :: STRING + ) :: INTEGER AS redeemAmount_raw, + _inserted_timestamp, + _log_id + from + {{ ref('silver__logs') }} l + WHERE + contract_address IN (SELECT TOKEN_ADDRESS FROM asset_details) + AND + topics [0] :: STRING = '0x3f693fff038bb8a046aa76d9516190ac7444f7d69cf952c4cbdc086fdef2d6fc' +{% if is_incremental() %} +AND _inserted_timestamp >= ( + SELECT + MAX( + _inserted_timestamp + ) - INTERVAL '12 hours' + FROM + {{ this }} +) +{% endif %} +) +select + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + a.token_address, + 'bTAROT' AS token_symbol, + redeemAmount_raw AS amount_unadj, + redeemAmount_raw / pow( + 10, + underlying_decimals + ) AS amount, + underlying_asset_address as received_contract_address, + underlying_asset_symbol as received_contract_symbol, + redeemer, + 'Tarot' as platform, + _inserted_timestamp, + _log_id +FROM + log_pull l +LEFT JOIN + asset_details a +ON + a.token_address = l.contract_address qualify(ROW_NUMBER() over(PARTITION BY _log_id +ORDER BY + _inserted_timestamp DESC)) = 1 \ No newline at end of file diff --git a/models/silver/defi/lending/tarot/silver__tarot_withdraws.yml b/models/silver/defi/lending/tarot/silver__tarot_withdraws.yml new file mode 100644 index 00000000..2eff55aa --- /dev/null +++ b/models/silver/defi/lending/tarot/silver__tarot_withdraws.yml @@ -0,0 +1,47 @@ +version: 2 +models: + - name: silver__tarot_withdraws + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - _log_id + columns: + - name: BLOCK_NUMBER + tests: + - not_null + - name: BLOCK_TIMESTAMP + tests: + - not_null + - name: TOKEN_ADDRESS + tests: + - not_null + - name: TOKEN_SYMBOL + tests: + - not_null + - name: AMOUNT + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: RECEIVED_CONTRACT_ADDRESS + tests: + - not_null + - name: RECEIVED_CONTRACT_SYMBOL + tests: + - not_null + - name: REDEEMED_TOKEN + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: REDEEMER + tests: + - not_null + - name: TX_HASH + tests: + - not_null + - name: EVENT_INDEX + tests: + - not_null \ No newline at end of file