From e8b81108a61a50f87b1533deefc325af6c27f9d0 Mon Sep 17 00:00:00 2001 From: Pavel Pustovalov Date: Tue, 4 Jul 2023 14:16:43 +0200 Subject: [PATCH] Show near metada for aurora tx --- .../templates/address/_near_tx.html.eex | 13 ++++++ .../templates/transaction/overview.html.eex | 46 +++++++++++++++++-- .../lib/ethereum_jsonrpc/receipt.ex | 15 ++++-- .../lib/ethereum_jsonrpc/receipts.ex | 15 +++++- apps/ethereum_jsonrpc/mix.exs | 5 +- .../lib/explorer/chain/transaction.ex | 8 +++- .../20230630163400_add_near_data.exs | 10 ++++ mix.lock | 1 + 8 files changed, 101 insertions(+), 12 deletions(-) create mode 100644 apps/block_scout_web/lib/block_scout_web/templates/address/_near_tx.html.eex create mode 100644 apps/explorer/priv/repo/migrations/20230630163400_add_near_data.exs diff --git a/apps/block_scout_web/lib/block_scout_web/templates/address/_near_tx.html.eex b/apps/block_scout_web/lib/block_scout_web/templates/address/_near_tx.html.eex new file mode 100644 index 000000000000..a095ed9ef778 --- /dev/null +++ b/apps/block_scout_web/lib/block_scout_web/templates/address/_near_tx.html.eex @@ -0,0 +1,13 @@ +<% hash = @hash %> +<% near_network = @near_network %> + +<%= cond do %> + <% near_network == "mainnet" -> %> + <%= link hash, to: "https://explorer.near.org/transactions/#{hash}", target: "_blank", rel: "nofollow noopener noreferrer", class: assigns[:class] %> + + <% near_network == "testnet" -> %> + <%= link hash, to: "https://explorer.testnet.near.org/transactions/#{hash}", target: "_blank", rel: "nofollow noopener noreferrer", class: assigns[:class] %> + + <% true -> %> + <%= hash %> +<% end %> diff --git a/apps/block_scout_web/lib/block_scout_web/templates/transaction/overview.html.eex b/apps/block_scout_web/lib/block_scout_web/templates/transaction/overview.html.eex index 08a92a4fb370..c4b9ab55abcc 100644 --- a/apps/block_scout_web/lib/block_scout_web/templates/transaction/overview.html.eex +++ b/apps/block_scout_web/lib/block_scout_web/templates/transaction/overview.html.eex @@ -7,7 +7,7 @@ <% base_fee_per_gas = if block, do: block.base_fee_per_gas, else: nil %> <% max_priority_fee_per_gas = @transaction.max_priority_fee_per_gas %> <% max_fee_per_gas = @transaction.max_fee_per_gas %> -<% burned_fee = +<% burned_fee = if !is_nil(max_fee_per_gas) and !is_nil(@transaction.gas_used) and !is_nil(base_fee_per_gas) do if Decimal.compare(max_fee_per_gas.value, 0) == :eq do %Wei{value: Decimal.new(0)} @@ -27,6 +27,10 @@ <% {:ok, created_from_address} = if to_address_hash, do: Chain.hash_to_address(to_address_hash), else: {:ok, nil} %> <% created_from_address_hash_str = if from_address_hash(created_from_address), do: "0x" <> Base.encode16(from_address_hash(created_from_address).bytes, case: :lower), else: nil %> <%= render BlockScoutWeb.Advertisement.TextAdView, "index.html", conn: @conn %> +<% near_receipt_hash = @transaction.near_receipt_hash %> +<% near_transaction_hash = @transaction.near_transaction_hash %> +<% near_network = System.get_env("NEAR_NETWORK") || nil %> +
@@ -122,7 +126,7 @@ <% true -> %> <%= render BlockScoutWeb.FormView, "_tag.html", text: formatted_status, additional_classes: ["success", "large"] %> <% end %> - + <%= if confirmations > 0 do %> <%= gettext "Confirmed by " %><%= confirmations %><%= " " <> confirmations_ds_name(confirmations) %> <% end %> @@ -390,6 +394,42 @@
<%= type %>
<% end %> + + <%= if !is_nil(near_receipt_hash) do %> +
+
+ <%= gettext "Near Receipt" %> +
+
+ + <%= near_receipt_hash %> + + <%= render BlockScoutWeb.CommonComponentsView, "_btn_copy.html", + additional_classes: ["btn-copy-icon-small", "btn-copy-icon-custom", "btn-copy-icon-no-borders"], + clipboard_text: near_receipt_hash, + aria_label: gettext("Copy Near Receipt"), + title: gettext("Copy Near Receipt") %> +
+
+ <% end %> + + <%= if !is_nil(near_transaction_hash) do %> +
+
+ <%= gettext "Near Transaction" %> +
+
+ + <%= render BlockScoutWeb.AddressView, "_near_tx.html", hash: near_transaction_hash, near_network: near_network %> + + <%= render BlockScoutWeb.CommonComponentsView, "_btn_copy.html", + additional_classes: ["btn-copy-icon-small", "btn-copy-icon-custom", "btn-copy-icon-no-borders"], + clipboard_text: near_transaction_hash, + aria_label: gettext("Copy Near Transaction"), + title: gettext("Copy Near Transaction") %> +
+
+ <% end %>
@@ -429,7 +469,7 @@
<%= format_wei_value(priority_fee, :ether) %>
- <% end %> + <% end %> <%= if !is_nil(burned_fee) do %>
diff --git a/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/receipt.ex b/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/receipt.ex index 986c41706215..bdafb826460f 100644 --- a/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/receipt.ex +++ b/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/receipt.ex @@ -117,7 +117,9 @@ defmodule EthereumJSONRPC.Receipt do created_contract_address_hash: String.t() | nil, status: status(), transaction_hash: String.t(), - transaction_index: non_neg_integer() + transaction_index: non_neg_integer(), + near_receipt_hash: String.t() | nil, + near_transaction_hash: String.t() | nil } def elixir_to_params( %{ @@ -125,7 +127,9 @@ defmodule EthereumJSONRPC.Receipt do "gasUsed" => gas_used, "contractAddress" => created_contract_address_hash, "transactionHash" => transaction_hash, - "transactionIndex" => transaction_index + "transactionIndex" => transaction_index, + "nearReceiptHash" => near_receipt_hash, + "nearTransactionHash" => near_transaction_hash } = elixir ) do status = elixir_to_status(elixir) @@ -136,7 +140,9 @@ defmodule EthereumJSONRPC.Receipt do created_contract_address_hash: created_contract_address_hash, status: status, transaction_hash: transaction_hash, - transaction_index: transaction_index + transaction_index: transaction_index, + near_receipt_hash: near_receipt_hash, + near_transaction_hash: near_transaction_hash } end @@ -268,6 +274,9 @@ defmodule EthereumJSONRPC.Receipt do {:ok, {key, result}} end + defp entry_to_elixir({"nearReceiptHash" = key, value}), do: {:ok, {key, value}} + defp entry_to_elixir({"nearTransactionHash" = key, value}), do: {:ok, {key, value}} + defp entry_to_elixir({"logs" = key, logs}) do {:ok, {key, Logs.to_elixir(logs)}} end diff --git a/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/receipts.ex b/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/receipts.ex index 8e0cffa7f2b1..5f03f3e16f99 100644 --- a/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/receipts.ex +++ b/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/receipts.ex @@ -227,8 +227,11 @@ defmodule EthereumJSONRPC.Receipts do defp response_to_receipt(%{id: id, result: receipt}, id_to_transaction_params) do %{gas: gas} = Map.fetch!(id_to_transaction_params, id) - # gas from the transaction is needed for pre-Byzantium derived status - {:ok, Map.put(receipt, "gas", gas)} + receipt + |> Map.put("gas", gas) + |> convert_hash("nearReceiptHash") + |> convert_hash("nearTransactionHash") + |> (&{:ok, &1}).() end defp response_to_receipt(%{id: id, error: reason}, id_to_transaction_params) do @@ -237,6 +240,14 @@ defmodule EthereumJSONRPC.Receipts do {:error, annotated_reason} end + defp convert_hash(receipt, key) do + hash_hex = receipt[key] |> String.slice(2..-1) + hash_bin = Base.decode16!(hash_hex, case: :lower) + hash_base58 = Base58.encode(hash_bin) + + Map.put(receipt, key, hash_base58) + end + defp reduce_responses(responses, id_to_transaction_params) when is_list(responses) and is_map(id_to_transaction_params) do responses diff --git a/apps/ethereum_jsonrpc/mix.exs b/apps/ethereum_jsonrpc/mix.exs index 4918340873de..e5fa45774acf 100644 --- a/apps/ethereum_jsonrpc/mix.exs +++ b/apps/ethereum_jsonrpc/mix.exs @@ -31,7 +31,7 @@ defmodule EthereumJsonrpc.MixProject do def application do [ mod: {EthereumJSONRPC.Application, []}, - extra_applications: [:logger] + extra_applications: [:logger, :b58] ] end @@ -85,7 +85,8 @@ defmodule EthereumJsonrpc.MixProject do {:decimal, "~> 2.0"}, {:decorator, "~> 1.4"}, {:hackney, "~> 1.18"}, - {:poolboy, "~> 1.5.2"} + {:poolboy, "~> 1.5.2"}, + {:b58, "~> 1.0.2"} ] end end diff --git a/apps/explorer/lib/explorer/chain/transaction.ex b/apps/explorer/lib/explorer/chain/transaction.ex index c6c8e3009aa6..d0079413fcd6 100644 --- a/apps/explorer/lib/explorer/chain/transaction.ex +++ b/apps/explorer/lib/explorer/chain/transaction.ex @@ -34,7 +34,7 @@ defmodule Explorer.Chain.Transaction do alias Explorer.SmartContract.SigProviderInterface @optional_attrs ~w(max_priority_fee_per_gas max_fee_per_gas block_hash block_number created_contract_address_hash cumulative_gas_used earliest_processing_start - error gas_used index created_contract_code_indexed_at status to_address_hash revert_reason type has_error_in_internal_txs)a + error gas_used index created_contract_code_indexed_at status to_address_hash revert_reason type has_error_in_internal_txs near_receipt_hash near_transaction_hash)a @required_attrs ~w(from_address_hash gas gas_price hash input nonce r s v value)a @@ -174,7 +174,9 @@ defmodule Explorer.Chain.Transaction do max_priority_fee_per_gas: wei_per_gas | nil, max_fee_per_gas: wei_per_gas | nil, type: non_neg_integer() | nil, - has_error_in_internal_txs: boolean() + has_error_in_internal_txs: boolean(), + near_receipt_hash: String.t() | nil, + near_transaction_hash: String.t() | nil } @derive {Poison.Encoder, @@ -241,6 +243,8 @@ defmodule Explorer.Chain.Transaction do field(:type, :integer) field(:has_error_in_internal_txs, :boolean) field(:has_token_transfers, :boolean, virtual: true) + field(:near_receipt_hash, :string) + field(:near_transaction_hash, :string) # A transient field for deriving old block hash during transaction upserts. # Used to force refetch of a block in case a transaction is re-collated diff --git a/apps/explorer/priv/repo/migrations/20230630163400_add_near_data.exs b/apps/explorer/priv/repo/migrations/20230630163400_add_near_data.exs new file mode 100644 index 000000000000..42d5ccaf5a60 --- /dev/null +++ b/apps/explorer/priv/repo/migrations/20230630163400_add_near_data.exs @@ -0,0 +1,10 @@ +defmodule Explorer.Repo.Migrations.AddNearData do + use Ecto.Migration + + def change do + alter table(:transactions) do + add(:near_receipt_hash, :string, null: true) + add(:near_transaction_hash, :string, null: true) + end + end +end diff --git a/mix.lock b/mix.lock index b058a11b1e9f..7f97e4ea88ad 100644 --- a/mix.lock +++ b/mix.lock @@ -4,6 +4,7 @@ "absinthe_plug": {:git, "https://github.com/blockscout/absinthe_plug.git", "c435d43f316769e1beee1dbe500b623124c96785", [tag: "1.5.3"]}, "absinthe_relay": {:hex, :absinthe_relay, "1.5.2", "cfb8aed70f4e4c7718d3f1c212332d2ea728f17c7fc0f68f1e461f0f5f0c4b9a", [:mix], [{:absinthe, "~> 1.5.0 or ~> 1.6.0 or ~> 1.7.0", [hex: :absinthe, repo: "hexpm", optional: false]}, {:ecto, "~> 2.0 or ~> 3.0", [hex: :ecto, repo: "hexpm", optional: true]}], "hexpm", "0587ee913afa31512e1457a5064ee88427f8fe7bcfbeeecd41c71d9cff0b62b6"}, "accept": {:hex, :accept, "0.3.5", "b33b127abca7cc948bbe6caa4c263369abf1347cfa9d8e699c6d214660f10cd1", [:rebar3], [], "hexpm", "11b18c220bcc2eab63b5470c038ef10eb6783bcb1fcdb11aa4137defa5ac1bb8"}, + "b58": {:hex, :b58, "1.0.3", "d300d6ae5a3de956a54b9e8220e924e4fee1a349de983df2340fe61e0e464202", [:mix], [], "hexpm", "af62a98a8661fd89978cf3a3a4b5b2ebe82209de6ac6164f0b112e36af72fc59"}, "bamboo": {:hex, :bamboo, "2.3.0", "d2392a2cabe91edf488553d3c70638b532e8db7b76b84b0a39e3dfe492ffd6fc", [:mix], [{:hackney, ">= 1.15.2", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:mime, "~> 1.4 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "dd0037e68e108fd04d0e8773921512c940e35d981e097b5793543e3b2f9cd3f6"}, "bcrypt_elixir": {:hex, :bcrypt_elixir, "3.0.1", "9be815469e6bfefec40fa74658ecbbe6897acfb57614df1416eeccd4903f602c", [:make, :mix], [{:comeonin, "~> 5.3", [hex: :comeonin, repo: "hexpm", optional: false]}, {:elixir_make, "~> 0.6", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "486bb95efb645d1efc6794c1ddd776a186a9a713abf06f45708a6ce324fb96cf"}, "benchee": {:hex, :benchee, "1.1.0", "f3a43817209a92a1fade36ef36b86e1052627fd8934a8b937ac9ab3a76c43062", [:mix], [{:deep_merge, "~> 1.0", [hex: :deep_merge, repo: "hexpm", optional: false]}, {:statistex, "~> 1.0", [hex: :statistex, repo: "hexpm", optional: false]}], "hexpm", "7da57d545003165a012b587077f6ba90b89210fd88074ce3c60ce239eb5e6d93"},