Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Commit

Permalink
Add missing events info (#171)
Browse files Browse the repository at this point in the history
* add events index

* add events tab to transactions
  • Loading branch information
SantiagoPittella authored Sep 20, 2023
1 parent ef36145 commit b1611c2
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 59 deletions.
14 changes: 14 additions & 0 deletions lib/starknet_explorer/data.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down
111 changes: 74 additions & 37 deletions lib/starknet_explorer_web/live/event_index_live.ex
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -12,7 +15,6 @@ defmodule StarknetExplorerWeb.EventIndexLive do
<div class="max-w-7xl mx-auto">
<div class="table-header !justify-start gap-5">
<h2>Events</h2>
<span class="gray-label text-sm">Mocked</span>
</div>
<div class="table-block">
<div class="grid-6 table-th">
Expand All @@ -23,52 +25,76 @@ defmodule StarknetExplorerWeb.EventIndexLive do
<div>From Address</div>
<div>Age</div>
</div>
<%= for idx <- 0..30 do %>
<div id={"message-#{idx}"} class="grid-6 custom-list-item">
<%= 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 %>
<div class="custom-list-item grid-6">
<div>
<div class="list-h">Identifier</div>
<%= 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) %>
<div
class="flex gap-2 items-center copy-container"
id={"copy-transaction-hash-#{identifier}"}
phx-hook="Copy"
>
<div class="relative">
<div class="break-all text-hover-blue">
<%= live_redirect(
identifier,
to: ~p"/#{@network}/events/#{identifier}",
class: "text-hover-blue"
) %>
</div>
<div class="absolute top-1/2 -right-6 tranform -translate-y-1/2">
<div class="relative">
<img
class="copy-btn copy-text w-4 h-4"
src={~p"/images/copy.svg"}
data-text={identifier}
/>
<img
class="copy-check absolute top-0 left-0 w-4 h-4 opacity-0 pointer-events-none"
src={~p"/images/check-square.svg"}
/>
</div>
</div>
</div>
</div>
</div>
<div>
<div class="list-h">Block Number</div>
<div>98369</div>
<div>
<span class="blue-label">
<%= live_redirect(to_string(block_number),
to: ~p"/#{@network}/blocks/#{event["block_hash"]}"
) %>
</span>
</div>
</div>
<div>
<div class="list-h">Transaction Hash</div>
<%= live_redirect(
Utils.shorten_block_hash(
"0x01b4d24a461851e8eb9924369b5d9e23e79e8bbea6abc93eae4323462a25ddac"
),
to:
~p"/#{@network}/transactions/0x01b4d24a461851e8eb9924369b5d9e23e79e8bbea6abc93eae4323462a25ddac",
class: "text-hover-blue"
) %>
<div>
<%= live_redirect(tx_hash |> Utils.shorten_block_hash(),
to: ~p"/#{@network}/transactions/#{tx_hash}"
) %>
</div>
</div>
<div>
<div class="list-h">Name</div>
<div><span class="violet-label">transfer</span></div>
<div>
<%= Data.get_event_name(event, @network) %>
</div>
</div>
<div class="list-h">From Address</div>
<div>
<div class="list-h">From Address</div>
<%= 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}"
) %>
</div>
<div>
<div class="list-h">Age</div>
<div>7min</div>
<div>
<%= Utils.get_block_age(@block) %>
</div>
</div>
</div>
<% end %>
Expand All @@ -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
38 changes: 16 additions & 22 deletions lib/starknet_explorer_web/live/transaction_live.ex
Original file line number Diff line number Diff line change
@@ -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"""
Expand Down Expand Up @@ -108,45 +107,36 @@ defmodule StarknetExplorerWeb.TransactionLive do
<div class={"table-th !pt-7 border-t border-gray-700 #{if @transaction.sender_address, do: "grid-6", else: "grid-5"}"}>
<div>Identifier</div>
<div>Block Number</div>
<div>Transaction Hash</div>
<div>Name</div>
<%= if @transaction.sender_address do %>
<div>From Address</div>
<% end %>
<div>From Address</div>
<div>Age</div>
</div>
<%= for %{keys: [identifier | _], from_address: _} <- @events do %>
<%= for event <- @transaction_receipt.events do %>
<div class={"custom-list-item #{if @transaction.sender_address, do: "grid-6", else: "grid-5"}"}>
<div>
<div class="list-h">Identifier</div>
<div>
<%= identifier |> Utils.shorten_block_hash() %>
<%= # TODO, this wont work yet. Fix when using SQL.
@transaction_receipt.block_number %>
</div>
</div>
<div>
<div class="list-h">Block Number</div>
<div><span class="blue-label"><%= @transaction_receipt.block_number %></span></div>
</div>
<div>
<div class="list-h">Transaction Hash</div>
<div><%= @transaction.hash |> Utils.shorten_block_hash() %></div>
</div>
<div>
<div class="list-h">Name</div>
<div>
<span class="lilac-label">Transfer</span>
<span class="gray-label text-sm">Mocked</span>
<%= Data.get_event_name(event, @network) %>
</div>
</div>
<%= if @transaction.sender_address do %>
<div>
<div class="list-h">From Address</div>
<div><%= @transaction.sender_address |> Utils.shorten_block_hash() %></div>
</div>
<% end %>
<div>
<div class="list-h">From Address</div>
<div><%= event.from_address |> Utils.shorten_block_hash() %></div>
</div>
<div>
<div class="list-h">Age</div>
<div>1h</div>
<div><%= Utils.get_block_age_from_timestamp(@block_timestamp) %></div>
</div>
</div>
<% end %>
Expand Down Expand Up @@ -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)])
Expand All @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions lib/starknet_rpc.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit b1611c2

Please sign in to comment.