From cc89b1f6db11c77dfd7c99ea6bafd249b99261f2 Mon Sep 17 00:00:00 2001 From: Felipe Renan Date: Fri, 28 Sep 2018 11:02:35 -0300 Subject: [PATCH] Ignore burn address token balance for Tokens ERC-721 As we always get errors trying to fetch token balances when the address is the burn addres for Tokens ERC-721, we are gonna ignore them when we are processing the token balances from the token transfer. --- .../lib/indexer/address/token_balances.ex | 37 +++++++++++++----- apps/indexer/lib/indexer/token_transfers.ex | 9 +++-- .../indexer/address/token_balances_test.exs | 38 +++++++++++++++++++ .../test/indexer/token_transfers_test.exs | 9 +++-- 4 files changed, 77 insertions(+), 16 deletions(-) diff --git a/apps/indexer/lib/indexer/address/token_balances.ex b/apps/indexer/lib/indexer/address/token_balances.ex index 830db6fc9e..4d47c0213a 100644 --- a/apps/indexer/lib/indexer/address/token_balances.ex +++ b/apps/indexer/lib/indexer/address/token_balances.ex @@ -8,16 +8,17 @@ defmodule Indexer.Address.TokenBalances do end defp reducer({:token_transfers_params, token_transfers_params}, initial) when is_list(token_transfers_params) do - Enum.reduce(token_transfers_params, initial, fn %{ - block_number: block_number, - from_address_hash: from_address_hash, - to_address_hash: to_address_hash, - token_contract_address_hash: token_contract_address_hash - }, - acc - when is_integer(block_number) and is_binary(from_address_hash) and - is_binary(to_address_hash) and - is_binary(token_contract_address_hash) -> + token_transfers_params + |> ignore_burn_address_transfers_for_token_erc_721 + |> Enum.reduce(initial, fn %{ + block_number: block_number, + from_address_hash: from_address_hash, + to_address_hash: to_address_hash, + token_contract_address_hash: token_contract_address_hash + }, + acc + when is_integer(block_number) and is_binary(from_address_hash) and + is_binary(to_address_hash) and is_binary(token_contract_address_hash) -> acc |> MapSet.put(%{ address_hash: from_address_hash, @@ -36,4 +37,20 @@ defmodule Indexer.Address.TokenBalances do }) end) end + + defp ignore_burn_address_transfers_for_token_erc_721(token_transfers_params) do + Enum.filter(token_transfers_params, &do_filter_burn_address/1) + end + + def do_filter_burn_address(%{from_address_hash: "0x0000000000000000000000000000000000000000", token_type: "ERC-721"}) do + false + end + + def do_filter_burn_address(%{to_address_hash: "0x0000000000000000000000000000000000000000", token_type: "ERC-721"}) do + false + end + + def do_filter_burn_address(_token_balance_param) do + true + end end diff --git a/apps/indexer/lib/indexer/token_transfers.ex b/apps/indexer/lib/indexer/token_transfers.ex index c7483a0d5b..87c30b1766 100644 --- a/apps/indexer/lib/indexer/token_transfers.ex +++ b/apps/indexer/lib/indexer/token_transfers.ex @@ -44,7 +44,8 @@ defmodule Indexer.TokenTransfers do from_address_hash: truncate_address_hash(log.second_topic), to_address_hash: truncate_address_hash(log.third_topic), token_contract_address_hash: log.address_hash, - transaction_hash: log.transaction_hash + transaction_hash: log.transaction_hash, + token_type: "ERC-20" } token = %{ @@ -67,7 +68,8 @@ defmodule Indexer.TokenTransfers do to_address_hash: truncate_address_hash(log.third_topic), token_contract_address_hash: log.address_hash, token_id: token_id || 0, - transaction_hash: log.transaction_hash + transaction_hash: log.transaction_hash, + token_type: "ERC-721" } token = %{ @@ -90,7 +92,8 @@ defmodule Indexer.TokenTransfers do to_address_hash: encode_address_hash(to_address_hash), token_contract_address_hash: log.address_hash, token_id: token_id, - transaction_hash: log.transaction_hash + transaction_hash: log.transaction_hash, + token_type: "ERC-721" } token = %{ diff --git a/apps/indexer/test/indexer/address/token_balances_test.exs b/apps/indexer/test/indexer/address/token_balances_test.exs index 18c2faa658..885d42dcb9 100644 --- a/apps/indexer/test/indexer/address/token_balances_test.exs +++ b/apps/indexer/test/indexer/address/token_balances_test.exs @@ -34,5 +34,43 @@ defmodule Indexer.Address.TokenBalancesTest do assert %{address_hash: to_address_hash, block_number: block_number} assert %{address_hash: token_contract_address_hash, block_number: block_number} end + + test "does not set params when the from_address_hash is the burn address for the Token ERC-721" do + block_number = 1 + from_address_hash = "0x0000000000000000000000000000000000000000" + to_address_hash = "0x5b8410f67eb8040bb1cd1e8a4ff9d5f6ce678a15" + token_contract_address_hash = "0xe18035bf8712672935fdb4e5e431b1a0183d2dfc" + + token_transfer_params = %{ + block_number: block_number, + from_address_hash: from_address_hash, + to_address_hash: to_address_hash, + token_contract_address_hash: token_contract_address_hash, + token_type: "ERC-721" + } + + params_set = TokenBalances.params_set(%{token_transfers_params: [token_transfer_params]}) + + assert MapSet.size(params_set) == 0 + end + + test "does not set params when the to_address_hash is the burn address for the Token ERC-721" do + block_number = 1 + from_address_hash = "0x5b8410f67eb8040bb1cd1e8a4ff9d5f6ce678a15" + to_address_hash = "0x0000000000000000000000000000000000000000" + token_contract_address_hash = "0xe18035bf8712672935fdb4e5e431b1a0183d2dfc" + + token_transfer_params = %{ + block_number: block_number, + from_address_hash: from_address_hash, + to_address_hash: to_address_hash, + token_contract_address_hash: token_contract_address_hash, + token_type: "ERC-721" + } + + params_set = TokenBalances.params_set(%{token_transfers_params: [token_transfer_params]}) + + assert MapSet.size(params_set) == 0 + end end end diff --git a/apps/indexer/test/indexer/token_transfers_test.exs b/apps/indexer/test/indexer/token_transfers_test.exs index 47d0dafc35..5560647f33 100644 --- a/apps/indexer/test/indexer/token_transfers_test.exs +++ b/apps/indexer/test/indexer/token_transfers_test.exs @@ -66,7 +66,8 @@ defmodule Indexer.TokenTransfersTest do to_address_hash: truncated_hash(log_3.third_topic), token_contract_address_hash: log_3.address_hash, token_id: 183, - transaction_hash: log_3.transaction_hash + transaction_hash: log_3.transaction_hash, + token_type: "ERC-721" }, %{ amount: Decimal.new(17_000_000_000_000_000_000), @@ -75,7 +76,8 @@ defmodule Indexer.TokenTransfersTest do from_address_hash: truncated_hash(log_1.second_topic), to_address_hash: truncated_hash(log_1.third_topic), token_contract_address_hash: log_1.address_hash, - transaction_hash: log_1.transaction_hash + transaction_hash: log_1.transaction_hash, + token_type: "ERC-20" } ] } @@ -113,7 +115,8 @@ defmodule Indexer.TokenTransfersTest do to_address_hash: "0xbe8cdfc13ffda20c844ac3da2b53a23ac5787f1e", token_contract_address_hash: log.address_hash, token_id: 14_939, - transaction_hash: log.transaction_hash + transaction_hash: log.transaction_hash, + token_type: "ERC-721" } ] }