Merge pull request #2305 from poanetwork/pp-address-controllers-improvements

Improve Address controllers
pull/2327/head
Victor Baranov 5 years ago committed by GitHub
commit 09e69efb0c
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/address_coin_balance_controller.ex
  3. 2
      apps/block_scout_web/lib/block_scout_web/controllers/address_internal_transaction_controller.ex
  4. 4
      apps/block_scout_web/lib/block_scout_web/controllers/address_logs_controller.ex
  5. 2
      apps/block_scout_web/lib/block_scout_web/controllers/address_token_controller.ex
  6. 4
      apps/block_scout_web/lib/block_scout_web/controllers/address_transaction_controller.ex
  7. 2
      apps/block_scout_web/lib/block_scout_web/controllers/address_validation_controller.ex
  8. 4
      apps/block_scout_web/lib/block_scout_web/controllers/tokens/holder_controller.ex
  9. 4
      apps/block_scout_web/lib/block_scout_web/controllers/tokens/inventory_controller.ex
  10. 4
      apps/block_scout_web/lib/block_scout_web/controllers/tokens/read_contract_controller.ex
  11. 4
      apps/block_scout_web/lib/block_scout_web/controllers/tokens/transfer_controller.ex
  12. 95
      apps/explorer/lib/explorer/chain.ex
  13. 4
      apps/explorer/test/explorer/chain_test.exs
  14. 4
      apps/indexer/lib/indexer/fetcher/token.ex
  15. 4
      apps/indexer/lib/indexer/fetcher/token_updater.ex

