stand up models

This commit is contained in:
Austin 2024-06-11 10:14:01 -04:00
parent 6a45384e35
commit 866efe6e3e
83 changed files with 2601 additions and 1 deletions

66
.github/workflows/dbt_run_adhoc.yml vendored Normal file
View File

@ -0,0 +1,66 @@
name: dbt_run_adhoc
run-name: dbt_run_adhoc
on:
workflow_dispatch:
branches:
- "main"
inputs:
environment:
type: choice
description: DBT Run Environment
required: true
options:
- dev
- prod
default: dev
warehouse:
type: choice
description: Snowflake warehouse
required: true
options:
- DBT
- DBT_CLOUD
- DBT_EMERGENCY
default: DBT
dbt_command:
type: string
description: 'DBT Run Command'
required: true
env:
DBT_PROFILES_DIR: ./
ACCOUNT: "${{ vars.ACCOUNT }}"
ROLE: "${{ vars.ROLE }}"
USER: "${{ vars.USER }}"
PASSWORD: "${{ secrets.PASSWORD }}"
REGION: "${{ vars.REGION }}"
DATABASE: "${{ vars.DATABASE }}"
WAREHOUSE: "${{ inputs.warehouse }}"
SCHEMA: "${{ vars.SCHEMA }}"
concurrency:
group: ${{ github.workflow }}
jobs:
run_dbt_jobs:
runs-on: ubuntu-latest
environment:
name: workflow_${{ inputs.environment }}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: "3.10"
cache: "pip"
- name: install dependencies
run: |
pip install -r requirements.txt
dbt deps
- name: Run DBT Jobs
run: |
${{ inputs.dbt_command }}

View File

@ -0,0 +1,44 @@
name: dbt_run_scheduled_non_realtime
run-name: dbt_run_scheduled_non_realtime
on:
workflow_dispatch:
branches:
- "main"
env:
DBT_PROFILES_DIR: ./
ACCOUNT: "${{ vars.ACCOUNT }}"
ROLE: "${{ vars.ROLE }}"
USER: "${{ vars.USER }}"
PASSWORD: "${{ secrets.PASSWORD }}"
REGION: "${{ vars.REGION }}"
DATABASE: "${{ vars.DATABASE }}"
WAREHOUSE: "${{ vars.WAREHOUSE }}"
SCHEMA: "${{ vars.SCHEMA }}"
concurrency:
group: ${{ github.workflow }}
jobs:
run_dbt_jobs:
runs-on: ubuntu-latest
environment:
name: workflow_prod
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: "3.10"
cache: "pip"
- name: install dependencies
run: |
pip install -r requirements.txt
dbt deps
- name: Run DBT Jobs
run: |
dbt run -m "kaia_models,tag:non_realtime"

48
.github/workflows/dbt_test_intraday.yml vendored Normal file
View File

@ -0,0 +1,48 @@
name: dbt_test_intraday
run-name: dbt_test_intraday
on:
workflow_dispatch:
schedule:
# Runs “At minute 5 past every 4th hour.” (see https://crontab.guru)
- cron: '10 */4 * * *'
env:
DBT_PROFILES_DIR: ./
ACCOUNT: "${{ vars.ACCOUNT }}"
ROLE: "${{ vars.ROLE }}"
USER: "${{ vars.USER }}"
PASSWORD: "${{ secrets.PASSWORD }}"
REGION: "${{ vars.REGION }}"
DATABASE: "${{ vars.DATABASE }}"
WAREHOUSE: "${{ vars.WAREHOUSE }}"
SCHEMA: "${{ vars.SCHEMA }}"
concurrency:
group: ${{ github.workflow }}
jobs:
run_dbt_jobs:
runs-on: ubuntu-latest
environment:
name: workflow_test
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: "3.10"
cache: "pip"
- name: install dependencies
run: |
pip install -r requirements.txt
dbt deps
- name: Run DBT Jobs
run: |
dbt test -m "kaia_models,tag:recent_test"

18
.gitignore vendored Normal file
View File

@ -0,0 +1,18 @@
target/
dbt_modules/
# newer versions of dbt use this directory instead of dbt_modules for test dependencies
dbt_packages/
logs/
.venv/
.python-version
# Visual Studio Code files
*/.vscode
*.code-workspace
.history/
**/.DS_Store
.vscode/
.env
dbt-env/

110
README.md
View File

