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

Commit

Permalink
rollback to events extracted from rpc (#160)
Browse files Browse the repository at this point in the history
* rollback to events extracted from rpc

* fix warning
  • Loading branch information
SantiagoPittella authored Sep 15, 2023
1 parent 2b1113d commit 8eb7ae7
Show file tree
Hide file tree
Showing 2 changed files with 219 additions and 44 deletions.
28 changes: 17 additions & 11 deletions lib/starknet_explorer/data.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule StarknetExplorer.Data do
alias StarknetExplorer.{Rpc, Transaction, Block, TransactionReceipt, Calldata, Events}
alias StarknetExplorer.{Rpc, Transaction, Block, TransactionReceipt, Calldata}
alias StarknetExplorer.{Rpc, Transaction, Block, TransactionReceipt}
alias StarknetExplorerWeb.Utils

Expand Down Expand Up @@ -163,6 +163,22 @@ defmodule StarknetExplorer.Data do
{:ok, tx}
end

# This behavior is modified for testing porpouses.
# def get_block_events_paginated(block, pagination, network) do
# # If the entries are empty, means that the events was not fetch yet.
# with %Scrivener.Page{entries: []} <- Events.paginate_events(pagination, block.number, network) do
# :ok = Events.store_events_from_rpc(block, network)
# get_block_events_paginated(block, pagination, network)
# else
# page -> page
# end
# end
def get_block_events_paginated(block_hash, pagination, network) do
{:ok, events} = Rpc.get_block_events_paginated(block_hash, pagination, network)

events
end

def full_transaction(tx_hash, network) do
tx =
case Transaction.get_by_hash_with_receipt(tx_hash) do
Expand Down Expand Up @@ -280,16 +296,6 @@ defmodule StarknetExplorer.Data do
)
end

def get_block_events_paginated(block, pagination, network) do
# If the entries are empty, means that the events was not fetch yet.
with %Scrivener.Page{entries: []} <- Events.paginate_events(pagination, block.number, network) do
:ok = Events.store_events_from_rpc(block, network)
get_block_events_paginated(block, pagination, network)
else
page -> page
end
end

def get_class_at(block_number, contract_address, network) do
{:ok, class_hash} =
Rpc.get_class_at(%{"block_number" => block_number}, contract_address, network)
Expand Down
235 changes: 202 additions & 33 deletions lib/starknet_explorer_web/live/pages/block_detail.ex
Original file line number Diff line number Diff line change
Expand Up @@ -178,34 +178,107 @@ defmodule StarknetExplorerWeb.BlockDetailLive do
{:noreply, socket}
end

def handle_event("inc_events", _page, socket) do
page =
# This behavior was changed temporary for testing.
# def handle_event("inc_events", _page, socket) do
# page =
# Data.get_block_events_paginated(
# socket.assigns.block,
# %{page: socket.assigns.page.page_number + 1},
# socket.assigns.network
# )

# assigns = [
# page: page,
# view: "events"
# ]

# {:noreply, assign(socket, assigns)}
# end

# def handle_event("dec_events", _page, socket) do
# page =
# Data.get_block_events_paginated(
# socket.assigns.block,
# # This isn't working
# %{page: socket.assigns.page.page_number - 1},
# socket.assigns.network
# )

# assigns = [
# page: page,
# view: "events"
# ]

# {:noreply, assign(socket, assigns)}
# end

# @impl true
# def handle_event(
# "select-view",
# %{"view" => "events"},
# socket
# ) do
# page =
# Data.get_block_events_paginated(
# socket.assigns.block,
# %{},
# socket.assigns.network
# )

# assigns = [
# page: page,
# view: "events"
# ]

# {:noreply, assign(socket, assigns)}
# end

@chunk_size 30
defp get_previous_continuation_token(token) when token < @chunk_size, do: 0
defp get_previous_continuation_token(token), do: token - @chunk_size

def handle_event("inc_events", _value, socket) do
continuation_token = Map.get(socket.assigns, :idx_first, 0) + @chunk_size

events =
Data.get_block_events_paginated(
socket.assigns.block,
%{page: socket.assigns.page.page_number + 1},
socket.assigns.block.hash,
%{
"chunk_size" => @chunk_size,
"continuation_token" => Integer.to_string(continuation_token)
},
socket.assigns.network
)
)["events"]

assigns = [
page: page,
view: "events"
events: events,
view: "events",
idx_first: continuation_token,
idx_last: length(events) + continuation_token
]

{:noreply, assign(socket, assigns)}
end

def handle_event("dec_events", _page, socket) do
page =
def handle_event("dec_events", _value, socket) do
continuation_token =
get_previous_continuation_token(Map.get(socket.assigns, :idx_first, @chunk_size))

events =
Data.get_block_events_paginated(
socket.assigns.block,
# This isn't working
%{page: socket.assigns.page.page_number - 1},
socket.assigns.block.hash,
%{
"chunk_size" => @chunk_size,
"continuation_token" => Integer.to_string(continuation_token)
},
socket.assigns.network
)
)["events"]

assigns = [
page: page,
view: "events"
events: events,
view: "events",
idx_first: continuation_token,
idx_last: length(events) + continuation_token
]

{:noreply, assign(socket, assigns)}
Expand All @@ -217,16 +290,19 @@ defmodule StarknetExplorerWeb.BlockDetailLive do
%{"view" => "events"},
socket
) do
page =
events =
Data.get_block_events_paginated(
socket.assigns.block,
%{},
socket.assigns.block.hash,
%{"chunk_size" => @chunk_size},
socket.assigns.network
)

assigns = [
page: page,
view: "events"
events: events["events"],
view: "events",
idx_first: 0,
idx_last: @chunk_size,
chunk_size: @chunk_size
]

