From 8027f19e82942ec08994e798e18431865733cedd Mon Sep 17 00:00:00 2001 From: Victor Baranov Date: Tue, 1 Sep 2020 12:48:11 +0300 Subject: [PATCH] Update token metadata at token transfer parsing --- CHANGELOG.md | 1 + apps/explorer/lib/explorer/chain/address.ex | 11 ++++++ .../lib/explorer/chain/bridged_token.ex | 8 +++++ apps/explorer/lib/explorer/chain/token.ex | 8 +++++ .../lib/explorer/chain/transaction.ex | 19 +++++++++++ .../lib/indexer/transform/token_transfers.ex | 34 ++++++++++++++++++- 6 files changed, 80 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e009ab9332..a600364ffe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## Current ### Features +- [#3273](https://github.com/poanetwork/blockscout/pull/3273) - Update token metadata at burn/mint events - [#3268](https://github.com/poanetwork/blockscout/pull/3268) - Token total supply on-demand fetcher - [#3261](https://github.com/poanetwork/blockscout/pull/3261) - Bridged tokens table diff --git a/apps/explorer/lib/explorer/chain/address.ex b/apps/explorer/lib/explorer/chain/address.ex index 1b167fbc78..0bf19330ac 100644 --- a/apps/explorer/lib/explorer/chain/address.ex +++ b/apps/explorer/lib/explorer/chain/address.ex @@ -71,6 +71,17 @@ defmodule Explorer.Chain.Address do :names ]} + @derive {Jason.Encoder, + except: [ + :__meta__, + :smart_contract, + :decompiled_smart_contracts, + :token, + :contracts_creation_internal_transaction, + :contracts_creation_transaction, + :names + ]} + @primary_key {:hash, Hash.Address, autogenerate: false} schema "addresses" do field(:fetched_coin_balance, Wei) diff --git a/apps/explorer/lib/explorer/chain/bridged_token.ex b/apps/explorer/lib/explorer/chain/bridged_token.ex index 15d13f4018..7399d46b21 100644 --- a/apps/explorer/lib/explorer/chain/bridged_token.ex +++ b/apps/explorer/lib/explorer/chain/bridged_token.ex @@ -31,6 +31,14 @@ defmodule Explorer.Chain.BridgedToken do :updated_at ]} + @derive {Jason.Encoder, + except: [ + :__meta__, + :home_token_contract_address, + :inserted_at, + :updated_at + ]} + @primary_key false schema "bridged_tokens" do field(:foreign_chain_id, :decimal) diff --git a/apps/explorer/lib/explorer/chain/token.ex b/apps/explorer/lib/explorer/chain/token.ex index e2b0c3fe62..e48ab5443b 100644 --- a/apps/explorer/lib/explorer/chain/token.ex +++ b/apps/explorer/lib/explorer/chain/token.ex @@ -57,6 +57,14 @@ defmodule Explorer.Chain.Token do :updated_at ]} + @derive {Jason.Encoder, + except: [ + :__meta__, + :contract_address, + :inserted_at, + :updated_at + ]} + @primary_key false schema "tokens" do field(:name, :string) diff --git a/apps/explorer/lib/explorer/chain/transaction.ex b/apps/explorer/lib/explorer/chain/transaction.ex index e4518c7349..607ad85e4c 100644 --- a/apps/explorer/lib/explorer/chain/transaction.ex +++ b/apps/explorer/lib/explorer/chain/transaction.ex @@ -185,6 +185,25 @@ defmodule Explorer.Chain.Transaction do :value ]} + @derive {Jason.Encoder, + only: [ + :block_number, + :cumulative_gas_used, + :error, + :gas, + :gas_price, + :gas_used, + :index, + :created_contract_code_indexed_at, + :input, + :nonce, + :r, + :s, + :v, + :status, + :value + ]} + @primary_key {:hash, Hash.Full, autogenerate: false} schema "transactions" do field(:block_number, :integer) diff --git a/apps/indexer/lib/indexer/transform/token_transfers.ex b/apps/indexer/lib/indexer/transform/token_transfers.ex index 303efa059e..33ca2e0136 100644 --- a/apps/indexer/lib/indexer/transform/token_transfers.ex +++ b/apps/indexer/lib/indexer/transform/token_transfers.ex @@ -6,7 +6,11 @@ defmodule Indexer.Transform.TokenTransfers do require Logger alias ABI.TypeDecoder - alias Explorer.Chain.TokenTransfer + alias Explorer.{Chain, Repo} + alias Explorer.Chain.{Token, TokenTransfer} + alias Explorer.Token.MetadataRetriever + + @burn_address "0x0000000000000000000000000000000000000000" @doc """ Returns a list of token transfers given a list of logs. @@ -54,6 +58,8 @@ defmodule Indexer.Transform.TokenTransfers do type: "ERC-20" } + update_token(log.address_hash, token_transfer) + {token, token_transfer} end @@ -79,6 +85,8 @@ defmodule Indexer.Transform.TokenTransfers do type: "ERC-721" } + update_token(log.address_hash, token_transfer) + {token, token_transfer} end @@ -104,9 +112,33 @@ 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) + + token_params = + address_hash_string + |> MetadataRetriever.get_functions_of() + + token = Repo.get_by(Token, contract_address_hash: address_hash) + + 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 + end + + :ok + end + defp truncate_address_hash(nil), do: "0x0000000000000000000000000000000000000000" defp truncate_address_hash("0x000000000000000000000000" <> truncated_hash) do