From b886e9432e433a8b37a9fd5973dd911b23b47d07 Mon Sep 17 00:00:00 2001 From: Andrew <47720952+andrewhong5297@users.noreply.github.com> Date: Tue, 4 Jun 2024 14:04:07 -0400 Subject: [PATCH 01/27] fix a few first --- models/phoenix/phoenix_v1_trades.sql | 4 ++- models/raydium/raydium_v3_trades.sql | 49 ++++++++++++++-------------- models/raydium/raydium_v4_trades.sql | 46 +++++++++++++------------- 3 files changed, 49 insertions(+), 50 deletions(-) diff --git a/models/phoenix/phoenix_v1_trades.sql b/models/phoenix/phoenix_v1_trades.sql index 73988e49756..c0e75c30c1e 100644 --- a/models/phoenix/phoenix_v1_trades.sql +++ b/models/phoenix/phoenix_v1_trades.sql @@ -128,6 +128,7 @@ , 'phoenix' as project , 1 as version , 'solana' as blockchain + , l.call_block_slot as block_slot , case when s.call_outer_executing_account = 'PhoeNiXZ8ByJGLkxNfZRnkUfjvmuYqLR89jjFHGqdXY' then 'direct' else s.call_outer_executing_account end as trade_source @@ -154,7 +155,7 @@ else l.tokenB_filled/pow(10,p.tokenB_decimals) end token_sold_amount , p.pool_id - , s.account_trader as trader_id + , s.call_tx_signer as trader_id , s.call_tx_id as tx_id , s.call_outer_instruction_index as outer_instruction_index , COALESCE(s.call_inner_instruction_index,0) as inner_instruction_index @@ -196,6 +197,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 diff --git a/models/raydium/raydium_v3_trades.sql b/models/raydium/raydium_v3_trades.sql index f18af20411c..c542f0c7ccb 100644 --- a/models/raydium/raydium_v3_trades.sql +++ b/models/raydium/raydium_v3_trades.sql @@ -47,6 +47,7 @@ , 'raydium' as project , 3 as version , 'solana' as blockchain + , call_block_slot as block_slot , case when sp.call_is_inner = False then 'direct' else sp.call_outer_executing_account end as trade_source @@ -54,33 +55,33 @@ 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 FROM ( @@ -109,29 +110,26 @@ INNER JOIN pools p ON sp.account_poolState = p.pool_id --account 2 and p.recent_init = 1 --for some reason, some pools get created twice. - 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)) {% 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 %} - 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 (sp.call_is_inner = true AND tr_2.call_inner_instruction_index = sp.call_inner_instruction_index + 2)) + 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 (sp.call_is_inner = true AND tr_2.inner_instruction_index = sp.call_inner_instruction_index + 2)) {% 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 - --comment to force run for testing ) SELECT @@ -140,6 +138,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 diff --git a/models/raydium/raydium_v4_trades.sql b/models/raydium/raydium_v4_trades.sql index dcfb1425b07..a41d7724446 100644 --- a/models/raydium/raydium_v4_trades.sql +++ b/models/raydium/raydium_v4_trades.sql @@ -45,8 +45,8 @@ , 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 - , COALESCE(tk_2.token_mint_address, cast(null as varchar)) as token_bought_mint_address - , COALESCE(tk_1.token_mint_address, cast(null as varchar)) as token_sold_mint_address + , COALESCE(trs_2.token_mint_address, cast(null as varchar)) as token_bought_mint_address + , COALESCE(trs_1.token_mint_address, cast(null as varchar)) as token_sold_mint_address , trs_2.account_source as token_bought_vault , trs_1.account_destination as token_sold_vault FROM ( @@ -56,38 +56,36 @@ SELECT account_serumMarket, account_amm, call_is_inner, call_outer_instruction_index, call_inner_instruction_index, call_tx_id, call_block_time, call_block_slot, call_outer_executing_account, call_tx_signer, call_tx_index FROM {{ source('raydium_amm_solana', 'raydium_amm_call_swapBaseIn') }} ) sp - INNER JOIN {{ source('spl_token_solana', 'spl_token_call_transfer') }} trs_1 - ON trs_1.call_tx_id = sp.call_tx_id - AND trs_1.call_block_time = sp.call_block_time - AND trs_1.call_outer_instruction_index = sp.call_outer_instruction_index - AND ((sp.call_is_inner = false AND (trs_1.call_inner_instruction_index = 1 OR trs_1.call_inner_instruction_index = 2)) - OR (sp.call_is_inner = true AND (trs_1.call_inner_instruction_index = sp.call_inner_instruction_index + 1 OR trs_1.call_inner_instruction_index = sp.call_inner_instruction_index + 2)) + INNER JOIN {{ ref('tokens_solana_transfers') }} trs_1 + ON trs_1.tx_id = sp.call_tx_id + AND trs_1.block_time = sp.call_block_time + AND trs_1.outer_instruction_index = sp.call_outer_instruction_index + AND ((sp.call_is_inner = false AND (trs_1.inner_instruction_index = 1 OR trs_1.inner_instruction_index = 2)) + OR (sp.call_is_inner = true AND (trs_1.inner_instruction_index = sp.call_inner_instruction_index + 1 OR trs_1.inner_instruction_index = sp.call_inner_instruction_index + 2)) ) {% if is_incremental() %} - AND {{incremental_predicate('trs_1.call_block_time')}} + AND {{incremental_predicate('trs_1.block_time')}} {% else %} - AND trs_1.call_block_time >= TIMESTAMP '{{project_start_date}}' + AND trs_1.block_time >= TIMESTAMP '{{project_start_date}}' {% endif %} - INNER JOIN {{ source('spl_token_solana', 'spl_token_call_transfer') }} trs_2 - ON trs_2.call_tx_id = sp.call_tx_id - AND trs_2.call_block_time = sp.call_block_time - AND trs_2.call_outer_instruction_index = sp.call_outer_instruction_index - AND ((sp.call_is_inner = false AND (trs_2.call_inner_instruction_index = 2 OR trs_2.call_inner_instruction_index = 3)) - OR (sp.call_is_inner = true AND (trs_2.call_inner_instruction_index = sp.call_inner_instruction_index + 2 OR trs_2.call_inner_instruction_index = sp.call_inner_instruction_index + 3)) + INNER JOIN {{ ref('tokens_solana_transfers') }} trs_2 + ON trs_2.tx_id = sp.call_tx_id + AND trs_2.block_time = sp.call_block_time + AND trs_2.outer_instruction_index = sp.call_outer_instruction_index + AND ((sp.call_is_inner = false AND (trs_2.inner_instruction_index = 2 OR trs_2.inner_instruction_index = 3)) + OR (sp.call_is_inner = true AND (trs_2.inner_instruction_index = sp.call_inner_instruction_index + 2 OR trs_2.inner_instruction_index = sp.call_inner_instruction_index + 3)) ) {% if is_incremental() %} - AND {{incremental_predicate('trs_2.call_block_time')}} + AND {{incremental_predicate('trs_2.block_time')}} {% else %} - AND trs_2.call_block_time >= TIMESTAMP '{{project_start_date}}' + AND trs_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 = trs_1.account_destination LEFT JOIN {{ ref('solana_utils_token_accounts') }} tk_2 ON tk_2.address = trs_2.account_source - LEFT JOIN {{ ref('tokens_solana_fungible') }} dec_1 ON dec_1.token_mint_address = tk_1.token_mint_address - LEFT JOIN {{ ref('tokens_solana_fungible') }} dec_2 ON dec_2.token_mint_address = tk_2.token_mint_address + LEFT JOIN {{ ref('tokens_solana_fungible') }} dec_1 ON dec_1.token_mint_address = trs_1.token_mint_address + LEFT JOIN {{ ref('tokens_solana_fungible') }} dec_2 ON dec_2.token_mint_address = trs_2.token_mint_address WHERE 1=1 - and tk_1.token_mint_address != tk_2.token_mint_address --gets rid of dupes from the OR statement in transfer joins - and tk_2.token_balance_owner = '5Q544fKrFoe6tsEbD7S8EmxGTJYAKtTVhAW5Q5pge4j1' --raydium pool v4 authority. makes sure we don't accidently catch some fee transfer or something after the swap. should add for lifinity too later. + and trs_1.token_mint_address != trs_2.token_mint_address --gets rid of dupes from the OR statement in transfer joins + and trs_2.token_balance_owner = '5Q544fKrFoe6tsEbD7S8EmxGTJYAKtTVhAW5Q5pge4j1' --raydium pool v4 authority. makes sure we don't accidently catch some fee transfer or something after the swap. should add for lifinity too later. {% if is_incremental() %} AND {{incremental_predicate('sp.call_block_time')}} {% else %} From 9206d0e760163f4c0442c833c9fd49b6e7b5642e Mon Sep 17 00:00:00 2001 From: Andrew <47720952+andrewhong5297@users.noreply.github.com> Date: Tue, 4 Jun 2024 18:45:21 -0400 Subject: [PATCH 02/27] ssss --- models/raydium/raydium_v4_trades.sql | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/models/raydium/raydium_v4_trades.sql b/models/raydium/raydium_v4_trades.sql index a41d7724446..696f8ffb7e0 100644 --- a/models/raydium/raydium_v4_trades.sql +++ b/models/raydium/raydium_v4_trades.sql @@ -47,8 +47,8 @@ , sp.call_tx_index as tx_index , COALESCE(trs_2.token_mint_address, cast(null as varchar)) as token_bought_mint_address , COALESCE(trs_1.token_mint_address, cast(null as varchar)) as token_sold_mint_address - , trs_2.account_source as token_bought_vault - , trs_1.account_destination as token_sold_vault + , trs_2.from_token_account as token_bought_vault + , trs_1.to_token_account as token_sold_vault FROM ( SELECT account_serumMarket, account_amm, call_is_inner, call_outer_instruction_index, call_inner_instruction_index, call_tx_id, call_block_time, call_block_slot, call_outer_executing_account, call_tx_signer, call_tx_index FROM {{ source('raydium_amm_solana', 'raydium_amm_call_swapBaseOut') }} @@ -80,7 +80,7 @@ {% else %} AND trs_2.block_time >= TIMESTAMP '{{project_start_date}}' {% endif %} - LEFT JOIN {{ ref('solana_utils_token_accounts') }} tk_2 ON tk_2.address = trs_2.account_source + LEFT JOIN {{ ref('solana_utils_token_accounts') }} tk_2 ON tk_2.address = trs_2.from_token_account LEFT JOIN {{ ref('tokens_solana_fungible') }} dec_1 ON dec_1.token_mint_address = trs_1.token_mint_address LEFT JOIN {{ ref('tokens_solana_fungible') }} dec_2 ON dec_2.token_mint_address = trs_2.token_mint_address WHERE 1=1 From 58c25e684ab2b1975ce72d8f4a457fe8e74950d3 Mon Sep 17 00:00:00 2001 From: Andrew <47720952+andrewhong5297@users.noreply.github.com> Date: Tue, 4 Jun 2024 21:38:05 -0400 Subject: [PATCH 03/27] change --- models/raydium/raydium_v4_trades.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/raydium/raydium_v4_trades.sql b/models/raydium/raydium_v4_trades.sql index 696f8ffb7e0..5bcbbfe342d 100644 --- a/models/raydium/raydium_v4_trades.sql +++ b/models/raydium/raydium_v4_trades.sql @@ -85,7 +85,7 @@ LEFT JOIN {{ ref('tokens_solana_fungible') }} dec_2 ON dec_2.token_mint_address = trs_2.token_mint_address WHERE 1=1 and trs_1.token_mint_address != trs_2.token_mint_address --gets rid of dupes from the OR statement in transfer joins - and trs_2.token_balance_owner = '5Q544fKrFoe6tsEbD7S8EmxGTJYAKtTVhAW5Q5pge4j1' --raydium pool v4 authority. makes sure we don't accidently catch some fee transfer or something after the swap. should add for lifinity too later. + and tk_2.token_balance_owner = '5Q544fKrFoe6tsEbD7S8EmxGTJYAKtTVhAW5Q5pge4j1' --raydium pool v4 authority. makes sure we don't accidently catch some fee transfer or something after the swap. should add for lifinity too later. {% if is_incremental() %} AND {{incremental_predicate('sp.call_block_time')}} {% else %} From 6637703cb7515c4a5b473761cb11f4bc0be10403 Mon Sep 17 00:00:00 2001 From: Andrew <47720952+andrewhong5297@users.noreply.github.com> Date: Wed, 5 Jun 2024 04:41:55 -0400 Subject: [PATCH 04/27] shorter --- models/phoenix/phoenix_v1_trades.sql | 1 + models/raydium/raydium_v3_trades.sql | 21 +++++++-------------- models/raydium/raydium_v4_trades.sql | 3 ++- 3 files changed, 10 insertions(+), 15 deletions(-) diff --git a/models/phoenix/phoenix_v1_trades.sql b/models/phoenix/phoenix_v1_trades.sql index c0e75c30c1e..4cfad297280 100644 --- a/models/phoenix/phoenix_v1_trades.sql +++ b/models/phoenix/phoenix_v1_trades.sql @@ -101,6 +101,7 @@ {% if is_incremental() %} AND {{incremental_predicate('l.call_block_time')}} {% endif %} + AND call_block_time >= now() - interval '7 day' --qa ), max_log_index AS ( SELECT diff --git a/models/raydium/raydium_v3_trades.sql b/models/raydium/raydium_v3_trades.sql index c542f0c7ccb..e7ce0a714f8 100644 --- a/models/raydium/raydium_v3_trades.sql +++ b/models/raydium/raydium_v3_trades.sql @@ -88,24 +88,10 @@ SELECT account_poolState , call_is_inner, call_outer_instruction_index, call_inner_instruction_index, call_tx_id, call_block_time, call_block_slot, call_outer_executing_account, call_tx_signer, call_tx_index FROM {{ source('raydium_clmm_solana', 'amm_v3_call_swap') }} - WHERE 1=1 - {% if is_incremental() %} - AND {{incremental_predicate('call_block_time')}} - {% else %} - AND call_block_time >= TIMESTAMP '{{project_start_date}}' - {% endif %} - UNION ALL - SELECT account_poolState , call_is_inner, call_outer_instruction_index, call_inner_instruction_index, call_tx_id, call_block_time, call_block_slot, call_outer_executing_account, call_tx_signer, call_tx_index FROM {{ source('raydium_clmm_solana', 'amm_v3_call_swapV2') }} - WHERE 1=1 - {% if is_incremental() %} - AND {{incremental_predicate('call_block_time')}} - {% else %} - AND call_block_time >= TIMESTAMP '{{project_start_date}}' - {% endif %} ) sp INNER JOIN pools p ON sp.account_poolState = p.pool_id --account 2 @@ -130,6 +116,13 @@ {% else %} AND tr_2.block_time >= TIMESTAMP '{{project_start_date}}' {% endif %} + WHERE 1=1 + {% if is_incremental() %} + AND {{incremental_predicate('call_block_time')}} + {% else %} + -- AND call_block_time >= TIMESTAMP '{{project_start_date}}' + AND call_block_time >= now() - interval '7 day' + {% endif %} ) SELECT diff --git a/models/raydium/raydium_v4_trades.sql b/models/raydium/raydium_v4_trades.sql index 5bcbbfe342d..a82b0d6461e 100644 --- a/models/raydium/raydium_v4_trades.sql +++ b/models/raydium/raydium_v4_trades.sql @@ -89,7 +89,8 @@ {% if is_incremental() %} AND {{incremental_predicate('sp.call_block_time')}} {% else %} - AND sp.call_block_time >= TIMESTAMP '{{project_start_date}}' + -- AND sp.call_block_time >= TIMESTAMP '{{project_start_date}}' + AND sp.call_block_time >= now() - interval '7 day' --qa {% endif %} --force ) From 21a5f10055acfda1fa4bed20a6319c39d66c038c Mon Sep 17 00:00:00 2001 From: Andrew <47720952+andrewhong5297@users.noreply.github.com> Date: Wed, 5 Jun 2024 05:00:59 -0400 Subject: [PATCH 05/27] lol --- models/phoenix/phoenix_v1_trades.sql | 2 +- models/raydium/raydium_v3_trades.sql | 2 +- models/raydium/raydium_v4_trades.sql | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/models/phoenix/phoenix_v1_trades.sql b/models/phoenix/phoenix_v1_trades.sql index 4cfad297280..02ee501e5c2 100644 --- a/models/phoenix/phoenix_v1_trades.sql +++ b/models/phoenix/phoenix_v1_trades.sql @@ -101,7 +101,7 @@ {% if is_incremental() %} AND {{incremental_predicate('l.call_block_time')}} {% endif %} - AND call_block_time >= now() - interval '7 day' --qa + AND call_block_time >= now() - interval '7 day' day --qa ), max_log_index AS ( SELECT diff --git a/models/raydium/raydium_v3_trades.sql b/models/raydium/raydium_v3_trades.sql index e7ce0a714f8..d5547cba37e 100644 --- a/models/raydium/raydium_v3_trades.sql +++ b/models/raydium/raydium_v3_trades.sql @@ -121,7 +121,7 @@ AND {{incremental_predicate('call_block_time')}} {% else %} -- AND call_block_time >= TIMESTAMP '{{project_start_date}}' - AND call_block_time >= now() - interval '7 day' + AND call_block_time >= now() - interval '7' day {% endif %} ) diff --git a/models/raydium/raydium_v4_trades.sql b/models/raydium/raydium_v4_trades.sql index a82b0d6461e..30bc0af52f9 100644 --- a/models/raydium/raydium_v4_trades.sql +++ b/models/raydium/raydium_v4_trades.sql @@ -90,7 +90,7 @@ AND {{incremental_predicate('sp.call_block_time')}} {% else %} -- AND sp.call_block_time >= TIMESTAMP '{{project_start_date}}' - AND sp.call_block_time >= now() - interval '7 day' --qa + AND sp.call_block_time >= now() - interval '7' day --qa {% endif %} --force ) From 9255c72b598a19bbfcb8b37ae5de7c871258d765 Mon Sep 17 00:00:00 2001 From: Andrew <47720952+andrewhong5297@users.noreply.github.com> Date: Wed, 5 Jun 2024 06:00:23 -0400 Subject: [PATCH 06/27] fix --- models/phoenix/phoenix_v1_trades.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/phoenix/phoenix_v1_trades.sql b/models/phoenix/phoenix_v1_trades.sql index 02ee501e5c2..f3757d0690f 100644 --- a/models/phoenix/phoenix_v1_trades.sql +++ b/models/phoenix/phoenix_v1_trades.sql @@ -101,7 +101,7 @@ {% if is_incremental() %} AND {{incremental_predicate('l.call_block_time')}} {% endif %} - AND call_block_time >= now() - interval '7 day' day --qa + AND call_block_time >= now() - interval '7' day --qa ), max_log_index AS ( SELECT From 63e2a309c933f9734bf3de1f4be09f815d62dbd6 Mon Sep 17 00:00:00 2001 From: Andrew <47720952+andrewhong5297@users.noreply.github.com> Date: Wed, 5 Jun 2024 09:20:32 -0400 Subject: [PATCH 07/27] add more QA filter time rows --- models/phoenix/phoenix_v1_trades.sql | 1 + models/raydium/raydium_v3_trades.sql | 6 ++++-- models/raydium/raydium_v4_trades.sql | 12 ++++++++---- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/models/phoenix/phoenix_v1_trades.sql b/models/phoenix/phoenix_v1_trades.sql index f3757d0690f..84084f49951 100644 --- a/models/phoenix/phoenix_v1_trades.sql +++ b/models/phoenix/phoenix_v1_trades.sql @@ -184,6 +184,7 @@ {% if is_incremental() %} WHERE {{incremental_predicate('call_block_time')}} {% endif %} + AND call_block_time >= now() - interval '7' day --qa ) s ON s.call_block_slot = l.call_block_slot AND s.call_tx_id = l.call_tx_id AND s.account_market = l.market diff --git a/models/raydium/raydium_v3_trades.sql b/models/raydium/raydium_v3_trades.sql index d5547cba37e..0eb8c783d9d 100644 --- a/models/raydium/raydium_v3_trades.sql +++ b/models/raydium/raydium_v3_trades.sql @@ -104,7 +104,8 @@ {% if is_incremental() %} AND {{incremental_predicate('tr_1.block_time')}} {% else %} - AND tr_1.block_time >= TIMESTAMP '{{project_start_date}}' + -- AND tr_1.block_time >= TIMESTAMP '{{project_start_date}}' + AND tr_1.block_time >= now() - interval '7' day {% endif %} INNER JOIN {{ ref('tokens_solana_transfers') }} tr_2 ON tr_2.tx_id = sp.call_tx_id @@ -114,7 +115,8 @@ {% if is_incremental() %} AND {{incremental_predicate('tr_2.block_time')}} {% else %} - AND tr_2.block_time >= TIMESTAMP '{{project_start_date}}' + -- AND tr_2.block_time >= TIMESTAMP '{{project_start_date}}' + AND tr_2.block_time >= now() - interval '7' day {% endif %} WHERE 1=1 {% if is_incremental() %} diff --git a/models/raydium/raydium_v4_trades.sql b/models/raydium/raydium_v4_trades.sql index 30bc0af52f9..0d352b81b15 100644 --- a/models/raydium/raydium_v4_trades.sql +++ b/models/raydium/raydium_v4_trades.sql @@ -66,7 +66,8 @@ {% if is_incremental() %} AND {{incremental_predicate('trs_1.block_time')}} {% else %} - AND trs_1.block_time >= TIMESTAMP '{{project_start_date}}' + -- AND trs_1.block_time >= TIMESTAMP '{{project_start_date}}' + AND trs_1.block_time >= now() - interval '7' day {% endif %} INNER JOIN {{ ref('tokens_solana_transfers') }} trs_2 ON trs_2.tx_id = sp.call_tx_id @@ -78,7 +79,8 @@ {% if is_incremental() %} AND {{incremental_predicate('trs_2.block_time')}} {% else %} - AND trs_2.block_time >= TIMESTAMP '{{project_start_date}}' + -- AND trs_2.block_time >= TIMESTAMP '{{project_start_date}}' + AND trs_2.block_time >= now() - interval '7' day {% endif %} LEFT JOIN {{ ref('solana_utils_token_accounts') }} tk_2 ON tk_2.address = trs_2.from_token_account LEFT JOIN {{ ref('tokens_solana_fungible') }} dec_1 ON dec_1.token_mint_address = trs_1.token_mint_address @@ -129,7 +131,8 @@ LEFT JOIN {{ source('prices', 'usd') }} p_bought ON p_bought.blockchain = 'solan {% if is_incremental() %} AND {{incremental_predicate('p_bought.minute')}} {% else %} - AND p_bought.minute >= TIMESTAMP '{{project_start_date}}' + -- AND p_bought.minute >= TIMESTAMP '{{project_start_date}}' + AND p_bought.minute >= now() - interval '7' day {% endif %} LEFT JOIN {{ source('prices', 'usd') }} p_sold ON p_sold.blockchain = 'solana' AND date_trunc('minute', tb.block_time) = p_sold.minute @@ -137,5 +140,6 @@ LEFT JOIN {{ source('prices', 'usd') }} p_sold ON p_sold.blockchain = 'solana' {% if is_incremental() %} AND {{incremental_predicate('p_sold.minute')}} {% else %} - AND p_sold.minute >= TIMESTAMP '{{project_start_date}}' + -- AND p_sold.minute >= TIMESTAMP '{{project_start_date}}' + AND p_sold.minute >= now() - interval '7' day {% endif %} \ No newline at end of file From a9427edac33bdab69e32098353a51ec56d2092ef Mon Sep 17 00:00:00 2001 From: Andrew <47720952+andrewhong5297@users.noreply.github.com> Date: Wed, 5 Jun 2024 10:06:54 -0400 Subject: [PATCH 08/27] fix --- models/phoenix/phoenix_v1_trades.sql | 2 +- models/raydium/raydium_v3_trades.sql | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/models/phoenix/phoenix_v1_trades.sql b/models/phoenix/phoenix_v1_trades.sql index 84084f49951..873eaa3517b 100644 --- a/models/phoenix/phoenix_v1_trades.sql +++ b/models/phoenix/phoenix_v1_trades.sql @@ -184,7 +184,7 @@ {% if is_incremental() %} WHERE {{incremental_predicate('call_block_time')}} {% endif %} - AND call_block_time >= now() - interval '7' day --qa + WHERE call_block_time >= now() - interval '7' day --qa ) s ON s.call_block_slot = l.call_block_slot AND s.call_tx_id = l.call_tx_id AND s.account_market = l.market diff --git a/models/raydium/raydium_v3_trades.sql b/models/raydium/raydium_v3_trades.sql index 0eb8c783d9d..f22eeb63bdc 100644 --- a/models/raydium/raydium_v3_trades.sql +++ b/models/raydium/raydium_v3_trades.sql @@ -162,7 +162,8 @@ LEFT JOIN {{ source('prices', 'usd') }} p_bought ON p_bought.blockchain = 'solan {% if is_incremental() %} AND {{incremental_predicate('p_bought.minute')}} {% else %} - AND p_bought.minute >= TIMESTAMP '{{project_start_date}}' + -- AND p_bought.minute >= TIMESTAMP '{{project_start_date}}' + AND p_bought.minute >= now() - interval '7' day {% endif %} LEFT JOIN {{ source('prices', 'usd') }} p_sold ON p_sold.blockchain = 'solana' AND date_trunc('minute', tb.block_time) = p_sold.minute @@ -170,5 +171,6 @@ LEFT JOIN {{ source('prices', 'usd') }} p_sold ON p_sold.blockchain = 'solana' {% if is_incremental() %} AND {{incremental_predicate('p_sold.minute')}} {% else %} - AND p_sold.minute >= TIMESTAMP '{{project_start_date}}' + -- AND p_sold.minute >= TIMESTAMP '{{project_start_date}}' + AND p_sold.minute >= now() - interval '7' day {% endif %} \ No newline at end of file From 6cd73689001bc93ca835a0bb4cd15ea8c1c8227a Mon Sep 17 00:00:00 2001 From: Andrew <47720952+andrewhong5297@users.noreply.github.com> Date: Wed, 5 Jun 2024 11:17:55 -0400 Subject: [PATCH 09/27] fix --- models/phoenix/phoenix_v1_trades.sql | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/models/phoenix/phoenix_v1_trades.sql b/models/phoenix/phoenix_v1_trades.sql index 873eaa3517b..e8475a618fd 100644 --- a/models/phoenix/phoenix_v1_trades.sql +++ b/models/phoenix/phoenix_v1_trades.sql @@ -181,10 +181,11 @@ * , 2 * bytearray_to_integer (bytearray_substring (call_data, 3, 1)) - 1 as side --if side = 1 then tokenB was bought, else tokenA was bought FROM {{ source('phoenix_v1_solana','phoenix_v1_call_Swap') }} + WHERE 1=1 {% if is_incremental() %} - WHERE {{incremental_predicate('call_block_time')}} + AND {{incremental_predicate('call_block_time')}} {% endif %} - WHERE call_block_time >= now() - interval '7' day --qa + AND call_block_time >= now() - interval '7' day --qa ) s ON s.call_block_slot = l.call_block_slot AND s.call_tx_id = l.call_tx_id AND s.account_market = l.market From f8d54a19181f1bf1f2bd61fd96b0b5270e72c004 Mon Sep 17 00:00:00 2001 From: Andrew <47720952+andrewhong5297@users.noreply.github.com> Date: Wed, 5 Jun 2024 15:14:09 -0400 Subject: [PATCH 10/27] revert raydium --- models/raydium/raydium_v3_trades.sql | 78 +++++++++++++++------------- 1 file changed, 41 insertions(+), 37 deletions(-) diff --git a/models/raydium/raydium_v3_trades.sql b/models/raydium/raydium_v3_trades.sql index f22eeb63bdc..f18af20411c 100644 --- a/models/raydium/raydium_v3_trades.sql +++ b/models/raydium/raydium_v3_trades.sql @@ -47,7 +47,6 @@ , 'raydium' as project , 3 as version , 'solana' as blockchain - , call_block_slot as block_slot , case when sp.call_is_inner = False then 'direct' else sp.call_outer_executing_account end as trade_source @@ -55,76 +54,84 @@ 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 tr_1.token_mint_address = p.tokenA then COALESCE(tokenB_symbol, tokenB) + , case when tk_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 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) + , 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) 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 tr_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 tk_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 tr_1.token_mint_address = p.tokenA then p.tokenB + , case when tk_1.token_mint_address = p.tokenA then p.tokenB else p.tokenA end as token_bought_mint_address - , case when tr_1.token_mint_address = p.tokenA then p.tokenA + , case when tk_1.token_mint_address = p.tokenA then p.tokenA else p.tokenB end as token_sold_mint_address - , case when tr_1.token_mint_address = p.tokenA then p.tokenBVault + , case when tk_1.token_mint_address = p.tokenA then p.tokenBVault else p.tokenAVault end as token_bought_vault - , case when tr_1.token_mint_address = p.tokenA then p.tokenAVault + , case when tk_1.token_mint_address = p.tokenA then p.tokenAVault else p.tokenBVault end as token_sold_vault FROM ( SELECT account_poolState , call_is_inner, call_outer_instruction_index, call_inner_instruction_index, call_tx_id, call_block_time, call_block_slot, call_outer_executing_account, call_tx_signer, call_tx_index FROM {{ source('raydium_clmm_solana', 'amm_v3_call_swap') }} + WHERE 1=1 + {% if is_incremental() %} + AND {{incremental_predicate('call_block_time')}} + {% else %} + AND call_block_time >= TIMESTAMP '{{project_start_date}}' + {% endif %} + UNION ALL + SELECT account_poolState , call_is_inner, call_outer_instruction_index, call_inner_instruction_index, call_tx_id, call_block_time, call_block_slot, call_outer_executing_account, call_tx_signer, call_tx_index FROM {{ source('raydium_clmm_solana', 'amm_v3_call_swapV2') }} + WHERE 1=1 + {% if is_incremental() %} + AND {{incremental_predicate('call_block_time')}} + {% else %} + AND call_block_time >= TIMESTAMP '{{project_start_date}}' + {% endif %} ) sp INNER JOIN pools p ON sp.account_poolState = p.pool_id --account 2 and p.recent_init = 1 --for some reason, some pools get created twice. - 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)) - {% if is_incremental() %} - AND {{incremental_predicate('tr_1.block_time')}} - {% else %} - -- AND tr_1.block_time >= TIMESTAMP '{{project_start_date}}' - AND tr_1.block_time >= now() - interval '7' day - {% endif %} - 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 (sp.call_is_inner = true AND tr_2.inner_instruction_index = sp.call_inner_instruction_index + 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)) {% if is_incremental() %} - AND {{incremental_predicate('tr_2.block_time')}} + AND {{incremental_predicate('tr_1.call_block_time')}} {% else %} - -- AND tr_2.block_time >= TIMESTAMP '{{project_start_date}}' - AND tr_2.block_time >= now() - interval '7' day + AND tr_1.call_block_time >= TIMESTAMP '{{project_start_date}}' {% endif %} - WHERE 1=1 + 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 (sp.call_is_inner = true AND tr_2.call_inner_instruction_index = sp.call_inner_instruction_index + 2)) {% if is_incremental() %} - AND {{incremental_predicate('call_block_time')}} + AND {{incremental_predicate('tr_2.call_block_time')}} {% else %} - -- AND call_block_time >= TIMESTAMP '{{project_start_date}}' - AND call_block_time >= now() - interval '7' day + AND tr_2.call_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 + --comment to force run for testing ) SELECT @@ -133,7 +140,6 @@ 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 @@ -162,8 +168,7 @@ LEFT JOIN {{ source('prices', 'usd') }} p_bought ON p_bought.blockchain = 'solan {% if is_incremental() %} AND {{incremental_predicate('p_bought.minute')}} {% else %} - -- AND p_bought.minute >= TIMESTAMP '{{project_start_date}}' - AND p_bought.minute >= now() - interval '7' day + AND p_bought.minute >= TIMESTAMP '{{project_start_date}}' {% endif %} LEFT JOIN {{ source('prices', 'usd') }} p_sold ON p_sold.blockchain = 'solana' AND date_trunc('minute', tb.block_time) = p_sold.minute @@ -171,6 +176,5 @@ LEFT JOIN {{ source('prices', 'usd') }} p_sold ON p_sold.blockchain = 'solana' {% if is_incremental() %} AND {{incremental_predicate('p_sold.minute')}} {% else %} - -- AND p_sold.minute >= TIMESTAMP '{{project_start_date}}' - AND p_sold.minute >= now() - interval '7' day + AND p_sold.minute >= TIMESTAMP '{{project_start_date}}' {% endif %} \ No newline at end of file From 1d8891514e8721d35bbc8ca1d081470eb6e7fd46 Mon Sep 17 00:00:00 2001 From: Andrew <47720952+andrewhong5297@users.noreply.github.com> Date: Wed, 5 Jun 2024 15:14:32 -0400 Subject: [PATCH 11/27] revert raydium v4 --- models/raydium/raydium_v4_trades.sql | 61 +++++++++++++--------------- 1 file changed, 29 insertions(+), 32 deletions(-) diff --git a/models/raydium/raydium_v4_trades.sql b/models/raydium/raydium_v4_trades.sql index 0d352b81b15..dcfb1425b07 100644 --- a/models/raydium/raydium_v4_trades.sql +++ b/models/raydium/raydium_v4_trades.sql @@ -45,10 +45,10 @@ , 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 - , COALESCE(trs_2.token_mint_address, cast(null as varchar)) as token_bought_mint_address - , COALESCE(trs_1.token_mint_address, cast(null as varchar)) as token_sold_mint_address - , trs_2.from_token_account as token_bought_vault - , trs_1.to_token_account as token_sold_vault + , COALESCE(tk_2.token_mint_address, cast(null as varchar)) as token_bought_mint_address + , COALESCE(tk_1.token_mint_address, cast(null as varchar)) as token_sold_mint_address + , trs_2.account_source as token_bought_vault + , trs_1.account_destination as token_sold_vault FROM ( SELECT account_serumMarket, account_amm, call_is_inner, call_outer_instruction_index, call_inner_instruction_index, call_tx_id, call_block_time, call_block_slot, call_outer_executing_account, call_tx_signer, call_tx_index FROM {{ source('raydium_amm_solana', 'raydium_amm_call_swapBaseOut') }} @@ -56,43 +56,42 @@ SELECT account_serumMarket, account_amm, call_is_inner, call_outer_instruction_index, call_inner_instruction_index, call_tx_id, call_block_time, call_block_slot, call_outer_executing_account, call_tx_signer, call_tx_index FROM {{ source('raydium_amm_solana', 'raydium_amm_call_swapBaseIn') }} ) sp - INNER JOIN {{ ref('tokens_solana_transfers') }} trs_1 - ON trs_1.tx_id = sp.call_tx_id - AND trs_1.block_time = sp.call_block_time - AND trs_1.outer_instruction_index = sp.call_outer_instruction_index - AND ((sp.call_is_inner = false AND (trs_1.inner_instruction_index = 1 OR trs_1.inner_instruction_index = 2)) - OR (sp.call_is_inner = true AND (trs_1.inner_instruction_index = sp.call_inner_instruction_index + 1 OR trs_1.inner_instruction_index = sp.call_inner_instruction_index + 2)) + INNER JOIN {{ source('spl_token_solana', 'spl_token_call_transfer') }} trs_1 + ON trs_1.call_tx_id = sp.call_tx_id + AND trs_1.call_block_time = sp.call_block_time + AND trs_1.call_outer_instruction_index = sp.call_outer_instruction_index + AND ((sp.call_is_inner = false AND (trs_1.call_inner_instruction_index = 1 OR trs_1.call_inner_instruction_index = 2)) + OR (sp.call_is_inner = true AND (trs_1.call_inner_instruction_index = sp.call_inner_instruction_index + 1 OR trs_1.call_inner_instruction_index = sp.call_inner_instruction_index + 2)) ) {% if is_incremental() %} - AND {{incremental_predicate('trs_1.block_time')}} + AND {{incremental_predicate('trs_1.call_block_time')}} {% else %} - -- AND trs_1.block_time >= TIMESTAMP '{{project_start_date}}' - AND trs_1.block_time >= now() - interval '7' day + AND trs_1.call_block_time >= TIMESTAMP '{{project_start_date}}' {% endif %} - INNER JOIN {{ ref('tokens_solana_transfers') }} trs_2 - ON trs_2.tx_id = sp.call_tx_id - AND trs_2.block_time = sp.call_block_time - AND trs_2.outer_instruction_index = sp.call_outer_instruction_index - AND ((sp.call_is_inner = false AND (trs_2.inner_instruction_index = 2 OR trs_2.inner_instruction_index = 3)) - OR (sp.call_is_inner = true AND (trs_2.inner_instruction_index = sp.call_inner_instruction_index + 2 OR trs_2.inner_instruction_index = sp.call_inner_instruction_index + 3)) + INNER JOIN {{ source('spl_token_solana', 'spl_token_call_transfer') }} trs_2 + ON trs_2.call_tx_id = sp.call_tx_id + AND trs_2.call_block_time = sp.call_block_time + AND trs_2.call_outer_instruction_index = sp.call_outer_instruction_index + AND ((sp.call_is_inner = false AND (trs_2.call_inner_instruction_index = 2 OR trs_2.call_inner_instruction_index = 3)) + OR (sp.call_is_inner = true AND (trs_2.call_inner_instruction_index = sp.call_inner_instruction_index + 2 OR trs_2.call_inner_instruction_index = sp.call_inner_instruction_index + 3)) ) {% if is_incremental() %} - AND {{incremental_predicate('trs_2.block_time')}} + AND {{incremental_predicate('trs_2.call_block_time')}} {% else %} - -- AND trs_2.block_time >= TIMESTAMP '{{project_start_date}}' - AND trs_2.block_time >= now() - interval '7' day + AND trs_2.call_block_time >= TIMESTAMP '{{project_start_date}}' {% endif %} - LEFT JOIN {{ ref('solana_utils_token_accounts') }} tk_2 ON tk_2.address = trs_2.from_token_account - LEFT JOIN {{ ref('tokens_solana_fungible') }} dec_1 ON dec_1.token_mint_address = trs_1.token_mint_address - LEFT JOIN {{ ref('tokens_solana_fungible') }} dec_2 ON dec_2.token_mint_address = trs_2.token_mint_address + --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 = trs_1.account_destination + LEFT JOIN {{ ref('solana_utils_token_accounts') }} tk_2 ON tk_2.address = trs_2.account_source + LEFT JOIN {{ ref('tokens_solana_fungible') }} dec_1 ON dec_1.token_mint_address = tk_1.token_mint_address + LEFT JOIN {{ ref('tokens_solana_fungible') }} dec_2 ON dec_2.token_mint_address = tk_2.token_mint_address WHERE 1=1 - and trs_1.token_mint_address != trs_2.token_mint_address --gets rid of dupes from the OR statement in transfer joins + and tk_1.token_mint_address != tk_2.token_mint_address --gets rid of dupes from the OR statement in transfer joins and tk_2.token_balance_owner = '5Q544fKrFoe6tsEbD7S8EmxGTJYAKtTVhAW5Q5pge4j1' --raydium pool v4 authority. makes sure we don't accidently catch some fee transfer or something after the swap. should add for lifinity too later. {% if is_incremental() %} AND {{incremental_predicate('sp.call_block_time')}} {% else %} - -- AND sp.call_block_time >= TIMESTAMP '{{project_start_date}}' - AND sp.call_block_time >= now() - interval '7' day --qa + AND sp.call_block_time >= TIMESTAMP '{{project_start_date}}' {% endif %} --force ) @@ -131,8 +130,7 @@ LEFT JOIN {{ source('prices', 'usd') }} p_bought ON p_bought.blockchain = 'solan {% if is_incremental() %} AND {{incremental_predicate('p_bought.minute')}} {% else %} - -- AND p_bought.minute >= TIMESTAMP '{{project_start_date}}' - AND p_bought.minute >= now() - interval '7' day + AND p_bought.minute >= TIMESTAMP '{{project_start_date}}' {% endif %} LEFT JOIN {{ source('prices', 'usd') }} p_sold ON p_sold.blockchain = 'solana' AND date_trunc('minute', tb.block_time) = p_sold.minute @@ -140,6 +138,5 @@ LEFT JOIN {{ source('prices', 'usd') }} p_sold ON p_sold.blockchain = 'solana' {% if is_incremental() %} AND {{incremental_predicate('p_sold.minute')}} {% else %} - -- AND p_sold.minute >= TIMESTAMP '{{project_start_date}}' - AND p_sold.minute >= now() - interval '7' day + AND p_sold.minute >= TIMESTAMP '{{project_start_date}}' {% endif %} \ No newline at end of file From 3ddda232362b7c3ba303daf36b695d0cfe44ad28 Mon Sep 17 00:00:00 2001 From: Andrew <47720952+andrewhong5297@users.noreply.github.com> Date: Wed, 5 Jun 2024 15:15:30 -0400 Subject: [PATCH 12/27] add --- models/phoenix/phoenix_v1_trades.sql | 1 + models/phoenix/schema.yml | 3 +++ 2 files changed, 4 insertions(+) diff --git a/models/phoenix/phoenix_v1_trades.sql b/models/phoenix/phoenix_v1_trades.sql index e8475a618fd..6e5380600fc 100644 --- a/models/phoenix/phoenix_v1_trades.sql +++ b/models/phoenix/phoenix_v1_trades.sql @@ -217,6 +217,7 @@ SELECT , tb.token_sold_vault , tb.token_bought_vault , tb.pool_id as project_program_id + , 'PhoeNiXZ8ByJGLkxNfZRnkUfjvmuYqLR89jjFHGqdXY' as project_main_id , tb.trader_id , tb.tx_id , tb.outer_instruction_index diff --git a/models/phoenix/schema.yml b/models/phoenix/schema.yml index 7e43d0ee1ea..3304ce933b6 100644 --- a/models/phoenix/schema.yml +++ b/models/phoenix/schema.yml @@ -85,6 +85,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" From 1875712f26ab6f42704ebb695ae3ac3f99f92f1c Mon Sep 17 00:00:00 2001 From: Andrew <47720952+andrewhong5297@users.noreply.github.com> Date: Wed, 5 Jun 2024 15:22:34 -0400 Subject: [PATCH 13/27] change --- models/phoenix/schema.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/models/phoenix/schema.yml b/models/phoenix/schema.yml index 3304ce933b6..d312c0184b7 100644 --- a/models/phoenix/schema.yml +++ b/models/phoenix/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)" From e086c577d115e13003a12b6fa59e6f8979c09a6e Mon Sep 17 00:00:00 2001 From: Andrew <47720952+andrewhong5297@users.noreply.github.com> Date: Thu, 6 Jun 2024 09:29:06 -0400 Subject: [PATCH 14/27] push --- models/phoenix/phoenix_v1_trades.sql | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/models/phoenix/phoenix_v1_trades.sql b/models/phoenix/phoenix_v1_trades.sql index 6e5380600fc..1786191c363 100644 --- a/models/phoenix/phoenix_v1_trades.sql +++ b/models/phoenix/phoenix_v1_trades.sql @@ -101,7 +101,7 @@ {% if is_incremental() %} AND {{incremental_predicate('l.call_block_time')}} {% endif %} - AND call_block_time >= now() - interval '7' day --qa + -- AND call_block_time >= now() - interval '7' day --qa ), max_log_index AS ( SELECT @@ -185,7 +185,7 @@ {% if is_incremental() %} AND {{incremental_predicate('call_block_time')}} {% endif %} - AND call_block_time >= now() - interval '7' day --qa + -- AND call_block_time >= now() - interval '7' day --qa ) s ON s.call_block_slot = l.call_block_slot AND s.call_tx_id = l.call_tx_id AND s.account_market = l.market From 49c069e1c1f94df001228098629002e5e923090d Mon Sep 17 00:00:00 2001 From: Andrew <47720952+andrewhong5297@users.noreply.github.com> Date: Tue, 11 Jun 2024 12:27:32 -0400 Subject: [PATCH 15/27] test seed --- models/phoenix/phoenix_v1_trades.sql | 46 +++++++++++-------- seeds/phoenix_solana/_schema.yml | 4 ++ .../phoenix_solana_market_metadata.csv | 14 ++++++ 3 files changed, 44 insertions(+), 20 deletions(-) create mode 100644 seeds/phoenix_solana/_schema.yml create mode 100644 seeds/phoenix_solana/phoenix_solana_market_metadata.csv diff --git a/models/phoenix/phoenix_v1_trades.sql b/models/phoenix/phoenix_v1_trades.sql index 1786191c363..1b917c64275 100644 --- a/models/phoenix/phoenix_v1_trades.sql +++ b/models/phoenix/phoenix_v1_trades.sql @@ -19,22 +19,26 @@ {% set project_start_date = '2023-02-15' %} --grabbed program deployed at time (account created at) WITH - pools as ( - SELECT - tkA.symbol as tokenA_symbol - , length(json_extract_scalar(initializeParams, '$.InitializeParams.numBaseLotsPerBaseUnit')) - 1 as tokenA_decimals --if lot size is 1000, then its 3 decimals - , ip.account_baseMint as tokenA - , ip.account_baseVault as tokenAVault - , tkB.symbol as tokenB_symbol - , length(json_extract_scalar(initializeParams, '$.InitializeParams.numQuoteLotsPerQuoteUnit')) - 1 as tokenB_decimals - , ip.account_quoteMint as tokenB - , ip.account_quoteVault as tokenBVault - , cast(json_extract_scalar(initializeParams, '$.InitializeParams.takerFeeBps') as double)/100 as fee_tier - , ip.account_market as pool_id - , ip.call_tx_id as init_tx - FROM {{ source('phoenix_v1_solana','phoenix_v1_call_InitializeMarket') }} ip - LEFT JOIN {{ ref('tokens_solana_fungible') }} tkA ON tkA.token_mint_address = ip.account_baseMint - LEFT JOIN {{ ref('tokens_solana_fungible') }} tkB ON tkB.token_mint_address = ip.account_quoteMint + market_metadata as ( + SELECT * FROM ref('phoenix_solana_market_metadata') + ) + + , pools as ( + SELECT + tkA.symbol as tokenA_symbol + , length(json_extract_scalar(initializeParams, '$.InitializeParams.numBaseLotsPerBaseUnit')) - 1 as tokenA_decimals --if lot size is 1000, then its 3 decimals + , ip.account_baseMint as tokenA + , ip.account_baseVault as tokenAVault + , tkB.symbol as tokenB_symbol + , length(json_extract_scalar(initializeParams, '$.InitializeParams.numQuoteLotsPerQuoteUnit')) - 1 as tokenB_decimals + , ip.account_quoteMint as tokenB + , ip.account_quoteVault as tokenBVault + , cast(json_extract_scalar(initializeParams, '$.InitializeParams.takerFeeBps') as double)/100 as fee_tier + , ip.account_market as pool_id + , ip.call_tx_id as init_tx + FROM {{ source('phoenix_v1_solana','phoenix_v1_call_InitializeMarket') }} ip + LEFT JOIN {{ ref('tokens_solana_fungible') }} tkA ON tkA.token_mint_address = ip.account_baseMint + LEFT JOIN {{ ref('tokens_solana_fungible') }} tkB ON tkB.token_mint_address = ip.account_quoteMint ) , logs AS ( @@ -101,7 +105,7 @@ {% if is_incremental() %} AND {{incremental_predicate('l.call_block_time')}} {% endif %} - -- AND call_block_time >= now() - interval '7' day --qa + AND call_block_time >= now() - interval '7' day --qa ), max_log_index AS ( SELECT @@ -185,7 +189,7 @@ {% if is_incremental() %} AND {{incremental_predicate('call_block_time')}} {% endif %} - -- AND call_block_time >= now() - interval '7' day --qa + AND call_block_time >= now() - interval '7' day --qa ) s ON s.call_block_slot = l.call_block_slot AND s.call_tx_id = l.call_tx_id AND s.account_market = l.market @@ -230,7 +234,8 @@ LEFT JOIN {{ source('prices', 'usd') }} p_bought ON p_bought.blockchain = 'solan {% if is_incremental() %} AND {{incremental_predicate('p_bought.minute')}} {% else %} - AND p_bought.minute >= TIMESTAMP '{{project_start_date}}' + -- AND p_bought.minute >= TIMESTAMP '{{project_start_date}}' + AND p_bought.minute >= now() - interval '7' day --qa {% endif %} LEFT JOIN {{ source('prices', 'usd') }} p_sold ON p_sold.blockchain = 'solana' AND date_trunc('minute', tb.block_time) = p_sold.minute @@ -238,7 +243,8 @@ LEFT JOIN {{ source('prices', 'usd') }} p_sold ON p_sold.blockchain = 'solana' {% if is_incremental() %} AND {{incremental_predicate('p_sold.minute')}} {% else %} - AND p_sold.minute >= TIMESTAMP '{{project_start_date}}' + -- AND p_sold.minute >= TIMESTAMP '{{project_start_date}}' + AND p_sold.minute >= now() - interval '7' day --qa {% endif %} WHERE 1=1 AND recent_swap = 1 \ No newline at end of file diff --git a/seeds/phoenix_solana/_schema.yml b/seeds/phoenix_solana/_schema.yml new file mode 100644 index 00000000000..9e09b632adb --- /dev/null +++ b/seeds/phoenix_solana/_schema.yml @@ -0,0 +1,4 @@ +version: 2 + +seeds: + - name: phoenix_solana_market_metadata \ No newline at end of file diff --git a/seeds/phoenix_solana/phoenix_solana_market_metadata.csv b/seeds/phoenix_solana/phoenix_solana_market_metadata.csv new file mode 100644 index 00000000000..92bf2c4f221 --- /dev/null +++ b/seeds/phoenix_solana/phoenix_solana_market_metadata.csv @@ -0,0 +1,14 @@ +market_id,base_mint,quote_mint,base_decimals,quote_decimals,base_atoms_per_raw_base_unit,quote_atoms_per_quote_unit,base_atoms_per_base_lot,quote_atoms_per_quote_lot,tick_size_in_quote_atoms_per_base_unit,base_lots_per_base_unit,market_name,base_symbol,quote_symbol,raw_base_units_per_base_unit,raw_base_units_per_base_lot,quote_units_per_quote_lot,tick_size_in_quote_units_per_raw_base_unit +4DoNfFBfF7UokCC2FQzriy7yHK6DY6NVdYpuekQ5pRgg,So11111111111111111111111111111111111111112,EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v,9,6,1000000000,1000000,1000000,1,1000,1000,SOL/USDC,SOL,USDC,1.000000000000000,0.001000000000000,0.000001000000000,0.001000000000000 +FZRgpfpvicJ3p23DfmZuvUgcQZBHJsWScTf2N2jK8dy6,mSoLzYCxHdYgdzU16g5QSh3i5K3z3KZK7ytfqcJm7So,So11111111111111111111111111111111111111112,9,9,1000000000,1000000000,100000,1,10000,10000,mSOL/SOL,mSOL,SOL,1.000000000000000,0.000100000000000,0.000000001000000,0.000010000000000 +GBMoNx84HsFdVK63t8BZuDgyZhSBaeKWB4pHHpoeRM9z,DezXAZ8z7PnrnRJjz3wXBoRgixCa6xjnB7YaB1pPB263,EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v,5,6,100000,1000000,10000000000,1,100,10,Bonk/USDC,Bonk,USDC,1000000.000000000000000,100000.000000000000000,0.000001000000000,0.000000000100000 +FicF181nDsEcasznMTPp9aLa5Rbpdtd11GtSEa1UUWzx,DezXAZ8z7PnrnRJjz3wXBoRgixCa6xjnB7YaB1pPB263,So11111111111111111111111111111111111111112,5,9,100000,1000000000,100000000,1,10000,1000,Bonk/SOL,Bonk,SOL,1000000.000000000000000,1000.000000000000000,0.000000001000000,0.000000000010000 +2t9TBYyUyovhHQq434uAiBxW6DmJCg7w4xdDoSK6LRjP,J1toso1uCk3RLmjorhTtrVwY9HJ7X8V9yYac6Y7kGCPn,So11111111111111111111111111111111111111112,9,9,1000000000,1000000000,100000,1,10000,10000,JitoSOL/SOL,JitoSOL,SOL,1.000000000000000,0.000100000000000,0.000000001000000,0.000010000000000 +Ew3vFDdtdGrknJAVVfraxCA37uNJtimXYPY4QjnfhFHH,7vfCXTUXx5WJV5JADk17DUJ4ksgau7utNKj4b963voxs,EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v,8,6,100000000,1000000,10000,1,10000,10000,WETH/USDC,WETH,USDC,1.000000000000000,0.000100000000000,0.000001000000000,0.010000000000000 +2sTMN9A1D1qeZLF95XQgJCUPiKe5DiV52jLfZGqMP46m,HZ1JovNiVvGrGNiiYvEozEVgZ58xaU3RKwX8eACQBCt3,EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v,6,6,1000000,1000000,10000,1,100,100,PYTH/USDC,PYTH,USDC,1.000000000000000,0.010000000000000,0.000001000000000,0.000100000000000 +BRLLmdtPGuuFn3BU6orYw4KHaohAEptBToi3dwRUnHQZ,jtojtomepa8beP8AuQc6eXt5FriJwfFMwQx2v2f9mCL,EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v,9,6,1000000000,1000000,10000000,1,100,100,JTO/USDC,JTO,USDC,1.000000000000000,0.010000000000000,0.000001000000000,0.000100000000000 +5x91Aaegvx1JmW7g8gDfWqwb6kPF7CdNunqNoYCdLjk1,HzwqbKZw8HxMN6bF2yFZNrht3c2iXXzpKcFu7uBEDKtr,EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v,6,6,1000000,1000000,100000,1,10,10,EURC/USDC,EURC,USDC,1.000000000000000,0.100000000000000,0.000001000000000,0.000010000000000 +6ojSigXF7nDPyhFRgmn3V9ywhYseKF9J32ZrranMGVSX,EKpQGSJtjMFqKZ9KQanSqYXRcF8fBopzLHYxdM65zcjm,EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v,6,6,1000000,1000000,10000,1,100,100,$WIF/USDC,$WIF,USDC,1.000000000000000,0.010000000000000,0.000001000000000,0.000100000000000 +3J9LfemPBLowAJgpG3YdYPB9n6pUk7HEjwgS6Y5ToSFg,So11111111111111111111111111111111111111112,Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB,9,6,1000000000,1000000,1000000,1,1000,1000,SOL/USDT,SOL,USDT,1.000000000000000,0.001000000000000,0.000001000000000,0.001000000000000 +2jxpfobdZDU3z9MsDCjAz8psSaTb5HPoDEtusFLGrPnD,DezXAZ8z7PnrnRJjz3wXBoRgixCa6xjnB7YaB1pPB263,Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB,5,6,100000,1000000,100000000,1,1000,1000,Bonk/USDT,Bonk,USDT,1000000.000000000000000,1000.000000000000000,0.000001000000000,0.000000001000000 +5LQLfGtqcC5rm2WuGxJf4tjqYmDjsQAbKo2AMLQ8KB7p,J1toso1uCk3RLmjorhTtrVwY9HJ7X8V9yYac6Y7kGCPn,EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v,9,6,1000000000,1000000,1000000,1,1000,1000,JitoSOL/USDC,JitoSOL,USDC,1.000000000000000,0.001000000000000,0.000001000000000,0.001000000000000 From 9711c16783e719f7eb1acad790a3a39ce8442917 Mon Sep 17 00:00:00 2001 From: Andrew <47720952+andrewhong5297@users.noreply.github.com> Date: Tue, 11 Jun 2024 12:48:10 -0400 Subject: [PATCH 16/27] test --- models/phoenix/phoenix_v1_trades.sql | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/models/phoenix/phoenix_v1_trades.sql b/models/phoenix/phoenix_v1_trades.sql index 1b917c64275..c30a96647af 100644 --- a/models/phoenix/phoenix_v1_trades.sql +++ b/models/phoenix/phoenix_v1_trades.sql @@ -145,18 +145,18 @@ else COALESCE(tokenA_symbol, tokenA) end as token_bought_symbol , case when s.side = 1 then l.tokenB_filled - else l.tokenA_filled + else (l.tokenA_filled*coalesce(mm.raw_base_units_per_base_unit,1)) --base unit can be adjusted by phoenix, i.e. for BONK it starts at 1e6. There is a script for updating the markets seed file. end as token_bought_amount_raw , case when s.side = 1 then l.tokenB_filled/pow(10,p.tokenB_decimals) - else l.tokenA_filled/pow(10,p.tokenA_decimals) + else (l.tokenA_filled*coalesce(mm.raw_base_units_per_base_unit,1))/pow(10,p.tokenA_decimals) end token_bought_amount , case when s.side = 1 then COALESCE(tokenA_symbol, tokenA) else COALESCE(tokenB_symbol, tokenB) end as token_sold_symbol - , case when s.side = 1 then l.tokenA_filled + , case when s.side = 1 then (l.tokenA_filled*coalesce(mm.raw_base_units_per_base_unit,1)) else l.tokenB_filled end as token_sold_amount_raw - , case when s.side = 1 then l.tokenA_filled/pow(10,p.tokenA_decimals) + , case when s.side = 1 then (l.tokenA_filled*coalesce(mm.raw_base_units_per_base_unit,1))/pow(10,p.tokenA_decimals) else l.tokenB_filled/pow(10,p.tokenB_decimals) end token_sold_amount , p.pool_id @@ -196,6 +196,7 @@ AND s.call_outer_instruction_index = l.call_outer_instruction_index AND COALESCE(s.call_inner_instruction_index, 0) <= COALESCE(l.call_inner_instruction_index,0) --only get swaps before the log call JOIN pools p ON l.market = p.pool_id + LEFT JOIN market_metadata mm ON l.market = mm.market_id ) SELECT From b81ab116bd2e019dc150f561abcae0587b242dfe Mon Sep 17 00:00:00 2001 From: Andrew <47720952+andrewhong5297@users.noreply.github.com> Date: Tue, 11 Jun 2024 12:52:48 -0400 Subject: [PATCH 17/27] types --- seeds/phoenix_solana/_schema.yml | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/seeds/phoenix_solana/_schema.yml b/seeds/phoenix_solana/_schema.yml index 9e09b632adb..ff98169f3af 100644 --- a/seeds/phoenix_solana/_schema.yml +++ b/seeds/phoenix_solana/_schema.yml @@ -1,4 +1,24 @@ version: 2 seeds: - - name: phoenix_solana_market_metadata \ No newline at end of file + - name: phoenix_solana_market_metadata + config: + column_types: + market_id: varchar + base_mint: varchar + quote_mint: varchar + base_decimals: bigint + quote_decimals: bigint + base_atoms_per_raw_base_unit: bigint + quote_atoms_per_quote_unit: bigint + base_atoms_per_base_lot: bigint + quote_atoms_per_quote_lot: bigint + tick_size_in_quote_atoms_per_base_unit: bigint + base_lots_per_base_unit: bigint + market_name: varchar + base_symbol: varchar + quote_symbol: varchar + raw_base_units_per_base_unit: bigint + raw_base_units_per_base_lot: bigint + quote_units_per_quote_lot: bigint + tick_size_in_quote_units_per_raw_base_unit: bigint \ No newline at end of file From 3f6d52fdf7c60aa4fca4f95f5172f1939f76f7f4 Mon Sep 17 00:00:00 2001 From: Andrew <47720952+andrewhong5297@users.noreply.github.com> Date: Tue, 11 Jun 2024 14:56:32 -0400 Subject: [PATCH 18/27] 111 --- models/phoenix/phoenix_v1_trades.sql | 5 ++++- seeds/phoenix_solana/_schema.yml | 8 ++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/models/phoenix/phoenix_v1_trades.sql b/models/phoenix/phoenix_v1_trades.sql index c30a96647af..182ad791edd 100644 --- a/models/phoenix/phoenix_v1_trades.sql +++ b/models/phoenix/phoenix_v1_trades.sql @@ -20,7 +20,10 @@ WITH market_metadata as ( - SELECT * FROM ref('phoenix_solana_market_metadata') + SELECT + market_id + , cast(raw_base_units_per_base_unit as int) as raw_base_units_per_base_unit + FROM ref('phoenix_solana_market_metadata') ) , pools as ( diff --git a/seeds/phoenix_solana/_schema.yml b/seeds/phoenix_solana/_schema.yml index ff98169f3af..32b4d5dccbe 100644 --- a/seeds/phoenix_solana/_schema.yml +++ b/seeds/phoenix_solana/_schema.yml @@ -18,7 +18,7 @@ seeds: market_name: varchar base_symbol: varchar quote_symbol: varchar - raw_base_units_per_base_unit: bigint - raw_base_units_per_base_lot: bigint - quote_units_per_quote_lot: bigint - tick_size_in_quote_units_per_raw_base_unit: bigint \ No newline at end of file + raw_base_units_per_base_unit: double + raw_base_units_per_base_lot: double + quote_units_per_quote_lot: double + tick_size_in_quote_units_per_raw_base_unit: double \ No newline at end of file From 10cef95b48436ffd4d73d8497d6c7e45a485e42d Mon Sep 17 00:00:00 2001 From: Andrew <47720952+andrewhong5297@users.noreply.github.com> Date: Tue, 11 Jun 2024 15:21:39 -0400 Subject: [PATCH 19/27] 111 --- models/phoenix/phoenix_v1_trades.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/phoenix/phoenix_v1_trades.sql b/models/phoenix/phoenix_v1_trades.sql index 182ad791edd..789d26f3627 100644 --- a/models/phoenix/phoenix_v1_trades.sql +++ b/models/phoenix/phoenix_v1_trades.sql @@ -23,7 +23,7 @@ SELECT market_id , cast(raw_base_units_per_base_unit as int) as raw_base_units_per_base_unit - FROM ref('phoenix_solana_market_metadata') + FROM {{ ref('phoenix_solana_market_metadata') }} ) , pools as ( From bdb7f41f2d8775a5ec94c72e14764e7503bdf69d Mon Sep 17 00:00:00 2001 From: Andrew <47720952+andrewhong5297@users.noreply.github.com> Date: Tue, 11 Jun 2024 16:45:49 -0400 Subject: [PATCH 20/27] remove day filter --- models/phoenix/phoenix_v1_trades.sql | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/models/phoenix/phoenix_v1_trades.sql b/models/phoenix/phoenix_v1_trades.sql index 789d26f3627..ab4c4dff5b8 100644 --- a/models/phoenix/phoenix_v1_trades.sql +++ b/models/phoenix/phoenix_v1_trades.sql @@ -108,7 +108,7 @@ {% if is_incremental() %} AND {{incremental_predicate('l.call_block_time')}} {% endif %} - AND call_block_time >= now() - interval '7' day --qa + -- AND call_block_time >= now() - interval '7' day --qa ), max_log_index AS ( SELECT @@ -192,7 +192,7 @@ {% if is_incremental() %} AND {{incremental_predicate('call_block_time')}} {% endif %} - AND call_block_time >= now() - interval '7' day --qa + -- AND call_block_time >= now() - interval '7' day --qa ) s ON s.call_block_slot = l.call_block_slot AND s.call_tx_id = l.call_tx_id AND s.account_market = l.market @@ -238,8 +238,8 @@ LEFT JOIN {{ source('prices', 'usd') }} p_bought ON p_bought.blockchain = 'solan {% if is_incremental() %} AND {{incremental_predicate('p_bought.minute')}} {% else %} - -- AND p_bought.minute >= TIMESTAMP '{{project_start_date}}' - AND p_bought.minute >= now() - interval '7' day --qa + AND p_bought.minute >= TIMESTAMP '{{project_start_date}}' + -- AND p_bought.minute >= now() - interval '7' day --qa {% endif %} LEFT JOIN {{ source('prices', 'usd') }} p_sold ON p_sold.blockchain = 'solana' AND date_trunc('minute', tb.block_time) = p_sold.minute @@ -247,8 +247,8 @@ LEFT JOIN {{ source('prices', 'usd') }} p_sold ON p_sold.blockchain = 'solana' {% if is_incremental() %} AND {{incremental_predicate('p_sold.minute')}} {% else %} - -- AND p_sold.minute >= TIMESTAMP '{{project_start_date}}' - AND p_sold.minute >= now() - interval '7' day --qa + AND p_sold.minute >= TIMESTAMP '{{project_start_date}}' + -- AND p_sold.minute >= now() - interval '7' day --qa {% endif %} WHERE 1=1 AND recent_swap = 1 \ No newline at end of file From 3e89c4066ccc5b61bc5b2ca3df10b545e9356417 Mon Sep 17 00:00:00 2001 From: Andrew <47720952+andrewhong5297@users.noreply.github.com> Date: Sat, 22 Jun 2024 20:48:28 -0400 Subject: [PATCH 21/27] mvoe seed --- {seeds => solana/seeds}/phoenix_solana/_schema.yml | 0 .../seeds}/phoenix_solana/phoenix_solana_market_metadata.csv | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename {seeds => solana/seeds}/phoenix_solana/_schema.yml (100%) rename {seeds => solana/seeds}/phoenix_solana/phoenix_solana_market_metadata.csv (100%) diff --git a/seeds/phoenix_solana/_schema.yml b/solana/seeds/phoenix_solana/_schema.yml similarity index 100% rename from seeds/phoenix_solana/_schema.yml rename to solana/seeds/phoenix_solana/_schema.yml diff --git a/seeds/phoenix_solana/phoenix_solana_market_metadata.csv b/solana/seeds/phoenix_solana/phoenix_solana_market_metadata.csv similarity index 100% rename from seeds/phoenix_solana/phoenix_solana_market_metadata.csv rename to solana/seeds/phoenix_solana/phoenix_solana_market_metadata.csv From 6f79116dd07eea6a16f9693219fc7c8edd85982e Mon Sep 17 00:00:00 2001 From: Andrew <47720952+andrewhong5297@users.noreply.github.com> Date: Tue, 25 Jun 2024 05:45:19 -0400 Subject: [PATCH 22/27] price --- solana/models/_sector/dex/phoenix/phoenix_v1_trades.sql | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/solana/models/_sector/dex/phoenix/phoenix_v1_trades.sql b/solana/models/_sector/dex/phoenix/phoenix_v1_trades.sql index ab4c4dff5b8..26b42b1a3be 100644 --- a/solana/models/_sector/dex/phoenix/phoenix_v1_trades.sql +++ b/solana/models/_sector/dex/phoenix/phoenix_v1_trades.sql @@ -217,7 +217,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 , tb.fee_tier as fee_tier , tb.fee_tier * COALESCE(tb.token_sold_amount * p_sold.price, tb.token_bought_amount * p_bought.price) as fee_usd , tb.token_sold_mint_address From 26681d3dd16be680e2cef23052902916a8544a60 Mon Sep 17 00:00:00 2001 From: Andrew <47720952+andrewhong5297@users.noreply.github.com> Date: Thu, 27 Jun 2024 10:01:55 -0400 Subject: [PATCH 23/27] remove seed --- .../_sector/dex/phoenix/phoenix_v1_trades.sql | 75 ++++++------------- solana/seeds/phoenix_solana/_schema.yml | 24 ------ .../phoenix_solana_market_metadata.csv | 14 ---- 3 files changed, 21 insertions(+), 92 deletions(-) delete mode 100644 solana/seeds/phoenix_solana/_schema.yml delete mode 100644 solana/seeds/phoenix_solana/phoenix_solana_market_metadata.csv diff --git a/solana/models/_sector/dex/phoenix/phoenix_v1_trades.sql b/solana/models/_sector/dex/phoenix/phoenix_v1_trades.sql index 26b42b1a3be..4a0ea493565 100644 --- a/solana/models/_sector/dex/phoenix/phoenix_v1_trades.sql +++ b/solana/models/_sector/dex/phoenix/phoenix_v1_trades.sql @@ -20,28 +20,39 @@ WITH market_metadata as ( - SELECT - market_id - , cast(raw_base_units_per_base_unit as int) as raw_base_units_per_base_unit - FROM {{ ref('phoenix_solana_market_metadata') }} + --you can check phoenix_v1_call_InitializeMarket for this data, our decoding just has some nulls/incompletes so recreating manually. + SELECT + * + FROM ( + VALUES + ('4DoNfFBfF7UokCC2FQzriy7yHK6DY6NVdYpuekQ5pRgg', 'So11111111111111111111111111111111111111112', 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v', 1), + ('FZRgpfpvicJ3p23DfmZuvUgcQZBHJsWScTf2N2jK8dy6', 'mSoLzYCxHdYgdzU16g5QSh3i5K3z3KZK7ytfqcJm7So', 'So11111111111111111111111111111111111111112', 1), + ('GBMoNx84HsFdVK63t8BZuDgyZhSBaeKWB4pHHpoeRM9z', 'DezXAZ8z7PnrnRJjz3wXBoRgixCa6xjnB7YaB1pPB263', 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v', 1000000), + ('FicF181nDsEcasznMTPp9aLa5Rbpdtd11GtSEa1UUWzx', 'DezXAZ8z7PnrnRJjz3wXBoRgixCa6xjnB7YaB1pPB263', 'So11111111111111111111111111111111111111112', 1000000), + ('2t9TBYyUyovhHQq434uAiBxW6DmJCg7w4xdDoSK6LRjP', 'J1toso1uCk3RLmjorhTtrVwY9HJ7X8V9yYac6Y7kGCPn', 'So11111111111111111111111111111111111111112', 1), + ('Ew3vFDdtdGrknJAVVfraxCA37uNJtimXYPY4QjnfhFHH', '7vfCXTUXx5WJV5JADk17DUJ4ksgau7utNKj4b963voxs', 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v', 1), + ('2sTMN9A1D1qeZLF95XQgJCUPiKe5DiV52jLfZGqMP46m', 'HZ1JovNiVvGrGNiiYvEozEVgZ58xaU3RKwX8eACQBCt3', 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v', 1), + ('BRLLmdtPGuuFn3BU6orYw4KHaohAEptBToi3dwRUnHQZ', 'jtojtomepa8beP8AuQc6eXt5FriJwfFMwQx2v2f9mCL', 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v', 1), + ('5x91Aaegvx1JmW7g8gDfWqwb6kPF7CdNunqNoYCdLjk1', 'HzwqbKZw8HxMN6bF2yFZNrht3c2iXXzpKcFu7uBEDKtr', 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v', 1), + ('6ojSigXF7nDPyhFRgmn3V9ywhYseKF9J32ZrranMGVSX', 'EKpQGSJtjMFqKZ9KQanSqYXRcF8fBopzLHYxdM65zcjm', 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v', 1), + ('3J9LfemPBLowAJgpG3YdYPB9n6pUk7HEjwgS6Y5ToSFg', 'So11111111111111111111111111111111111111112', 'Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB', 1), + ('2jxpfobdZDU3z9MsDCjAz8psSaTb5HPoDEtusFLGrPnD', 'DezXAZ8z7PnrnRJjz3wXBoRgixCa6xjnB7YaB1pPB263', 'Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB', 1000000), + ('5LQLfGtqcC5rm2WuGxJf4tjqYmDjsQAbKo2AMLQ8KB7p', 'J1toso1uCk3RLmjorhTtrVwY9HJ7X8V9yYac6Y7kGCPn', 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v', 1) + ) AS t (market_id, base_mint, quote_mint, raw_base_units_per_base_unit); ) , pools as ( SELECT - tkA.symbol as tokenA_symbol - , length(json_extract_scalar(initializeParams, '$.InitializeParams.numBaseLotsPerBaseUnit')) - 1 as tokenA_decimals --if lot size is 1000, then its 3 decimals + length(json_extract_scalar(initializeParams, '$.InitializeParams.numBaseLotsPerBaseUnit')) - 1 as tokenA_decimals --if lot size is 1000, then its 3 decimals , ip.account_baseMint as tokenA , ip.account_baseVault as tokenAVault - , tkB.symbol as tokenB_symbol - , length(json_extract_scalar(initializeParams, '$.InitializeParams.numQuoteLotsPerQuoteUnit')) - 1 as tokenB_decimals + length(json_extract_scalar(initializeParams, '$.InitializeParams.numQuoteLotsPerQuoteUnit')) - 1 as tokenB_decimals , ip.account_quoteMint as tokenB , ip.account_quoteVault as tokenBVault , cast(json_extract_scalar(initializeParams, '$.InitializeParams.takerFeeBps') as double)/100 as fee_tier , ip.account_market as pool_id , ip.call_tx_id as init_tx FROM {{ source('phoenix_v1_solana','phoenix_v1_call_InitializeMarket') }} ip - LEFT JOIN {{ ref('tokens_solana_fungible') }} tkA ON tkA.token_mint_address = ip.account_baseMint - LEFT JOIN {{ ref('tokens_solana_fungible') }} tkB ON tkB.token_mint_address = ip.account_quoteMint ) , logs AS ( @@ -140,28 +151,12 @@ , case when s.call_outer_executing_account = 'PhoeNiXZ8ByJGLkxNfZRnkUfjvmuYqLR89jjFHGqdXY' then 'direct' else s.call_outer_executing_account end as trade_source - , case - 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 s.side = 1 then COALESCE(tokenB_symbol, tokenB) - else COALESCE(tokenA_symbol, tokenA) - end as token_bought_symbol , case when s.side = 1 then l.tokenB_filled else (l.tokenA_filled*coalesce(mm.raw_base_units_per_base_unit,1)) --base unit can be adjusted by phoenix, i.e. for BONK it starts at 1e6. There is a script for updating the markets seed file. end as token_bought_amount_raw - , case when s.side = 1 then l.tokenB_filled/pow(10,p.tokenB_decimals) - else (l.tokenA_filled*coalesce(mm.raw_base_units_per_base_unit,1))/pow(10,p.tokenA_decimals) - end token_bought_amount - , case when s.side = 1 then COALESCE(tokenA_symbol, tokenA) - else COALESCE(tokenB_symbol, tokenB) - end as token_sold_symbol , case when s.side = 1 then (l.tokenA_filled*coalesce(mm.raw_base_units_per_base_unit,1)) else l.tokenB_filled end as token_sold_amount_raw - , case when s.side = 1 then (l.tokenA_filled*coalesce(mm.raw_base_units_per_base_unit,1))/pow(10,p.tokenA_decimals) - else l.tokenB_filled/pow(10,p.tokenB_decimals) - end token_sold_amount , p.pool_id , s.call_tx_signer as trader_id , s.call_tx_id as tx_id @@ -209,20 +204,10 @@ SELECT , 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 - , tb.token_bought_amount , tb.token_bought_amount_raw - , tb.token_sold_symbol - , tb.token_sold_amount , tb.token_sold_amount_raw - , 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 , tb.fee_tier as fee_tier - , tb.fee_tier * COALESCE(tb.token_sold_amount * p_sold.price, tb.token_bought_amount * p_bought.price) as fee_usd , tb.token_sold_mint_address , tb.token_bought_mint_address , tb.token_sold_vault @@ -235,23 +220,5 @@ SELECT , tb.inner_instruction_index , tb.tx_index FROM trades tb -LEFT JOIN {{ source('prices', 'usd') }} p_bought ON p_bought.blockchain = 'solana' - AND date_trunc('minute', tb.block_time) = p_bought.minute - AND token_bought_mint_address = toBase58(p_bought.contract_address) - {% if is_incremental() %} - AND {{incremental_predicate('p_bought.minute')}} - {% else %} - AND p_bought.minute >= TIMESTAMP '{{project_start_date}}' - -- AND p_bought.minute >= now() - interval '7' day --qa - {% endif %} -LEFT JOIN {{ source('prices', 'usd') }} p_sold ON p_sold.blockchain = 'solana' - AND date_trunc('minute', tb.block_time) = p_sold.minute - AND token_sold_mint_address = toBase58(p_sold.contract_address) - {% if is_incremental() %} - AND {{incremental_predicate('p_sold.minute')}} - {% else %} - AND p_sold.minute >= TIMESTAMP '{{project_start_date}}' - -- AND p_sold.minute >= now() - interval '7' day --qa - {% endif %} WHERE 1=1 AND recent_swap = 1 \ No newline at end of file diff --git a/solana/seeds/phoenix_solana/_schema.yml b/solana/seeds/phoenix_solana/_schema.yml deleted file mode 100644 index 32b4d5dccbe..00000000000 --- a/solana/seeds/phoenix_solana/_schema.yml +++ /dev/null @@ -1,24 +0,0 @@ -version: 2 - -seeds: - - name: phoenix_solana_market_metadata - config: - column_types: - market_id: varchar - base_mint: varchar - quote_mint: varchar - base_decimals: bigint - quote_decimals: bigint - base_atoms_per_raw_base_unit: bigint - quote_atoms_per_quote_unit: bigint - base_atoms_per_base_lot: bigint - quote_atoms_per_quote_lot: bigint - tick_size_in_quote_atoms_per_base_unit: bigint - base_lots_per_base_unit: bigint - market_name: varchar - base_symbol: varchar - quote_symbol: varchar - raw_base_units_per_base_unit: double - raw_base_units_per_base_lot: double - quote_units_per_quote_lot: double - tick_size_in_quote_units_per_raw_base_unit: double \ No newline at end of file diff --git a/solana/seeds/phoenix_solana/phoenix_solana_market_metadata.csv b/solana/seeds/phoenix_solana/phoenix_solana_market_metadata.csv deleted file mode 100644 index 92bf2c4f221..00000000000 --- a/solana/seeds/phoenix_solana/phoenix_solana_market_metadata.csv +++ /dev/null @@ -1,14 +0,0 @@ -market_id,base_mint,quote_mint,base_decimals,quote_decimals,base_atoms_per_raw_base_unit,quote_atoms_per_quote_unit,base_atoms_per_base_lot,quote_atoms_per_quote_lot,tick_size_in_quote_atoms_per_base_unit,base_lots_per_base_unit,market_name,base_symbol,quote_symbol,raw_base_units_per_base_unit,raw_base_units_per_base_lot,quote_units_per_quote_lot,tick_size_in_quote_units_per_raw_base_unit -4DoNfFBfF7UokCC2FQzriy7yHK6DY6NVdYpuekQ5pRgg,So11111111111111111111111111111111111111112,EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v,9,6,1000000000,1000000,1000000,1,1000,1000,SOL/USDC,SOL,USDC,1.000000000000000,0.001000000000000,0.000001000000000,0.001000000000000 -FZRgpfpvicJ3p23DfmZuvUgcQZBHJsWScTf2N2jK8dy6,mSoLzYCxHdYgdzU16g5QSh3i5K3z3KZK7ytfqcJm7So,So11111111111111111111111111111111111111112,9,9,1000000000,1000000000,100000,1,10000,10000,mSOL/SOL,mSOL,SOL,1.000000000000000,0.000100000000000,0.000000001000000,0.000010000000000 -GBMoNx84HsFdVK63t8BZuDgyZhSBaeKWB4pHHpoeRM9z,DezXAZ8z7PnrnRJjz3wXBoRgixCa6xjnB7YaB1pPB263,EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v,5,6,100000,1000000,10000000000,1,100,10,Bonk/USDC,Bonk,USDC,1000000.000000000000000,100000.000000000000000,0.000001000000000,0.000000000100000 -FicF181nDsEcasznMTPp9aLa5Rbpdtd11GtSEa1UUWzx,DezXAZ8z7PnrnRJjz3wXBoRgixCa6xjnB7YaB1pPB263,So11111111111111111111111111111111111111112,5,9,100000,1000000000,100000000,1,10000,1000,Bonk/SOL,Bonk,SOL,1000000.000000000000000,1000.000000000000000,0.000000001000000,0.000000000010000 -2t9TBYyUyovhHQq434uAiBxW6DmJCg7w4xdDoSK6LRjP,J1toso1uCk3RLmjorhTtrVwY9HJ7X8V9yYac6Y7kGCPn,So11111111111111111111111111111111111111112,9,9,1000000000,1000000000,100000,1,10000,10000,JitoSOL/SOL,JitoSOL,SOL,1.000000000000000,0.000100000000000,0.000000001000000,0.000010000000000 -Ew3vFDdtdGrknJAVVfraxCA37uNJtimXYPY4QjnfhFHH,7vfCXTUXx5WJV5JADk17DUJ4ksgau7utNKj4b963voxs,EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v,8,6,100000000,1000000,10000,1,10000,10000,WETH/USDC,WETH,USDC,1.000000000000000,0.000100000000000,0.000001000000000,0.010000000000000 -2sTMN9A1D1qeZLF95XQgJCUPiKe5DiV52jLfZGqMP46m,HZ1JovNiVvGrGNiiYvEozEVgZ58xaU3RKwX8eACQBCt3,EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v,6,6,1000000,1000000,10000,1,100,100,PYTH/USDC,PYTH,USDC,1.000000000000000,0.010000000000000,0.000001000000000,0.000100000000000 -BRLLmdtPGuuFn3BU6orYw4KHaohAEptBToi3dwRUnHQZ,jtojtomepa8beP8AuQc6eXt5FriJwfFMwQx2v2f9mCL,EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v,9,6,1000000000,1000000,10000000,1,100,100,JTO/USDC,JTO,USDC,1.000000000000000,0.010000000000000,0.000001000000000,0.000100000000000 -5x91Aaegvx1JmW7g8gDfWqwb6kPF7CdNunqNoYCdLjk1,HzwqbKZw8HxMN6bF2yFZNrht3c2iXXzpKcFu7uBEDKtr,EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v,6,6,1000000,1000000,100000,1,10,10,EURC/USDC,EURC,USDC,1.000000000000000,0.100000000000000,0.000001000000000,0.000010000000000 -6ojSigXF7nDPyhFRgmn3V9ywhYseKF9J32ZrranMGVSX,EKpQGSJtjMFqKZ9KQanSqYXRcF8fBopzLHYxdM65zcjm,EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v,6,6,1000000,1000000,10000,1,100,100,$WIF/USDC,$WIF,USDC,1.000000000000000,0.010000000000000,0.000001000000000,0.000100000000000 -3J9LfemPBLowAJgpG3YdYPB9n6pUk7HEjwgS6Y5ToSFg,So11111111111111111111111111111111111111112,Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB,9,6,1000000000,1000000,1000000,1,1000,1000,SOL/USDT,SOL,USDT,1.000000000000000,0.001000000000000,0.000001000000000,0.001000000000000 -2jxpfobdZDU3z9MsDCjAz8psSaTb5HPoDEtusFLGrPnD,DezXAZ8z7PnrnRJjz3wXBoRgixCa6xjnB7YaB1pPB263,Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB,5,6,100000,1000000,100000000,1,1000,1000,Bonk/USDT,Bonk,USDT,1000000.000000000000000,1000.000000000000000,0.000001000000000,0.000000001000000 -5LQLfGtqcC5rm2WuGxJf4tjqYmDjsQAbKo2AMLQ8KB7p,J1toso1uCk3RLmjorhTtrVwY9HJ7X8V9yYac6Y7kGCPn,EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v,9,6,1000000000,1000000,1000000,1,1000,1000,JitoSOL/USDC,JitoSOL,USDC,1.000000000000000,0.001000000000000,0.000001000000000,0.001000000000000 From 7ef31bc0db27f7ef71cc392c980baab46018a7de Mon Sep 17 00:00:00 2001 From: Andrew <47720952+andrewhong5297@users.noreply.github.com> Date: Thu, 27 Jun 2024 10:05:21 -0400 Subject: [PATCH 24/27] remove some schema cols --- solana/models/_sector/dex/phoenix/schema.yml | 25 -------------------- 1 file changed, 25 deletions(-) diff --git a/solana/models/_sector/dex/phoenix/schema.yml b/solana/models/_sector/dex/phoenix/schema.yml index d312c0184b7..fd6fb60de55 100644 --- a/solana/models/_sector/dex/phoenix/schema.yml +++ b/solana/models/_sector/dex/phoenix/schema.yml @@ -39,40 +39,15 @@ models: - &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)" - - &token_bought_symbol - name: token_bought_symbol - description: "Token symbol for token bought in the trade" - - &token_sold_symbol - name: token_sold_symbol - description: "Token symbol for token sold in the trade" - - &token_pair - name: token_pair - description: "Token symbol pair for each token involved in the trade" - - &token_bought_amount - name: token_bought_amount - description: "Value of the token bought at time of execution in the original currency" - - &token_sold_amount - name: token_sold_amount - description: "Value of the token sold at time of execution in the original currency" - &token_bought_amount_raw name: token_bought_amount_raw description: "Raw value of the token bought at time of execution in the original currency" - &token_sold_amount_raw name: token_sold_amount_raw description: "Raw value of the token sold at time of execution in the original currency" - - &amount_usd - name: amount_usd - description: "USD value of the trade at time of execution" - tests: - - dbt_utils.accepted_range: - max_value: 1000000000 # $1b is an arbitrary number, intended to flag outlier amounts early - severity: warn - &fee_tier name: fee_tier description: "dexfee tier (fee %)" - - &fee_usd - name: fee_usd - description: "dexfee usd paid on swap" - &token_bought_mint_address name: token_bought_address description: "token mint address of the token bought" From e531b524d7cb38dc114c179fd0b75b4fc963d238 Mon Sep 17 00:00:00 2001 From: Andrew <47720952+andrewhong5297@users.noreply.github.com> Date: Thu, 27 Jun 2024 10:11:33 -0400 Subject: [PATCH 25/27] remove semi --- solana/models/_sector/dex/phoenix/phoenix_v1_trades.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solana/models/_sector/dex/phoenix/phoenix_v1_trades.sql b/solana/models/_sector/dex/phoenix/phoenix_v1_trades.sql index 4a0ea493565..1343e29e5e9 100644 --- a/solana/models/_sector/dex/phoenix/phoenix_v1_trades.sql +++ b/solana/models/_sector/dex/phoenix/phoenix_v1_trades.sql @@ -38,7 +38,7 @@ ('3J9LfemPBLowAJgpG3YdYPB9n6pUk7HEjwgS6Y5ToSFg', 'So11111111111111111111111111111111111111112', 'Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB', 1), ('2jxpfobdZDU3z9MsDCjAz8psSaTb5HPoDEtusFLGrPnD', 'DezXAZ8z7PnrnRJjz3wXBoRgixCa6xjnB7YaB1pPB263', 'Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB', 1000000), ('5LQLfGtqcC5rm2WuGxJf4tjqYmDjsQAbKo2AMLQ8KB7p', 'J1toso1uCk3RLmjorhTtrVwY9HJ7X8V9yYac6Y7kGCPn', 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v', 1) - ) AS t (market_id, base_mint, quote_mint, raw_base_units_per_base_unit); + ) AS t (market_id, base_mint, quote_mint, raw_base_units_per_base_unit) ) , pools as ( From e96df4ff8200365ae722f7fe1bedbd326808581d Mon Sep 17 00:00:00 2001 From: Andrew <47720952+andrewhong5297@users.noreply.github.com> Date: Thu, 27 Jun 2024 10:17:11 -0400 Subject: [PATCH 26/27] oops --- solana/models/_sector/dex/phoenix/phoenix_v1_trades.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solana/models/_sector/dex/phoenix/phoenix_v1_trades.sql b/solana/models/_sector/dex/phoenix/phoenix_v1_trades.sql index 1343e29e5e9..9cc188f2761 100644 --- a/solana/models/_sector/dex/phoenix/phoenix_v1_trades.sql +++ b/solana/models/_sector/dex/phoenix/phoenix_v1_trades.sql @@ -46,7 +46,7 @@ length(json_extract_scalar(initializeParams, '$.InitializeParams.numBaseLotsPerBaseUnit')) - 1 as tokenA_decimals --if lot size is 1000, then its 3 decimals , ip.account_baseMint as tokenA , ip.account_baseVault as tokenAVault - length(json_extract_scalar(initializeParams, '$.InitializeParams.numQuoteLotsPerQuoteUnit')) - 1 as tokenB_decimals + , length(json_extract_scalar(initializeParams, '$.InitializeParams.numQuoteLotsPerQuoteUnit')) - 1 as tokenB_decimals , ip.account_quoteMint as tokenB , ip.account_quoteVault as tokenBVault , cast(json_extract_scalar(initializeParams, '$.InitializeParams.takerFeeBps') as double)/100 as fee_tier From e7b10cf2b1a8c6d559e7ca51c0fef8e42e8ea58a Mon Sep 17 00:00:00 2001 From: 0xRob <0xRobin5@gmail.com> Date: Thu, 27 Jun 2024 17:03:05 +0200 Subject: [PATCH 27/27] rename model to _base_trades + add compatibility view + add schema test --- .../_sector/dex/dex_solana_base_trades.sql | 2 +- .../dex/phoenix/phoenix_v1_base_trades.sql | 224 ++++++++++++++++++ .../_sector/dex/phoenix/phoenix_v1_trades.sql | 217 +---------------- solana/models/_sector/dex/phoenix/schema.yml | 23 +- 4 files changed, 242 insertions(+), 224 deletions(-) create mode 100644 solana/models/_sector/dex/phoenix/phoenix_v1_base_trades.sql diff --git a/solana/models/_sector/dex/dex_solana_base_trades.sql b/solana/models/_sector/dex/dex_solana_base_trades.sql index d292b968dec..ed95db20e67 100644 --- a/solana/models/_sector/dex/dex_solana_base_trades.sql +++ b/solana/models/_sector/dex/dex_solana_base_trades.sql @@ -18,7 +18,7 @@ ref('orca_whirlpool_trades') , ref('raydium_v3_trades') , ref('raydium_v4_trades') - , ref('phoenix_v1_trades') + , ref('phoenix_v1_base_trades') , ref('meteora_v1_solana_trades') , ref('meteora_v2_solana_trades') , ref('goosefx_ssl_v2_solana_trades') diff --git a/solana/models/_sector/dex/phoenix/phoenix_v1_base_trades.sql b/solana/models/_sector/dex/phoenix/phoenix_v1_base_trades.sql new file mode 100644 index 00000000000..879200f6b55 --- /dev/null +++ b/solana/models/_sector/dex/phoenix/phoenix_v1_base_trades.sql @@ -0,0 +1,224 @@ + {{ + config( + + schema = 'phoenix_v1', + alias = 'base_trades', + partition_by = ['block_month'], + materialized = 'incremental', + file_format = 'delta', + incremental_strategy = 'merge', + incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.block_time')], + unique_key = ['tx_id', 'outer_instruction_index', 'inner_instruction_index', 'tx_index','block_month'], + pre_hook='{{ enforce_join_distribution("PARTITIONED") }}', + post_hook='{{ expose_spells(\'["solana"]\', + "project", + "phoenix", + \'["ilemi","jarryx"]\') }}') +}} + +{% set project_start_date = '2023-02-15' %} --grabbed program deployed at time (account created at) + + WITH + market_metadata as ( + --you can check phoenix_v1_call_InitializeMarket for this data, our decoding just has some nulls/incompletes so recreating manually. + SELECT + * + FROM ( + VALUES + ('4DoNfFBfF7UokCC2FQzriy7yHK6DY6NVdYpuekQ5pRgg', 'So11111111111111111111111111111111111111112', 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v', 1), + ('FZRgpfpvicJ3p23DfmZuvUgcQZBHJsWScTf2N2jK8dy6', 'mSoLzYCxHdYgdzU16g5QSh3i5K3z3KZK7ytfqcJm7So', 'So11111111111111111111111111111111111111112', 1), + ('GBMoNx84HsFdVK63t8BZuDgyZhSBaeKWB4pHHpoeRM9z', 'DezXAZ8z7PnrnRJjz3wXBoRgixCa6xjnB7YaB1pPB263', 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v', 1000000), + ('FicF181nDsEcasznMTPp9aLa5Rbpdtd11GtSEa1UUWzx', 'DezXAZ8z7PnrnRJjz3wXBoRgixCa6xjnB7YaB1pPB263', 'So11111111111111111111111111111111111111112', 1000000), + ('2t9TBYyUyovhHQq434uAiBxW6DmJCg7w4xdDoSK6LRjP', 'J1toso1uCk3RLmjorhTtrVwY9HJ7X8V9yYac6Y7kGCPn', 'So11111111111111111111111111111111111111112', 1), + ('Ew3vFDdtdGrknJAVVfraxCA37uNJtimXYPY4QjnfhFHH', '7vfCXTUXx5WJV5JADk17DUJ4ksgau7utNKj4b963voxs', 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v', 1), + ('2sTMN9A1D1qeZLF95XQgJCUPiKe5DiV52jLfZGqMP46m', 'HZ1JovNiVvGrGNiiYvEozEVgZ58xaU3RKwX8eACQBCt3', 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v', 1), + ('BRLLmdtPGuuFn3BU6orYw4KHaohAEptBToi3dwRUnHQZ', 'jtojtomepa8beP8AuQc6eXt5FriJwfFMwQx2v2f9mCL', 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v', 1), + ('5x91Aaegvx1JmW7g8gDfWqwb6kPF7CdNunqNoYCdLjk1', 'HzwqbKZw8HxMN6bF2yFZNrht3c2iXXzpKcFu7uBEDKtr', 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v', 1), + ('6ojSigXF7nDPyhFRgmn3V9ywhYseKF9J32ZrranMGVSX', 'EKpQGSJtjMFqKZ9KQanSqYXRcF8fBopzLHYxdM65zcjm', 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v', 1), + ('3J9LfemPBLowAJgpG3YdYPB9n6pUk7HEjwgS6Y5ToSFg', 'So11111111111111111111111111111111111111112', 'Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB', 1), + ('2jxpfobdZDU3z9MsDCjAz8psSaTb5HPoDEtusFLGrPnD', 'DezXAZ8z7PnrnRJjz3wXBoRgixCa6xjnB7YaB1pPB263', 'Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB', 1000000), + ('5LQLfGtqcC5rm2WuGxJf4tjqYmDjsQAbKo2AMLQ8KB7p', 'J1toso1uCk3RLmjorhTtrVwY9HJ7X8V9yYac6Y7kGCPn', 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v', 1) + ) AS t (market_id, base_mint, quote_mint, raw_base_units_per_base_unit) + ) + + , pools as ( + SELECT + length(json_extract_scalar(initializeParams, '$.InitializeParams.numBaseLotsPerBaseUnit')) - 1 as tokenA_decimals --if lot size is 1000, then its 3 decimals + , ip.account_baseMint as tokenA + , ip.account_baseVault as tokenAVault + , length(json_extract_scalar(initializeParams, '$.InitializeParams.numQuoteLotsPerQuoteUnit')) - 1 as tokenB_decimals + , ip.account_quoteMint as tokenB + , ip.account_quoteVault as tokenBVault + , cast(json_extract_scalar(initializeParams, '$.InitializeParams.takerFeeBps') as double)/100 as fee_tier + , ip.account_market as pool_id + , ip.call_tx_id as init_tx + FROM {{ source('phoenix_v1_solana','phoenix_v1_call_InitializeMarket') }} ip + ) + + , logs AS ( + SELECT + call_tx_id, + call_is_inner, + call_block_slot, + call_block_time, + call_inner_instruction_index, + call_outer_instruction_index, + call_tx_index, + BYTEARRAY_TO_BIGINT ( + BYTEARRAY_REVERSE (BYTEARRAY_SUBSTRING (l.call_data, 4, 8)) + ) AS seq, + TO_BASE58 ((BYTEARRAY_SUBSTRING (l.call_data, 28, 32))) AS market, + cast(BYTEARRAY_TO_BIGINT ( + BYTEARRAY_REVERSE ( + BYTEARRAY_SUBSTRING ( + l.call_data, + BYTEARRAY_LENGTH (l.call_data) - 23, + 8 + ) + ) + ) as uint256) AS tokenA_filled, + l.call_outer_instruction_index AS index, + cast(BYTEARRAY_TO_BIGINT ( + BYTEARRAY_REVERSE ( + BYTEARRAY_SUBSTRING ( + l.call_data, + BYTEARRAY_LENGTH (l.call_data) - 15, + 8 + ) + ) + ) as uint256) AS tokenB_filled + FROM + {{ source('phoenix_v1_solana','phoenix_v1_call_Log')}} AS l + WHERE 1=1 + --filter for 0 events + and bytearray_length (l.call_data) > 93 + --instruction is swap + and BYTEARRAY_TO_BIGINT ( + BYTEARRAY_REVERSE (BYTEARRAY_SUBSTRING (l.call_data, 3, 1)) + ) = 0 + --filter for trade size > 0 + AND BYTEARRAY_TO_BIGINT ( + BYTEARRAY_REVERSE ( + BYTEARRAY_SUBSTRING ( + l.call_data, + BYTEARRAY_LENGTH (l.call_data) - 23, + 8 + ) + ) + ) > 0 + --filter for event FillSummary + AND BYTEARRAY_TO_BIGINT ( + BYTEARRAY_REVERSE ( + BYTEARRAY_SUBSTRING ( + l.call_data, + BYTEARRAY_LENGTH (l.call_data) - 42, + 1 + ) + ) + ) = 6 + {% if is_incremental() %} + AND {{incremental_predicate('l.call_block_time')}} + {% endif %} + -- AND call_block_time >= now() - interval '7' day --qa + ), + max_log_index AS ( + SELECT + market, + seq, + MAX(index) AS index + FROM + logs + GROUP BY + market, + seq + ), + filtered_logs AS ( + SELECT + l.* + FROM + logs AS l + JOIN max_log_index AS m ON l.market = m.market + AND l.seq = m.seq + AND l.index = m.index + ), + trades as ( + SELECT + l.call_block_time as block_time + , 'phoenix' as project + , 1 as version + , 'solana' as blockchain + , l.call_block_slot as block_slot + , case when s.call_outer_executing_account = 'PhoeNiXZ8ByJGLkxNfZRnkUfjvmuYqLR89jjFHGqdXY' then 'direct' + else s.call_outer_executing_account + end as trade_source + , case when s.side = 1 then l.tokenB_filled + else (l.tokenA_filled*coalesce(mm.raw_base_units_per_base_unit,1)) --base unit can be adjusted by phoenix, i.e. for BONK it starts at 1e6. There is a script for updating the markets seed file. + end as token_bought_amount_raw + , case when s.side = 1 then (l.tokenA_filled*coalesce(mm.raw_base_units_per_base_unit,1)) + else l.tokenB_filled + end as token_sold_amount_raw + , p.pool_id + , s.call_tx_signer as trader_id + , s.call_tx_id as tx_id + , s.call_outer_instruction_index as outer_instruction_index + , COALESCE(s.call_inner_instruction_index,0) as inner_instruction_index + , s.call_tx_index as tx_index + , case when s.side = 1 then p.tokenB + else p.tokenA + end as token_bought_mint_address + , case when s.side = 1 then p.tokenBVault + else p.tokenAVault + end as token_bought_vault + , case when s.side = 1 then p.tokenA + else p.tokenB + end as token_sold_mint_address + , case when s.side = 1 then p.tokenAVault + else p.tokenBVault + end as token_sold_vault + , p.fee_tier + , row_number() over (partition by seq order by COALESCE(s.call_inner_instruction_index, 0) desc) as recent_swap -- this ties the log to only the most recent swap call + FROM filtered_logs l + LEFT JOIN ( + SELECT + * + , 2 * bytearray_to_integer (bytearray_substring (call_data, 3, 1)) - 1 as side --if side = 1 then tokenB was bought, else tokenA was bought + FROM {{ source('phoenix_v1_solana','phoenix_v1_call_Swap') }} + WHERE 1=1 + {% if is_incremental() %} + AND {{incremental_predicate('call_block_time')}} + {% endif %} + -- AND call_block_time >= now() - interval '7' day --qa + ) s ON s.call_block_slot = l.call_block_slot + AND s.call_tx_id = l.call_tx_id + AND s.account_market = l.market + AND s.call_outer_instruction_index = l.call_outer_instruction_index + AND COALESCE(s.call_inner_instruction_index, 0) <= COALESCE(l.call_inner_instruction_index,0) --only get swaps before the log call + JOIN pools p ON l.market = p.pool_id + LEFT JOIN market_metadata mm ON l.market = mm.market_id + ) + +SELECT + tb.blockchain + , tb.project + , tb.version + , CAST(date_trunc('month', tb.block_time) AS DATE) as block_month + , tb.block_time + , tb.block_slot + , tb.trade_source + , tb.token_bought_amount_raw + , tb.token_sold_amount_raw + , tb.fee_tier as fee_tier + , tb.token_sold_mint_address + , tb.token_bought_mint_address + , tb.token_sold_vault + , tb.token_bought_vault + , tb.pool_id as project_program_id + , 'PhoeNiXZ8ByJGLkxNfZRnkUfjvmuYqLR89jjFHGqdXY' as project_main_id + , tb.trader_id + , tb.tx_id + , tb.outer_instruction_index + , tb.inner_instruction_index + , tb.tx_index +FROM trades tb +WHERE 1=1 +AND recent_swap = 1 diff --git a/solana/models/_sector/dex/phoenix/phoenix_v1_trades.sql b/solana/models/_sector/dex/phoenix/phoenix_v1_trades.sql index 9cc188f2761..9c1e398984a 100644 --- a/solana/models/_sector/dex/phoenix/phoenix_v1_trades.sql +++ b/solana/models/_sector/dex/phoenix/phoenix_v1_trades.sql @@ -1,224 +1,13 @@ {{ config( - schema = 'phoenix_v1', alias = 'trades', - partition_by = ['block_month'], - materialized = 'incremental', - file_format = 'delta', - incremental_strategy = 'merge', - incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.block_time')], - unique_key = ['tx_id', 'outer_instruction_index', 'inner_instruction_index', 'tx_index','block_month'], - pre_hook='{{ enforce_join_distribution("PARTITIONED") }}', post_hook='{{ expose_spells(\'["solana"]\', "project", "phoenix", \'["ilemi","jarryx"]\') }}') }} -{% set project_start_date = '2023-02-15' %} --grabbed program deployed at time (account created at) - - WITH - market_metadata as ( - --you can check phoenix_v1_call_InitializeMarket for this data, our decoding just has some nulls/incompletes so recreating manually. - SELECT - * - FROM ( - VALUES - ('4DoNfFBfF7UokCC2FQzriy7yHK6DY6NVdYpuekQ5pRgg', 'So11111111111111111111111111111111111111112', 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v', 1), - ('FZRgpfpvicJ3p23DfmZuvUgcQZBHJsWScTf2N2jK8dy6', 'mSoLzYCxHdYgdzU16g5QSh3i5K3z3KZK7ytfqcJm7So', 'So11111111111111111111111111111111111111112', 1), - ('GBMoNx84HsFdVK63t8BZuDgyZhSBaeKWB4pHHpoeRM9z', 'DezXAZ8z7PnrnRJjz3wXBoRgixCa6xjnB7YaB1pPB263', 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v', 1000000), - ('FicF181nDsEcasznMTPp9aLa5Rbpdtd11GtSEa1UUWzx', 'DezXAZ8z7PnrnRJjz3wXBoRgixCa6xjnB7YaB1pPB263', 'So11111111111111111111111111111111111111112', 1000000), - ('2t9TBYyUyovhHQq434uAiBxW6DmJCg7w4xdDoSK6LRjP', 'J1toso1uCk3RLmjorhTtrVwY9HJ7X8V9yYac6Y7kGCPn', 'So11111111111111111111111111111111111111112', 1), - ('Ew3vFDdtdGrknJAVVfraxCA37uNJtimXYPY4QjnfhFHH', '7vfCXTUXx5WJV5JADk17DUJ4ksgau7utNKj4b963voxs', 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v', 1), - ('2sTMN9A1D1qeZLF95XQgJCUPiKe5DiV52jLfZGqMP46m', 'HZ1JovNiVvGrGNiiYvEozEVgZ58xaU3RKwX8eACQBCt3', 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v', 1), - ('BRLLmdtPGuuFn3BU6orYw4KHaohAEptBToi3dwRUnHQZ', 'jtojtomepa8beP8AuQc6eXt5FriJwfFMwQx2v2f9mCL', 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v', 1), - ('5x91Aaegvx1JmW7g8gDfWqwb6kPF7CdNunqNoYCdLjk1', 'HzwqbKZw8HxMN6bF2yFZNrht3c2iXXzpKcFu7uBEDKtr', 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v', 1), - ('6ojSigXF7nDPyhFRgmn3V9ywhYseKF9J32ZrranMGVSX', 'EKpQGSJtjMFqKZ9KQanSqYXRcF8fBopzLHYxdM65zcjm', 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v', 1), - ('3J9LfemPBLowAJgpG3YdYPB9n6pUk7HEjwgS6Y5ToSFg', 'So11111111111111111111111111111111111111112', 'Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB', 1), - ('2jxpfobdZDU3z9MsDCjAz8psSaTb5HPoDEtusFLGrPnD', 'DezXAZ8z7PnrnRJjz3wXBoRgixCa6xjnB7YaB1pPB263', 'Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB', 1000000), - ('5LQLfGtqcC5rm2WuGxJf4tjqYmDjsQAbKo2AMLQ8KB7p', 'J1toso1uCk3RLmjorhTtrVwY9HJ7X8V9yYac6Y7kGCPn', 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v', 1) - ) AS t (market_id, base_mint, quote_mint, raw_base_units_per_base_unit) - ) - - , pools as ( - SELECT - length(json_extract_scalar(initializeParams, '$.InitializeParams.numBaseLotsPerBaseUnit')) - 1 as tokenA_decimals --if lot size is 1000, then its 3 decimals - , ip.account_baseMint as tokenA - , ip.account_baseVault as tokenAVault - , length(json_extract_scalar(initializeParams, '$.InitializeParams.numQuoteLotsPerQuoteUnit')) - 1 as tokenB_decimals - , ip.account_quoteMint as tokenB - , ip.account_quoteVault as tokenBVault - , cast(json_extract_scalar(initializeParams, '$.InitializeParams.takerFeeBps') as double)/100 as fee_tier - , ip.account_market as pool_id - , ip.call_tx_id as init_tx - FROM {{ source('phoenix_v1_solana','phoenix_v1_call_InitializeMarket') }} ip - ) - - , logs AS ( - SELECT - call_tx_id, - call_is_inner, - call_block_slot, - call_block_time, - call_inner_instruction_index, - call_outer_instruction_index, - call_tx_index, - BYTEARRAY_TO_BIGINT ( - BYTEARRAY_REVERSE (BYTEARRAY_SUBSTRING (l.call_data, 4, 8)) - ) AS seq, - TO_BASE58 ((BYTEARRAY_SUBSTRING (l.call_data, 28, 32))) AS market, - cast(BYTEARRAY_TO_BIGINT ( - BYTEARRAY_REVERSE ( - BYTEARRAY_SUBSTRING ( - l.call_data, - BYTEARRAY_LENGTH (l.call_data) - 23, - 8 - ) - ) - ) as uint256) AS tokenA_filled, - l.call_outer_instruction_index AS index, - cast(BYTEARRAY_TO_BIGINT ( - BYTEARRAY_REVERSE ( - BYTEARRAY_SUBSTRING ( - l.call_data, - BYTEARRAY_LENGTH (l.call_data) - 15, - 8 - ) - ) - ) as uint256) AS tokenB_filled - FROM - {{ source('phoenix_v1_solana','phoenix_v1_call_Log')}} AS l - WHERE 1=1 - --filter for 0 events - and bytearray_length (l.call_data) > 93 - --instruction is swap - and BYTEARRAY_TO_BIGINT ( - BYTEARRAY_REVERSE (BYTEARRAY_SUBSTRING (l.call_data, 3, 1)) - ) = 0 - --filter for trade size > 0 - AND BYTEARRAY_TO_BIGINT ( - BYTEARRAY_REVERSE ( - BYTEARRAY_SUBSTRING ( - l.call_data, - BYTEARRAY_LENGTH (l.call_data) - 23, - 8 - ) - ) - ) > 0 - --filter for event FillSummary - AND BYTEARRAY_TO_BIGINT ( - BYTEARRAY_REVERSE ( - BYTEARRAY_SUBSTRING ( - l.call_data, - BYTEARRAY_LENGTH (l.call_data) - 42, - 1 - ) - ) - ) = 6 - {% if is_incremental() %} - AND {{incremental_predicate('l.call_block_time')}} - {% endif %} - -- AND call_block_time >= now() - interval '7' day --qa - ), - max_log_index AS ( - SELECT - market, - seq, - MAX(index) AS index - FROM - logs - GROUP BY - market, - seq - ), - filtered_logs AS ( - SELECT - l.* - FROM - logs AS l - JOIN max_log_index AS m ON l.market = m.market - AND l.seq = m.seq - AND l.index = m.index - ), - trades as ( - SELECT - l.call_block_time as block_time - , 'phoenix' as project - , 1 as version - , 'solana' as blockchain - , l.call_block_slot as block_slot - , case when s.call_outer_executing_account = 'PhoeNiXZ8ByJGLkxNfZRnkUfjvmuYqLR89jjFHGqdXY' then 'direct' - else s.call_outer_executing_account - end as trade_source - , case when s.side = 1 then l.tokenB_filled - else (l.tokenA_filled*coalesce(mm.raw_base_units_per_base_unit,1)) --base unit can be adjusted by phoenix, i.e. for BONK it starts at 1e6. There is a script for updating the markets seed file. - end as token_bought_amount_raw - , case when s.side = 1 then (l.tokenA_filled*coalesce(mm.raw_base_units_per_base_unit,1)) - else l.tokenB_filled - end as token_sold_amount_raw - , p.pool_id - , s.call_tx_signer as trader_id - , s.call_tx_id as tx_id - , s.call_outer_instruction_index as outer_instruction_index - , COALESCE(s.call_inner_instruction_index,0) as inner_instruction_index - , s.call_tx_index as tx_index - , case when s.side = 1 then p.tokenB - else p.tokenA - end as token_bought_mint_address - , case when s.side = 1 then p.tokenBVault - else p.tokenAVault - end as token_bought_vault - , case when s.side = 1 then p.tokenA - else p.tokenB - end as token_sold_mint_address - , case when s.side = 1 then p.tokenAVault - else p.tokenBVault - end as token_sold_vault - , p.fee_tier - , row_number() over (partition by seq order by COALESCE(s.call_inner_instruction_index, 0) desc) as recent_swap -- this ties the log to only the most recent swap call - FROM filtered_logs l - LEFT JOIN ( - SELECT - * - , 2 * bytearray_to_integer (bytearray_substring (call_data, 3, 1)) - 1 as side --if side = 1 then tokenB was bought, else tokenA was bought - FROM {{ source('phoenix_v1_solana','phoenix_v1_call_Swap') }} - WHERE 1=1 - {% if is_incremental() %} - AND {{incremental_predicate('call_block_time')}} - {% endif %} - -- AND call_block_time >= now() - interval '7' day --qa - ) s ON s.call_block_slot = l.call_block_slot - AND s.call_tx_id = l.call_tx_id - AND s.account_market = l.market - AND s.call_outer_instruction_index = l.call_outer_instruction_index - AND COALESCE(s.call_inner_instruction_index, 0) <= COALESCE(l.call_inner_instruction_index,0) --only get swaps before the log call - JOIN pools p ON l.market = p.pool_id - LEFT JOIN market_metadata mm ON l.market = mm.market_id - ) - -SELECT - tb.blockchain - , tb.project - , tb.version - , CAST(date_trunc('month', tb.block_time) AS DATE) as block_month - , tb.block_time - , tb.block_slot - , tb.trade_source - , tb.token_bought_amount_raw - , tb.token_sold_amount_raw - , tb.fee_tier as fee_tier - , tb.token_sold_mint_address - , tb.token_bought_mint_address - , tb.token_sold_vault - , tb.token_bought_vault - , tb.pool_id as project_program_id - , 'PhoeNiXZ8ByJGLkxNfZRnkUfjvmuYqLR89jjFHGqdXY' as project_main_id - , tb.trader_id - , tb.tx_id - , tb.outer_instruction_index - , tb.inner_instruction_index - , tb.tx_index -FROM trades tb -WHERE 1=1 -AND recent_swap = 1 \ No newline at end of file +-- backwards compatible view so we don't break any user queries +select * from {{ref('dex_solana_trades')}} +where project = 'phoenix' and version = 1 diff --git a/solana/models/_sector/dex/phoenix/schema.yml b/solana/models/_sector/dex/phoenix/schema.yml index fd6fb60de55..604ff4b6b2b 100644 --- a/solana/models/_sector/dex/phoenix/schema.yml +++ b/solana/models/_sector/dex/phoenix/schema.yml @@ -1,22 +1,27 @@ version: 2 models: - - name: phoenix_v1_trades + - name: phoenix_v1_base_trades meta: blockchain: solana contributors: [ilemi, jarryx] config: - tags: ['solana','dex'] + tags: ['solana','dex'] description: > all phoenix v1 dex trades on Solana tests: + - check_columns_solana_dex_trades - dbt_utils.unique_combination_of_columns: - combination_of_columns: - - block_month - - tx_id - - outer_instruction_index - - inner_instruction_index - - tx_index + combination_of_columns: [ 'tx_id', 'outer_instruction_index', 'inner_instruction_index', 'tx_index', 'block_month' ] + + - name: phoenix_v1_trades + meta: + blockchain: solana + contributors: [ ilemi, jarryx ] + config: + tags: [ 'solana','dex' ] + description: > + all phoenix v1 dex trades on Solana columns: - &blockchain name: blockchain @@ -80,4 +85,4 @@ models: description: "inner instruction index for a given transaction id" - &tx_index name: tx_index - description: "index of the transaction in the block slot" \ No newline at end of file + description: "index of the transaction in the block slot"