Add zkEVM fields to API v2 response for transaction page

pull/7584/head
POA 2 years ago
parent 7cf6202bc5
commit 9b50f4d796
  1. 9
      apps/block_scout_web/lib/block_scout_web/controllers/api/v2/transaction_controller.ex
  2. 26
      apps/block_scout_web/lib/block_scout_web/views/api/v2/transaction_view.ex
  3. 8
      apps/explorer/lib/explorer/chain/transaction.ex
  4. 2
      apps/explorer/lib/explorer/chain/zkevm_batch_txn.ex
  5. 4
      apps/explorer/priv/repo/migrations/20230420110800_create_zkevm_tables.exs

@ -63,12 +63,19 @@ defmodule BlockScoutWeb.API.V2.TransactionController do
@api_true [api?: true]
def transaction(conn, %{"transaction_hash_param" => transaction_hash_string} = params) do
necessity_by_association =
@transaction_necessity_by_association
|> Map.put(:transaction_actions, :optional)
|> Map.put(:zkevm_batch, :optional)
|> Map.put(:zkevm_sequence_txn, :optional)
|> Map.put(:zkevm_verify_txn, :optional)
with {:format, {:ok, transaction_hash}} <- {:format, Chain.string_to_transaction_hash(transaction_hash_string)},
{:not_found, {:ok, transaction}} <-
{:not_found,
Chain.hash_to_transaction(
transaction_hash,
necessity_by_association: Map.put(@transaction_necessity_by_association, :transaction_actions, :optional),
necessity_by_association: necessity_by_association,
api?: true
)},
{:ok, false} <- AccessHelper.restricted_access?(to_string(transaction.from_address_hash), params),

@ -408,12 +408,26 @@ defmodule BlockScoutWeb.API.V2.TransactionView do
"has_error_in_internal_txs" => transaction.has_error_in_internal_txs
}
if Application.get_env(:explorer, :chain_type) == "polygon_edge" && single_tx? do
result
|> Map.put("polygon_edge_deposit", polygon_edge_deposit(transaction.hash, conn))
|> Map.put("polygon_edge_withdrawal", polygon_edge_withdrawal(transaction.hash, conn))
else
result
case single_tx? && Application.get_env(:explorer, :chain_type) do
"polygon_edge" ->
result
|> Map.put("polygon_edge_deposit", polygon_edge_deposit(transaction.hash, conn))
|> Map.put("polygon_edge_withdrawal", polygon_edge_withdrawal(transaction.hash, conn))
"polygon_zkevm" ->
result
|> add_optional_transaction_field(transaction, "zkevm_batch_number", :zkevm_batch, :number)
|> add_optional_transaction_field(transaction, "zkevm_sequence_hash", :zkevm_sequence_txn, :hash)
|> add_optional_transaction_field(transaction, "zkevm_verify_hash", :zkevm_verify_txn, :hash)
_ -> result
end
end
defp add_optional_transaction_field(result, transaction, field_name, assoc_name, assoc_field) do
case Map.get(transaction, assoc_name) do
nil -> result
item -> Map.put(result, field_name, Map.get(item, assoc_field))
end
end

@ -27,7 +27,8 @@ defmodule Explorer.Chain.Transaction do
TokenTransfer,
Transaction,
TransactionAction,
Wei
Wei,
ZkevmBatchTxn
}
alias Explorer.Chain.Transaction.{Fork, Status}
@ -275,6 +276,11 @@ defmodule Explorer.Chain.Transaction do
has_many(:uncles, through: [:forks, :uncle])
has_one(:zkevm_batch_txn, ZkevmBatchTxn, foreign_key: :hash)
has_one(:zkevm_batch, through: [:zkevm_batch_txn, :batch])
has_one(:zkevm_sequence_txn, through: [:zkevm_batch, :sequence_transaction])
has_one(:zkevm_verify_txn, through: [:zkevm_batch, :verify_transaction])
belongs_to(
:created_contract_address,
Address,

@ -17,7 +17,7 @@ defmodule Explorer.Chain.ZkevmBatchTxn do
@primary_key false
schema "zkevm_batch_l2_transactions" do
belongs_to(:batch, ZkevmTxnBatch, foreign_key: :batch_number, references: :number, type: :integer)
belongs_to(:l2_transaction, Transaction, foreign_key: :hash, references: :hash, type: Hash.Full)
belongs_to(:l2_transaction, Transaction, foreign_key: :hash, primary_key: true, references: :hash, type: Hash.Full)
timestamps()
end

@ -45,10 +45,8 @@ defmodule Explorer.Repo.Migrations.CreateZkevmTables do
null: false
)
add(:hash, :bytea, null: false)
add(:hash, :bytea, null: false, primary_key: true)
timestamps(null: false, type: :utc_datetime_usec)
end
create(unique_index(:zkevm_batch_l2_transactions, :hash))
end
end

Loading…
Cancel
Save