Skip to content

Commit 6cf8a40

Browse files
committed
Merge branch 'main' into np/refactoring
2 parents a3d958a + f66f782 commit 6cf8a40

File tree

4 files changed

+48
-28
lines changed

4 files changed

+48
-28
lines changed

internal/tools/clickhouse/0002_clickhouse_create_logs_table.sql

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,23 @@ CREATE TABLE IF NOT EXISTS logs (
3737
transaction_index,
3838
log_index
3939
),
40-
PROJECTION chain_topic0_projection
40+
PROJECTION chain_address_block_number_full_projection
41+
(
42+
SELECT
43+
*
44+
ORDER BY
45+
chain_id,
46+
address,
47+
block_number
48+
),
49+
PROJECTION chain_topic0_full_projection
4150
(
4251
SELECT
43-
_part_offset
52+
*
4453
ORDER BY
4554
chain_id,
4655
topic_0,
4756
block_number,
48-
transaction_index,
49-
log_index,
5057
address
5158
),
5259
PROJECTION address_topic0_state_projection

internal/tools/clickhouse/0006_clickhouse_create_token_transfers.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ CREATE TABLE IF NOT EXISTS token_transfers
1717
`insert_timestamp` DateTime DEFAULT now(),
1818
`is_deleted` UInt8 DEFAULT 0,
1919

20+
INDEX idx_block_number block_number TYPE minmax GRANULARITY 1,
2021
INDEX idx_block_timestamp block_timestamp TYPE minmax GRANULARITY 1,
2122
INDEX idx_from_address from_address TYPE bloom_filter GRANULARITY 3,
2223
INDEX idx_to_address to_address TYPE bloom_filter GRANULARITY 3,

internal/tools/clickhouse/0008_clickhouse_create_token_balances.sql

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ CREATE TABLE IF NOT EXISTS token_balances
2020
`insert_timestamp` DateTime DEFAULT now(),
2121
`is_deleted` UInt8 DEFAULT 0,
2222

23+
INDEX idx_block_number block_number TYPE minmax GRANULARITY 1,
2324
INDEX idx_block_timestamp block_timestamp TYPE minmax GRANULARITY 1,
2425
INDEX idx_token_address token_address TYPE bloom_filter GRANULARITY 3,
2526
INDEX idx_owner_address owner_address TYPE bloom_filter GRANULARITY 3,
@@ -31,11 +32,11 @@ CREATE TABLE IF NOT EXISTS token_balances
3132
owner_address,
3233
token_address,
3334
token_id,
34-
sumState(balance_delta * if(is_deleted = 0, 1, -1)) AS balance_state,
35-
minState(block_number) AS min_block_number_state,
36-
minState(block_timestamp) AS min_block_timestamp_state,
37-
maxState(block_number) AS max_block_number_state,
38-
maxState(block_timestamp) AS max_block_timestamp_state
35+
sum(balance_delta * if(is_deleted = 0, 1, -1)) AS balance_state,
36+
min(block_number) AS min_block_number_state,
37+
min(block_timestamp) AS min_block_timestamp_state,
38+
max(block_number) AS max_block_number_state,
39+
max(block_timestamp) AS max_block_timestamp_state
3940
GROUP BY chain_id, owner_address, token_address, token_id
4041
),
4142

@@ -46,11 +47,11 @@ CREATE TABLE IF NOT EXISTS token_balances
4647
token_address,
4748
token_id,
4849
owner_address,
49-
sumState(balance_delta * if(is_deleted = 0, 1, -1)) AS balance_state,
50-
minState(block_number) AS min_block_number_state,
51-
minState(block_timestamp) AS min_block_timestamp_state,
52-
maxState(block_number) AS max_block_number_state,
53-
maxState(block_timestamp) AS max_block_timestamp_state
50+
sum(balance_delta * if(is_deleted = 0, 1, -1)) AS balance_state,
51+
min(block_number) AS min_block_number_state,
52+
min(block_timestamp) AS min_block_timestamp_state,
53+
max(block_number) AS max_block_number_state,
54+
max(block_timestamp) AS max_block_timestamp_state
5455
GROUP BY chain_id, token_address, token_id, owner_address
5556
),
5657

