Merge pull request #2216 from poanetwork/improve-tokens-controllers

Improve token's controllers by avoiding unnecessary preloads
pull/2275/head
Victor Baranov 5 years ago committed by GitHub
commit 285d5ae56d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      CHANGELOG.md
  2. 2
      apps/block_scout_web/lib/block_scout_web/controllers/tokens/holder_controller.ex
  3. 2
      apps/block_scout_web/lib/block_scout_web/controllers/tokens/inventory_controller.ex
  4. 2
      apps/block_scout_web/lib/block_scout_web/controllers/tokens/read_contract_controller.ex
  5. 2
      apps/block_scout_web/lib/block_scout_web/controllers/tokens/transfer_controller.ex
  6. 11
      apps/explorer/lib/explorer/chain.ex
  7. 11
      apps/explorer/test/explorer/chain_test.exs
  8. 2
      apps/indexer/lib/indexer/fetcher/token.ex
  9. 2
      apps/indexer/lib/indexer/fetcher/token_updater.ex

@ -6,6 +6,7 @@
- [#2151](https://github.com/poanetwork/blockscout/pull/2151) - hide dropdown menu then other networks list is empty - [#2151](https://github.com/poanetwork/blockscout/pull/2151) - hide dropdown menu then other networks list is empty
- [#2191](https://github.com/poanetwork/blockscout/pull/2191) - allow to configure token metadata update interval - [#2191](https://github.com/poanetwork/blockscout/pull/2191) - allow to configure token metadata update interval
- [#2146](https://github.com/poanetwork/blockscout/pull/2146) - feat: add eth_getLogs rpc endpoint - [#2146](https://github.com/poanetwork/blockscout/pull/2146) - feat: add eth_getLogs rpc endpoint
- [#2216](https://github.com/poanetwork/blockscout/pull/2216) - Improve token's controllers by avoiding unnecessary preloads
- [#2235](https://github.com/poanetwork/blockscout/pull/2235) - save and show additional validation fields to smart contract - [#2235](https://github.com/poanetwork/blockscout/pull/2235) - save and show additional validation fields to smart contract
- [#2190](https://github.com/poanetwork/blockscout/pull/2190) - show all token transfers - [#2190](https://github.com/poanetwork/blockscout/pull/2190) - show all token transfers
- [#2193](https://github.com/poanetwork/blockscout/pull/2193) - feat: add BLOCKSCOUT_HOST, and use it in API docs - [#2193](https://github.com/poanetwork/blockscout/pull/2193) - feat: add BLOCKSCOUT_HOST, and use it in API docs

@ -44,7 +44,7 @@ defmodule BlockScoutWeb.Tokens.HolderController do
def index(conn, %{"token_id" => address_hash_string}) do def index(conn, %{"token_id" => address_hash_string}) do
with {:ok, address_hash} <- Chain.string_to_address_hash(address_hash_string), with {:ok, address_hash} <- Chain.string_to_address_hash(address_hash_string),
{:ok, token} <- Chain.token_from_address_hash(address_hash) do {:ok, token} <- Chain.token_from_address_hash(address_hash, [{:contract_address, :smart_contract}]) do
render( render(
conn, conn,
"index.html", "index.html",

@ -61,7 +61,7 @@ defmodule BlockScoutWeb.Tokens.InventoryController do
def index(conn, %{"token_id" => address_hash_string}) do def index(conn, %{"token_id" => address_hash_string}) do
with {:ok, address_hash} <- Chain.string_to_address_hash(address_hash_string), with {:ok, address_hash} <- Chain.string_to_address_hash(address_hash_string),
{:ok, token} <- Chain.token_from_address_hash(address_hash) do {:ok, token} <- Chain.token_from_address_hash(address_hash, [{:contract_address, :smart_contract}]) do
render( render(
conn, conn,
"index.html", "index.html",

@ -5,7 +5,7 @@ defmodule BlockScoutWeb.Tokens.ReadContractController do
def index(conn, %{"token_id" => address_hash_string}) do def index(conn, %{"token_id" => address_hash_string}) do
with {:ok, address_hash} <- Chain.string_to_address_hash(address_hash_string), with {:ok, address_hash} <- Chain.string_to_address_hash(address_hash_string),
{:ok, token} <- Chain.token_from_address_hash(address_hash) do {:ok, token} <- Chain.token_from_address_hash(address_hash, [{:contract_address, :smart_contract}]) do
render( render(
conn, conn,
"index.html", "index.html",

@ -45,7 +45,7 @@ defmodule BlockScoutWeb.Tokens.TransferController do
def index(conn, %{"token_id" => address_hash_string}) do def index(conn, %{"token_id" => address_hash_string}) do
with {:ok, address_hash} <- Chain.string_to_address_hash(address_hash_string), with {:ok, address_hash} <- Chain.string_to_address_hash(address_hash_string),
{:ok, token} <- Chain.token_from_address_hash(address_hash) do {:ok, token} <- Chain.token_from_address_hash(address_hash, [{:contract_address, :smart_contract}]) do
render( render(
conn, conn,
"index.html", "index.html",

@ -2671,14 +2671,19 @@ defmodule Explorer.Chain do
@doc """ @doc """
Fetches a `t:Token.t/0` by an address hash. Fetches a `t:Token.t/0` by an address hash.
Optionally accepts a list of bindings to preload, just like `Ecto.Query.preload/3`
""" """
@spec token_from_address_hash(Hash.Address.t()) :: {:ok, Token.t()} | {:error, :not_found} @spec token_from_address_hash(Hash.Address.t(), [Macro.t()]) :: {:ok, Token.t()} | {:error, :not_found}
def token_from_address_hash(%Hash{byte_count: unquote(Hash.Address.byte_count())} = hash) do def token_from_address_hash(
%Hash{byte_count: unquote(Hash.Address.byte_count())} = hash,
preloads \\ []
) do
query = query =
from( from(
token in Token, token in Token,
where: token.contract_address_hash == ^hash, where: token.contract_address_hash == ^hash,
preload: [{:contract_address, :smart_contract}] preload: ^preloads
) )
case Repo.one(query) do case Repo.one(query) do

@ -3430,6 +3430,17 @@ defmodule Explorer.ChainTest do
token = build(:token) token = build(:token)
assert {:error, :not_found} = Chain.token_from_address_hash(token.contract_address.hash) assert {:error, :not_found} = Chain.token_from_address_hash(token.contract_address.hash)
end end
test "with contract_address' smart_contract preloaded" do
smart_contract = build(:smart_contract)
address = insert(:address, smart_contract: smart_contract)
token = insert(:token, contract_address: address)
assert {:ok, result} =
Chain.token_from_address_hash(token.contract_address_hash, [{:contract_address, :smart_contract}])
assert smart_contract = result.contract_address.smart_contract
end
end end
test "stream_uncataloged_token_contract_address_hashes/2 reduces with given reducer and accumulator" do test "stream_uncataloged_token_contract_address_hashes/2 reduces with given reducer and accumulator" do

@ -52,7 +52,7 @@ defmodule Indexer.Fetcher.Token do
@impl BufferedTask @impl BufferedTask
@decorate trace(name: "fetch", resource: "Indexer.Fetcher.Token.run/2", service: :indexer, tracer: Tracer) @decorate trace(name: "fetch", resource: "Indexer.Fetcher.Token.run/2", service: :indexer, tracer: Tracer)
def run([token_contract_address], _json_rpc_named_arguments) do def run([token_contract_address], _json_rpc_named_arguments) do
case Chain.token_from_address_hash(token_contract_address) do case Chain.token_from_address_hash(token_contract_address, [{:contract_address, :smart_contract}]) do
{:ok, %Token{} = token} -> {:ok, %Token{} = token} ->
catalog_token(token) catalog_token(token)
end end

@ -37,7 +37,7 @@ defmodule Indexer.Fetcher.TokenUpdater do
@doc false @doc false
def update_metadata(token_addresses) when is_list(token_addresses) do def update_metadata(token_addresses) when is_list(token_addresses) do
Enum.each(token_addresses, fn address -> Enum.each(token_addresses, fn address ->
case Chain.token_from_address_hash(address) do case Chain.token_from_address_hash(address, [{:contract_address, :smart_contract}]) do
{:ok, %Token{cataloged: true} = token} -> {:ok, %Token{cataloged: true} = token} ->
update_metadata(token) update_metadata(token)
end end

Loading…
Cancel
Save