added docs, tests, silver and gold models for native transfers

This commit is contained in:
Mike Stepanovic 2025-06-02 14:57:17 -06:00
parent d9cabceeb3
commit 9727c2f3e0
8 changed files with 237 additions and 1 deletions

View File

@ -0,0 +1,5 @@
{% docs from_address %}
The account address that sent the transfer.
{% enddocs %}

View File

@ -0,0 +1,5 @@
{% docs core__ez_native_transfers %}
This table contains a flattened easy version of the native transfers. This table uses the fact_transfers table as a base and filters down to only Movement (Aptos) tokens. The logic used to derive this table requires the withdrawal event to occur in the previous event to the deposit event and also requires the withdrawal event to be the same amount as the deposit event. The only exception to that rule is when a "CoinRegisterEvent" occurs in between the withdraw and deposit events. Any transfers that do not meet this criteria are not included in this table.
{% enddocs %}

View File

@ -0,0 +1,5 @@
{% docs to_address %}
The account address that received the transfer.
{% enddocs %}

View File

@ -0,0 +1,40 @@
{{ config(
materialized = 'incremental',
unique_key = ['tx_hash','to_address','from_address','block_timestamp::DATE'],
incremental_strategy = 'merge',
incremental_predicates = ["dynamic_range_predicate", "block_timestamp::DATE"],
merge_exclude_columns = ["inserted_timestamp"],
cluster_by = ['block_timestamp::DATE'],
post_hook = "ALTER TABLE {{ this }} ADD SEARCH OPTIMIZATION ON EQUALITY(tx_hash, version, from_address, to_address);",
tags = ['core']
) }}
SELECT
block_number,
block_timestamp,
tx_hash,
version,
success,
from_address,
to_address,
amount,
token_address,
{{ dbt_utils.generate_surrogate_key(
['tx_hash','to_address','from_address','block_timestamp::DATE']
) }} AS ez_native_transfers_id,
SYSDATE() AS inserted_timestamp,
SYSDATE() AS modified_timestamp,
'{{ invocation_id }}' AS _invocation_id
FROM
{{ ref(
'silver__transfers_native'
) }}
{% if is_incremental() %}
WHERE modified_timestamp >= (
SELECT
MAX(modified_timestamp)
FROM
{{ this }}
)
{% endif %}

View File

@ -1,5 +1,40 @@
version: 2
models:
- name: core__ez_native_transfers
description: '{{ doc("core__ez_native_transfers") }}'
tests:
- dbt_utils.recency:
datepart: hour
field: MODIFIED_TIMESTAMP
interval: 3
severity: error
tags: ['test_recency']
columns:
- name: block_number
description: '{{ doc("block_number") }}'
- name: block_timestamp
description: '{{ doc("block_timestamp") }}'
- name: tx_hash
description: '{{ doc("tx_hash") }}'
- name: version
description: '{{ doc("version") }}'
- name: success
description: '{{ doc("success") }}'
- name: from_address
description: '{{ doc("from_address") }}'
- name: to_address
description: '{{ doc("to_address") }}'
- name: amount
description: '{{ doc("amount") }}'
- name: token_address
description: '{{ doc("token_address") }}'
- name: ez_native_transfers_id
description: '{{ doc("pk") }}'
- name: inserted_timestamp
description: '{{ doc("inserted_timestamp") }}'
- name: modified_timestamp
description: '{{ doc("modified_timestamp") }}'
- name: core__fact_blocks
description: '{{ doc("core__fact_blocks") }}'
tests:

View File

@ -4,10 +4,12 @@
incremental_strategy = 'merge',
merge_exclude_columns = ["inserted_timestamp"],
cluster_by = ['block_timestamp::DATE','modified_timestamp::DATE'],
tags = ['core','full_test']
tags = ['core']
) }}
-- depends_on: {{ ref('core__fact_events') }}
-- depends_on: {{ ref('silver__fungiblestore_owners') }}
-- depends_on: {{ ref('silver__fungiblestore_metadata') }}
{% if execute %}
{% set base_query %}

