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

Commit

Permalink
Put gateway functionality behind flag (#181)
Browse files Browse the repository at this point in the history
* put gateway functionality behind env variable flag

* update readme

* update doc

* change toc back

* fix warning
  • Loading branch information
igamigo authored Sep 21, 2023
1 parent 2844b80 commit e79724a
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 34 deletions.
1 change: 1 addition & 0 deletions .github/workflows/deploy-testing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ jobs:
GIT_BRANCH: ${{ github.head_ref || github.ref_name }}
ANSIBLE_STDOUT_CALLBACK: "yaml"
ENABLE_LISTENER: "true"
ENABLE_GATEWAY_DATA: "true"
CONTINUATION_TOKEN_FORMAT: "large"
run: |
ansible-playbook -i ansible/inventory.yaml ansible/playbooks/deployment.yaml
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

- [Requirements](#requirements)
- [Local development](#local-development)
- [Setup](#setup)
- [RPC Provider](#rpc-provider)
- [RPC with Juno](#rpc-with-juno)
- [Database](#database)
Expand Down Expand Up @@ -47,6 +48,12 @@ export TESTNET_RPC_API_HOST=testnet_rpc_hostname
export TESTNET_2_RPC_API_HOST=testnet_2_rpc_hostname
```

Some of the desired data related to Starknet is not currently available through the RPC standard. Because of this, the explorer also gets information through the feeder gateway API. In order to enable this functionality, the variable `ENABLE_GATEWAY_DATA` needs to be set to `true` (if nothing is set, it will default to `false`). Note that this is only pertinent to the Starknet networks and not other particular networks that are compatible with the RPC standard.

```bash
export ENABLE_GATEWAY_DATA=true
``````

### RPC with Juno

```bash
Expand Down
1 change: 1 addition & 0 deletions ansible/playbooks/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
aws_region: "{{ lookup('ansible.builtin.env', 'AWS_REGION') }}"
prover_storage: "{{ lookup('ansible.builtin.env', 'PROVER_STORAGE') }}"
enable_listener: "{{ lookup('ansible.builtin.env', 'ENABLE_LISTENER') }}"
enable_gateway_data: "{{ lookup('ansible.builtin.env', 'ENABLE_GATEWAY_DATA') }}"

- name: Create user systemd directory
ansible.builtin.file:
Expand Down
1 change: 1 addition & 0 deletions ansible/playbooks/templates/.env.j2
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ AWS_SECRET_ACCESS_KEY={{ aws_secret_access_key }}
AWS_REGION={{ aws_region }}
PROVER_STORAGE={{ prover_storage }}
ENABLE_LISTENER={{ enable_listener }}
ENABLE_GATEWAY_DATA={{ enable_gateway_data }}
3 changes: 2 additions & 1 deletion config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ config :starknet_explorer,
testnet_host: testnet_rpc_host,
testnet_2_host: testnet_2_rpc_host,
enable_listener: enable_listener?,
continuation_token_format: continuation_token_format
continuation_token_format: continuation_token_format,
enable_gateway_data: System.get_env("ENABLE_GATEWAY_DATA") == "true"

config :starknet_explorer, rpc_host: rpc_host

Expand Down
2 changes: 2 additions & 0 deletions docs/explorer.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ deployed contracts and so on. So, what we're missing is:
[starknet_docs_contract_classes]: https://docs.starknet.io/documentation/architecture_and_concepts/Smart_Contracts/contract-classes/
[starknet_docs_events]: https://docs.starknet.io/documentation/architecture_and_concepts/Smart_Contracts/starknet-events/

> Gateway data is fetched and shown only if the environment variable `ENABLE_GATEWAY_DATA` is set to `true`. Otherwise, that logic is skipped to be able to support networks that are only compatible with the Starknet RPC standard.
### Explorer data
As a way to have a cache and a full view into the network's data,
we're building a sqlite database. Currently it has tables for blocks,
Expand Down
11 changes: 9 additions & 2 deletions lib/starknet_explorer/block/block_updater.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@ defmodule StarknetExplorer.BlockUpdater do
network: network
}

Logger.info("starting block updater")
Process.send_after(self(), :update_gas_fee, 100)
case Application.get_env(:starknet_explorer, :enable_gateway_data) do
true ->
Logger.info("starting block updater")
Process.send_after(self(), :update_gas_fee, 100)

_ ->
:skip
end

{:ok, state}
end

Expand Down
22 changes: 17 additions & 5 deletions lib/starknet_explorer/block/block_utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,27 @@ defmodule StarknetExplorer.BlockUtils do
end

def store_block(block = %{"block_number" => block_number}, network) do
with {:ok, receipts} <- receipts_for_block(block, network),
{:ok, gateway_block = %{"gas_price" => gas_price}} <-
StarknetExplorer.Gateway.fetch_block(block_number, network) do
with {:ok, receipts} <- receipts_for_block(block, network) do
block =
block
|> Map.put("gas_fee_in_wei", gas_price)
|> Map.put("execution_resources", calculate_gateway_block_steps(gateway_block))
|> Map.put("network", network)

block =
case Application.get_env(:starknet_explorer, :enable_gateway_data) do
true ->
{:ok, gateway_block = %{"gas_price" => gas_price}} =
StarknetExplorer.Gateway.fetch_block(block_number, network)

block
|> Map.put("gas_fee_in_wei", gas_price)
|> Map.put("execution_resources", calculate_gateway_block_steps(gateway_block))

_ ->
block
|> Map.put("gas_fee_in_wei", "0")
|> Map.put("execution_resources", 0)
end

Block.insert_from_rpc_response(block, receipts, network)
end
end
Expand Down
53 changes: 31 additions & 22 deletions lib/starknet_explorer_web/live/pages/block_detail.ex
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,14 @@ defmodule StarknetExplorerWeb.BlockDetailLive do
block_age: Utils.get_block_age(block)
]

Process.send_after(self(), :get_gateway_information, 200)
case Application.get_env(:starknet_explorer, :enable_gateway_data) do
true ->
Process.send_after(self(), :get_gateway_information, 200)

_ ->
:skip
end

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

Expand Down Expand Up @@ -691,32 +698,34 @@ defmodule StarknetExplorerWeb.BlockDetailLive do
</div>
</div>
</div>
<div class="grid-4 custom-list-item">
<div class="block-label">
Gas Price
</div>
<div class="col-span-3">
<div class="flex flex-col lg:flex-row items-start lg:items-center gap-2">
<div
class="break-all bg-se-cash-green/10 text-se-cash-green rounded-full px-4 py-1"
phx-update="replace"
id="gas-price"
>
<%= @gas_price %> ETH
<%= if Application.get_env(:starknet_explorer, :enable_gateway_data) do %>
<div class="grid-4 custom-list-item">
<div class="block-label">
Gas Price
</div>
<div class="col-span-3">
<div class="flex flex-col lg:flex-row items-start lg:items-center gap-2">
<div
class="break-all bg-se-cash-green/10 text-se-cash-green rounded-full px-4 py-1"
phx-update="replace"
id="gas-price"
>
<%= @gas_price %> ETH
</div>
</div>
</div>
</div>
</div>
<div class="grid-4 custom-list-item">
<div class="block-label">
Total execution resources
</div>
<div class="col-span-3">
<div class="flex flex-col lg:flex-row items-start lg:items-center gap-2">
<%= "#{@execution_resources} steps" %>
<div class="grid-4 custom-list-item">
<div class="block-label">
Total execution resources
</div>
<div class="col-span-3">
<div class="flex flex-col lg:flex-row items-start lg:items-center gap-2">
<%= "#{@execution_resources} steps" %>
</div>
</div>
</div>
</div>
<% end %>
"""
end

Expand Down
8 changes: 4 additions & 4 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, Rpc, BlockUtils}
alias StarknetExplorer.{Data, Message, Rpc}

defp transaction_header(assigns) do
~H"""
Expand Down Expand Up @@ -584,11 +584,11 @@ defmodule StarknetExplorerWeb.TransactionLive do
transaction =
case transaction.type do
"L1_HANDLER" ->
max_fee = Utils.hex_wei_to_eth(transaction.max_fee)
transaction |> Map.put(:max_fee, max_fee)
transaction

_ ->
transaction
max_fee = Utils.hex_wei_to_eth(transaction.max_fee)
transaction |> Map.put(:max_fee, max_fee)
end

receipt = transaction.receipt |> Map.put(:actual_fee, actual_fee)
Expand Down
2 changes: 2 additions & 0 deletions lib/starknet_explorer_web/live/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ defmodule StarknetExplorerWeb.Utils do
|> Decimal.new()
|> Decimal.div(@wei_to_eth_constant)
|> Decimal.to_string(:normal)
|> String.trim_trailing("0")
|> String.replace_suffix(".", ".0")
end

def atomize_keys(map) when is_map(map) do
Expand Down

0 comments on commit e79724a

Please sign in to comment.