1155 refactoring: get_balances_of_with_abi/2

(cherry picked from commit 7445aa7558)
pull/4761/head
Viktor Baranov 4 years ago
parent d052f00e53
commit f9e6a9cb30
  1. 34
      apps/explorer/lib/explorer/token/balance_reader.ex
  2. 41
      apps/indexer/lib/indexer/token_balances.ex
  3. 8
      apps/indexer/test/indexer/token_balances_test.exs

@ -43,33 +43,31 @@ defmodule Explorer.Token.BalanceReader do
%{token_contract_address_hash: String.t(), address_hash: String.t(), block_number: non_neg_integer()} %{token_contract_address_hash: String.t(), address_hash: String.t(), block_number: non_neg_integer()}
]) :: [{:ok, non_neg_integer()} | {:error, String.t()}] ]) :: [{:ok, non_neg_integer()} | {:error, String.t()}]
def get_balances_of(token_balance_requests) do def get_balances_of(token_balance_requests) do
regular_balances =
token_balance_requests token_balance_requests
|> Enum.filter(fn request ->
if Map.has_key?(request, :token_type) do
request.token_type !== "ERC-1155"
else
true
end
end)
|> Enum.map(&format_balance_request/1) |> Enum.map(&format_balance_request/1)
|> Reader.query_contracts(@balance_function_abi) |> Reader.query_contracts(@balance_function_abi)
|> Enum.map(&format_balance_result/1) |> Enum.map(&format_balance_result/1)
end
erc1155_balances = @spec get_balances_of_with_abi(
[
%{token_contract_address_hash: String.t(), address_hash: String.t(), block_number: non_neg_integer()}
],
Map.t()
) :: [{:ok, non_neg_integer()} | {:error, String.t()}]
def get_balances_of_with_abi(token_balance_requests, abi) do
formatted_balances_requests =
if abi == @erc1155_balance_function_abi do
token_balance_requests token_balance_requests
|> Enum.filter(fn request -> |> Enum.map(&format_erc_1155_balance_request/1)
if Map.has_key?(request, :token_type) do
request.token_type == "ERC-1155"
else else
false token_balance_requests
|> Enum.map(&format_balance_request/1)
end end
end)
|> Enum.map(&format_erc_1155_balance_request/1)
|> Reader.query_contracts(@erc1155_balance_function_abi)
|> Enum.map(&format_balance_result/1)
regular_balances ++ erc1155_balances formatted_balances_requests
|> Reader.query_contracts(abi)
|> Enum.map(&format_balance_result/1)
end end
defp format_balance_request(%{ defp format_balance_request(%{

@ -13,6 +13,18 @@ defmodule Indexer.TokenBalances do
alias Indexer.Fetcher.TokenBalance alias Indexer.Fetcher.TokenBalance
alias Indexer.Tracer alias Indexer.Tracer
@erc1155_balance_function_abi [
%{
"constant" => true,
"inputs" => [%{"name" => "_owner", "type" => "address"}, %{"name" => "_id", "type" => "uint256"}],
"name" => "balanceOf",
"outputs" => [%{"name" => "", "type" => "uint256"}],
"payable" => false,
"stateMutability" => "view",
"type" => "function"
}
]
@doc """ @doc """
Fetches TokenBalances from specific Addresses and Blocks in the Blockchain Fetches TokenBalances from specific Addresses and Blocks in the Blockchain
@ -35,12 +47,39 @@ defmodule Indexer.TokenBalances do
def fetch_token_balances_from_blockchain(token_balances) do def fetch_token_balances_from_blockchain(token_balances) do
Logger.debug("fetching token balances", count: Enum.count(token_balances)) Logger.debug("fetching token balances", count: Enum.count(token_balances))
requested_token_balances = regular_token_balances =
token_balances token_balances
|> Enum.filter(fn request ->
if Map.has_key?(request, :token_type) do
request.token_type !== "ERC-1155"
else
true
end
end)
erc1155_token_balances =
token_balances
|> Enum.filter(fn request ->
if Map.has_key?(request, :token_type) do
request.token_type == "ERC-1155"
else
false
end
end)
requested_regular_token_balances =
regular_token_balances
|> BalanceReader.get_balances_of() |> BalanceReader.get_balances_of()
|> Stream.zip(token_balances) |> Stream.zip(token_balances)
|> Enum.map(fn {result, token_balance} -> set_token_balance_value(result, token_balance) end) |> Enum.map(fn {result, token_balance} -> set_token_balance_value(result, token_balance) end)
requested_erc1155_token_balances =
erc1155_token_balances
|> BalanceReader.get_balances_of_with_abi(@erc1155_balance_function_abi)
|> Stream.zip(token_balances)
|> Enum.map(fn {result, token_balance} -> set_token_balance_value(result, token_balance) end)
requested_token_balances = requested_regular_token_balances ++ requested_erc1155_token_balances
fetched_token_balances = Enum.filter(requested_token_balances, &ignore_request_with_errors/1) fetched_token_balances = Enum.filter(requested_token_balances, &ignore_request_with_errors/1)
requested_token_balances requested_token_balances

@ -26,8 +26,10 @@ defmodule Indexer.TokenBalancesTest do
token = insert(:token, contract_address: build(:contract_address)) token = insert(:token, contract_address: build(:contract_address))
address_hash_string = Hash.to_string(address.hash) address_hash_string = Hash.to_string(address.hash)
token_contract_address_hash = Hash.to_string(token.contract_address_hash)
data = %{ data = %{
token_contract_address_hash: Hash.to_string(token.contract_address_hash), token_contract_address_hash: token_contract_address_hash,
address_hash: address_hash_string, address_hash: address_hash_string,
block_number: 1_000, block_number: 1_000,
token_id: 11, token_id: 11,
@ -40,8 +42,8 @@ defmodule Indexer.TokenBalancesTest do
assert %{ assert %{
value: 1_000_000_000_000_000_000_000_000, value: 1_000_000_000_000_000_000_000_000,
token_contract_address_hash: token_contract_address_hash, token_contract_address_hash: ^token_contract_address_hash,
address_hash: address_hash, address_hash: ^address_hash_string,
block_number: 1_000, block_number: 1_000,
value_fetched_at: _ value_fetched_at: _
} = List.first(result) } = List.first(result)

Loading…
Cancel
Save