From 94b7005e7ba4c94a1aac7b97013233fd97e6c322 Mon Sep 17 00:00:00 2001 From: Viktor Baranov Date: Fri, 20 Aug 2021 14:47:07 +0300 Subject: [PATCH] Eliminate multiple Updates of the same token in mint/burn token transfers --- CHANGELOG.md | 1 + .../lib/indexer/transform/token_transfers.ex | 53 +++++++++++-------- 2 files changed, 32 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 89c6916c46..9389f02bf1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - [#4452](https://github.com/blockscout/blockscout/pull/4452) - Add names for smart-conrtact's function response ### Fixes +- [#4535](https://github.com/blockscout/blockscout/pull/4535) - Indexer performance update:: Eliminate multiple updates of the same token while parsing mint/burn token transfers batch - [#4527](https://github.com/blockscout/blockscout/pull/4527) - Indexer performance update: refactor coin balance daily fetcher - [#4525](https://github.com/blockscout/blockscout/pull/4525) - Uncataloged token transfers query performance improvement - [#4513](https://github.com/blockscout/blockscout/pull/4513) - Fix installation with custom default path: add NETWORK_PATH variable to the current_path diff --git a/apps/indexer/lib/indexer/transform/token_transfers.ex b/apps/indexer/lib/indexer/transform/token_transfers.ex index 33ca2e0136..aaec1e8ca5 100644 --- a/apps/indexer/lib/indexer/transform/token_transfers.ex +++ b/apps/indexer/lib/indexer/transform/token_transfers.ex @@ -18,9 +18,24 @@ defmodule Indexer.Transform.TokenTransfers do def parse(logs) do initial_acc = %{tokens: [], token_transfers: []} - logs - |> Enum.filter(&(&1.first_topic == unquote(TokenTransfer.constant()))) - |> Enum.reduce(initial_acc, &do_parse/2) + token_transfers_from_logs = + logs + |> Enum.filter(&(&1.first_topic == unquote(TokenTransfer.constant()))) + |> Enum.reduce(initial_acc, &do_parse/2) + + token_transfers = token_transfers_from_logs.token_transfers + + token_transfers + |> Enum.filter(fn token_transfer -> + token_transfer.to_address_hash == @burn_address || token_transfer.from_address_hash == @burn_address + end) + |> Enum.map(fn token_transfer -> + token_transfer.token_contract_address_hash + end) + |> Enum.dedup() + |> Enum.each(&update_token/1) + + token_transfers_from_logs end defp do_parse(log, %{tokens: tokens, token_transfers: token_transfers} = acc) do @@ -58,8 +73,6 @@ defmodule Indexer.Transform.TokenTransfers do type: "ERC-20" } - update_token(log.address_hash, token_transfer) - {token, token_transfer} end @@ -85,8 +98,6 @@ defmodule Indexer.Transform.TokenTransfers do type: "ERC-721" } - update_token(log.address_hash, token_transfer) - {token, token_transfer} end @@ -112,28 +123,26 @@ defmodule Indexer.Transform.TokenTransfers do type: "ERC-721" } - update_token(log.address_hash, token_transfer) - {token, token_transfer} end - defp update_token(address_hash_string, token_transfer) do - if token_transfer.to_address_hash == @burn_address || token_transfer.from_address_hash == @burn_address do - {:ok, address_hash} = Chain.string_to_address_hash(address_hash_string) + defp update_token(nil), do: :ok + + defp update_token(address_hash_string) do + {:ok, address_hash} = Chain.string_to_address_hash(address_hash_string) - token_params = - address_hash_string - |> MetadataRetriever.get_functions_of() + token_params = + address_hash_string + |> MetadataRetriever.get_functions_of() - token = Repo.get_by(Token, contract_address_hash: address_hash) + token = Repo.get_by(Token, contract_address_hash: address_hash) - if token do - token_to_update = - token - |> Repo.preload([:contract_address]) + if token do + token_to_update = + token + |> Repo.preload([:contract_address]) - {:ok, _} = Chain.update_token(%{token_to_update | updated_at: DateTime.utc_now()}, token_params) - end + {:ok, _} = Chain.update_token(%{token_to_update | updated_at: DateTime.utc_now()}, token_params) end :ok