Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
{{
config(
schema = 'alphaq_solana'
, 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 = ['block_month', 'surrogate_key']
, pre_hook = '{{ enforce_join_distribution("PARTITIONED") }}'
)
}}

{% set project_start_date = '2025-11-05' %} -- shorter timeframe for testing. Project start '2025-07-09'

-- alphaq swaps from instruction_calls table
WITH swaps AS (
SELECT
block_slot
, block_date
, block_time
, COALESCE(inner_instruction_index,0) as inner_instruction_index -- adjust to index 0 for direct trades
, outer_instruction_index
, inner_executing_account
, outer_executing_account
, executing_account
, is_inner
, tx_id
, tx_signer
, tx_index
, CAST(NULL as VARCHAR) AS pool_id -- alphaq does not use a pool system like other AMM's. Each token has it's own single vault.
FROM {{ source('solana','instruction_calls') }}
WHERE 1=1
AND executing_account = 'ALPHAQmeA7bjrVuccPsYPiCvsi428SNwte66Srvs4pHA' -- alphaq swap program id
AND BYTEARRAY_SUBSTRING(data, 1, 1) = 0x0c -- Swap tag/discriminator. See: https://dune.com/queries/5868521
AND tx_success = true
{% if is_incremental() -%}
AND {{ incremental_predicate('block_time') }}
{% else -%}
AND block_time >= TIMESTAMP '{{ project_start_date }}'
{% endif -%}
)

-- Join inner token_transfers initiated by amm swap instructions.
-- inner_instruction_index + 2 -> token that the trader bought
-- inner_instruction_index + 1 -> token that the trader sold
, transfers AS (
SELECT
s.block_date
, s.block_time
, s.block_slot
, CASE
WHEN s.is_inner = false THEN 'direct'
ELSE s.outer_executing_account
END as trade_source
, t.amount as token_bought_amount_raw
, t1.amount as token_sold_amount_raw
, t.from_token_account as token_bought_vault -- For a list of all alphaq vaults see: https://dune.com/queries/5868644
, t1.to_token_account as token_sold_vault
, t.token_mint_address as token_bought_mint_address
, t1.token_mint_address as token_sold_mint_address
, s.pool_id AS project_program_id
, s.tx_signer as trader_id
, s.tx_id
, s.outer_instruction_index
, s.inner_instruction_index
, s.tx_index
FROM swaps s
INNER JOIN {{ source('tokens_solana','transfers') }} t ON t.tx_id = s.tx_id --buy
AND t.block_date = s.block_date
AND t.block_slot = s.block_slot
AND t.outer_instruction_index = s.outer_instruction_index
AND t.inner_instruction_index = s.inner_instruction_index + 2
AND (t.token_version = 'spl_token' or t.token_version = 'spl_token_2022')
{% if is_incremental() -%}
AND {{ incremental_predicate('t.block_time') }}
{% else -%}
AND t.block_time >= TIMESTAMP '{{ project_start_date }}'
{% endif -%}
INNER JOIN {{ source('tokens_solana','transfers') }} t1 ON t1.tx_id = s.tx_id --sell
AND t1.block_date = s.block_date
AND t1.block_slot = s.block_slot
AND t1.outer_instruction_index = s.outer_instruction_index
AND t1.inner_instruction_index = s.inner_instruction_index + 1
AND (t1.token_version = 'spl_token' or t1.token_version = 'spl_token_2022')
{% if is_incremental() -%}
AND {{ incremental_predicate('t1.block_time') }}
{% else -%}
AND t1.block_time >= TIMESTAMP '{{ project_start_date }}'
{% endif -%}

)

