Skip to content

Commit

Permalink
Merge pull request #129 from Synthetixio/oi-models
Browse files Browse the repository at this point in the history
Update V3 OI Models
  • Loading branch information
Tburm authored Oct 11, 2024
2 parents 97329df + c8b7331 commit 1073300
Show file tree
Hide file tree
Showing 8 changed files with 500 additions and 168 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
with arbitrum as (
with base as (
select
mu.id,
mu.block_timestamp as ts,
Expand All @@ -17,36 +17,90 @@ with arbitrum as (
{{ convert_wei('current_funding_rate') }} * 365.25
+ {{ convert_wei('interest_rate') }} as long_rate_apr,
{{ convert_wei('current_funding_rate') }} * -1 * 365.25
+ {{ convert_wei('interest_rate') }} as short_rate_apr
+ {{ convert_wei('interest_rate') }} as short_rate_apr,
LAG({{ convert_wei('size') }}, 1, 0) over (
partition by m.market_symbol
order by
mu.block_timestamp
) as prev_size
from
{{ ref('perp_market_updated_arbitrum_mainnet') }}
as mu
left join
{{ ref('fct_perp_markets_arbitrum_mainnet') }}
as m
{{ ref('perp_market_updated_arbitrum_mainnet') }} as mu
left join {{ ref('fct_perp_markets_arbitrum_mainnet') }} as m
on mu.market_id = m.id
),

oi as (
select
id,
ts,
size * price as market_oi_usd,
LAG(
size * price,
1,
0
) over (
partition by market_symbol
order by
ts asc
) as prev_market_oi_usd,
(
size + skew
) * price / 2 as long_oi,
(
size - skew
) * price / 2 as short_oi,
case
when size * price = 0 then null
else ((size + skew) * price / 2) / (
size * price
)
end as long_oi_pct,
case
when size * price = 0 then null
else ((size - skew) * price / 2) / (
size * price
)
end as short_oi_pct
from
base
),

total_oi as (
select
id,
SUM(
market_oi_usd - prev_market_oi_usd
) over (
order by
ts asc
) as total_oi_usd
from
oi
)

select
*,
size * price as size_usd,
(
size + skew
) * price / 2 as long_oi,
(
size - skew
) * price / 2 as short_oi,
case
when size * price = 0 then null
else ((size + skew) * price / 2) / (
size * price
)
end as long_oi_pct,
case
when size * price = 0 then null
else ((size - skew) * price / 2) / (
size * price
)
end as short_oi_pct
base.*,
ROUND(
oi.market_oi_usd,
2
) as market_oi_usd,
ROUND(
total_oi.total_oi_usd,
2
) as total_oi_usd,
ROUND(
oi.long_oi,
2
) as long_oi,
ROUND(
oi.short_oi,
2
) as short_oi,
oi.long_oi_pct,
oi.short_oi_pct
from
arbitrum
base
inner join oi
on base.id = oi.id
inner join total_oi
on base.id = total_oi.id
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ models:
tests:
- not_null
- accepted_values:
values: ["InterestCharged"]
values: ["InterestCharged"]
- name: account_id
description: "ID of the account"
data_type: numeric
Expand Down Expand Up @@ -84,7 +84,7 @@ models:
tests:
- not_null
- accepted_values:
values: ["CollateralModified"]
values: ["CollateralModified"]
- name: synth_market_id
description: "Synth market ID"
data_type: numeric
Expand Down Expand Up @@ -133,7 +133,7 @@ models:
tests:
- not_null
- accepted_values:
values: ["PreviousOrderExpired"]
values: ["PreviousOrderExpired"]
- name: market_id
description: "ID of the market"
data_type: numeric
Expand Down Expand Up @@ -382,7 +382,7 @@ models:
columns:
- name: id
description: "Market ID"
data_type: numeric
data_type: numeric
tests:
- not_null
- dbt_utils.accepted_range:
Expand Down Expand Up @@ -445,7 +445,7 @@ models:
tests:
- not_null
- name: price
description: "Price (ETH)"
description: "Price (USD)"
data_type: numeric
tests:
- not_null
Expand All @@ -465,46 +465,75 @@ models:
tests:
- not_null
- name: funding_rate
description: "Funding rate (ETH)"
description: "Funding rate (%)"
data_type: numeric
tests:
- not_null
- name: funding_velocity
description: "Funding velocity (ETH)"
description: "Funding velocity"
data_type: numeric
tests:
- not_null
- name: interest_rate
description: "Interest rate (ETH)"
description: "Interest rate (%)"
data_type: numeric
tests:
- not_null
- name: funding_rate_apr
description: "Funding rate APR (ETH)"
description: "Funding rate APR (%)"
data_type: numeric
tests:
- not_null
- name: long_rate_apr
description: "Long rate APR (ETH)"
description: "Long rate APR (%)"
data_type: numeric
tests:
- not_null
- name: short_rate_apr
description: "Short rate APR (ETH)"
description: "Short rate APR (%)"
data_type: numeric
tests:
- not_null
- name: prev_size
description: "Previous size (ETH)"
data_type: numeric
tests:
- not_null
- name: size_usd
description: "Size (USD)"
- dbt_utils.accepted_range:
min_value: 0
inclusive: true
- name: market_oi_usd
description: "Market open interest (USD)"
data_type: numeric
tests:
- not_null
- dbt_utils.accepted_range:
min_value: 0
inclusive: true
- name: total_oi_usd
description: "Total open interest (USD)"
data_type: numeric
tests:
- not_null
- dbt_utils.accepted_range:
min_value: 0
inclusive: true
- name: long_oi
description: "Long open interest (ETH)"
description: "Long open interest (USD)"
data_type: numeric
tests:
- not_null
- dbt_utils.accepted_range:
min_value: 0
inclusive: true
- name: short_oi
description: "Short open interest (ETH)"
description: "Short open interest (USD)"
data_type: numeric
tests:
- not_null
- dbt_utils.accepted_range:
min_value: 0
inclusive: true
- name: long_oi_pct
description: "Long open interest (%)"
data_type: numeric
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
with arbitrum as (
with base as (
select
mu.id,
mu.block_timestamp as ts,
Expand All @@ -17,36 +17,90 @@ with arbitrum as (
{{ convert_wei('current_funding_rate') }} * 365.25
+ {{ convert_wei('interest_rate') }} as long_rate_apr,
{{ convert_wei('current_funding_rate') }} * -1 * 365.25
+ {{ convert_wei('interest_rate') }} as short_rate_apr
+ {{ convert_wei('interest_rate') }} as short_rate_apr,
LAG({{ convert_wei('size') }}, 1, 0) over (
partition by m.market_symbol
order by
mu.block_timestamp
) as prev_size
from
{{ ref('perp_market_updated_arbitrum_sepolia') }}
as mu
left join
{{ ref('fct_perp_markets_arbitrum_sepolia') }}
as m
{{ ref('perp_market_updated_arbitrum_sepolia') }} as mu
left join {{ ref('fct_perp_markets_arbitrum_sepolia') }} as m
on mu.market_id = m.id
),

oi as (
select
id,
ts,
size * price as market_oi_usd,
LAG(
size * price,
1,
0
) over (
partition by market_symbol
order by
ts asc
) as prev_market_oi_usd,
(
size + skew
) * price / 2 as long_oi,
(
size - skew
) * price / 2 as short_oi,
case
when size * price = 0 then null
else ((size + skew) * price / 2) / (
size * price
)
end as long_oi_pct,
case
when size * price = 0 then null
else ((size - skew) * price / 2) / (
size * price
)
end as short_oi_pct
from
base
),

total_oi as (
select
id,
SUM(
market_oi_usd - prev_market_oi_usd
) over (
order by
ts asc
) as total_oi_usd
from
oi
)

select
*,
size * price as size_usd,
(
size + skew
) * price / 2 as long_oi,
(
size - skew
) * price / 2 as short_oi,
case
when size * price = 0 then null
else ((size + skew) * price / 2) / (
size * price
)
end as long_oi_pct,
case
when size * price = 0 then null
else ((size - skew) * price / 2) / (
size * price
)
end as short_oi_pct
base.*,
ROUND(
oi.market_oi_usd,
2
) as market_oi_usd,
ROUND(
total_oi.total_oi_usd,
2
) as total_oi_usd,
ROUND(
oi.long_oi,
2
) as long_oi,
ROUND(
oi.short_oi,
2
) as short_oi,
oi.long_oi_pct,
oi.short_oi_pct
from
arbitrum
base
inner join oi
on base.id = oi.id
inner join total_oi
on base.id = total_oi.id
Loading

0 comments on commit 1073300

Please sign in to comment.