Skip to content

Commit 73eeb25

Browse files
authored
fix erc1155 single transfers materialized view (#166)
### TL;DR Added support for ERC1155 token amount parsing in token balance materialized view ### What changed? Updated the token balance calculation logic to properly handle ERC1155 token amounts by parsing the data field when it contains batch transfer information (130 characters). For ERC1155 transfers, it now extracts the amount from the appropriate position in the data field, defaulting to 1 if not present. ### Why make this change? ERC1155 tokens can transfer multiple tokens in a single transaction. The previous implementation treated all ERC1155 transfers as single unit transfers, which was incorrect for batch transfers. This change ensures accurate token balance tracking for ERC1155 tokens.
2 parents 6dd120e + c387c59 commit 73eeb25

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

internal/tools/clickhouse_create_token_balances_mv.sql

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ FROM
2020
(topic_0 = '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef' AND topic_3 = '') as is_erc20,
2121
(topic_0 = '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef' AND topic_3 != '') as is_erc721,
2222
(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,
23+
if(is_erc1155, concat('0x', substring(topic_2, 27, 40)), concat('0x', substring(topic_1, 27, 40))) AS sender_address, -- ERC20 & ERC721 both have topic_1 as sender
24+
if(is_erc1155, concat('0x', substring(topic_3, 27, 40)), concat('0x', substring(topic_2, 27, 40))) AS receiver_address, -- ERC20 & ERC721 both have topic_2 as receiver
2525
multiIf(is_erc20, 'erc20', is_erc721, 'erc721', 'erc1155') as token_type,
2626
multiIf(
2727
is_erc1155,
@@ -33,10 +33,12 @@ FROM
3333
multiIf(
3434
is_erc20 AND length(data) = 66,
3535
reinterpretAsInt256(reverse(unhex(substring(data, 3)))),
36-
is_erc721 OR is_erc1155,
36+
is_erc721,
3737
toInt256(1),
38+
is_erc1155,
39+
if(length(data) = 130, reinterpretAsInt256(reverse(unhex(substring(data, 67, 64)))), toInt256(1)),
3840
toInt256(0) -- unknown
39-
) as transfer_amount,
41+
) AS transfer_amount,
4042
(sign * transfer_amount) as amount
4143
FROM logs
4244
WHERE

0 commit comments

Comments
 (0)