Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
PatelPrinci authored Dec 11, 2024
2 parents 9d25c9c + 4bf03f7 commit aa735b3
Show file tree
Hide file tree
Showing 34 changed files with 496 additions and 107 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{{ config(
schema = 'cow_protocol_arbitrum',
alias='trades',
materialized='incremental',
partition_by = ['block_month'],
unique_key = ['tx_hash', 'order_uid', 'evt_index'],
on_schema_change='sync_all_columns',
file_format ='delta',
incremental_strategy='merge',
incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.block_time')],
post_hook='{{ expose_spells(\'["arbitrum"]\',
"project",
"cow_protocol",
\'["olgafetisova"]\') }}'
schema='cow_protocol_arbitrum',
alias='trades',
materialized='incremental',
partition_by = ['block_month'],
unique_key = ['tx_hash', 'order_uid', 'evt_index'],
on_schema_change='sync_all_columns',
file_format ='delta',
incremental_strategy='merge',
incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.block_time')],
post_hook='{{ expose_spells(\'["arbitrum"]\',
"project",
"cow_protocol",
\'["olgafetisova"]\') }}'
)
}}

Expand All @@ -26,17 +26,17 @@ trades_with_prices AS (
evt_block_number as block_number,
evt_tx_hash as tx_hash,
evt_index,
settlement.contract_address as project_contract_address,
trade.contract_address as project_contract_address,
owner as trader,
orderUid as order_uid,
sellToken as sell_token,
buyToken as buy_token,
(sellAmount - feeAmount) as sell_amount,
sellAmount - feeAmount as sell_amount,
buyAmount as buy_amount,
feeAmount as fee_amount,
ps.price as sell_price,
pb.price as buy_price
FROM {{ source('gnosis_protocol_v2_arbitrum', 'GPv2Settlement_evt_Trade') }} settlement
FROM {{ source('gnosis_protocol_v2_arbitrum', 'GPv2Settlement_evt_Trade') }} trade
LEFT OUTER JOIN {{ source('prices', 'usd') }} as ps
ON sellToken = ps.contract_address
AND ps.minute = date_trunc('minute', evt_block_time)
Expand All @@ -45,7 +45,12 @@ trades_with_prices AS (
AND {{ incremental_predicate('ps.minute') }}
{% endif %}
LEFT OUTER JOIN {{ source('prices', 'usd') }} as pb
ON pb.contract_address = buyToken
ON pb.contract_address = (
CASE
WHEN buyToken = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
THEN 0x82af49447d8a07e3bd95bd0d56f35241523fbab1
ELSE buyToken
END)
AND pb.minute = date_trunc('minute', evt_block_time)
AND pb.blockchain = 'arbitrum'
{% if is_incremental() %}
Expand Down Expand Up @@ -73,7 +78,8 @@ trades_with_token_units as (
END) as sell_token,
buy_token as buy_token_address,
(CASE
WHEN tb.symbol IS NULL THEN cast(buy_token AS varchar)
WHEN tb.symbol IS NULL THEN cast(buy_token as varchar)
WHEN buy_token = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee THEN 'ETH'
ELSE tb.symbol
END) as buy_token,
sell_amount / pow(10, ts.decimals) as units_sold,
Expand All @@ -89,7 +95,12 @@ trades_with_token_units as (
LEFT OUTER JOIN {{ source('tokens', 'erc20') }} ts
ON ts.blockchain='arbitrum' AND ts.contract_address = sell_token
LEFT OUTER JOIN {{ source('tokens', 'erc20') }} tb
ON tb.blockchain='arbitrum' AND tb.contract_address = buy_token
ON tb.blockchain='arbitrum' AND tb.contract_address =
(CASE
WHEN buy_token = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
THEN 0x82af49447d8a07e3bd95bd0d56f35241523fbab1
ELSE buy_token
END)
),
sorted_orders as (
select
Expand Down Expand Up @@ -146,6 +157,25 @@ uid_to_app_id as (
i = j
),

eth_flow_senders as (
select
sender,
bytearray_concat(
bytearray_concat(
output_orderHash,
bytearray_substring(event.contract_address, 1, 20)
),
0xffffffff
) AS order_uid
from {{ source('cow_protocol_arbitrum', 'CoWSwapEthFlow_evt_OrderPlacement') }} event
inner join {{ source('cow_protocol_arbitrum', 'CoWSwapEthFlow_call_createOrder') }} call
on call_block_number = evt_block_number
and call_tx_hash = evt_tx_hash
{% if is_incremental() %}
where {{ incremental_predicate('evt_block_time') }}
{% endif %}
),

valued_trades as (
SELECT block_date,
block_month,
Expand All @@ -156,9 +186,10 @@ valued_trades as (
ARRAY[-1] as trace_address,
project_contract_address,
trades.order_uid,
trader,
-- ETH Flow orders have trader = sender of orderCreation.
case when sender is not null then sender else trader end as trader,
sell_token_address,
sell_token,
case when sender is not null then 'ETH' else sell_token end as sell_token,
buy_token_address,
buy_token,
case
Expand Down Expand Up @@ -216,11 +247,12 @@ valued_trades as (
JOIN uid_to_app_id
ON uid = trades.order_uid
AND hash=tx_hash
LEFT OUTER JOIN eth_flow_senders efs
ON trades.order_uid = efs.order_uid
)

select
*,
-- Relative surplus (in %) is the difference between limit price and executed price as a ratio of the limit price.
-- Absolute surplus (in USD) is relative surplus multiplied with the value of the trade
usd_value * (atoms_bought * limit_sell_amount - atoms_sold * limit_buy_amount) / (atoms_bought * limit_sell_amount) as surplus_usd
select *,
-- Relative surplus (in %) is the difference between limit price and executed price as a ratio of the limit price.
-- Absolute surplus (in USD) is relative surplus multiplied with the value of the trade
usd_value * (atoms_bought * limit_sell_amount - atoms_sold * limit_buy_amount) / (atoms_bought * limit_sell_amount) as surplus_usd
from valued_trades
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{{ config(
schema = 'cow_protocol_gnosis',
alias='trades',
materialized='incremental',
partition_by = ['block_month'],
unique_key = ['tx_hash', 'order_uid', 'evt_index'],
on_schema_change='sync_all_columns',
file_format ='delta',
incremental_strategy='merge',
incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.block_time')],
post_hook='{{ expose_spells(\'["gnosis"]\',
"project",
"cow_protocol",
\'["bh2smith", "gentrexha", "olgafetisova"]\') }}'
schema='cow_protocol_gnosis',
alias='trades',
materialized='incremental',
partition_by = ['block_month'],
unique_key = ['tx_hash', 'order_uid', 'evt_index'],
on_schema_change='sync_all_columns',
file_format ='delta',
incremental_strategy='merge',
incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.block_time')],
post_hook='{{ expose_spells(\'["gnosis"]\',
"project",
"cow_protocol",
\'["bh2smith", "gentrexha", "olgafetisova"]\') }}'
)
}}

Expand All @@ -26,17 +26,17 @@ trades_with_prices AS (
evt_block_number as block_number,
evt_tx_hash as tx_hash,
evt_index,
settlement.contract_address as project_contract_address,
trade.contract_address as project_contract_address,
owner as trader,
orderUid as order_uid,
sellToken as sell_token,
buyToken as buy_token,
(sellAmount - feeAmount) as sell_amount,
sellAmount - feeAmount as sell_amount,
buyAmount as buy_amount,
feeAmount as fee_amount,
ps.price as sell_price,
pb.price as buy_price
FROM {{ source('gnosis_protocol_v2_gnosis', 'GPv2Settlement_evt_Trade') }} settlement
FROM {{ source('gnosis_protocol_v2_gnosis', 'GPv2Settlement_evt_Trade') }} trade
LEFT OUTER JOIN {{ source('prices', 'usd') }} as ps
ON sellToken = ps.contract_address
AND ps.minute = date_trunc('minute', evt_block_time)
Expand Down Expand Up @@ -78,7 +78,7 @@ trades_with_token_units as (
END) as sell_token,
buy_token as buy_token_address,
(CASE
WHEN tb.symbol IS NULL THEN cast(buy_token AS varchar)
WHEN tb.symbol IS NULL THEN cast(buy_token as varchar)
WHEN buy_token = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee THEN 'xDAI'
ELSE tb.symbol
END) as buy_token,
Expand Down Expand Up @@ -157,6 +157,25 @@ uid_to_app_id as (
i = j
),

eth_flow_senders as (
select
sender,
bytearray_concat(
bytearray_concat(
output_orderHash,
bytearray_substring(event.contract_address, 1, 20)
),
0xffffffff
) AS order_uid
from {{ source('cow_protocol_gnosis', 'CoWSwapEthFlow_evt_OrderPlacement') }} event
inner join {{ source('cow_protocol_gnosis', 'CoWSwapEthFlow_call_createOrder') }} call
on call_block_number = evt_block_number
and call_tx_hash = evt_tx_hash
{% if is_incremental() %}
where {{ incremental_predicate('evt_block_time') }}
{% endif %}
),

valued_trades as (
SELECT block_date,
block_month,
Expand All @@ -167,9 +186,10 @@ valued_trades as (
ARRAY[-1] as trace_address,
project_contract_address,
trades.order_uid,
trader,
-- ETH Flow orders have trader = sender of orderCreation.
case when sender is not null then sender else trader end as trader,
sell_token_address,
sell_token,
case when sender is not null then 'xDAI' else sell_token end as sell_token,
buy_token_address,
buy_token,
case
Expand Down Expand Up @@ -227,11 +247,12 @@ valued_trades as (
JOIN uid_to_app_id
ON uid = trades.order_uid
AND hash=tx_hash
LEFT OUTER JOIN eth_flow_senders efs
ON trades.order_uid = efs.order_uid
)

select
*,
-- Relative surplus (in %) is the difference between limit price and executed price as a ratio of the limit price.
-- Absolute surplus (in USD) is relative surplus multiplied with the value of the trade
usd_value * (atoms_bought * limit_sell_amount - atoms_sold * limit_buy_amount) / (atoms_bought * limit_sell_amount) as surplus_usd
select *,
-- Relative surplus (in %) is the difference between limit price and executed price as a ratio of the limit price.
-- Absolute surplus (in USD) is relative surplus multiplied with the value of the trade
usd_value * (atoms_bought * limit_sell_amount - atoms_sold * limit_buy_amount) / (atoms_bought * limit_sell_amount) as surplus_usd
from valued_trades
2 changes: 2 additions & 0 deletions dbt_subprojects/dex/models/dex_info.sql
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,6 @@ FROM (VALUES
, ('flashliquidity', 'Flashliquidity', 'Direct', 'flashliquidity')
, ('akronswap', 'Akronswap', 'Direct', 'AkronFinance')
, ('saddle_finance', 'Saddle Finance', 'Direct', 'saddlefinance')
, ('katana', 'Katana', 'Direct', 'AxieInfinity')
, ('jetswap', 'JetSwap', 'Direct', 'Jetfuelfinance')
) AS temp_table (project, name, marketplace_type, x_username)
4 changes: 2 additions & 2 deletions dbt_subprojects/dex/models/trades/_schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ models:
- name: dex_trades
meta:
docs_slug: /curated/trading/DEX/dex-trades
blockchain: arbitrum, avalanche_c, base, bnb, celo, ethereum, fantom, gnosis, kaia, optimism, polygon, scroll, zksync, linea, blast, sei
blockchain: arbitrum, avalanche_c, base, bnb, celo, ethereum, fantom, gnosis, kaia, optimism, polygon, scroll, zksync, linea, blast, sei, ronin
sector: dex
short_description: The `dex.trades` table captures detailed data on trades executed via decentralized exchanges (DEXs). This table contains a detailed breakdown of trade execution containing one or many trades per transaction.
contributors: 0xRob, hosuke, jeff-dude, tomfutago
Expand Down Expand Up @@ -102,7 +102,7 @@ models:

- name: dex_base_trades
meta:
blockchain: arbitrum, avalanche_c, base, bnb, celo, ethereum, fantom, gnosis, kaia,optimism, polygon, scroll, zksync, linea, blast, sei
blockchain: arbitrum, avalanche_c, base, bnb, celo, ethereum, fantom, gnosis, kaia,optimism, polygon, scroll, zksync, linea, blast, sei, ronin
sector: dex
contributors: 0xRob, hosuke, jeff-dude, tomfutago, viniabussafi
config:
Expand Down
1 change: 1 addition & 0 deletions dbt_subprojects/dex/models/trades/dex_base_trades.sql
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
, ref('dex_nova_base_trades')
, ref('dex_optimism_base_trades')
, ref('dex_polygon_base_trades')
, ref('dex_ronin_base_trades')
, ref('dex_scroll_base_trades')
, ref('dex_sei_base_trades')
, ref('dex_worldchain_base_trades')
Expand Down
1 change: 1 addition & 0 deletions dbt_subprojects/dex/models/trades/dex_trades.sql
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
, "nova"
, "optimism"
, "polygon"
, "ronin"
, "scroll"
, "sei"
, "zkevm"
Expand Down
18 changes: 18 additions & 0 deletions dbt_subprojects/dex/models/trades/gnosis/_schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,21 @@ models:
seed_file: ref('honeyswap_gnosis_base_trades_seed')
filter:
version: 2

- name: elk_finance_gnosis_base_trades
meta:
blockchain: gnosis
sector: dex
project: elk_finance
contributors: blanchemaria6
config:
tags: ['gnosis', 'dex', 'trades', 'elk_finance']
description: "elk finance gnosis base trades"
data_tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- tx_hash
- evt_index
- check_dex_base_trades_seed:
seed_file: ref('elk_finance_gnosis_base_trades_seed')

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
, ref('sushiswap_v2_gnosis_base_trades')
, ref('balancer_v2_gnosis_base_trades')
, ref('honeyswap_v2_gnosis_base_trades')
, ref('elk_finance_gnosis_base_trades')
] %}

WITH base_union AS (
Expand Down Expand Up @@ -48,4 +49,4 @@ WITH base_union AS (
, blockchain = 'gnosis'
, columns = ['from', 'to', 'index']
)
}}
}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{{
config(
schema = 'elk_finance_gnosis',
alias = 'base_trades',
materialized = 'incremental',
file_format = 'delta',
incremental_strategy = 'merge',
unique_key = ['tx_hash', 'evt_index'],
incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.block_time')]
)
}}

{{
uniswap_compatible_v2_trades(
blockchain = 'gnosis',
project = 'elk_finance',
version = '1',
Pair_evt_Swap = source('elk_finance_gnosis', 'ElkPair_evt_Swap'),
Factory_evt_PairCreated = source('elk_finance_gnosis', 'ElkFactory_evt_PairCreated')
)
}}
18 changes: 18 additions & 0 deletions dbt_subprojects/dex/models/trades/polygon/_schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,21 @@ models:
- evt_index
- check_dex_base_trades_seed:
seed_file: ref('dfyn_polygon_base_trades_seed')

- name: jetswap_polygon_base_trades
meta:
blockchain: polygon
sector: dex
project: jetswap
contributors: intensodefi
config:
tags: [ 'polygon', 'dex', 'trades', 'jetswap' ]
description: "jetswap polygon base trades"
data_tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- tx_hash
- evt_index
- check_dex_base_trades_seed:
seed_file: ref('jetswap_polygon_base_trades_seed')

Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
, ref('gridex_polygon_base_trades')
, ref('swaap_v2_polygon_base_trades')
, ref('dfyn_polygon_base_trades')
, ref('jetswap_polygon_base_trades')
] %}

WITH base_union AS (
Expand Down
Loading

0 comments on commit aa735b3

Please sign in to comment.