@@ -62,6 +63,6 @@ CREATE TABLE IF NOT EXISTS token_balances
6263
)
6364
)
6465
ENGINE = ReplacingMergeTree(insert_timestamp, is_deleted)
65-
PARTITION BY chain_id
66+
PARTITION BY (chain_id, toStartOfQuarter(block_timestamp))
6667
ORDER BY (chain_id, owner_address, token_address, token_id, block_number, transaction_index, log_index, direction)
6768
SETTINGS index_granularity = 8192, lightweight_mutation_projection_mode = 'rebuild', deduplicate_merge_projection_mode = 'rebuild', allow_part_offset_column_in_projections=1;

internal/tools/clickhouse/0009_clickhouse_create_token_balances_mv.sql

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
-- ERC20
2-
CREATE MATERIALIZED VIEW IF NOT EXISTS token_balances_erc20_mv
2+
CREATE MATERIALIZED VIEW IF NOT EXISTS token_balances_erc20_from_mv
33
TO token_balances
44
AS
5-
-- FROM side (outgoing, negative delta)
65
SELECT
76
chain_id,
87
token_type,
@@ -19,8 +18,11 @@ SELECT
1918
insert_timestamp,
2019
is_deleted
2120
FROM token_transfers
22-
WHERE token_type = 'erc20'
23-
UNION ALL
21+
WHERE token_type = 'erc20';
22+
23+
CREATE MATERIALIZED VIEW IF NOT EXISTS token_balances_erc20_to_mv
24+
TO token_balances
25+
AS
2426
-- TO side (incoming, positive delta)
2527
SELECT
2628
chain_id,
@@ -41,7 +43,7 @@ FROM token_transfers
4143
WHERE token_type = 'erc20';
4244

4345
-- ERC721
44-
CREATE MATERIALIZED VIEW IF NOT EXISTS token_balances_erc721_mv
46+
CREATE MATERIALIZED VIEW IF NOT EXISTS token_balances_erc721_from_mv
4547
TO token_balances
4648
AS
4749
SELECT
@@ -60,8 +62,11 @@ SELECT
6062
insert_timestamp,
6163
is_deleted
6264
FROM token_transfers
63-
WHERE token_type = 'erc721'
64-
UNION ALL
65+
WHERE token_type = 'erc721';
66+
67+
CREATE MATERIALIZED VIEW IF NOT EXISTS token_balances_erc721_to_mv
68+
TO token_balances
69+
AS
6570
SELECT
6671
chain_id,
6772
token_type,
@@ -81,7 +86,7 @@ FROM token_transfers
8186
WHERE token_type = 'erc721';
8287

8388
-- ERC1155
84-
CREATE MATERIALIZED VIEW IF NOT EXISTS token_balances_erc1155_mv
89+
CREATE MATERIALIZED VIEW IF NOT EXISTS token_balances_erc1155_from_mv
8590
TO token_balances
8691
AS
8792
SELECT
@@ -100,8 +105,11 @@ SELECT
100105
insert_timestamp,
101106
is_deleted
102107
FROM token_transfers
103-
WHERE token_type = 'erc1155'
104-
UNION ALL
108+
WHERE token_type = 'erc1155';
109+
110+
CREATE MATERIALIZED VIEW IF NOT EXISTS token_balances_erc1155_to_mv
111+
TO token_balances
112+
AS
105113
SELECT
106114
chain_id,
107115
token_type,
@@ -121,7 +129,7 @@ FROM token_transfers
121129
WHERE token_type = 'erc1155';
122130

123131
-- ERC6909
124-
CREATE MATERIALIZED VIEW IF NOT EXISTS token_balances_erc6909_mv
132+
CREATE MATERIALIZED VIEW IF NOT EXISTS token_balances_erc6909_from_mv
125133
TO token_balances
126134
AS
127135
SELECT
@@ -140,8 +148,11 @@ SELECT
140148
insert_timestamp,
141149
is_deleted
142150
FROM token_transfers
143-
WHERE token_type = 'erc6909'
144-
UNION ALL
151+
WHERE token_type = 'erc6909';
152+
153+
CREATE MATERIALIZED VIEW IF NOT EXISTS token_balances_erc6909_to_mv
154+
TO token_balances
155+
AS
145156
SELECT
146157
chain_id,
147158
token_type,

0 commit comments

Comments
 (0)