From b1611c2cee59f054424fb1e04810a88fe8558301 Mon Sep 17 00:00:00 2001 From: Santiago Pittella <87827390+SantiagoPittella@users.noreply.github.com> Date: Wed, 20 Sep 2023 11:09:06 -0300 Subject: [PATCH] Add missing events info (#171) * add events index * add events tab to transactions --- lib/starknet_explorer/data.ex | 14 +++ .../live/event_index_live.ex | 111 ++++++++++++------ .../live/transaction_live.ex | 38 +++--- lib/starknet_rpc.ex | 10 ++ 4 files changed, 114 insertions(+), 59 deletions(-) diff --git a/lib/starknet_explorer/data.ex b/lib/starknet_explorer/data.ex index 3f75e745..50a19d91 100644 --- a/lib/starknet_explorer/data.ex +++ b/lib/starknet_explorer/data.ex @@ -183,6 +183,10 @@ defmodule StarknetExplorer.Data do events end + def get_events(params, network) do + Rpc.get_events(params, network) + end + def full_transaction(tx_hash, network) do {:ok, tx} = transaction(tx_hash, network) receipt = tx.receipt @@ -247,6 +251,16 @@ defmodule StarknetExplorer.Data do |> _get_event_name(event_name_hashed) end + def get_event_name(%{keys: [event_name_hashed | _]} = _event, _network) + when event_name_hashed in @common_event_hashes, + do: @common_event_hash_to_name[event_name_hashed] + + def get_event_name(%{keys: [event_name_hashed | _]} = event, network) do + get_class_at(event["block_number"], event["from_address"], network) + |> Map.get("abi") + |> _get_event_name(event_name_hashed) + end + def get_event_name(%{"keys" => [event_name_hashed | _]} = _event, _network) when event_name_hashed in @common_event_hashes, do: @common_event_hash_to_name[event_name_hashed] diff --git a/lib/starknet_explorer_web/live/event_index_live.ex b/lib/starknet_explorer_web/live/event_index_live.ex index 851c0e44..ff4dee4f 100644 --- a/lib/starknet_explorer_web/live/event_index_live.ex +++ b/lib/starknet_explorer_web/live/event_index_live.ex @@ -1,6 +1,9 @@ defmodule StarknetExplorerWeb.EventIndexLive do use StarknetExplorerWeb, :live_view alias StarknetExplorerWeb.Utils + alias StarknetExplorer.{Data, BlockUtils, Rpc} + + @page_size 30 @impl true def render(assigns) do @@ -12,7 +15,6 @@ defmodule StarknetExplorerWeb.EventIndexLive do

Events

- Mocked
@@ -23,52 +25,76 @@ defmodule StarknetExplorerWeb.EventIndexLive do
From Address
Age
- <%= for idx <- 0..30 do %> -
+ <%= for {idx, event = %{"block_number" => block_number, "from_address" => from_address, "transaction_hash" => tx_hash}} <- Enum.with_index(@page["events"], fn element, index -> {index, element} end) do %> +
Identifier
- <%= live_redirect( - Utils.shorten_block_hash( - "0x01b4d24a461851e8eb9924369b5d9e23e79e8bbea6abc93eae4323462a25ddac_1" - ), - to: - ~p"/#{@network}/events/0x06e681a4da193cfd86e28a2879a17f4aedb4439d61a4a776b1e5686e9a4f96b2", - class: "text-hover-blue" - ) %> + <% identifier = + Integer.to_string(block_number) <> "_" <> Integer.to_string(idx + @page_number) %> +
+
+
+ <%= live_redirect( + identifier, + to: ~p"/#{@network}/events/#{identifier}", + class: "text-hover-blue" + ) %> +
+
+
+ + +
+
+
+
Block Number
-
98369
+
+ + <%= live_redirect(to_string(block_number), + to: ~p"/#{@network}/blocks/#{event["block_hash"]}" + ) %> + +
Transaction Hash
- <%= live_redirect( - Utils.shorten_block_hash( - "0x01b4d24a461851e8eb9924369b5d9e23e79e8bbea6abc93eae4323462a25ddac" - ), - to: - ~p"/#{@network}/transactions/0x01b4d24a461851e8eb9924369b5d9e23e79e8bbea6abc93eae4323462a25ddac", - class: "text-hover-blue" - ) %> +
+ <%= live_redirect(tx_hash |> Utils.shorten_block_hash(), + to: ~p"/#{@network}/transactions/#{tx_hash}" + ) %> +
Name
-
transfer
+
+ <%= Data.get_event_name(event, @network) %> +
+
From Address
-
From Address
- <%= live_redirect( - Utils.shorten_block_hash( - "0x073314940630fd6dcda0d772d4c972c4e0a9946bef9dabf4ef84eda8ef542b82" - ), - to: - ~p"/#{@network}/contracts/0x06e681a4da193cfd86e28a2879a17f4aedb4439d61a4a776b1e5686e9a4f96b2", - class: "text-hover-blue" + <%= live_redirect(from_address |> Utils.shorten_block_hash(), + to: ~p"/#{@network}/contracts/#{from_address}" ) %>
Age
-
7min
+
+ <%= Utils.get_block_age(@block) %> +
<% end %> @@ -79,14 +105,25 @@ defmodule StarknetExplorerWeb.EventIndexLive do @impl true def mount(_params, _session, socket) do - Process.send(self(), :load_events, []) + block_height = BlockUtils.block_height(socket.assigns.network) - 1 + {:ok, block} = Rpc.get_block_by_number(block_height, socket.assigns.network) - {:ok, assign(socket, events: [])} - end + {:ok, page} = + Data.get_events( + %{ + "chunk_size" => @page_size, + "from_block" => %{"block_number" => block_height}, + "to_block" => %{"block_number" => block_height} + }, + socket.assigns.network + ) - @impl true - def handle_info(:load_events, socket) do - # TODO: Fetch this from the db - {:noreply, assign(socket, events: [])} + assigns = [ + page: page, + page_number: 0, + block: block + ] + + {:ok, assign(socket, assigns)} end end diff --git a/lib/starknet_explorer_web/live/transaction_live.ex b/lib/starknet_explorer_web/live/transaction_live.ex index 43b9aa2e..808b3704 100644 --- a/lib/starknet_explorer_web/live/transaction_live.ex +++ b/lib/starknet_explorer_web/live/transaction_live.ex @@ -1,8 +1,7 @@ defmodule StarknetExplorerWeb.TransactionLive do use StarknetExplorerWeb, :live_view alias StarknetExplorerWeb.Utils - alias StarknetExplorer.Data - alias StarknetExplorer.Message + alias StarknetExplorer.{Data, Message, Rpc, BlockUtils} defp transaction_header(assigns) do ~H""" @@ -108,45 +107,36 @@ defmodule StarknetExplorerWeb.TransactionLive do
Identifier
Block Number
-
Transaction Hash
Name
- <%= if @transaction.sender_address do %> -
From Address
- <% end %> +
From Address
Age
- <%= for %{keys: [identifier | _], from_address: _} <- @events do %> + <%= for event <- @transaction_receipt.events do %>
Identifier
- <%= identifier |> Utils.shorten_block_hash() %> + <%= # TODO, this wont work yet. Fix when using SQL. + @transaction_receipt.block_number %>
Block Number
<%= @transaction_receipt.block_number %>
-
-
Transaction Hash
-
<%= @transaction.hash |> Utils.shorten_block_hash() %>
-
Name
- Transfer - Mocked + <%= Data.get_event_name(event, @network) %>
- <%= if @transaction.sender_address do %> -
-
From Address
-
<%= @transaction.sender_address |> Utils.shorten_block_hash() %>
-
- <% end %> +
+
From Address
+
<%= event.from_address |> Utils.shorten_block_hash() %>
+
Age
-
1h
+
<%= Utils.get_block_age_from_timestamp(@block_timestamp) %>
<% end %> @@ -580,6 +570,9 @@ defmodule StarknetExplorerWeb.TransactionLive do {:ok, transaction = %{receipt: receipt}} = Data.full_transaction(transaction_hash, socket.assigns.network) + {:ok, %{"timestamp" => block_timestamp}} = + Rpc.get_block_by_hash(receipt.block_hash, socket.assigns.network) + # a tx should not have both L1->L2 and L2->L1 messages AFAIK, but just in case merge both scenarios messages_sent = (Message.from_transaction_receipt(receipt) ++ [Message.from_transaction(transaction)]) @@ -606,7 +599,8 @@ defmodule StarknetExplorerWeb.TransactionLive do transaction_hash: transaction_hash, transaction_view: "overview", events: receipt.events, - messages: messages_sent + messages: messages_sent, + block_timestamp: block_timestamp ] socket = assign(socket, assigns) diff --git a/lib/starknet_rpc.ex b/lib/starknet_rpc.ex index 3de80e06..5b966409 100644 --- a/lib/starknet_rpc.ex +++ b/lib/starknet_rpc.ex @@ -24,6 +24,16 @@ defmodule StarknetExplorer.Rpc do network ) + def get_events(params, network), + do: + send_request( + "starknet_getEvents", + [ + params + ], + network + ) + def get_block_height_no_cache(network), do: send_request_no_cache("starknet_blockNumber", [], network)