|
|
|
@ -96,12 +96,19 @@ defmodule Indexer.Fetcher.TokenBalance do |
|
|
|
|
@impl BufferedTask |
|
|
|
|
@decorate trace(name: "fetch", resource: "Indexer.Fetcher.TokenBalance.run/2", tracer: Tracer, service: :indexer) |
|
|
|
|
def run(entries, _json_rpc_named_arguments) do |
|
|
|
|
params = Enum.map(entries, &format_params/1) |
|
|
|
|
|
|
|
|
|
missing_balance_of_tokens = |
|
|
|
|
params |
|
|
|
|
|> Enum.map(& &1.token_contract_address_hash) |
|
|
|
|
|> Enum.uniq() |
|
|
|
|
|> MissingBalanceOfToken.get_by_hashes() |
|
|
|
|
|
|
|
|
|
result = |
|
|
|
|
entries |
|
|
|
|
|> Enum.map(&format_params/1) |
|
|
|
|
|> MissingBalanceOfToken.filter_token_balances_params() |
|
|
|
|
params |
|
|
|
|
|> MissingBalanceOfToken.filter_token_balances_params(true, missing_balance_of_tokens) |
|
|
|
|
|> increase_retries_count() |
|
|
|
|
|> fetch_from_blockchain() |
|
|
|
|
|> fetch_from_blockchain(missing_balance_of_tokens) |
|
|
|
|
|> import_token_balances() |
|
|
|
|
|
|
|
|
|
if result == :ok do |
|
|
|
@ -111,7 +118,7 @@ defmodule Indexer.Fetcher.TokenBalance do |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def fetch_from_blockchain(params_list) do |
|
|
|
|
def fetch_from_blockchain(params_list, missing_balance_of_tokens) do |
|
|
|
|
retryable_params_list = |
|
|
|
|
params_list |
|
|
|
|
|> Enum.filter(&(&1.retries_count <= @max_retries)) |
|
|
|
@ -130,6 +137,8 @@ defmodule Indexer.Fetcher.TokenBalance do |
|
|
|
|
failed_token_balances: failed_token_balances |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
handle_success_balances(fetched_token_balances, missing_balance_of_tokens) |
|
|
|
|
|
|
|
|
|
if Enum.empty?(failed_token_balances) do |
|
|
|
|
{:halt, all_token_balances} |
|
|
|
|
else |
|
|
|
@ -149,6 +158,23 @@ defmodule Indexer.Fetcher.TokenBalance do |
|
|
|
|
fetched_token_balances |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
defp handle_success_balances(fetched_token_balances, missing_balance_of_tokens) do |
|
|
|
|
successful_token_hashes = |
|
|
|
|
fetched_token_balances |
|
|
|
|
|> Enum.map(&to_string(&1.token_contract_address_hash)) |
|
|
|
|
|> MapSet.new() |
|
|
|
|
|
|
|
|
|
missing_balance_of_token_hashes = |
|
|
|
|
missing_balance_of_tokens |
|
|
|
|
|> Enum.map(&to_string(&1.token_contract_address_hash)) |
|
|
|
|
|> MapSet.new() |
|
|
|
|
|
|
|
|
|
successful_token_hashes |
|
|
|
|
|> MapSet.intersection(missing_balance_of_token_hashes) |
|
|
|
|
|> MapSet.to_list() |
|
|
|
|
|> MissingBalanceOfToken.mark_as_implemented() |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
defp handle_failed_balances(failed_token_balances) do |
|
|
|
|
{missing_balance_of_balances, other_failed_balances} = |
|
|
|
|
Enum.split_with(failed_token_balances, fn |
|
|
|
@ -158,9 +184,14 @@ defmodule Indexer.Fetcher.TokenBalance do |
|
|
|
|
|
|
|
|
|
MissingBalanceOfToken.insert_from_params(missing_balance_of_balances) |
|
|
|
|
|
|
|
|
|
Enum.each(missing_balance_of_balances, fn balance -> |
|
|
|
|
TokenBalance.delete_placeholders_below(balance.token_contract_address_hash, balance.block_number) |
|
|
|
|
CurrentTokenBalance.delete_placeholders_below(balance.token_contract_address_hash, balance.block_number) |
|
|
|
|
missing_balance_of_balances |
|
|
|
|
|> Enum.group_by(& &1.token_contract_address_hash, & &1.block_number) |
|
|
|
|
|> Enum.map(fn {token_contract_address_hash, block_numbers} -> |
|
|
|
|
{token_contract_address_hash, Enum.max(block_numbers)} |
|
|
|
|
end) |
|
|
|
|
|> Enum.each(fn {token_contract_address_hash, block_number} -> |
|
|
|
|
TokenBalance.delete_placeholders_below(token_contract_address_hash, block_number) |
|
|
|
|
CurrentTokenBalance.delete_placeholders_below(token_contract_address_hash, block_number) |
|
|
|
|
end) |
|
|
|
|
|
|
|
|
|
other_failed_balances |
|
|
|
|