diff --git a/apps/block_scout_web/lib/block_scout_web/resolvers/token_transfer.ex b/apps/block_scout_web/lib/block_scout_web/resolvers/token_transfer.ex index 38b8b3f4acaf..3fb7175c64a9 100644 --- a/apps/block_scout_web/lib/block_scout_web/resolvers/token_transfer.ex +++ b/apps/block_scout_web/lib/block_scout_web/resolvers/token_transfer.ex @@ -16,6 +16,27 @@ defmodule BlockScoutWeb.Resolvers.TokenTransfer do |> Connection.from_query(&Repo.all/1, connection_args, options(args)) end + def get_by(%{transaction_hash: hash}, args, _) do + hash + |> GraphQL.token_tx_transfers_query_by_txhash() + |> Connection.from_query(&Repo.all/1, args, options(args)) + end + + def get_by(_, %{address_hash: address_hash} = args, _) do + connection_args = Map.take(args, [:after, :before, :first, :last]) + + address_hash + |> GraphQL.token_tx_transfers_query_by_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_tx_transfers_query() + |> Connection.from_query(&Repo.all/1, connection_args, options(args)) + end + defp options(%{before: _}), do: [] defp options(%{count: count}), do: [count: count] diff --git a/apps/block_scout_web/lib/block_scout_web/schema/types.ex b/apps/block_scout_web/lib/block_scout_web/schema/types.ex index f0404d561589..a52c4e9a938f 100644 --- a/apps/block_scout_web/lib/block_scout_web/schema/types.ex +++ b/apps/block_scout_web/lib/block_scout_web/schema/types.ex @@ -14,6 +14,7 @@ defmodule BlockScoutWeb.Schema.Types do CeloValidator, CeloValidatorGroup, InternalTransaction, + TokenTransfer, Transaction } @@ -326,6 +327,7 @@ defmodule BlockScoutWeb.Schema.Types do node object(:celo_transfer, id_fetcher: &celo_transfer_id_fetcher/2) do field(:value, :decimal) field(:token, :string) + field(:token_address, :string) field(:block_number, :integer) field(:from_address_hash, :address_hash) field(:to_address_hash, :address_hash) @@ -371,6 +373,19 @@ defmodule BlockScoutWeb.Schema.Types do last * child_complexity end) end + + connection field(:token_transfer, node_type: :celo_transfer) do + arg(:count, :integer) + resolve(&TokenTransfer.get_by/3) + + complexity(fn + %{first: first}, child_complexity -> + first * child_complexity + + %{last: last}, child_complexity -> + last * child_complexity + end) + end end @desc """ diff --git a/apps/explorer/lib/explorer/graphql.ex b/apps/explorer/lib/explorer/graphql.ex index befbd26c1119..66cd59d57e22 100644 --- a/apps/explorer/lib/explorer/graphql.ex +++ b/apps/explorer/lib/explorer/graphql.ex @@ -248,6 +248,22 @@ defmodule Explorer.GraphQL do |> order_by([transaction: t], desc: t.block_number, asc: t.nonce) end + def token_tx_transfers_query_by_txhash(tx_hash) do + query = token_tx_transfers_query() + + from( + t in subquery(query), + where: t.transaction_hash == ^tx_hash, + order_by: [t.log_index] + ) + end + + def token_tx_transfers_query_by_address(address_hash) do + token_tx_transfers_query() + |> where([t], t.from_address_hash == ^address_hash or t.to_address_hash == ^address_hash) + |> order_by([transaction: t], desc: t.block_number, asc: t.nonce) + end + def txtransfers_query do token_contract_names = Util.get_token_contract_names() @@ -270,7 +286,7 @@ defmodule Explorer.GraphQL do gas_used: tx.gas_used, gas_price: tx.gas_price, fee_currency: tx.gas_currency_hash, - fee_token: fragment("coalesce(?, 'cGLD')", token.symbol), + fee_token: fragment("coalesce(?, 'CELO')", token.symbol), gateway_fee: tx.gateway_fee, gateway_fee_recipient: tx.gas_fee_recipient_hash, timestamp: b.timestamp, @@ -284,6 +300,42 @@ defmodule Explorer.GraphQL do ) end + def token_tx_transfers_query do + from( + tt in TokenTransfer, + inner_join: tx in Transaction, + as: :transaction, + on: tx.hash == tt.transaction_hash, + inner_join: b in Block, + on: tt.block_number == b.number, + left_join: wf in CeloWalletAccounts, + on: tt.from_address_hash == wf.wallet_address_hash, + left_join: wt in CeloWalletAccounts, + on: tt.to_address_hash == wt.wallet_address_hash, + left_join: token in Token, + on: tt.token_contract_address_hash == token.contract_address_hash, + select: %{ + gas_used: tx.gas_used, + gas_price: tx.gas_price, + timestamp: b.timestamp, + input: tx.input, + transaction_hash: tt.transaction_hash, + from_address_hash: tt.from_address_hash, + to_address_hash: tt.to_address_hash, + from_account_hash: wf.account_address_hash, + to_account_hash: wt.account_address_hash, + log_index: tt.log_index, + value: tt.amount, + comment: tt.comment, + token: token.symbol, + token_address: token.contract_address_hash, + nonce: tx.nonce, + block_number: tt.block_number + }, + order_by: [desc: tt.block_number, desc: tt.amount, desc: tt.log_index] + ) + end + def celo_tx_transfers_query do token_contract_names = Util.get_token_contract_names() token_symbols = Util.get_token_contract_symbols() @@ -329,6 +381,7 @@ defmodule Explorer.GraphQL do value: tt.amount, comment: tt.comment, token: tkn.token_symbol, + token_address: tt.token_contract_address_hash, nonce: tx.nonce, block_number: tt.block_number }, diff --git a/apps/indexer/lib/indexer/fetcher/celo_epoch_rewards.ex b/apps/indexer/lib/indexer/fetcher/celo_epoch_rewards.ex index 6b29fd2e2294..3b5c65342598 100644 --- a/apps/indexer/lib/indexer/fetcher/celo_epoch_rewards.ex +++ b/apps/indexer/lib/indexer/fetcher/celo_epoch_rewards.ex @@ -18,7 +18,12 @@ defmodule Indexer.Fetcher.CeloEpochRewards do @doc false def child_spec([init_options, gen_server_options]) do - Util.default_child_spec(init_options, gen_server_options, __MODULE__) + init_options_with_polling = + init_options + |> Keyword.put(:poll, true) + |> Keyword.put(:poll_interval, :timer.minutes(60)) + + Util.default_child_spec(init_options_with_polling, gen_server_options, __MODULE__) end @impl BufferedTask