From 4702e972fdc891d0df1afa009736037e36c95793 Mon Sep 17 00:00:00 2001 From: PatelPrinci Date: Fri, 10 Jan 2025 14:37:16 +0530 Subject: [PATCH 1/3] last head commit --- .../seeds/_sector/perpetual/trades/perpetual_trades_seed.csv | 2 +- sources/pika/optimism/pika_optimism_sources.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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 415812b847a..5691f837994 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 @@ -78,4 +78,4 @@ base,2023-09-21,WETH,ETH/USD,0xad378c374f7996235e927e693edea32605c0a61f,Close Lo base,2024-01-03,snxUSD,Ethereum,0x0a2af931effd34b81ebcc57e3d3c9b1e1de1c9ce,close,Synthetix,3,0xb1dcb41bd0d90f418bcea2e7338df2290e50f1cd2185b2b75ec01fb7163fd240 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 \ No newline at end of file +arbitrum,2023-02-17,,,0xda1a7ea276fbdb16ebabb5b38257b1d56b302e4a,open-long,vela_exchange,1,0x94cacbb99ca4d6b11fb660dc71e6da77f98f6038752f71b158903c492578e34c diff --git a/sources/pika/optimism/pika_optimism_sources.yml b/sources/pika/optimism/pika_optimism_sources.yml index 20d96da4402..f492056e1a7 100644 --- a/sources/pika/optimism/pika_optimism_sources.yml +++ b/sources/pika/optimism/pika_optimism_sources.yml @@ -17,7 +17,7 @@ sources: - name: PikaPerpV2_evt_ClosePosition description: "Details the swaps to close existing positions" - - name: pika_perp_v3_optimism # since this is latest pika contract + - name: pika_perp_v3_optimism # since this is latest pika contracts description: Optimism decoded tables related to Pika Protocol v3 tables: - name: PikaPerpV3_evt_NewPosition From ada7d9daf215ab88125131161f126200735659a2 Mon Sep 17 00:00:00 2001 From: PatelPrinci Date: Sat, 25 Jan 2025 18:37:21 +0530 Subject: [PATCH 2/3] polynomial code --- ...ial_protocol_optimism_perpetual_trades.sql | 46 ++++++++++ ..._protocol_v1_optimism_perpetual_trades.sql | 91 +++++++++++++++++++ .../polynomial_protocol/polynomial_schema.yml | 84 +++++++++++++++++ .../trades/perpetual_trades_seed.csv | 3 + .../polynomial_protocol_optimism_sources.yml | 10 ++ 5 files changed, 234 insertions(+) create mode 100644 dbt_subprojects/hourly_spellbook/models/_sector/perpetual/projects/polynomial_protocol/polynomial_protocol_optimism_perpetual_trades.sql create mode 100644 dbt_subprojects/hourly_spellbook/models/_sector/perpetual/projects/polynomial_protocol/polynomial_protocol_v1_optimism_perpetual_trades.sql create mode 100644 dbt_subprojects/hourly_spellbook/models/_sector/perpetual/projects/polynomial_protocol/polynomial_schema.yml create mode 100644 sources/polynomial_protocol/polynomial_protocol_optimism_sources.yml diff --git a/dbt_subprojects/hourly_spellbook/models/_sector/perpetual/projects/polynomial_protocol/polynomial_protocol_optimism_perpetual_trades.sql b/dbt_subprojects/hourly_spellbook/models/_sector/perpetual/projects/polynomial_protocol/polynomial_protocol_optimism_perpetual_trades.sql new file mode 100644 index 00000000000..7b141481c5a --- /dev/null +++ b/dbt_subprojects/hourly_spellbook/models/_sector/perpetual/projects/polynomial_protocol/polynomial_protocol_optimism_perpetual_trades.sql @@ -0,0 +1,46 @@ +{{ config( + schema = 'polynomial_protocol_perpetual_trades', + alias = 'perpetual_trades', + post_hook='{{ expose_spells(blockchains = \'["optimism"]\', + spell_type = "project", + spell_name = "polynomial_protocol", + contributors = \'["princi"]\') }}' + ) +}} + +{% set polynomial_protocol_optimism_perpetual_trade_models = [ + ref('polynomial_protocol_v1_optimism_perpetual_trades') +] %} + +SELECT * +FROM +( + {% for polynomial_protocol_perpetual_trades in polynomial_protocol_optimism_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 {{ polynomial_protocol_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/polynomial_protocol/polynomial_protocol_v1_optimism_perpetual_trades.sql b/dbt_subprojects/hourly_spellbook/models/_sector/perpetual/projects/polynomial_protocol/polynomial_protocol_v1_optimism_perpetual_trades.sql new file mode 100644 index 00000000000..193bbe2efca --- /dev/null +++ b/dbt_subprojects/hourly_spellbook/models/_sector/perpetual/projects/polynomial_protocol/polynomial_protocol_v1_optimism_perpetual_trades.sql @@ -0,0 +1,91 @@ +{{ config( + alias = 'perpetual_trades', + schema = 'polynomial_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 ( + -- Open Position events + SELECT + evt_block_time as block_time, + evt_block_number as block_number, + 'open_position' as trade_data, + strikeId as product_id, + evt_tx_from as trader, + contract_address as market_address, + evt_index, + evt_tx_hash as tx_hash, + CAST(premiumCollected AS DOUBLE) as fee_usd, + CAST(amount AS DOUBLE) as volume_usd, + CAST(collateral AS DOUBLE) as margin_usd + FROM {{ source('polynomial_protocol_optimism', 'CallSellingVault_evt_OpenPosition') }} + {% if is_incremental() %} + WHERE {{ incremental_predicate('evt_block_time') }} + {% else %} + WHERE evt_block_time >= DATE '{{ project_start_date }}' + {% endif %} + + UNION ALL + + -- Close Position events + SELECT + evt_block_time as block_time, + evt_block_number as block_number, + 'close_position' as trade_data, + strikeId as product_id, + evt_tx_from as trader, + contract_address as market_address, + evt_index, + evt_tx_hash as tx_hash, + CAST(premiumPaid AS DOUBLE) as fee_usd, + CAST(amount AS DOUBLE) as volume_usd, + CAST(collateralWithdrawn AS DOUBLE) as margin_usd + FROM {{ source('polynomial_protocol_optimism', 'CallSellingVault_evt_ClosePosition') }} + {% if is_incremental() %} + WHERE {{ incremental_predicate('evt_block_time') }} + {% else %} + WHERE evt_block_time >= DATE '{{ project_start_date }}' + {% endif %} +) + +SELECT + 'optimism' as blockchain, + 'polynomial_protocol' as project, + '1' as version, + 'polynomial_protocol' 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, + CAST(NULL AS VARCHAR) as virtual_asset, + CAST(NULL AS VARCHAR) as underlying_asset, + CAST(NULL AS VARCHAR) as market, + pe.market_address, + pe.volume_usd, + pe.fee_usd, + pe.margin_usd, + CASE + WHEN pe.trade_data = 'open_position' THEN 'open' + WHEN pe.trade_data = 'close_position' THEN 'close' + END as trade, + pe.trader, + CAST(NULL AS UINT256) as volume_raw, + pe.tx_hash, + txns."to" as tx_to, + txns."from" as tx_from, + pe.evt_index, + CAST(NULL AS DOUBLE) as pnl +FROM perp_events pe +INNER JOIN {{ source('optimism', 'transactions') }} txns + ON pe.tx_hash = txns.hash + AND pe.block_number = txns.block_number +{% if is_incremental() %} + AND {{ incremental_predicate('txns.block_time') }} +{% else %} + AND txns.block_time >= DATE '{{ project_start_date }}' +{% endif %} \ No newline at end of file diff --git a/dbt_subprojects/hourly_spellbook/models/_sector/perpetual/projects/polynomial_protocol/polynomial_schema.yml b/dbt_subprojects/hourly_spellbook/models/_sector/perpetual/projects/polynomial_protocol/polynomial_schema.yml new file mode 100644 index 00000000000..27361684e5e --- /dev/null +++ b/dbt_subprojects/hourly_spellbook/models/_sector/perpetual/projects/polynomial_protocol/polynomial_schema.yml @@ -0,0 +1,84 @@ +version: 2 + +models: + - name: polynomial_protocol_v1_optimism_perpetual_trades + meta: + blockchain: optimism + sector: perpetual + contributors: princi + config: + tags: ['optimism', 'perpetuals', 'perps', 'polynomial_protocol','cross-chain'] + description: + Perpetual swaps/trades table on polynomial_protocol 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/seeds/_sector/perpetual/trades/perpetual_trades_seed.csv b/dbt_subprojects/hourly_spellbook/seeds/_sector/perpetual/trades/perpetual_trades_seed.csv index 5691f837994..bbc44326af7 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 @@ -79,3 +79,6 @@ 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 +optimism,2023-01-27,,,0x2d46292cbb3c601c6e2c74c32df3a4fce99b59c7,open,polynomial_protocol,1,0x46aad7c8e414d9ac253ddf9eec143ad158c7c14dcc7b184dbb3f88d7d903195c +optimism,2023-01-27,,,0x2d46292cbb3c601c6e2c74c32df3a4fce99b59c7,open,polynomial_protocol,1,0x1886b328576e700035ed0459761fb2898d2feae6eb6bd6c0f3576d0a02b4194e + diff --git a/sources/polynomial_protocol/polynomial_protocol_optimism_sources.yml b/sources/polynomial_protocol/polynomial_protocol_optimism_sources.yml new file mode 100644 index 00000000000..52ce46623c3 --- /dev/null +++ b/sources/polynomial_protocol/polynomial_protocol_optimism_sources.yml @@ -0,0 +1,10 @@ +version: 2 + +sources: + - name: polynomial_protocol_optimism + description: > + Decoded event tables for Perpetual trades on polynomial protocol + tables: + - name: CallSellingVault_evt_OpenPosition + + - name: CallSellingVault_evt_ClosePosition \ No newline at end of file From 40697c8ec7062f1c4e67c030032de357713cfe35 Mon Sep 17 00:00:00 2001 From: PatelPrinci Date: Thu, 30 Jan 2025 11:41:48 +0530 Subject: [PATCH 3/3] schema updated --- .../polynomial_protocol_optimism_perpetual_trades.sql | 2 +- .../polynomial_protocol_v1_optimism_perpetual_trades.sql | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/dbt_subprojects/hourly_spellbook/models/_sector/perpetual/projects/polynomial_protocol/polynomial_protocol_optimism_perpetual_trades.sql b/dbt_subprojects/hourly_spellbook/models/_sector/perpetual/projects/polynomial_protocol/polynomial_protocol_optimism_perpetual_trades.sql index 7b141481c5a..3d20062f5fc 100644 --- a/dbt_subprojects/hourly_spellbook/models/_sector/perpetual/projects/polynomial_protocol/polynomial_protocol_optimism_perpetual_trades.sql +++ b/dbt_subprojects/hourly_spellbook/models/_sector/perpetual/projects/polynomial_protocol/polynomial_protocol_optimism_perpetual_trades.sql @@ -1,5 +1,5 @@ {{ config( - schema = 'polynomial_protocol_perpetual_trades', + schema = 'polynomial_protocol_optimism', alias = 'perpetual_trades', post_hook='{{ expose_spells(blockchains = \'["optimism"]\', spell_type = "project", diff --git a/dbt_subprojects/hourly_spellbook/models/_sector/perpetual/projects/polynomial_protocol/polynomial_protocol_v1_optimism_perpetual_trades.sql b/dbt_subprojects/hourly_spellbook/models/_sector/perpetual/projects/polynomial_protocol/polynomial_protocol_v1_optimism_perpetual_trades.sql index 193bbe2efca..e05c55094cf 100644 --- a/dbt_subprojects/hourly_spellbook/models/_sector/perpetual/projects/polynomial_protocol/polynomial_protocol_v1_optimism_perpetual_trades.sql +++ b/dbt_subprojects/hourly_spellbook/models/_sector/perpetual/projects/polynomial_protocol/polynomial_protocol_v1_optimism_perpetual_trades.sql @@ -1,6 +1,6 @@ {{ config( alias = 'perpetual_trades', - schema = 'polynomial_base', + schema = 'polynomial_protocol_v1_optimism', partition_by = ['block_month'], materialized = 'incremental', file_format = 'delta',