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

Commit

Permalink
Show transactions execution resources (#246)
Browse files Browse the repository at this point in the history
* WIP

* Show builtins counters

* Handle no gateway

* minor fix

* minor fix

* remove unused alias
  • Loading branch information
pefontana authored Sep 26, 2023
1 parent ed4ae3a commit 9b21cc5
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 12 deletions.
4 changes: 3 additions & 1 deletion lib/starknet_explorer/transaction_receipt.ex
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ defmodule StarknetExplorer.TransactionReceipt do
@networks [:mainnet, :testnet, :testnet2]

@fields @invoke_tx_receipt_fields ++
@l1_receipt_handler ++ @declare_tx_receipt ++ @deploy_account_tx_receipt ++ [:network]
@l1_receipt_handler ++
@declare_tx_receipt ++ @deploy_account_tx_receipt ++ [:network, :execution_resources]
schema "transaction_receipts" do
belongs_to :transaction, Transaction
field :transaction_hash
Expand All @@ -101,6 +102,7 @@ defmodule StarknetExplorer.TransactionReceipt do
field :events, {:array, :map}
field :contract_address
field :network, Ecto.Enum, values: @networks
field :execution_resources, :map
timestamps()
end

Expand Down
43 changes: 32 additions & 11 deletions lib/starknet_explorer_web/live/transaction_live.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule StarknetExplorerWeb.TransactionLive do
use StarknetExplorerWeb, :live_view
alias StarknetExplorerWeb.Utils
alias StarknetExplorer.{Data, Message, Events}
alias StarknetExplorer.{Data, Message, Events, Gateway}

defp transaction_header(assigns) do
~H"""
Expand Down Expand Up @@ -674,21 +674,23 @@ defmodule StarknetExplorerWeb.TransactionLive do
</div>
<div class="pt-3 mb-3">
<div class="mb-5 text-gray-500 md:text-white !flex-row gap-5">
<span>Execution Resources</span><span class="gray-label text-sm">Mocked</span>
<span>Execution Resources</span>
</div>
<div class="flex flex-col lg:flex-row items-center gap-5 px-5 md:p-0">
<div class="flex flex-col justify-center items-center gap-2">
<span class="blue-label">STEPS</span> 5083
<span class="blue-label">STEPS</span> <%= "#{@transaction_receipt.execution_resources["n_steps"]}" %>
</div>
<div class="flex flex-col justify-center items-center gap-2">
<span class="green-label">MEMORY</span> 224
</div>
<div class="flex flex-col justify-center items-center gap-2">
<span class="pink-label">PEDERSEN_BUILTIN</span> 21
</div>
<div class="flex flex-col justify-center items-center gap-2">
<span class="violet-label">RANGE_CHECK_BUILTIN</span> 224
<span class="green-label">MEMORY HOLES</span> <%= "#{@transaction_receipt.execution_resources["n_memory_holes"]}" %>
</div>
<%= for {builtin_name , instance_counter} <- @transaction_receipt.execution_resources["builtin_instance_counter"] do %>
<div class="flex flex-col justify-center items-center gap-2">
<span class={Utils.builtin_color(builtin_name)}>
<%= Utils.builtin_name(builtin_name) %>
</span>
<%= instance_counter %>
</div>
<% end %>
</div>
</div>
"""
Expand Down Expand Up @@ -720,7 +722,26 @@ defmodule StarknetExplorerWeb.TransactionLive do
transaction |> Map.put(:max_fee, max_fee)
end

receipt = transaction.receipt |> Map.put(:actual_fee, actual_fee)
execution_resources =
case Application.get_env(:starknet_explorer, :enable_gateway_data) do
true ->
{:ok, receipt} =
Gateway.get_transaction_receipt(transaction_hash, socket.assigns.network)

receipt["execution_resources"]

false ->
%{
"builtin_instance_counter" => %{},
"n_memory_holes" => "-",
"n_steps" => "-"
}
end

receipt =
transaction.receipt
|> Map.put(:execution_resources, execution_resources)
|> Map.put(:actual_fee, actual_fee)

internal_calls =
case receipt.execution_status != "REVERTED" &&
Expand Down
18 changes: 18 additions & 0 deletions lib/starknet_explorer_web/live/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,24 @@ defmodule StarknetExplorerWeb.Utils do
end
end

@doc """
iex> builtin_name("range_check_builtin")
RANGE CHECK BUILTIN
"""
def builtin_name(builtin_name) do
builtin_name
|> String.replace("_", " ")
|> String.upcase()
end

def builtin_color(builtin_name) do
case builtin_name do
"pedersen_builtin" -> "pink-label"
"range_check_builtin" -> "violet-label"
_ -> "green-label"
end
end

# This case is for mainnet
def network_path(:mainnet), do: "/"
def network_path(:testnet), do: "/testnet/"
Expand Down
3 changes: 3 additions & 0 deletions lib/starknet_gateway.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ defmodule StarknetExplorer.Gateway do
def trace_transaction(transaction_hash, network),
do: gateway_request("get_transaction_trace", %{transactionHash: transaction_hash}, network)

def get_transaction_receipt(transaction_hash, network),
do: gateway_request("get_transaction_receipt", %{transactionHash: transaction_hash}, network)

defp get_block_request(query, network), do: gateway_request("get_block", query, network)

defp gateway_request(method, query, network) do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ defmodule StarknetExplorer.Repo.Migrations.TransactionReceipts do
add :messages_sent, {:array, :map}, null: true
add :events, {:array, :map}, null: true
add :contract_address, :string
add :execution_resources, :map
timestamps()
end

Expand Down

0 comments on commit 9b21cc5

Please sign in to comment.