Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BSX on BASE #7539

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{{ config(
schema = 'bsx_perpetual_trades',
alias = 'perpetual_trades',
post_hook='{{ expose_spells(blockchains = \'["base"]\',
spell_type = "project",
spell_name = "bsx",
contributors = \'["princi"]\') }}'
)
}}

{% set bsx_base_perpetual_trade_models = [
ref('bsx_v1_base_perpetual_trades')
] %}

SELECT *
FROM
(
{% for bsx_perpetual_trades in bsx_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 {{ bsx_perpetual_trades }}
{% if not loop.last %}
UNION ALL
{% endif %}
{% endfor %}
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
version: 2

models:
- name: bsx_v1_base_perpetual_trades
meta:
blockchain: base
sector: perpetual
contributors: princi
config:
tags: ['base', 'perpetuals', 'perps', 'bsx','cross-chain']
description:
Perpetual swaps/trades table on bsx 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 base.transactions table"
- &tx_to
name: tx_to
description: "The address receiving the transaction; based on the base.transactions table"
- &evt_index
name: evt_index
description: "Event index number"
- &block_month
name: block_month
description: "Month of the transaction"
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
{{ config(
alias = 'perpetual_trades',
schema = 'bsx_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,
productId as product_id,
account as trader,
contract_address as market_address,
evt_index,
evt_tx_hash as tx_hash,
CAST(fee AS DOUBLE)/1E18 as fee_usd,
CAST(NULL AS DOUBLE) as volume_usd,
CAST(NULL AS DOUBLE) as pnl
FROM
{{ source('bsx_base', 'BSX1000x_evt_OpenPosition') }}
{% if not is_incremental() %}
WHERE evt_block_time >= DATE '{{project_start_date}}'
{% else %}
WHERE {{ incremental_predicate('evt_block_time') }}
{% endif %}

UNION ALL

-- close position events
SELECT
evt_block_time as block_time,
evt_block_number as block_number,
'close_position' as trade_data,
productId as product_id,
account as trader,
contract_address as market_address,
evt_index,
evt_tx_hash as tx_hash,
CAST(fee AS DOUBLE)/1E18 as fee_usd,
CAST(NULL AS DOUBLE) as volume_usd,
CAST(pnl AS DOUBLE)/1E18 as pnl
FROM
{{ source('bsx_base', 'BSX1000x_evt_ClosePosition') }}
{% if not is_incremental() %}
WHERE evt_block_time >= DATE '{{project_start_date}}'
{% else %}
WHERE {{ incremental_predicate('evt_block_time') }}
{% endif %}
)

SELECT
'base' as blockchain,
'bsx' as project,
'1' as version,
'bsx' 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,
pe.product_id as market,
pe.market_address,
pe.volume_usd,
pe.fee_usd,
CAST(NULL AS DOUBLE) as 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,
pe.pnl
FROM
perp_events pe
INNER JOIN {{ source('base', 'transactions') }} txns
ON pe.tx_hash = txns.hash
AND pe.block_number = txns.block_number
{% if not is_incremental() %}
AND txns.block_time >= DATE '{{project_start_date}}'
{% else %}
AND {{ incremental_predicate('txns.block_time') }}
{% endif %}

Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,5 @@ 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,2024-08-01,,1,0x797d6f745f691133ce90438e1ba3eeeb16e4b5b5,open,bsx,1,0x1a4068aa07c3dcf53d3d6240c59ddab2ac2beaddf86538f049efa6172ae73ea4
base,2024-08-01,,1,0x797d6f745f691133ce90438e1ba3eeeb16e4b5b5,close,bex,1,0xf7a0efb5bf4837abe4b02231de9ee0a6f6f1650305710832de7a404f0358078d
10 changes: 10 additions & 0 deletions sources/bsx/bsx_base_sources.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: 2

sources:
- name: bsx_base
description: base decoded tables related to BSX
tables:
- name: BSX1000x_evt_OpenPosition
description: "Details the new positions opened"
- name: BSX1000x_evt_ClosePosition
description: "Details the swaps to close existing positions"
Loading