--add pertinent info in prep for union on solana_base_trades
SELECT
'solana' as blockchain
, 'alphaq' AS Project
, 1 AS version
, 'v1' as version_name
, date_trunc('month',s.block_date) as block_month
, s.block_time
, s.block_slot
, s.block_date
, s.trade_source
, s.token_bought_amount_raw
, s.token_sold_amount_raw
, CAST(NULL AS DOUBLE) as fee_tier
, s.token_bought_mint_address
, s.token_sold_mint_address
, s.token_bought_vault
, s.token_sold_vault
, s.project_program_id
, 'ALPHAQmeA7bjrVuccPsYPiCvsi428SNwte66Srvs4pHA' AS project_main_id
, s.trader_id
, s.tx_id
, s.outer_instruction_index
, s.inner_instruction_index
, s.tx_index
, {{ dbt_utils.generate_surrogate_key(['tx_id', 'tx_index', 'outer_instruction_index', 'inner_instruction_index']) }} as surrogate_key
FROM transfers s
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{{
config(
schema = 'alphaq_solana',
alias = 'trades',
materialized = 'view',
post_hook='{{ expose_spells(\'["solana"]\',
"project",
"alphaq",
\'["flyingfish"]\') }}')
}}

select
blockchain
, project
, version
, version_name
, block_month
, block_date
, 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
from {{ref('dex_solana_trades')}}
where project = 'alphaq'
116 changes: 116 additions & 0 deletions dbt_subprojects/solana/models/_sector/dex/alphaq/schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
version: 2

models:
- name: alphaq_solana_base_trades
meta:
blockchain: solana
contributors: [ flyingfish ]
config:
tags: [ 'solana','dex', 'alphaq' ]
description: >
all raw alphaq dex trades on Solana
data_tests:
- check_columns_solana_dex_trades
- dbt_utils.unique_combination_of_columns:
combination_of_columns: ['block_month', 'surrogate_key']

- name: alphaq_solana_trades
meta:
blockchain: solana
contributors: [flyingfish]
config:
tags: ['solana','dex', 'alphaq']
description: >
all alphaq dex trades on Solana
columns:
- &blockchain
name: blockchain
description: "Blockchain which the DEX is deployed"
- &project
name: project
description: "Project name of the DEX"
- &version
name: version
description: "Version of the contract built and deployed by the DEX project"
- &version_name
name: version_name
description: "Descriptive name of the DEX version (1)"
- &block_month
name: block_month
description: "UTC event block month of each DEX trade"
- &block_date
name: block_date
description: "UTC event block date of each DEX trade"
- &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 alphaq or 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"
- &fee_tier
name: fee_tier
description: "alphaq fee tier (fee %)"
- &fee_usd
name: fee_usd
description: "alphaq fee usd paid on swap"
- &token_bought_mint_address
name: token_bought_mint_address
description: "token mint address of the token bought"
- &token_sold_mint_address
name: token_sold_mint_address
description: "token mint address of the token sold"
- &token_bought_vault
name: token_bought_vault
description: "token associated address for the alphaq, of the token bought"
- &token_sold_vault
name: token_sold_vault
description: "token associated address for the alphaq, of the token sold"
- &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"
- &tx_id
name: tx_id
description: "Unique transaction id value tied to each transaction on the DEX"
- &outer_instruction_index
name: outer_instruction_index
description: "top level instruction index for a given transaction id"
- &inner_instruction_index
name: inner_instruction_index
description: "inner instruction index for a given transaction id"
- &tx_index
name: tx_index
description: "index of the transaction in the block slot"

Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
, ref('goonfi_solana_base_trades')
, ref('obric_solana_base_trades')
, ref('aquifer_solana_base_trades')
, ref('alphaq_solana_base_trades')
]
%}

Expand Down Expand Up @@ -80,9 +81,11 @@ FROM
WHERE
1=1
{% if is_incremental() -%}
AND {{incremental_predicate('block_time')}}
AND {{incremental_predicate('block_time')}} AND block_time >= now() - interval '7' day
{% else %}
AND block_time >= now() - interval '7' day
{% endif -%}
{% if not loop.last -%}
UNION ALL
{% endif -%}
{% endfor %}
{% endfor %}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ with base_trades as (
FROM
{{ ref('dex_solana_base_trades')}}
{% if is_incremental() -%}
WHERE {{incremental_predicate('block_time')}}
WHERE {{incremental_predicate('block_time')}} AND block_time >= now() - interval '7' day
{% else %}
WHERE block_time >= now() - interval '7' day
{% endif -%}
)

Expand Down