From 42bcdbd45c77c9286bc11cfa24bbf35616672726 Mon Sep 17 00:00:00 2001 From: Troy Date: Fri, 6 Dec 2024 12:46:30 -0700 Subject: [PATCH] final model update --- .../mainnet/lt_trades_optimism_mainnet.sql | 116 +++++++++++------- 1 file changed, 71 insertions(+), 45 deletions(-) diff --git a/transformers/synthetix/models/marts/optimism/mainnet/lt_trades_optimism_mainnet.sql b/transformers/synthetix/models/marts/optimism/mainnet/lt_trades_optimism_mainnet.sql index e073004b..7aeabaa5 100644 --- a/transformers/synthetix/models/marts/optimism/mainnet/lt_trades_optimism_mainnet.sql +++ b/transformers/synthetix/models/marts/optimism/mainnet/lt_trades_optimism_mainnet.sql @@ -1,47 +1,73 @@ -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, - 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') }} - as leveraged_token_amount, - {{ convert_wei('base_asset_amount') }} as base_asset_amount - from {{ ref('tlx_lt_minted_optimism_mainnet') }} - union all +with trades as ( + select - id, + 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, + 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') }} + 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, + 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 +), + +prices as ( + select distinct + market, 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 + last(price) + over (partition by market, block_number order by id) + as price + from {{ ref('fct_v2_trades_optimism_mainnet') }} +) + +select + trades.*, + prices.price +from trades +left join prices + on + trades.market = prices.market + and trades.block_number = prices.block_number