This repository has been archived by the owner on Oct 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
206 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
defmodule StarknetExplorerWeb.BlockIndexLive do | ||
use StarknetExplorerWeb, :live_view | ||
alias StarknetExplorerWeb.Utils | ||
|
||
@impl true | ||
def render(assigns) do | ||
~H""" | ||
<div class="flex justify-center items-center pt-14"> | ||
<h1 class="text-white text-4xl font-mono">Blocks</h1> | ||
</div> | ||
<div class="table-block bg-[#182635]"> | ||
<div> | ||
<ul class="grid grid-cols-4 grid-flow-col text-lg gap-20 px-2 text-white/50"> | ||
<li scope="col" class="py-5">Number</li> | ||
<li scope="col" class="py-5">Block Hash</li> | ||
<li scope="col" class="py-5">Status</li> | ||
<li scope="col" class="py-5">Age</li> | ||
</ul> | ||
</div> | ||
<div id="blocks" class="px-2"> | ||
<%= for block <- @blocks do %> | ||
<ul | ||
id={"block-#{block["block_number"]}"} | ||
class="grid gap-20 grid-cols-4 auto-cols-[minmax(0,1fr)] border-b-[0.5px] border-gray-600 last:border-none border-spacing-6" | ||
> | ||
<li scope="row" class="py-4"> | ||
<%= live_redirect(to_string(block["block_number"]), | ||
to: "/block/#{block["block_number"]}", | ||
class: "text-blue-500 hover:text-blue-700 underline-none font-medium" | ||
) %> | ||
</li> | ||
<li scope="row" class="py-4"> | ||
<%= live_redirect(Utils.shorten_block_hash(block["block_hash"]), | ||
to: "/block/#{block["block_hash"]}", | ||
class: "text-blue-500 hover:text-blue-700 underline-none font-medium", | ||
title: block["block_hash"] | ||
) %> | ||
</li> | ||
<li scope="row" class="py-4"><%= block["status"] %></li> | ||
<li scope="row" class="py-4"><%= Utils.get_block_age(block) %></li> | ||
</ul> | ||
<% end %> | ||
</div> | ||
</div> | ||
""" | ||
end | ||
|
||
@impl true | ||
def mount(_params, _session, socket) do | ||
Process.send(self(), :load_blocks, []) | ||
|
||
{:ok, | ||
assign(socket, | ||
blocks: [], | ||
latest_block: [] | ||
)} | ||
end | ||
|
||
@impl true | ||
def handle_info(:load_blocks, socket) do | ||
{:noreply, | ||
assign(socket, | ||
blocks: Utils.list_blocks(), | ||
latest_block: Utils.get_latest_block_with_transactions() | ||
)} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
defmodule StarknetExplorerWeb.TransactionIndexLive do | ||
use StarknetExplorerWeb, :live_view | ||
alias StarknetExplorerWeb.Utils | ||
|
||
@impl true | ||
def render(assigns) do | ||
~H""" | ||
<div class="flex justify-center items-center pt-14"> | ||
<h1 class="text-white text-4xl font-mono">Transactions</h1> | ||
</div> | ||
<div class="table-block bg-[#182635]"> | ||
<div> | ||
<ul class="grid grid-cols-4 grid-flow-col text-lg gap-20 px-2 text-white/50"> | ||
<li scope="col" class="py-5">Transaction Hash</li> | ||
<li scope="col" class="py-5">Type</li> | ||
<li scope="col" class="py-5">Status</li> | ||
<li scope="col" class="py-5">Age</li> | ||
</ul> | ||
</div> | ||
<div id="transactions" class="px-2"> | ||
<%= for block <- @latest_block do %> | ||
<%= for {transaction, idx} <- Enum.with_index(block["transactions"]) do %> | ||
<ul | ||
id={"transaction-#{idx}"} | ||
class="grid gap-20 grid-cols-4 auto-cols-[minmax(0,1fr)] border-b-[0.5px] border-gray-600 last:border-none border-spacing-6" | ||
> | ||
<li scope="row" class="py-4"> | ||
<%= live_redirect(Utils.shorten_block_hash(transaction["transaction_hash"]), | ||
to: "/transactions/#{transaction["transaction_hash"]}", | ||
class: "text-blue-500 hover:text-blue-700 underline-none font-medium" | ||
) %> | ||
</li> | ||
<li scope="row" class="py-4"> | ||
<%= transaction["type"] %> | ||
</li> | ||
<li scope="row" class="py-4"><%= block["status"] %></li> | ||
<li scope="row" class="py-4"><%= Utils.get_block_age(block) %></li> | ||
</ul> | ||
<% end %> | ||
<% end %> | ||
</div> | ||
</div> | ||
""" | ||
end | ||
|
||
@impl true | ||
def mount(_params, _session, socket) do | ||
Process.send(self(), :load_blocks, []) | ||
|
||
{:ok, | ||
assign(socket, | ||
blocks: [], | ||
latest_block: [] | ||
)} | ||
end | ||
|
||
@impl true | ||
def handle_info(:load_blocks, socket) do | ||
{:noreply, | ||
assign(socket, | ||
blocks: Utils.list_blocks(), | ||
latest_block: Utils.get_latest_block_with_transactions() | ||
)} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
defmodule StarknetExplorerWeb.Utils do | ||
alias StarknetExplorer.Rpc | ||
alias StarknetExplorer.DateUtils | ||
|
||
def shorten_block_hash(block_hash) do | ||
"#{String.slice(block_hash, 0, 6)}...#{String.slice(block_hash, -4, 4)}" | ||
end | ||
|
||
def get_latest_block_with_transactions() do | ||
{:ok, block} = Rpc.get_block_by_number(get_latest_block_number()) | ||
[block] | ||
end | ||
|
||
def get_latest_block_number() do | ||
{:ok, latest_block} = Rpc.get_latest_block() | ||
latest_block["block_number"] | ||
end | ||
|
||
def list_blocks() do | ||
Enum.reverse(list_blocks(get_latest_block_number(), 15, [])) | ||
end | ||
|
||
def list_blocks(_block_number, 0, acc) do | ||
acc | ||
end | ||
|
||
def list_blocks(block_number, remaining, acc) do | ||
{:ok, block} = Rpc.get_block_by_number(block_number) | ||
prev_block_number = block_number - 1 | ||
list_blocks(prev_block_number, remaining - 1, [block | acc]) | ||
end | ||
|
||
def get_block_age(block) do | ||
%{minutes: minutes, hours: hours, days: days} = | ||
DateUtils.calculate_time_difference(block["timestamp"]) | ||
|
||
case days do | ||
0 -> | ||
case hours do | ||
0 -> "#{minutes} min" | ||
_ -> "#{hours} h" | ||
end | ||
|
||
_ -> | ||
"#{days} d" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters