-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Avantis Protocol on Base #7450
Open
PatelPrinci
wants to merge
37
commits into
duneanalytics:main
Choose a base branch
from
Lampros-Tech:base-avantis
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Avantis Protocol on Base #7450
Changes from 9 commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
4702e97
last head commit
PatelPrinci 8dab466
avantis code
PatelPrinci 669222b
Merge branch 'main' into base-avantis
PatelPrinci e6bd341
implemented new format for post hook
PatelPrinci debb57e
implemented the new post_hook format
PatelPrinci 4545ed1
removed blockchain filed in post hook
PatelPrinci d8c37b6
fixed the hash value mistake in meridian tx
PatelPrinci 10cb9c6
added incremental filters
PatelPrinci 40b6939
changed sources format in main perp table
PatelPrinci d26315a
added data tests in avantis schema.yml file
PatelPrinci c1b668a
added check base seed file
PatelPrinci aaf1213
added perpetual seed check
PatelPrinci 12d0006
added data tests in market address
PatelPrinci 1fa6a6e
added data tests in market address
PatelPrinci 5ca4eea
removed data tests in market address
PatelPrinci de837c7
removed the column in data tests
PatelPrinci ecb12a9
checked the data tests
PatelPrinci 55a6555
removed the extra character
PatelPrinci 898ef7f
added data tests in market
PatelPrinci ea78914
checking tests
PatelPrinci aa2ef15
Merge branch 'main' into base-avantis
PatelPrinci 57aea90
added post hook new format
PatelPrinci 995a964
post hook new format added
PatelPrinci 75cb7d7
space issue fixed
PatelPrinci 53927d9
incremental filters added
PatelPrinci 00a7439
space moved
PatelPrinci acbe0c1
changed sql files
PatelPrinci dae579a
sql files added
PatelPrinci 8d86bff
removed character
PatelPrinci 9bda2ab
added time date
PatelPrinci 2791c71
back to old code
PatelPrinci fd77b18
removed unwanted line
PatelPrinci e0c73d1
Updated Avantis Perpetual Trades Model
PatelPrinci 9d8b378
space changed
PatelPrinci 2997e4a
Merge branch 'main' into base-avantis
PatelPrinci ce3e9db
source format updated
PatelPrinci e9ff043
source format updated
PatelPrinci File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
...rly_spellbook/models/_sector/perpetual/projects/avantis/avantis_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 = 'avantis_perpetual_trades', | ||
alias = 'perpetual_trades', | ||
post_hook='{{ expose_spells(\'["base"]\', | ||
"project", | ||
"avantis", | ||
\'["princi"]\') }}' | ||
PatelPrinci marked this conversation as resolved.
Show resolved
Hide resolved
PatelPrinci marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
}} | ||
|
||
{% set avantis_base_perpetual_trade_models = [ | ||
ref('avantis_v1_base_perpetual_trades') | ||
] %} | ||
|
||
SELECT * | ||
FROM | ||
( | ||
{% for avantis_perpetual_trades in avantis_base_perpetual_trade_models %} | ||
SELECT | ||
blockchain | ||
,block_date | ||
PatelPrinci marked this conversation as resolved.
Show resolved
Hide resolved
|
||
,block_month | ||
,block_time | ||
,virtual_asset | ||
,underlying_asset | ||
PatelPrinci marked this conversation as resolved.
Show resolved
Hide resolved
|
||
,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 {{ avantis_perpetual_trades }} | ||
{% if not loop.last %} | ||
UNION ALL | ||
{% endif %} | ||
{% endfor %} | ||
) |
76 changes: 76 additions & 0 deletions
76
...subprojects/hourly_spellbook/models/_sector/perpetual/projects/avantis/avantis_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,76 @@ | ||
version: 2 | ||
|
||
models: | ||
- name: avantis_v1_base_perpetual_trades | ||
meta: | ||
blockchain: base | ||
sector: perpetual | ||
contributors: princi | ||
config: | ||
tags: ['base', 'perpetuals', 'perps', 'avantis','cross-chain'] | ||
description: > | ||
Perpetual swaps/trades table on avantis protocol across blockchains | ||
columns: | ||
- &blockchain | ||
jeff-dude marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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" | ||
- &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" |
208 changes: 208 additions & 0 deletions
208
..._spellbook/models/_sector/perpetual/projects/avantis/avantis_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,208 @@ | ||||||
{{ config( | ||||||
schema = 'avantis_base', | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
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 = "avantis", | ||||||
contributors = \'["princi"]\') }}' | ||||||
) | ||||||
}} | ||||||
|
||||||
-- {% set project_start_date = '2024-01-01' %} | ||||||
|
||||||
WITH trading_events AS ( | ||||||
-- OpenLimitPlaced events | ||||||
SELECT | ||||||
evt_block_time, | ||||||
evt_block_number, | ||||||
evt_tx_hash, | ||||||
evt_tx_from, | ||||||
evt_tx_to, | ||||||
evt_index, | ||||||
contract_address, | ||||||
trader, | ||||||
pairIndex, | ||||||
index as position_size, | ||||||
openPrice / 1e8 as price_usd, | ||||||
executionFee / 1e18 as fee_usd, | ||||||
isBuy, | ||||||
'limit_order' as event_type | ||||||
FROM {{ source('avantis_base', 'Trading_evt_OpenLimitPlaced') }} | ||||||
{% if is_incremental() %} | ||||||
WHERE {{ incremental_predicate('evt_block_time') }} | ||||||
{% endif %} | ||||||
|
||||||
jeff-dude marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
UNION ALL | ||||||
|
||||||
-- MarginUpdated events | ||||||
SELECT | ||||||
evt_block_time, | ||||||
evt_block_number, | ||||||
evt_tx_hash, | ||||||
evt_tx_from, | ||||||
evt_tx_to, | ||||||
evt_index, | ||||||
contract_address, | ||||||
trader, | ||||||
pairIndex, | ||||||
index as position_size, | ||||||
NULL as price_usd, -- No direct price equivalent | ||||||
marginFees / 1e18 as fee_usd, | ||||||
CASE | ||||||
WHEN newTrade = 'Long' THEN true | ||||||
WHEN newTrade = 'Short' THEN false | ||||||
ELSE NULL | ||||||
END as isBuy, | ||||||
'margin_update' as event_type | ||||||
FROM {{ source('avantis_base', 'Trading_evt_MarginUpdated') }} | ||||||
{% if is_incremental() %} | ||||||
WHERE {{ incremental_predicate('evt_block_time') }} | ||||||
{% 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' | ||||||
-- Add more trading pairs as needed | ||||||
ELSE CONCAT('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' | ||||||
-- Add more trading pairs as needed | ||||||
ELSE CONCAT('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' | ||||||
-- Add more trading pairs as needed | ||||||
ELSE CONCAT('pair_index_', CAST(pairIndex as VARCHAR)) | ||||||
END AS market, | ||||||
|
||||||
contract_address AS market_address, | ||||||
COALESCE(price_usd * position_size, position_size) AS volume_usd, | ||||||
fee_usd, | ||||||
position_size AS margin_usd, | ||||||
|
||||||
CASE | ||||||
WHEN event_type = 'limit_order' THEN | ||||||
CASE WHEN isBuy THEN 'long' ELSE 'short' END | ||||||
WHEN event_type = 'margin_update' THEN | ||||||
CASE WHEN isBuy THEN 'update_long' ELSE 'update_short' END | ||||||
END AS trade, | ||||||
|
||||||
'avantis' AS project, | ||||||
'1' AS version, | ||||||
'avantis' AS frontend, | ||||||
trader, | ||||||
COALESCE(price_usd * position_size, position_size) AS volume_raw, | ||||||
evt_tx_hash AS tx_hash, | ||||||
evt_index | ||||||
FROM trading_events | ||||||
) | ||||||
|
||||||
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 {{ source('base', 'transactions') }} AS tx | ||||||
jeff-dude marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
ON perps.tx_hash = tx.hash | ||||||
AND perps.block_number = tx.block_number | ||||||
AND tx.block_time >= DATE '2024-01-01' | ||||||
|
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: avantis_base | ||
description: base decoded tables related to Avantis | ||
tables: | ||
- name: Trading_evt_OpenLimitPlaced | ||
description: "Details the new positions opened" | ||
- name: Trading_evt_MarginUpdated | ||
description: "Details the swaps to close existing positions" |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.