diff --git a/dbt_subprojects/hourly_spellbook/models/_sector/perpetual/projects/dip_exchange/dip_exchange_base_perpetual_trades.sql b/dbt_subprojects/hourly_spellbook/models/_sector/perpetual/projects/dip_exchange/dip_exchange_base_perpetual_trades.sql new file mode 100644 index 00000000000..29c54dbf280 --- /dev/null +++ b/dbt_subprojects/hourly_spellbook/models/_sector/perpetual/projects/dip_exchange/dip_exchange_base_perpetual_trades.sql @@ -0,0 +1,46 @@ +{{ config( + schema = 'dip_exchange_base', + alias = 'perpetual_trades', + post_hook='{{ expose_spells(blockchains = \'["base"]\', + spell_type = "project", + spell_name = "dip_exchange", + contributors = \'["princi"]\') }}' + ) +}} + +{% set dip_exchange_base_perpetual_trade_models = [ + ref('dip_exchange_v1_base_perpetual_trades') +] %} + +SELECT * +FROM +( + {% for dip_exchange_perpetual_trades in dip_exchange_base_perpetual_trade_models %} + SELECT + blockchain + ,block_date + ,block_month + ,block_time + ,virtual_asset + ,underlying_asset + ,market + ,market_address + ,volume_usd + ,fee_usd + ,margin_usd + ,trade + ,project + ,version + ,frontend + ,trader + ,volume_raw + ,tx_hash + ,tx_from + ,tx_to + ,evt_index + FROM {{ dip_exchange_perpetual_trades }} + {% if not loop.last %} + UNION ALL + {% endif %} + {% endfor %} +) \ No newline at end of file diff --git a/dbt_subprojects/hourly_spellbook/models/_sector/perpetual/projects/dip_exchange/dip_exchange_schema.yml b/dbt_subprojects/hourly_spellbook/models/_sector/perpetual/projects/dip_exchange/dip_exchange_schema.yml new file mode 100644 index 00000000000..20d418fe3dc --- /dev/null +++ b/dbt_subprojects/hourly_spellbook/models/_sector/perpetual/projects/dip_exchange/dip_exchange_schema.yml @@ -0,0 +1,84 @@ +version: 2 + +models: + - name: dip_exchange_v1_base_perpetual_trades + meta: + blockchain: base + sector: perpetual + contributors: princi + config: + tags: ['base', 'perpetuals', 'perps', 'dip_exchange'] + description: + Perpetual swaps/trades table on dip_exchange protocol across blockchains + data_tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - tx_hash + - evt_index + columns: + - &blockchain + name: blockchain + description: "Blockchain where the perpetuals market is deployed" + - &block_date + name: block_date + description: "Date of the transaction" + - &block_time + name: block_time + description: "Time of the transaction" + - &virtual_asset + name: virtual_asset + description: "How the protocol represents the underlying asset" + - &underlying_asset + name: underlying_asset + description: "The real underlying asset that is represented in the swap" + - &market + name: market + description: "The futures market involved in the transaction" + - &market_address + name: market_address + description: "Contract address of the market" + data_tests: + - perpetual_trades_market_address: + perpetual_trades_seed: ref('perpetual_trades_seed') + - &volume_usd + name: volume_usd + description: "The size of the position taken for the swap in USD; already in absolute value and decimal normalized" + - &fee_usd + name: fee_usd + description: "The fees charged to the user for the swap in USD" + - &margin_usd + name: margin_usd + description: "The amount of collateral/margin used in a trade in USD" + - &trade + name: trade + description: "Indicates the trade's direction whether a short, long, of if a position is being closed" + - &project + name: project + description: "The underlying protocol/project where the swap took place" + - &version + name: version + description: "The version of the protocol/project" + - &frontend + name: frontend + description: "The frontend protocol/project where the specific swap was executed; built on top of the 'project' and defaults to the 'project' if no other frontend is specified" + - &trader + name: trader + description: "The address which made the swap in the protocol" + - &volume_raw + name: volume_raw + description: "The size of the position in raw form" + - &tx_hash + name: tx_hash + description: "The hash of the transactions" + - &tx_from + name: tx_from + description: "The address that originated the transaction; based on the optimism.transactions table" + - &tx_to + name: tx_to + description: "The address receiving the transaction; based on the optimism.transactions table" + - &evt_index + name: evt_index + description: "Event index number" + - &block_month + name: block_month + description: "Month of the transaction" \ No newline at end of file diff --git a/dbt_subprojects/hourly_spellbook/models/_sector/perpetual/projects/dip_exchange/dip_exchange_v1_base_perpetual_trades.sql b/dbt_subprojects/hourly_spellbook/models/_sector/perpetual/projects/dip_exchange/dip_exchange_v1_base_perpetual_trades.sql new file mode 100644 index 00000000000..29a4526a288 --- /dev/null +++ b/dbt_subprojects/hourly_spellbook/models/_sector/perpetual/projects/dip_exchange/dip_exchange_v1_base_perpetual_trades.sql @@ -0,0 +1,119 @@ +{{ config( + alias = 'perpetual_trades', + schema = 'dip_exchange_v1_base', + partition_by = ['block_month'], + materialized = 'incremental', + file_format = 'delta', + incremental_strategy = 'merge', + unique_key = ['tx_hash', 'evt_index'] +)}} + +{% set project_start_date = '2023-01-01' %} + +WITH perp_events AS ( + -- Increase Position events + SELECT + evt_block_time AS block_time, + evt_block_number AS block_number, + 'increase' AS trade_data, + key AS position_id, + account AS trader, + contract_address AS market_address, + evt_index, + evt_tx_hash AS tx_hash, + evt_tx_from AS tx_from, + evt_tx_to AS tx_to, + CAST(feeValue AS DOUBLE)/1e30 AS fee_usd, + CAST(sizeChanged AS DOUBLE)/1e30 AS volume_usd, + CAST(collateralValue AS DOUBLE)/1e30 AS margin_usd, + CAST(indexPrice AS DOUBLE)/1e30 AS price, + collateralToken, + indexToken, + side, + CAST(NULL AS DOUBLE) AS pnl + FROM {{ source('pool_base', 'Pool_evt_IncreasePosition') }} + WHERE evt_block_time >= DATE '{{ project_start_date }}' + + UNION ALL + + -- Decrease Position events + SELECT + evt_block_time AS block_time, + evt_block_number AS block_number, + 'decrease' AS trade_data, + key AS position_id, + account AS trader, + contract_address AS market_address, + evt_index, + evt_tx_hash AS tx_hash, + evt_tx_from AS tx_from, + evt_tx_to AS tx_to, + CAST(feeValue AS DOUBLE)/1e30 AS fee_usd, + CAST(sizeChanged AS DOUBLE)/1e30 AS volume_usd, + CAST(collateralChanged AS DOUBLE)/1e30 AS margin_usd, + CAST(indexPrice AS DOUBLE)/1e30 AS price, + collateralToken, + indexToken, + side, + TRY_CAST(JSON_EXTRACT_SCALAR(pnl, '$.abs') AS DOUBLE)/1e30 AS pnl + FROM {{ source('pool_base', 'Pool_evt_DecreasePosition') }} + WHERE evt_block_time >= DATE '{{ project_start_date }}' + + UNION ALL + + -- Liquidate Position events + SELECT + evt_block_time AS block_time, + evt_block_number AS block_number, + 'liquidate' AS trade_data, + key AS position_id, + account AS trader, + contract_address AS market_address, + evt_index, + evt_tx_hash AS tx_hash, + evt_tx_from AS tx_from, + evt_tx_to AS tx_to, + CAST(feeValue AS DOUBLE)/1e30 AS fee_usd, + CAST(size AS DOUBLE)/1e30 AS volume_usd, + CAST(collateralValue AS DOUBLE)/1e30 AS margin_usd, + CAST(indexPrice AS DOUBLE)/1e30 AS price, + collateralToken, + indexToken, + side, + TRY_CAST(JSON_EXTRACT_SCALAR(pnl, '$.abs') AS DOUBLE)/1e30 AS pnl + FROM {{ source('pool_base', 'Pool_evt_LiquidatePosition') }} + WHERE evt_block_time >= DATE '{{ project_start_date }}' +) + +SELECT + 'base' AS blockchain, + 'dip_exchange' AS project, + '1' AS version, + 'dip_exchange' AS frontend, + CAST(DATE_TRUNC('day', pe.block_time) AS DATE) AS block_date, + CAST(DATE_TRUNC('month', pe.block_time) AS DATE) AS block_month, + pe.block_time, + pe.indexToken AS virtual_asset, + pe.collateralToken AS underlying_asset, + pe.position_id AS market, + pe.market_address, + pe.volume_usd, + pe.fee_usd, + pe.margin_usd, + CASE + WHEN pe.trade_data = 'increase' AND pe.side = 0 THEN 'open_long' + WHEN pe.trade_data = 'increase' AND pe.side = 1 THEN 'open_short' + WHEN pe.trade_data = 'decrease' AND pe.side = 0 THEN 'close_long' + WHEN pe.trade_data = 'decrease' AND pe.side = 1 THEN 'close_short' + WHEN pe.trade_data = 'liquidate' THEN 'liquidation' + END AS trade, + pe.trader, + pe.side AS trade_type, + pe.price, + CAST(NULL AS UINT256) AS volume_raw, + pe.tx_hash, + pe.tx_to, + pe.tx_from, + pe.evt_index, + pe.pnl +FROM perp_events pe diff --git a/dbt_subprojects/hourly_spellbook/seeds/_sector/perpetual/trades/perpetual_trades_seed.csv b/dbt_subprojects/hourly_spellbook/seeds/_sector/perpetual/trades/perpetual_trades_seed.csv index 0d3ee2df542..13e1c23f23a 100644 --- a/dbt_subprojects/hourly_spellbook/seeds/_sector/perpetual/trades/perpetual_trades_seed.csv +++ b/dbt_subprojects/hourly_spellbook/seeds/_sector/perpetual/trades/perpetual_trades_seed.csv @@ -81,5 +81,7 @@ base,2024-01-03,snxUSD,Ethereum,0x0a2af931effd34b81ebcc57e3d3c9b1e1de1c9ce,close base,2024-02-15,snxUSD,Ethereum,0x0a2af931effd34b81ebcc57e3d3c9b1e1de1c9ce,long,Synthetix,3,0xf0cae8268019a3e6bff055d496a5f52ed805f60f7582791d93944c026fd61b98 arbitrum,2023-02-17,,,0xda1a7ea276fbdb16ebabb5b38257b1d56b302e4a,open-long,vela_exchange,1,0x6a783688a2e013bfe84a6e7ae65dfd2f2c01e452b360a07a4bdf4d502ee8d187 arbitrum,2023-02-17,,,0xda1a7ea276fbdb16ebabb5b38257b1d56b302e4a,open-long,vela_exchange,1,0x94cacbb99ca4d6b11fb660dc71e6da77f98f6038752f71b158903c492578e34c +base,2023-08-22,0x4200000000000000000000000000000000000006,0x1cb1956233af0f6925a72a2c124b76e28dc7889d992737e2226359bd19803359,0xd91bba888c1f80bed01b66830d006c26a7e8625c,close_long,dip-exchange,1,0x6c41f99938e3323b9a457b222b9d212b3bbff59eb4bf554f7258e8766b0421e8 +base,2023-08-22,0x4200000000000000000000000000000000000006,0x1cb1956233af0f6925a72a2c124b76e28dc7889d992737e2226359bd19803359,0xd91bba888c1f80bed01b66830d006c26a7e8625c,close_long,dip-exchange,1,0x1037b865994ca091c005323a1c513e36f3c9fd182a4d8aa5baad99485ee91d53 base,2024-10-22,BTC-USD,BTC-USD,0x6cd5ac19a07518a8092eeffda4f1174c72704eeb,long,gains_network,1,0x34397f0d62a19bfe6a325c2a906560af6cf0f699b0893a333996c0b361c6a4d8 base,2024-10-06,COMP-USD,COMP-USD,0x6cd5ac19a07518a8092eeffda4f1174c72704eeb,long,gains_network,1,0x8836d5e2955cdbaee1292a5016ea51301c459897bf7ad319553e0c51c4fcbce9 \ No newline at end of file diff --git a/sources/dip_exchange/dip_exchange_base_sources.yml b/sources/dip_exchange/dip_exchange_base_sources.yml new file mode 100644 index 00000000000..5781b7a13c2 --- /dev/null +++ b/sources/dip_exchange/dip_exchange_base_sources.yml @@ -0,0 +1,10 @@ +version: 2 + +sources: + - name: pool_base + description: > + Decoded event tables for Perpetual trades on dip exchange protocol + tables: + - name: Pool_evt_IncreasePosition + - name: Pool_evt_DecreasePosition + - name: Pool_evt_LiquidatePosition