@ -1 +1,109 @@
# kaia-models
## Profile Set Up
#### Use the following within profiles.yml
----
```yml
kaia:
target: dev
outputs:
dev:
type: snowflake
account: <ACCOUNT>
role: <ROLE>
user: <USERNAME>
password: <PASSWORD>
region: <REGION>
database: KAIA_DEV
warehouse: <WAREHOUSE>
schema: silver
threads: 12
client_session_keep_alive: False
query_tag: <TAG>
prod:
type: snowflake
account: <ACCOUNT>
role: <ROLE>
user: <USERNAME>
password: <PASSWORD>
region: <REGION>
database: KAIA
warehouse: <WAREHOUSE>
schema: silver
threads: 12
client_session_keep_alive: False
query_tag: <TAG>
```
### Variables
To control the creation of UDF or SP macros with dbt run:
* UPDATE_UDFS_AND_SPS
When True, executes all macros included in the on-run-start hooks within dbt_project.yml on model run as normal
When False, none of the on-run-start macros are executed on model run
Default values are False
* Usage:
dbt run --vars '{"UPDATE_UDFS_AND_SPS":True}' -m ...
To reload records in a curated complete table without a full-refresh, such as `silver_bridge.complete_bridge_activity`:
* HEAL_CURATED_MODEL
Default is an empty array []
When item is included in var array [], incremental logic will be skipped for that CTE / code block
When item is not included in var array [] or does not match specified item in model, incremental logic will apply
Example set up: `{% if is_incremental() and 'axelar' not in var('HEAL_CURATED_MODEL') %}`
* Usage:
Single CTE: dbt run --vars '{"HEAL_CURATED_MODEL":"axelar"}' -m ...
Multiple CTEs: dbt run --vars '{"HEAL_CURATED_MODEL":["axelar","across","celer_cbridge"]}' -m ...
### Resources:
- Learn more about dbt [in the docs](https://docs.getdbt.com/docs/introduction)
- Check out [Discourse](https://discourse.getdbt.com/) for commonly asked questions and answers
- Join the [chat](https://community.getdbt.com/) on Slack for live discussions and support
- Find [dbt events](https://events.getdbt.com) near you
- Check out [the blog](https://blog.getdbt.com/) for the latest news on dbt's development and best practices
## Applying Model Tags
### Database / Schema level tags
Database and schema tags are applied via the `add_database_or_schema_tags` macro. These tags are inherited by their downstream objects. To add/modify tags call the appropriate tag set function within the macro.
```
{{ set_database_tag_value('SOME_DATABASE_TAG_KEY','SOME_DATABASE_TAG_VALUE') }}
{{ set_schema_tag_value('SOME_SCHEMA_TAG_KEY','SOME_SCHEMA_TAG_VALUE') }}
```
### Model tags
To add/update a model's snowflake tags, add/modify the `meta` model property under `config`. Only table level tags are supported at this time via DBT.
```
{{ config(
...,
meta={
'database_tags':{
'table': {
'PURPOSE': 'SOME_PURPOSE'
}
}
},
...
) }}
```
By default, model tags are pushed to Snowflake on each load. You can disable this by setting the `UPDATE_SNOWFLAKE_TAGS` project variable to `False` during a run.
```
dbt run --vars '{"UPDATE_SNOWFLAKE_TAGS":False}' -s models/core/core__fact_blocks.sql
```
### Querying for existing tags on a model in snowflake
```
select *
from table(kaia.information_schema.tag_references('kaia.core.fact_blocks', 'table'));
```

0
analysis/.gitkeep Normal file
View File

0
data/.gitkeep Normal file
View File

View File

@ -0,0 +1,2 @@
workflow_name,workflow_schedule
dbt_run_scheduled_non_realtime,"15,45 * * * *"
1 workflow_name workflow_schedule
2 dbt_run_scheduled_non_realtime 15,45 * * * *

69
dbt_project.yml Normal file
View File

@ -0,0 +1,69 @@
# Name your project! Project names should contain only lowercase characters
# and underscores. A good package name should reflect your organization's
# name or the intended use of these models
name: "kaia_models"
version: "1.0.0"
config-version: 2
# This setting configures which "profile" dbt uses for this project.
profile: "kaia"
# These configurations specify where dbt should look for different types of files.
# The `source-paths` config, for example, states that models in this project can be
# found in the "models/" directory. You probably won't need to change these!
model-paths: ["models"]
analysis-paths: ["analysis"]
test-paths: ["tests"]
seed-paths: ["data"]
macro-paths: ["macros"]
snapshot-paths: ["snapshots"]
target-path: "target" # directory which will store compiled SQL files
clean-targets: # directories to be removed by `dbt clean`
- "target"
- "dbt_modules"
- "dbt_packages"
tests:
+store_failures: true # all tests
on-run-start:
- "{{ create_sps() }}"
- "{{ create_udfs() }}"
on-run-end:
- '{{ apply_meta_as_tags(results) }}'
dispatch:
- macro_namespace: dbt
search_order:
- kaia-models
- dbt_snowflake_query_tags
- dbt
query-comment:
comment: '{{ dbt_snowflake_query_tags.get_query_comment(node) }}'
append: true # Snowflake removes prefixed comments.
# Configuring models
# Full documentation: https://docs.getdbt.com/docs/configuring-models
models:
+copy_grants: true
+on_schema_change: "append_new_columns"
# In this example config, we tell dbt to build all models in the example/ directory
# as tables. These settings can be overridden in the individual model files
# using the `{{ config(...) }}` macro.
vars:
"dbt_date:time_zone": GMT
STREAMLINE_INVOKE_STREAMS: False
STREAMLINE_USE_DEV_FOR_EXTERNAL_TABLES: False
UPDATE_UDFS_AND_SPS: False
UPDATE_SNOWFLAKE_TAGS: True
OBSERV_FULL_TEST: False
WAIT: 0
HEAL_MODEL: False
HEAL_CURATED_MODEL: []
START_GHA_TASKS: False

0
macros/.gitkeep Normal file
View File

8
macros/create_sps.sql Normal file
View File

@ -0,0 +1,8 @@
{% macro create_sps() %}
{% if var("UPDATE_UDFS_AND_SPS") %}
{% if target.database == 'KAIA' %}
CREATE schema IF NOT EXISTS _internal;
{{ sp_create_prod_clone('_internal') }};
{% endif %}
{% endif %}
{% endmacro %}

5
macros/create_udfs.sql Normal file
View File

@ -0,0 +1,5 @@
{% macro create_udfs() %}
{% if var("UPDATE_UDFS_AND_SPS") %}
{{- fsc_utils.create_udfs() -}}
{% endif %}
{% endmacro %}

View File

@ -0,0 +1,17 @@
{% macro generate_schema_name(custom_schema_name=none, node=none) -%}
{% set node_name = node.name %}
{% set split_name = node_name.split('__') %}
{{ split_name[0] | trim }}
{%- endmacro %}
{% macro generate_alias_name(custom_alias_name=none, node=none) -%}
{% set node_name = node.name %}
{% set split_name = node_name.split('__') %}
{{ split_name[1] | trim }}
{%- endmacro %}
{% macro generate_tmp_view_name(model_name) -%}
{% set node_name = model_name.name %}
{% set split_name = node_name.split('__') %}
{{ target.database ~ '.' ~ split_name[0] ~ '.' ~ split_name [1] ~ '__dbt_tmp' | trim }}
{%- endmacro %}

View File

@ -0,0 +1,44 @@
{% macro get_merge_sql(
target,
source,
unique_key,
dest_columns,
incremental_predicates
) -%}
{% set predicate_override = "" %}
{% if incremental_predicates [0] == "dynamic_range" %}
-- run some queries to dynamically determine the min + max of this 'input_column' in the new data
{% set input_column = incremental_predicates [1] %}
{% set get_limits_query %}
SELECT
MIN(
{{ input_column }}
) AS lower_limit,
MAX(
{{ input_column }}
) AS upper_limit
FROM
{{ source }}
{% endset %}
{% set limits = run_query(get_limits_query) [0] %}
{% set lower_limit,
upper_limit = limits [0],
limits [1] %}
-- use those calculated min + max values to limit 'target' scan, to only the days with new data
{% set predicate_override %}
dbt_internal_dest.{{ input_column }} BETWEEN '{{ lower_limit }}'
AND '{{ upper_limit }}' {% endset %}
{% endif %}
{% set predicates = [predicate_override] if predicate_override else incremental_predicates %}
-- standard merge from here
{% set merge_sql = dbt.get_merge_sql(
target,
source,
unique_key,
dest_columns,
predicates
) %}
{{ return(merge_sql) }}
{% endmacro %}

View File

@ -0,0 +1,8 @@
{% macro dbt_snowflake_get_tmp_relation_type(
strategy,
unique_key,
language
) %}
-- always table
{{ return('table') }}
{% endmacro %}

View File

@ -0,0 +1,10 @@
{% macro run_sp_create_prod_clone() %}
{% set clone_query %}
call kaia._internal.create_prod_clone(
'kaia',
'kaia_dev',
'internal_dev'
);
{% endset %}
{% do run_query(clone_query) %}
{% endmacro %}

View File

@ -0,0 +1,44 @@
{% macro sp_create_prod_clone(target_schema) -%}
create or replace procedure {{ target_schema }}.create_prod_clone(source_db_name string, destination_db_name string, role_name string)
returns boolean
language javascript
execute as caller
as
$$
snowflake.execute({sqlText: `BEGIN TRANSACTION;`});
try {
snowflake.execute({sqlText: `CREATE OR REPLACE DATABASE ${DESTINATION_DB_NAME} CLONE ${SOURCE_DB_NAME}`});
snowflake.execute({sqlText: `DROP SCHEMA IF EXISTS ${DESTINATION_DB_NAME}._INTERNAL`}); /* this only needs to be in prod */
snowflake.execute({sqlText: `GRANT OWNERSHIP ON ALL SCHEMAS IN DATABASE ${DESTINATION_DB_NAME} TO ROLE ${ROLE_NAME} COPY CURRENT GRANTS;`});
snowflake.execute({sqlText: `GRANT OWNERSHIP ON ALL FUNCTIONS IN DATABASE ${DESTINATION_DB_NAME} TO ROLE ${ROLE_NAME} COPY CURRENT GRANTS;`});
snowflake.execute({sqlText: `GRANT OWNERSHIP ON ALL PROCEDURES IN DATABASE ${DESTINATION_DB_NAME} TO ROLE ${ROLE_NAME} COPY CURRENT GRANTS;`});
snowflake.execute({sqlText: `GRANT OWNERSHIP ON ALL VIEWS IN DATABASE ${DESTINATION_DB_NAME} TO ROLE ${ROLE_NAME} COPY CURRENT GRANTS;`});
snowflake.execute({sqlText: `GRANT OWNERSHIP ON ALL STAGES IN DATABASE ${DESTINATION_DB_NAME} TO ROLE ${ROLE_NAME} COPY CURRENT GRANTS;`});
snowflake.execute({sqlText: `GRANT OWNERSHIP ON ALL TABLES IN DATABASE ${DESTINATION_DB_NAME} TO ROLE ${ROLE_NAME} COPY CURRENT GRANTS;`});
snowflake.execute({sqlText: `GRANT OWNERSHIP ON FUTURE FUNCTIONS IN DATABASE ${DESTINATION_DB_NAME} TO ROLE ${ROLE_NAME};`});
snowflake.execute({sqlText: `GRANT OWNERSHIP ON FUTURE PROCEDURES IN DATABASE ${DESTINATION_DB_NAME} TO ROLE ${ROLE_NAME};`});
snowflake.execute({sqlText: `GRANT OWNERSHIP ON FUTURE VIEWS IN DATABASE ${DESTINATION_DB_NAME} TO ROLE ${ROLE_NAME};`});
snowflake.execute({sqlText: `GRANT OWNERSHIP ON FUTURE STAGES IN DATABASE ${DESTINATION_DB_NAME} TO ROLE ${ROLE_NAME};`});
snowflake.execute({sqlText: `GRANT OWNERSHIP ON FUTURE TABLES IN DATABASE ${DESTINATION_DB_NAME} TO ROLE ${ROLE_NAME};`});
snowflake.execute({sqlText: `GRANT OWNERSHIP ON DATABASE ${DESTINATION_DB_NAME} TO ROLE ${ROLE_NAME} COPY CURRENT GRANTS;`})
var existing_tags = snowflake.execute({sqlText: `SHOW TAGS IN DATABASE ${DESTINATION_DB_NAME};`});
while (existing_tags.next()) {
var schema = existing_tags.getColumnValue(4);
var tag_name = existing_tags.getColumnValue(2)
snowflake.execute({sqlText: `GRANT OWNERSHIP ON TAG ${DESTINATION_DB_NAME}.${schema}.${tag_name} TO ROLE ${ROLE_NAME} COPY CURRENT GRANTS;`});
}
snowflake.execute({sqlText: `COMMIT;`});
} catch (err) {
snowflake.execute({sqlText: `ROLLBACK;`});
throw(err);
}
return true
$$
{%- endmacro %}

View File

@ -0,0 +1,4 @@
{% macro add_database_or_schema_tags() %}
{{ set_database_tag_value('BLOCKCHAIN_NAME','KAIA') }}
{{ set_database_tag_value('BLOCKCHAIN_TYPE','EVM') }}
{% endmacro %}

View File

@ -0,0 +1,127 @@
{% macro apply_meta_as_tags(results) %}
{% if var("UPDATE_SNOWFLAKE_TAGS") %}
{{ log('apply_meta_as_tags', info=False) }}
{{ log(results, info=False) }}
{% if execute %}
{%- set tags_by_schema = {} -%}
{% for res in results -%}
{% if res.node.meta.database_tags %}
{%- set model_database = res.node.database -%}
{%- set model_schema = res.node.schema -%}
{%- set model_schema_full = model_database+'.'+model_schema -%}
{%- set model_alias = res.node.alias -%}
{% if model_schema_full not in tags_by_schema.keys() %}
{{ log('need to fetch tags for schema '+model_schema_full, info=False) }}
{%- call statement('main', fetch_result=True) -%}
show tags in {{model_database}}.{{model_schema}}
{%- endcall -%}
{%- set _ = tags_by_schema.update({model_schema_full: load_result('main')['table'].columns.get('name').values()|list}) -%}
{{ log('Added tags to cache', info=False) }}
{% else %}
{{ log('already have tag info for schema', info=False) }}
{% endif %}
{%- set current_tags_in_schema = tags_by_schema[model_schema_full] -%}
{{ log('current_tags_in_schema:', info=False) }}
{{ log(current_tags_in_schema, info=False) }}
{{ log("========== Processing tags for "+model_schema_full+"."+model_alias+" ==========", info=False) }}
{% set line -%}
node: {{ res.node.unique_id }}; status: {{ res.status }} (message: {{ res.message }})
node full: {{ res.node}}
meta: {{ res.node.meta}}
materialized: {{ res.node.config.materialized }}
{%- endset %}
{{ log(line, info=False) }}
{%- call statement('main', fetch_result=True) -%}
select LEVEL,UPPER(TAG_NAME) as TAG_NAME,TAG_VALUE from table(information_schema.tag_references_all_columns('{{model_schema}}.{{model_alias}}', 'table'))
{%- endcall -%}
{%- set existing_tags_for_table = load_result('main')['data'] -%}
{{ log('Existing tags for table:', info=False) }}
{{ log(existing_tags_for_table, info=False) }}
{{ log('--', info=False) }}
{% for table_tag in res.node.meta.database_tags.table %}
{{ create_tag_if_missing(current_tags_in_schema,table_tag|upper) }}
{% set desired_tag_value = res.node.meta.database_tags.table[table_tag] %}
{{set_table_tag_value_if_different(model_schema,model_alias,table_tag,desired_tag_value,existing_tags_for_table)}}
{% endfor %}
{{ log("========== Finished processing tags for "+model_alias+" ==========", info=False) }}
{% endif %}
{% endfor %}
{% endif %}
{% endif %}
{% endmacro %}
{% macro create_tag_if_missing(all_tag_names,table_tag) %}
{% if table_tag not in all_tag_names %}
{{ log('Creating missing tag '+table_tag, info=False) }}
{%- call statement('main', fetch_result=True) -%}
create tag if not exists silver.{{table_tag}}
{%- endcall -%}
{{ log(load_result('main').data, info=False) }}
{% else %}
{{ log('Tag already exists: '+table_tag, info=False) }}
{% endif %}
{% endmacro %}
{% macro set_table_tag_value_if_different(model_schema,table_name,tag_name,desired_tag_value,existing_tags) %}
{{ log('Ensuring tag '+tag_name+' has value '+desired_tag_value+' at table level', info=False) }}
{%- set existing_tag_for_table = existing_tags|selectattr('0','equalto','TABLE')|selectattr('1','equalto',tag_name|upper)|list -%}
{{ log('Filtered tags for table:', info=False) }}
{{ log(existing_tag_for_table[0], info=False) }}
{% if existing_tag_for_table|length > 0 and existing_tag_for_table[0][2]==desired_tag_value %}
{{ log('Correct tag value already exists', info=False) }}
{% else %}
{{ log('Setting tag value for '+tag_name+' to value '+desired_tag_value, info=False) }}
{%- call statement('main', fetch_result=True) -%}
alter table {{model_schema}}.{{table_name}} set tag {{tag_name}} = '{{desired_tag_value}}'
{%- endcall -%}
{{ log(load_result('main').data, info=False) }}
{% endif %}
{% endmacro %}
{% macro set_column_tag_value_if_different(table_name,column_name,tag_name,desired_tag_value,existing_tags) %}
{{ log('Ensuring tag '+tag_name+' has value '+desired_tag_value+' at column level', info=False) }}
{%- set existing_tag_for_column = existing_tags|selectattr('0','equalto','COLUMN')|selectattr('1','equalto',tag_name|upper)|list -%}
{{ log('Filtered tags for column:', info=False) }}
{{ log(existing_tag_for_column[0], info=False) }}
{% if existing_tag_for_column|length > 0 and existing_tag_for_column[0][2]==desired_tag_value %}
{{ log('Correct tag value already exists', info=False) }}
{% else %}
{{ log('Setting tag value for '+tag_name+' to value '+desired_tag_value, info=False) }}
{%- call statement('main', fetch_result=True) -%}
alter table {{table_name}} modify column {{column_name}} set tag {{tag_name}} = '{{desired_tag_value}}'
{%- endcall -%}
{{ log(load_result('main').data, info=False) }}
{% endif %}
{% endmacro %}
{% macro set_database_tag_value(tag_name,tag_value) %}
{% set query %}
create tag if not exists silver.{{tag_name}}
{% endset %}
{% do run_query(query) %}
{% set query %}
alter database {{target.database}} set tag {{target.database}}.silver.{{tag_name}} = '{{tag_value}}'
{% endset %}
{% do run_query(query) %}
{% endmacro %}
{% macro set_schema_tag_value(target_schema,tag_name,tag_value) %}
{% set query %}
create tag if not exists silver.{{tag_name}}
{% endset %}
{% do run_query(query) %}
{% set query %}
alter schema {{target.database}}.{{target_schema}} set tag {{target.database}}.silver.{{tag_name}} = '{{tag_value}}'
{% endset %}
{% do run_query(query) %}
{% endmacro %}

View File

@ -0,0 +1,249 @@
{% macro missing_txs(
model
) %}
WITH txs_base AS (
SELECT
block_number AS base_block_number,
tx_hash AS base_tx_hash
FROM
{{ ref('test_silver__transactions_full') }}
),
model_name AS (
SELECT
block_number AS model_block_number,
tx_hash AS model_tx_hash
FROM
{{ model }}
)
SELECT
base_block_number,
base_tx_hash,
model_block_number,
model_tx_hash
FROM
txs_base
LEFT JOIN model_name
ON base_block_number = model_block_number
AND base_tx_hash = model_tx_hash
WHERE
(
model_tx_hash IS NULL
OR model_block_number IS NULL
)
{% endmacro %}
{% macro recent_missing_txs(
model
) %}
WITH txs_base AS (
SELECT
block_number AS base_block_number,
tx_hash AS base_tx_hash
FROM
{{ ref('test_silver__transactions_recent') }}
),
model_name AS (
SELECT
block_number AS model_block_number,
tx_hash AS model_tx_hash
FROM
{{ model }}
)
SELECT
base_block_number,
base_tx_hash,
model_block_number,
model_tx_hash
FROM
txs_base
LEFT JOIN model_name
ON base_block_number = model_block_number
AND base_tx_hash = model_tx_hash
WHERE
model_tx_hash IS NULL
OR model_block_number IS NULL
{% endmacro %}
{% macro gold_missing_txs(
model
) %}
WITH txs_base AS (
SELECT
block_number AS base_block_number,
tx_hash AS base_tx_hash
FROM
{{ ref('test_gold__transactions_full') }}
),
model_name AS (
SELECT
block_number AS model_block_number,
tx_hash AS model_tx_hash
FROM
{{ model }}
)
SELECT
base_block_number,
base_tx_hash,
model_block_number,
model_tx_hash
FROM
txs_base
LEFT JOIN model_name
ON base_block_number = model_block_number
AND base_tx_hash = model_tx_hash
WHERE
(
model_tx_hash IS NULL
OR model_block_number IS NULL
)
{% endmacro %}
{% macro gold_recent_missing_txs(
model
) %}
WITH txs_base AS (
SELECT
block_number AS base_block_number,
tx_hash AS base_tx_hash
FROM
{{ ref('test_gold__transactions_recent') }}
),
model_name AS (
SELECT
block_number AS model_block_number,
tx_hash AS model_tx_hash
FROM
{{ model }}
)
SELECT
base_block_number,
base_tx_hash,
model_block_number,
model_tx_hash
FROM
txs_base
LEFT JOIN model_name
ON base_block_number = model_block_number
AND base_tx_hash = model_tx_hash
WHERE
model_tx_hash IS NULL
OR model_block_number IS NULL
{% endmacro %}
{% macro missing_confirmed_txs(
model1,
model2
) %}
WITH txs_base AS (
SELECT
block_number AS base_block_number,
block_hash AS base_block_hash,
tx_hash AS base_tx_hash
FROM
{{ model1 }}
),
model_name AS (
SELECT
block_number AS model_block_number,
block_hash AS model_block_hash,
tx_hash AS model_tx_hash
FROM
{{ model2 }}
)
SELECT
DISTINCT base_block_number AS block_number
FROM
txs_base
LEFT JOIN model_name
ON base_block_number = model_block_number
AND base_tx_hash = model_tx_hash
AND base_block_hash = model_block_hash
WHERE
model_tx_hash IS NULL
AND model_block_number <= (
SELECT
MAX(base_block_number)
FROM
txs_base
)
{% endmacro %}
{% macro tx_gaps(
model
) %}
WITH block_base AS (
SELECT
block_number,
tx_count
FROM
{{ ref('test_gold__blocks_full') }}
),
model_name AS (
SELECT
block_number,
COUNT(
DISTINCT tx_hash
) AS model_tx_count
FROM
{{ model }}
GROUP BY
block_number
)
SELECT
block_base.block_number,
tx_count,
model_name.block_number AS model_block_number,
model_tx_count
FROM
block_base
LEFT JOIN model_name
ON block_base.block_number = model_name.block_number
WHERE
(
tx_count <> model_tx_count
)
OR (
model_tx_count IS NULL
AND tx_count <> 0
)
{% endmacro %}
{% macro recent_tx_gaps(
model
) %}
WITH block_base AS (
SELECT
block_number,
tx_count
FROM
{{ ref('test_gold__blocks_recent') }}
),
model_name AS (
SELECT
block_number,
COUNT(
DISTINCT tx_hash
) AS model_tx_count
FROM
{{ model }}
GROUP BY
block_number
)
SELECT
block_base.block_number,
tx_count,
model_name.block_number AS model_block_number,
model_tx_count
FROM
block_base
LEFT JOIN model_name
ON block_base.block_number = model_name.block_number
WHERE
(
tx_count <> model_tx_count
)
OR (
model_tx_count IS NULL
AND tx_count <> 0
)
{% endmacro %}

42
macros/utils.sql Normal file
View File

@ -0,0 +1,42 @@
{% macro if_data_call_wait() %}
{% if var(
"STREAMLINE_INVOKE_STREAMS"
) %}
{% set query %}
SELECT
1
WHERE
EXISTS(
SELECT
1
FROM
{{ model.schema ~ "." ~ model.alias }}
LIMIT
1
) {% endset %}
{% if execute %}
{% set results = run_query(
query
) %}
{% if results %}
{{ log(
"Waiting...",
info = True
) }}
{% set wait_query %}
SELECT
system$wait(
{{ var(
"WAIT",
600
) }}
) {% endset %}
{% do run_query(wait_query) %}
{% else %}
SELECT
NULL;
{% endif %}
{% endif %}
{% endif %}
{% endmacro %}

View File

@ -0,0 +1,126 @@
{% docs kaia_block_header_json %}
This JSON column contains the block header details.
{% enddocs %}
{% docs kaia_blockchain %}
The blockchain on which transactions are being confirmed.
{% enddocs %}
{% docs kaia_blocks_hash %}
The hash of the block header for a given block.
{% enddocs %}
{% docs kaia_blocks_nonce %}
Block nonce is a value used during mining to demonstrate proof of work for a given block.
{% enddocs %}
{% docs kaia_blocks_table_doc %}
This table contains block level data for the kaia Blockchain. This table can be used to analyze trends at a block level, for example gas fees vs. total transactions over time. For more information on EVM transactions, please see [Etherscan Resources](https://etherscan.io/directory/Learning_Resources/Ethereum) or [The Ethereum Organization](https://ethereum.org/en/developers/docs/blocks/)
{% enddocs %}
{% docs kaia_difficulty %}
The effort required to mine the block.
{% enddocs %}
{% docs kaia_extra_data %}
Any data included by the validator for a given block.
{% enddocs %}
{% docs kaia_gas_limit %}
Total gas limit provided by all transactions in the block.
{% enddocs %}
{% docs kaia_gas_used %}
Total gas used in the block.
{% enddocs %}
{% docs kaia_miner %}
Miner who successfully added a given block to the blockchain.
{% enddocs %}
{% docs kaia_network %}
The network on the blockchain used by a transaction.
{% enddocs %}
{% docs kaia_parent_hash %}
The hash of the block from which a given block is generated. Also known as the parent block.
{% enddocs %}
{% docs kaia_receipts_root %}
The root of the state trie.
{% enddocs %}
{% docs kaia_sha3_uncles %}
The mechanism which Ethereum Javascript RLP encodes an empty string.
{% enddocs %}
{% docs kaia_size %}
Block size, which is determined by a given block's gas limit.
{% enddocs %}
{% docs kaia_total_difficulty %}
Total difficulty of the chain at a given block.
{% enddocs %}
{% docs kaia_tx_count %}
Total number of transactions within a block.
{% enddocs %}
{% docs kaia_uncle_blocks %}
Uncle blocks occur when two blocks are mined and broadcasted at the same time, with the same block number. The block validated across the most nodes will be added to the primary chain, and the other one becomes an uncle block. Miners do receive rewards for uncle blocks.
{% enddocs %}

View File

@ -0,0 +1,96 @@
{% docs kaia_event_index %}
Event number within a transaction.
{% enddocs %}
{% docs kaia_event_inputs %}
The decoded event inputs for a given event.
{% enddocs %}
{% docs kaia_event_name %}
The decoded event name for a given event.
{% enddocs %}
{% docs kaia_event_removed %}
Whether the event has been removed from the transaction.
{% enddocs %}
{% docs kaia_origin_sig %}
The function signature of this transaction.
{% enddocs %}
{% docs kaia_log_id_events %}
This is the primary key for this table. This is a concatenation of the transaction hash and the event index at which the event occurred.
{% enddocs %}
{% docs kaia_logs_contract_address %}
The address interacted with for a given event.
{% enddocs %}
{% docs kaia_logs_contract_name %}
The name of the contract or token, where possible.
{% enddocs %}
{% docs kaia_logs_data %}
The un-decoded event data.
{% enddocs %}
{% docs kaia_logs_table_doc %}
This table contains flattened event logs from transactions on the kaia Blockchain. Transactions may have multiple events, which are denoted by the event index for a transaction hash. Therefore, this table is unique on the combination of transaction hash and event index.
{% enddocs %}
{% docs kaia_logs_tx_hash %}
Transaction hash is a unique 66-character identifier that is generated when a transaction is executed. This field will not be unique in this table, as a given transaction can include multiple events.
{% enddocs %}
{% docs kaia_origin_from %}
The from address of this transaction.
{% enddocs %}
{% docs kaia_origin_to %}
The to address of this transaction.
{% enddocs %}
{% docs kaia_topics %}
The un-decoded event input topics.
{% enddocs %}

View File

@ -0,0 +1,112 @@
{% docs kaia_traces_block_no %}
The block number of this transaction.
{% enddocs %}
{% docs kaia_traces_blocktime %}
The block timestamp of this transaction.
{% enddocs %}
{% docs kaia_traces_call_data %}
The raw JSON data for this trace.
{% enddocs %}
{% docs kaia_traces_from %}
The sending address of this trace. This is not necessarily the from address of the transaction.
{% enddocs %}
{% docs kaia_traces_gas %}
The gas supplied for this trace.
{% enddocs %}
{% docs kaia_traces_gas_used %}
The gas used for this trace.
{% enddocs %}
{% docs kaia_traces_identifier %}
This field represents the position and type of the trace within the transaction.
{% enddocs %}
{% docs kaia_trace_index %}
The index of the trace within the transaction.
{% enddocs %}
{% docs kaia_traces_input %}
The input data for this trace.
{% enddocs %}
{% docs kaia_traces_output %}
The output data for this trace.
{% enddocs %}
{% docs kaia_traces_sub %}
The amount of nested sub traces for this trace.
{% enddocs %}
{% docs kaia_traces_table_doc %}
This table contains flattened trace data for internal contract calls on the kaia Blockchain. Hex encoded fields can be decoded to integers by using `utils.udf_hex_to_int()`.
{% enddocs %}
{% docs kaia_traces_to %}
The receiving address of this trace. This is not necessarily the to address of the transaction.
{% enddocs %}
{% docs kaia_traces_tx_hash %}
The transaction hash for the trace. Please note, this is not necessarily unique in this table as transactions frequently have multiple traces.
{% enddocs %}
{% docs kaia_traces_type %}
The type of internal transaction. Common trace types are `CALL`, `DELEGATECALL`, and `STATICCALL`.
{% enddocs %}
{% docs kaia_traces_value %}
The amount of ETH transferred in this trace.
{% enddocs %}

View File

@ -0,0 +1,105 @@
{% docs kaia_cumulative_gas_used %}
The total amount of gas used when this transaction was executed in the block.
{% enddocs %}
{% docs kaia_tx_block_hash %}
Block hash is a unique 66-character identifier that is generate when a block is produced.
{% enddocs %}
{% docs kaia_tx_fee %}
Amount paid to validate the transaction in ETH.
{% enddocs %}
{% docs kaia_tx_gas_limit %}
Maximum amount of gas allocated for the transaction.
{% enddocs %}
{% docs kaia_tx_gas_price %}
Cost per unit of gas in Gwei.
{% enddocs %}
{% docs kaia_tx_gas_used %}
Gas used by transaction.
{% enddocs %}
{% docs kaia_tx_hash %}
Transaction hash is a unique 66-character identifier that is generated when a transaction is executed.
{% enddocs %}
{% docs kaia_tx_input_data %}
This column contains additional data for this transaction, and is commonly used as part of a contract interaction or as a message to the recipient.
{% enddocs %}
{% docs kaia_tx_json %}
This JSON column contains the transaction details, including event logs.
{% enddocs %}
{% docs kaia_tx_nonce %}
The number of transactions sent from a given address.
{% enddocs %}
{% docs kaia_tx_origin_sig %}
The function signature of the contract call.
{% enddocs %}
{% docs kaia_tx_position %}
The position of the transaction within the block.
{% enddocs %}
{% docs kaia_tx_status %}
Status of the transaction.
{% enddocs %}
{% docs kaia_tx_table_doc %}
This table contains transaction level data for the kaia Blockchain. Each transaction will have a unique transaction hash, along with transactions fees and a ETH value transferred when applicable. Transactions may be native ETH transfers or interactions with contract addresses. For more information, please see [The Ethereum Organization - Transactions](https://ethereum.org/en/developers/docs/transactions/)
{% enddocs %}
{% docs kaia_value %}
The value transacted in ETH.
{% enddocs %}

View File

@ -0,0 +1,141 @@
{% docs kaia_eth_amount %}
ETH value transferred.
{% enddocs %}
{% docs kaia_eth_amount_usd %}
ETH value transferred, in USD.
{% enddocs %}
{% docs kaia_ez_eth_transfers_table_doc %}
This table contains all native ETH transfers, including equivalent USD amounts. The origin addresses correspond to the to and from addresses from the `fact_transactions` table. The `identifier` and `tx_hash` columns relate this table back to `fact_traces`, which contains more details on the transfers.
{% enddocs %}
{% docs kaia_ez_transfers_table_doc %}
This table will contain all events in the ```fact_token_transfers table```, along with joined columns such as token price, symbol, and decimals where possible that allow for easier analysis of token transfer events. Please note Native ETH transfers are not included here.
{% enddocs %}
{% docs kaia_log_id_transfers %}
This is the primary key for this table. This is a concatenation of the transaction hash and the event index at which the transfer event occurred. This field can be used to find more details on the event within the ```fact_event_logs``` table.
{% enddocs %}
{% docs kaia_eth_origin_from %}
The from address at the transaction level.
{% enddocs %}
{% docs kaia_eth_origin_to %}
The to address at the transaction level.
{% enddocs %}
{% docs kaia_transfer_amount %}
The decimal transformed amount for this token. Tokens without a decimal adjustment will be nulled out here.
{% enddocs %}
{% docs kaia_transfer_amount_precise %}
The decimal transformed amount for this token returned as a string to preserve precision. Tokens without a decimal adjustment will be nulled out here.
{% enddocs %}
{% docs kaia_transfer_amount_usd %}
The amount in US dollars for this transfer at the time of the transfer. Tokens without a decimal adjustment or price will be nulled out here.
{% enddocs %}
{% docs kaia_transfer_contract_address %}
Contract address of the token being transferred.
{% enddocs %}
{% docs kaia_transfer_from_address %}
The sending address of this transfer.
{% enddocs %}
{% docs kaia_transfer_has_decimal %}
Whether or not our contracts model contains the necessary decimal adjustment for this token.
{% enddocs %}
{% docs kaia_transfer_has_price %}
Whether or not our prices model contains this hourly token price.
{% enddocs %}
{% docs kaia_transfer_raw_amount %}
The amount of tokens transferred. This value is not decimal adjusted.
{% enddocs %}
{% docs kaia_transfer_raw_amount_precise %}
The amount of tokens transferred returned as a string to preserve precision. This value is not decimal adjusted.
{% enddocs %}
{% docs kaia_transfer_table_doc %}
This table contains events on the kaia Blockchain with an event name of 'Transfer'. The contract address is the token transferred, and the raw amount field is the amount of tokens transferred. Please note this amount is not decimal adjusted. This table will not contain transfers of native ETH.
{% enddocs %}
{% docs kaia_transfer_to_address %}
The receiving address of this transfer. This can be a contract address.
{% enddocs %}
{% docs kaia_transfer_token_price %}
The price, if available, for this token at the transfer time.
{% enddocs %}
{% docs kaia_transfer_tx_hash %}
Transaction hash is a unique 66-character identifier that is generated when a transaction is executed. This will not be unique in this table as a transaction could include multiple transfer events.
{% enddocs %}

View File

@ -0,0 +1,76 @@
{% docs __overview__ %}
# Welcome to the Flipside Crypto kaia Models Documentation!
## **What does this documentation cover?**
The documentation included here details the design of the kaia tables and views available via [Flipside Crypto.](https://flipsidecrypto.xyz/) For more information on how these models are built, please see [the github repository.](https://github.com/FlipsideCrypto/kaia-models)
## **How do I use these docs?**
The easiest way to navigate this documentation is to use the Quick Links below. These links will take you to the documentation for each table, which contains a description, a list of the columns, and other helpful information.
If you are experienced with dbt docs, feel free to use the sidebar to navigate the documentation, as well as explore the relationships between tables and the logic building them.
There is more information on how to use dbt docs in the last section of this document.
## **Quick Links to Table Documentation**
**Click on the links below to jump to the documentation for each schema.**
### Core Tables (kaia.core)
**Dimension Tables:**
- [dim_labels](https://flipsidecrypto.github.io/kaia-models/#!/model/model.kaia_models.core__dim_labels)
**Fact Tables:**
- [fact_blocks](https://flipsidecrypto.github.io/kaia-models/#!/model/model.kaia_models.core__fact_blocks)
- [fact_event_logs](https://flipsidecrypto.github.io/kaia-models/#!/model/model.kaia_models.core__fact_event_logs)
- [fact_traces](https://flipsidecrypto.github.io/kaia-models/#!/model/model.kaia_models.core__fact_traces)
- [fact_transactions](https://flipsidecrypto.github.io/kaia-models/#!/model/model.kaia_models.core__fact_transactions)
## **Helpful User-Defined Functions (UDFs)**
UDFs are custom functions built by the Flipside team that can be used in your queries to make your life easier.
Please visit [LiveQuery Functions Overview](https://flipsidecrypto.github.io/livequery-models/#!/overview) for a full list of helpful UDFs.
## **Data Model Overview**
The kaia models are built a few different ways, but the core fact tables are built using three layers of sql models: **bronze, silver, and gold (or core).**
- Bronze: Data is loaded in from the source as a view
- Silver: All necessary parsing, filtering, de-duping, and other transformations are done here
- Gold (or core): Final views and tables that are available publicly
The dimension tables are sourced from a variety of on-chain and off-chain sources.
Convenience views (denoted ez_) are a combination of different fact and dimension tables. These views are built to make it easier to query the data.
## **Using dbt docs**
### Navigation
You can use the ```Project``` and ```Database``` navigation tabs on the left side of the window to explore the models in the project.
### Database Tab
This view shows relations (tables and views) grouped into database schemas. Note that ephemeral models are *not* shown in this interface, as they do not exist in the database.
### Graph Exploration
You can click the blue icon on the bottom-right corner of the page to view the lineage graph of your models.
On model pages, you'll see the immediate parents and children of the model you're exploring. By clicking the Expand button at the top-right of this lineage pane, you'll be able to see all of the models that are used to build, or are built from, the model you're exploring.
Once expanded, you'll be able to use the ```--models``` and ```--exclude``` model selection syntax to filter the models in the graph. For more information on model selection, check out the [dbt docs](https://docs.getdbt.com/docs/model-selection-syntax).
Note that you can also right-click on models to interactively filter and explore the graph.
### **More information**
- [Flipside](https://flipsidecrypto.xyz/)
- [Velocity](https://app.flipsidecrypto.com/velocity?nav=Discover)
- [Tutorials](https://docs.flipsidecrypto.com/our-data/tutorials)
- [Github](https://github.com/FlipsideCrypto/kaia-models)
- [What is dbt?](https://docs.getdbt.com/docs/introduction)
{% enddocs %}

View File

@ -0,0 +1,11 @@
{% docs internal_column %}
Deprecated. This column is no longer used. Please remove from your query by Jan. 31 2024.
{% enddocs %}
{% docs amount_deprecation %}
This column is being deprecated for standardization purposes on Jan. 31 2024. Please use the equivalent column without the native asset prefix. For example, use `amount` instead of `eth_amount`.
{% enddocs %}

View File

@ -0,0 +1,19 @@
{% docs pk %}
The unique identifier for each row in the table.
{% enddocs %}
{% docs inserted_timestamp %}
The utc timestamp at which the row was inserted into the table.
{% enddocs %}
{% docs modified_timestamp %}
The utc timestamp at which the row was last modified.
{% enddocs %}

View File

@ -0,0 +1,5 @@
{% docs kaia_block_number %}
Also known as block height. The block number, which indicates the length of the blockchain, increases after the addition of each new block.
{% enddocs %}

View File

@ -0,0 +1,5 @@
{% docs kaia_block_timestamp %}
The date and time at which the block was produced.
{% enddocs %}

View File

@ -0,0 +1,5 @@
{% docs kaia_decimals %}
The number of decimal places this contract needs adjusted where token values exist. For example, use the decimal field to correctly transform raw amounts in ```fact_token_transfers```.
{% enddocs %}

View File

@ -0,0 +1,5 @@
{% docs kaia_from_address %}
The sending address of this transaction.
{% enddocs %}

View File

@ -0,0 +1,5 @@
{% docs kaia_ingested_at %}
Internal column.
{% enddocs %}

View File

@ -0,0 +1,5 @@
{% docs kaia_symbol %}
The symbol belonging to the address of the token
{% enddocs %}

View File

@ -0,0 +1,5 @@
{% docs kaia_to_address %}
The receiving address of this transaction. This can be a contract address.
{% enddocs %}

View File

@ -0,0 +1,17 @@
{% docs precise_amount_unadjusted %}
The precise, unadjusted amount of the transaction. This is returned as a string to avoid precision loss.
{% enddocs %}
{% docs precise_amount_adjusted %}
The precise, adjusted amount of the transaction. This is returned as a string to avoid precision loss.
{% enddocs %}
{% docs tx_fee_precise %}
The precise amount of the transaction fee. This is returned as a string to avoid precision loss.
{% enddocs %}

View File

@ -0,0 +1,5 @@
{% docs kaia_project_name %}
The name of the project for this address.
{% enddocs %}

View File

@ -0,0 +1,5 @@
{% docs kaia_label_address %}
Address that the label is for. This is the field that should be used to join other tables with labels.
{% enddocs %}

View File

@ -0,0 +1,5 @@
{% docs kaia_label_address_name %}
The most granular label for this address.
{% enddocs %}

View File

@ -0,0 +1,5 @@
{% docs kaia_label_blockchain %}
The name of the blockchain.
{% enddocs %}

View File

@ -0,0 +1,5 @@
{% docs kaia_label_creator %}
The name of the creator of the label.
{% enddocs %}

View File

@ -0,0 +1,5 @@
{% docs kaia_label_subtype %}
A sub-category nested within label type providing further detail.
{% enddocs %}

View File

@ -0,0 +1,5 @@
{% docs kaia_label_type %}
A high-level category describing the addresses main function or ownership.
{% enddocs %}

View File

@ -0,0 +1,5 @@
{% docs kaia_labels_table %}
This table contains labels for addresses on the Base Blockchain.
{% enddocs %}

View File

@ -0,0 +1,5 @@
{% docs table_dim_labels %}
The labels table is a store of one-to-one address identifiers, or an address name. Labels are broken out into a "type" (such as cex, dex, dapp, games, etc.) and a "subtype" (ex: contract_deployer, hot_wallet, token_contract, etc.) in order to help classify each address name into similar groups. Our labels are sourced from many different places, but can primarily be grouped into two categories: automatic and manual. Automatic labels are continuously labeled based on certain criteria, such as a known contract deploying another contract, behavior based algorithms for finding deposit wallets, and consistent data pulls of custom protocol APIs. Manual labels are done periodically to find addresses that cannot be found programmatically such as finding new protocol addresses, centralized exchange hot wallets, or trending addresses. Labels can also be added by our community by using our add-a-label tool (https://science.flipsidecrypto.xyz/add-a-label/) or on-chain with near (https://near.social/lord1.near/widget/Form) and are reviewed by our labels team. A label can be removed by our labels team if it is found to be incorrect or no longer relevant; this generally will only happen for mislabeled deposit wallets.
{% enddocs %}

View File

@ -0,0 +1,36 @@
{{ config(
materialized = 'incremental',
unique_key = ['address', 'blockchain'],
incremental_strategy = 'merge',
cluster_by = 'modified_timestamp::DATE',
tags = ['non_realtime']
) }}
SELECT
blockchain,
creator,
address,
address_name,
label_type,
label_subtype,
project_name,
labels_combined_id AS dim_labels_id,
inserted_timestamp,
modified_timestamp
FROM
{{ source(
'klaytn_silver',
'labels'
) }}
{% if is_incremental() %}
WHERE
modified_timestamp >= (
SELECT
MAX(
modified_timestamp
)
FROM
{{ this }}
)
{% endif %}

View File

@ -0,0 +1,59 @@
version: 2
models:
- name: core__dim_labels
description: '{{ doc("table_dim_labels") }}'
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- BLOCKCHAIN
- CREATOR
- ADDRESS
columns:
- name: BLOCKCHAIN
description: '{{ doc("kaia_label_blockchain") }}'
tests:
- not_null
- name: CREATOR
description: '{{ doc("kaia_label_creator") }}'
tests:
- not_null
- name: ADDRESS
description: '{{ doc("kaia_label_address") }}'
tests:
- not_null
- dbt_expectations.expect_column_values_to_match_regex:
regex: 0[xX][0-9a-fA-F]+
- name: ADDRESS_NAME
description: '{{ doc("kaia_labels_table") }}'
tests:
- dbt_expectations.expect_column_values_to_be_in_type_list:
column_type_list:
- STRING
- VARCHAR
- name: LABEL_TYPE
description: '{{ doc("kaia_label_type") }}'
tests:
- not_null
- name: LABEL_SUBTYPE
description: '{{ doc("kaia_label_subtype") }}'
tests:
- not_null
- dbt_expectations.expect_column_values_to_be_in_type_list:
column_type_list:
- STRING
- VARCHAR
- name: PROJECT_NAME
description: '{{ doc("kaia_project_name") }}'
tests:
- not_null
- dbt_expectations.expect_column_values_to_be_in_type_list:
column_type_list:
- STRING
- VARCHAR
- name: DIM_LABELS_ID
description: '{{ doc("pk") }}'
- name: INSERTED_TIMESTAMP
description: '{{ doc("inserted_timestamp") }}'
- name: MODIFIED_TIMESTAMP
description: '{{ doc("modified_timestamp") }}'

View File

@ -0,0 +1,90 @@
{{ config(
materialized = 'incremental',
unique_key = "block_number",
incremental_strategy = 'delete+insert',
merge_exclude_columns = ["inserted_timestamp"],
cluster_by = "block_timestamp::date",
tags = ['non_realtime']
) }}
SELECT
A.block_number AS block_number,
block_timestamp,
'mainnet' AS network,
'kaia' AS blockchain,
tx_count,
difficulty,
total_difficulty,
extra_data,
gas_limit,
gas_used,
HASH,
parent_hash,
receipts_root,
sha3_uncles,
SIZE,
uncles AS uncle_blocks,
OBJECT_CONSTRUCT(
'baseFeePerGas',
base_fee_per_gas,
'difficulty',
difficulty,
'extraData',
extra_data,
'gasLimit',
gas_limit,
'gasUsed',
gas_used,
'hash',
HASH,
'logsBloom',
logs_bloom,
'miner',
miner,
'nonce',
nonce,
'number',
NUMBER,
'parentHash',
parent_hash,
'receiptsRoot',
receipts_root,
'sha3Uncles',
sha3_uncles,
'size',
SIZE,
'stateRoot',
state_root,
'timestamp',
block_timestamp,
'totalDifficulty',
total_difficulty,
'transactionsRoot',
transactions_root,
'uncles',
uncles
) AS block_header_json,
blocks_id AS fact_blocks_id,
inserted_timestamp,
modified_timestamp,
FROM
{{ source(
'klaytn_silver',
'blocks'
) }} A
{% if is_incremental() %}
WHERE
A.modified_timestamp > (
SELECT
MAX(
modified_timestamp
)
FROM
{{ this }}
)
{% endif %}
qualify(ROW_NUMBER() over (PARTITION BY A.block_number
ORDER BY
A.modified_timestamp DESC)) = 1

View File

@ -0,0 +1,46 @@
version: 2
models:
- name: core__fact_blocks
description: '{{ doc("kaia_blocks_table_doc") }}'
columns:
- name: BLOCK_NUMBER
description: '{{ doc("kaia_block_number") }}'
- name: BLOCK_TIMESTAMP
description: '{{ doc("kaia_block_timestamp") }}'
- name: NETWORK
description: '{{ doc("kaia_network") }}'
- name: BLOCKCHAIN
description: '{{ doc("kaia_blockchain") }}'
- name: TX_COUNT
description: '{{ doc("kaia_tx_count") }}'
- name: DIFFICULTY
description: '{{ doc("kaia_difficulty") }}'
- name: TOTAL_DIFFICULTY
description: '{{ doc("kaia_total_difficulty") }}'
- name: EXTRA_DATA
description: '{{ doc("kaia_extra_data") }}'
- name: GAS_LIMIT
description: '{{ doc("kaia_gas_limit") }}'
- name: GAS_USED
description: '{{ doc("kaia_gas_used") }}'
- name: HASH
description: '{{ doc("kaia_blocks_hash") }}'
- name: PARENT_HASH
description: '{{ doc("kaia_parent_hash") }}'
- name: RECEIPTS_ROOT
description: '{{ doc("kaia_receipts_root") }}'
- name: SHA3_UNCLES
description: '{{ doc("kaia_sha3_uncles") }}'
- name: SIZE
description: '{{ doc("kaia_size") }}'
- name: UNCLE_BLOCKS
description: '{{ doc("kaia_uncle_blocks") }}'
- name: BLOCK_HEADER_JSON
description: '{{ doc("kaia_block_header_json") }}'
- name: FACT_BLOCKS_ID
description: '{{ doc("pk") }}'
- name: INSERTED_TIMESTAMP
description: '{{ doc("inserted_timestamp") }}'
- name: MODIFIED_TIMESTAMP
description: '{{ doc("modified_timestamp") }}'

View File

@ -0,0 +1,41 @@
{{ config(
materialized = 'incremental',
incremental_strategy = 'delete+insert',
unique_key = "block_number",
cluster_by = "block_timestamp::date",
post_hook = "ALTER TABLE {{ this }} ADD SEARCH OPTIMIZATION",
tags = ['non_realtime']
) }}
SELECT
block_number,
block_timestamp,
tx_hash,
origin_function_signature,
origin_from_address,
origin_to_address,
event_index,
contract_address,
topics,
DATA,
event_removed,
tx_status AS tx_succeeded,
_log_id,
logs_id AS fact_event_logs_id,
inserted_timestamp,
modified_timestamp
FROM
{{ source(
'klaytn_silver',
'logs'
) }}
{% if is_incremental() %}
WHERE
modified_timestamp > (
SELECT
MAX(modified_timestamp) _INSERTED_TIMESTAMP
FROM
{{ this }}
)
{% endif %}

View File

@ -0,0 +1,38 @@
version: 2
models:
- name: core__fact_event_logs
description: '{{ doc("kaia_logs_table_doc") }}'
columns:
- name: BLOCK_NUMBER
description: '{{ doc("kaia_block_number") }}'
- name: BLOCK_TIMESTAMP
description: '{{ doc("kaia_block_timestamp") }}'
- name: TX_HASH
description: '{{ doc("kaia_logs_tx_hash") }}'
- name: EVENT_INDEX
description: '{{ doc("kaia_event_index") }}'
- name: CONTRACT_ADDRESS
description: '{{ doc("kaia_logs_contract_address") }}'
- name: TOPICS
description: '{{ doc("kaia_topics") }}'
- name: DATA
description: '{{ doc("kaia_logs_data") }}'
- name: EVENT_REMOVED
description: '{{ doc("kaia_event_removed") }}'
- name: _LOG_ID
description: '{{ doc("internal_column") }}'
- name: TX_SUCCEEDED
description: '{{ doc("kaia_tx_status") }}'
- name: ORIGIN_FUNCTION_SIGNATURE
description: '{{ doc("kaia_origin_sig") }}'
- name: ORIGIN_FROM_ADDRESS
description: '{{ doc("kaia_origin_from") }}'
- name: ORIGIN_TO_ADDRESS
description: '{{ doc("kaia_origin_to") }}'
- name: FACT_EVENT_LOGS_ID
description: '{{ doc("pk") }}'
- name: INSERTED_TIMESTAMP
description: '{{ doc("inserted_timestamp") }}'
- name: MODIFIED_TIMESTAMP
description: '{{ doc("modified_timestamp") }}'

View File

@ -0,0 +1,53 @@
{{ config (
materialized = "incremental",
incremental_strategy = 'delete+insert',
unique_key = "block_number",
cluster_by = "block_timestamp::date",
post_hook = "ALTER TABLE {{ this }} ADD SEARCH OPTIMIZATION",
tags = ['non_realtime'],
full_refresh = false
) }}
SELECT
block_number,
tx_hash,
block_timestamp,
tx_status AS tx_succeeded,
tx_position,
trace_index,
from_address,
to_address,
value_precise_raw,
value_precise,
VALUE,
gas,
gas_used,
input,
output,
TYPE,
identifier,
sub_traces,
error_reason,
trace_status AS trace_succeeded,
DATA,
is_pending,
_call_id,
_inserted_timestamp,
traces_id AS fact_traces_id,
inserted_timestamp,
modified_timestamp
FROM
{{ source(
'klaytn_silver',
'traces'
) }}
{% if is_incremental() %}
WHERE
modified_timestamp > (
SELECT
MAX(modified_timestamp)
FROM
{{ this }}
)
{% endif %}

View File

@ -0,0 +1,56 @@
version: 2
models:
- name: core__fact_traces
description: '{{ doc("kaia_traces_table_doc") }}'
columns:
- name: BLOCK_NUMBER
description: '{{ doc("kaia_traces_block_no") }}'
- name: BLOCK_TIMESTAMP
description: '{{ doc("kaia_traces_blocktime") }}'
- name: TX_HASH
description: '{{ doc("kaia_traces_tx_hash") }}'
- name: FROM_ADDRESS
description: '{{ doc("kaia_traces_from") }}'
- name: TO_ADDRESS
description: '{{ doc("kaia_traces_to") }}'
- name: VALUE
description: '{{ doc("kaia_traces_value") }}'
- name: VALUE_PRECISE_RAW
description: '{{ doc("precise_amount_unadjusted") }}'
- name: VALUE_PRECISE
description: '{{ doc("precise_amount_adjusted") }}'
- name: GAS
description: '{{ doc("kaia_traces_gas") }}'
- name: GAS_USED
description: '{{ doc("kaia_traces_gas_used") }}'
- name: INPUT
description: '{{ doc("kaia_traces_input") }}'
- name: OUTPUT
description: '{{ doc("kaia_traces_output") }}'
- name: TYPE
description: '{{ doc("kaia_traces_type") }}'
- name: IDENTIFIER
description: '{{ doc("kaia_traces_identifier") }}'
- name: DATA
description: '{{ doc("kaia_traces_call_data") }}'
- name: TX_SUCCEEDED
description: '{{ doc("kaia_tx_status") }}'
- name: SUB_TRACES
description: '{{ doc("kaia_traces_sub") }}'
- name: TRACE_SUCCEEDED
description: The status of the trace, either `SUCCESS` or `FAIL`
- name: ERROR_REASON
description: The reason for the trace failure, if any.
- name: TRACE_INDEX
description: The index of the trace within the transaction.
- name: FACT_TRACES_ID
description: '{{ doc("pk") }}'
- name: INSERTED_TIMESTAMP
description: '{{ doc("inserted_timestamp") }}'
- name: MODIFIED_TIMESTAMP
description: '{{ doc("modified_timestamp") }}'

View File

@ -0,0 +1,53 @@
{{ config(
materialized = 'incremental',
unique_key = "block_number",
incremental_strategy = 'delete+insert',
cluster_by = "block_timestamp::date",
tags = ['non_realtime']
) }}
SELECT
block_number,
block_timestamp,
block_hash,
tx_hash,
nonce,
POSITION,
origin_function_signature,
from_address,
to_address,
VALUE,
value_precise_raw,
value_precise,
tx_fee,
tx_fee_precise,
gas_price,
effective_gas_price,
gas AS gas_limit,
gas_used,
cumulative_gas_used,
max_fee_per_gas,
max_priority_fee_per_gas,
input_data,
tx_status AS tx_succeeded,
r,
s,
v,
transactions_id AS fact_transactions_id,
inserted_timestamp,
modified_timestamp
FROM
{{ source(
'klaytn_silver',
'transactions'
) }}
{% if is_incremental() %}
WHERE
modified_timestamp > (
SELECT
MAX(modified_timestamp)
FROM
{{ this }}
)
{% endif %}

View File

@ -0,0 +1,64 @@
version: 2
models:
- name: core__fact_transactions
description: '{{ doc("kaia_tx_table_doc") }}'
columns:
- name: BLOCK_NUMBER
description: '{{ doc("kaia_block_number") }}'
- name: BLOCK_TIMESTAMP
description: '{{ doc("kaia_block_timestamp") }}'
- name: BLOCK_HASH
description: '{{ doc("kaia_tx_block_hash") }}'
- name: TX_HASH
description: '{{ doc("kaia_tx_hash") }}'
- name: NONCE
description: '{{ doc("kaia_tx_nonce") }}'
- name: POSITION
description: '{{ doc("kaia_tx_position") }}'
- name: FROM_ADDRESS
description: '{{ doc("kaia_from_address") }}'
- name: TO_ADDRESS
description: '{{ doc("kaia_to_address") }}'
- name: VALUE
description: '{{ doc("kaia_value") }}'
- name: VALUE_PRECISE_RAW
description: '{{ doc("precise_amount_unadjusted") }}'
- name: VALUE_PRECISE
description: '{{ doc("precise_amount_adjusted") }}'
- name: TX_FEE
description: '{{ doc("kaia_tx_fee") }}'
- name: TX_FEE_PRECISE
description: '{{ doc("tx_fee_precise") }}'
- name: GAS_PRICE
description: '{{ doc("kaia_tx_gas_price") }}'
- name: EFFECTIVE_GAS_PRICE
description: The total base charge plus tip paid for each unit of gas, in Gwei.
- name: GAS_LIMIT
description: '{{ doc("kaia_tx_gas_limit") }}'
- name: GAS_USED
description: '{{ doc("kaia_tx_gas_used") }}'
- name: CUMULATIVE_GAS_USED
description: '{{ doc("kaia_cumulative_gas_used") }}'
- name: MAX_FEE_PER_GAS
description: The maximum fee per gas of the transaction, in Gwei.
- name: MAX_PRIORITY_FEE_PER_GAS
description: The maximum priority fee per gas of the transaction, in Gwei.
- name: SUCCEEDED
description: '{{ doc("kaia_tx_status") }}'
- name: INPUT_DATA
description: '{{ doc("kaia_tx_input_data") }}'
- name: ORIGIN_FUNCTION_SIGNATURE
description: '{{ doc("kaia_tx_origin_sig") }}'
- name: R
description: The r value of the transaction signature.
- name: S
description: The s value of the transaction signature.
- name: V
description: The v value of the transaction signature.
- name: FACT_TRANSACTIONS_ID
description: '{{ doc("pk") }}'
- name: INSERTED_TIMESTAMP
description: '{{ doc("inserted_timestamp") }}'
- name: MODIFIED_TIMESTAMP
description: '{{ doc("modified_timestamp") }}'

View File

@ -0,0 +1,9 @@
{{ config (
materialized = 'view',
tags = ['full_test']
) }}
SELECT
*
FROM
{{ ref('core__fact_blocks') }}

View File

@ -0,0 +1,10 @@
version: 2
models:
- name: test_gold__blocks_full
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- BLOCK_NUMBER
- fsc_utils.sequence_gaps:
column_name: BLOCK_NUMBER
where: BLOCK_TIMESTAMP < CURRENT_DATE - 1

View File

@ -0,0 +1,24 @@
{{ config (
materialized = 'view',
tags = ['recent_test']
) }}
WITH last_3_days AS (
SELECT
block_number
FROM
{{ ref("_block_lookback") }}
)
SELECT
*
FROM
{{ ref('core__fact_blocks') }}
WHERE
block_number >= (
SELECT
block_number
FROM
last_3_days
)

View File

@ -0,0 +1,19 @@
version: 2
models:
- name: test_gold__blocks_recent
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- BLOCK_NUMBER
- fsc_utils.sequence_gaps:
column_name: BLOCK_NUMBER
config:
severity: error
error_if: ">10"
columns:
- name: BLOCK_TIMESTAMP
tests:
- dbt_expectations.expect_row_values_to_have_recent_data:
datepart: hour
interval: 3

View File

@ -0,0 +1,9 @@
{{ config (
materialized = 'view',
tags = ['full_test']
) }}
SELECT
*
FROM
{{ ref('core__fact_event_logs') }}

View File

@ -0,0 +1,19 @@
version: 2
models:
- name: test_gold__event_logs_full
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- _LOG_ID
- fsc_utils.sequence_gaps:
partition_by:
- BLOCK_NUMBER
- TX_HASH
column_name: EVENT_INDEX
where: BLOCK_TIMESTAMP < CURRENT_DATE - 1
columns:
- name: BLOCK_TIMESTAMP
tests:
- dbt_expectations.expect_row_values_to_have_recent_data:
datepart: day
interval: 1

View File

@ -0,0 +1,23 @@
{{ config (
materialized = 'view',
tags = ['recent_test']
) }}
WITH last_3_days AS (
SELECT
block_number
FROM
{{ ref("_block_lookback") }}
)
SELECT
*
FROM
{{ ref('core__fact_event_logs') }}
WHERE
block_number >= (
SELECT
block_number
FROM
last_3_days
)

View File

@ -0,0 +1,18 @@
version: 2
models:
- name: test_gold__event_logs_recent
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- _LOG_ID
- fsc_utils.sequence_gaps:
partition_by:
- BLOCK_NUMBER
- TX_HASH
column_name: EVENT_INDEX
columns:
- name: BLOCK_TIMESTAMP
tests:
- dbt_expectations.expect_row_values_to_have_recent_data:
datepart: hour
interval: 3

View File

@ -0,0 +1,13 @@
{{ config (
materialized = 'view',
tags = ['full_test']
) }}
SELECT
*
FROM
{{ ref('core__fact_traces') }}
WHERE
from_address <> '0x'
AND
to_address <> '0x'

View File

@ -0,0 +1,17 @@
version: 2
models:
- name: test_gold__traces_full
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- BLOCK_NUMBER
- TX_POSITION
- TRACE_INDEX
columns:
- name: BLOCK_TIMESTAMP
tests:
- dbt_expectations.expect_row_values_to_have_recent_data:
datepart: day
interval: 1

View File

@ -0,0 +1,27 @@
{{ config (
materialized = 'view',
tags = ['recent_test']
) }}
WITH last_3_days AS (
SELECT
block_number
FROM
{{ ref("_block_lookback") }}
)
SELECT
*
FROM
{{ ref('core__fact_traces') }}
WHERE
block_number >= (
SELECT
block_number
FROM
last_3_days
)
AND
from_address <> '0x'
AND
to_address <> '0x'

View File

@ -0,0 +1,16 @@
version: 2
models:
- name: test_gold__traces_recent
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- BLOCK_NUMBER
- TX_POSITION
- TRACE_INDEX
columns:
- name: BLOCK_TIMESTAMP
tests:
- dbt_expectations.expect_row_values_to_have_recent_data:
datepart: day
interval: 1

View File

@ -0,0 +1,9 @@
{{ config (
materialized = 'view',
tags = ['full_test']
) }}
SELECT
*
FROM
{{ ref('core__fact_transactions') }}

View File

@ -0,0 +1,19 @@
version: 2
models:
- name: test_gold__transactions_full
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- TX_HASH
- fsc_utils.sequence_gaps:
partition_by:
- BLOCK_NUMBER
column_name: POSITION
where: BLOCK_TIMESTAMP < CURRENT_DATE - 1
columns:
- name: BLOCK_TIMESTAMP
tests:
- dbt_expectations.expect_row_values_to_have_recent_data:
datepart: day
interval: 1

View File

@ -0,0 +1,23 @@
{{ config (
materialized = 'view',
tags = ['recent_test']
) }}
WITH last_3_days AS (
SELECT
block_number
FROM
{{ ref("_block_lookback") }}
)
SELECT
*
FROM
{{ ref('core__fact_transactions') }}
WHERE
block_number >= (
SELECT
block_number
FROM
last_3_days
)

View File

@ -0,0 +1,18 @@
version: 2
models:
- name: test_gold__transactions_recent
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- TX_HASH
- fsc_utils.sequence_gaps:
partition_by:
- BLOCK_NUMBER
column_name: POSITION
columns:
- name: BLOCK_TIMESTAMP
tests:
- dbt_expectations.expect_row_values_to_have_recent_data:
datepart: hour
interval: 3

View File

@ -0,0 +1,14 @@
{{ config (
materialized = "ephemeral"
) }}
SELECT
MIN(block_number) AS block_number
FROM
{{ source(
'klaytn_silver',
'blocks'
) }}
WHERE
block_timestamp >= DATEADD('hour', -72, TRUNCATE(SYSDATE(), 'HOUR'))
AND block_timestamp < DATEADD('hour', -71, TRUNCATE(SYSDATE(), 'HOUR'))

13
models/sources.yml Normal file
View File

@ -0,0 +1,13 @@
version: 2
sources:
- name: klaytn_silver
database: klaytn
schema: silver
tables:
- name: labels
- name: blocks
- name: logs
- name: receipts
- name: traces
- name: transactions

16
package-lock.yml Normal file
View File

@ -0,0 +1,16 @@
packages:
- package: calogica/dbt_expectations
version: 0.8.2
- package: dbt-labs/dbt_external_tables
version: 0.8.2
- package: dbt-labs/dbt_utils
version: 1.0.0
- git: https://github.com/FlipsideCrypto/fsc-utils.git
revision: 484e9db07d2060286768bb745e1b0e879178d43b
- package: get-select/dbt_snowflake_query_tags
version: 2.5.0
- package: calogica/dbt_date
version: 0.7.2
- git: https://github.com/FlipsideCrypto/livequery-models.git
revision: b024188be4e9c6bc00ed77797ebdc92d351d620e
sha1_hash: 5b45e0f95979d82d85fd603d44f5bf35be9a7064

11
packages.yml Normal file
View File

@ -0,0 +1,11 @@
packages:
- package: calogica/dbt_expectations
version: 0.8.2
- package: dbt-labs/dbt_external_tables
version: 0.8.2
- package: dbt-labs/dbt_utils
version: 1.0.0
- git: https://github.com/FlipsideCrypto/fsc-utils.git
revision: v1.23.0
- package: get-select/dbt_snowflake_query_tags
version: [">=2.0.0", "<3.0.0"]

31
profiles.yml Normal file
View File

@ -0,0 +1,31 @@
kaia:
target: prod
outputs:
dev:
type: snowflake
account: "{{ env_var('ACCOUNT') }}"
role: "{{ env_var('ROLE') }}"
user: "{{ env_var('USER') }}"
password: "{{ env_var('PASSWORD') }}"
region: "{{ env_var('REGION') }}"
database: "{{ env_var('DATABASE') }}"
warehouse: "{{ env_var('WAREHOUSE') }}"
schema: SILVER
threads: 4
client_session_keep_alive: False
query_tag: kaia_curator
prod:
type: snowflake
account: "{{ env_var('ACCOUNT') }}"
role: "{{ env_var('ROLE') }}"
user: "{{ env_var('USER') }}"
password: "{{ env_var('PASSWORD') }}"
region: "{{ env_var('REGION') }}"
database: "{{ env_var('DATABASE') }}"
warehouse: "{{ env_var('WAREHOUSE') }}"
schema: SILVER
threads: 4
client_session_keep_alive: False
query_tag: kaia_curator
config:
send_anonymous_usage_stats: False

2
requirements.txt Normal file
View File

@ -0,0 +1,2 @@
dbt-snowflake>=1.7,<1.8
protobuf==4.25.3

0
snapshots/.gitkeep Normal file
View File

View File

@ -0,0 +1,2 @@
-- depends_on: {{ ref('test_gold__transactions_full') }}
{{ gold_missing_txs(ref("test_gold__traces_full")) }}

View File

@ -0,0 +1,2 @@
-- depends_on: {{ ref('test_gold__blocks_full') }}
{{ tx_gaps(ref("test_gold__transactions_full")) }}

View File

@ -0,0 +1,2 @@
-- depends_on: {{ ref('test_gold__transactions_recent') }}
{{ gold_recent_missing_txs(ref("test_gold__traces_recent")) }}

View File

@ -0,0 +1,2 @@
-- depends_on: {{ ref('test_gold__blocks_recent') }}
{{ recent_tx_gaps(ref("test_gold__transactions_recent")) }}