From ef8063b4f563b572b07ae2a5a318503f73329fa2 Mon Sep 17 00:00:00 2001 From: Jake Loo <2171134+jakeloo@users.noreply.github.com> Date: Fri, 12 Sep 2025 16:19:14 +0000 Subject: [PATCH 1/2] Update mview schema --- ...09_clickhouse_create_token_balances_mv.sql | 37 ++++++++++++------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/internal/tools/clickhouse/0009_clickhouse_create_token_balances_mv.sql b/internal/tools/clickhouse/0009_clickhouse_create_token_balances_mv.sql index 63e523e1..933f6d4a 100644 --- a/internal/tools/clickhouse/0009_clickhouse_create_token_balances_mv.sql +++ b/internal/tools/clickhouse/0009_clickhouse_create_token_balances_mv.sql @@ -1,8 +1,7 @@ -- ERC20 -CREATE MATERIALIZED VIEW IF NOT EXISTS token_balances_erc20_mv +CREATE MATERIALIZED VIEW IF NOT EXISTS token_balances_erc20_from_mv TO token_balances AS --- FROM side (outgoing, negative delta) SELECT chain_id, token_type, @@ -19,8 +18,11 @@ SELECT insert_timestamp, is_deleted FROM token_transfers -WHERE token_type = 'erc20' -UNION ALL +WHERE token_type = 'erc20'; + +CREATE MATERIALIZED VIEW IF NOT EXISTS token_balances_erc20_to_mv +TO token_balances +AS -- TO side (incoming, positive delta) SELECT chain_id, @@ -41,7 +43,7 @@ FROM token_transfers WHERE token_type = 'erc20'; -- ERC721 -CREATE MATERIALIZED VIEW IF NOT EXISTS token_balances_erc721_mv +CREATE MATERIALIZED VIEW IF NOT EXISTS token_balances_erc721_from_mv TO token_balances AS SELECT @@ -60,8 +62,11 @@ SELECT insert_timestamp, is_deleted FROM token_transfers -WHERE token_type = 'erc721' -UNION ALL +WHERE token_type = 'erc721'; + +CREATE MATERIALIZED VIEW IF NOT EXISTS token_balances_erc721_to_mv +TO token_balances +AS SELECT chain_id, token_type, @@ -81,7 +86,7 @@ FROM token_transfers WHERE token_type = 'erc721'; -- ERC1155 -CREATE MATERIALIZED VIEW IF NOT EXISTS token_balances_erc1155_mv +CREATE MATERIALIZED VIEW IF NOT EXISTS token_balances_erc1155_from_mv TO token_balances AS SELECT @@ -100,8 +105,11 @@ SELECT insert_timestamp, is_deleted FROM token_transfers -WHERE token_type = 'erc1155' -UNION ALL +WHERE token_type = 'erc1155'; + +CREATE MATERIALIZED VIEW IF NOT EXISTS token_balances_erc1155_to_mv +TO token_balances +AS SELECT chain_id, token_type, @@ -121,7 +129,7 @@ FROM token_transfers WHERE token_type = 'erc1155'; -- ERC6909 -CREATE MATERIALIZED VIEW IF NOT EXISTS token_balances_erc6909_mv +CREATE MATERIALIZED VIEW IF NOT EXISTS token_balances_erc6909_from_mv TO token_balances AS SELECT @@ -140,8 +148,11 @@ SELECT insert_timestamp, is_deleted FROM token_transfers -WHERE token_type = 'erc6909' -UNION ALL +WHERE token_type = 'erc6909'; + +CREATE MATERIALIZED VIEW IF NOT EXISTS token_balances_erc6909_to_mv +TO token_balances +AS SELECT chain_id, token_type, From 8cc72f377c28028b28d65159527a5274852e3d52 Mon Sep 17 00:00:00 2001 From: Jake Loo <2171134+jakeloo@users.noreply.github.com> Date: Fri, 12 Sep 2025 16:28:16 +0000 Subject: [PATCH 2/2] Fix projection --- .../0008_clickhouse_create_token_balances.sql | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/internal/tools/clickhouse/0008_clickhouse_create_token_balances.sql b/internal/tools/clickhouse/0008_clickhouse_create_token_balances.sql index 49444f1c..522d12fd 100644 --- a/internal/tools/clickhouse/0008_clickhouse_create_token_balances.sql +++ b/internal/tools/clickhouse/0008_clickhouse_create_token_balances.sql @@ -31,11 +31,11 @@ CREATE TABLE IF NOT EXISTS token_balances owner_address, token_address, token_id, - sumState(balance_delta * if(is_deleted = 0, 1, -1)) AS balance_state, - minState(block_number) AS min_block_number_state, - minState(block_timestamp) AS min_block_timestamp_state, - maxState(block_number) AS max_block_number_state, - maxState(block_timestamp) AS max_block_timestamp_state + sum(balance_delta * if(is_deleted = 0, 1, -1)) AS balance_state, + min(block_number) AS min_block_number_state, + min(block_timestamp) AS min_block_timestamp_state, + max(block_number) AS max_block_number_state, + max(block_timestamp) AS max_block_timestamp_state GROUP BY chain_id, owner_address, token_address, token_id ), @@ -46,11 +46,11 @@ CREATE TABLE IF NOT EXISTS token_balances token_address, token_id, owner_address, - sumState(balance_delta * if(is_deleted = 0, 1, -1)) AS balance_state, - minState(block_number) AS min_block_number_state, - minState(block_timestamp) AS min_block_timestamp_state, - maxState(block_number) AS max_block_number_state, - maxState(block_timestamp) AS max_block_timestamp_state + sum(balance_delta * if(is_deleted = 0, 1, -1)) AS balance_state, + min(block_number) AS min_block_number_state, + min(block_timestamp) AS min_block_timestamp_state, + max(block_number) AS max_block_number_state, + max(block_timestamp) AS max_block_timestamp_state GROUP BY chain_id, token_address, token_id, owner_address ),