diff --git a/solana/models/_sector/dex/lifinity/lifinity_v1_trades.sql b/solana/models/_sector/dex/lifinity/lifinity_v1_trades.sql index 8d5a4aea042..85d1286ddca 100644 --- a/solana/models/_sector/dex/lifinity/lifinity_v1_trades.sql +++ b/solana/models/_sector/dex/lifinity/lifinity_v1_trades.sql @@ -4,7 +4,6 @@ schema = 'lifinity_v1', alias = 'trades', partition_by = ['block_month'], - tags = ['prod_exclude'], materialized = 'incremental', file_format = 'delta', incremental_strategy = 'merge', @@ -36,21 +35,30 @@ WITH , ip.account_arguments[2] as pool_id , ip.account_arguments[3] as pool_mint_id , ip.tx_id as init_tx - FROM {{ source('solana','instruction_calls') }} ip + FROM ( + SELECT + * + FROM {{ source('solana','instruction_calls') }} + WHERE cardinality(account_arguments) >= 5 --filter out broken cases/inits for now + and bytearray_substring(data,1,8) = 0xafaf6d1f0d989bed + and executing_account = 'EewxydAPCCVuNEyrVN68PuSYdQ7wKn27V9Gjeoi8dy3S' + and tx_success + and block_time > TIMESTAMP '2022-01-26' + ) ip INNER JOIN {{ ref('solana_utils_token_accounts') }} mintA ON mintA.address = ip.account_arguments[4] + AND mintA.account_type = 'fungible' INNER JOIN {{ ref('solana_utils_token_accounts') }} mintB ON mintB.address = ip.account_arguments[5] + AND mintB.account_type = 'fungible' LEFT JOIN {{ ref('tokens_solana_fungible') }} tkA ON tkA.token_mint_address = mintA.token_mint_address + AND tkA.token_version = 'spl_token' LEFT JOIN {{ ref('tokens_solana_fungible') }} tkB ON tkB.token_mint_address = mintB.token_mint_address - WHERE bytearray_substring(ip.data,1,8) = 0xafaf6d1f0d989bed - and executing_account = 'EewxydAPCCVuNEyrVN68PuSYdQ7wKn27V9Gjeoi8dy3S' - and tx_success - and cardinality(account_arguments) >= 5 --filter out broken cases/inits for now - and block_time > TIMESTAMP '{{project_start_date}}' + AND tkB.token_version = 'spl_token' ) , all_swaps as ( SELECT sp.call_block_time as block_time + , sp.call_block_slot as block_slot , 'lifinity' as project , 1 as version , 'solana' as blockchain @@ -61,66 +69,66 @@ WITH when lower(tokenA_symbol) > lower(tokenB_symbol) then concat(tokenB_symbol, '-', tokenA_symbol) else concat(tokenA_symbol, '-', tokenB_symbol) end as token_pair - , case when tk_1.token_mint_address = p.tokenA then COALESCE(tokenB_symbol, tokenB) + , case when tr_1.token_mint_address = p.tokenA then COALESCE(tokenB_symbol, tokenB) else COALESCE(tokenA_symbol, tokenA) end as token_bought_symbol -- token bought is always the second instruction (transfer) in the inner instructions , tr_2.amount as token_bought_amount_raw - , tr_2.amount/pow(10,COALESCE(case when tk_1.token_mint_address = p.tokenA then p.tokenB_decimals else tokenA_decimals end,9)) as token_bought_amount - , case when tk_1.token_mint_address = p.tokenA then COALESCE(tokenA_symbol, tokenA) + , tr_2.amount/pow(10,COALESCE(case when tr_1.token_mint_address = p.tokenA then p.tokenB_decimals else tokenA_decimals end,9)) as token_bought_amount + , case when tr_1.token_mint_address = p.tokenA then COALESCE(tokenA_symbol, tokenA) else COALESCE(tokenB_symbol, tokenB) end as token_sold_symbol , tr_1.amount as token_sold_amount_raw - , tr_1.amount/pow(10,COALESCE(case when tk_1.token_mint_address = p.tokenA then p.tokenA_decimals else tokenB_decimals end,9)) as token_sold_amount + , tr_1.amount/pow(10,COALESCE(case when tr_1.token_mint_address = p.tokenA then p.tokenA_decimals else tokenB_decimals end,9)) as token_sold_amount , p.pool_id , sp.call_tx_signer as trader_id , sp.call_tx_id as tx_id , sp.call_outer_instruction_index as outer_instruction_index , COALESCE(sp.call_inner_instruction_index, 0) as inner_instruction_index , sp.call_tx_index as tx_index - , case when tk_1.token_mint_address = p.tokenA then p.tokenB + , case when tr_1.token_mint_address = p.tokenA then p.tokenB else p.tokenA end as token_bought_mint_address - , case when tk_1.token_mint_address = p.tokenA then p.tokenA + , case when tr_1.token_mint_address = p.tokenA then p.tokenA else p.tokenB end as token_sold_mint_address - , case when tk_1.token_mint_address = p.tokenA then p.tokenBVault + , case when tr_1.token_mint_address = p.tokenA then p.tokenBVault else p.tokenAVault end as token_bought_vault - , case when tk_1.token_mint_address = p.tokenA then p.tokenAVault + , case when tr_1.token_mint_address = p.tokenA then p.tokenAVault else p.tokenBVault end as token_sold_vault --swap out can be either 2nd or 3rd transfer, we need to filter for the first transfer out. - , tr_2.call_inner_instruction_index as transfer_out_index + , tr_2.inner_instruction_index as transfer_out_index , row_number() over (partition by sp.call_tx_id, sp.call_outer_instruction_index, sp.call_inner_instruction_index - order by COALESCE(tr_2.call_inner_instruction_index, 0) asc) as first_transfer_out + order by COALESCE(tr_2.inner_instruction_index, 0) asc) as first_transfer_out FROM {{ source('lifinity_amm_solana', 'lifinity_amm_call_swap') }} sp INNER JOIN pools p ON sp.account_amm = p.pool_id --account 2 - INNER JOIN {{ source('spl_token_solana', 'spl_token_call_transfer') }} tr_1 - ON tr_1.call_tx_id = sp.call_tx_id - AND tr_1.call_outer_instruction_index = sp.call_outer_instruction_index - AND ((sp.call_is_inner = false AND tr_1.call_inner_instruction_index = 1) - OR (sp.call_is_inner = true AND tr_1.call_inner_instruction_index = sp.call_inner_instruction_index + 1)) + INNER JOIN {{ ref('tokens_solana_transfers') }} tr_1 + ON tr_1.tx_id = sp.call_tx_id + AND tr_1.outer_instruction_index = sp.call_outer_instruction_index + AND ((sp.call_is_inner = false AND tr_1.inner_instruction_index = 1) + OR (sp.call_is_inner = true AND tr_1.inner_instruction_index = sp.call_inner_instruction_index + 1)) + AND tr_1.token_version = 'spl_token' {% if is_incremental() %} - AND {{incremental_predicate('tr_1.call_block_time')}} + AND {{incremental_predicate('tr_1.block_time')}} {% else %} - AND tr_1.call_block_time >= TIMESTAMP '{{project_start_date}}' + AND tr_1.block_time >= TIMESTAMP '{{project_start_date}}' {% endif %} --swap out can be either 2nd or 3rd transfer. - INNER JOIN {{ source('spl_token_solana', 'spl_token_call_transfer') }} tr_2 - ON tr_2.call_tx_id = sp.call_tx_id - AND tr_2.call_outer_instruction_index = sp.call_outer_instruction_index - AND ((sp.call_is_inner = false AND (tr_2.call_inner_instruction_index = 2 OR tr_2.call_inner_instruction_index = 3)) - OR (sp.call_is_inner = true AND (tr_2.call_inner_instruction_index = sp.call_inner_instruction_index + 2 OR tr_2.call_inner_instruction_index = sp.call_inner_instruction_index + 3)) + INNER JOIN {{ ref('tokens_solana_transfers') }} tr_2 + ON tr_2.tx_id = sp.call_tx_id + AND tr_2.outer_instruction_index = sp.call_outer_instruction_index + AND ((sp.call_is_inner = false AND (tr_2.inner_instruction_index = 2 OR tr_2.inner_instruction_index = 3)) + OR (sp.call_is_inner = true AND (tr_2.inner_instruction_index = sp.call_inner_instruction_index + 2 OR tr_2.inner_instruction_index = sp.call_inner_instruction_index + 3)) ) + AND tr_2.token_version = 'spl_token' {% if is_incremental() %} - AND {{incremental_predicate('tr_2.call_block_time')}} + AND {{incremental_predicate('tr_2.block_time')}} {% else %} - AND tr_2.call_block_time >= TIMESTAMP '{{project_start_date}}' + AND tr_2.block_time >= TIMESTAMP '{{project_start_date}}' {% endif %} - --we want to get what token was transfered out first as this is the sold token. THIS MUST BE THE DESTINATION account, the source account is commonly created/closed through swap legs. - LEFT JOIN {{ ref('solana_utils_token_accounts') }} tk_1 ON tk_1.address = tr_1.account_destination WHERE 1=1 {% if is_incremental() %} AND {{incremental_predicate('sp.call_block_time')}} @@ -135,6 +143,7 @@ SELECT , tb.version , CAST(date_trunc('month', tb.block_time) AS DATE) as block_month , tb.block_time + , tb.block_slot , tb.token_pair , tb.trade_source , tb.token_bought_symbol @@ -143,7 +152,10 @@ SELECT , tb.token_sold_symbol , tb.token_sold_amount , tb.token_sold_amount_raw - , COALESCE(tb.token_sold_amount * p_sold.price, tb.token_bought_amount * p_bought.price) as amount_usd + , case when p_sold.price is not null and p_bought.price is not null + then least(tb.token_sold_amount * p_sold.price, tb.token_bought_amount * p_bought.price) + else COALESCE(tb.token_sold_amount * p_sold.price, tb.token_bought_amount * p_bought.price) + end as amount_usd , cast(null as double) as fee_tier , cast(null as double) as fee_usd , tb.token_sold_mint_address @@ -151,6 +163,7 @@ SELECT , tb.token_sold_vault , tb.token_bought_vault , tb.pool_id as project_program_id + , 'EewxydAPCCVuNEyrVN68PuSYdQ7wKn27V9Gjeoi8dy3S' as project_main_id , tb.trader_id , tb.tx_id , tb.outer_instruction_index diff --git a/solana/models/_sector/dex/lifinity/lifinity_v2_trades.sql b/solana/models/_sector/dex/lifinity/lifinity_v2_trades.sql index 917574ab7c0..a5525002e91 100644 --- a/solana/models/_sector/dex/lifinity/lifinity_v2_trades.sql +++ b/solana/models/_sector/dex/lifinity/lifinity_v2_trades.sql @@ -5,7 +5,6 @@ alias = 'trades', partition_by = ['block_month'], materialized = 'incremental', - tags = ['prod_exclude'], file_format = 'delta', incremental_strategy = 'merge', incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.block_time')], @@ -36,21 +35,30 @@ WITH , ip.account_arguments[2] as pool_id , ip.account_arguments[3] as pool_mint_id , ip.tx_id as init_tx - FROM {{ source('solana','instruction_calls') }} ip + FROM ( + SELECT + * + FROM {{ source('solana','instruction_calls') }} + WHERE cardinality(account_arguments) >= 5 --filter out broken cases/inits for now + and bytearray_substring(data,1,8) = 0xafaf6d1f0d989bed + and executing_account = '2wT8Yq49kHgDzXuPxZSaeLaH1qbmGXtEyPy64bL7aD3c' + and tx_success + and block_time > TIMESTAMP '2022-01-26' + ) ip INNER JOIN {{ ref('solana_utils_token_accounts') }} mintA ON mintA.address = ip.account_arguments[4] + AND mintA.account_type = 'fungible' INNER JOIN {{ ref('solana_utils_token_accounts') }} mintB ON mintB.address = ip.account_arguments[5] + AND mintB.account_type = 'fungible' LEFT JOIN {{ ref('tokens_solana_fungible') }} tkA ON tkA.token_mint_address = mintA.token_mint_address + AND tkA.token_version = 'spl_token' LEFT JOIN {{ ref('tokens_solana_fungible') }} tkB ON tkB.token_mint_address = mintB.token_mint_address - WHERE bytearray_substring(ip.data,1,8) = 0xafaf6d1f0d989bed - and executing_account = '2wT8Yq49kHgDzXuPxZSaeLaH1qbmGXtEyPy64bL7aD3c' - and tx_success - and cardinality(account_arguments) >= 5 --filter out broken cases/inits for now - and block_time > TIMESTAMP '{{project_start_date}}' + AND tkB.token_version = 'spl_token' ) , all_swaps as ( SELECT sp.call_block_time as block_time + , sp.call_block_slot as block_slot , 'lifinity' as project , 2 as version , 'solana' as blockchain @@ -61,66 +69,66 @@ WITH when lower(tokenA_symbol) > lower(tokenB_symbol) then concat(tokenB_symbol, '-', tokenA_symbol) else concat(tokenA_symbol, '-', tokenB_symbol) end as token_pair - , case when tk_1.token_mint_address = p.tokenA then COALESCE(tokenB_symbol, tokenB) + , case when tr_1.token_mint_address = p.tokenA then COALESCE(tokenB_symbol, tokenB) else COALESCE(tokenA_symbol, tokenA) end as token_bought_symbol -- token bought is always the second instruction (transfer) in the inner instructions , tr_2.amount as token_bought_amount_raw - , tr_2.amount/pow(10,COALESCE(case when tk_1.token_mint_address = p.tokenA then p.tokenB_decimals else tokenA_decimals end,9)) as token_bought_amount - , case when tk_1.token_mint_address = p.tokenA then COALESCE(tokenA_symbol, tokenA) + , tr_2.amount/pow(10,COALESCE(case when tr_1.token_mint_address = p.tokenA then p.tokenB_decimals else tokenA_decimals end,9)) as token_bought_amount + , case when tr_1.token_mint_address = p.tokenA then COALESCE(tokenA_symbol, tokenA) else COALESCE(tokenB_symbol, tokenB) end as token_sold_symbol , tr_1.amount as token_sold_amount_raw - , tr_1.amount/pow(10,COALESCE(case when tk_1.token_mint_address = p.tokenA then p.tokenA_decimals else tokenB_decimals end,9)) as token_sold_amount + , tr_1.amount/pow(10,COALESCE(case when tr_1.token_mint_address = p.tokenA then p.tokenA_decimals else tokenB_decimals end,9)) as token_sold_amount , p.pool_id , sp.call_tx_signer as trader_id , sp.call_tx_id as tx_id , sp.call_outer_instruction_index as outer_instruction_index , COALESCE(sp.call_inner_instruction_index, 0) as inner_instruction_index , sp.call_tx_index as tx_index - , case when tk_1.token_mint_address = p.tokenA then p.tokenB + , case when tr_1.token_mint_address = p.tokenA then p.tokenB else p.tokenA end as token_bought_mint_address - , case when tk_1.token_mint_address = p.tokenA then p.tokenA + , case when tr_1.token_mint_address = p.tokenA then p.tokenA else p.tokenB end as token_sold_mint_address - , case when tk_1.token_mint_address = p.tokenA then p.tokenBVault + , case when tr_1.token_mint_address = p.tokenA then p.tokenBVault else p.tokenAVault end as token_bought_vault - , case when tk_1.token_mint_address = p.tokenA then p.tokenAVault + , case when tr_1.token_mint_address = p.tokenA then p.tokenAVault else p.tokenBVault end as token_sold_vault --swap out can be either 2nd or 3rd transfer, we need to filter for the first transfer out. - , tr_2.call_inner_instruction_index as transfer_out_index + , tr_2.inner_instruction_index as transfer_out_index , row_number() over (partition by sp.call_tx_id, sp.call_outer_instruction_index, sp.call_inner_instruction_index - order by COALESCE(tr_2.call_inner_instruction_index, 0) asc) as first_transfer_out + order by COALESCE(tr_2.inner_instruction_index, 0) asc) as first_transfer_out FROM {{ source('lifinity_amm_v2_solana', 'lifinity_amm_v2_call_swap') }} sp INNER JOIN pools p ON sp.account_amm = p.pool_id --account 2 - INNER JOIN {{ source('spl_token_solana', 'spl_token_call_transfer') }} tr_1 - ON tr_1.call_tx_id = sp.call_tx_id - AND tr_1.call_outer_instruction_index = sp.call_outer_instruction_index - AND ((sp.call_is_inner = false AND tr_1.call_inner_instruction_index = 1) - OR (sp.call_is_inner = true AND tr_1.call_inner_instruction_index = sp.call_inner_instruction_index + 1)) + INNER JOIN {{ ref('tokens_solana_transfers') }} tr_1 + ON tr_1.tx_id = sp.call_tx_id + AND tr_1.outer_instruction_index = sp.call_outer_instruction_index + AND ((sp.call_is_inner = false AND tr_1.inner_instruction_index = 1) + OR (sp.call_is_inner = true AND tr_1.inner_instruction_index = sp.call_inner_instruction_index + 1)) + AND tr_1.token_version = 'spl_token' {% if is_incremental() %} - AND {{incremental_predicate('tr_1.call_block_time')}} + AND {{incremental_predicate('tr_1.block_time')}} {% else %} - AND tr_1.call_block_time >= TIMESTAMP '{{project_start_date}}' + AND tr_1.block_time >= TIMESTAMP '{{project_start_date}}' {% endif %} --swap out can be either 2nd or 3rd transfer. - INNER JOIN {{ source('spl_token_solana', 'spl_token_call_transfer') }} tr_2 - ON tr_2.call_tx_id = sp.call_tx_id - AND tr_2.call_outer_instruction_index = sp.call_outer_instruction_index - AND ((sp.call_is_inner = false AND (tr_2.call_inner_instruction_index = 2 OR tr_2.call_inner_instruction_index = 3)) - OR (sp.call_is_inner = true AND (tr_2.call_inner_instruction_index = sp.call_inner_instruction_index + 2 OR tr_2.call_inner_instruction_index = sp.call_inner_instruction_index + 3)) + INNER JOIN {{ ref('tokens_solana_transfers') }} tr_2 + ON tr_2.tx_id = sp.call_tx_id + AND tr_2.outer_instruction_index = sp.call_outer_instruction_index + AND ((sp.call_is_inner = false AND (tr_2.inner_instruction_index = 2 OR tr_2.inner_instruction_index = 3)) + OR (sp.call_is_inner = true AND (tr_2.inner_instruction_index = sp.call_inner_instruction_index + 2 OR tr_2.inner_instruction_index = sp.call_inner_instruction_index + 3)) ) + AND tr_2.token_version = 'spl_token' {% if is_incremental() %} - AND {{incremental_predicate('tr_2.call_block_time')}} + AND {{incremental_predicate('tr_2.block_time')}} {% else %} - AND tr_2.call_block_time >= TIMESTAMP '{{project_start_date}}' + AND tr_2.block_time >= TIMESTAMP '{{project_start_date}}' {% endif %} - --we want to get what token was transfered out first as this is the sold token. THIS MUST BE THE DESTINATION account, the source account is commonly created/closed through swap legs. - LEFT JOIN {{ ref('solana_utils_token_accounts') }} tk_1 ON tk_1.address = tr_1.account_destination WHERE 1=1 {% if is_incremental() %} AND {{incremental_predicate('sp.call_block_time')}} @@ -135,6 +143,7 @@ SELECT , tb.version , CAST(date_trunc('month', tb.block_time) AS DATE) as block_month , tb.block_time + , tb.block_slot , tb.token_pair , tb.trade_source , tb.token_bought_symbol @@ -143,7 +152,10 @@ SELECT , tb.token_sold_symbol , tb.token_sold_amount , tb.token_sold_amount_raw - , COALESCE(tb.token_sold_amount * p_sold.price, tb.token_bought_amount * p_bought.price) as amount_usd + , case when p_sold.price is not null and p_bought.price is not null + then least(tb.token_sold_amount * p_sold.price, tb.token_bought_amount * p_bought.price) + else COALESCE(tb.token_sold_amount * p_sold.price, tb.token_bought_amount * p_bought.price) + end as amount_usd , cast(null as double) as fee_tier , cast(null as double) as fee_usd , tb.token_sold_mint_address @@ -151,6 +163,7 @@ SELECT , tb.token_sold_vault , tb.token_bought_vault , tb.pool_id as project_program_id + , '2wT8Yq49kHgDzXuPxZSaeLaH1qbmGXtEyPy64bL7aD3c' as project_main_id , tb.trader_id , tb.tx_id , tb.outer_instruction_index diff --git a/solana/models/_sector/dex/lifinity/schema.yml b/solana/models/_sector/dex/lifinity/schema.yml index 338b9dcba93..8a00d00fbca 100644 --- a/solana/models/_sector/dex/lifinity/schema.yml +++ b/solana/models/_sector/dex/lifinity/schema.yml @@ -33,6 +33,9 @@ models: - &block_time name: block_time description: "UTC event block time of each DEX trade" + - &block_slot + name: block_slot + description: "block slot of each DEX trade" - &trade_source name: trade_source description: "Was the trade a direct call to the dexor did it go through another program like Jupiter (Dex Aggregator)" @@ -85,6 +88,9 @@ models: - &project_program_id name: project_program_id description: "pool program id of the project" + - &project_main_id + name: project_main_id + description: "main program id of the project" - &trader_id name: trader_id description: "id (address) of trader who purchased a token" @@ -123,6 +129,7 @@ models: - *project - *version - *block_time + - *block_slot - *trade_source - *token_bought_symbol - *token_sold_symbol @@ -139,6 +146,7 @@ models: - *token_bought_vault - *token_sold_vault - *project_program_id + - *project_main_id - *trader_id - *tx_id - *outer_instruction_index