1+ CREATE TABLE IF NOT EXISTS token_balances
2+ (
3+ ` token_type` String,
4+ ` chain_id` UInt256,
5+ ` owner` FixedString(42 ),
6+ ` address` FixedString(42 ),
7+ ` token_id` UInt256,
8+ ` balance` Int256
9+ )
10+ ENGINE = SummingMergeTree
11+ ORDER BY (token_type, chain_id, owner, address, token_id);
12+
13+ CREATE MATERIALIZED VIEW IF NOT EXISTS single_token_transfers_mv TO token_balances AS
14+ SELECT chain_id, owner, address, token_type, token_id, sum (amount) as balance
15+ FROM
16+ (
17+ SELECT
18+ chain_id,
19+ address,
20+ (topic_0 = ' 0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef' AND topic_3 = ' ' ) as is_erc20,
21+ (topic_0 = ' 0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef' AND topic_3 != ' ' ) as is_erc721,
22+ (topic_0 = ' 0xc3d58168c5ae7397731d063d5bbf3d657854427343f4c083240f7aacaa2d0f62' ) as is_erc1155,
23+ if(is_erc1155, concat(' 0x' , substring (topic_2, 27 , 40 )), concat(' 0x' , substring (topic_1, 27 , 40 ))) AS sender_address,
24+ if(is_erc1155, concat(' 0x' , substring (topic_3, 27 , 40 )), concat(' 0x' , substring (topic_2, 27 , 40 ))) AS receiver_address,
25+ multiIf(is_erc20, ' erc20' , is_erc721, ' erc721' , ' erc1155' ) as token_type,
26+ multiIf(
27+ is_erc1155,
28+ reinterpretAsUInt256(reverse(unhex(substring (data, 3 , 64 )))),
29+ is_erc721,
30+ reinterpretAsUInt256(reverse(unhex(substring (topic_3, 3 , 64 )))),
31+ toUInt256(0 ) -- other
32+ ) AS token_id,
33+ multiIf(
34+ is_erc20 AND length(data) = 66 ,
35+ reinterpretAsInt256(reverse(unhex(substring (data, 3 )))),
36+ is_erc721 OR is_erc1155,
37+ toInt256(1 ),
38+ toInt256(0 ) -- unknown
39+ ) as transfer_amount,
40+ (sign * transfer_amount) as amount
41+ FROM logs
42+ WHERE
43+ topic_0 IN (
44+ ' 0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef' ,
45+ ' 0xc3d58168c5ae7397731d063d5bbf3d657854427343f4c083240f7aacaa2d0f62'
46+ )
47+ )
48+ array join
49+ [chain_id, chain_id] AS chain_id,
50+ [sender_address, receiver_address] AS owner,
51+ [- amount, amount] as amount,
52+ [token_type, token_type] AS token_type,
53+ [token_id, token_id] AS token_id,
54+ [address, address] AS address
55+ GROUP BY chain_id, owner, address, token_type, token_id;
56+
57+ CREATE MATERIALIZED VIEW IF NOT EXISTS erc1155_batch_token_transfers_mv TO token_balances AS
58+ SELECT chain_id, owner, address, token_type, token_id, sum (amount) as balance
59+ FROM (
60+ WITH
61+ metadata as (
62+ SELECT
63+ * ,
64+ 3 + 2 * 64 as ids_length_idx,
65+ ids_length_idx + 64 as ids_values_idx,
66+ reinterpretAsUInt64(reverse(unhex(substring (data, ids_length_idx, 64 )))) AS ids_length,
67+ ids_length_idx + 64 + (ids_length * 64 ) as amounts_length_idx,
68+ reinterpretAsUInt64(reverse(unhex(substring (data, amounts_length_idx, 64 )))) AS amounts_length,
69+ amounts_length_idx + 64 as amounts_values_idx
70+ FROM logs
71+ WHERE topic_0 = ' 0x4a39dc06d4c0dbc64b70af90fd698a233a518aa5d07e595d983b8c0526c8f7fb'
72+ ),
73+ decoded AS (
74+ SELECT
75+ * ,
76+ arrayMap(
77+ x - > substring (data, ids_values_idx + (x - 1 ) * 64 , 64 ),
78+ range(1 , ids_length + 1 )
79+ ) AS ids_hex,
80+ arrayMap(
81+ x - > substring (data, amounts_values_idx + (x - 1 ) * 64 , 64 ),
82+ range(1 , amounts_length + 1 )
83+ ) AS amounts_hex
84+ FROM metadata
85+ )
86+ SELECT
87+ chain_id,
88+ address,
89+ concat(' 0x' , substring (topic_2, 27 , 40 )) AS sender_address,
90+ concat(' 0x' , substring (topic_3, 27 , 40 )) AS receiver_address,
91+ ' erc1155' as token_type,
92+ reinterpretAsUInt256(reverse(unhex(substring (hex_id, 1 , 64 )))) AS token_id,
93+ reinterpretAsInt256(reverse(unhex(substring (hex_amount, 1 , 64 )))) AS transfer_amount,
94+ (sign * transfer_amount) as amount
95+ FROM decoded
96+ ARRAY JOIN ids_hex AS hex_id, amounts_hex AS hex_amount
97+ )
98+ array join
99+ [chain_id, chain_id] AS chain_id,
100+ [sender_address, receiver_address] AS owner,
101+ [- amount, amount] as amount,
102+ [token_type, token_type] AS token_type,
103+ [token_id, token_id] AS token_id,
104+ [address, address] AS address
105+ GROUP BY chain_id, owner, address, token_type, token_id;
0 commit comments