Skip to content

Commit

Permalink
Lifinity migrate (#6244)
Browse files Browse the repository at this point in the history
* changes

* up

* add project main id

* remove tags

* fix

* Update lifinity_v2_trades.sql

* 1

* update

* me
  • Loading branch information
andrewhong5297 authored Jun 26, 2024
1 parent ef61abf commit c98d6f2
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 68 deletions.
81 changes: 47 additions & 34 deletions solana/models/_sector/dex/lifinity/lifinity_v1_trades.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
schema = 'lifinity_v1',
alias = 'trades',
partition_by = ['block_month'],
tags = ['prod_exclude'],
materialized = 'incremental',
file_format = 'delta',
incremental_strategy = 'merge',
Expand Down Expand Up @@ -36,21 +35,30 @@ WITH
, ip.account_arguments[2] as pool_id
, ip.account_arguments[3] as pool_mint_id
, ip.tx_id as init_tx
FROM {{ source('solana','instruction_calls') }} ip
FROM (
SELECT
*
FROM {{ source('solana','instruction_calls') }}
WHERE cardinality(account_arguments) >= 5 --filter out broken cases/inits for now
and bytearray_substring(data,1,8) = 0xafaf6d1f0d989bed
and executing_account = 'EewxydAPCCVuNEyrVN68PuSYdQ7wKn27V9Gjeoi8dy3S'
and tx_success
and block_time > TIMESTAMP '2022-01-26'
) ip
INNER JOIN {{ ref('solana_utils_token_accounts') }} mintA ON mintA.address = ip.account_arguments[4]
AND mintA.account_type = 'fungible'
INNER JOIN {{ ref('solana_utils_token_accounts') }} mintB ON mintB.address = ip.account_arguments[5]
AND mintB.account_type = 'fungible'
LEFT JOIN {{ ref('tokens_solana_fungible') }} tkA ON tkA.token_mint_address = mintA.token_mint_address
AND tkA.token_version = 'spl_token'
LEFT JOIN {{ ref('tokens_solana_fungible') }} tkB ON tkB.token_mint_address = mintB.token_mint_address
WHERE bytearray_substring(ip.data,1,8) = 0xafaf6d1f0d989bed
and executing_account = 'EewxydAPCCVuNEyrVN68PuSYdQ7wKn27V9Gjeoi8dy3S'
and tx_success
and cardinality(account_arguments) >= 5 --filter out broken cases/inits for now
and block_time > TIMESTAMP '{{project_start_date}}'
AND tkB.token_version = 'spl_token'
)

, all_swaps as (
SELECT
sp.call_block_time as block_time
, sp.call_block_slot as block_slot
, 'lifinity' as project
, 1 as version
, 'solana' as blockchain
Expand All @@ -61,66 +69,66 @@ WITH
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
--swap out can be either 2nd or 3rd transfer, we need to filter for the first transfer out.
, tr_2.call_inner_instruction_index as transfer_out_index
, tr_2.inner_instruction_index as transfer_out_index
, row_number() over (partition by sp.call_tx_id, sp.call_outer_instruction_index, sp.call_inner_instruction_index
order by COALESCE(tr_2.call_inner_instruction_index, 0) asc) as first_transfer_out
order by COALESCE(tr_2.inner_instruction_index, 0) asc) as first_transfer_out
FROM {{ source('lifinity_amm_solana', 'lifinity_amm_call_swap') }} sp
INNER JOIN pools p
ON sp.account_amm = p.pool_id --account 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))
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))
AND tr_1.token_version = 'spl_token'
{% 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 %}
--swap out can be either 2nd or 3rd transfer.
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 tr_2.call_inner_instruction_index = 3))
OR (sp.call_is_inner = true AND (tr_2.call_inner_instruction_index = sp.call_inner_instruction_index + 2 OR tr_2.call_inner_instruction_index = sp.call_inner_instruction_index + 3))
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 tr_2.inner_instruction_index = 3))
OR (sp.call_is_inner = true AND (tr_2.inner_instruction_index = sp.call_inner_instruction_index + 2 OR tr_2.inner_instruction_index = sp.call_inner_instruction_index + 3))
)
AND tr_2.token_version = 'spl_token'
{% 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
WHERE 1=1
{% if is_incremental() %}
AND {{incremental_predicate('sp.call_block_time')}}
Expand All @@ -135,6 +143,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
Expand All @@ -143,14 +152,18 @@ 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
, cast(null as double) as fee_tier
, cast(null as double) as fee_usd
, 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
, 'EewxydAPCCVuNEyrVN68PuSYdQ7wKn27V9Gjeoi8dy3S' as project_main_id
, tb.trader_id
, tb.tx_id
, tb.outer_instruction_index
Expand Down
81 changes: 47 additions & 34 deletions solana/models/_sector/dex/lifinity/lifinity_v2_trades.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
alias = 'trades',
partition_by = ['block_month'],
materialized = 'incremental',
tags = ['prod_exclude'],
file_format = 'delta',
incremental_strategy = 'merge',
incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.block_time')],
Expand Down Expand Up @@ -36,21 +35,30 @@ WITH
, ip.account_arguments[2] as pool_id
, ip.account_arguments[3] as pool_mint_id
, ip.tx_id as init_tx
FROM {{ source('solana','instruction_calls') }} ip
FROM (
SELECT
*
FROM {{ source('solana','instruction_calls') }}
WHERE cardinality(account_arguments) >= 5 --filter out broken cases/inits for now
and bytearray_substring(data,1,8) = 0xafaf6d1f0d989bed
and executing_account = '2wT8Yq49kHgDzXuPxZSaeLaH1qbmGXtEyPy64bL7aD3c'
and tx_success
and block_time > TIMESTAMP '2022-01-26'
) ip
INNER JOIN {{ ref('solana_utils_token_accounts') }} mintA ON mintA.address = ip.account_arguments[4]
AND mintA.account_type = 'fungible'
INNER JOIN {{ ref('solana_utils_token_accounts') }} mintB ON mintB.address = ip.account_arguments[5]
AND mintB.account_type = 'fungible'
LEFT JOIN {{ ref('tokens_solana_fungible') }} tkA ON tkA.token_mint_address = mintA.token_mint_address
AND tkA.token_version = 'spl_token'
LEFT JOIN {{ ref('tokens_solana_fungible') }} tkB ON tkB.token_mint_address = mintB.token_mint_address
WHERE bytearray_substring(ip.data,1,8) = 0xafaf6d1f0d989bed
and executing_account = '2wT8Yq49kHgDzXuPxZSaeLaH1qbmGXtEyPy64bL7aD3c'
and tx_success
and cardinality(account_arguments) >= 5 --filter out broken cases/inits for now
and block_time > TIMESTAMP '{{project_start_date}}'
AND tkB.token_version = 'spl_token'
)

, all_swaps as (
SELECT
sp.call_block_time as block_time
, sp.call_block_slot as block_slot
, 'lifinity' as project
, 2 as version
, 'solana' as blockchain
Expand All @@ -61,66 +69,66 @@ WITH
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
--swap out can be either 2nd or 3rd transfer, we need to filter for the first transfer out.
, tr_2.call_inner_instruction_index as transfer_out_index
, tr_2.inner_instruction_index as transfer_out_index
, row_number() over (partition by sp.call_tx_id, sp.call_outer_instruction_index, sp.call_inner_instruction_index
order by COALESCE(tr_2.call_inner_instruction_index, 0) asc) as first_transfer_out
order by COALESCE(tr_2.inner_instruction_index, 0) asc) as first_transfer_out
FROM {{ source('lifinity_amm_v2_solana', 'lifinity_amm_v2_call_swap') }} sp
INNER JOIN pools p
ON sp.account_amm = p.pool_id --account 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))
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))
AND tr_1.token_version = 'spl_token'
{% 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 %}
--swap out can be either 2nd or 3rd transfer.
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 tr_2.call_inner_instruction_index = 3))
OR (sp.call_is_inner = true AND (tr_2.call_inner_instruction_index = sp.call_inner_instruction_index + 2 OR tr_2.call_inner_instruction_index = sp.call_inner_instruction_index + 3))
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 tr_2.inner_instruction_index = 3))
OR (sp.call_is_inner = true AND (tr_2.inner_instruction_index = sp.call_inner_instruction_index + 2 OR tr_2.inner_instruction_index = sp.call_inner_instruction_index + 3))
)
AND tr_2.token_version = 'spl_token'
{% 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
WHERE 1=1
{% if is_incremental() %}
AND {{incremental_predicate('sp.call_block_time')}}
Expand All @@ -135,6 +143,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
Expand All @@ -143,14 +152,18 @@ 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
, cast(null as double) as fee_tier
, cast(null as double) as fee_usd
, 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
, '2wT8Yq49kHgDzXuPxZSaeLaH1qbmGXtEyPy64bL7aD3c' as project_main_id
, tb.trader_id
, tb.tx_id
, tb.outer_instruction_index
Expand Down
Loading

0 comments on commit c98d6f2

Please sign in to comment.