@ -12,6 +12,7 @@
- [#2291](https://github.com/poanetwork/blockscout/pull/2291) - dashboard fix for md resolution, transactions load fix, block info row fix, addresses page issue, check mark issue
### Chore
- [#2305](https://github.com/poanetwork/blockscout/pull/2305) - Improve Address controllers
- [#2302](https://github.com/poanetwork/blockscout/pull/2302) - fix names for xDai source
- [#2289](https://github.com/poanetwork/blockscout/pull/2289) - Optional websockets for dev environment
- [#2307](https://github.com/poanetwork/blockscout/pull/2307) - add GoJoy to README

@ -16,7 +16,7 @@ defmodule BlockScoutWeb.AddressCoinBalanceController do
def index(conn, %{"address_id" => address_hash_string, "type" => "JSON"} = params) do
with {:ok, address_hash} <- Chain.string_to_address_hash(address_hash_string),
{:ok, address} <- Chain.hash_to_address(address_hash) do
{:ok, address} <- Chain.hash_to_address(address_hash, [], false) do
full_options = paging_options(params)
coin_balances_plus_one = Chain.address_to_coin_balances(address_hash, full_options)

@ -16,7 +16,7 @@ defmodule BlockScoutWeb.AddressInternalTransactionController do
def index(conn, %{"address_id" => address_hash_string, "type" => "JSON"} = params) do
with {:ok, address_hash} <- Chain.string_to_address_hash(address_hash_string),
{:ok, address} <- Chain.hash_to_address(address_hash) do
{:ok, address} <- Chain.hash_to_address(address_hash, [], false) do
full_options =
[
necessity_by_association: %{

@ -16,7 +16,7 @@ defmodule BlockScoutWeb.AddressLogsController do
def index(conn, %{"address_id" => address_hash_string, "type" => "JSON"} = params) do
with {:ok, address_hash} <- Chain.string_to_address_hash(address_hash_string),
{:ok, address} <- Chain.hash_to_address(address_hash) do
{:ok, address} <- Chain.hash_to_address(address_hash, [], false) do
logs_plus_one = Chain.address_to_logs(address, paging_options(params))
{results, next_page} = split_list_by_page(logs_plus_one)
@ -74,7 +74,7 @@ defmodule BlockScoutWeb.AddressLogsController do
def search_logs(conn, %{"topic" => topic, "address_id" => address_hash_string} = params) do
with {:ok, address_hash} <- Chain.string_to_address_hash(address_hash_string),
{:ok, address} <- Chain.hash_to_address(address_hash) do
{:ok, address} <- Chain.hash_to_address(address_hash, [], false) do
topic = String.trim(topic)
formatted_topic = if String.starts_with?(topic, "0x"), do: topic, else: "0x" <> topic

@ -12,7 +12,7 @@ defmodule BlockScoutWeb.AddressTokenController do
def index(conn, %{"address_id" => address_hash_string, "type" => "JSON"} = params) do
with {:ok, address_hash} <- Chain.string_to_address_hash(address_hash_string),
{:ok, address} <- Chain.hash_to_address(address_hash) do
{:ok, address} <- Chain.hash_to_address(address_hash, [], false) do
tokens_plus_one = Chain.address_tokens_with_balance(address_hash, paging_options(params))
{tokens, next_page} = split_list_by_page(tokens_plus_one)

@ -28,8 +28,10 @@ defmodule BlockScoutWeb.AddressTransactionController do
]
def index(conn, %{"address_id" => address_hash_string, "type" => "JSON"} = params) do
address_options = [necessity_by_association: %{:names => :optional}]
with {:ok, address_hash} <- Chain.string_to_address_hash(address_hash_string),
{:ok, address} <- Chain.hash_to_address(address_hash, [:names], false) do
{:ok, address} <- Chain.hash_to_address(address_hash, address_options, false) do
options =
@transaction_necessity_by_association
|> put_in([:necessity_by_association, :block], :required)

@ -17,7 +17,7 @@ defmodule BlockScoutWeb.AddressValidationController do
def index(conn, %{"address_id" => address_hash_string, "type" => "JSON"} = params) do
with {:ok, address_hash} <- Chain.string_to_address_hash(address_hash_string),
{:ok, address} <- Chain.find_or_insert_address_from_hash(address_hash) do
{:ok, address} <- Chain.find_or_insert_address_from_hash(address_hash, [], false) do
full_options =
Keyword.merge(
[

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

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

@ -4,8 +4,10 @@ defmodule BlockScoutWeb.Tokens.ReadContractController do
alias Explorer.{Chain, Market}
def index(conn, %{"token_id" => address_hash_string}) do
options = [necessity_by_association: %{[contract_address: :smart_contract] => :optional}]
with {:ok, address_hash} <- Chain.string_to_address_hash(address_hash_string),
{:ok, token} <- Chain.token_from_address_hash(address_hash, [{:contract_address, :smart_contract}]) do
{:ok, token} <- Chain.token_from_address_hash(address_hash, options) do
render(
conn,
"index.html",

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

@ -273,12 +273,12 @@ defmodule Explorer.Chain do
inner_join: transaction in assoc(log, :transaction),
order_by: [desc: transaction.block_number, desc: transaction.index],
preload: [:transaction],
where:
log.address_hash == ^address_hash and
(transaction.block_number < ^block_number or
(transaction.block_number == ^block_number and transaction.index > ^transaction_index) or
(transaction.block_number == ^block_number and transaction.index == ^transaction_index and
log.index > ^log_index)),
where: transaction.block_number < ^block_number,
or_where: transaction.block_number == ^block_number and transaction.index > ^transaction_index,
or_where:
transaction.block_number == ^block_number and transaction.index == ^transaction_index and
log.index > ^log_index,
where: log.address_hash == ^address_hash,
limit: ^paging_options.page_size,
select: log
)
@ -676,31 +676,40 @@ defmodule Explorer.Chain do
iex> Explorer.Chain.hash_to_address(hash)
{:error, :not_found}
Optionally accepts:
- a list of bindings to preload, just like `Ecto.Query.preload/3`
- a boolean to also fetch the `has_decompiled_code?` virtual field or not
## Options
* `:necessity_by_association` - use to load `t:association/0` as `:required` or `:optional`. If an association is
`:required`, and the `t:Explorer.Chain.Address.t/0` has no associated record for that association,
then the `t:Explorer.Chain.Address.t/0` will not be included in the list.
Optionally it also accepts a boolean to fetch the `has_decompiled_code?` virtual field or not
"""
@spec hash_to_address(Hash.Address.t(), [Macro.t()], boolean()) :: {:ok, Address.t()} | {:error, :not_found}
@spec hash_to_address(Hash.Address.t(), [necessity_by_association_option], boolean()) ::
{:ok, Address.t()} | {:error, :not_found}
def hash_to_address(
%Hash{byte_count: unquote(Hash.Address.byte_count())} = hash,
preloads \\ [
:contracts_creation_internal_transaction,
:names,
:smart_contract,
:token,
:contracts_creation_transaction
options \\ [
necessity_by_association: %{
:contracts_creation_internal_transaction => :optional,
:names => :optional,
:smart_contract => :optional,
:token => :optional,
:contracts_creation_transaction => :optional
}
],
query_decompiled_code_flag \\ true
) do
necessity_by_association = Keyword.get(options, :necessity_by_association, %{})
query =
from(
address in Address,
preload: ^preloads,
where: address.hash == ^hash
)
query
|> join_associations(necessity_by_association)
|> with_decompiled_code_flag(hash, query_decompiled_code_flag)
|> Repo.one()
|> case do
@ -772,16 +781,39 @@ defmodule Explorer.Chain do
iex> {:ok, %Explorer.Chain.Address{hash: found_hash}} = Explorer.Chain.hash_to_address(hash)
iex> found_hash == hash
true
## Options
* `:necessity_by_association` - use to load `t:association/0` as `:required` or `:optional`. If an association is
`:required`, and the `t:Explorer.Chain.Address.t/0` has no associated record for that association,
then the `t:Explorer.Chain.Address.t/0` will not be included in the list.
Optionally it also accepts a boolean to fetch the `has_decompiled_code?` virtual field or not
"""
@spec find_or_insert_address_from_hash(Hash.Address.t()) :: {:ok, Address.t()}
def find_or_insert_address_from_hash(%Hash{byte_count: unquote(Hash.Address.byte_count())} = hash) do
case hash_to_address(hash) do
@spec find_or_insert_address_from_hash(Hash.Address.t(), [necessity_by_association_option], boolean()) ::
{:ok, Address.t()}
def find_or_insert_address_from_hash(
%Hash{byte_count: unquote(Hash.Address.byte_count())} = hash,
options \\ [
necessity_by_association: %{
:contracts_creation_internal_transaction => :optional,
:names => :optional,
:smart_contract => :optional,
:token => :optional,
:contracts_creation_transaction => :optional
}
],
query_decompiled_code_flag \\ true
) do
case hash_to_address(hash, options, query_decompiled_code_flag) do
{:ok, address} ->
{:ok, address}
{:error, :not_found} ->
create_address(%{hash: to_string(hash)})
hash_to_address(hash)
hash_to_address(hash, options, query_decompiled_code_flag)
end
end
@ -2715,21 +2747,30 @@ defmodule Explorer.Chain do
@doc """
Fetches a `t:Token.t/0` by an address hash.
Optionally accepts a list of bindings to preload, just like `Ecto.Query.preload/3`
## Options
* `:necessity_by_association` - use to load `t:association/0` as `:required` or `:optional`. If an association is
`:required`, and the `t:Token.t/0` has no associated record for that association,
then the `t:Token.t/0` will not be included in the list.
"""
@spec token_from_address_hash(Hash.Address.t(), [Macro.t()]) :: {:ok, Token.t()} | {:error, :not_found}
@spec token_from_address_hash(Hash.Address.t(), [necessity_by_association_option]) ::
{:ok, Token.t()} | {:error, :not_found}
def token_from_address_hash(
%Hash{byte_count: unquote(Hash.Address.byte_count())} = hash,
preloads \\ []
options \\ []
) do
necessity_by_association = Keyword.get(options, :necessity_by_association, %{})
query =
from(
token in Token,
where: token.contract_address_hash == ^hash,
preload: ^preloads
where: token.contract_address_hash == ^hash
)
case Repo.one(query) do
query
|> join_associations(necessity_by_association)
|> Repo.one()
|> case do
nil ->
{:error, :not_found}

@ -3456,9 +3456,9 @@ defmodule Explorer.ChainTest do
smart_contract = build(:smart_contract)
address = insert(:address, smart_contract: smart_contract)
token = insert(:token, contract_address: address)
options = [necessity_by_association: %{[contract_address: :smart_contract] => :optional}]
assert {:ok, result} =
Chain.token_from_address_hash(token.contract_address_hash, [{:contract_address, :smart_contract}])
assert {:ok, result} = Chain.token_from_address_hash(token.contract_address_hash, options)
assert smart_contract = result.contract_address.smart_contract
end

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

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

Loading…
Cancel
Save