Skip to content

Commit

Permalink
fix: add token instances preloads (blockscout#10288)
Browse files Browse the repository at this point in the history
  • Loading branch information
sl1depengwyn authored Jun 20, 2024
1 parent 8cb56f7 commit 726bc84
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule BlockScoutWeb.API.V2.TokenController do

alias BlockScoutWeb.AccessHelper
alias BlockScoutWeb.API.V2.{AddressView, TransactionView}
alias Explorer.{Chain, Helper, Repo}
alias Explorer.{Chain, Helper}
alias Explorer.Chain.{Address, BridgedToken, Token, Token.Instance}
alias Explorer.Chain.CSVExport.Helper, as: CSVHelper
alias Indexer.Fetcher.OnDemand.TokenInstanceMetadataRefetch, as: TokenInstanceMetadataRefetchOnDemand
Expand Down Expand Up @@ -138,7 +138,10 @@ defmodule BlockScoutWeb.API.V2.TokenController do
{:not_found, false} <- {:not_found, Chain.erc_20_token?(token)},
{:format, {:ok, holder_address_hash}} <- {:format, Chain.string_to_address_hash(holder_address_hash_string)},
{:ok, false} <- AccessHelper.restricted_access?(holder_address_hash_string, params) do
holder_address = %Address{Repo.get_by(Address, hash: holder_address_hash) | proxy_implementations: nil}
holder_address = Address.get(holder_address_hash, @api_true)

holder_address_with_proxy_implementations =
holder_address && %Address{holder_address | proxy_implementations: nil}

results_plus_one =
Instance.token_instances_by_holder_address_hash(
Expand All @@ -158,7 +161,7 @@ defmodule BlockScoutWeb.API.V2.TokenController do
|> put_status(200)
|> put_view(AddressView)
|> render(:nft_list, %{
token_instances: token_instances |> put_owner(holder_address),
token_instances: token_instances |> put_owner(holder_address_with_proxy_implementations),
next_page_params: next_page_params,
token: token
})
Expand Down
13 changes: 11 additions & 2 deletions apps/explorer/lib/explorer/chain.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4357,7 +4357,7 @@ defmodule Explorer.Chain do
|> Instance.address_to_unique_token_instances()
|> Instance.page_token_instance(paging_options)
|> limit(^paging_options.page_size)
|> preload([_], [:owner])
|> preload([_], owner: [:names, :smart_contract, :proxy_implementations])
|> select_repo(options).all()
|> Enum.map(&put_owner_to_token_instance(&1, token, options))
end
Expand All @@ -4383,7 +4383,16 @@ defmodule Explorer.Chain do
|> Instance.owner_query()
|> select_repo(options).one()

%{token_instance | owner: select_repo(options).get_by(Address, hash: owner_address_hash)}
owner =
Address.get(
owner_address_hash,
options
|> Keyword.merge(
necessity_by_association: %{[owner: [:names, :smart_contract, :proxy_implementations]] => :optional}
)
)

%{token_instance | owner: owner}
end

def put_owner_to_token_instance(%Instance{} = token_instance, _token, _options), do: token_instance
Expand Down
11 changes: 11 additions & 0 deletions apps/explorer/lib/explorer/chain/address.ex
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,17 @@ defmodule Explorer.Chain.Address do
|> unique_constraint(:hash)
end

@spec get(Hash.Address.t(), [Chain.necessity_by_association_option() | Chain.api?()]) :: t() | nil
def get(hash, options) do
necessity_by_association = Keyword.get(options, :necessity_by_association, %{})

query = from(address in Address, where: address.hash == ^hash)

query
|> Chain.join_associations(necessity_by_association)
|> Chain.select_repo(options).one()
end

def checksum(address_or_hash, iodata? \\ false)

def checksum(nil, _iodata?), do: ""
Expand Down

0 comments on commit 726bc84

Please sign in to comment.