View File

@ -0,0 +1,110 @@
{{ config(
materialized = 'incremental',
unique_key = ['tx_hash','_transfer_key','block_timestamp::DATE'],
incremental_strategy = 'merge',
merge_exclude_columns = ["inserted_timestamp"],
cluster_by = ['block_timestamp::DATE'],
post_hook = "ALTER TABLE {{ this }} ADD SEARCH OPTIMIZATION ON EQUALITY(tx_hash, version, from_address, to_address);",
tags = ['core']
) }}
-- depends_on: {{ ref('silver__transfers') }}
-- depends_on: {{ ref('core__fact_events') }}
WITH xfer AS (
SELECT
block_number,
block_timestamp,
tx_hash,
version,
success,
event_index,
transfer_event,
account_address,
amount,
token_address
FROM
{{ ref('silver__transfers') }}
WHERE
amount > 0
AND token_address = '0x1::aptos_coin::AptosCoin'
{% if is_incremental() %}
AND modified_timestamp >= (
SELECT
MAX(modified_timestamp)
FROM
{{ this }}
)
{% endif %}
),
wth AS (
SELECT
*
FROM
xfer
WHERE
transfer_event = 'WithdrawEvent'
),
dep AS (
SELECT
*
FROM
xfer
WHERE
transfer_event = 'DepositEvent'
),
reg AS (
SELECT
block_number,
block_timestamp,
tx_hash,
version,
success,
event_index
FROM
{{ ref('core__fact_events') }}
WHERE
event_type = '0x1::account::CoinRegisterEvent'
{% if is_incremental() %}
AND modified_timestamp >= (
SELECT
MAX(modified_timestamp)
FROM
{{ this }}
)
{% endif %}
)
SELECT
wth.block_number,
wth.block_timestamp,
wth.tx_hash,
wth.version,
wth.success,
wth.account_address AS from_address,
dep.account_address AS to_address,
wth.amount,
wth.token_address,
wth.event_index || ':' || dep.event_index AS _transfer_key,
{{ dbt_utils.generate_surrogate_key(
['wth.tx_hash','wth.event_index','dep.event_index']
) }} AS transfers_native_id,
SYSDATE() AS inserted_timestamp,
SYSDATE() AS modified_timestamp,
'{{ invocation_id }}' AS _invocation_id
FROM
wth
LEFT JOIN reg
ON wth.tx_hash = reg.tx_hash
AND wth.event_index + 1 = reg.event_index
JOIN dep
ON wth.tx_hash = dep.tx_hash
AND wth.amount = dep.amount
WHERE
wth.account_address <> dep.account_address
AND (
wth.event_index + 2 = dep.event_index
OR (reg.tx_hash IS NOT NULL AND reg.event_index + 1 = dep.event_index)
)

View File

@ -484,5 +484,39 @@ models:
data_type: TIMESTAMP_NTZ
- name: modified_timestamp
data_type: TIMESTAMP_NTZ
- name: _invocation_id
data_type: VARCHAR
- name: silver__transfers_native
config:
contract:
enforced: true
columns:
- name: block_number
data_type: NUMBER
- name: block_timestamp
data_type: TIMESTAMP_NTZ
- name: tx_hash
data_type: VARCHAR
- name: version
data_type: NUMBER
- name: success
data_type: BOOLEAN
- name: from_address
data_type: VARCHAR
- name: to_address
data_type: VARCHAR
- name: amount
data_type: NUMBER
- name: token_address
data_type: VARCHAR
- name: _transfer_key
data_type: VARCHAR
- name: transfers_native_id
data_type: VARCHAR
- name: inserted_timestamp
data_type: TIMESTAMP_NTZ
- name: modified_timestamp
data_type: TIMESTAMP_NTZ
- name: _invocation_id
data_type: VARCHAR