-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Raydium's CP-Swap (CPMM) to DEX table (#7549)
* Create raydium_v5_base_trades.sql * Update raydium_v5_base_trades.sql * Update sources.yml * Update schema.yml * Create raydium_v5_trades.sql * Update schema.yml * Update raydium_v5_base_trades.sql * Update sources.yml * add to cross-project base trades * force short runtimes for CI * Revert "force short runtimes for CI" This reverts commit b5c5174. --------- Co-authored-by: jeff-dude <[email protected]>
- Loading branch information
1 parent
62de04c
commit ae9c646
Showing
5 changed files
with
189 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
dbt_subprojects/solana/models/_sector/dex/raydium/raydium_v5_base_trades.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
{{ | ||
config( | ||
schema = 'raydium_v5', | ||
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") }}' | ||
) | ||
}} | ||
|
||
{% set project_start_date = '2024-05-16' %} --grabbed program deployed at time (account created at). | ||
|
||
WITH | ||
all_swaps as ( | ||
SELECT | ||
sp.call_block_time as block_time | ||
, sp.call_block_slot as block_slot | ||
, 'raydium' as project | ||
, 5 as version | ||
, 'solana' as blockchain | ||
, case when sp.call_is_inner = False then 'direct' | ||
else sp.call_outer_executing_account | ||
end as trade_source | ||
-- -- token bought is always the second instruction (transfer) in the inner instructions | ||
, trs_2.amount as token_bought_amount_raw | ||
, trs_1.amount as token_sold_amount_raw | ||
, account_poolState as pool_id --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 | ||
, 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 | ||
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_cp_solana', 'raydium_cp_swap_call_swapBaseOutput') }} | ||
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_cp_solana', 'raydium_cp_swap_call_swapBaseInput') }} | ||
) 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)) | ||
) | ||
AND trs_1.token_version = 'spl_token' | ||
{% if is_incremental() %} | ||
AND {{incremental_predicate('trs_1.block_time')}} | ||
{% else %} | ||
AND trs_1.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)) | ||
) | ||
AND trs_2.token_version = 'spl_token' | ||
{% if is_incremental() %} | ||
AND {{incremental_predicate('trs_2.block_time')}} | ||
{% 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.from_token_account | ||
AND tk_2.account_type = 'fungible' | ||
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_2.token_balance_owner = 'GpMZbSM2GgvTKHJirzeGfMFoaZ8UR2X7F4v8vHTvxFbL' --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}}' | ||
{% endif %} | ||
) | ||
|
||
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 | ||
, cast(null as double) 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 | ||
, 'CPMMoo8L3F4NbTegBCKVNunggL7H1ZpdTHKxQB5qKP1C' as project_main_id | ||
, tb.trader_id | ||
, tb.tx_id | ||
, tb.outer_instruction_index | ||
, tb.inner_instruction_index | ||
, tb.tx_index | ||
FROM all_swaps tb |
13 changes: 13 additions & 0 deletions
13
dbt_subprojects/solana/models/_sector/dex/raydium/raydium_v5_trades.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{{ | ||
config( | ||
schema = 'raydium_v5', | ||
alias = 'trades', | ||
materialized = 'view', | ||
post_hook='{{ expose_spells(\'["solana"]\', | ||
"project", | ||
"raydium", | ||
\'["0xsharples"]\') }}') | ||
}} | ||
|
||
select * from {{ref('dex_solana_trades')}} | ||
where project = 'raydium' and version = 5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters