-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* last head commit * dip exchange on base * fixed filename * changed the source format for decoded table * removed the polynomial file
- Loading branch information
1 parent
0561f69
commit 13f8b22
Showing
5 changed files
with
261 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
...ook/models/_sector/perpetual/projects/dip_exchange/dip_exchange_base_perpetual_trades.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 %} | ||
) |
84 changes: 84 additions & 0 deletions
84
...s/hourly_spellbook/models/_sector/perpetual/projects/dip_exchange/dip_exchange_schema.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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" |
119 changes: 119 additions & 0 deletions
119
.../models/_sector/perpetual/projects/dip_exchange/dip_exchange_v1_base_perpetual_trades.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |