Skip to content

Commit

Permalink
Merge pull request #1455 from zmcNotafraid/issue-1437
Browse files Browse the repository at this point in the history
Issue 1437
  • Loading branch information
zmcNotafraid committed Jun 14, 2023
2 parents 6f6c3c4 + ba870c9 commit 7742d2a
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 30 deletions.
2 changes: 1 addition & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ config :godwoken_explorer, GodwokenExplorerWeb.Endpoint,
url: [host: "localhost"],
check_origin: false,
secret_key_base: "RyKusGni7iTLOYLtHal3FRI4uKsV4mD/v25fyKBfVsxdrYChqL0IVTd07VvZoLx9",
render_errors: [view: GodwokenExplorerWeb.ErrorView, accepts: ~w(html json), layout: false],
render_errors: [view: GodwokenExplorerWeb.API.ErrorJSON, accepts: ~w(html json), layout: false],
pubsub_server: GodwokenExplorer.PubSub,
live_view: [signing_salt: "Bd1hG/MH"]

Expand Down
76 changes: 50 additions & 26 deletions lib/godwoken_explorer/graphql/resolovers/transaction.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ defmodule GodwokenExplorer.Graphql.Resolvers.Transaction do

@sorter_fields [:block_number, :index, :hash]
@default_sorter @sorter_fields
@account_tx_limit 100_000

def transaction(_parent, %{input: input} = _args, _resolution) do
query = query_with_eth_hash_or_tx_hash(input)
Expand Down Expand Up @@ -64,19 +63,38 @@ defmodule GodwokenExplorer.Graphql.Resolvers.Transaction do
|> query_with_block_range(input)
|> query_with_block_age_range(input)
|> query_with_method_id_name(input)
|> select([t], %{hash: t.hash, block_number: t.block_number, index: t.index})
|> transactions_order_by(input)
|> paginate_query(input, %{
cursor_fields: paginate_cursor(input),
total_count_primary_key_field: :hash
})
|> do_transactions()
|> do_transactions(input)
end

defp do_transactions({:error, {:not_found, []}}), do: {:ok, nil}
defp do_transactions({:error, _} = error), do: error

defp do_transactions(result) do
{:ok, result}
defp do_transactions({:error, {:not_found, []}}, _input), do: {:ok, nil}
defp do_transactions({:error, _} = error, _input), do: error

defp do_transactions(
%Paginator.Page{
metadata: metadata,
entries: entries
},
input
) do
tx_hashes = entries |> Enum.map(fn entry -> entry.hash end)

tx_results =
from(t in Transaction)
|> where([t], t.hash in ^tx_hashes)
|> transactions_order_by(input)
|> Repo.all()

{:ok,
%Paginator.Page{
metadata: metadata,
entries: tx_results
}}
end

defp query_with_account_address(query, input, from_address, to_address) do
Expand Down Expand Up @@ -149,25 +167,7 @@ defmodule GodwokenExplorer.Graphql.Resolvers.Transaction do

{from_account, to_account} ->
if input[:combine_from_to] do
base_query =
query
|> where(
[t],
t.to_account_id == ^to_account.id or t.from_account_id == ^from_account.id
)

if to_account.type == :eth_user do
tx_hashes =
Polyjuice
|> where([p], p.native_transfer_address_hash == ^to_account.eth_address)
|> select([p], p.tx_hash)
|> limit(@account_tx_limit)
|> Repo.all()

base_query |> or_where([t], t.hash in ^tx_hashes)
else
base_query
end
query_tx_hashes_by_account_type(query, from_account)
else
query
|> where(
Expand Down Expand Up @@ -287,4 +287,28 @@ defmodule GodwokenExplorer.Graphql.Resolvers.Transaction do

{:ok, Repo.get_by(Account, script_hash: l2_script_hash)}
end

defp query_tx_hashes_by_account_type(query, account) do
if account.type == :eth_user do
query |> where([t], t.from_account_id == ^account.id)

transaction_query =
from(t in Transaction,
select: t.hash,
where: t.from_account_id == ^account.id
)

polyjuice_query =
from(p in Polyjuice,
join: t in Transaction,
on: t.hash == p.tx_hash,
select: t.hash,
where: p.native_transfer_address_hash == ^account.eth_address
)

query |> where([t], t.hash in subquery(union_all(transaction_query, ^polyjuice_query)))
else
query |> where([t], t.to_account_id == ^account.id)
end
end
end
4 changes: 2 additions & 2 deletions lib/godwoken_explorer/transaction.ex
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ defmodule GodwokenExplorer.Transaction do
references: :hash
)

field :method_id, Data
field :method_name, :string
field(:method_id, Data)
field(:method_name, :string)
timestamps()
end

Expand Down
3 changes: 2 additions & 1 deletion test/graphql/transaction_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,8 @@ defmodule GodwokenExplorer.Graphql.TransactionTest do
transactions(
input: {
from_eth_address: "#{receiver_user.eth_address |> to_string()}",
to_eth_address: "#{receiver_user.eth_address |> to_string()}"
to_eth_address: "#{receiver_user.eth_address |> to_string()}",
combine_from_to: true
}
) {
entries {
Expand Down

0 comments on commit 7742d2a

Please sign in to comment.