Merge pull request #3268 from poanetwork/token-supply-on-demand-fetcher

Token total supply on-demand fetcher
pull/3273/head
Victor Baranov 4 years ago committed by GitHub
commit 4317db0946
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      .dialyzer-ignore
  2. 3
      CHANGELOG.md
  3. 4
      apps/block_scout_web/lib/block_scout_web/controllers/tokens/transfer_controller.ex
  4. 2
      apps/explorer/lib/explorer/chain.ex
  5. 48
      apps/indexer/lib/indexer/fetcher/token_total_supply_on_demand.ex
  6. 2
      apps/indexer/lib/indexer/supervisor.ex

@ -22,3 +22,4 @@ lib/block_scout_web/views/layout_view.ex:224: The call 'Elixir.Poison.Parser':'p
lib/block_scout_web/controllers/api/rpc/transaction_controller.ex:21 lib/block_scout_web/controllers/api/rpc/transaction_controller.ex:21
lib/block_scout_web/controllers/api/rpc/transaction_controller.ex:22 lib/block_scout_web/controllers/api/rpc/transaction_controller.ex:22
lib/explorer/smart_contract/reader.ex:336 lib/explorer/smart_contract/reader.ex:336
lib/indexer/fetcher/token_total_supply_on_demand.ex:16

@ -1,6 +1,7 @@
## Current ## Current
### Features ### Features
- [#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 - [#3261](https://github.com/poanetwork/blockscout/pull/3261) - Bridged tokens table
### Fixes ### Fixes
@ -19,7 +20,7 @@
### Features ### Features
- [#3252](https://github.com/poanetwork/blockscout/pull/3252) - Gas price at the main page - [#3252](https://github.com/poanetwork/blockscout/pull/3252) - Gas price at the main page
- [#3239](https://github.com/poanetwork/blockscout/pull/3239) - Hide address page tabs if no items - [#3239](https://github.com/poanetwork/blockscout/pull/3239) - Hide address page tabs if no items
- [#3236](https://github.com/poanetwork/blockscout/pull/3236) - Easy verification of contracts which hash verified twins (the same bytecode) - [#3236](https://github.com/poanetwork/blockscout/pull/3236) - Easy verification of contracts which has verified twins (the same bytecode)
- [#3227](https://github.com/poanetwork/blockscout/pull/3227) - Distinguishing of bridged tokens - [#3227](https://github.com/poanetwork/blockscout/pull/3227) - Distinguishing of bridged tokens
- [#3224](https://github.com/poanetwork/blockscout/pull/3224) - Top tokens page - [#3224](https://github.com/poanetwork/blockscout/pull/3224) - Top tokens page

@ -4,6 +4,7 @@ defmodule BlockScoutWeb.Tokens.TransferController do
alias BlockScoutWeb.Tokens.TransferView alias BlockScoutWeb.Tokens.TransferView
alias Explorer.{Chain, Market} alias Explorer.{Chain, Market}
alias Explorer.Chain.Address alias Explorer.Chain.Address
alias Indexer.Fetcher.TokenTotalSupplyOnDemand
alias Phoenix.View alias Phoenix.View
import BlockScoutWeb.Chain, only: [split_list_by_page: 1, paging_options: 1, next_page_params: 3] import BlockScoutWeb.Chain, only: [split_list_by_page: 1, paging_options: 1, next_page_params: 3]
@ -63,7 +64,8 @@ defmodule BlockScoutWeb.Tokens.TransferController do
"index.html", "index.html",
counters_path: token_path(conn, :token_counters, %{"id" => Address.checksum(address_hash)}), counters_path: token_path(conn, :token_counters, %{"id" => Address.checksum(address_hash)}),
current_path: current_path(conn), current_path: current_path(conn),
token: Market.add_price(token) token: Market.add_price(token),
token_total_supply_status: TokenTotalSupplyOnDemand.trigger_fetch(address_hash)
) )
else else
:error -> :error ->

@ -3923,7 +3923,7 @@ defmodule Explorer.Chain do
end end
) )
|> Multi.run(:token, fn repo, _ -> |> Multi.run(:token, fn repo, _ ->
with {:error, %Changeset{errors: [{^stale_error_field, {^stale_error_message, []}}]}} <- with {:error, %Changeset{errors: [{^stale_error_field, {^stale_error_message, [_]}}]}} <-
repo.insert(token_changeset, token_opts) do repo.insert(token_changeset, token_opts) do
# the original token passed into `update_token/2` as stale error means it is unchanged # the original token passed into `update_token/2` as stale error means it is unchanged
{:ok, token} {:ok, token}

@ -0,0 +1,48 @@
defmodule Indexer.Fetcher.TokenTotalSupplyOnDemand do
@moduledoc """
Ensures that we have a reasonably up to date token supply.
"""
use GenServer
use Indexer.Fetcher
alias Explorer.{Chain, Repo}
alias Explorer.Chain.{Address, Token}
alias Explorer.Token.MetadataRetriever
## Interface
@spec trigger_fetch(Address.t()) :: :ok
def trigger_fetch(address) do
do_trigger_fetch(address)
end
## Callbacks
def start_link([init_opts, server_opts]) do
GenServer.start_link(__MODULE__, init_opts, server_opts)
end
def init(init_arg) do
{:ok, init_arg}
end
## Implementation
defp do_trigger_fetch(address) when not is_nil(address) do
token_address_hash = "0x" <> Base.encode16(address.bytes)
token_params =
token_address_hash
|> MetadataRetriever.get_functions_of()
token =
Token
|> Repo.get_by(contract_address_hash: address)
|> Repo.preload([:contract_address])
{:ok, _} = Chain.update_token(%{token | updated_at: DateTime.utc_now()}, token_params)
:ok
end
end

@ -20,6 +20,7 @@ defmodule Indexer.Supervisor do
Token, Token,
TokenBalance, TokenBalance,
TokenInstance, TokenInstance,
TokenTotalSupplyOnDemand,
TokenUpdater, TokenUpdater,
UncleBlock UncleBlock
} }
@ -117,6 +118,7 @@ defmodule Indexer.Supervisor do
# Out-of-band fetchers # Out-of-band fetchers
{CoinBalanceOnDemand.Supervisor, [json_rpc_named_arguments]}, {CoinBalanceOnDemand.Supervisor, [json_rpc_named_arguments]},
{TokenTotalSupplyOnDemand.Supervisor, [json_rpc_named_arguments]},
# Temporary workers # Temporary workers
{UncatalogedTokenTransfers.Supervisor, [[]]}, {UncatalogedTokenTransfers.Supervisor, [[]]},

Loading…
Cancel
Save