Merge pull request #20 from FlipsideCrypto/an-5685-nonnative-transfers

An 5685 nonnative transfers
This commit is contained in:
Mike Stepanovic 2025-01-14 12:13:58 -07:00 committed by GitHub
commit 41a5d77d3d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 309 additions and 22 deletions

View File

@ -9,7 +9,76 @@
tags = ['core','full_test']
) }}
SELECT
WITH native_transfers AS (
SELECT
block_id,
block_timestamp,
tx_id,
tx_succeeded,
transition_id,
index,
transfer_type,
sender,
receiver,
amount,
is_native,
token_id
FROM
{{ ref('silver__native_transfers') }}
{% if is_incremental() %}
WHERE
modified_timestamp >= DATEADD(
'minute',
-5,(
SELECT
MAX(
modified_timestamp
)
FROM
{{ this }}
)
)
{% endif %}
),
nonnative_transfers AS (
SELECT
block_id,
block_timestamp,
tx_id,
tx_succeeded,
transition_id,
index,
transfer_type,
sender,
receiver,
amount,
is_native,
token_id
FROM
{{ ref('silver__nonnative_transfers') }}
{% if is_incremental() %}
WHERE
modified_timestamp >= DATEADD(
'minute',
-5,(
SELECT
MAX(
modified_timestamp
)
FROM
{{ this }}
)
)
{% endif %}
),
all_transfers AS (
SELECT * FROM native_transfers
UNION ALL
SELECT * FROM nonnative_transfers
)
SELECT
block_id,
block_timestamp,
tx_id,
@ -21,27 +90,12 @@ SELECT
receiver,
amount,
is_native,
token_address,
token_id as token_address,
{{ dbt_utils.generate_surrogate_key(
['tx_id','transition_id','transfer_type']
) }} AS fact_transfers_id,
SYSDATE() AS inserted_timestamp,
SYSDATE() AS modified_timestamp,
'{{ invocation_id }}' AS _invocation_id
FROM
{{ ref('silver__native_transfers') }}
{% if is_incremental() %}
WHERE
modified_timestamp >= DATEADD(
'minute',
-5,(
SELECT
MAX(
modified_timestamp
)
FROM
{{ this }}
)
)
{% endif %}
FROM
all_transfers

View File

@ -39,7 +39,7 @@ models:
tests:
- dbt_expectations.expect_column_to_exist
- name: IS_NATIVE
description: "Whether the transfer is a native transfer."
description: "Whether the transfer is a native transfer (credits.aleo) or a non-native transfer (token_registry.aleo)."
tests:
- dbt_expectations.expect_column_to_exist
- name: TOKEN_ADDRESS

View File

@ -5,7 +5,7 @@
incremental_strategy = 'merge',
merge_exclude_columns = ['inserted_timestamp'],
cluster_by = ['block_timestamp::DATE'],
tags = ['noncore', 'full_test']
tags = ['core', 'full_test']
) }}
WITH base AS (
@ -131,7 +131,7 @@ select
6
) AS amount,
TRUE AS is_native,
NULL AS token_address,
'3443843282313283355522573239085696902919850365217539366784739393210722344986field' AS token_id,
SYSDATE() as inserted_timestamp,
SYSDATE() as modified_timestamp,
'{{ invocation_id }}' as _invocation_id

View File

