Skip to content

Commit

Permalink
Add Raydium's CP-Swap (CPMM) to DEX table (#7549)
Browse files Browse the repository at this point in the history
* 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
JacobSharples and jeff-dude authored Jan 29, 2025
1 parent 62de04c commit ae9c646
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
{% set solana_dexes = [
ref('raydium_v3_base_trades')
, ref('raydium_v4_base_trades')
, ref('raydium_v5_base_trades')
, ref('orca_whirlpool_base_trades')
, ref('orca_whirlpool_v2_base_trades')
, ref('phoenix_v1_base_trades')
Expand Down
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
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
54 changes: 53 additions & 1 deletion dbt_subprojects/solana/models/_sector/dex/raydium/schema.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 2
version: 3

models:
- name: raydium_v3_base_trades
Expand Down Expand Up @@ -162,3 +162,55 @@ models:
- *outer_instruction_index
- *inner_instruction_index
- *tx_index

- name: raydium_v5_base_trades
meta:
blockchain: solana
contributors: [ 0xsharples ]
config:
tags: [ 'solana','dex' ]
description: >
all raw raydium_v5 constant product amm dex trades on Solana
data_tests:
- check_columns_solana_dex_trades
- dbt_utils.unique_combination_of_columns:
combination_of_columns: [ 'tx_id', 'outer_instruction_index', 'inner_instruction_index', 'tx_index', 'block_month' ]

- name: raydium_v5_trades
meta:
blockchain: solana
contributors: [ 0xsharples ]
config:
tags: ['solana','dex']
description: >
all raydium_v5 constant product amm dex trades on Solana
columns:
- *blockchain
- *project
- *version
- *block_month
- *block_time
- *block_slot
- *trade_source
- *token_bought_symbol
- *token_sold_symbol
- *token_pair
- *token_bought_amount
- *token_sold_amount
- *token_bought_amount_raw
- *token_sold_amount_raw
- *amount_usd
- *fee_tier
- *fee_usd
- *token_bought_mint_address
- *token_sold_mint_address
- *token_bought_vault
- *token_sold_vault
- *project_program_id
- *project_main_id
- *trader_id
- *tx_id
- *outer_instruction_index
- *inner_instruction_index
- *tx_index

16 changes: 14 additions & 2 deletions dbt_subprojects/solana/models/_sector/dex/raydium/sources.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 2
version: 3

sources:
- name: raydium_clmm_solana
Expand Down Expand Up @@ -26,4 +26,16 @@ sources:
- name: raydium_amm_call_swapBaseOut
loaded_at_field: call_block_time
- name: raydium_amm_call_swapBaseIn
loaded_at_field: call_block_time
loaded_at_field: call_block_time
- name: raydium_cp_solana
description: "raydium_cpmm_solana v5 decoded tables"
freshness: # default freshness
warn_after: { count: 12, period: hour }
error_after: { count: 24, period: hour }
tables:
- name: raydium_cp_swap_call_initialize
loaded_at_field: call_block_time
- name: raydium_cp_swap_call_swapBaseOutput
loaded_at_field: call_block_time
- name: raydium_cp_swap_call_swapBaseInput
loaded_at_field: call_block_time

0 comments on commit ae9c646

Please sign in to comment.