From a1aaab360631b75b049fe3fb2c0349dc15653e38 Mon Sep 17 00:00:00 2001 From: Enrique Ruiz Date: Mon, 6 Dec 2021 14:21:34 +0100 Subject: [PATCH] Remove filter by contract names (#496) * Fix issue with some missed transfers that are not celo related * Get rid off comments --- .../resolvers/token_transfer_tx.ex | 27 ++++++++++++ .../lib/block_scout_web/schema.ex | 20 +++++++++ apps/explorer/lib/explorer/graphql.ex | 42 +++++++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 apps/block_scout_web/lib/block_scout_web/resolvers/token_transfer_tx.ex diff --git a/apps/block_scout_web/lib/block_scout_web/resolvers/token_transfer_tx.ex b/apps/block_scout_web/lib/block_scout_web/resolvers/token_transfer_tx.ex new file mode 100644 index 000000000000..ad4f35fdf56d --- /dev/null +++ b/apps/block_scout_web/lib/block_scout_web/resolvers/token_transfer_tx.ex @@ -0,0 +1,27 @@ +defmodule BlockScoutWeb.Resolvers.TokenTransferTx do + @moduledoc false + + alias Absinthe.Relay.Connection + alias Explorer.{GraphQL, Repo} + + def get_by(_, %{address_hash: address_hash} = args, _) do + connection_args = Map.take(args, [:after, :before, :first, :last]) + + address_hash + |> GraphQL.token_txtransfers_query_for_address() + |> Connection.from_query(&Repo.all/1, connection_args, options(args)) + end + + def get_by(_, args, _) do + connection_args = Map.take(args, [:after, :before, :first, :last]) + + GraphQL.token_txtransfers_query() + |> Connection.from_query(&Repo.all/1, connection_args, options(args)) + end + + defp options(%{before: _}), do: [] + + defp options(%{count: count}), do: [count: count] + + defp options(_), do: [] +end diff --git a/apps/block_scout_web/lib/block_scout_web/schema.ex b/apps/block_scout_web/lib/block_scout_web/schema.ex index d2b909eab2f2..87a6b92ebad1 100644 --- a/apps/block_scout_web/lib/block_scout_web/schema.ex +++ b/apps/block_scout_web/lib/block_scout_web/schema.ex @@ -22,6 +22,7 @@ defmodule BlockScoutWeb.Schema do Competitor, InternalTransaction, TokenTransfer, + TokenTransferTx, Transaction } @@ -186,6 +187,25 @@ defmodule BlockScoutWeb.Schema do end) end + @desc "Token transfer transactions." + connection field(:token_transfer_txs, node_type: :transfer_tx) do + arg(:address_hash, :address_hash) + arg(:count, :integer) + + resolve(&TokenTransferTx.get_by/3) + + complexity(fn + %{first: first}, child_complexity -> + first * child_complexity + + %{last: last}, child_complexity -> + last * child_complexity + + %{}, _child_complexity -> + 0 + end) + end + @desc "Gets CELO and stable token transfer transactions." connection field(:transfer_txs, node_type: :transfer_tx) do arg(:address_hash, :address_hash) diff --git a/apps/explorer/lib/explorer/graphql.ex b/apps/explorer/lib/explorer/graphql.ex index 66cd59d57e22..7c6529d159d4 100644 --- a/apps/explorer/lib/explorer/graphql.ex +++ b/apps/explorer/lib/explorer/graphql.ex @@ -232,6 +232,17 @@ defmodule Explorer.GraphQL do ) end + def token_txtransfers_query_for_address(address_hash) do + query = + token_txtransfers_query() + |> where([t], t.to_address_hash == ^address_hash or t.from_address_hash == ^address_hash) + + from( + t in subquery(query), + order_by: [desc: t.block_number, asc: t.nonce] + ) + end + def celo_tx_transfers_query_by_txhash(tx_hash) do query = celo_tx_transfers_query() @@ -300,6 +311,37 @@ defmodule Explorer.GraphQL do ) end + def token_txtransfers_query do + from( + tt in TokenTransfer, + where: not is_nil(tt.transaction_hash), + inner_join: tx in Transaction, + on: tx.hash == tt.transaction_hash, + inner_join: b in Block, + on: tx.block_hash == b.hash, + left_join: token in Token, + on: tx.gas_currency_hash == token.contract_address_hash, + select: %{ + transaction_hash: tt.transaction_hash, + to_address_hash: tt.to_address_hash, + from_address_hash: tt.from_address_hash, + gas_used: tx.gas_used, + gas_price: tx.gas_price, + fee_currency: tx.gas_currency_hash, + fee_token: fragment("coalesce(?, 'CELO')", token.symbol), + gateway_fee: tx.gateway_fee, + gateway_fee_recipient: tx.gas_fee_recipient_hash, + timestamp: b.timestamp, + input: tx.input, + nonce: tx.nonce, + block_number: tt.block_number + }, + distinct: [desc: tt.block_number, desc: tt.transaction_hash], + # to get the ordering from distinct clause, something is needed here too + order_by: [desc: tt.from_address_hash, desc: tt.to_address_hash] + ) + end + def token_tx_transfers_query do from( tt in TokenTransfer,