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

feat: add camelot pair fee rates spellbook #5846

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
a63ece9
chore: add time series for each pair
whalehunting Apr 28, 2024
ae52ded
fix: remove invalid comment syntax
whalehunting Apr 28, 2024
96bb697
Merge remote-tracking branch 'upstream/main' into feat-add-camelot-pa…
whalehunting Apr 28, 2024
0f1d7bf
chore: reformat query file using sqlfmt
whalehunting Apr 28, 2024
094125f
feat: fee rate updates by pair by minute
whalehunting Apr 28, 2024
d016478
fix: add sources?
whalehunting Apr 28, 2024
455c723
fix: remove duplicated sources
whalehunting Apr 28, 2024
cf771bb
feat: fill missing fee rates with last_value() function
whalehunting Apr 28, 2024
ab440f9
feat: add v3 fee rates
whalehunting Apr 28, 2024
a55e4ba
fix: partition by blockhain instead of minute
whalehunting Apr 29, 2024
7fcb82b
Merge branch 'main' into feat-add-camelot-pair-fee-updates
whalehunting Apr 29, 2024
a675c53
fix: also include v3 pairs
whalehunting Apr 29, 2024
204296f
Merge branch 'main' into feat-add-camelot-pair-fee-updates
whalehunting Apr 30, 2024
6f82ccd
Merge branch 'main' into feat-add-camelot-pair-fee-updates
whalehunting Apr 30, 2024
4f8e967
perf: only create prices feeds for minutes where camelot trades happened
whalehunting Apr 30, 2024
7a5346c
fix: fix unique keys
whalehunting May 1, 2024
2cde41c
perf: limit scope from all minutes to minutes where activity happened
whalehunting May 1, 2024
a31d83d
fix: prevent duplicates
whalehunting May 1, 2024
e5d73b6
Merge remote-tracking branch 'upstream/main' into feat-add-camelot-pa…
whalehunting May 1, 2024
71fe1c2
test: remove faulty columns in uniqueness test
whalehunting May 1, 2024
b9bd6bc
fix: move incremental time filters to trades cte
whalehunting May 1, 2024
a76be6a
chore: remove whitespace
whalehunting May 2, 2024
22f9e79
Merge remote-tracking branch 'upstream/main' into feat-add-camelot-pa…
whalehunting May 2, 2024
63cf76c
chore: move spell to arbitrum folder
whalehunting May 2, 2024
a1c42f2
chore: move spell schema to general arbitrum schema file
whalehunting May 2, 2024
a5be330
fix: use ref syntax for dex.trades
whalehunting May 2, 2024
2d63b1f
fix: remove partitioning
whalehunting May 2, 2024
5230dba
fix: use incremental_predicate
whalehunting May 2, 2024
3f27886
test: add uniqueness test in schema
whalehunting May 2, 2024
6778a3b
style: use union distinct
whalehunting May 2, 2024
209bcef
fix: remove anchors
whalehunting May 2, 2024
ad80d3f
chore: i hate yaml
whalehunting May 2, 2024
280a11b
Revert "chore: i hate yaml"
whalehunting May 2, 2024
ce7aad9
Merge branch 'main' into feat-add-camelot-pair-fee-updates
whalehunting May 3, 2024
10a24df
fix: fix yaml
whalehunting May 3, 2024
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
157 changes: 157 additions & 0 deletions models/camelot/arbitrum/camelot_arbitrum_pair_fee_rates.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
{{
config(
schema="camelot_arbitrum",
alias="pair_fee_rates",
materialized="incremental",
file_format="delta",
incremental_strategy="merge",
unique_key=["minute", "blockchain", "pair", "version"],
)
}}

{% set blockchain = "arbitrum" %}
{% set project_start_date = "2022-06-14" %}
{% set v2_fee_precision = "1e5" %}
{% set v2_default_fee = "300" %} -- 0.3%
{% set v3_fee_precision = "1e6" %}
{% set v3_default_fee = "100" %} -- 0.01%

