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

Avantis Protocol on Base #7450

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

{% 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
,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 {{ avantis_perpetual_trades }}
{% if not loop.last %}
UNION ALL
{% endif %}
{% endfor %}
)
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
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"
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
{{ config(
schema = 'avantis_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"]\',
"project",
"avantis",
\'["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 delta_prod.avantis_base.Trading_evt_OpenLimitPlaced

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 delta_prod.avantis_base.Trading_evt_MarginUpdated
),

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 delta_prod.base.transactions AS tx
ON perps.tx_hash = tx.hash
AND perps.block_number = tx.block_number
AND tx.block_time >= DATE '2024-01-01'
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,7 @@ base,2023-09-28,WETH,ETH/USD,0xad378c374f7996235e927e693edea32605c0a61f,Open Lon
base,2023-09-21,WETH,ETH/USD,0xad378c374f7996235e927e693edea32605c0a61f,Close Long,nether_fi,v1,0x57e31be2f717daf90242bb5e5187866e5702a5c40576d6cd81b962f30282c083
base,2024-01-03,snxUSD,Ethereum,0x0a2af931effd34b81ebcc57e3d3c9b1e1de1c9ce,close,Synthetix,3,0xb1dcb41bd0d90f418bcea2e7338df2290e50f1cd2185b2b75ec01fb7163fd240
base,2024-02-15,snxUSD,Ethereum,0x0a2af931effd34b81ebcc57e3d3c9b1e1de1c9ce,long,Synthetix,3,0xf0cae8268019a3e6bff055d496a5f52ed805f60f7582791d93944c026fd61b98
base,2024-02-08,DOT-USD,DOT-USD,0x5ff292d70ba9cd9e7ccb313782811b3d7120535f,long,avantis,1,0x2eea014578cae9fde1044f638996207b10cb66957ba46dc6025f3b9baf1b5c92
base,2024-02-08,LINK-USD,LINK-USD,0x5ff292d70ba9cd9e7ccb313782811b3d7120535f,long,avantis,1,0x29cb1f5da35ca745fbf2e7912d8e7eb89cbad2c3da627dd22c0f86c6db06f4a3
arbitrum,2023-02-17,,,0xda1a7ea276fbdb16ebabb5b38257b1d56b302e4a,open-long,vela_exchange,1,0x6a783688a2e013bfe84a6e7ae65dfd2f2c01e452b360a07a4bdf4d502ee8d187
arbitrum,2023-02-17,,,0xda1a7ea276fbdb16ebabb5b38257b1d56b302e4a,open-long,vela_exchange,1,0x94cacbb99ca4d6b11fb660dc71e6da77f98f6038752f71b158903c492578e34c
arbitrum,2023-02-17,,,0xda1a7ea276fbdb16ebabb5b38257b1d56b302e4a,open-long,vela_exchange,1,0x94cacbb99ca4d6b11fb660dc71e6da77f98f6038752f71b158903c492578e34c
10 changes: 10 additions & 0 deletions sources/avantis/avantis_base_sources.yml
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"
2 changes: 1 addition & 1 deletion sources/pika/optimism/pika_optimism_sources.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ sources:
- name: PikaPerpV2_evt_ClosePosition
description: "Details the swaps to close existing positions"

- name: pika_perp_v3_optimism # since this is latest pika contract
- name: pika_perp_v3_optimism # since this is latest pika contracts
description: Optimism decoded tables related to Pika Protocol v3
tables:
- name: PikaPerpV3_evt_NewPosition
Expand Down
Loading