Skip to content

Commit

Permalink
update model
Browse files Browse the repository at this point in the history
  • Loading branch information
Tburm committed Dec 6, 2024
1 parent 38f72ca commit 4f68259
Showing 1 changed file with 38 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -1,63 +1,47 @@
with trades as (
select
a.id,
a.block_number,
a.ts,
a.transaction_hash,
a.contract,
a.event_name,
a.account,
a.token,
a.leverage,
upper(substring(a.token from '([^_]+)')) as market,
a.leveraged_token_amount,
a.base_asset_amount,
abs(a.leveraged_token_amount) as nominal_volume,
abs(a.leveraged_token_amount) * a.leverage as notional_volume
from (

select
id,
block_number,
ts,
block_timestamp as ts,
transaction_hash,
contract,
event_name,
account,
token,
upper(substring(token from '([^_]+)')) as market,
leveraged_token_amount,
base_asset_amount
from (
select
id,
block_number,
block_timestamp as ts,
transaction_hash,
contract,
event_name,
account,
token,
{{ convert_wei('leveraged_token_amount') }}
as leveraged_token_amount,
{{ convert_wei('base_asset_amount') }} as base_asset_amount
from {{ ref('tlx_lt_minted_optimism_mainnet') }}
union all
select
id,
block_number,
block_timestamp as ts,
transaction_hash,
contract,
event_name,
account,
token,
{{ convert_wei('leveraged_token_amount') }}
* -1 as leveraged_token_amount,
{{ convert_wei('base_asset_amount') }} * -1 as base_asset_amount
from {{ ref('tlx_lt_redeemed_optimism_mainnet') }}
) as a
),

prices as (
select distinct
market,
cast(regexp_replace(token, '.*_(long|short)', '') as int) as leverage,
{{ convert_wei('leveraged_token_amount') }}
as leveraged_token_amount,
{{ convert_wei('base_asset_amount') }} as base_asset_amount
from {{ ref('tlx_lt_minted_optimism_mainnet') }}
union all
select
id,
block_number,
last(price)
over (partition by market, block_number order by id)
as price
from {{ ref('fct_v2_trades_optimism_mainnet') }}
)

select
trades.*,
prices.price,
abs(trades.leveraged_token_amount) * prices.price as volume
from trades
left join prices
on
trades.market = prices.market
and trades.block_number = prices.block_number
block_timestamp as ts,
transaction_hash,
contract,
event_name,
account,
token,
cast(regexp_replace(token, '.*_(long|short)', '') as int) as leverage,
{{ convert_wei('leveraged_token_amount') }}
* -1 as leveraged_token_amount,
{{ convert_wei('base_asset_amount') }} * -1 as base_asset_amount
from {{ ref('tlx_lt_redeemed_optimism_mainnet') }}
) as a

0 comments on commit 4f68259

Please sign in to comment.