Skip to content

Commit ba309f0

Browse files
thecaptncrunchcursoragentjeff-dude
authored
Add KatanaPerps Katana model to perpetual.trades (#9509)
Co-authored-by: Cursor Agent <cursoragent@cursor.com> Co-authored-by: thecaptncrunch <thecaptncrunch@users.noreply.github.com> Co-authored-by: jeff-dude <102681548+jeff-dude@users.noreply.github.com>
1 parent fcafb6b commit ba309f0

5 files changed

Lines changed: 222 additions & 3 deletions

File tree

dbt_subprojects/hourly_spellbook/models/_sector/perpetual/perpetual_schema.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ version: 2
33
models:
44
- name: perpetual_trades
55
meta:
6-
blockchain: optimism, avalanche_c, arbitrum, polygon
6+
blockchain: optimism, avalanche_c, arbitrum, polygon, katana
77
sector: perpetual
8-
contributors: msilb7, drethereum, rplust, Henrystats
8+
contributors: msilb7, drethereum, rplust, Henrystats, cursor-agent
99
config:
10-
tags: ['optimism', 'avalanche_c', 'arbitrum', 'perpetuals', 'perps', 'cross-chain', 'pika', 'perpetual-protocol', 'synthetix']
10+
tags: ['optimism', 'avalanche_c', 'arbitrum', 'katana', 'perpetuals', 'perps', 'cross-chain', 'pika', 'perpetual-protocol', 'synthetix']
1111
description: >
1212
Perpetual swaps/trades table
1313
data_tests:

dbt_subprojects/hourly_spellbook/models/_sector/perpetual/perpetual_trades.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
,ref('bmx_base_perpetual_trades')
3838
,ref('nether_fi_base_perpetual_trades')
3939
,ref('gains_network_apechain_perpetual_trades')
40+
,ref('katanaperps_katana_perpetual_trades')
4041
] %}
4142

