diff --git a/dbt_project.yml b/dbt_project.yml index 4654188d..b760b1f4 100644 --- a/dbt_project.yml +++ b/dbt_project.yml @@ -43,6 +43,3 @@ models: vars: "dbt_date:time_zone": America/Los_Angeles - -tests: - +severity: warn # all tests diff --git a/macros/tests/sequential_gaps.sql b/macros/tests/sequential_gaps.sql index e9f2dc17..650b335f 100644 --- a/macros/tests/sequential_gaps.sql +++ b/macros/tests/sequential_gaps.sql @@ -21,7 +21,7 @@ {{ table }} ) SELECT - {{ partition_sql }}, + {{ partition_sql + "," if partition_sql }} {{ previous_column }}, {{ column }}, {{ column }} - {{ previous_column }} diff --git a/models/terra/anchor__bonds.sql b/models/terra/anchor__bonds.sql new file mode 100644 index 00000000..c2e15ee2 --- /dev/null +++ b/models/terra/anchor__bonds.sql @@ -0,0 +1,105 @@ +{{ config( + materialized = 'incremental', + unique_key = 'block_id || tx_id', + incremental_strategy = 'delete+insert', + cluster_by = ['block_timestamp', 'block_id'], + tags=['snowflake', 'terra', 'anchor', 'bonds'] +) }} + +WITH prices AS ( + + SELECT + date_trunc('hour', block_timestamp) as hour, + currency, + symbol, + avg(price_usd) as price + FROM {{ ref('terra__oracle_prices')}} + + WHERE 1=1 + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} + + GROUP BY 1,2,3 + +), + +msgs AS ( + +SELECT + m.blockchain, + chain_id, + block_id, + block_timestamp, + tx_id, + msg_value:sender::string AS sender, + msg_value:coins[0]:amount / POW(10,6) AS bonded_amount, + bonded_amount * price AS bonded_amount_usd, + msg_value:coins[0]:denom::string as bonded_currency, + msg_value:execute_msg:bond:validator::string AS validator, + msg_value:contract::string AS contract_address, + l.address_name AS contract_label +FROM {{ref('silver_terra__msgs')}} m + +LEFT OUTER JOIN {{source('shared','udm_address_labels_new')}} as l +ON msg_value:contract::string = l.address + +LEFT OUTER JOIN prices o + ON date_trunc('hour', block_timestamp) = o.hour + AND msg_value:coins[0]:denom::string = o.currency + +WHERE msg_value:execute_msg:bond IS NOT NULL + AND tx_status = 'SUCCEEDED' + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} + +), + +events AS ( + +SELECT + tx_id, + event_attributes:minted / POW(10,6) AS minted_amount, + minted_amount * price AS minted_amount_usd, + event_attributes:"1_contract_address"::string as minted_currency +FROM {{ref('silver_terra__msg_events')}} + +LEFT OUTER JOIN prices o + ON date_trunc('hour', block_timestamp) = o.hour + AND event_attributes:"1_contract_address"::string = o.currency + +WHERE tx_id IN(SELECT tx_id FROM msgs) + AND event_type = 'from_contract' + AND tx_status = 'SUCCEEDED' + AND minted_currency IS NOT NULL + AND minted_amount IS NOT NULL + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} + +) + +SELECT + blockchain, + chain_id, + block_id, + block_timestamp, + m.tx_id, + sender, + bonded_amount, + bonded_amount_usd, + bonded_currency, + validator, + minted_amount, + minted_amount_usd, + minted_currency, + contract_address, + contract_label +FROM msgs m + +JOIN events e + ON m.tx_id = e.tx_id \ No newline at end of file diff --git a/models/terra/anchor__bonds.yml b/models/terra/anchor__bonds.yml new file mode 100644 index 00000000..6b1cbae0 --- /dev/null +++ b/models/terra/anchor__bonds.yml @@ -0,0 +1,49 @@ +version: 2 +models: + - name: anchor__bonds + columns: + - name: BLOCKCHAIN + tests: + - not_null + - name: CHAIN_ID + tests: + - not_null + - name: BLOCK_ID + tests: + - not_null + - name: BLOCK_TIMESTAMP + tests: + - not_null + - name: TX_ID + tests: + - not_null + - name: BONDED_AMOUNT + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: BONDED_AMOUNT_USD + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: BONDED_CURRENCY + tests: + - not_null + - name: MINTED_AMOUNT + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: MINTED_AMOUNT_USD + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: MINTED_CURRENCY + tests: + - not_null \ No newline at end of file diff --git a/models/terra/anchor__borrows.sql b/models/terra/anchor__borrows.sql new file mode 100644 index 00000000..595c72c3 --- /dev/null +++ b/models/terra/anchor__borrows.sql @@ -0,0 +1,55 @@ +{{ config( + materialized = 'incremental', + unique_key = 'block_id || tx_id', + incremental_strategy = 'delete+insert', + cluster_by = ['block_timestamp', 'block_id'], + tags=['snowflake', 'terra', 'anchor', 'borrows'] +) }} + +WITH prices AS ( + + SELECT + date_trunc('hour', block_timestamp) as hour, + currency, + symbol, + avg(price_usd) as price + FROM {{ ref('terra__oracle_prices')}} + + WHERE 1=1 + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} + + GROUP BY 1,2,3 + +) + +SELECT + m.blockchain, + chain_id, + block_id, + block_timestamp, + tx_id, + msg_value:sender::string as sender, + msg_value:execute_msg:borrow_stable:borrow_amount / POW(10,6) as amount, + amount * price AS amount_usd, + 'uusd' as currency, + msg_value:contract::string as contract_address, + l.address_name as contract_label +FROM {{ref('silver_terra__msgs')}} m + +LEFT OUTER JOIN prices o + ON date_trunc('hour', block_timestamp) = o.hour + AND 'uusd' = o.currency + +LEFT OUTER JOIN {{source('shared','udm_address_labels_new')}} as l +ON msg_value:contract::string = l.address + +WHERE msg_value:execute_msg:borrow_stable IS NOT NULL -- Anchor Borrow + AND msg_value:contract::string = 'terra1sepfj7s0aeg5967uxnfk4thzlerrsktkpelm5s' -- Anchor Market Contract + AND tx_status = 'SUCCEEDED' + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} \ No newline at end of file diff --git a/models/terra/anchor__borrows.yml b/models/terra/anchor__borrows.yml new file mode 100644 index 00000000..25504ac5 --- /dev/null +++ b/models/terra/anchor__borrows.yml @@ -0,0 +1,44 @@ +version: 2 +models: + - name: anchor__borrows + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - TX_ID + - BLOCK_ID + columns: + - name: BLOCKCHAIN + tests: + - not_null + - name: CHAIN_ID + tests: + - not_null + - name: BLOCK_ID + tests: + - not_null + - name: BLOCK_TIMESTAMP + tests: + - not_null + - name: TX_ID + tests: + - not_null + - name: AMOUNT + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: AMOUNT_USD + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: CURRENCY + tests: + - not_null + - name: CONTRACT_ADDRESS + tests: + - not_null + - dbt_expectations.expect_column_values_to_match_regex: + regex: terra[0-9a-zA-Z]{39,40} \ No newline at end of file diff --git a/models/terra/anchor__burns.sql b/models/terra/anchor__burns.sql new file mode 100644 index 00000000..9b09737a --- /dev/null +++ b/models/terra/anchor__burns.sql @@ -0,0 +1,54 @@ +{{ config( + materialized = 'incremental', + unique_key = 'block_id || tx_id', + incremental_strategy = 'delete+insert', + cluster_by = ['block_timestamp', 'block_id'], + tags=['snowflake', 'terra', 'anchor', 'burns'] +) }} + +WITH prices AS ( + + SELECT + date_trunc('hour', block_timestamp) as hour, + currency, + symbol, + avg(price_usd) as price + FROM {{ ref('terra__oracle_prices')}} + + WHERE 1=1 + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} + + GROUP BY 1,2,3 + +) + +SELECT + m.blockchain, + chain_id, + block_id, + block_timestamp, + tx_id, + msg_value:sender::string as sender, + msg_value:execute_msg:send:amount / POW(10,6) as amount, + amount * price AS amount_usd, + msg_value:contract::string as currency, + msg_value:execute_msg:send:contract::string as contract_address, + l.address_name AS contract_label +FROM {{ref('silver_terra__msgs')}} m + +LEFT OUTER JOIN prices o + ON date_trunc('hour', block_timestamp) = o.hour + AND msg_value:contract::string = o.currency + +LEFT OUTER JOIN {{source('shared','udm_address_labels_new')}} as l +ON msg_value:execute_msg:send:contract::string = l.address + +WHERE msg_value:execute_msg:send:msg:unbond IS NOT NULL + AND tx_status = 'SUCCEEDED' + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} \ No newline at end of file diff --git a/models/terra/anchor__burns.yml b/models/terra/anchor__burns.yml new file mode 100644 index 00000000..3f3f342b --- /dev/null +++ b/models/terra/anchor__burns.yml @@ -0,0 +1,44 @@ +version: 2 +models: + - name: anchor__burns + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - TX_ID + - BLOCK_ID + columns: + - name: BLOCKCHAIN + tests: + - not_null + - name: CHAIN_ID + tests: + - not_null + - name: BLOCK_ID + tests: + - not_null + - name: BLOCK_TIMESTAMP + tests: + - not_null + - name: TX_ID + tests: + - not_null + - name: AMOUNT + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: AMOUNT_USD + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: CURRENCY + tests: + - not_null + - name: CONTRACT_ADDRESS + tests: + - not_null + - dbt_expectations.expect_column_values_to_match_regex: + regex: terra[0-9a-zA-Z]{39,40} \ No newline at end of file diff --git a/models/terra/anchor__collateral.sql b/models/terra/anchor__collateral.sql new file mode 100644 index 00000000..11032db9 --- /dev/null +++ b/models/terra/anchor__collateral.sql @@ -0,0 +1,126 @@ +{{ config( + materialized = 'incremental', + unique_key = 'block_id || tx_id', + incremental_strategy = 'delete+insert', + cluster_by = ['block_timestamp', 'block_id'], + tags=['snowflake', 'terra', 'anchor', 'collateral'] +) }} + +WITH prices AS ( + + SELECT + date_trunc('hour', block_timestamp) as hour, + currency, + symbol, + avg(price_usd) as price + FROM {{ ref('terra__oracle_prices')}} + + WHERE 1=1 + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} + + GROUP BY 1,2,3 + +), + +msgs AS ( + +SELECT + m.blockchain, + chain_id, + block_id, + block_timestamp, + tx_id, + 'withdraw' as action, + msg_value:sender::string AS sender, + msg_value:execute_msg:send:contract::string AS contract_address, + l.address_name AS contract_label +FROM {{ref('silver_terra__msgs')}} m + +LEFT OUTER JOIN {{source('shared','udm_address_labels_new')}} as l +ON msg_value:execute_msg:send:contract::string = l.address + +WHERE msg_value:execute_msg:withdraw_collateral IS NOT NULL + AND tx_status = 'SUCCEEDED' + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} + +), + +events AS ( + + SELECT + tx_id, + event_attributes:collaterals[0]:amount / POW(10,6) as amount, + amount * price AS amount_usd, + event_attributes:collaterals[0]:denom::string as currency +FROM {{ref('silver_terra__msg_events')}} m + +LEFT OUTER JOIN prices o + ON date_trunc('hour', block_timestamp) = o.hour + AND event_attributes:collaterals[0]:denom::string = o.currency + +WHERE tx_id IN(SELECT tx_id FROM msgs) + AND event_type = 'from_contract' + AND msg_index = 0 + AND tx_status = 'SUCCEEDED' + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} + +) + +SELECT + blockchain, + chain_id, + block_id, + block_timestamp, + m.tx_id, + action as event_type, + sender, + amount, + amount_usd, + currency, + contract_address, + contract_label +FROM msgs m + +JOIN events e + ON m.tx_id = e.tx_id + + +UNION + +SELECT + m.blockchain, + chain_id, + block_id, + block_timestamp, + tx_id, + 'provide' as event_type, + msg_value:sender::string AS sender, + msg_value:execute_msg:send:amount / POW(10,6) as amount, + amount * price AS amount_usd, + msg_value:contract::string as currency, + msg_value:execute_msg:send:contract::string AS contract_address, + l.address_name AS contract_label +FROM {{ref('silver_terra__msgs')}} m + +LEFT OUTER JOIN {{source('shared','udm_address_labels_new')}} as l +ON msg_value:execute_msg:send:contract::string = l.address + +LEFT OUTER JOIN prices o + ON date_trunc('hour', block_timestamp) = o.hour + AND msg_value:contract::string = o.currency + +WHERE msg_value:execute_msg:send:msg:deposit_collateral IS NOT NULL + AND tx_status = 'SUCCEEDED' + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} \ No newline at end of file diff --git a/models/terra/anchor__collateral.yml b/models/terra/anchor__collateral.yml new file mode 100644 index 00000000..b78cf57a --- /dev/null +++ b/models/terra/anchor__collateral.yml @@ -0,0 +1,38 @@ +version: 2 +models: + - name: anchor__collateral + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - TX_ID + - BLOCK_ID + - AMOUNT + - CURRENCY + columns: + - name: BLOCKCHAIN + tests: + - not_null + - name: CHAIN_ID + tests: + - not_null + - name: BLOCK_ID + tests: + - not_null + - name: BLOCK_TIMESTAMP + tests: + - not_null + - name: TX_ID + tests: + - not_null + - name: AMOUNT + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: AMOUNT_USD + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float \ No newline at end of file diff --git a/models/terra/anchor__deposits.sql b/models/terra/anchor__deposits.sql new file mode 100644 index 00000000..d85228c4 --- /dev/null +++ b/models/terra/anchor__deposits.sql @@ -0,0 +1,105 @@ +{{ config( + materialized = 'incremental', + unique_key = 'block_id || tx_id', + incremental_strategy = 'delete+insert', + cluster_by = ['block_timestamp', 'block_id'], + tags=['snowflake', 'terra', 'anchor', 'deposits'] +) }} + +WITH prices AS ( + + SELECT + date_trunc('hour', block_timestamp) as hour, + currency, + symbol, + avg(price_usd) as price + FROM {{ ref('terra__oracle_prices')}} + + WHERE 1=1 + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} + + GROUP BY 1,2,3 + +), + +msgs AS ( + +SELECT + m.blockchain, + chain_id, + block_id, + block_timestamp, + tx_id, + msg_index, + msg_value:sender::string as sender, + msg_value:coins[0]:amount / POW(10,6) as deposit_amount, + deposit_amount * price AS deposit_amount_usd, + msg_value:coins[0]:denom::string as deposit_currency, + msg_value:contract::string as contract_address, + l.address_name as contract_label +FROM {{ref('silver_terra__msgs')}} m + +LEFT OUTER JOIN {{source('shared','udm_address_labels_new')}} as l +ON msg_value:contract::string = l.address + +LEFT OUTER JOIN prices o + ON date_trunc('hour', block_timestamp) = o.hour + AND msg_value:coins[0]:denom::string = o.currency + +WHERE msg_value:execute_msg:deposit_stable IS NOT NULL + AND msg_value:contract::string = 'terra1sepfj7s0aeg5967uxnfk4thzlerrsktkpelm5s' + AND tx_status = 'SUCCEEDED' + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} + +), + +events AS ( + +SELECT + tx_id, + msg_index, + event_attributes:mint_amount / POW(10,6) as mint_amount, + mint_amount * price AS mint_amount_usd, + event_attributes:"1_contract_address"::string as mint_currency +FROM {{ref('silver_terra__msg_events')}} + +LEFT OUTER JOIN prices o + ON date_trunc('hour', block_timestamp) = o.hour + AND event_attributes:"1_contract_address"::string = o.currency + +WHERE event_type = 'from_contract' + AND tx_id IN(SELECT tx_id FROM msgs) + AND event_attributes:"0_action"::string = 'deposit_stable' + AND tx_status = 'SUCCEEDED' + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} + +) + +SELECT + blockchain, + chain_id, + block_id, + block_timestamp, + m.tx_id, + sender, + deposit_amount, + deposit_amount_usd, + deposit_currency, + mint_amount, + mint_amount_usd, + mint_currency, + contract_address, + contract_label +FROM msgs m + +JOIN events e + ON m.tx_id = e.tx_id \ No newline at end of file diff --git a/models/terra/anchor__deposits.yml b/models/terra/anchor__deposits.yml new file mode 100644 index 00000000..d8dcdeb0 --- /dev/null +++ b/models/terra/anchor__deposits.yml @@ -0,0 +1,47 @@ +version: 2 +models: + - name: anchor__deposits + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - TX_ID + - BLOCK_ID + columns: + - name: BLOCKCHAIN + tests: + - not_null + - name: CHAIN_ID + tests: + - not_null + - name: BLOCK_ID + tests: + - not_null + - name: BLOCK_TIMESTAMP + tests: + - not_null + - name: TX_ID + tests: + - not_null + - name: DEPOSIT_AMOUNT + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: DEPOSIT_AMOUNT_USD + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: DEPOSIT_CURRENCY + tests: + - not_null + - name: SENDER + tests: + - not_null + - name: CONTRACT_ADDRESS + tests: + - not_null + - dbt_expectations.expect_column_values_to_match_regex: + regex: terra[0-9a-zA-Z]{39,40} \ No newline at end of file diff --git a/models/terra/anchor__gov_staking.sql b/models/terra/anchor__gov_staking.sql new file mode 100644 index 00000000..6541ab53 --- /dev/null +++ b/models/terra/anchor__gov_staking.sql @@ -0,0 +1,127 @@ +{{ config( + materialized = 'incremental', + unique_key = 'block_id || tx_id', + incremental_strategy = 'delete+insert', + cluster_by = ['block_timestamp', 'block_id'], + tags=['snowflake', 'terra', 'anchor', 'anchor_gov'] +) }} + +WITH prices AS ( + + SELECT + date_trunc('hour', block_timestamp) as hour, + currency, + symbol, + avg(price_usd) as price + FROM {{ ref('terra__oracle_prices')}} + + WHERE 1=1 + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} + + GROUP BY 1,2,3 + +), + +stake_msgs AS ( + +SELECT + t.blockchain, + chain_id, + block_id, + block_timestamp, + tx_id, + msg_value:sender::string as sender, + msg_value:execute_msg:send:amount / POW(10,6) as amount, + amount * o.price AS amount_usd, + msg_value:contract::string as currency, + msg_value:execute_msg:send:contract::string as contract_address, + l.address_name AS contract_label +FROM {{ref('silver_terra__msgs')}} t + +LEFT OUTER JOIN prices o + ON date_trunc('hour', t.block_timestamp) = o.hour + AND msg_value:contract::string = o.currency + +LEFT OUTER JOIN {{source('shared','udm_address_labels_new')}} as l +ON msg_value:execute_msg:send:contract::string = l.address + +WHERE msg_value:execute_msg:send:msg:stake_voting_tokens IS NOT NULL + AND msg_value:execute_msg:send:contract::string = 'terra1f32xyep306hhcxxxf7mlyh0ucggc00rm2s9da5' + AND tx_status = 'SUCCEEDED' + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} + +), + +stake_events AS ( +SELECT + tx_id, + event_attributes:share as shares +FROM {{ref('silver_terra__msg_events')}} +WHERE tx_id IN(SELECT DISTINCT tx_id FROM stake_msgs) + AND event_type = 'from_contract' + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} +) + +-- Staking +SELECT + blockchain, + chain_id, + block_id, + block_timestamp, + m.tx_id, + 'stake' as event_type, + sender, + amount, + amount_usd, + currency, + shares, + contract_address, + contract_label +FROM stake_msgs m + +JOIN stake_events e + ON m.tx_id = e.tx_id + +UNION + +-- Unstaking + +SELECT + t.blockchain, + chain_id, + block_id, + block_timestamp, + tx_id, + 'unstake' as event_type, + msg_value:sender::string as sender, + msg_value:execute_msg:withdraw_voting_tokens:amount / POW(10,6) as amount, + amount * o.price AS amount_usd, + 'terra14z56l0fp2lsf86zy3hty2z47ezkhnthtr9yq76' as currency, + NULL as shares, + msg_value:contract::string as contract_address, + l.address_name as contract_label +FROM {{ref('silver_terra__msgs')}} t + +LEFT OUTER JOIN prices o + ON date_trunc('hour', t.block_timestamp) = o.hour + AND 'terra14z56l0fp2lsf86zy3hty2z47ezkhnthtr9yq76' = o.currency + +LEFT OUTER JOIN {{source('shared','udm_address_labels_new')}} as l +ON msg_value:contract::string = l.address + +WHERE msg_value:execute_msg:withdraw_voting_tokens IS NOT NULL + AND msg_value:contract::string = 'terra1f32xyep306hhcxxxf7mlyh0ucggc00rm2s9da5' + AND tx_status = 'SUCCEEDED' + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} \ No newline at end of file diff --git a/models/terra/anchor__gov_staking.yml b/models/terra/anchor__gov_staking.yml new file mode 100644 index 00000000..75d871d9 --- /dev/null +++ b/models/terra/anchor__gov_staking.yml @@ -0,0 +1,50 @@ +version: 2 +models: + - name: anchor__gov_staking + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - TX_ID + - BLOCK_ID + - SHARES + columns: + - name: BLOCKCHAIN + tests: + - not_null + - name: CHAIN_ID + tests: + - not_null + - name: BLOCK_ID + tests: + - not_null + - name: BLOCK_TIMESTAMP + tests: + - not_null + - name: TX_ID + tests: + - not_null + - name: AMOUNT + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: AMOUNT_USD + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: CURRENCY + tests: + - not_null + - name: SENDER + tests: + - not_null + - dbt_expectations.expect_column_values_to_match_regex: + regex: terra[0-9a-zA-Z]{39,40} + - name: CONTRACT_ADDRESS + tests: + - not_null + - dbt_expectations.expect_column_values_to_match_regex: + regex: terra[0-9a-zA-Z]{39,40} \ No newline at end of file diff --git a/models/terra/anchor__gov_submit_proposal.sql b/models/terra/anchor__gov_submit_proposal.sql new file mode 100644 index 00000000..f0f3beff --- /dev/null +++ b/models/terra/anchor__gov_submit_proposal.sql @@ -0,0 +1,69 @@ +{{ config( + materialized = 'incremental', + unique_key = 'block_id || tx_id', + incremental_strategy = 'delete+insert', + cluster_by = ['block_timestamp', 'block_id'], + tags=['snowflake', 'terra', 'anchor', 'anchor_gov'] +) }} + +WITH msgs AS ( +SELECT + blockchain, + chain_id, + block_id, + block_timestamp, + tx_id, + msg_value:sender::string as creator, + msg_value:execute_msg:send:amount / POW(10,6) as amount, + msg_value:execute_msg:send:msg:create_poll:title::string as title, + msg_value:execute_msg:send:msg:create_poll:link::string as link, + msg_value:execute_msg:send:msg:create_poll:description::string as description, + msg_value:execute_msg:send:msg:create_poll:execute_msg:msg as msg, + msg_value:execute_msg:send:contract::string as contract_address +FROM {{ref('silver_terra__msgs')}} +WHERE msg_value:execute_msg:send:msg:create_poll IS NOT NULL + AND msg_value:execute_msg:send:contract::string = 'terra1f32xyep306hhcxxxf7mlyh0ucggc00rm2s9da5' -- ANC Governance + AND tx_status = 'SUCCEEDED' + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} +), + +events AS ( +SELECT + tx_id, + COALESCE(to_timestamp(event_attributes:end_time),event_attributes:end_height) as end_time, + event_attributes:poll_id as poll_id +FROM {{ref('silver_terra__msg_events')}} +WHERE tx_id IN(select tx_id from msgs) + AND event_type = 'from_contract' + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} +) + +SELECT + m.blockchain, + chain_id, + block_id, + block_timestamp, + m.tx_id, + poll_id, + end_time, + creator, + amount, + title, + link, + description, + msg, + contract_address, + l.address_name AS contract_label +FROM msgs m + +JOIN events e + ON m.tx_id = e.tx_id + +LEFT OUTER JOIN {{source('shared','udm_address_labels_new')}} as l +ON contract_address = l.address diff --git a/models/terra/anchor__gov_submit_proposal.yml b/models/terra/anchor__gov_submit_proposal.yml new file mode 100644 index 00000000..1cc36145 --- /dev/null +++ b/models/terra/anchor__gov_submit_proposal.yml @@ -0,0 +1,41 @@ +version: 2 +models: + - name: anchor__gov_submit_proposal + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - TX_ID + - BLOCK_ID + columns: + - name: BLOCKCHAIN + tests: + - not_null + - name: CHAIN_ID + tests: + - not_null + - name: BLOCK_ID + tests: + - not_null + - name: BLOCK_TIMESTAMP + tests: + - not_null + - name: TX_ID + tests: + - not_null + - name: AMOUNT + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: CREATOR + tests: + - not_null + - dbt_expectations.expect_column_values_to_match_regex: + regex: terra[0-9a-zA-Z]{39,40} + - name: CONTRACT_ADDRESS + tests: + - not_null + - dbt_expectations.expect_column_values_to_match_regex: + regex: terra[0-9a-zA-Z]{39,40} \ No newline at end of file diff --git a/models/terra/anchor__gov_vote.sql b/models/terra/anchor__gov_vote.sql new file mode 100644 index 00000000..5ed25822 --- /dev/null +++ b/models/terra/anchor__gov_vote.sql @@ -0,0 +1,32 @@ +{{ config( + materialized = 'incremental', + unique_key = 'block_id || tx_id', + incremental_strategy = 'delete+insert', + cluster_by = ['block_timestamp', 'block_id'], + tags=['snowflake', 'terra', 'anchor', 'anchor_gov'] +) }} + +SELECT + m.blockchain, + chain_id, + block_id, + block_timestamp, + tx_id, + msg_value:sender::string as voter, + msg_value:execute_msg:cast_vote:poll_id as poll_id, + msg_value:execute_msg:cast_vote:vote::string as vote, + msg_value:execute_msg:cast_vote:amount / POW(10,6) as balance, + msg_value:contract::string as contract_address, + l.address_name as contract_label +FROM {{ref('silver_terra__msgs')}} m + +LEFT OUTER JOIN {{source('shared','udm_address_labels_new')}} as l +ON msg_value:contract::string = l.address + +WHERE msg_value:contract::string = 'terra1f32xyep306hhcxxxf7mlyh0ucggc00rm2s9da5' -- ANC Governance + AND msg_value:execute_msg:cast_vote IS NOT NULL + AND tx_status = 'SUCCEEDED' + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} \ No newline at end of file diff --git a/models/terra/anchor__gov_vote.yml b/models/terra/anchor__gov_vote.yml new file mode 100644 index 00000000..ee7b9a68 --- /dev/null +++ b/models/terra/anchor__gov_vote.yml @@ -0,0 +1,41 @@ +version: 2 +models: + - name: anchor__gov_vote + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - TX_ID + - BLOCK_ID + columns: + - name: BLOCKCHAIN + tests: + - not_null + - name: CHAIN_ID + tests: + - not_null + - name: BLOCK_ID + tests: + - not_null + - name: BLOCK_TIMESTAMP + tests: + - not_null + - name: TX_ID + tests: + - not_null + - name: BALANCE + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: VOTER + tests: + - not_null + - dbt_expectations.expect_column_values_to_match_regex: + regex: terra[0-9a-zA-Z]{39,40} + - name: CONTRACT_ADDRESS + tests: + - not_null + - dbt_expectations.expect_column_values_to_match_regex: + regex: terra[0-9a-zA-Z]{39,40} \ No newline at end of file diff --git a/models/terra/anchor__liquidations.sql b/models/terra/anchor__liquidations.sql new file mode 100644 index 00000000..ce0316a2 --- /dev/null +++ b/models/terra/anchor__liquidations.sql @@ -0,0 +1,108 @@ +{{ config( + materialized = 'incremental', + unique_key = 'block_id || tx_id', + incremental_strategy = 'delete+insert', + cluster_by = ['block_timestamp', 'block_id'], + tags=['snowflake', 'terra', 'anchor', 'liquidations'] +) }} + +WITH prices AS ( + + SELECT + date_trunc('hour', block_timestamp) as hour, + currency, + symbol, + avg(price_usd) as price + FROM {{ ref('terra__oracle_prices')}} + + WHERE 1=1 + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} + + GROUP BY 1,2,3 + +), + +msgs AS ( + +SELECT + m.blockchain, + chain_id, + block_id, + block_timestamp, + tx_id, + msg_value:execute_msg:liquidate_collateral:borrower::string AS borrower, + msg_value:contract::string as contract_address, + l.address_name as contract_label +FROM {{ref('silver_terra__msgs')}} m + +LEFT OUTER JOIN {{source('shared','udm_address_labels_new')}} as l +ON msg_value:contract::string = l.address + +WHERE msg_value:contract::string = 'terra1tmnqgvg567ypvsvk6rwsga3srp7e3lg6u0elp8' + AND msg_value:execute_msg:liquidate_collateral IS NOT NULL + AND tx_status = 'SUCCEEDED' + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} + +), + +events AS ( + +SELECT + tx_id, + event_attributes:liquidator::string AS liquidator, + event_attributes:collateral_amount / POW(10,6) AS liquidated_amount, + liquidated_amount * l.price AS liquidated_amount_usd, + event_attributes:collateral_token::string as liquidated_currency, + event_attributes:"1_repay_amount" / POW(10,6) as repay_amount, + event_attributes:"1_borrower"::string as borrower, + repay_amount * r.price as repay_amount_usd, + event_attributes:stable_denom::string as repay_currency, + event_attributes:bid_fee / POW(10,6) AS bid_fee +FROM {{ref('silver_terra__msg_events')}} + +LEFT OUTER JOIN prices l + ON date_trunc('hour', block_timestamp) = l.hour + AND event_attributes:collateral_token::string = l.currency + +LEFT OUTER JOIN prices r + ON date_trunc('hour', block_timestamp) = r.hour + AND event_attributes:stable_denom::string = r.currency + +WHERE event_type = 'from_contract' + AND tx_id IN(SELECT tx_id FROM msgs) + AND tx_status = 'SUCCEEDED' + AND event_attributes:"0_action"::string = 'liquidate_collateral' + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} + +) + +SELECT + blockchain, + chain_id, + block_id, + block_timestamp, + m.tx_id, + bid_fee, + m.borrower, + liquidator, + liquidated_amount, + liquidated_amount_usd, + liquidated_currency, + repay_amount, + repay_amount_usd, + repay_currency, + contract_address, + contract_label +FROM msgs m + +JOIN events e + ON m.tx_id = e.tx_id AND m.borrower = e.borrower \ No newline at end of file diff --git a/models/terra/anchor__liquidations.yml b/models/terra/anchor__liquidations.yml new file mode 100644 index 00000000..6e9f5765 --- /dev/null +++ b/models/terra/anchor__liquidations.yml @@ -0,0 +1,27 @@ +version: 2 +models: + - name: anchor__liquidations + columns: + - name: BLOCKCHAIN + tests: + - not_null + - name: CHAIN_ID + tests: + - not_null + - name: BLOCK_ID + tests: + - not_null + - name: BLOCK_TIMESTAMP + tests: + - not_null + - name: TX_ID + tests: + - not_null + - name: BORROWER + tests: + - not_null + - name: CONTRACT_ADDRESS + tests: + - not_null + - dbt_expectations.expect_column_values_to_match_regex: + regex: terra[0-9a-zA-Z]{39,40} \ No newline at end of file diff --git a/models/terra/anchor__redeem.sql b/models/terra/anchor__redeem.sql new file mode 100644 index 00000000..b51cd761 --- /dev/null +++ b/models/terra/anchor__redeem.sql @@ -0,0 +1,54 @@ +{{ config( + materialized = 'incremental', + unique_key = 'block_id || tx_id', + incremental_strategy = 'delete+insert', + cluster_by = ['block_timestamp', 'block_id'], + tags=['snowflake', 'terra', 'anchor', 'redeem'] +) }} + +WITH prices AS ( + + SELECT + date_trunc('hour', block_timestamp) as hour, + currency, + symbol, + avg(price_usd) as price + FROM {{ ref('terra__oracle_prices')}} + + WHERE 1=1 + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} + + GROUP BY 1,2,3 + +) + +SELECT + m.blockchain, + chain_id, + block_id, + block_timestamp, + tx_id, + msg_value:sender::string as sender, + msg_value:execute_msg:send:amount / POW(10,6) as amount, + amount * price AS amount_usd, + msg_value:contract::string as currency, + msg_value:execute_msg:send:contract::string as contract_address, + l.address_name AS contract_label +FROM {{ref('silver_terra__msgs')}} m + +LEFT OUTER JOIN {{source('shared','udm_address_labels_new')}} as l +ON msg_value:execute_msg:send:contract::string = l.address + +LEFT OUTER JOIN prices r + ON date_trunc('hour', block_timestamp) = hour + AND msg_value:contract::string = r.currency + +WHERE msg_value:execute_msg:send:msg:redeem_stable IS NOT NULL + AND tx_status = 'SUCCEEDED' + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} \ No newline at end of file diff --git a/models/terra/anchor__redeem.yml b/models/terra/anchor__redeem.yml new file mode 100644 index 00000000..fe727d5d --- /dev/null +++ b/models/terra/anchor__redeem.yml @@ -0,0 +1,50 @@ +version: 2 +models: + - name: anchor__redeem + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - TX_ID + - BLOCK_ID + columns: + - name: BLOCKCHAIN + tests: + - not_null + - name: CHAIN_ID + tests: + - not_null + - name: BLOCK_ID + tests: + - not_null + - name: BLOCK_TIMESTAMP + tests: + - not_null + - name: TX_ID + tests: + - not_null + - name: AMOUNT + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: AMOUNT_USD + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: CURRENCY + tests: + - not_null + - name: SENDER + tests: + - not_null + - dbt_expectations.expect_column_values_to_match_regex: + regex: terra[0-9a-zA-Z]{39,40} + - name: CONTRACT_ADDRESS + tests: + - not_null + - dbt_expectations.expect_column_values_to_match_regex: + regex: terra[0-9a-zA-Z]{39,40} \ No newline at end of file diff --git a/models/terra/anchor__repay.sql b/models/terra/anchor__repay.sql new file mode 100644 index 00000000..abb4ca93 --- /dev/null +++ b/models/terra/anchor__repay.sql @@ -0,0 +1,54 @@ +{{ config( + materialized = 'incremental', + unique_key = 'block_id || tx_id', + incremental_strategy = 'delete+insert', + cluster_by = ['block_timestamp', 'block_id'], + tags=['snowflake', 'terra', 'anchor', 'repay'] +) }} + +WITH prices AS ( + + SELECT + date_trunc('hour', block_timestamp) as hour, + currency, + symbol, + avg(price_usd) as price + FROM {{ ref('terra__oracle_prices')}} + + WHERE 1=1 + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} + + GROUP BY 1,2,3 + +) + +SELECT + m.blockchain, + chain_id, + block_id, + block_timestamp, + tx_id, + msg_value:sender::string as sender, + msg_value:coins[0]:amount / POW(10,6) as amount, + amount * price AS amount_usd, + msg_value:coins[0]:denom::string as currency, + msg_value:contract::string as contract_address, + l.address_name AS contract_label +FROM {{ref('silver_terra__msgs')}} m + +LEFT OUTER JOIN {{source('shared','udm_address_labels_new')}} as l +ON msg_value:contract::string = l.address + +LEFT OUTER JOIN prices r + ON date_trunc('hour', block_timestamp) = hour + AND msg_value:coins[0]:denom::string = r.currency + +WHERE msg_value:execute_msg:repay_stable IS NOT NULL + AND tx_status = 'SUCCEEDED' + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} \ No newline at end of file diff --git a/models/terra/anchor__repay.yml b/models/terra/anchor__repay.yml new file mode 100644 index 00000000..e91293d7 --- /dev/null +++ b/models/terra/anchor__repay.yml @@ -0,0 +1,50 @@ +version: 2 +models: + - name: anchor__repay + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - TX_ID + - BLOCK_ID + columns: + - name: BLOCKCHAIN + tests: + - not_null + - name: CHAIN_ID + tests: + - not_null + - name: BLOCK_ID + tests: + - not_null + - name: BLOCK_TIMESTAMP + tests: + - not_null + - name: TX_ID + tests: + - not_null + - name: AMOUNT + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: AMOUNT_USD + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: CURRENCY + tests: + - not_null + - name: SENDER + tests: + - not_null + - dbt_expectations.expect_column_values_to_match_regex: + regex: terra[0-9a-zA-Z]{39,40} + - name: CONTRACT_ADDRESS + tests: + - not_null + - dbt_expectations.expect_column_values_to_match_regex: + regex: terra[0-9a-zA-Z]{39,40} \ No newline at end of file diff --git a/models/terra/anchor__reward_claims.sql b/models/terra/anchor__reward_claims.sql new file mode 100644 index 00000000..ecf7c76b --- /dev/null +++ b/models/terra/anchor__reward_claims.sql @@ -0,0 +1,149 @@ +{{ config( + materialized = 'incremental', + unique_key = 'block_id || tx_id', + incremental_strategy = 'delete+insert', + cluster_by = ['block_timestamp', 'block_id'], + tags=['snowflake', 'terra', 'anchor', 'reward_claims'] +) }} +WITH prices AS ( + + SELECT + date_trunc('hour', block_timestamp) as hour, + currency, + symbol, + avg(price_usd) as price + FROM {{ ref('terra__oracle_prices')}} + + WHERE 1=1 + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} + + GROUP BY 1,2,3 + +), + +withdraw_msgs AS ( + +SELECT + tx_id, + msg_index, + msg_value:contract::string as claim_0_contract +FROM {{ref('silver_terra__msgs')}} +WHERE msg_value:execute_msg:withdraw IS NOT NULL + AND msg_index = 0 + AND msg_value:contract::string = 'terra1897an2xux840p9lrh6py3ryankc6mspw49xse3' + AND tx_status = 'SUCCEEDED' + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} + +), + +claim_msgs AS ( + +SELECT + tx_id, + msg_index, + msg_value:sender::string as sender, + msg_value:contract::string as claim_1_contract +FROM {{ref('silver_terra__msgs')}} +WHERE msg_value:execute_msg:claim_rewards IS NOT NULL + AND msg_index = 1 + AND msg_value:contract::string = 'terra1sepfj7s0aeg5967uxnfk4thzlerrsktkpelm5s' + AND tx_status = 'SUCCEEDED' + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} + +), + +withdraw AS ( + +SELECT + m.tx_id, + claim_0_contract, + event_attributes:"0_amount" / POW(10,6) as claim_0_amount, + event_attributes:"1_contract_address"::string as claim_0_currency +FROM {{ref('silver_terra__msg_events')}} e + +JOIN withdraw_msgs m + ON m.tx_id = e.tx_id + AND m.msg_index = e.msg_index + +WHERE event_type = 'from_contract' + AND tx_status = 'SUCCEEDED' + AND event_attributes:"0_action"::string = 'withdraw' + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} + +), + +claim AS ( + +SELECT + blockchain, + chain_id, + block_id, + block_timestamp, + m.tx_id, + sender, + claim_1_contract, + event_attributes:claim_amount / POW(10,6) as claim_1_amount, + event_attributes:"2_contract_address"::string as claim_1_currency +FROM {{ref('silver_terra__msg_events')}} e + +JOIN claim_msgs m + ON m.tx_id = e.tx_id + AND m.msg_index = e.msg_index + +WHERE event_type = 'from_contract' + AND tx_status = 'SUCCEEDED' + AND event_attributes:"0_action"::string = 'claim_rewards' + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} + +) + +SELECT + c.blockchain, + chain_id, + block_id, + block_timestamp, + c.tx_id, + sender, + claim_0_amount, + claim_0_amount * p0.price AS claim_0_amount_usd, + claim_0_currency, + claim_0_contract, + l0.address_name AS claim_0_contract_label, + claim_1_amount, + claim_1_amount * p1.price AS claim_1_amount_usd, + claim_1_currency, + claim_1_contract, + l1.address_name AS claim_1_contract_label +FROM claim c + +JOIN withdraw w + ON c.tx_id = w.tx_id + +LEFT OUTER JOIN {{source('shared','udm_address_labels_new')}} as l0 +ON claim_0_contract = l0.address + +LEFT OUTER JOIN {{source('shared','udm_address_labels_new')}} as l1 +ON claim_1_contract = l1.address + +LEFT OUTER JOIN prices p0 + ON date_trunc('hour', block_timestamp) = p0.hour + AND claim_0_currency = p0.currency + + LEFT OUTER JOIN prices p1 + ON date_trunc('hour', block_timestamp) = p1.hour + AND claim_1_currency = p1.currency \ No newline at end of file diff --git a/models/terra/anchor__reward_claims.yml b/models/terra/anchor__reward_claims.yml new file mode 100644 index 00000000..eaa66088 --- /dev/null +++ b/models/terra/anchor__reward_claims.yml @@ -0,0 +1,73 @@ +version: 2 +models: + - name: anchor__reward_claims + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - TX_ID + - BLOCK_ID + columns: + - name: BLOCKCHAIN + tests: + - not_null + - name: CHAIN_ID + tests: + - not_null + - name: BLOCK_ID + tests: + - not_null + - name: BLOCK_TIMESTAMP + tests: + - not_null + - name: TX_ID + tests: + - not_null + - name: SENDER + tests: + - not_null + - name: CLAIM_0_AMOUNT + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: CLAIM_0_AMOUNT_USD + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: CLAIM_0_CURRENCY + tests: + - not_null + - name: CLAIM_0_CONTRACT + tests: + - not_null + - name: CLAIM_0_CONTRACT_LABEL + tests: + - not_null + - name: CLAIM_1_AMOUNT + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: CLAIM_1_AMOUNT_USD + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: CLAIM_1_CURRENCY + tests: + - not_null + - name: CLAIM_1_CONTRACT + tests: + - not_null + - dbt_expectations.expect_column_values_to_match_regex: + regex: terra[0-9a-zA-Z]{39,40} + - name: CLAIM_1_CONTRACT_LABEL + tests: + - not_null \ No newline at end of file diff --git a/models/terra/anchor_dbt__stake.sql b/models/terra/anchor_dbt__stake.sql new file mode 100644 index 00000000..985a4f4b --- /dev/null +++ b/models/terra/anchor_dbt__stake.sql @@ -0,0 +1,130 @@ +{{ + config( + materialized='incremental', + sort='block_timestamp', + unique_key='block_id', + incremental_strategy='delete+insert', + cluster_by=['block_timestamp'], + tags=['snowflake', 'terra', 'anchor_dbt', 'stake'] + ) +}} + +WITH prices AS ( + + SELECT + date_trunc('hour', block_timestamp) as hour, + currency, + symbol, + avg(price_usd) as price + FROM {{ ref('terra__oracle_prices')}} + + WHERE 1=1 + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} + + GROUP BY 1,2,3 + +), + +msgs AS ( + +SELECT + m.blockchain, + chain_id, + block_id, + block_timestamp, + tx_id, + 'unstake' as action, + msg_value:sender::string as sender, + msg_value:execute_msg:withdraw_voting_tokens:amount / POW(10,6) as amount, + msg_value:contract::string as contract_address, + l.address_name AS contract_label +FROM {{ref('silver_terra__msgs')}} m + +LEFT OUTER JOIN {{source('shared','udm_address_labels_new')}} as l +ON msg_value:contract::string = l.address + +WHERE msg_value:execute_msg:withdraw_voting_tokens IS NOT NULL + AND msg_value:contract::string = 'terra1f32xyep306hhcxxxf7mlyh0ucggc00rm2s9da5' + AND tx_status = 'SUCCEEDED' + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} + +), + +events AS ( + +SELECT + tx_id, + price, + event_attributes:"0_contract_address"::string as currency +FROM {{ref('silver_terra__msg_events')}} + +LEFT OUTER JOIN prices r + ON date_trunc('hour', block_timestamp) = hour + AND event_attributes:"0_contract_address"::string = r.currency + +WHERE event_type = 'execute_contract' + AND tx_id IN(SELECT tx_id FROM msgs) + AND msg_index = 0 + AND tx_status = 'SUCCEEDED' + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} + +) + +SELECT + blockchain, + chain_id, + block_id, + block_timestamp, + m.tx_id, + action, + sender, + amount, + amount * price AS amount_usd, + currency, + contract_address, + contract_label +FROM msgs m + +JOIN events e + ON m.tx_id = e.tx_id + +UNION + +SELECT + m.blockchain, + chain_id, + block_id, + block_timestamp, + tx_id, + 'stake' as action, + msg_value:sender::string as sender, + msg_value:execute_msg:send:amount / POW(10,6) as amount, + amount * price AS amount_usd, + msg_value:contract::string as currency, + msg_value:execute_msg:send:contract::string as contract_address, + l.address_name AS contract_label +FROM {{ref('silver_terra__msgs')}} m + +LEFT OUTER JOIN {{source('shared','udm_address_labels_new')}} as l +ON msg_value:execute_msg:send:contract::string = l.address + +LEFT OUTER JOIN prices r + ON date_trunc('hour', block_timestamp) = hour + AND msg_value:contract::string = r.currency + +WHERE msg_value:execute_msg:send:msg:stake_voting_tokens IS NOT NULL + AND msg_value:execute_msg:send:contract::string = 'terra1f32xyep306hhcxxxf7mlyh0ucggc00rm2s9da5' + AND tx_status = 'SUCCEEDED' + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} \ No newline at end of file diff --git a/models/terra/anchor_dbt__stake.yml b/models/terra/anchor_dbt__stake.yml new file mode 100644 index 00000000..9aba2311 --- /dev/null +++ b/models/terra/anchor_dbt__stake.yml @@ -0,0 +1,54 @@ +version: 2 +models: + - name: anchor_dbt__stake + description: Anchor STAKE + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - TX_ID + - BLOCK_ID + columns: + - name: BLOCKCHAIN + tests: + - not_null + - name: CHAIN_ID + tests: + - not_null + - name: BLOCK_ID + tests: + - not_null + - name: BLOCK_TIMESTAMP + tests: + - not_null + - name: TX_ID + tests: + - not_null + - name: ACTION + tests: + - not_null + - name: SENDER + tests: + - not_null + - dbt_expectations.expect_column_values_to_match_regex: + regex: terra[0-9a-zA-Z]{39,40} + - name: AMOUNT + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: AMOUNT_USD + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: CONTRACT_ADDRESS + tests: + - not_null + - dbt_expectations.expect_column_values_to_match_regex: + regex: terra[0-9a-zA-Z]{39,40} + - name: CONTRACT_LABEL + tests: + - not_null diff --git a/models/terra/mirror__close_collateral.sql b/models/terra/mirror__close_collateral.sql new file mode 100644 index 00000000..d546dfcf --- /dev/null +++ b/models/terra/mirror__close_collateral.sql @@ -0,0 +1,284 @@ +{{ config( + materialized = 'incremental', + unique_key = 'block_id || tx_id', + incremental_strategy = 'delete+insert', + cluster_by = ['block_timestamp', 'block_id'], + tags = ['snowflake', 'terra', 'mirror', 'mirror_close_collateral'] +) }} + +WITH prices AS ( + + SELECT + DATE_TRUNC( + 'hour', + block_timestamp + ) AS HOUR, + currency, + symbol, + AVG(price_usd) AS price + FROM + {{ ref('terra__oracle_prices') }} + WHERE + 1 = 1 + +{% if is_incremental() %} +AND block_timestamp :: DATE >= ( + SELECT + MAX( + block_timestamp :: DATE + ) + FROM + {{ ref('silver_terra__msgs') }} +) +{% endif %} +GROUP BY + 1, + 2, + 3 +), +prices_backup AS ( + SELECT + DATE_TRUNC( + 'day', + block_timestamp + ) AS DAY, + currency, + symbol, + AVG(price_usd) AS price + FROM + {{ ref('terra__oracle_prices') }} + WHERE + 1 = 1 + +{% if is_incremental() %} +AND block_timestamp :: DATE >= ( + SELECT + MAX( + block_timestamp :: DATE + ) + FROM + {{ ref('silver_terra__msgs') }} +) +{% endif %} +GROUP BY + 1, + 2, + 3 +), +msgs AS ( + SELECT + m.blockchain, + chain_id, + block_id, + block_timestamp, + tx_id, + msg_value :execute_msg :send :msg :burn :position_idx AS collateral_id, + msg_value :sender :: STRING AS sender, + msg_value :contract :: STRING AS contract_address, + l.address_name AS contract_label + FROM + {{ ref('silver_terra__msgs') }} + m + LEFT OUTER JOIN {{ source( + 'shared', + 'udm_address_labels_new' + ) }} AS l + ON msg_value :contract :: STRING = l.address + WHERE + msg_value :execute_msg :send :msg :burn IS NOT NULL + AND tx_status = 'SUCCEEDED' + +{% if is_incremental() %} +AND block_timestamp :: DATE >= ( + SELECT + MAX( + block_timestamp :: DATE + ) + FROM + {{ ref('silver_terra__msgs') }} +) +{% endif %} +), +burns AS ( + SELECT + tx_id, + event_attributes :burn_amount [0] :amount / pow( + 10, + 6 + ) AS burn_amount, + burn_amount * i.price AS burn_amount_usd, + event_attributes :burn_amount [0] :denom :: STRING AS burn_currency, + event_attributes :protocol_fee [0] :amount / pow( + 10, + 6 + ) AS protocol_fee_amount, + protocol_fee_amount * f.price AS protocol_fee_amount_usd, + event_attributes :protocol_fee [0] :denom :: STRING AS protocol_fee_currency + FROM + {{ ref('silver_terra__msg_events') }} + t + LEFT OUTER JOIN prices i + ON DATE_TRUNC( + 'hour', + t.block_timestamp + ) = i.hour + AND t.event_attributes :burn_amount [0] :denom :: STRING = i.currency + LEFT OUTER JOIN prices f + ON DATE_TRUNC( + 'hour', + t.block_timestamp + ) = f.hour + AND t.event_attributes :protocol_fee [0] :denom :: STRING = f.currency + LEFT OUTER JOIN prices_backup i_b + ON DATE_TRUNC( + 'day', + t.block_timestamp + ) = i_b.day + AND t.event_attributes :burn_amount [0] :denom :: STRING = i_b.currency + LEFT OUTER JOIN prices_backup f_b + ON DATE_TRUNC( + 'day', + t.block_timestamp + ) = f_b.day + AND t.event_attributes :protocol_fee [0] :denom :: STRING = f_b.currency + WHERE + event_attributes :burn_amount IS NOT NULL + AND event_attributes :protocol_fee IS NOT NULL + AND tx_id IN( + SELECT + DISTINCT tx_id + FROM + msgs + ) + AND tx_status = 'SUCCEEDED' + +{% if is_incremental() %} +AND block_timestamp :: DATE >= ( + SELECT + MAX( + block_timestamp :: DATE + ) + FROM + {{ ref('silver_terra__msgs') }} +) +{% endif %} +), +withdraw AS ( + SELECT + tx_id, + event_attributes :tax_amount [0] :amount / pow( + 10, + 6 + ) AS tax_amount, + tax_amount * o.price AS tax_amount_usd, + event_attributes :tax_amount [0] :denom :: STRING AS tax_currency, + event_attributes :protocol_fee [0] :amount / pow( + 10, + 6 + ) AS protocol_fee_amount, + protocol_fee_amount * f.price AS protocol_fee_amount_usd, + event_attributes :protocol_fee [0] :denom :: STRING AS protocol_fee_currency, + event_attributes :withdraw_amount [0] :amount / pow( + 10, + 6 + ) AS withdraw_amount, + withdraw_amount * i.price AS withdraw_amount_usd, + event_attributes :withdraw_amount [0] :denom :: STRING AS withdraw_currency + FROM + {{ ref('silver_terra__msg_events') }} + t + LEFT OUTER JOIN prices o + ON DATE_TRUNC( + 'hour', + t.block_timestamp + ) = o.hour + AND t.event_attributes :tax_amount [0] :denom :: STRING = o.currency + LEFT OUTER JOIN prices i + ON DATE_TRUNC( + 'hour', + t.block_timestamp + ) = i.hour + AND t.event_attributes :withdraw_amount [0] :denom :: STRING = i.currency + LEFT OUTER JOIN prices f + ON DATE_TRUNC( + 'hour', + t.block_timestamp + ) = f.hour + AND t.event_attributes :protocol_fee [0] :denom :: STRING = f.currency + LEFT OUTER JOIN prices_backup o_b + ON DATE_TRUNC( + 'day', + t.block_timestamp + ) = o_b.day + AND t.event_attributes :tax_amount [0] :denom :: STRING = o_b.currency + LEFT OUTER JOIN prices_backup i_b + ON DATE_TRUNC( + 'day', + t.block_timestamp + ) = i_b.day + AND t.event_attributes :withdraw_amount [0] :denom :: STRING = i_b.currency + LEFT OUTER JOIN prices_backup f_b + ON DATE_TRUNC( + 'day', + t.block_timestamp + ) = f_b.day + AND t.event_attributes :protocol_fee [0] :denom :: STRING = f_b.currency + WHERE + event_attributes :withdraw_amount IS NOT NULL + AND event_attributes :"2_action" != 'release_shorting_funds' + AND tx_id IN( + SELECT + DISTINCT tx_id + FROM + msgs + ) + AND tx_status = 'SUCCEEDED' + +{% if is_incremental() %} +AND block_timestamp :: DATE >= ( + SELECT + MAX( + block_timestamp :: DATE + ) + FROM + {{ ref('silver_terra__msgs') }} +) +{% endif %} +) +SELECT + blockchain, + chain_id, + block_id, + block_timestamp, + m.tx_id, + collateral_id, + sender, + tax_amount, + tax_amount_usd, + tax_currency, + COALESCE( + b.protocol_fee_amount, + w.protocol_fee_amount + ) AS protocol_fee_amount, + COALESCE( + b.protocol_fee_amount_usd, + w.protocol_fee_amount_usd + ) AS protocol_fee_amount_usd, + COALESCE( + b.protocol_fee_currency, + w.protocol_fee_currency + ) AS protocol_fee_currency, + burn_amount, + burn_amount_usd, + burn_currency, + withdraw_amount, + withdraw_amount_usd, + withdraw_currency, + contract_address, + contract_label +FROM + msgs m + JOIN burns b + ON m.tx_id = b.tx_id + JOIN withdraw w + ON m.tx_id = w.tx_id diff --git a/models/terra/mirror__close_collateral.yml b/models/terra/mirror__close_collateral.yml new file mode 100644 index 00000000..1c311aea --- /dev/null +++ b/models/terra/mirror__close_collateral.yml @@ -0,0 +1,109 @@ +version: 2 +models: + - name: mirror__close_collateral + description: Mirror CLOSE COLLATERAL + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - TX_ID + - BLOCK_ID + columns: + - name: BLOCKCHAIN + description: "" + tests: + - not_null + - name: CHAIN_ID + description: "" + tests: + - not_null + - name: BLOCK_ID + description: "" + tests: + - not_null + - name: BLOCK_TIMESTAMP + description: "" + tests: + - not_null + - name: TX_ID + description: "" + tests: + - not_null + - name: COLLATERAL_ID + tests: + - not_null + - name: SENDER + description: "" + tests: + - not_null + - dbt_expectations.expect_column_values_to_match_regex: + regex: terra[0-9a-zA-Z]{39,40} + - name: TAX_AMOUNT + tests: + - not_null + - name: TAX_AMOUNT_USD + tests: + - not_null + - name: TAX_CURRENCY + tests: + - not_null + - name: PROTOCOL_FEE_AMOUNT + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: PROTOCOL_FEE_AMOUNT_USD + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: PROTOCOL_FEE_CURRENCY + tests: + - not_null + - name: BURN_AMOUNT + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: BURN_AMOUNT_USD + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: BURN_CURRENCY + tests: + - not_null + - name: WITHDRAW_AMOUNT + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: WITHDRAW_AMOUNT_USD + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: WITHDRAW_CURRENCY + tests: + - not_null + - name: CONTRACT_ADDRESS + description: "" + tests: + - not_null + - dbt_expectations.expect_column_values_to_match_regex: + regex: terra[0-9a-zA-Z]{39,40} + - name: CONTRACT_LABEL + description: "" + tests: + - not_null \ No newline at end of file diff --git a/models/terra/mirror__close_short_farm.sql b/models/terra/mirror__close_short_farm.sql new file mode 100644 index 00000000..17713e1a --- /dev/null +++ b/models/terra/mirror__close_short_farm.sql @@ -0,0 +1,216 @@ +{{ config( + materialized = 'incremental', + unique_key = 'block_id || tx_id', + incremental_strategy = 'delete+insert', + cluster_by = ['block_timestamp', 'block_id'], + tags=['snowflake', 'terra', 'mirror', 'short_farm'] +) }} + +WITH prices AS ( + + SELECT + date_trunc('hour', block_timestamp) as hour, + currency, + symbol, + avg(price_usd) as price + FROM {{ ref('terra__oracle_prices')}} + + WHERE 1=1 + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} + + GROUP BY 1,2,3 + +), + +prices_backup AS ( + + SELECT + date_trunc('day', block_timestamp) as day, + currency, + symbol, + avg(price_usd) as price + FROM {{ ref('terra__oracle_prices')}} + + WHERE 1=1 + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} + + GROUP BY 1,2,3 + +), + +tx AS ( + +SELECT + blockchain, + chain_id, + block_id, + block_timestamp, + tx_id, + msg_value +FROM {{ref('silver_terra__msgs')}} +WHERE msg_value:execute_msg:send:msg:burn IS NOT NULL + AND msg_value:execute_msg:send:contract::string = 'terra1wfz7h3aqf4cjmjcvc6s8lxdhh7k30nkczyf0mj' + AND tx_status = 'SUCCEEDED' + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} + +), + +event_tx AS ( + +SELECT + block_timestamp, + tx_id, + event_attributes +FROM {{ref('silver_terra__msg_events')}} +WHERE tx_id IN(select tx_id from tx) + AND event_type = 'from_contract' + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} + +), + +msgs as ( + +SELECT + t.blockchain, + chain_id, + block_id, + block_timestamp, + tx_id, + msg_value:execute_msg:send:msg:burn:position_idx as collateral_id, + msg_value:sender::string as sender, + msg_value:execute_msg:send:contract::string as contract_address, + l.address_name AS contract_label +FROM tx t + +LEFT OUTER JOIN {{source('shared','udm_address_labels_new')}} as l +ON msg_value:execute_msg:send:contract::string = l.address + +), + +withdraw_events AS ( + +SELECT + tx_id, + + event_attributes:withdraw_amount[0]:amount / POW(10,6) AS withdraw_amount, + withdraw_amount * coalesce(o.price,o_b.price) AS withdraw_amount_usd, + event_attributes:withdraw_amount[0]:denom::string AS withdraw_currency, + + event_attributes:unlocked_amount[0]:amount / POW(10,6) AS unlocked_amount, + unlocked_amount * coalesce(i.price,i_b.price) AS unlocked_amount_usd, + event_attributes:unlocked_amount[0]:denom::string AS unlocked_currency, + + (event_attributes:"0_tax_amount"[0]:amount + event_attributes:"1_tax_amount"[0]:amount) / POW(10,6) AS tax_amount, + tax_amount * coalesce(a.price,a_b.price) AS tax_amount_usd, + event_attributes:"0_tax_amount"[0]:denom::string AS tax_currency + +FROM event_tx t + +LEFT OUTER JOIN prices o + ON date_trunc('hour', t.block_timestamp) = o.hour + AND t.event_attributes:withdraw_amount[0]:denom::string = o.currency + +LEFT OUTER JOIN prices i + ON date_trunc('hour', t.block_timestamp) = i.hour + AND t.event_attributes:unlocked_amount[0]:denom::string = i.currency + +LEFT OUTER JOIN prices a + ON date_trunc('hour', t.block_timestamp) = a.hour + AND t.event_attributes:"0_tax_amount"[0]:denom::string = a.currency + +LEFT OUTER JOIN prices_backup o_b + ON date_trunc('day', t.block_timestamp) = o_b.day + AND t.event_attributes:withdraw_amount[0]:denom::string = o_b.currency + +LEFT OUTER JOIN prices_backup i_b + ON date_trunc('day', t.block_timestamp) = i_b.day + AND t.event_attributes:unlocked_amount[0]:denom::string = i_b.currency + +LEFT OUTER JOIN prices_backup a_b + ON date_trunc('day', t.block_timestamp) = a_b.day + AND t.event_attributes:"0_tax_amount"[0]:denom::string = a_b.currency + +WHERE event_attributes:withdraw_amount IS NOT NULL + +), + +burn_events AS ( + +SELECT + tx_id, + + event_attributes:burn_amount[0]:amount / POW(10,6) AS burn_amount, + burn_amount * coalesce(o.price,o_b.price) AS burn_amount_usd, + event_attributes:burn_amount[0]:denom::string AS burn_currency, + + event_attributes:protocol_fee[0]:amount / POW(10,6) AS protocol_fee_amount, + protocol_fee_amount * coalesce(i.price,i_b.price) AS protocol_fee_amount_usd, + event_attributes:protocol_fee[0]:denom::string AS protocol_fee_currency +FROM event_tx t + +LEFT OUTER JOIN prices o + ON date_trunc('hour', t.block_timestamp) = o.hour + AND t.event_attributes:burn_amount[0]:denom::string = o.currency + +LEFT OUTER JOIN prices i + ON date_trunc('hour', t.block_timestamp) = i.hour + AND t.event_attributes:protocol_fee[0]:denom::string = i.currency + +LEFT OUTER JOIN prices_backup o_b + ON date_trunc('day', t.block_timestamp) = o_b.day + AND t.event_attributes:burn_amount[0]:denom::string = o_b.currency + +LEFT OUTER JOIN prices_backup i_b + ON date_trunc('day', t.block_timestamp) = i_b.day + AND t.event_attributes:protocol_fee[0]:denom::string = i_b.currency + +WHERE event_attributes:burn_amount IS NOT NULL + +) + +SELECT + blockchain, + chain_id, + block_id, + block_timestamp, + m.tx_id, + collateral_id, + sender, + tax_amount, + tax_amount_usd, + tax_currency, + protocol_fee_amount, + protocol_fee_amount_usd, + protocol_fee_currency, + burn_amount, + burn_amount_usd, + burn_currency, + withdraw_amount, + withdraw_amount_usd, + withdraw_currency, + unlocked_amount, + unlocked_amount_usd, + unlocked_currency, + contract_address, + contract_label +FROM msgs m + +JOIN withdraw_events w + ON m.tx_id = w.tx_id + +JOIN burn_events b + ON m.tx_id = b.tx_id + +WHERE unlocked_amount IS NOT NULL \ No newline at end of file diff --git a/models/terra/mirror__close_short_farm.yml b/models/terra/mirror__close_short_farm.yml new file mode 100644 index 00000000..70c69395 --- /dev/null +++ b/models/terra/mirror__close_short_farm.yml @@ -0,0 +1,135 @@ +version: 2 +models: + - name: mirror__close_short_farm + description: Mirror CLOSE SHORT FARM + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - TX_ID + - BLOCK_ID + columns: + - name: BLOCKCHAIN + description: "" + tests: + - not_null + - name: CHAIN_ID + description: "" + tests: + - not_null + - name: BLOCK_ID + description: "" + tests: + - not_null + - name: BLOCK_TIMESTAMP + description: "" + tests: + - not_null + - name: TX_ID + description: "" + tests: + - not_null + - name: COLLATERAL_ID + tests: + - not_null + - name: SENDER + description: "" + tests: + - not_null + - dbt_expectations.expect_column_values_to_match_regex: + regex: terra[0-9a-zA-Z]{39,40} + - name: TAX_AMOUNT + tests: + - not_null + - name: TAX_AMOUNT_USD + tests: + # - not_null: + # where: TAX_CURRENCY <> 'terra1hzh9vpxhsk8253se0vv5jj6etdvxu3nv8z07zu' + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: TAX_CURRENCY + tests: + - not_null + - name: PROTOCOL_FEE_AMOUNT + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: PROTOCOL_FEE_AMOUNT_USD + tests: + # - not_null: + # where: PROTOCOL_FEE_CURRENCY <> 'terra1hzh9vpxhsk8253se0vv5jj6etdvxu3nv8z07zu' + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + + - name: PROTOCOL_FEE_CURRENCY + tests: + - not_null + - name: BURN_AMOUNT + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: BURN_AMOUNT_USD + tests: + # - not_null: + # where: BURN_CURRENCY <> 'terra1hzh9vpxhsk8253se0vv5jj6etdvxu3nv8z07zu' + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: BURN_CURRENCY + tests: + - not_null + - name: WITHDRAW_AMOUNT + tests: + - not_null: + where: WITHDRAW_CURRENCY <> 'terra1hzh9vpxhsk8253se0vv5jj6etdvxu3nv8z07zu' + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: WITHDRAW_AMOUNT_USD + tests: + # - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: WITHDRAW_CURRENCY + tests: + - not_null + - name: UNLOCKED_AMOUNT + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: UNLOCKED_AMOUNT_USD + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: UNLOCKED_CURRENCY + tests: + - not_null + - name: CONTRACT_ADDRESS + description: "" + tests: + - not_null + - dbt_expectations.expect_column_values_to_match_regex: + regex: terra[0-9a-zA-Z]{39,40} + - name: CONTRACT_LABEL + description: "" + tests: + - not_null \ No newline at end of file diff --git a/models/terra/mirror__gov_staking.sql b/models/terra/mirror__gov_staking.sql new file mode 100644 index 00000000..8a5f1a5e --- /dev/null +++ b/models/terra/mirror__gov_staking.sql @@ -0,0 +1,188 @@ +{{ config( + materialized = 'incremental', + unique_key = 'block_id || tx_id || msg_index ', + incremental_strategy = 'delete+insert', + cluster_by = ['block_timestamp', 'block_id','msg_index'], + tags = ['snowflake', 'terra', 'mirror', 'mirror_gov'] +) }} + +WITH prices AS ( + + SELECT + DATE_TRUNC( + 'hour', + block_timestamp + ) AS HOUR, + currency, + symbol, + AVG(price_usd) AS price + FROM + {{ ref('terra__oracle_prices') }} + WHERE + 1 = 1 + +{% if is_incremental() %} +AND block_timestamp :: DATE >= ( + SELECT + MAX( + block_timestamp :: DATE + ) + FROM + {{ ref('silver_terra__msgs') }} +) +{% endif %} +GROUP BY + 1, + 2, + 3 +), +stake_msgs AS ( + SELECT + t.blockchain, + chain_id, + block_id, + block_timestamp, + t.tx_id, + t.msg_index, + msg_value :sender :: STRING AS sender, + msg_value :execute_msg :send :amount / pow( + 10, + 6 + ) AS event_amount, + event_amount * o.price AS event_amount_usd, + msg_value :contract :: STRING AS event_currency, + msg_value :execute_msg :send :contract :: STRING AS contract_address, + l.address_name AS contract_label + FROM + {{ ref('silver_terra__msgs') }} + t + LEFT OUTER JOIN prices o + ON DATE_TRUNC( + 'hour', + t.block_timestamp + ) = o.hour + AND msg_value :contract :: STRING = o.currency + LEFT OUTER JOIN {{ source( + 'shared', + 'udm_address_labels_new' + ) }} AS l + ON msg_value :execute_msg :send :contract :: STRING = l.address + WHERE + msg_value :execute_msg :send :msg :stake_voting_tokens IS NOT NULL + AND msg_value :execute_msg :send :contract :: STRING = 'terra1wh39swv7nq36pnefnupttm2nr96kz7jjddyt2x' + AND tx_status = 'SUCCEEDED' + +{% if is_incremental() %} +AND block_timestamp :: DATE >= ( + SELECT + MAX( + block_timestamp :: DATE + ) + FROM + {{ ref('silver_terra__msgs') }} +) +{% endif %} +), +stake_events AS ( + SELECT + tx_id, + msg_index, + event_attributes :share :: FLOAT AS shares + FROM + {{ ref('silver_terra__msg_events') }} + WHERE + tx_id IN( + SELECT + DISTINCT tx_id + FROM + stake_msgs + ) + AND event_type = 'from_contract' + +{% if is_incremental() %} +AND block_timestamp :: DATE >= ( + SELECT + MAX( + block_timestamp :: DATE + ) + FROM + {{ ref('silver_terra__msgs') }} +) +{% endif %} +) -- Staking +SELECT + blockchain, + chain_id, + block_id, + block_timestamp, + m.tx_id, + e.msg_index, + 'stake' AS event_type, + sender, + event_amount, + event_amount_usd, + event_currency, + shares, + contract_address, + contract_label +FROM + stake_msgs m + JOIN stake_events e + ON m.tx_id = e.tx_id + AND m.msg_index = e.msg_index +UNION + -- Unstaking +SELECT + t.blockchain, + t.chain_id, + t.block_id, + t.block_timestamp, + t.tx_id, + t.msg_index, + 'unstake' AS event_type, + msg_value :sender :: STRING AS sender, + CASE + WHEN msg_value :execute_msg :withdraw_voting_tokens :amount IS NULL THEN q.event_attributes :"0_amount" + ELSE msg_value :execute_msg :withdraw_voting_tokens :amount + END / pow( + 10, + 6 + ) AS event_amount, + event_amount * o.price AS event_amount_usd, + 'terra15gwkyepfc6xgca5t5zefzwy42uts8l2m4g40k6' AS event_currency, + NULL AS shares, + msg_value :contract :: STRING AS contract_address, + l.address_name AS contract_label +FROM + {{ ref('silver_terra__msgs') }} + t + LEFT JOIN {{ ref('silver_terra__msg_events') }} + q + ON t.tx_id = q.tx_id + AND q.event_type = 'from_contract' + LEFT OUTER JOIN prices o + ON DATE_TRUNC( + 'hour', + t.block_timestamp + ) = o.hour + AND 'terra15gwkyepfc6xgca5t5zefzwy42uts8l2m4g40k6' = o.currency + LEFT OUTER JOIN {{ source( + 'shared', + 'udm_address_labels_new' + ) }} AS l + ON msg_value :contract :: STRING = l.address +WHERE + msg_value :execute_msg :withdraw_voting_tokens IS NOT NULL + AND msg_value :contract :: STRING = 'terra1wh39swv7nq36pnefnupttm2nr96kz7jjddyt2x' + AND t.tx_status = 'SUCCEEDED' + +{% if is_incremental() %} +AND block_timestamp :: DATE >= ( + SELECT + MAX( + block_timestamp :: DATE + ) + FROM + {{ ref('silver_terra__msgs') }} +) +{% endif %} diff --git a/models/terra/mirror__gov_staking.yml b/models/terra/mirror__gov_staking.yml new file mode 100644 index 00000000..b656741b --- /dev/null +++ b/models/terra/mirror__gov_staking.yml @@ -0,0 +1,81 @@ +version: 2 +models: + - name: mirror__gov_staking + description: Mirror GOV STAKING + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - MSG_INDEX + - TX_ID + - BLOCK_ID + columns: + - name: BLOCKCHAIN + description: "" + tests: + - not_null + - name: CHAIN_ID + description: "" + tests: + - not_null + - name: BLOCK_ID + description: "" + tests: + - not_null + - name: BLOCK_TIMESTAMP + description: "" + tests: + - not_null + - name: TX_ID + description: "" + tests: + - not_null + - name: EVENT_TYPE + tests: + - not_null + - name: SENDER + description: "" + tests: + - not_null + - dbt_expectations.expect_column_values_to_match_regex: + regex: terra[0-9a-zA-Z]{39,40} + - name: EVENT_AMOUNT + description: "" + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: EVENT_AMOUNT_USD + description: "" + tests: + - not_null #failing due to missing usd info + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: EVENT_CURRENCY + description: "" + tests: + - not_null + - dbt_expectations.expect_column_values_to_match_regex: + regex: terra[0-9a-zA-Z]{39,40} + - name: SHARES + description: "" + tests: + - not_null: + where: EVENT_TYPE = 'STAKE' + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: CONTRACT_ADDRESS + description: "" + tests: + - not_null + - dbt_expectations.expect_column_values_to_match_regex: + regex: terra[0-9a-zA-Z]{39,40} + - name: CONTRACT_LABEL + description: "" + tests: + - not_null \ No newline at end of file diff --git a/models/terra/mirror__gov_submit_proposal.sql b/models/terra/mirror__gov_submit_proposal.sql new file mode 100644 index 00000000..bb3100b6 --- /dev/null +++ b/models/terra/mirror__gov_submit_proposal.sql @@ -0,0 +1,97 @@ +{{ config( + materialized = 'incremental', + unique_key = 'block_id || tx_id', + incremental_strategy = 'delete+insert', + cluster_by = ['block_timestamp', 'block_id'], + tags = ['snowflake', 'terra', 'mirror', 'mirror_gov'] +) }} + +WITH msgs AS ( + + SELECT + blockchain, + chain_id, + block_id, + block_timestamp, + tx_id, + msg_value :sender :: STRING AS creator, + msg_value :execute_msg :send :amount / pow( + 10, + 6 + ) AS amount, + msg_value :execute_msg :send :msg :create_poll :title :: STRING AS title, + msg_value :execute_msg :send :msg :create_poll :link :: STRING AS link, + msg_value :execute_msg :send :msg :create_poll :description :: STRING AS description, + msg_value :execute_msg :send :msg :create_poll :execute_msg :msg AS msg, + msg_value :execute_msg :send :contract :: STRING AS contract_address + FROM + {{ ref('silver_terra__msgs') }} + WHERE + msg_value :execute_msg :send :contract :: STRING = 'terra1wh39swv7nq36pnefnupttm2nr96kz7jjddyt2x' -- MIR Governance + AND tx_status = 'SUCCEEDED' + +{% if is_incremental() %} +AND block_timestamp :: DATE >= ( + SELECT + MAX( + block_timestamp :: DATE + ) + FROM + {{ ref('silver_terra__msgs') }} +) +{% endif %} +), +events AS ( + SELECT + tx_id, + COALESCE(TO_TIMESTAMP(event_attributes :end_time), event_attributes :end_height) AS end_time, + event_attributes :poll_id :: NUMBER AS poll_id + FROM + {{ ref('silver_terra__msg_events') }} + WHERE + tx_id IN( + SELECT + tx_id + FROM + msgs + ) + AND event_attributes :"0_action" :: STRING = 'send' + AND event_type = 'from_contract' + AND poll_id IS NOT NULL + +{% if is_incremental() %} +AND block_timestamp :: DATE >= ( + SELECT + MAX( + block_timestamp :: DATE + ) + FROM + {{ ref('silver_terra__msgs') }} +) +{% endif %} +) +SELECT + m.blockchain, + chain_id, + block_id, + block_timestamp, + m.tx_id, + poll_id, + end_time, + creator, + amount, + title, + link, + description, + msg, + contract_address, + l.address_name AS contract_label +FROM + msgs m + JOIN events e + ON m.tx_id = e.tx_id + LEFT OUTER JOIN {{ source( + 'shared', + 'udm_address_labels_new' + ) }} AS l + ON contract_address = l.address diff --git a/models/terra/mirror__gov_submit_proposal.yml b/models/terra/mirror__gov_submit_proposal.yml new file mode 100644 index 00000000..79908c41 --- /dev/null +++ b/models/terra/mirror__gov_submit_proposal.yml @@ -0,0 +1,82 @@ +version: 2 +models: + - name: mirror__gov_submit_proposal + description: Mirror GOV SUBMIT PROPOSAL + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - TX_ID + - BLOCK_ID + columns: + - name: BLOCKCHAIN + description: "" + tests: + - not_null + - name: CHAIN_ID + description: "" + tests: + - not_null + - name: BLOCK_ID + description: "" + tests: + - not_null + - name: BLOCK_TIMESTAMP + description: "" + tests: + - not_null + - name: TX_ID + description: "" + tests: + - not_null + - name: POLL_ID + description: "" + tests: + - unique + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - NUMBER + - name: END_TIME + description: "" + tests: + - not_null + - name: CREATOR + description: "" + tests: + - not_null + - dbt_expectations.expect_column_values_to_match_regex: + regex: terra[0-9a-zA-Z]{39,40} + - name: AMOUNT + description: "" + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: TITLE + description: "" + tests: + - not_null + - name: LINK + description: "" + tests: + - not_null + - name: DESCRIPTION + description: "" + tests: + - not_null + - name: MSG + description: "" + tests: + - not_null + - name: CONTRACT_ADDRESS + description: "" + tests: + - not_null + - dbt_expectations.expect_column_values_to_match_regex: + regex: terra[0-9a-zA-Z]{39,40} + - name: CONTRACT_LABEL + description: "" + tests: + - not_null \ No newline at end of file diff --git a/models/terra/mirror__gov_vote.sql b/models/terra/mirror__gov_vote.sql new file mode 100644 index 00000000..cbeeb77f --- /dev/null +++ b/models/terra/mirror__gov_vote.sql @@ -0,0 +1,32 @@ +{{ config( + materialized = 'incremental', + unique_key = 'block_id || tx_id', + incremental_strategy = 'delete+insert', + cluster_by = ['block_timestamp', 'block_id'], + tags=['snowflake', 'terra', 'mirror', 'mirror_gov'] +) }} + +SELECT + m.blockchain, + chain_id, + block_id, + block_timestamp, + tx_id, + msg_value:sender::string as voter, + msg_value:execute_msg:cast_vote:poll_id::number as poll_id, + msg_value:execute_msg:cast_vote:vote::string as vote, + msg_value:execute_msg:cast_vote:amount / POW(10,6) as balance, + msg_value:contract::string as contract_address, + l.address_name as contract_label +FROM {{ref('silver_terra__msgs')}} m + +LEFT OUTER JOIN {{source('shared','udm_address_labels_new')}} as l +ON msg_value:contract::string = l.address + +WHERE msg_value:contract::string = 'terra1wh39swv7nq36pnefnupttm2nr96kz7jjddyt2x' -- MIR Governance + AND msg_value:execute_msg:cast_vote IS NOT NULL + AND tx_status = 'SUCCEEDED' + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} diff --git a/models/terra/mirror__gov_vote.yml b/models/terra/mirror__gov_vote.yml new file mode 100644 index 00000000..66dd09a8 --- /dev/null +++ b/models/terra/mirror__gov_vote.yml @@ -0,0 +1,67 @@ +version: 2 +models: + - name: mirror__gov_vote + description: Mirror GOV VOTE + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - TX_ID + - BLOCK_ID + columns: + - name: BLOCKCHAIN + description: "" + tests: + - not_null + - name: CHAIN_ID + description: "" + tests: + - not_null + - name: BLOCK_ID + description: "" + tests: + - not_null + - name: BLOCK_TIMESTAMP + description: "" + tests: + - not_null + - name: TX_ID + description: "" + tests: + - not_null + - name: VOTER + description: "" + tests: + - not_null + - dbt_expectations.expect_column_values_to_match_regex: + regex: terra[0-9a-zA-Z]{39,40} + - name: POLL_ID + description: "" + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - number + - name: VOTE + description: "" + tests: + - not_null + - accepted_values: + values: ['yes','no','abstain'] + - name: BALANCE + description: "" + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: CONTRACT_ADDRESS + description: "" + tests: + - not_null + - dbt_expectations.expect_column_values_to_match_regex: + regex: terra[0-9a-zA-Z]{39,40} + - name: CONTRACT_LABEL + description: "" + tests: + - not_null \ No newline at end of file diff --git a/models/terra/mirror__liquidations.sql b/models/terra/mirror__liquidations.sql new file mode 100644 index 00000000..2b4fb91b --- /dev/null +++ b/models/terra/mirror__liquidations.sql @@ -0,0 +1,132 @@ +{{ config( + materialized = 'incremental', + unique_key = 'block_id || tx_id', + incremental_strategy = 'delete+insert', + cluster_by = ['block_timestamp', 'block_id'], + tags=['snowflake', 'terra', 'mirror', 'liquidations'] +) }} + +WITH prices AS ( + + SELECT + date_trunc('hour', block_timestamp) as hour, + currency, + symbol, + avg(price_usd) as price + FROM {{ ref('terra__oracle_prices')}} + + WHERE 1=1 + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} + + GROUP BY 1,2,3 + +), + +msgs AS ( + +SELECT + blockchain, + chain_id, + block_id, + block_timestamp, + tx_id, + msg_value:sender::string as sender, + msg_value:execute_msg:send:msg:auction:position_idx as collateral_id, + msg_value:execute_msg:send:contract::string as contract_address +FROM {{ref('silver_terra__msgs')}} +WHERE msg_value:execute_msg:send:msg:auction IS NOT NULL + AND tx_status = 'SUCCEEDED' + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} +), + +events AS ( + +SELECT + tx_id, + COALESCE((event_attributes:"0_tax_amount"[0]:amount + event_attributes:"1_tax_amount"[0]:amount), event_attributes:tax_amount[0]:amount) / POW(10,6) AS tax_amount, + COALESCE(event_attributes:"1_tax_amount"[0]:denom::string,event_attributes:tax_amount[0]:denom::string) AS tax_currency, + + event_attributes:protocol_fee[0]:amount / POW(10,6) as protocol_fee_amount, + event_attributes:protocol_fee[0]:denom::string as protocol_fee_currency, + + event_attributes:liquidated_amount[0]:amount / POW(10,6) as liquidated_amount, + event_attributes:liquidated_amount[0]:denom::string as liquidated_currency, + + event_attributes:return_collateral_amount[0]:amount / POW(10,6) as return_collateral_amount, + event_attributes:return_collateral_amount[0]:denom::string as return_collateral_currency, + + event_attributes:unlocked_amount[0]:amount / POW(10,6) as unlocked_amount, + event_attributes:unlocked_amount[0]:denom::string as unlocked_currency, + + event_attributes:owner::string as owner +FROM {{ref('silver_terra__msg_events')}} +WHERE event_type = 'from_contract' + AND tx_id IN(SELECT tx_id FROM msgs) + AND event_attributes:return_collateral_amount IS NOT NULL + AND tx_status = 'SUCCEEDED' + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} + +) + +SELECT + m.blockchain, + chain_id, + block_id, + block_timestamp, + m.tx_id, + collateral_id, + sender as buyer, + owner, + tax_amount, + tax_amount * t.price as tax_amount_usd, + tax_currency, + protocol_fee_amount, + protocol_fee_amount * p.price as protocol_fee_amount_usd, + protocol_fee_currency, + liquidated_amount, + liquidated_amount * l.price as liquidated_amount_usd, + liquidated_currency, + return_collateral_amount, + return_collateral_amount * r.price as return_collateral_amount_usd, + return_collateral_currency, + unlocked_amount, + unlocked_amount * u.price as unlocked_amount_usd, + unlocked_currency, + contract_address, + g.address_name AS contract_label +FROM msgs m + +JOIN events e + ON m.tx_id = e.tx_id + +LEFT OUTER JOIN {{source('shared','udm_address_labels_new')}} as g +ON m.contract_address = g.address + +LEFT OUTER JOIN prices t + ON date_trunc('hour', m.block_timestamp) = t.hour + AND tax_currency = t.currency + +LEFT OUTER JOIN prices p + ON date_trunc('hour', m.block_timestamp) = p.hour + AND protocol_fee_currency = p.currency + +LEFT OUTER JOIN prices l + ON date_trunc('hour', m.block_timestamp) = l.hour + AND liquidated_currency = l.currency + +LEFT OUTER JOIN prices r + ON date_trunc('hour', m.block_timestamp) = r.hour + AND return_collateral_currency = r.currency + +LEFT OUTER JOIN prices u + ON date_trunc('hour', m.block_timestamp) = u.hour + AND unlocked_currency = u.currency \ No newline at end of file diff --git a/models/terra/mirror__liquidations.yml b/models/terra/mirror__liquidations.yml new file mode 100644 index 00000000..a2883e6b --- /dev/null +++ b/models/terra/mirror__liquidations.yml @@ -0,0 +1,152 @@ +version: 2 +models: + - name: mirror__liquidations + description: Mirror LIQUIDATIONS + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - BLOCK_ID + - TX_ID + columns: + - name: BLOCKCHAIN + description: "" + tests: + - not_null + - name: CHAIN_ID + description: "" + tests: + - not_null + - name: BLOCK_ID + description: "" + tests: + - not_null + - name: BLOCK_TIMESTAMP + description: "" + tests: + - not_null + - dbt_expectations.expect_row_values_to_have_recent_data: + datepart: day + interval: 1 + - name: TX_ID + description: "" + tests: + - not_null + - name: COLLATERAL_ID + description: "" + tests: + - not_null + - name: BUYER + description: "" + tests: + - not_null + - dbt_expectations.expect_column_values_to_match_regex: + regex: terra[0-9a-zA-Z]{39,40} + - name: OWNER + description: "" + tests: + - not_null + - dbt_expectations.expect_column_values_to_match_regex: + regex: terra[0-9a-zA-Z]{39,40} + - name: TAX_AMOUNT + description: "" + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: TAX_AMOUNT_USD + description: "" + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: TAX_CURRENCY + description: "" + tests: + - not_null + - name: PROTOCOL_FEE_AMOUNT + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: PROTOCOL_FEE_AMOUNT_USD + description: "" + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: PROTOCOL_FEE_CURRENCY + description: "" + tests: + - not_null + - name: LIQUIDATED_AMOUNT + description: "" + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: LIQUIDATED_AMOUNT_USD + description: "" + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: LIQUIDATED_CURRENCY + description: "" + tests: + - not_null + - name: RETURN_COLLATERAL_AMOUNT + description: "" + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: RETURN_COLLATERAL_AMOUNT_USD + description: "" + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: RETURN_COLLATERAL_CURRENCY + description: "" + tests: + - not_null + - name: UNLOCKED_AMOUNT + tests: + - not_null: + where: UNLOCKED_CURRENCY <> NULL + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: UNLOCKED_AMOUNT_USD + tests: + - not_null: + where: UNLOCKED_CURRENCY <> NULL + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: UNLOCKED_CURRENCY + description: "" + tests: + - name: CONTRACT_ADDRESS + description: "" + tests: + - not_null + - dbt_expectations.expect_column_values_to_match_regex: + regex: terra[0-9a-zA-Z]{39,40} + - name: CONTRACT_LABEL + description: "" + tests: + - not_null \ No newline at end of file diff --git a/models/terra/mirror__open_collateral.sql b/models/terra/mirror__open_collateral.sql new file mode 100644 index 00000000..0827b70e --- /dev/null +++ b/models/terra/mirror__open_collateral.sql @@ -0,0 +1,107 @@ +{{ config( + materialized = 'incremental', + unique_key = 'block_id || tx_id', + incremental_strategy = 'delete+insert', + cluster_by = ['block_timestamp', 'block_id'], + tags=['snowflake', 'terra', 'mirror', 'collateral'] +) }} + + +WITH prices AS ( + + SELECT + date_trunc('hour', block_timestamp) as hour, + currency, + symbol, + avg(price_usd) as price + FROM {{ ref('terra__oracle_prices')}} + + WHERE 1=1 + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} + + GROUP BY 1,2,3 + +), + +msgs AS( + +SELECT + m.blockchain, + chain_id, + block_id, + block_timestamp, + tx_id, + msg_value:execute_msg:open_position:collateral_ratio as collateral_ratio, + msg_value:sender::string as sender, + msg_value:contract::string as contract_address, + l.address_name as contract_label +FROM {{ref('silver_terra__msgs')}} m + +LEFT OUTER JOIN {{source('shared','udm_address_labels_new')}} as l +ON msg_value:contract::string = l.address + +WHERE msg_value:contract::string = 'terra1wfz7h3aqf4cjmjcvc6s8lxdhh7k30nkczyf0mj' -- Mirror Mint Contract + AND msg_value:execute_msg:open_position IS NOT NULL + AND tx_status = 'SUCCEEDED' + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} + +), + +events AS( + +SELECT + tx_id, + event_attributes:position_idx as collateral_id, + event_attributes:collateral_amount[0]:amount/ POW(10,6) as collateral_amount, + collateral_amount * o.price AS collateral_amount_usd, + event_attributes:collateral_amount[0]:denom::string as collateral_currency, + event_attributes:mint_amount[0]:amount/ POW(10,6) as mint_amount, + mint_amount * i.price AS mint_amount_usd, + event_attributes:mint_amount[0]:denom::string as mint_currency +FROM {{ref('silver_terra__msg_events')}} t + +LEFT OUTER JOIN prices o + ON date_trunc('hour', t.block_timestamp) = o.hour + AND t.event_attributes:collateral_amount[0]:denom::string = o.currency + +LEFT OUTER JOIN prices i + ON date_trunc('hour', t.block_timestamp) = i.hour + AND t.event_attributes:mint_amount[0]:denom::string = i.currency + +WHERE t.event_attributes:is_short::string = 'false' + AND tx_id IN(SELECT DISTINCT tx_id FROM msgs) + AND event_type = 'from_contract' + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} + +) + +SELECT + blockchain, + chain_id, + block_id, + block_timestamp, + m.tx_id, + collateral_id, + collateral_ratio, + sender, + collateral_amount, + collateral_amount_usd, + collateral_currency, + mint_amount, + mint_amount_usd, + mint_currency, + contract_address, + contract_label +FROM msgs m + +JOIN events e + ON m.tx_id = e.tx_id \ No newline at end of file diff --git a/models/terra/mirror__open_collateral.yml b/models/terra/mirror__open_collateral.yml new file mode 100644 index 00000000..44bbcd59 --- /dev/null +++ b/models/terra/mirror__open_collateral.yml @@ -0,0 +1,99 @@ +version: 2 +models: + - name: mirror__open_collateral + description: Mirror OPEN COLLATERAL + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - BLOCK_ID + - TX_ID + - COLLATERAL_ID + columns: + - name: BLOCKCHAIN + description: "" + tests: + - not_null + - name: CHAIN_ID + description: "" + tests: + - not_null + - name: BLOCK_ID + description: "" + tests: + - not_null + - name: BLOCK_TIMESTAMP + description: "" + tests: + - not_null + - dbt_expectations.expect_row_values_to_have_recent_data: + datepart: day + interval: 1 + - name: TX_ID + description: "" + tests: + - not_null + - name: COLLATERAL_ID + description: "" + tests: + - unique + - not_null + - name: COLLATERAL_RATIO + description: "" + tests: + - not_null + - name: SENDER + description: "" + tests: + - not_null + - dbt_expectations.expect_column_values_to_match_regex: + regex: terra[0-9a-zA-Z]{39,40} + - name: COLLATERAL_AMOUNT + tests: + - not_null: + where: COLLATERAL_CURRENCY <> NULL + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: COLLATERAL_AMOUNT_USD + tests: + - not_null: + where: COLLATERAL_CURRENCY <> NULL + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: COLLATERAL_CURRENCY + description: "" + tests: + - not_null + - name: MINT_AMOUNT + tests: + - not_null: + where: MINT_CURRENCY <> NULL + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: MINT_AMOUNT_USD + tests: + - not_null: + where: MINT_CURRENCY <> NULL + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: MINT_CURRENCY + description: "" + tests: + - not_null + - name: CONTRACT_ADDRESS + description: "" + tests: + - not_null + - dbt_expectations.expect_column_values_to_match_regex: + regex: terra[0-9a-zA-Z]{39,40} + - name: CONTRACT_LABEL + description: "" + tests: + - not_null \ No newline at end of file diff --git a/models/terra/mirror__open_short_farm.sql b/models/terra/mirror__open_short_farm.sql new file mode 100644 index 00000000..865d2740 --- /dev/null +++ b/models/terra/mirror__open_short_farm.sql @@ -0,0 +1,141 @@ +{{ config( + materialized = 'incremental', + unique_key = 'block_id || tx_id', + incremental_strategy = 'delete+insert', + cluster_by = ['block_timestamp', 'block_id'], + tags=['snowflake', 'terra', 'mirror', 'short_farm'] +) }} + +WITH prices AS ( + + SELECT + date_trunc('hour', block_timestamp) as hour, + currency, + symbol, + avg(price_usd) as price + FROM {{ ref('terra__oracle_prices')}} + + WHERE 1=1 + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} + + GROUP BY 1,2,3 + +), + +msgs as( + +SELECT + m.blockchain, + chain_id, + block_id, + block_timestamp, + tx_id, + msg_value:sender::string AS sender, + msg_value:execute_msg:open_position:collateral_ratio AS collateral_ratio, + msg_value:contract::string AS contract_address, + l.address_name AS contract_label +FROM {{ref('silver_terra__msgs')}} m + +LEFT OUTER JOIN {{source('shared','udm_address_labels_new')}} as l +ON msg_value:contract::string = l.address + +WHERE msg_value:contract::string = 'terra1wfz7h3aqf4cjmjcvc6s8lxdhh7k30nkczyf0mj' --Mirror Mint + AND msg_value:execute_msg:open_position IS NOT NULL + AND tx_status = 'SUCCEEDED' + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} + +), + +events as ( + +SELECT + tx_id, + event_attributes:"0_position_idx" as collateral_id, + + event_attributes:collateral_amount[0]:amount / POW(10,6) AS collateral_amount, + collateral_amount * o.price AS collateral_amount_usd, + event_attributes:collateral_amount[0]:denom::string AS collateral_currency, + + event_attributes:mint_amount[0]:amount / POW(10,6) AS mint_amount, + mint_amount * i.price AS mint_amount_usd, + event_attributes:mint_amount[0]:denom::string AS mint_currency, + + event_attributes:return_amount / POW(10,6) AS return_amount, + return_amount * r.price AS return_amount_usd, + 'uusd' AS return_currency, + + event_attributes:locked_amount[0]:amount / POW(10,6) AS locked_amount, + locked_amount * l.price AS locked_amount_usd, + event_attributes:locked_amount[0]:denom::string AS locked_currency, + + event_attributes:tax_amount / POW(10,6) AS tax_amount, + event_attributes:commission_amount / POW(10,6) AS commission_amount, + event_attributes:spread_amount / POW(10,6) AS spread_amount, + + to_timestamp(event_attributes:unlock_time::numeric) AS unlock_time +FROM {{ref('silver_terra__msg_events')}} t + +LEFT OUTER JOIN prices o + ON date_trunc('hour', t.block_timestamp) = o.hour + AND t.event_attributes:collateral_amount[0]:denom::string = o.currency + +LEFT OUTER JOIN prices i + ON date_trunc('hour', t.block_timestamp) = i.hour + AND t.event_attributes:mint_amount[0]:denom::string = i.currency + +LEFT OUTER JOIN prices r + ON date_trunc('hour', t.block_timestamp) = r.hour + AND 'uusd' = r.currency + +LEFT OUTER JOIN prices l + ON date_trunc('hour', t.block_timestamp) = l.hour + AND t.event_attributes:locked_amount[0]:denom::string = l.currency + +WHERE event_type = 'from_contract' + AND event_attributes:is_short::string = 'true' + AND event_attributes:from::string = 'terra1wfz7h3aqf4cjmjcvc6s8lxdhh7k30nkczyf0mj' -- Mirror Mint + AND tx_status = 'SUCCEEDED' + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} + +) + +SELECT + blockchain, + chain_id, + block_id, + block_timestamp, + m.tx_id, + collateral_id, + collateral_ratio, + sender, + tax_amount, + commission_amount, + spread_amount, + collateral_amount, + collateral_amount_usd, + collateral_currency, + mint_amount, + mint_amount_usd, + mint_currency, + return_amount, + return_amount_usd, + return_currency, + locked_amount, + locked_amount_usd, + locked_currency, + unlock_time, + contract_address, + contract_label +FROM msgs m + +JOIN events e + ON m.tx_id = e.tx_id diff --git a/models/terra/mirror__open_short_farm.yml b/models/terra/mirror__open_short_farm.yml new file mode 100644 index 00000000..9a74d1b4 --- /dev/null +++ b/models/terra/mirror__open_short_farm.yml @@ -0,0 +1,161 @@ +version: 2 +models: + - name: mirror__open_short_farm + description: Mirror OPEN SHORT FARM + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - BLOCK_ID + - TX_ID + columns: + - name: BLOCKCHAIN + description: "" + tests: + - not_null + - name: CHAIN_ID + description: "" + tests: + - not_null + - name: BLOCK_ID + description: "" + tests: + - not_null + - name: BLOCK_TIMESTAMP + description: "" + tests: + - not_null + - dbt_expectations.expect_row_values_to_have_recent_data: + datepart: day + interval: 1 + - name: TX_ID + description: "" + tests: + - not_null + - name: COLLATERAL_ID + description: "" + tests: + - not_null + - name: COLLATERAL_RATIO + description: "" + tests: + - not_null + - name: SENDER + description: "" + tests: + - not_null + - dbt_expectations.expect_column_values_to_match_regex: + regex: terra[0-9a-zA-Z]{39,40} + - name: TAX_AMOUNT + description: "" + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: COMMISSION_AMOUNT + description: "" + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: SPREAD_AMOUNT + description: "" + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: COLLATERAL_AMOUNT + description: "" + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: COLLATERAL_AMOUNT_USD + description: "" + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: COLLATERAL_CURRENCY + description: "" + tests: + - not_null + - name: MINT_AMOUNT + description: "" + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: MINT_AMOUNT_USD + description: "" + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: MINT_CURRENCY + description: "" + tests: + - not_null + - name: RETURN_AMOUNT + description: "" + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: RETURN_AMOUNT_USD + description: "" + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: RETURN_CURRENCY + description: "" + tests: + - not_null + - name: LOCKED_AMOUNT + description: "" + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: LOCKED_AMOUNT_USD + description: "" + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: LOCKED_CURRENCY + description: "" + tests: + - not_null + - name: UNLOCK_TIME + description: "" + tests: + - not_null + - name: CONTRACT_ADDRESS + description: "" + tests: + - not_null + - dbt_expectations.expect_column_values_to_match_regex: + regex: terra[0-9a-zA-Z]{39,40} + - name: CONTRACT_LABEL + description: "" + tests: + - not_null \ No newline at end of file diff --git a/models/terra/mirror__reward_claims.sql b/models/terra/mirror__reward_claims.sql new file mode 100644 index 00000000..201add57 --- /dev/null +++ b/models/terra/mirror__reward_claims.sql @@ -0,0 +1,155 @@ +{{ config( + materialized = 'incremental', + unique_key = 'block_id || tx_id', + incremental_strategy = 'delete+insert', + cluster_by = ['block_timestamp', 'block_id'], + tags = ['snowflake', 'terra', 'mirror', 'reward_claims'] +) }} + +WITH prices AS ( + + SELECT + DATE_TRUNC( + 'hour', + block_timestamp + ) AS HOUR, + currency, + symbol, + AVG(price_usd) AS price + FROM + {{ ref('terra__oracle_prices') }} + WHERE + 1 = 1 + +{% if is_incremental() %} +AND block_timestamp :: DATE >= ( + SELECT + MAX( + block_timestamp :: DATE + ) + FROM + {{ ref('silver_terra__msgs') }} +) +{% endif %} +GROUP BY + 1, + 2, + 3 +), +prices_backup AS ( + SELECT + DATE_TRUNC( + 'day', + block_timestamp + ) AS DAY, + currency, + symbol, + AVG(price_usd) AS price + FROM + {{ ref('terra__oracle_prices') }} + WHERE + 1 = 1 + +{% if is_incremental() %} +AND block_timestamp :: DATE >= ( + SELECT + MAX( + block_timestamp :: DATE + ) + FROM + {{ ref('silver_terra__msgs') }} +) +{% endif %} +GROUP BY + 1, + 2, + 3 +), +msgs AS ( + SELECT + blockchain, + chain_id, + block_id, + block_timestamp, + tx_id, + msg_value :sender :: STRING AS sender, + msg_value :contract :: STRING AS contract_address + FROM + terra.msgs + WHERE + msg_value :execute_msg :withdraw_voting_rewards IS NOT NULL + +{% if is_incremental() %} +AND block_timestamp :: DATE >= ( + SELECT + MAX( + block_timestamp :: DATE + ) + FROM + {{ ref('silver_terra__msgs') }} +) +{% endif %} +), +events AS ( + SELECT + m.tx_id, + event_attributes :"1_amount" / pow( + 10, + 6 + ) AS claim_amount, + event_attributes :"1_contract_address" :: STRING AS claim_currency + FROM + {{ ref('silver_terra__msg_events') }} + e + JOIN msgs m + ON m.tx_id = e.tx_id + WHERE + event_attributes :"0_action" = 'withdraw' + +{% if is_incremental() %} +AND e.block_timestamp :: DATE >= ( + SELECT + MAX( + block_timestamp :: DATE + ) + FROM + {{ ref('silver_terra__msgs') }} +) +{% endif %} +) +SELECT + m.blockchain, + chain_id, + block_id, + block_timestamp, + m.tx_id, + sender, + claim_amount, + claim_amount * COALESCE( + p.price, + pb.price + ) AS claim_amount_usd, + claim_currency, + contract_address, + l.address_name AS contract_label +FROM + msgs m + JOIN events e + ON m.tx_id = e.tx_id + LEFT OUTER JOIN {{ source( + 'shared', + 'udm_address_labels_new' + ) }} AS l + ON contract_address = l.address + LEFT OUTER JOIN prices p + ON DATE_TRUNC( + 'hour', + m.block_timestamp + ) = p.hour + AND claim_currency = p.currency + LEFT OUTER JOIN prices_backup pb + ON DATE_TRUNC( + 'day', + m.block_timestamp + ) = pb.day + AND claim_currency = pb.currency diff --git a/models/terra/mirror__reward_claims.yml b/models/terra/mirror__reward_claims.yml new file mode 100644 index 00000000..062bd039 --- /dev/null +++ b/models/terra/mirror__reward_claims.yml @@ -0,0 +1,55 @@ +version: 2 +models: + - name: mirror__reward_claims + description: Mirror REWARD CLAIMS + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - TX_ID + - BLOCK_ID + columns: + - name: BLOCKCHAIN + description: "" + tests: + - not_null + - name: CHAIN_ID + description: "" + tests: + - not_null + - name: BLOCK_ID + description: "" + tests: + - not_null + - name: BLOCK_TIMESTAMP + description: "" + tests: + - not_null + - name: TX_ID + description: "" + tests: + - not_null + - name: SENDER + description: "" + tests: + - not_null + - dbt_expectations.expect_column_values_to_match_regex: + regex: terra[0-9a-zA-Z]{39,40} + - name: CLAIM_AMOUNT + tests: + - not_null + - name: CLAIM_AMOUNT_USD + tests: + - not_null + - name: CLAIM_CURRENCY + tests: + - not_null + - name: CONTRACT_ADDRESS + description: "" + tests: + - not_null + - dbt_expectations.expect_column_values_to_match_regex: + regex: terra[0-9a-zA-Z]{39,40} + - name: CONTRACT_LABEL + description: "" + tests: + - not_null \ No newline at end of file diff --git a/models/terra/terra__airdrop_claims.sql b/models/terra/terra__airdrop_claims.sql new file mode 100644 index 00000000..ee56ae3d --- /dev/null +++ b/models/terra/terra__airdrop_claims.sql @@ -0,0 +1,43 @@ +{{ config( + materialized = 'incremental', + unique_key = 'block_id || tx_id', + incremental_strategy = 'delete+insert', + cluster_by = ['block_timestamp', 'block_id'], + tags = ['snowflake', 'terra', 'airdrops', 'claims'] +) }} + +SELECT + t.blockchain, + chain_id, + block_id, + block_timestamp, + tx_id, + msg_value :execute_msg :claim :stage :: NUMBER AS airdrop_id, + msg_value :sender :: STRING AS claimer, + msg_value :execute_msg :claim :amount / pow( + 10, + 6 + ) AS amount, + msg_value :contract :: STRING AS contract_address, + l.address_name AS contract_label +FROM + {{ ref('silver_terra__msgs') }} + LEFT OUTER JOIN {{ source( + 'shared', + 'udm_address_labels_new' + ) }} AS l + ON msg_value :contract :: STRING = l.address +WHERE + msg_value :execute_msg :claim :amount IS NOT NULL + AND tx_status = 'SUCCEEDED' + +{% if is_incremental() %} +AND block_timestamp :: DATE >= ( + SELECT + MAX( + block_timestamp :: DATE + ) + FROM + {{ ref('silver_terra__msgs') }} +) +{% endif %} diff --git a/models/terra/terra__airdrop_claims.yml b/models/terra/terra__airdrop_claims.yml new file mode 100644 index 00000000..47458073 --- /dev/null +++ b/models/terra/terra__airdrop_claims.yml @@ -0,0 +1,55 @@ +version: 2 +models: + - name: terra__airdrop_claims + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - BLOCK_ID + - TX_ID + - AIRDROP_ID + - CONTRACT_ADDRESS + columns: + - name: BLOCKCHAIN + tests: + - not_null + - name: BLOCK_ID + tests: + - not_null + - name: BLOCK_TIMESTAMP + tests: + - not_null + - dbt_expectations.expect_row_values_to_have_recent_data: + datepart: day + interval: 1 + - name: CHAIN_ID + tests: + - not_null + - name: CLAIMER + tests: + - not_null + - name: AMOUNT + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: CONTRACT_ADDRESS + tests: + - not_null + - dbt_expectations.expect_column_values_to_match_regex: + regex: terra[0-9a-zA-Z]{39,40} + - name: CONTRACT_LABEL + tests: + - not_null: + where: CONTRACT_ADDRESS = 'terra1kalp2knjm4cs3f59ukr4hdhuuncp648eqrgshw' + - name: TX_ID + tests: + - not_null + - name: AIRDROP_ID + tests: + - not_null: + where: CONTRACT_ADDRESS NOT IN ('terra1s2yuugawj98gkpy6h9ua7ppss94sqgw2r7tyma', 'terra1268e62h8r0fcr2nt0kplxp8jx5qalwq73a5fuk') + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - number \ No newline at end of file diff --git a/models/terra/terra__staking.sql b/models/terra/terra__staking.sql index c4105d06..3d96bcb3 100644 --- a/models/terra/terra__staking.sql +++ b/models/terra/terra__staking.sql @@ -57,7 +57,7 @@ redelegate AS ( prices AS ( SELECT date_trunc('hour', block_timestamp) as hour, - currency, + currency,1 symbol, avg(price_usd) as price_usd FROM {{ ref('terra__oracle_prices')}} diff --git a/models/terra/terra__transfers.sql b/models/terra/terra__transfers.sql index 0e45bc15..6554c641 100644 --- a/models/terra/terra__transfers.sql +++ b/models/terra/terra__transfers.sql @@ -129,6 +129,30 @@ transfers AS( AND block_timestamp >= getdate() - INTERVAL '1 days' -- {% else %} -- AND block_timestamp >= getdate() - interval '9 months' {% endif %} + +UNION + +SELECT + blockchain, + chain_id, + tx_status, + block_id, + block_timestamp, + tx_id, + msg_type, + msg_value:sender::string as event_from, + msg_value:execute_msg:transfer:recipient::string as event_to, + msg_value:execute_msg:transfer:amount / pow(10,6) as event_amount, + msg_value:contract::string as event_currency +FROM {{ref('silver_terra__msgs')}} +WHERE msg_value:execute_msg:transfer IS NOT NULL + +{% if is_incremental() %} + AND block_timestamp >= getdate() - interval '1 days' +-- {% else %} +-- AND block_timestamp >= getdate() - interval '9 months' +{% endif %} + ) SELECT t.blockchain, diff --git a/models/terra/terraswap__lp_actions.sql b/models/terra/terraswap__lp_actions.sql new file mode 100644 index 00000000..c53dde7c --- /dev/null +++ b/models/terra/terraswap__lp_actions.sql @@ -0,0 +1,213 @@ +{{ config( + materialized = 'incremental', + unique_key = 'block_id || tx_id', + incremental_strategy = 'delete+insert', + cluster_by = ['block_timestamp', 'block_id'], + tags=['snowflake', 'terra', 'terraswap', 'terraswap_lp_actions'] +) }} + +WITH prices as ( + + SELECT + date_trunc('hour', block_timestamp) as hour, + currency, + symbol, + avg(price_usd) as price + FROM {{ ref('terra__oracle_prices')}} + + WHERE 1=1 + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} + + GROUP BY 1,2,3 + +), + +provide_msgs AS ( + +SELECT + m.blockchain, + chain_id, + block_id, + block_timestamp, + tx_id, + 'provide_liquidity' as event_type, + msg_value:sender::string as sender, + msg_value:contract::string as pool_address, + l.address_name AS pool_name +FROM {{ref('silver_terra__msgs')}} m + +LEFT OUTER JOIN {{source('shared','udm_address_labels_new')}} as l +ON msg_value:contract::string = l.address + +WHERE msg_value:execute_msg:provide_liquidity IS NOT NULL + AND tx_status = 'SUCCEEDED' + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} + +), + +provide_events AS ( +SELECT + tx_id, + + event_attributes:assets[0]:amount / POW(10,6) AS token_0_amount, + token_0_amount * o.price AS token_0_amount_usd, + event_attributes:assets[0]:denom::string AS token_0_currency, + + event_attributes:assets[1]:amount / POW(10,6) AS token_1_amount, + token_1_amount * i.price AS token_1_amount_usd, + event_attributes:assets[1]:denom::string AS token_1_currency, + + event_attributes:share / POW(10,6) AS lp_share_amount, + CASE + WHEN event_attributes:"2_contract_address"::string = 'terra17yap3mhph35pcwvhza38c2lkj7gzywzy05h7l0' THEN event_attributes:"4_contract_address"::string + ELSE event_attributes:"2_contract_address"::string + END AS lp_pool_address, + l.address_name AS lp_pool_name +FROM {{ref('silver_terra__msg_events')}} t + +LEFT OUTER JOIN {{source('shared','udm_address_labels_new')}} as l +ON event_attributes:"2_contract_address"::string = l.address + +LEFT OUTER JOIN {{source('shared','udm_address_labels_new')}} as r +ON event_attributes:"4_contract_address"::string = r.address + +LEFT OUTER JOIN prices o + ON date_trunc('hour', t.block_timestamp) = o.hour + AND t.event_attributes:assets[0]:denom::string = o.currency + +LEFT OUTER JOIN prices i + ON date_trunc('hour', t.block_timestamp) = i.hour + AND t.event_attributes:assets[1]:denom::string = i.currency + +WHERE event_attributes:assets IS NOT NULL + AND tx_id IN(SELECT DISTINCT tx_id FROM provide_msgs) + AND event_type = 'from_contract' + + {% if is_incremental() %} + AND t.block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} + +), + +withdraw_msgs AS ( + +SELECT + m.blockchain, + chain_id, + block_id, + block_timestamp, + tx_id, + 'withdraw_liquidity' as event_type, + msg_value:sender::string as sender, + msg_value:contract::string as lp_pool_address, + l.address_name AS lp_pool_name, + msg_value:execute_msg:send:contract::string as pool_address, + p.address_name AS pool_name +FROM {{ref('silver_terra__msgs')}} m + +LEFT OUTER JOIN {{source('shared','udm_address_labels_new')}} as p +ON msg_value:contract::string = p.address + +LEFT OUTER JOIN {{source('shared','udm_address_labels_new')}} as l +ON msg_value:execute_msg:send:contract::string = l.address + +WHERE msg_value:execute_msg:send:msg:withdraw_liquidity IS NOT NULL + AND tx_status = 'SUCCEEDED' + + {% if is_incremental() %} + AND m.block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} + +), + +withdraw_events AS ( + +SELECT + tx_id, + + event_attributes:refund_assets[0]:amount / POW(10,6) AS token_0_amount, + token_0_amount * o.price AS token_0_amount_usd, + event_attributes:refund_assets[0]:denom::string AS token_0_currency, + + event_attributes:refund_assets[1]:amount / POW(10,6) AS token_1_amount, + token_1_amount * i.price AS token_1_amount_usd, + event_attributes:refund_assets[1]:denom::string AS token_1_currency, + + event_attributes:withdrawn_share / POW(10,6) as lp_share_amount +FROM {{ref('silver_terra__msg_events')}} t + +LEFT OUTER JOIN prices o + ON date_trunc('hour', t.block_timestamp) = o.hour + AND t.event_attributes:refund_assets[0]:denom::string = o.currency + +LEFT OUTER JOIN prices i + ON date_trunc('hour', t.block_timestamp) = i.hour + AND t.event_attributes:refund_assets[1]:denom::string = i.currency + +WHERE tx_id IN(SELECT tx_id FROM withdraw_msgs) + AND event_type = 'from_contract' + AND event_attributes:refund_assets IS NOT NULL + + {% if is_incremental() %} + AND t.block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} + +) + +-- Provide Liquidity +SELECT + blockchain, + chain_id, + block_id, + block_timestamp, + m.tx_id, + event_type, + sender, + token_0_amount, + token_0_amount_usd, + token_0_currency, + token_1_amount, + token_1_amount_usd, + token_1_currency, + pool_address, + pool_name, + lp_share_amount, + lp_pool_address, + lp_pool_name +FROM provide_msgs m + +JOIN provide_events e + ON m.tx_id = e.tx_id + +UNION + +-- Remove Liquidity +SELECT + blockchain, + chain_id, + block_id, + block_timestamp, + w.tx_id, + event_type, + sender, + token_0_amount, + token_0_amount_usd, + token_0_currency, + token_1_amount, + token_1_amount_usd, + token_1_currency, + pool_address, + pool_name, + lp_share_amount, + lp_pool_address, + lp_pool_name +FROM withdraw_msgs w + +JOIN withdraw_events we + ON w.tx_id = we.tx_id \ No newline at end of file diff --git a/models/terra/terraswap__lp_stake.sql b/models/terra/terraswap__lp_stake.sql new file mode 100644 index 00000000..37e6806a --- /dev/null +++ b/models/terra/terraswap__lp_stake.sql @@ -0,0 +1,91 @@ +{{ config( + materialized = 'incremental', + unique_key = 'block_id || tx_id', + incremental_strategy = 'delete+insert', + cluster_by = ['block_timestamp', 'block_id'], + tags=['snowflake', 'terra', 'terraswap', 'lp'] +) }} + +-- LP Un-staking +WITH msgs AS ( + +SELECT + blockchain, + chain_id, + block_id, + block_timestamp, + tx_id, + 'unstake_lp' as event_type, + msg_value:sender::string as sender, + msg_value:execute_msg:unbond:amount / POW(10,6) as amount +FROM {{ref('silver_terra__msgs')}} +WHERE msg_value:execute_msg:unbond IS NOT NULL + AND tx_status = 'SUCCEEDED' + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} + +), + +events AS ( + +SELECT + tx_id, + event_attributes:"0_contract_address"::string as contract_address +FROM {{ref('silver_terra__msg_events')}} +where tx_id IN(SELECT distinct tx_id from msgs) + AND event_type = 'execute_contract' + AND msg_index = 0 + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} + +) + +-- unstake +SELECT + m.blockchain, + chain_id, + block_id, + block_timestamp, + m.tx_id, + event_type, + sender, + amount, + contract_address, + address_name as contract_label +FROM msgs m + +JOIN events e + ON m.tx_id = e.tx_id + +LEFT OUTER JOIN {{source('shared','udm_address_labels_new')}} + ON contract_address = address + +UNION + +-- stake +SELECT + m.blockchain, + chain_id, + block_id, + block_timestamp, + tx_id, + 'stake_lp' as event_type, + msg_value:sender::string as sender, + msg_value:execute_msg:send:amount / POW(10,6) as amount, + msg_value:contract::string as contract_address, + address_name as contract_label +FROM {{ref('silver_terra__msgs')}} m + +LEFT OUTER JOIN {{source('shared','udm_address_labels_new')}} + ON msg_value:contract::string = address + +WHERE msg_value:execute_msg:send:msg:bond IS NOT NULL + AND tx_status = 'SUCCEEDED' + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} \ No newline at end of file diff --git a/models/terra/terraswap__swaps.sql b/models/terra/terraswap__swaps.sql new file mode 100644 index 00000000..36f27c97 --- /dev/null +++ b/models/terra/terraswap__swaps.sql @@ -0,0 +1,122 @@ +{{ config( + materialized = 'incremental', + unique_key = 'block_id || tx_id', + incremental_strategy = 'delete+insert', + cluster_by = ['block_timestamp', 'block_id'], + tags=['snowflake', 'terra', 'terraswap', 'swap'] +) }} + + +WITH prices AS ( + + SELECT + date_trunc('hour', block_timestamp) as hour, + currency, + symbol, + avg(price_usd) as price + FROM {{ ref('terra__oracle_prices')}} + + WHERE 1=1 + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} + + GROUP BY 1,2,3 + +), + +msgs as ( +-- native to non-native/native +SELECT + blockchain, + chain_id, + block_id, + block_timestamp, + tx_id, + msg_value:sender::string as sender, + msg_value:contract::string as pool_address +FROM {{ref('silver_terra__msgs')}} +WHERE msg_value:execute_msg:swap IS NOT NULL + AND tx_status = 'SUCCEEDED' + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} + + + UNION + +-- non-native to native +SELECT + blockchain, + chain_id, + block_id, + block_timestamp, + tx_id, + msg_value:sender::string as sender, + msg_value:execute_msg:send:contract::string as pool_address +FROM {{ref('silver_terra__msgs')}} + +WHERE msg_value:execute_msg:send:msg:swap IS NOT NULL + AND tx_status = 'SUCCEEDED' + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} +), + +events as ( +SELECT + tx_id, + as_number(event_attributes:tax_amount) / POW(10,6) as tax_amount, + event_attributes:commission_amount::numeric / POW(10,6) as commission_amount, + event_attributes:offer_amount::numeric / POW(10,6) as offer_amount, + event_attributes:offer_asset::string as offer_currency, + event_attributes:return_amount::numeric / POW(10,6) as return_amount, + event_attributes:ask_asset::string as return_currency +FROM {{ref('silver_terra__msg_events')}} + +WHERE event_type = 'from_contract' + AND tx_id IN(SELECT DISTINCT tx_id + FROM msgs ) + AND event_attributes:offer_amount IS NOT NULL + + {% if is_incremental() %} + AND block_timestamp::date >= (select max(block_timestamp::date) from {{ref('silver_terra__msgs')}}) + {% endif %} +) + + +SELECT + m.blockchain, + chain_id, + block_id, + block_timestamp, + m.tx_id, + sender, + tax_amount, + commission_amount, + offer_amount, + offer_amount * o.price AS offer_amount_usd, + offer_currency, + return_amount, + return_amount * r.price AS return_amount_usd, + return_currency, + pool_address, + l.address_name AS pool_name +FROM msgs m + +JOIN events e + ON m.tx_id = e.tx_id + +LEFT OUTER JOIN prices o + ON date_trunc('hour', m.block_timestamp) = o.hour + AND e.offer_currency = o.currency + +LEFT OUTER JOIN prices r + ON date_trunc('hour', m.block_timestamp) = r.hour + AND e.return_currency = r.currency + +LEFT OUTER JOIN {{source('shared','udm_address_labels_new')}} l + ON pool_address = address \ No newline at end of file diff --git a/packages.yml b/packages.yml index 1c4f8e44..bffcceea 100644 --- a/packages.yml +++ b/packages.yml @@ -1,3 +1,3 @@ packages: - - git: "https://github.com/calogica/dbt-expectations.git" - revision: 0.4.2 + - package: calogica/dbt_expectations + version: [">=0.4.0", "<0.5.0"] diff --git a/tests/terra/mirror__gov_submit_proposal__poll_id-assert_no_gap.sql b/tests/terra/mirror__gov_submit_proposal__poll_id-assert_no_gap.sql new file mode 100644 index 00000000..fefeeed0 --- /dev/null +++ b/tests/terra/mirror__gov_submit_proposal__poll_id-assert_no_gap.sql @@ -0,0 +1 @@ +{{ sequence_gaps(ref("mirror__gov_submit_proposal"), [], "poll_id") }}