Skip to content

Commit

Permalink
distinct function added
Browse files Browse the repository at this point in the history
  • Loading branch information
PatelPrinci committed Jan 30, 2025
2 parents 25687f4 + 67b478e commit bd43c7a
Show file tree
Hide file tree
Showing 42 changed files with 1,649 additions and 16 deletions.
54 changes: 53 additions & 1 deletion dbt_subprojects/dex/models/_projects/balancer/pools/_schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -317,4 +317,56 @@ models:
- *tx_index
- *block_time
- *block_number
- *swap_fee_percentage
- *swap_fee_percentage

- name: balancer_v3_arbitrum_pools_fees
meta:
blockchain: arbitrum
project: balancer_v3
contributors: viniabussafi
config:
tags: ['arbitrum', 'balancer', 'pools_fees']
description: >
Balancer v3 swap fees stored at the pool level.
data_tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- block_number
- tx_hash
- index
columns:
- *blockchain
- *version
- *contract_address
- *tx_hash
- *index
- *tx_index
- *block_time
- *block_number
- *swap_fee_percentage

- name: balancer_v3_base_pools_fees
meta:
blockchain: base
project: balancer_v3
contributors: viniabussafi
config:
tags: ['base', 'balancer', 'pools_fees']
description: >
Balancer v3 swap fees stored at the pool level.
data_tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- block_number
- tx_hash
- index
columns:
- *blockchain
- *version
- *contract_address
- *tx_hash
- *index
- *tx_index
- *block_time
- *block_number
- *swap_fee_percentage
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{{
config(
schema = 'balancer_v3_arbitrum',
alias = 'pools_fees',
materialized = 'incremental',
file_format = 'delta',
incremental_strategy = 'merge',
incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.block_time')],
unique_key = ['block_number', 'tx_hash', 'index']
)
}}

SELECT
'arbitrum' AS blockchain,
'3' AS version,
pool AS contract_address,
evt_tx_hash AS tx_hash,
evt_index AS tx_index,
evt_index AS index,
evt_block_time AS block_time,
evt_block_number AS block_number,
swapFeePercentage AS swap_fee_percentage
FROM {{ source ('balancer_v3_arbitrum', 'Vault_evt_SwapFeePercentageChanged') }}
WHERE 1 = 1
{% if is_incremental() %}
AND {{ incremental_predicate('evt_block_time') }}
{% endif %}

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
ref('balancer_v2_polygon_pools_fees'),
ref('balancer_v2_zkevm_pools_fees'),
ref('balancer_v3_ethereum_pools_fees'),
ref('balancer_v3_gnosis_pools_fees')
ref('balancer_v3_gnosis_pools_fees'),
ref('balancer_v3_arbitrum_pools_fees'),
ref('balancer_v3_base_pools_fees')
] %}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{{
config(
schema = 'balancer_v3_base',
alias = 'pools_fees',
materialized = 'incremental',
file_format = 'delta',
incremental_strategy = 'merge',
incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.block_time')],
unique_key = ['block_number', 'tx_hash', 'index']
)
}}

SELECT
'base' AS blockchain,
'3' AS version,
pool AS contract_address,
evt_tx_hash AS tx_hash,
evt_index AS tx_index,
evt_index AS index,
evt_block_time AS block_time,
evt_block_number AS block_number,
swapFeePercentage AS swap_fee_percentage
FROM {{ source ('balancer_v3_base', 'Vault_evt_SwapFeePercentageChanged') }}
WHERE 1 = 1
{% if is_incremental() %}
AND {{ incremental_predicate('evt_block_time') }}
{% endif %}

Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,43 @@ models:
- *tx_hash
- *tx_from
- *tx_to
- *evt_index
- *evt_index

- name: balancer_v3_arbitrum_trades
meta:
blockchain: arbitrum
sector: dex
contributors: viniabussafi
config:
tags: [ 'balancer', 'arbitrum', 'amm', 'trades', 'dex']
description: >
DEX trades on balancer v3 on Arbitrum
columns:
- *blockchain
- *project
- *version
- *block_month
- *block_date
- *block_time
- *block_number
- *token_bought_symbol
- *token_sold_symbol
- *token_pair
- *token_bought_amount
- *token_sold_amount
- *token_bought_amount_raw
- *token_sold_amount_raw
- *amount_usd
- *token_bought_address
- *token_sold_address
- *taker
- *maker
- *project_contract_address
- *pool_symbol
- *pool_type
- *pool_id
- *swap_fee
- *tx_hash
- *tx_from
- *tx_to
- *evt_index
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@


{% set balancer_models = [
ref('balancer_v2_arbitrum_trades')
ref('balancer_v2_arbitrum_trades'),
ref('balancer_v3_arbitrum_trades')
] %}

SELECT *
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
{{
config(
schema = 'balancer_v3_arbitrum',
alias = 'trades',
materialized = 'view',
post_hook = '{{ expose_spells(\'["arbitrum"]\',
spell_type = "project",
spell_name = "balancer",
contributors = \'["viniabussafi"]\') }}'
)
}}

WITH
dexs_arbitrum AS (
SELECT
tx_hash,
evt_index,
pool_id,
swap_fee,
pool_symbol,
pool_type
FROM {{ ref('balancer_v3_arbitrum_base_trades') }}
),

