Merge pull request #1048 from poanetwork/gsf-token-balance-error-address-does-not-exist

import addresses when importing token balances
pull/1063/head
Gustavo 6 years ago committed by GitHub
commit 67565e6927
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      apps/indexer/lib/indexer/token_balance/fetcher.ex
  2. 38
      apps/indexer/test/indexer/token_balance/fetcher_test.exs

@ -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,

@ -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

Loading…
Cancel
Save