4243
SELECT *
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
{{ config(
2+
alias = 'perpetual_trades',
3+
schema = 'katanaperps_katana',
4+
partition_by = ['block_month'],
5+
materialized = 'incremental',
6+
file_format = 'delta',
7+
incremental_strategy = 'merge',
8+
unique_key = ['block_date', 'blockchain', 'project', 'version', 'tx_hash', 'evt_index'],
9+
incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.block_time')]
10+
)
11+
}}
12+
13+
{% set project_start_date = '2026-03-23' %}
14+
15+
WITH trade_events AS (
16+
SELECT
17+
evt_block_time AS block_time,
18+
evt_block_number AS block_number,
19+
evt_index,
20+
contract_address AS market_address,
21+
evt_tx_hash AS tx_hash,
22+
buyWallet AS buy_wallet,
23+
sellWallet AS sell_wallet,
24+
baseAssetSymbol AS base_asset_symbol,
25+
quoteAssetSymbol AS quote_asset_symbol,
26+
CAST(baseQuantity AS UINT256) AS base_quantity_raw,
27+
CAST(quoteQuantity AS DOUBLE) / 1e8 AS quote_quantity_usd,
28+
CAST(makerFeeQuantity AS DOUBLE) / 1e8 AS maker_fee_usd,
29+
CAST(takerFeeQuantity AS DOUBLE) / 1e8 AS taker_fee_usd,
30+
makerSide AS maker_side
31+
FROM {{ source('katanaperps_katana', 'exchange_v1_evt_tradeexecuted') }}
32+
{% if is_incremental() -%}
33+
WHERE {{ incremental_predicate('evt_block_time') }}
34+
{% else -%}
35+
WHERE evt_block_time >= TIMESTAMP '{{ project_start_date }}'
36+
{% endif -%}
37+
),
38+
39+
trade_sides AS (
40+
SELECT
41+
block_time,
42+
block_number,
43+
market_address,
44+
tx_hash,
45+
CAST(CAST(evt_index AS BIGINT) * 10 + 1 AS BIGINT) AS evt_index,
46+
buy_wallet AS trader,
47+
'long' AS trade,
48+
CASE
49+
WHEN maker_side = 0 THEN maker_fee_usd
50+
ELSE taker_fee_usd
51+
END AS fee_usd,
52+
base_asset_symbol,
53+
quote_asset_symbol,
54+
base_quantity_raw,
55+
quote_quantity_usd
56+
FROM trade_events
57+
58+
UNION ALL
59+
60+
SELECT
61+
block_time,
62+
block_number,
63+
market_address,
64+
tx_hash,
65+
CAST(CAST(evt_index AS BIGINT) * 10 + 2 AS BIGINT) AS evt_index,
66+
sell_wallet AS trader,
67+
'short' AS trade,
68+
CASE
69+
WHEN maker_side = 1 THEN maker_fee_usd
70+
ELSE taker_fee_usd
71+
END AS fee_usd,
72+
base_asset_symbol,
73+
quote_asset_symbol,
74+
base_quantity_raw,
75+
quote_quantity_usd
76+
FROM trade_events
77+
),
78+
79+
transactions_filtered AS (
80+
SELECT
81+
hash,
82+
block_number,
83+
"from",
84+
"to",
85+
block_time,
86+
block_date
87+
FROM {{ source('katana', 'transactions') }}
88+
WHERE {% if is_incremental() -%}
89+
{{ incremental_predicate('block_time') }}
90+
{% else -%}
91+
block_time >= TIMESTAMP '{{ project_start_date }}'
92+
{% endif -%}
93+
)
94+
95+
SELECT
96+
'katana' AS blockchain,
97+
CAST(date_trunc('day', t.block_time) AS date) AS block_date,
98+
CAST(date_trunc('month', t.block_time) AS date) AS block_month,
99+
t.block_time,
100+
t.base_asset_symbol AS virtual_asset,
101+
t.base_asset_symbol AS underlying_asset,
102+
CONCAT(t.base_asset_symbol, '/', t.quote_asset_symbol) AS market,
103+
t.market_address,
104+
t.quote_quantity_usd AS volume_usd,
105+
t.fee_usd,
106+
CAST(NULL AS DOUBLE) AS margin_usd,
107+
t.trade,
108+
'katanaperps' AS project,
109+
'1' AS version,
110+
'katanaperps' AS frontend,
111+
t.trader,
112+
t.base_quantity_raw AS volume_raw,
113+
t.tx_hash,
114+
tx."from" AS tx_from,
115+
tx."to" AS tx_to,
116+
t.evt_index
117+
FROM trade_sides t
118+
INNER JOIN transactions_filtered tx
119+
ON t.tx_hash = tx.hash
120+
AND t.block_number = tx.block_number
121+
AND CAST(date_trunc('day', t.block_time) AS date) = tx.block_date
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
version: 2
2+
3+
models:
4+
- name: katanaperps_katana_perpetual_trades
5+
meta:
6+
blockchain: katana
7+
sector: perpetual
8+
project: katanaperps
9+
contributors: cursor-agent
10+
config:
11+
tags: ['katana', 'perpetuals', 'perps', 'katanaperps']
12+
description: >
13+
Katana Perps perpetual swaps/trades on katana
14+
data_tests:
15+
- dbt_utils.unique_combination_of_columns:
16+
combination_of_columns:
17+
- block_date
18+
- blockchain
19+
- project
20+
- version
21+
- tx_hash
22+
- evt_index
23+
columns:
24+
- &blockchain
25+
name: blockchain
26+
description: "Blockchain where the perpetuals market is deployed"
27+
- &block_date
28+
name: block_date
29+
description: "Date of the transaction"
30+
- &block_time
31+
name: block_time
32+
description: "Time of the transaction"
33+
- &virtual_asset
34+
name: virtual_asset
35+
description: "How the protocol represents the underlying asset"
36+
- &underlying_asset
37+
name: underlying_asset
38+
description: "The real underlying asset that is represented in the swap"
39+
- &market
40+
name: market
41+
description: "The futures market involved in the transaction"
42+
- &market_address
43+
name: market_address
44+
description: "Contract address of the market"
45+
- &volume_usd
46+
name: volume_usd
47+
description: "The size of the position taken for the swap in USD; already in absolute value and decimal normalized"
48+
- &fee_usd
49+
name: fee_usd
50+
description: "The fees charged to the user for the swap in USD"
51+
- &margin_usd
52+
name: margin_usd
53+
description: "The amount of collateral/margin used in a trade in USD"
54+
- &trade
55+
name: trade
56+
description: "Indicates the trade's direction whether a short, long, of if a position is being closed"
57+
- &project
58+
name: project
59+
description: "The underlying protocol/project where the swap took place"
60+
- &version
61+
name: version
62+
description: "The version of the protocol/project"
63+
- &frontend
64+
name: frontend
65+
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"
66+
- &trader
67+
name: trader
68+
description: "The address which made the swap in the protocol"
69+
- &volume_raw
70+
name: volume_raw
71+
description: "The size of the position in raw form"
72+
- &tx_hash
73+
name: tx_hash
74+
description: "The hash of the transaction"
75+
- &tx_from
76+
name: tx_from
77+
description: "The address that originated the transaction; based on the katana.transactions table"
78+
- &tx_to
79+
name: tx_to
80+
description: "The address receiving the transaction; based on the katana.transactions table"
81+
- &evt_index
82+
name: evt_index
83+
description: "Event index number"
84+
- &block_month
85+
name: block_month
86+
description: "Month of the transaction"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
version: 2
2+
3+
sources:
4+
- name: katanaperps_katana
5+
description: Katana decoded tables related to Katana Perps
6+
freshness:
7+
warn_after: { count: 12, period: hour }
8+
error_after: { count: 24, period: hour }
9+
tables:
10+
- name: exchange_v1_evt_tradeexecuted
11+
loaded_at_field: evt_block_time

0 commit comments

Comments
 (0)