diff --git a/models/core/core__ez_usn.sql b/models/core/core__ez_usn.sql new file mode 100644 index 0000000..7f3a789 --- /dev/null +++ b/models/core/core__ez_usn.sql @@ -0,0 +1,23 @@ +{{ config( + materialized = 'view', + secure = true +) }} + +WITH transactions AS ( + + SELECT + * + FROM + {{ ref('silver__usn') }} +) +SELECT + block_timestamp, + block_id, + method_names, + tx_hash, + tx_receiver, + tx_signer, + old_owner, + new_owner, + amount +FROM transactions \ No newline at end of file diff --git a/models/core/core__ez_usn.yaml b/models/core/core__ez_usn.yaml new file mode 100644 index 0000000..6dba9e9 --- /dev/null +++ b/models/core/core__ez_usn.yaml @@ -0,0 +1,90 @@ +version: 2 + +models: + - name: core__ez_usn + description: |- + This table records all the usn transaction in the near db + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - tx_hash + + columns: + - name: BLOCK_TIMESTAMP + description: "{{ doc('block_timestamp')}}" + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - TIMESTAMP_NTZ + + - 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: METHOD_NAMES + description: "{{ doc('method_names')}}" + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - STRING + - VARCHAR + + - name: TX_HASH + description: "{{ doc('tx_hash')}}" + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - STRING + - VARCHAR + + - name: TX_RECEIVER + description: "{{ doc('tx_receiver')}}" + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - STRING + - VARCHAR + + - name: TX_SIGNER + description: "{{ doc('tx_signer')}}" + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - STRING + - VARCHAR + + + - name: OLD_OWNER + description: "{{ doc('old_owner')}}" + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - STRING + - VARCHAR + + - name: NEW_OWNER + description: "{{ doc('new_owner')}}" + tests: + - 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 diff --git a/models/descriptions/amount.md b/models/descriptions/amount.md new file mode 100644 index 0000000..fcfd77a --- /dev/null +++ b/models/descriptions/amount.md @@ -0,0 +1,5 @@ +{% docs amount %} + +The amount of tokens transferred + +{% enddocs %} diff --git a/models/descriptions/method_names.md b/models/descriptions/method_names.md new file mode 100644 index 0000000..4727005 --- /dev/null +++ b/models/descriptions/method_names.md @@ -0,0 +1,6 @@ +{% docs method_names %} + +The function that is being called for the transaction +can be ft_transfer_call, ft_transfer or withdraw + +{% enddocs %} diff --git a/models/descriptions/new_owner.md b/models/descriptions/new_owner.md new file mode 100644 index 0000000..8605066 --- /dev/null +++ b/models/descriptions/new_owner.md @@ -0,0 +1,5 @@ +{% docs new_owner %} + +The address that receives the token. + +{% enddocs %} diff --git a/models/descriptions/old_owner.md b/models/descriptions/old_owner.md new file mode 100644 index 0000000..3b4b537 --- /dev/null +++ b/models/descriptions/old_owner.md @@ -0,0 +1,6 @@ +{% docs old_owner %} + +The previous owner of the token. +Can also be the address signing the transaction + +{% enddocs %} diff --git a/models/silver/silver__usn.sql b/models/silver/silver__usn.sql new file mode 100644 index 0000000..579b262 --- /dev/null +++ b/models/silver/silver__usn.sql @@ -0,0 +1,108 @@ +{{ config( + materialized = 'incremental', + unique_key = 'tx_hash', + incremental_strategy = 'delete+insert', + cluster_by = ['block_timestamp::DATE', '_inserted_timestamp::DATE'], +) }} + +WITH txs AS ( + + SELECT + * + FROM + {{ ref('silver__transactions') }} + WHERE + {{ incremental_load_filter('_inserted_timestamp') }} +), + +usn_tx AS ( +SELECT + block_id, + tx_hash, + block_timestamp, + tx_receiver, + tx_signer, + tx: actions[0]: FunctionCall: method_name :: STRING AS method_names, + tx: receipt[0] :: VARIANT AS receipts, + tx: receipt[0]: outcome: logs :: VARIANT AS events, + _ingested_at, + _inserted_timestamp +FROM txs +where tx_receiver = 'usn' +), + +usn_method_call AS ( +SELECT + block_id, + tx_hash, + block_timestamp, + tx_receiver, + tx_signer, + method_names, + events, + _ingested_at, + _inserted_timestamp +FROM usn_tx +where method_names='ft_transfer_call' or method_names='ft_transfer' or method_names='withdraw' +), + +parse_event AS ( +SELECT + block_id, + tx_hash, + block_timestamp, + tx_receiver, + tx_signer, + method_names, + flatten_events.value AS events, + _ingested_at, + _inserted_timestamp +FROM usn_method_call, +lateral flatten(input => usn_method_call.events) AS flatten_events +), + +extract_events AS ( +SELECT + block_id, + tx_hash, + block_timestamp, + tx_receiver, + tx_signer, + method_names, + substr(events, 12, 1000) AS event_string, + _ingested_at, + _inserted_timestamp +FROM parse_event +), + +extract_data AS ( +SELECT + method_names, + block_id, + tx_hash, + block_timestamp, + tx_receiver, + tx_signer, + parse_json(event_string) AS event_data, + _ingested_at, + _inserted_timestamp +FROM extract_events +), + +final AS ( +SELECT + block_timestamp, + block_id, + method_names, + tx_hash, + tx_receiver, + tx_signer, + event_data: data[0]: old_owner_id::string AS old_owner, + event_data: data[0]: new_owner_id::string AS new_owner, + event_data: data[0]: amount::integer AS amount, + _ingested_at, + _inserted_timestamp +FROM extract_data +) + +SELECT * FROM final diff --git a/models/silver/silver__usn.yaml b/models/silver/silver__usn.yaml new file mode 100644 index 0000000..4c323d3 --- /dev/null +++ b/models/silver/silver__usn.yaml @@ -0,0 +1,90 @@ +version: 2 + +models: + - name: silver__usn + description: |- + This table records all the usn transaction in the near db + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - tx_hash + + columns: + - name: BLOCK_TIMESTAMP + description: "{{ doc('block_timestamp')}}" + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - TIMESTAMP_NTZ + + - 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: METHOD_NAMES + description: "{{ doc('method_names')}}" + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - STRING + - VARCHAR + + - name: TX_HASH + description: "{{ doc('tx_hash')}}" + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - STRING + - VARCHAR + + - name: TX_RECEIVER + description: "{{ doc('tx_receiver')}}" + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - STRING + - VARCHAR + + - name: TX_SIGNER + description: "{{ doc('tx_signer')}}" + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - STRING + - VARCHAR + + + - name: OLD_OWNER + description: "{{ doc('old_owner')}}" + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - STRING + - VARCHAR + + - name: NEW_OWNER + description: "{{ doc('new_owner')}}" + tests: + - 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