with
v2_pairs_with_initial_fee_rates as (
select
date_trunc('minute', evt_block_time) as minute,
pair,
'2' as version,
token0,
{{ v2_default_fee }} / {{ v2_fee_precision }} as token0_fee_rate,
token1,
{{ v2_default_fee }} / {{ v2_fee_precision }} as token1_fee_rate
from {{ source("camelot_arbitrum", "CamelotFactory_evt_PairCreated") }}
),
v2_directional_fee_rate_updates as (
select
date_trunc('minute', evt_block_time) as minute,
pair,
version,
token0,
token0feepercent / {{ v2_fee_precision }} as token0_fee_rate,
token1,
token1feepercent / {{ v2_fee_precision }} as token1_fee_rate
from
{{ source("camelot_arbitrum", "CamelotPair_evt_FeePercentUpdated") }}
as fee_updates
join
v2_pairs_with_initial_fee_rates as pairs
on fee_updates.contract_address = pairs.pair
),
v3_pairs_with_initial_fee_rates as (
select
date_trunc('minute', evt_block_time) as minute,
pool as pair,
'3' as version,
token0,
{{ v3_default_fee }} / {{ v3_fee_precision }} as token0_fee_rate,
token1,
{{ v3_default_fee }} / {{ v3_fee_precision }} as token1_fee_rate
from {{ source("camelot_v3_arbitrum", "AlgebraFactory_evt_Pool") }}
),
v3_directional_fee_rate_updates as (
select
date_trunc('minute', evt_block_time) as minute,
pair,
version,
token0,
feezto / {{ v3_fee_precision }} as token0_fee_rate,
token1,
feeotz / {{ v3_fee_precision }} as token1_fee_rate
from {{ source("camelot_v3_arbitrum", "AlgebraPool_evt_Fee") }} as fee_updates
join
v3_pairs_with_initial_fee_rates as pairs
on fee_updates.contract_address = pairs.pair
),
pairs as (
select *
from v2_pairs_with_initial_fee_rates
union all
select *
from v3_pairs_with_initial_fee_rates
),
fee_rate_updates as (
select
minute,
pair,
version,
token0,
avg(token0_fee_rate) as token0_fee_rate, -- Handle edge case where pair fees gets changed multiple times per minute
token1,
avg(token1_fee_rate) as token1_fee_rate -- Handle edge case where pair fees gets changed multiple times per minute
from
(
select *
from pairs
union all
select *
from v2_directional_fee_rate_updates
union all
select *
from v3_directional_fee_rate_updates
)
group by minute, pair, version, token0, token1
),
camelot_pair_trades_by_minute as (
select distinct
date_trunc('minute', block_time) as minute, project_contract_address as pair
from {{ ref("dex_trades") }}
where
blockchain = '{{blockchain}}' and project = 'camelot'
{% if not is_incremental() %}
and block_time >= timestamp '{{project_start_date}}'
{% endif %}
{% if is_incremental() %}
and {{ incremental_predicate("block_time") }}
{% endif %}
),
-- Prepare data structure (1 row for every minute where pair trades happened
-- and/or fee rates got updated)
pairs_by_minute as (
select minute, pair
from camelot_pair_trades_by_minute
union distinct
select minute, pair
from fee_rate_updates
),
-- Add version, token0, token1 columns
pairs_by_minute_with_metadata as (
select pairs_by_minute.minute, pairs_by_minute.pair, version, token0, token1
from pairs_by_minute
left join pairs on pairs_by_minute.pair = pairs.pair
)

select
'{{blockchain}}' as blockchain,
pairs.minute,
pairs.pair,
pairs.version,
pairs.token0,
coalesce(
token0_fee_rate,
last_value(token0_fee_rate) ignore nulls over (
partition by pairs.pair
order by pairs.minute
rows between unbounded preceding and current row
)
) as token0_fee_rate,
pairs.token1,
coalesce(
token1_fee_rate,
last_value(token1_fee_rate) ignore nulls over (
partition by pairs.pair
order by pairs.minute
rows between unbounded preceding and current row
)
) as token1_fee_rate
from pairs_by_minute_with_metadata as pairs
left join
fee_rate_updates
on (pairs.minute = fee_rate_updates.minute and pairs.pair = fee_rate_updates.pair)
order by minute desc, pair asc
43 changes: 39 additions & 4 deletions models/camelot/arbitrum/camelot_arbitrum_schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ models:
project: camelot_v2
contributors: ytoast
config:
tags: [ 'arbitrum','dex','trades', 'camelot', 'ytoast']
tags: ["arbitrum", "dex", "trades", "camelot", "ytoast"]
description: >
Camelot trades on Arbitrum on a specific contract/version.
tests:
Expand Down Expand Up @@ -102,7 +102,7 @@ models:
project: camelot_v3
contributors: whale_hunter
config:
tags: [ 'arbitrum','dex','trades', 'camelot', 'whale_hunter']
tags: ["arbitrum", "dex", "trades", "camelot", "whale_hunter"]
description: >
Camelot V3 trades on Arbitrum
tests:
Expand Down Expand Up @@ -150,9 +150,9 @@ models:
project: camelot
contributors: ytoast, whale_hunter
config:
tags: ['arbitrum','camelot','trades','dex','ytoast', 'whale_hunter']
tags: ["arbitrum", "camelot", "trades", "dex", "ytoast", "whale_hunter"]
description: >
Camelot trades on Arbitrum
Camelot trades on Arbitrum
columns:
- *blockchain
- *project
Expand All @@ -176,3 +176,38 @@ models:
- *tx_from
- *tx_to
- *evt_index

- name: camelot_arbitrum_pair_fee_rates
meta:
blockchain: arbitrum
sector: dex
project: camelot
contributors: whale_hunter
config:
tags: ["arbitrum", "camelot", "whale_hunter"]
description: >
Camelot fee rates over time across all pairs and versions on Arbitrum.
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- minute
- blockchain
- pair
- version
columns:
- name: blockchain
description: "Blockchain on which the pair is deployed"
- name: minute
description: "UTC event block time truncated to the minute mark"
- name: pair
description: "Pair address"
- name: version
description: "Version of the pair built and deployed by the DEX project"
- name: token0
description: "Address of token0"
- name: token0_fee_rate
description: "Fee rate as a fraction of 1, applied if token0 gets swapped to token1"
- name: token1
description: "Address of token1"
- name: token1_fee_rate
description: "Fee rate as a fraction of 1, applied if token1 gets swapped to token0"
9 changes: 9 additions & 0 deletions sources/camelot/arbitrum/camelot_arbitrum_sources.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: 2

sources:
- name: camelot_arbitrum
tables:
- name: CamelotPair_evt_FeePercentUpdated
- name: camelot_v3_arbitrum
tables:
- name: AlgebraPool_evt_Fee
Loading