diff --git a/apps/indexer/lib/indexer/token_balance/fetcher.ex b/apps/indexer/lib/indexer/token_balance/fetcher.ex index f8cf8e65a6..426d94f4b2 100644 --- a/apps/indexer/lib/indexer/token_balance/fetcher.ex +++ b/apps/indexer/lib/indexer/token_balance/fetcher.ex @@ -80,7 +80,10 @@ defmodule Indexer.TokenBalance.Fetcher do end def import_token_balances(token_balances_params) do + addresses_params = format_and_filter_address_params(token_balances_params) + import_params = %{ + addresses: %{params: addresses_params}, address_token_balances: %{params: token_balances_params}, address_current_token_balances: %{params: token_balances_params}, timeout: :infinity @@ -97,6 +100,12 @@ defmodule Indexer.TokenBalance.Fetcher do end end + defp format_and_filter_address_params(token_balances_params) do + token_balances_params + |> Enum.map(&%{hash: &1.address_hash}) + |> Enum.uniq() + end + defp entry(%{ token_contract_address_hash: token_contract_address_hash, address_hash: address_hash, diff --git a/apps/indexer/test/indexer/token_balance/fetcher_test.exs b/apps/indexer/test/indexer/token_balance/fetcher_test.exs index d8346bbff4..a34bd9c01e 100644 --- a/apps/indexer/test/indexer/token_balance/fetcher_test.exs +++ b/apps/indexer/test/indexer/token_balance/fetcher_test.exs @@ -87,5 +87,43 @@ defmodule Indexer.TokenBalance.FetcherTest do assert TokenBalance.Fetcher.import_token_balances(token_balances_params) == :error end + + test "insert the missing address, import the token balances and return :ok when the address does not exist yet" do + contract = insert(:token) + insert(:block, number: 19999) + + token_balances_params = [ + %{ + address_hash: "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", + block_number: 19999, + token_contract_address_hash: to_string(contract.contract_address_hash) + } + ] + + {:ok, address_hash} = Explorer.Chain.string_to_address_hash("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF") + assert TokenBalance.Fetcher.import_token_balances(token_balances_params) == :ok + assert {:ok, _} = Explorer.Chain.hash_to_address(address_hash) + end + + test "import the token balances and return :ok when there are multiple balances for the same address on the batch" do + contract = insert(:token) + contract2 = insert(:token) + insert(:block, number: 19999) + + token_balances_params = [ + %{ + address_hash: "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", + block_number: 19999, + token_contract_address_hash: to_string(contract.contract_address_hash) + }, + %{ + address_hash: "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", + block_number: 19999, + token_contract_address_hash: to_string(contract2.contract_address_hash) + } + ] + + assert TokenBalance.Fetcher.import_token_balances(token_balances_params) == :ok + end end end