1155 refactoring: get_balances_of_with_abi/2

pull/3902/head
Viktor Baranov 4 years ago
parent af86e20635
commit 7445aa7558
  1. 48
      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()}
]) :: [{:ok, non_neg_integer()} | {:error, String.t()}]
def get_balances_of(token_balance_requests) do
regular_balances =
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)
|> Reader.query_contracts(@balance_function_abi)
|> Enum.map(&format_balance_result/1)
token_balance_requests
|> Enum.map(&format_balance_request/1)
|> Reader.query_contracts(@balance_function_abi)
|> Enum.map(&format_balance_result/1)
end
erc1155_balances =
token_balance_requests
|> Enum.filter(fn request ->
if Map.has_key?(request, :token_type) do
request.token_type == "ERC-1155"
else
false
end
end)
|> Enum.map(&format_erc_1155_balance_request/1)
|> Reader.query_contracts(@erc1155_balance_function_abi)
|> Enum.map(&format_balance_result/1)
@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
|> Enum.map(&format_erc_1155_balance_request/1)
else
token_balance_requests
|> Enum.map(&format_balance_request/1)
end
regular_balances ++ erc1155_balances
formatted_balances_requests
|> Reader.query_contracts(abi)
|> Enum.map(&format_balance_result/1)
end
defp format_balance_request(%{

@ -13,6 +13,18 @@ defmodule Indexer.TokenBalances do
alias Indexer.Fetcher.TokenBalance
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 """
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
Logger.debug("fetching token balances", count: Enum.count(token_balances))
requested_token_balances =
regular_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()
|> Stream.zip(token_balances)
|> 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)
requested_token_balances

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

Loading…
Cancel
Save