An 1779/ez token transfers (#49)

* Token transfers model and tests

* logic to include transfers only

* Ez token transfers and tests

* Silver incremental model and core view

* Incremental adds and date change
This commit is contained in:
Ava Masucci 2022-08-17 11:19:09 -07:00 committed by GitHub
parent 123d690227
commit 417f5df20d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 311 additions and 0 deletions

View File

@ -0,0 +1,18 @@
{{ config(
materialized = 'view'
) }}
SELECT
block_height,
block_timestamp,
tx_id,
sender,
recipient,
token_contract,
amount,
tx_succeeded
FROM
{{ ref('silver__token_transfers') }}
WHERE
block_timestamp::date >= '2022-04-20'

View File

@ -0,0 +1,82 @@
version: 2
models:
- name: core__ez_token_transfers
description: |-
This table records all token transfers on the FLOW blockchain.
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- tx_id
- sender
- recipient
- token_contract
columns:
- name: tx_id
description: "{{ doc('tx_id') }}"
tests:
- not_null
- name: block_timestamp
description: "{{ doc('block_timestamp') }}"
tests:
- not_null
- 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: block_height
description: "{{ doc('block_height') }}"
tests:
- not_null
- dbt_expectations.expect_column_values_to_be_in_type_list:
column_type_list:
- NUMBER
- FLOAT
- name: sender
description: "{{ doc('sender') }}"
tests:
- not_null
- dbt_expectations.expect_column_values_to_be_in_type_list:
column_type_list:
- STRING
- VARCHAR
- name: recipient
description: "{{ doc('recipient') }}"
tests:
- dbt_expectations.expect_column_values_to_be_in_type_list:
column_type_list:
- STRING
- VARCHAR
- name: token_contract
description: "{{ doc('token_contract') }}"
tests:
- not_null
- dbt_expectations.expect_column_values_to_be_in_type_list:
column_type_list:
- STRING
- VARCHAR
- name: amount
description: "{{ doc('amount') }}"
tests:
- not_null
- dbt_expectations.expect_column_values_to_be_in_type_list:
column_type_list:
- NUMBER
- FLOAT
- name: tx_succeeded
description: "{{ doc('tx_succeeded') }}"
tests:
- not_null
- dbt_expectations.expect_column_values_to_be_in_type_list:
column_type_list:
- BOOLEAN

View File

@ -0,0 +1,5 @@
{% docs recipient %}
Address receiving the transferred token.
{% enddocs %}

View File

@ -0,0 +1,5 @@
{% docs sender %}
Address sending the transferred token.
{% enddocs %}

View File

@ -0,0 +1,119 @@
{{ config(
materialized = 'incremental',
incremental_strategy = 'delete+insert',
cluster_by = ['_inserted_timestamp::date'],
unique_key = "CONCAT_WS('-', tx_id, sender, recipient, token_contract)"
) }}
WITH transfers AS (
SELECT
_inserted_timestamp,
tx_id,
COUNT(event_type) AS event_count,
MAX(event_index + 1) AS max_index
FROM
{{ ref('silver__events_final') }}
WHERE
event_type IN ('TokensDeposited', 'TokensWithdrawn', 'FeesDeducted')
{% if is_incremental() %}
AND
_inserted_timestamp >= (
SELECT
MAX(_inserted_timestamp)
FROM
{{ this }}
)
{% endif %}
GROUP BY
_inserted_timestamp, tx_id
HAVING
event_count = max_index
),
withdraws AS (
SELECT
block_height,
_inserted_timestamp,
block_timestamp,
tx_id,
event_data:from::STRING AS sender,
event_contract AS token_contract,
event_data:amount::FLOAT AS amount,
tx_succeeded
FROM
{{ ref('silver__events_final') }}
WHERE
tx_id IN (SELECT tx_id FROM transfers)
AND
event_type = 'TokensWithdrawn'
{% if is_incremental() %}
AND
_inserted_timestamp >= (
SELECT
MAX(_inserted_timestamp)
FROM
{{ this }}
)
{% endif %}
GROUP BY
block_height, _inserted_timestamp, block_timestamp, tx_id, sender, token_contract, amount, tx_succeeded
),
deposits AS (
SELECT
tx_id,
_inserted_timestamp,
event_data:to::STRING AS recipient,
event_contract AS token_contract,
event_data:amount::FLOAT AS amount
FROM
{{ ref('silver__events_final') }}
WHERE
tx_id IN (SELECT tx_id FROM transfers)
AND
event_type = 'TokensDeposited'
{% if is_incremental() %}
AND
_inserted_timestamp >= (
SELECT
MAX(_inserted_timestamp)
FROM
{{ this }}
)
{% endif %}
GROUP BY
tx_id, _inserted_timestamp, recipient, token_contract, amount
)
SELECT
block_height,
w._inserted_timestamp AS _inserted_timestamp,
block_timestamp,
w.tx_id,
sender,
recipient,
w.token_contract,
SUM(COALESCE(d.amount, w.amount)) AS amount,
tx_succeeded
FROM
withdraws w
LEFT JOIN
deposits d
ON w.tx_id = d.tx_id
AND w.token_contract = d.token_contract
AND w.amount = d.amount
WHERE sender IS NOT NULL
GROUP BY
block_height, w._inserted_timestamp, block_timestamp, w.tx_id, sender, recipient, w.token_contract, tx_succeeded

View File

@ -0,0 +1,82 @@
version: 2
models:
- name: silver__token_transfers
description: |-
This table records all token transfers on the FLOW blockchain.
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- tx_id
- sender
- recipient
- token_contract
columns:
- name: tx_id
description: "{{ doc('tx_id') }}"
tests:
- not_null
- name: block_timestamp
description: "{{ doc('block_timestamp') }}"
tests:
- not_null
- 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: block_height
description: "{{ doc('block_height') }}"
tests:
- not_null
- dbt_expectations.expect_column_values_to_be_in_type_list:
column_type_list:
- NUMBER
- FLOAT
- name: sender
description: "{{ doc('sender') }}"
tests:
- not_null
- dbt_expectations.expect_column_values_to_be_in_type_list:
column_type_list:
- STRING
- VARCHAR
- name: recipient
description: "{{ doc('recipient') }}"
tests:
- dbt_expectations.expect_column_values_to_be_in_type_list:
column_type_list:
- STRING
- VARCHAR
- name: token_contract
description: "{{ doc('token_contract') }}"
tests:
- not_null
- dbt_expectations.expect_column_values_to_be_in_type_list:
column_type_list:
- STRING
- VARCHAR
- name: amount
description: "{{ doc('amount') }}"
tests:
- not_null
- dbt_expectations.expect_column_values_to_be_in_type_list:
column_type_list:
- NUMBER
- FLOAT
- name: tx_succeeded
description: "{{ doc('tx_succeeded') }}"
tests:
- not_null
- dbt_expectations.expect_column_values_to_be_in_type_list:
column_type_list:
- BOOLEAN