Add metadata updater

pull/1095/head
William Sanches 6 years ago
parent 06ce9ab5c8
commit 3528e4dc51
No known key found for this signature in database
GPG Key ID: 27250E49FB133014
  1. 3
      apps/explorer/lib/explorer/chain.ex
  2. 1
      apps/indexer/config/config.exs
  3. 49
      apps/indexer/lib/indexer/token/metadata_updater.ex
  4. 5
      apps/indexer/lib/indexer/token/supervisor.ex
  5. 61
      apps/indexer/test/indexer/token/metadata_updater_test.exs

@ -1953,8 +1953,9 @@ defmodule Explorer.Chain do
query =
from(
token in Token,
select: token.contract_address_hash,
where: token.cataloged == true,
select: token.contract_address_hash
order_by: [asc: token.updated_at]
)
query

@ -7,6 +7,7 @@ import Bitwise
config :indexer,
block_transformer: Indexer.Block.Transform.Base,
ecto_repos: [Explorer.Repo],
metadata_updater_days_interval: 7,
# bytes
memory_limit: 1 <<< 30

@ -0,0 +1,49 @@
defmodule Indexer.Token.MetadataUpdater do
@moduledoc """
Updates metadata for cataloged tokens
"""
use GenServer
alias Explorer.Chain
alias Explorer.Chain.Token
alias Explorer.Token.MetadataRetriever
def start_link(_) do
GenServer.start_link(__MODULE__, :ok, name: __MODULE__)
end
@impl true
def init(state) do
send(self(), :update_tokens)
{:ok, state}
end
@impl true
def handle_info(:update_tokens, state) do
{:ok, tokens} = Chain.stream_cataloged_token_contract_address_hashes([], &[&1 | &2])
update_metadata(tokens)
interval = Application.get_env(:indexer, :metadata_updater_days_interval)
Process.send_after(self(), :update_tokens, :timer.hours(interval) * 24)
{:noreply, state}
end
@doc false
def update_metadata(token_addresses) when is_list(token_addresses) do
Enum.each(token_addresses, fn address ->
case Chain.token_from_address_hash(address) do
{:ok, %Token{cataloged: true} = token} ->
update_metadata(token)
end
end)
end
def update_metadata(%Token{contract_address_hash: contract_address_hash} = token) do
contract_functions = MetadataRetriever.get_functions_of(contract_address_hash)
Chain.update_token(%{token | updated_at: DateTime.utc_now()}, contract_functions)
end
end

@ -5,7 +5,7 @@ defmodule Indexer.Token.Supervisor do
use Supervisor
alias Indexer.Token.Fetcher
alias Indexer.Token.{Fetcher, MetadataUpdater}
def child_spec([init_arguments]) do
child_spec([init_arguments, []])
@ -30,7 +30,8 @@ defmodule Indexer.Token.Supervisor do
Supervisor.init(
[
{Task.Supervisor, name: Indexer.Token.TaskSupervisor},
{Fetcher, [fetcher_arguments, [name: Fetcher]]}
{Fetcher, [fetcher_arguments, [name: Fetcher]]},
{MetadataUpdater, [[], [name: MetadataUpdater]]}
],
strategy: :one_for_one
)

@ -0,0 +1,61 @@
defmodule Indexer.Token.MetadataUpdaterTest do
use Explorer.DataCase
import Mox
alias Explorer.Chain
alias Explorer.Chain.Token
alias Indexer.Token.MetadataUpdater
setup :verify_on_exit!
describe "update_metadata/1" do
test "updates the metadata for a list of tokens" do
token = insert(:token, name: nil, symbol: nil, decimals: 10)
expect(
EthereumJSONRPC.Mox,
:json_rpc,
1,
fn [%{id: "decimals"}, %{id: "name"}, %{id: "symbol"}, %{id: "totalSupply"}], _opts ->
{:ok,
[
%{
id: "decimals",
result: "0x0000000000000000000000000000000000000000000000000000000000000012"
},
%{
id: "name",
result:
"0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000642616e636f720000000000000000000000000000000000000000000000000000"
},
%{
id: "symbol",
result:
"0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003424e540000000000000000000000000000000000000000000000000000000000"
},
%{
id: "totalSupply",
result: "0x0000000000000000000000000000000000000000000000000de0b6b3a7640000"
}
]}
end
)
MetadataUpdater.update_metadata([token.contract_address_hash])
expected_supply = Decimal.new(1_000_000_000_000_000_000)
decimals_expected = Decimal.new(18)
assert {:ok,
%Token{
name: "Bancor",
symbol: "BNT",
total_supply: ^expected_supply,
decimals: ^decimals_expected,
cataloged: true
}} = Chain.token_from_address_hash(token.contract_address_hash)
end
end
end
Loading…
Cancel
Save