-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Raydium's CP-Swap (CPMM) to DEX table #7549
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
0e7e8c4
Create raydium_v5_base_trades.sql
JacobSharples d6fa76f
Update raydium_v5_base_trades.sql
JacobSharples 8d26107
Update sources.yml
JacobSharples 21ebee7
Update schema.yml
JacobSharples 1ffbaa3
Create raydium_v5_trades.sql
JacobSharples 34463c6
Update schema.yml
JacobSharples d72f336
Update raydium_v5_base_trades.sql
JacobSharples c1dc647
Update sources.yml
JacobSharples 741932d
Merge branch 'main' into patch-4
jeff-dude 0698ab8
add to cross-project base trades
jeff-dude b5c5174
force short runtimes for CI
jeff-dude cf2ba8d
Revert "force short runtimes for CI"
jeff-dude File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fyi @JacobSharples we needed to add to this downstream model to then appear in
dex_solana.trades
🙏