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

YFX V4 on Base #7507

Open
wants to merge 7 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 = 'yfx_base',
alias = 'perpetual_trades',
post_hook='{{ expose_spells(\'["base"]\',
"project",
"yfx",
\'["principatel"]\') }}'
)
}}

{% set yfx_base_perpetual_trade_models = [
ref('yfx_v4_base_perpetual_trades')
] %}

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

models:
- name: yfx_v4_base_perpetual_trades
meta:
blockchain: base
sector: perpetual
project: yfx_v4
contributors: principatel
config:
tags: ['base', 'perpetual', 'yfx', 'perps']
description: >
YFX v4 perpetuals trades/swaps on base
data_tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- block_date
- blockchain
- project
- version
- 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 transaction"
- &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"

- name: yfx_base_perpetual_trades
meta:
blockchain: base
sector: perpetual
contributors: principatel
config:
tags: ['base', 'yfx', 'perps']
description: >
YFX swaps/trades table on Base
columns:
- *blockchain
- *block_date
- *block_time
- *virtual_asset
- *underlying_asset
- *market
- name: market_address # since object is a view, avoid test repeat with '*' usage
description: "Contract address of the market"
- *volume_usd
- *fee_usd
- *margin_usd
- *trade
- *project
- *version
- *frontend
- *trader
- *volume_raw
- *tx_hash
- *tx_from
- *tx_to
- *evt_index
- *block_month
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
{{ config(
schema = 'yfx_v4_base',
alias = 'perpetual_trades',
partition_by = ['block_month'],
materialized = 'incremental',
file_format = 'delta',
incremental_strategy = 'merge',
unique_key = ['block_date', 'blockchain', 'project', 'version', 'tx_hash', 'evt_index'],
post_hook='{{ expose_spells(\'["base"]\',
spell_type = "project",
spell_name = "yfx_v4",
contributors = \'["princi"]\') }}'
)
}}

WITH all_events AS (
SELECT
evt_block_time,
evt_block_number,
evt_tx_hash,
evt_tx_from as "from",
evt_tx_to as to,
evt_index,
contract_address,
taker as account,
market,
id as productId, -- Using id as productId for asset mapping
CAST(shortValue as double) / 1e18 as shortValue,
CAST(longValue as double) / 1e18 as longValue,
(CAST(feeToExchange as double) + CAST(feeToMaker as double) + CAST(feeToInviter as double)) as fee,
'open' as trade_type
FROM {{ source('yfx_v4_base', 'Pool_evt_OpenUpdate') }}
{% if is_incremental() %}
WHERE {{ incremental_predicate('evt_block_time') }}
{% endif %}

UNION ALL

SELECT
evt_block_time,
evt_block_number,
evt_tx_hash,
evt_tx_from as "from",
evt_tx_to as to,
evt_index,
contract_address,
taker as account,
market,
id as productId, -- Using id as productId for asset mapping
CAST(shortValue as double) / 1e18 as shortValue,
CAST(longValue as double) / 1e18 as longValue,
(CAST(feeToExchange as double) + CAST(feeToMaker as double) + CAST(feeToInviter as double)) as fee,
'close' as trade_type
FROM {{ source('yfx_v4_base', 'Pool_evt_CloseUpdate') }}
{% if is_incremental() %}
WHERE {{ incremental_predicate('evt_block_time') }}
{% endif %}
)

SELECT
'base' AS blockchain,
CAST(date_trunc('DAY', evt_block_time) AS date) AS block_date,
CAST(date_trunc('MONTH', evt_block_time) AS date) AS block_month,
evt_block_time AS block_time,
CASE
WHEN productId = UINT256 '1' OR productId = UINT256 '16' THEN 'ETH'
WHEN productId = UINT256 '2' OR productId = UINT256 '17' THEN 'BTC'
WHEN productId = UINT256 '3' OR productId = UINT256 '18' THEN 'LINK'
WHEN productId = UINT256 '4' OR productId = UINT256 '19' THEN 'SNX'
WHEN productId = UINT256 '5' OR productId = UINT256 '20' THEN 'SOL'
WHEN productId = UINT256 '6' OR productId = UINT256 '21' THEN 'AVAX'
WHEN productId = UINT256 '7' OR productId = UINT256 '22' THEN 'MATIC'
WHEN productId = UINT256 '8' THEN 'LUNA'
WHEN productId = UINT256 '9' OR productId = UINT256 '23' THEN 'AAVE'
WHEN productId = UINT256 '10' OR productId = UINT256 '24' THEN 'APE'
WHEN productId = UINT256 '11' OR productId = UINT256 '25' THEN 'AXS'
WHEN productId = UINT256 '12' OR productId = UINT256 '26' THEN 'UNI'
ELSE CONCAT('product_id_', CAST(productId as VARCHAR))
END AS virtual_asset,

