forked from duneanalytics/spellbook
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Gains Network Perpetual for Base (duneanalytics#7437)
* moved to previous hash * changed model name * Fixed gains_network_schema.yml * Fixed gains_network_v1_base_perpetual_trades.sql * Changed JSON Parsing Query and Seed File * added post hook new format and source syntax * fixed space issue * added incremental filters * added blockchain in post hook * added incremental filters and added CTE * removed the extra comments * fixed the schema and tabs * rename file and added models details
- Loading branch information
1 parent
b5008b6
commit 90c8baf
Showing
5 changed files
with
396 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
...llbook/models/_sector/perpetual/projects/gains_network/gains_network_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 = 'gains_network', | ||
alias = 'perpetual_trades', | ||
post_hook='{{ expose_spells(blockchains = \'["base"]\', | ||
spell_type = "project", | ||
spell_name = "gains_network", | ||
contributors = \'["princi"]\') }}' | ||
) | ||
}} | ||
|
||
{% set gains_network_base_perpetual_trade_models = [ | ||
ref('gains_network_v1_base_perpetual_trades') | ||
] %} | ||
|
||
SELECT * | ||
FROM | ||
( | ||
{% for gains_network_perpetual_trades in gains_network_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 {{ gains_network_perpetual_trades }} | ||
{% if not loop.last %} | ||
UNION ALL | ||
{% endif %} | ||
{% endfor %} | ||
) |
117 changes: 117 additions & 0 deletions
117
...hourly_spellbook/models/_sector/perpetual/projects/gains_network/gains_network_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,117 @@ | ||
version: 2 | ||
|
||
models: | ||
- name: gains_network_v1_base_perpetual_trades | ||
meta: | ||
blockchain: base | ||
sector: perpetual | ||
contributors: princi | ||
config: | ||
tags: ['base', 'perpetuals', 'perps', 'gains_network','cross-chain'] | ||
description: > | ||
Perpetual swaps/trades table on gains_network 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 transaction" | ||
- &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" | ||
|
||
- name: gains_network_perpetual_trades | ||
meta: | ||
blockchain: base | ||
sector: perpetual | ||
contributors: princi | ||
config: | ||
tags: ['base', gains network, 'perpetuals', 'perps'] | ||
description: > | ||
All gains network perpetual trades on Base | ||
columns: | ||
- *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 | ||
|
221 changes: 221 additions & 0 deletions
221
...odels/_sector/perpetual/projects/gains_network/gains_network_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,221 @@ | ||
{{ config( | ||
schema = 'gains_network_v1_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(blockchains = \'["base"]\', | ||
spell_type = "project", | ||
spell_name = "gains_network", | ||
contributors = \'["princi"]\') }}' | ||
) | ||
}} | ||
|
||
-- {% set project_start_date = '2024-01-01' %} | ||
|
||
WITH position_changes AS ( | ||
-- Position Size Decreases | ||
SELECT | ||
evt_block_time, | ||
evt_block_number, | ||
evt_tx_hash, | ||
evt_tx_from, | ||
evt_tx_to, | ||
evt_index, | ||
contract_address, | ||
trader, | ||
pairIndex, | ||
long, | ||
collateralDelta, | ||
collateralPriceUsd, | ||
oraclePrice, | ||
leverageDelta, | ||
"values" as value_data, | ||
'decrease' as action | ||
FROM {{source('gains_network_base','GNSMultiCollatDiamond_evt_PositionSizeDecreaseExecuted')}} | ||
{% if not is_incremental() %} | ||
WHERE evt_block_time >= DATE '{{project_start_date}}' | ||
{% else %} | ||
WHERE {{ incremental_predicate('evt_block_time') }} | ||
{% endif %} | ||
UNION ALL | ||
|
||
-- Position Size Increases | ||
SELECT | ||
evt_block_time, | ||
evt_block_number, | ||
evt_tx_hash, | ||
evt_tx_from, | ||
evt_tx_to, | ||
evt_index, | ||
contract_address, | ||
trader, | ||
pairIndex, | ||
long, | ||
collateralDelta, | ||
collateralPriceUsd, | ||
oraclePrice, | ||
leverageDelta, | ||
"values" as value_data, | ||
'increase' as action | ||
FROM {{ source('gains_network_base','GNSMultiCollatDiamond_evt_PositionSizeIncreaseExecuted')}} | ||
{% if not is_incremental() %} | ||
WHERE evt_block_time >= DATE '{{project_start_date}}' | ||
{% else %} | ||
WHERE {{ incremental_predicate('evt_block_time') }} | ||
{% endif %} | ||
), | ||
|
||
transactions_filtered AS ( | ||
SELECT | ||
hash, | ||
block_number, | ||
"from", | ||
"to", | ||
block_time | ||
FROM {{ source('base', 'transactions') }} | ||
WHERE {% if is_incremental() %} | ||
{{ incremental_predicate('block_time') }} | ||
{% else %} | ||
block_time >= TIMESTAMP '{{project_start_date}}' | ||
{% endif %} | ||
), | ||
|
||
perps AS ( | ||
SELECT | ||
evt_block_time AS block_time, | ||
evt_block_number AS block_number, | ||
CASE pairIndex | ||
WHEN 0 THEN 'BTC-USD' | ||
WHEN 1 THEN 'ETH-USD' | ||
WHEN 2 THEN 'LINK-USD' | ||
WHEN 3 THEN 'DOGE-USD' | ||
WHEN 4 THEN 'MATIC-USD' | ||
WHEN 5 THEN 'ADA-USD' | ||
WHEN 6 THEN 'SUSHI-USD' | ||
WHEN 7 THEN 'AAVE-USD' | ||
WHEN 8 THEN 'ALGO-USD' | ||
WHEN 9 THEN 'BAT-USD' | ||
WHEN 10 THEN 'COMP-USD' | ||
WHEN 11 THEN 'DOT-USD' | ||
WHEN 12 THEN 'EOS-USD' | ||
WHEN 13 THEN 'LTC-USD' | ||
WHEN 14 THEN 'MANA-USD' | ||
WHEN 15 THEN 'OMG-USD' | ||
WHEN 16 THEN 'SNX-USD' | ||
WHEN 17 THEN 'UNI-USD' | ||
WHEN 18 THEN 'XLM-USD' | ||
WHEN 19 THEN 'XRP-USD' | ||
WHEN 20 THEN 'ZEC-USD' | ||
WHEN 31 THEN 'LUNA-USD' | ||
WHEN 32 THEN 'YFI-USD' | ||
WHEN 33 THEN 'SOL-USD' | ||
ELSE 'pair_index_' || CAST(pairIndex AS VARCHAR) | ||
END AS virtual_asset, | ||
|
||
CASE pairIndex | ||
WHEN 0 THEN 'BTC-USD' | ||
WHEN 1 THEN 'ETH-USD' | ||
WHEN 2 THEN 'LINK-USD' | ||
WHEN 3 THEN 'DOGE-USD' | ||
WHEN 4 THEN 'MATIC-USD' | ||
WHEN 5 THEN 'ADA-USD' | ||
WHEN 6 THEN 'SUSHI-USD' | ||
WHEN 7 THEN 'AAVE-USD' | ||
WHEN 8 THEN 'ALGO-USD' | ||
WHEN 9 THEN 'BAT-USD' | ||
WHEN 10 THEN 'COMP-USD' | ||
WHEN 11 THEN 'DOT-USD' | ||
WHEN 12 THEN 'EOS-USD' | ||
WHEN 13 THEN 'LTC-USD' | ||
WHEN 14 THEN 'MANA-USD' | ||
WHEN 15 THEN 'OMG-USD' | ||
WHEN 16 THEN 'SNX-USD' | ||
WHEN 17 THEN 'UNI-USD' | ||
WHEN 18 THEN 'XLM-USD' | ||
WHEN 19 THEN 'XRP-USD' | ||
WHEN 20 THEN 'ZEC-USD' | ||
WHEN 31 THEN 'LUNA-USD' | ||
WHEN 32 THEN 'YFI-USD' | ||
WHEN 33 THEN 'SOL-USD' | ||
ELSE 'pair_index_' || CAST(pairIndex AS VARCHAR) | ||
END AS underlying_asset, | ||
|
||
CASE pairIndex | ||
WHEN 0 THEN 'BTC-USD' | ||
WHEN 1 THEN 'ETH-USD' | ||
WHEN 2 THEN 'LINK-USD' | ||
WHEN 3 THEN 'DOGE-USD' | ||
WHEN 4 THEN 'MATIC-USD' | ||
WHEN 5 THEN 'ADA-USD' | ||
WHEN 6 THEN 'SUSHI-USD' | ||
WHEN 7 THEN 'AAVE-USD' | ||
WHEN 8 THEN 'ALGO-USD' | ||
WHEN 9 THEN 'BAT-USD' | ||
WHEN 10 THEN 'COMP-USD' | ||
WHEN 11 THEN 'DOT-USD' | ||
WHEN 12 THEN 'EOS-USD' | ||
WHEN 13 THEN 'LTC-USD' | ||
WHEN 14 THEN 'MANA-USD' | ||
WHEN 15 THEN 'OMG-USD' | ||
WHEN 16 THEN 'SNX-USD' | ||
WHEN 17 THEN 'UNI-USD' | ||
WHEN 18 THEN 'XLM-USD' | ||
WHEN 19 THEN 'XRP-USD' | ||
WHEN 20 THEN 'ZEC-USD' | ||
WHEN 31 THEN 'LUNA-USD' | ||
WHEN 32 THEN 'YFI-USD' | ||
WHEN 33 THEN 'SOL-USD' | ||
ELSE 'pair_index_' || CAST(pairIndex AS VARCHAR) | ||
END AS market, | ||
|
||
contract_address AS market_address, | ||
(collateralDelta * collateralPriceUsd * leverageDelta) / 1e36 AS volume_usd, | ||
CAST(JSON_EXTRACT(value_data, '$.vaultFeeCollateral') AS double) / 1e18 AS fee_usd, | ||
collateralDelta / 1e18 AS margin_usd, | ||
|
||
CASE | ||
WHEN action = 'increase' AND long = true THEN 'long' | ||
WHEN action = 'increase' AND long = false THEN 'short' | ||
WHEN action = 'decrease' AND long = true THEN 'close_long' | ||
WHEN action = 'decrease' AND long = false THEN 'close_short' | ||
END AS trade, | ||
|
||
'gains_network' AS project, | ||
'1' AS version, | ||
'gains_network' AS frontend, | ||
trader, | ||
collateralDelta * leverageDelta AS volume_raw, | ||
evt_tx_hash AS tx_hash, | ||
evt_index | ||
FROM position_changes | ||
) | ||
|
||
SELECT | ||
'base' AS blockchain, | ||
CAST(date_trunc('DAY', perps.block_time) AS date) AS block_date, | ||
CAST(date_trunc('MONTH', perps.block_time) AS date) AS block_month, | ||
perps.block_time, | ||
perps.virtual_asset, | ||
perps.underlying_asset, | ||
perps.market, | ||
perps.market_address, | ||
perps.volume_usd, | ||
perps.fee_usd, | ||
perps.margin_usd, | ||
perps.trade, | ||
perps.project, | ||
perps.version, | ||
perps.frontend, | ||
perps.trader, | ||
CAST(perps.volume_raw as UINT256) as volume_raw, | ||
perps.tx_hash, | ||
tx."from" AS tx_from, | ||
tx."to" AS tx_to, | ||
perps.evt_index | ||
FROM perps | ||
INNER JOIN transactions_filtered tx | ||
ON perps.tx_hash = tx.hash | ||
AND perps.block_number = tx.block_number |
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
Oops, something went wrong.