{:noreply, assign(socket, assigns)}
Expand Down Expand Up @@ -625,20 +701,22 @@ defmodule StarknetExplorerWeb.BlockDetailLive do
<div>From Address</div>
<div>Age</div>
</div>
<%= for event <- @page.entries do %>
<%= for {idx, event = %{"block_number" => block_number, "from_address" => from_address, "transaction_hash" => tx_hash}} <- Enum.with_index(@events, fn element, index -> {index, element} end) do %>
<div class="custom-list-item grid-6">
<div>
<div class="list-h">Identifier</div>
<% identifier =
Integer.to_string(block_number) <> "_" <> Integer.to_string(idx + @idx_first) %>
<div
class="flex gap-2 items-center copy-container"
id={"copy-transaction-hash-#{event.id}"}
id={"copy-transaction-hash-#{identifier}"}
phx-hook="Copy"
>
<div class="relative">
<div class="break-all text-hover-blue">
<%= live_redirect(
event.id,
to: ~p"/#{@network}/events/#{event.id}",
identifier,
to: ~p"/#{@network}/events/#{identifier}",
class: "text-hover-blue"
) %>
</div>
Expand All @@ -647,7 +725,7 @@ defmodule StarknetExplorerWeb.BlockDetailLive do
<img
class="copy-btn copy-text w-4 h-4"
src={~p"/images/copy.svg"}
data-text={event.id}
data-text={identifier}
/>
<img
class="copy-check absolute top-0 left-0 w-4 h-4 opacity-0 pointer-events-none"
Expand All @@ -662,7 +740,7 @@ defmodule StarknetExplorerWeb.BlockDetailLive do
<div class="list-h">Block Number</div>
<div>
<span class="blue-label">
<%= live_redirect(to_string(event.block_number),
<%= live_redirect(to_string(block_number),
to: ~p"/#{@network}/blocks/#{@block.hash}"
) %>
</span>
Expand All @@ -671,21 +749,21 @@ defmodule StarknetExplorerWeb.BlockDetailLive do
<div>
<div class="list-h">Transaction Hash</div>
<div>
<%= live_redirect(event.transaction_hash |> Utils.shorten_block_hash(),
to: ~p"/#{@network}/transactions/#{event.transaction_hash}"
<%= live_redirect(tx_hash |> Utils.shorten_block_hash(),
to: ~p"/#{@network}/transactions/#{tx_hash}"
) %>
</div>
</div>
<div>
<div class="list-h">Name</div>
<div>
<%= event.name %>
<%= Data.get_event_name(event, @network) %>
</div>
</div>
<div class="list-h">From Address</div>
<div>
<%= live_redirect(event.from_address |> Utils.shorten_block_hash(),
to: ~p"/#{@network}/contracts/#{event.from_address}"
<%= live_redirect(from_address |> Utils.shorten_block_hash(),
to: ~p"/#{@network}/contracts/#{from_address}"
) %>
</div>
<div>
Expand All @@ -695,13 +773,104 @@ defmodule StarknetExplorerWeb.BlockDetailLive do
</div>
<% end %>
<div>
<%= if @page.page_number > 1 do %>
<%= if @idx_first != 0 do %>
<button phx-click="dec_events">Previous</button>
<% end %>
<%= if @page.page_number < @page.total_pages do %>
Showing from <%= @idx_first %> to <%= @idx_last %>
<%= if length(@events) >= @chunk_size do %>
<button phx-click="inc_events">Next</button>
<% end %>
</div>
"""
end

# def render_info(assigns = %{block: _block, view: "events"}) do
# ~H"""
# <div class="table-th !pt-7 border-t border-gray-700 grid-6">
# <div>Identifier</div>
# <div>Block Number</div>
# <div>Transaction Hash</div>
# <div>Name</div>
# <div>From Address</div>
# <div>Age</div>
# </div>
# <%= for event <- @page.entries do %>
# <div class="custom-list-item grid-6">
# <div>
# <div class="list-h">Identifier</div>
# <div
# class="flex gap-2 items-center copy-container"
# id={"copy-transaction-hash-#{event.id}"}
# phx-hook="Copy"
# >
# <div class="relative">
# <div class="break-all text-hover-blue">
# <%= live_redirect(
# event.id,
# to: ~p"/#{@network}/events/#{event.id}",
# 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={event.id}
# />
# <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>
# <span class="blue-label">
# <%= live_redirect(to_string(event.block_number),
# to: ~p"/#{@network}/blocks/#{@block.hash}"
# ) %>
# </span>
# </div>
# </div>
# <div>
# <div class="list-h">Transaction Hash</div>
# <div>
# <%= live_redirect(event.transaction_hash |> Utils.shorten_block_hash(),
# to: ~p"/#{@network}/transactions/#{event.transaction_hash}"
# ) %>
# </div>
# </div>
# <div>
# <div class="list-h">Name</div>
# <div>
# <%= event.name %>
# </div>
# </div>
# <div class="list-h">From Address</div>
# <div>
# <%= live_redirect(event.from_address |> Utils.shorten_block_hash(),
# to: ~p"/#{@network}/contracts/#{event.from_address}"
# ) %>
# </div>
# <div>
# <div class="list-h">Age</div>
# <div><%= @block_age %></div>
# </div>
# </div>
# <% end %>
# <div>
# <%= if @page.page_number > 1 do %>
# <button phx-click="dec_events">Previous</button>
# <% end %>
# <%= if @page.page_number < @page.total_pages do %>
# <button phx-click="inc_events">Next</button>
# <% end %>
# </div>
# """
# end
end

0 comments on commit 8eb7ae7

Please sign in to comment.