diff --git a/transformers/synthetix/macros/get_tlx_event_data.sql b/transformers/synthetix/macros/get_tlx_event_data.sql index 626b87ac..554959f5 100644 --- a/transformers/synthetix/macros/get_tlx_event_data.sql +++ b/transformers/synthetix/macros/get_tlx_event_data.sql @@ -21,13 +21,9 @@ ) SELECT *, - REGEXP_SUBSTR( - "_dbt_source_relation", - 'lt_([^_]+)_event', - 1, - 1, - 'i', - 1 + substring( + "_dbt_source_relation" + FROM 'lt_([^_]+_[^_]+)_event_[^_]+$' ) AS token FROM raw_data 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 new file mode 100644 index 00000000..7aeabaa5 --- /dev/null +++ b/transformers/synthetix/models/marts/optimism/mainnet/lt_trades_optimism_mainnet.sql @@ -0,0 +1,73 @@ +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, + 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, + 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 diff --git a/transformers/synthetix/models/marts/optimism/mainnet/schema.yml b/transformers/synthetix/models/marts/optimism/mainnet/schema.yml index c2d3598b..6b0a17ec 100644 --- a/transformers/synthetix/models/marts/optimism/mainnet/schema.yml +++ b/transformers/synthetix/models/marts/optimism/mainnet/schema.yml @@ -431,3 +431,45 @@ models: data_type: numeric - name: net_transfers data_type: numeric + - name: lt_trades_optimism_mainnet + columns: + - name: id + data_type: character varying + - name: block_number + data_type: integer + - name: ts + data_type: timestamp with time zone + - name: transaction_hash + data_type: text + - name: contract + data_type: text + - name: event_name + data_type: text + - name: account + data_type: text + - name: token + data_type: text + - name: leverage + data_type: numeric + - name: market + data_type: text + - name: leveraged_token_amount + data_type: numeric + - name: base_asset_amount + data_type: numeric + - name: price + data_type: numeric + - name: nominal_volume + data_type: numeric + tests: + - not_null + - dbt_utils.accepted_range: + min_value: 0 + inclusive: true + - name: notional_volume + data_type: numeric + tests: + - not_null + - dbt_utils.accepted_range: + min_value: 0 + inclusive: true diff --git a/transformers/synthetix/models/raw/optimism/mainnet/schema.yml b/transformers/synthetix/models/raw/optimism/mainnet/schema.yml index f5de6d0c..73123684 100644 --- a/transformers/synthetix/models/raw/optimism/mainnet/schema.yml +++ b/transformers/synthetix/models/raw/optimism/mainnet/schema.yml @@ -151,6 +151,10 @@ models: data_type: integer - name: contract data_type: text + - name: token + data_type: text + tests: + - not_null - name: event_name data_type: text - name: account @@ -171,6 +175,10 @@ models: data_type: integer - name: contract data_type: text + - name: token + data_type: text + tests: + - not_null - name: event_name data_type: text - name: account