dexs AS (
SELECT
dexs.blockchain,
dexs.project,
dexs.version,
dexs.block_month,
dexs.block_date,
dexs.block_time,
dexs.block_number,
dexs.token_bought_symbol,
dexs.token_sold_symbol,
dexs.token_pair,
dexs.token_bought_amount,
dexs.token_sold_amount,
dexs.token_bought_amount_raw,
dexs.token_sold_amount_raw,
dexs.amount_usd,
dexs.token_bought_address,
dexs.token_sold_address,
dexs.taker,
dexs.maker,
dexs.project_contract_address,
dexs.tx_hash,
dexs.tx_from,
dexs.tx_to,
dexs.evt_index,
dexs_arbitrum.pool_id,
dexs_arbitrum.swap_fee,
dexs_arbitrum.pool_symbol,
dexs_arbitrum.pool_type
FROM {{ ref('dex_trades') }} dexs
INNER JOIN dexs_arbitrum
ON dexs.tx_hash = dexs_arbitrum.tx_hash
AND dexs.evt_index = dexs_arbitrum.evt_index
WHERE dexs.blockchain = 'arbitrum'
AND dexs.project = 'balancer'
AND dexs.version = '3'
),

bpa AS (
SELECT
dexs.block_number,
dexs.tx_hash,
dexs.evt_index,
bpt_prices.contract_address,
dexs.block_time,
MAX(bpt_prices.day) AS bpa_max_block_date
FROM dexs
LEFT JOIN {{ source('balancer', 'bpt_prices') }} bpt_prices
ON bpt_prices.contract_address = dexs.token_bought_address
AND bpt_prices.day <= DATE_TRUNC('day', dexs.block_time)
AND bpt_prices.blockchain = 'arbitrum'
GROUP BY 1, 2, 3, 4, 5
),

bpb AS (
SELECT
dexs.block_number,
dexs.tx_hash,
dexs.evt_index,
bpt_prices.contract_address,
dexs.block_time,
MAX(bpt_prices.day) AS bpb_max_block_date
FROM dexs
LEFT JOIN {{ source('balancer', 'bpt_prices') }} bpt_prices
ON bpt_prices.contract_address = dexs.token_sold_address
AND bpt_prices.day <= DATE_TRUNC('day', dexs.block_time)
AND bpt_prices.blockchain = 'arbitrum'
GROUP BY 1, 2, 3, 4, 5
),

erc4626_prices AS(
SELECT
minute,
wrapped_token,
decimals,
APPROX_PERCENTILE(median_price, 0.5) AS price,
LEAD(minute, 1, NOW()) OVER (PARTITION BY wrapped_token ORDER BY minute) AS time_of_next_change
FROM {{ source('balancer_v3', 'erc4626_token_prices') }}
WHERE blockchain = 'arbitrum'
GROUP BY 1, 2, 3
)

SELECT
dexs.blockchain,
dexs.project,
dexs.version,
dexs.block_date,
dexs.block_number,
dexs.block_month,
dexs.block_time,
dexs.token_bought_symbol,
dexs.token_sold_symbol,
dexs.token_pair,
dexs.token_bought_amount,
dexs.token_sold_amount,
dexs.token_bought_amount_raw,
dexs.token_sold_amount_raw,
COALESCE(
dexs.amount_usd,
dexs.token_bought_amount_raw / POWER(10, COALESCE(erc20a.decimals, erc4626a.decimals, 18)) * COALESCE(bpa_bpt_prices.bpt_price, erc4626a.price),
dexs.token_sold_amount_raw / POWER(10, COALESCE(erc20b.decimals, erc4626b.decimals, 18)) * COALESCE(bpb_bpt_prices.bpt_price, erc4626b.price)
) AS amount_usd,
dexs.token_bought_address,
dexs.token_sold_address,
dexs.taker,
dexs.maker,
dexs.project_contract_address,
dexs.pool_id,
dexs.swap_fee,
dexs.pool_symbol,
dexs.pool_type,
dexs.tx_hash,
dexs.tx_from,
dexs.tx_to,
dexs.evt_index
FROM dexs
LEFT JOIN {{ source('tokens', 'erc20') }} erc20a
ON erc20a.contract_address = dexs.token_bought_address
AND erc20a.blockchain = dexs.blockchain
LEFT JOIN {{ source('tokens', 'erc20') }} erc20b
ON erc20b.contract_address = dexs.token_sold_address
AND erc20b.blockchain = dexs.blockchain
INNER JOIN bpa
ON bpa.block_number = dexs.block_number
AND bpa.tx_hash = dexs.tx_hash
AND bpa.evt_index = dexs.evt_index
LEFT JOIN {{ source('balancer', 'bpt_prices') }} bpa_bpt_prices
ON bpa_bpt_prices.contract_address = bpa.contract_address
AND bpa_bpt_prices.day = bpa.bpa_max_block_date
AND bpa_bpt_prices.blockchain = 'arbitrum'
INNER JOIN bpb
ON bpb.block_number = dexs.block_number
AND bpb.tx_hash = dexs.tx_hash
AND bpb.evt_index = dexs.evt_index
LEFT JOIN {{ source('balancer', 'bpt_prices') }} bpb_bpt_prices
ON bpb_bpt_prices.contract_address = bpb.contract_address
AND bpb_bpt_prices.day = bpb.bpb_max_block_date
AND bpb_bpt_prices.blockchain = 'arbitrum'
LEFT JOIN erc4626_prices erc4626a
ON erc4626a.wrapped_token = dexs.token_bought_address
AND erc4626a.minute <= dexs.block_time
AND dexs.block_time < erc4626a.time_of_next_change
LEFT JOIN erc4626_prices erc4626b
ON erc4626b.wrapped_token = dexs.token_sold_address
AND erc4626b.minute <= dexs.block_time
AND dexs.block_time < erc4626b.time_of_next_change
Loading

0 comments on commit bd43c7a

Please sign in to comment.