-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
71 additions
and
45 deletions.
There are no files selected for viewing
116 changes: 71 additions & 45 deletions
116
transformers/synthetix/models/marts/optimism/mainnet/lt_trades_optimism_mainnet.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |