mirror of
https://github.com/FlipsideCrypto/solana-models.git
synced 2026-02-06 11:47:08 +00:00
AN-5888-Lifinity (#828)
* base silver * gold + yml * small changes * yml file * update test name --------- Co-authored-by: tarikceric <tarik@flipsidecrypto.com>
This commit is contained in:
parent
4da6ac01b3
commit
6405152a75
@ -286,6 +286,27 @@ FROM
|
||||
WHERE
|
||||
modified_timestamp >= '{{ max_modified_timestamp }}'
|
||||
{% endif %}
|
||||
UNION ALL
|
||||
SELECT
|
||||
block_timestamp,
|
||||
block_id,
|
||||
tx_id,
|
||||
succeeded,
|
||||
swapper,
|
||||
from_amt AS swap_from_amount,
|
||||
from_mint AS swap_from_mint,
|
||||
to_amt AS swap_to_amount,
|
||||
to_mint AS swap_to_mint,
|
||||
program_id,
|
||||
swap_index,
|
||||
swaps_intermediate_lifinity_id as fact_swaps_id,
|
||||
inserted_timestamp,
|
||||
modified_timestamp
|
||||
FROM
|
||||
{{ ref('silver__swaps_intermediate_lifinity') }}
|
||||
{% if is_incremental() %}
|
||||
WHERE modified_timestamp >= '{{ max_modified_timestamp }}'
|
||||
{% endif %}
|
||||
)
|
||||
|
||||
select
|
||||
|
||||
@ -0,0 +1,148 @@
|
||||
-- depends_on: {{ ref('silver__decoded_instructions_combined') }}
|
||||
|
||||
{{ config(
|
||||
materialized = 'incremental',
|
||||
unique_key = ['swaps_intermediate_lifinity_id'],
|
||||
incremental_predicates = ["dynamic_range_predicate", "block_timestamp::date"],
|
||||
merge_exclude_columns = ["inserted_timestamp"],
|
||||
cluster_by = ['block_timestamp::DATE','modified_timestamp::DATE'],
|
||||
tags = ['scheduled_non_core','scheduled_non_core_hourly'],
|
||||
) }}
|
||||
|
||||
{% if execute %}
|
||||
{% set base_query %}
|
||||
CREATE OR REPLACE TEMPORARY TABLE silver.swaps_intermediate_lifinity__intermediate_tmp AS
|
||||
SELECT
|
||||
*
|
||||
FROM
|
||||
{{ ref('silver__decoded_instructions_combined') }}
|
||||
WHERE
|
||||
program_id = '2wT8Yq49kHgDzXuPxZSaeLaH1qbmGXtEyPy64bL7aD3c'
|
||||
AND event_type = 'swap'
|
||||
|
||||
{% if is_incremental() %}
|
||||
AND _inserted_timestamp >= (
|
||||
SELECT
|
||||
MAX(_inserted_timestamp) - INTERVAL '1 hour'
|
||||
FROM
|
||||
{{ this }}
|
||||
)
|
||||
{% else %}
|
||||
AND _inserted_timestamp::DATE >= '2022-10-01'
|
||||
{% endif %}
|
||||
{% endset %}
|
||||
|
||||
{% do run_query(base_query) %}
|
||||
{% set between_stmts = fsc_utils.dynamic_range_predicate(
|
||||
"silver.swaps_intermediate_lifinity__intermediate_tmp",
|
||||
"block_timestamp::date"
|
||||
) %}
|
||||
{% endif %}
|
||||
|
||||
WITH base AS (
|
||||
SELECT
|
||||
*
|
||||
FROM
|
||||
silver.swaps_intermediate_lifinity__intermediate_tmp
|
||||
),
|
||||
decoded AS (
|
||||
SELECT
|
||||
block_timestamp,
|
||||
block_id,
|
||||
tx_id,
|
||||
INDEX,
|
||||
inner_index,
|
||||
program_id,
|
||||
succeeded,
|
||||
silver.udf_get_account_pubkey_by_name('userTransferAuthority', decoded_instruction:accounts) AS user_authority,
|
||||
silver.udf_get_account_pubkey_by_name('sourceInfo', decoded_instruction:accounts) AS source_token_account,
|
||||
silver.udf_get_account_pubkey_by_name('destinationInfo', decoded_instruction:accounts) AS destination_token_account,
|
||||
silver.udf_get_account_pubkey_by_name('swapSource', decoded_instruction:accounts) AS swap_source,
|
||||
silver.udf_get_account_pubkey_by_name('swapDestination', decoded_instruction:accounts) AS swap_destination,
|
||||
TRY_PARSE_JSON(decoded_instruction:args:amountIn)::number AS amount_in,
|
||||
TRY_PARSE_JSON(decoded_instruction:args:minimumAmountOut)::number AS min_amount_out,
|
||||
signers,
|
||||
_inserted_timestamp
|
||||
FROM
|
||||
base
|
||||
),
|
||||
transfers AS (
|
||||
SELECT
|
||||
A.*,
|
||||
COALESCE(SPLIT_PART(INDEX :: text, '.', 1) :: INT, INDEX :: INT) AS index_1,
|
||||
NULLIF(SPLIT_PART(INDEX :: text, '.', 2), '') :: INT AS inner_index_1
|
||||
FROM
|
||||
{{ ref('silver__transfers') }} A
|
||||
INNER JOIN (
|
||||
SELECT
|
||||
DISTINCT tx_id,
|
||||
block_timestamp::DATE AS block_date
|
||||
FROM
|
||||
decoded
|
||||
) d
|
||||
ON d.block_date = A.block_timestamp::DATE
|
||||
AND d.tx_id = A.tx_id
|
||||
WHERE
|
||||
{{ between_stmts }}
|
||||
),
|
||||
final_swaps AS (
|
||||
SELECT
|
||||
d.block_id,
|
||||
d.block_timestamp,
|
||||
d.program_id,
|
||||
d.tx_id,
|
||||
d.index,
|
||||
d.inner_index,
|
||||
d.succeeded,
|
||||
COALESCE(d.user_authority, d.signers[0]::STRING) AS swapper,
|
||||
from_t.amount AS from_amt,
|
||||
from_t.mint AS from_mint,
|
||||
to_t.amount AS to_amt,
|
||||
to_t.mint AS to_mint,
|
||||
d.amount_in,
|
||||
d.min_amount_out,
|
||||
d._inserted_timestamp
|
||||
FROM
|
||||
decoded d
|
||||
LEFT JOIN transfers from_t
|
||||
ON d.tx_id = from_t.tx_id
|
||||
AND d.source_token_account = from_t.source_token_account
|
||||
AND d.swap_source = from_t.dest_token_account
|
||||
AND d.index = from_t.index_1
|
||||
AND from_t.succeeded = TRUE
|
||||
LEFT JOIN transfers to_t
|
||||
ON d.tx_id = to_t.tx_id
|
||||
AND d.destination_token_account = to_t.dest_token_account
|
||||
AND d.swap_destination = to_t.source_token_account
|
||||
AND d.index = to_t.index_1
|
||||
AND to_t.succeeded = TRUE
|
||||
QUALIFY ROW_NUMBER() OVER (PARTITION BY d.tx_id, d.index, d.inner_index ORDER BY COALESCE(from_t.amount, 0) DESC) = 1
|
||||
)
|
||||
SELECT
|
||||
block_id,
|
||||
block_timestamp,
|
||||
program_id,
|
||||
tx_id,
|
||||
index,
|
||||
inner_index,
|
||||
ROW_NUMBER() OVER (
|
||||
PARTITION BY tx_id
|
||||
ORDER BY
|
||||
index,
|
||||
inner_index
|
||||
) AS swap_index,
|
||||
succeeded,
|
||||
swapper,
|
||||
from_amt,
|
||||
from_mint,
|
||||
to_amt,
|
||||
to_mint,
|
||||
amount_in,
|
||||
min_amount_out,
|
||||
_inserted_timestamp,
|
||||
{{ dbt_utils.generate_surrogate_key(['tx_id','index','inner_index']) }} AS swaps_intermediate_lifinity_id,
|
||||
SYSDATE() AS inserted_timestamp,
|
||||
SYSDATE() AS modified_timestamp,
|
||||
'{{ invocation_id }}' AS _invocation_id
|
||||
FROM
|
||||
final_swaps
|
||||
@ -0,0 +1,94 @@
|
||||
version: 2
|
||||
models:
|
||||
- name: silver__swaps_intermediate_lifinity
|
||||
tests:
|
||||
- dbt_utils.unique_combination_of_columns:
|
||||
combination_of_columns:
|
||||
- TX_ID
|
||||
- SWAP_INDEX
|
||||
- PROGRAM_ID
|
||||
where: block_timestamp::date > current_date - 7
|
||||
recent_date_filter: &recent_date_filter
|
||||
config:
|
||||
where: _inserted_timestamp >= current_date - 7
|
||||
columns:
|
||||
- name: BLOCK_TIMESTAMP
|
||||
description: "{{ doc('block_timestamp') }}"
|
||||
tests:
|
||||
- not_null: *recent_date_filter
|
||||
- dbt_expectations.expect_row_values_to_have_recent_data:
|
||||
datepart: day
|
||||
interval: 2
|
||||
- name: BLOCK_ID
|
||||
description: "{{ doc('block_id') }}"
|
||||
tests:
|
||||
- not_null: *recent_date_filter
|
||||
- name: TX_ID
|
||||
description: "{{ doc('tx_id') }}"
|
||||
tests:
|
||||
- not_null: *recent_date_filter
|
||||
- name: INDEX
|
||||
description: "{{ doc('index') }}"
|
||||
tests:
|
||||
- not_null: *recent_date_filter
|
||||
- name: INNER_INDEX
|
||||
description: "{{ doc('inner_index') }}"
|
||||
- name: SUCCEEDED
|
||||
description: "{{ doc('tx_succeeded') }}"
|
||||
tests:
|
||||
- not_null: *recent_date_filter
|
||||
- name: PROGRAM_ID
|
||||
description: "{{ doc('program_id') }}"
|
||||
tests:
|
||||
- not_null: *recent_date_filter
|
||||
- name: SWAPPER
|
||||
description: "{{ doc('swaps_swapper') }}"
|
||||
tests:
|
||||
- not_null:
|
||||
where: succeeded = TRUE AND _inserted_timestamp >= current_date - 7
|
||||
- name: FROM_AMT
|
||||
description: "{{ doc('swaps_from_amt') }}"
|
||||
tests:
|
||||
- not_null: *recent_date_filter
|
||||
- name: FROM_MINT
|
||||
description: "{{ doc('swaps_from_mint') }}"
|
||||
tests:
|
||||
- not_null: *recent_date_filter
|
||||
- name: TO_AMT
|
||||
description: "{{ doc('swaps_to_amt') }}"
|
||||
tests:
|
||||
- not_null: *recent_date_filter
|
||||
- name: TO_MINT
|
||||
description: "{{ doc('swaps_to_mint') }}"
|
||||
tests:
|
||||
- not_null: *recent_date_filter
|
||||
- name: AMOUNT_IN
|
||||
description: "Amount of tokens specified in the swap instruction"
|
||||
- name: MIN_AMOUNT_OUT
|
||||
description: "Minimum amount of output tokens specified in the swap instruction"
|
||||
- name: SWAP_INDEX
|
||||
description: "{{ doc('swaps_swap_index') }}"
|
||||
tests:
|
||||
- not_null: *recent_date_filter
|
||||
- name: _INSERTED_TIMESTAMP
|
||||
description: "{{ doc('_inserted_timestamp') }}"
|
||||
tests:
|
||||
- not_null
|
||||
- name: SWAPS_INTERMEDIATE_LIFINITY_ID
|
||||
description: '{{ doc("pk") }}'
|
||||
tests:
|
||||
- unique: *recent_date_filter
|
||||
- name: INSERTED_TIMESTAMP
|
||||
description: '{{ doc("inserted_timestamp") }}'
|
||||
tests:
|
||||
- not_null: *recent_date_filter
|
||||
- name: MODIFIED_TIMESTAMP
|
||||
description: '{{ doc("modified_timestamp") }}'
|
||||
tests:
|
||||
- not_null: *recent_date_filter
|
||||
- name: _INVOCATION_ID
|
||||
description: '{{ doc("_invocation_id") }}'
|
||||
tests:
|
||||
- not_null:
|
||||
name: test_silver__not_null_swaps_intermediate_lifinity__invocation_id
|
||||
<<: *recent_date_filter
|
||||
Loading…
Reference in New Issue
Block a user