@ -0,0 +1,143 @@
{{ config(
materialized = 'incremental',
incremental_predicates = ["COALESCE(DBT_INTERNAL_DEST.block_timestamp::DATE,'2099-12-31') >= (select min(block_timestamp::DATE) from " ~ generate_tmp_view_name(this) ~ ")"],
unique_key = ['transition_id'],
incremental_strategy = 'merge',
merge_exclude_columns = ['inserted_timestamp'],
cluster_by = ['block_timestamp::DATE'],
tags = ['core', 'full_test']
) }}
WITH base AS (
SELECT
block_id,
block_timestamp,
tx_id,
transition_id,
index,
outputs,
program_id,
function,
succeeded
FROM
{{ ref('silver__transitions') }}
WHERE
program_id = 'token_registry.aleo'
AND function IN (
'transfer_public',
'transfer_private',
'transfer_public_as_signer',
'transfer_private_to_public',
'transfer_public_to_private'
)
{% if is_incremental() %}
AND modified_timestamp >= DATEADD(
MINUTE,
-5,(
SELECT
MAX(modified_timestamp)
FROM
{{ this }}
)
)
{% endif %}
),
output_args AS (
SELECT
block_id,
block_timestamp,
tx_id,
transition_id,
index,
program_id,
function,
succeeded,
REGEXP_SUBSTR(
outputs[array_size(outputs)-1] :value :: STRING,
'arguments:\\s*\\[(.*?)\\]',
1,
1,
'sie'
) as args_string
FROM
base
),
output_args_cleaned AS (
SELECT
block_id,
block_timestamp,
tx_id,
transition_id,
index,
program_id,
function,
succeeded,
SPLIT(
REGEXP_REPLACE(
REGEXP_REPLACE(
REGEXP_REPLACE(args_string, '\\s+', ''),
'\\[|\\]',
''
),
'u64$',
''
),
','
) as args_array
FROM output_args
),
mapped_transfers AS (
SELECT
block_id,
block_timestamp,
tx_id,
transition_id,
index,
program_id,
succeeded,
function,
CASE
WHEN function IN ('transfer_public', 'transfer_public_as_signer') THEN args_array[1]
WHEN function = 'transfer_private_to_public' THEN null
WHEN function = 'transfer_public_to_private' THEN args_array[2]
WHEN function = 'transfer_private' THEN null
END :: STRING as transfer_from,
CASE
WHEN function IN ('transfer_public', 'transfer_public_as_signer') THEN args_array[3]
WHEN function = 'transfer_private_to_public' THEN args_array[1]
WHEN function = 'transfer_public_to_private' THEN null
WHEN function = 'transfer_private' THEN null
END :: STRING as transfer_to,
CASE
WHEN function IN ('transfer_public', 'transfer_public_as_signer') THEN args_array[2]
WHEN function = 'transfer_private_to_public' THEN args_array[2]
WHEN function = 'transfer_public_to_private' THEN args_array[1]
WHEN function = 'transfer_private' THEN null
END :: STRING as amount,
CASE
WHEN function = 'transfer_private' THEN null
ELSE args_array[0]
END :: STRING as token_id
FROM output_args_cleaned
)
select
block_id,
block_timestamp,
tx_id,
transition_id,
index,
succeeded as tx_succeeded,
function as transfer_type,
transfer_from as sender,
transfer_to as receiver,
REPLACE(amount, 'u128', '') :: BIGINT / pow(
10,
6
) AS amount,
FALSE AS is_native,
token_id,
SYSDATE() as inserted_timestamp,
SYSDATE() as modified_timestamp,
'{{ invocation_id }}' as _invocation_id
from
mapped_transfers

View File

@ -0,0 +1,90 @@
version: 2
models:
- name: silver__nonnative_transfers
description: Records of native token transfers on Aleo between wallets
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- TRANSITION_ID
columns:
- name: BLOCK_ID
description: "{{ doc('block_id') }}"
tests:
- not_null
- dbt_expectations.expect_column_values_to_be_in_type_list:
column_type_list:
- NUMBER
- FLOAT
- name: BLOCK_TIMESTAMP
description: "{{ doc('block_timestamp') }}"
tests:
- not_null:
where: inserted_timestamp < dateadd('hour', -1, SYSDATE())
- dbt_expectations.expect_row_values_to_have_recent_data:
datepart: day
interval: 1
- dbt_expectations.expect_column_values_to_be_in_type_list:
column_type_list:
- TIMESTAMP_NTZ
- name: TX_ID
description: "{{ doc('tx_id') }}"
tests:
- not_null
- dbt_expectations.expect_column_values_to_be_in_type_list:
column_type_list:
- STRING
- VARCHAR
- name: TRANSITION_ID
description: "{{ doc('transition_id') }}"
tests:
- not_null
- dbt_expectations.expect_column_values_to_be_in_type_list:
column_type_list:
- STRING
- VARCHAR
- name: INDEX
tests:
- not_null
- dbt_expectations.expect_column_values_to_be_of_type:
column_type: NUMBER
- name: TX_SUCCEEDED
tests:
- not_null
- dbt_expectations.expect_column_values_to_be_in_type_list:
column_type_list:
- BOOLEAN
- name: TRANSFER_TYPE
description: "{{ doc('transfer_type') }}"
tests:
- not_null
- dbt_expectations.expect_column_values_to_be_in_type_list:
column_type_list:
- STRING
- VARCHAR
- name: SENDER
description: "Address that tokens are transferred from. If null, the sender is private and unresolvable."
tests:
- not_null:
where: transfer_type IN ('transfer_public', 'transfer_public_as_signer', 'transfer_public_to_private')
- dbt_expectations.expect_column_values_to_be_in_type_list:
column_type_list:
- STRING
- VARCHAR
- name: RECEIVER
description: "Address that tokens are transferred to. If null, the receiver is private and unresolvable."
tests:
- not_null:
where: transfer_type IN ('transfer_public', 'transfer_public_as_signer', 'transfer_private_to_public')
- dbt_expectations.expect_column_values_to_be_in_type_list:
column_type_list:
- STRING
- VARCHAR
- name: AMOUNT
description: "Number of tokens transferred. If null, the amount is private and unresolvable."
tests:
- not_null:
where: transfer_type != 'transfer_private'
- dbt_expectations.expect_column_values_to_be_in_type_list:
column_type_list:
- NUMBER
- FLOAT