CASE
WHEN productId = UINT256 '1' OR productId = UINT256 '16' THEN 'ETH'
WHEN productId = UINT256 '2' OR productId = UINT256 '17' THEN 'BTC'
WHEN productId = UINT256 '3' OR productId = UINT256 '18' THEN 'LINK'
WHEN productId = UINT256 '4' OR productId = UINT256 '19' THEN 'SNX'
WHEN productId = UINT256 '5' OR productId = UINT256 '20' THEN 'SOL'
WHEN productId = UINT256 '6' OR productId = UINT256 '21' THEN 'AVAX'
WHEN productId = UINT256 '7' OR productId = UINT256 '22' THEN 'MATIC'
WHEN productId = UINT256 '8' THEN 'LUNA'
WHEN productId = UINT256 '9' OR productId = UINT256 '23' THEN 'AAVE'
WHEN productId = UINT256 '10' OR productId = UINT256 '24' THEN 'APE'
WHEN productId = UINT256 '11' OR productId = UINT256 '25' THEN 'AXS'
WHEN productId = UINT256 '12' OR productId = UINT256 '26' THEN 'UNI'
ELSE CONCAT('product_id_', CAST(productId as VARCHAR))
END AS underlying_asset,

CASE
WHEN productId = UINT256 '1' OR productId = UINT256 '16' THEN 'ETH'
WHEN productId = UINT256 '2' OR productId = UINT256 '17' THEN 'BTC'
WHEN productId = UINT256 '3' OR productId = UINT256 '18' THEN 'LINK'
WHEN productId = UINT256 '4' OR productId = UINT256 '19' THEN 'SNX'
WHEN productId = UINT256 '5' OR productId = UINT256 '20' THEN 'SOL'
WHEN productId = UINT256 '6' OR productId = UINT256 '21' THEN 'AVAX'
WHEN productId = UINT256 '7' OR productId = UINT256 '22' THEN 'MATIC'
WHEN productId = UINT256 '8' THEN 'LUNA'
WHEN productId = UINT256 '9' OR productId = UINT256 '23' THEN 'AAVE'
WHEN productId = UINT256 '10' OR productId = UINT256 '24' THEN 'APE'
WHEN productId = UINT256 '11' OR productId = UINT256 '25' THEN 'AXS'
WHEN productId = UINT256 '12' OR productId = UINT256 '26' THEN 'UNI'
ELSE CONCAT('product_id_', CAST(productId as VARCHAR))
END AS market,
contract_address AS market_address,
GREATEST(shortValue, longValue) AS volume_usd,
CAST(fee / 1e18 AS DOUBLE) AS fee_usd,
GREATEST(shortValue, longValue) AS margin_usd,
CASE
WHEN longValue > shortValue AND trade_type = 'open' THEN 'Open Long'
WHEN shortValue > longValue AND trade_type = 'open' THEN 'Open Short'
WHEN longValue > shortValue AND trade_type = 'close' THEN 'Close Long'
WHEN shortValue > longValue AND trade_type = 'close' THEN 'Close Short'
END AS trade,
'yfx' AS project,
'4' AS version,
'yfx' AS frontend,
account AS trader,
CAST(GREATEST(shortValue, longValue) * 1e18 AS uint256) AS volume_raw,
evt_tx_hash AS tx_hash,
"from" AS tx_from,
to AS tx_to,
evt_index
FROM all_events
WHERE evt_block_time >= DATE '2024-01-01'
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{{ config(
schema = 'yfx',
alias = 'perpetual_trades',
post_hook='{{ expose_spells(\'["base"]\',
"project",
"yfx",
\'["principatel"]\') }}'
)
}}

{% set yfx_perpetual_trade_models = [
ref('yfx_base_perpetual_trades')
] %}

SELECT *
FROM
(
{% for yfx_perpetual_model in yfx_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 {{ yfx_perpetual_model }}
{% if not loop.last %}
UNION ALL
{% endif %}
{% endfor %}
)
Loading