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

Commit

Permalink
Add sepolia network
Browse files Browse the repository at this point in the history
  • Loading branch information
pefontana committed Jan 18, 2024
1 parent 3660ed3 commit 5f25dc2
Show file tree
Hide file tree
Showing 17 changed files with 55 additions and 16 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
export DB_TYPE= # set to postgresql if you want to use postgresql, empty otherwise (sqlite).
export DISABLE_MAINNET_SYNC=true # Set to true if you want to disable mainnet sync
export DISABLE_TESTNET_SYNC=true # Set to true if you want to disable testnet sync
export DISABLE_SEPOLIA_SYNC=true # Set to true if you want to disable sepolia sync
export RPC_API_HOST=<YOUR_RPC_API_HOST> # Set to your mainnet rpc api host, is will be used in the explorer as "Mainnet"
export TESTNET_RPC_API_HOST=<YOUR_TESTNET_RPC_API_HOST> # Set to your testnet rpc api host, is will be used in the explorer as "Testnet"
export SEPOLIA_RPC_API_HOST=<YOUR_SEPOLIA_RPC_API_HOST> # Set to your sepolia rpc api host, is will be used in the explorer as "Sepolia"
export DB_NAME=<DB_NAME> # set DB creds
export DB_USER=<DB_USER>
export DB_PASS=<DB_PASS>
Expand Down
2 changes: 1 addition & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ config :esbuild,
]

config :starknet_explorer,
allowed_networks: [:mainnet, :testnet]
allowed_networks: [:mainnet, :testnet, :sepolia]

# Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above.
Expand Down
9 changes: 8 additions & 1 deletion config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,16 @@ testnet_rpc_host =
environment variable for testnet is missing.
"""

sepolia_rpc_host =
System.get_env("SEPOLIA_RPC_API_HOST") ||
raise """
environment variable for sepolia is missing.
"""

config :starknet_explorer,
rpc_host: rpc_host,
testnet_host: testnet_rpc_host
testnet_host: testnet_rpc_host,
sepolia_host: sepolia_rpc_host

config :starknet_explorer, rpc_host: rpc_host

Expand Down
2 changes: 1 addition & 1 deletion lib/cache_request_warmer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule StarknetExplorer.Cache.BlockWarmer do
def interval, do: @interval

def execute(%{network: network})
when network in [:mainnet, :testnet] do
when network in [:mainnet, :testnet, :sepolia] do
Logger.debug("Warming cache for #{network}")
# Try/Rescue is not really an elixir/erlang thing to do, but
# I think this is the most simple way to do this, since
Expand Down
16 changes: 15 additions & 1 deletion lib/starknet_explorer/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ defmodule StarknetExplorer.Application do
]
end

sepolia_state_sync =
if System.get_env("DISABLE_SEPOLIA_SYNC") == "true" do
[]
else
# Start the State Sync System server for sepolia.
[
Supervisor.child_spec(
{StarknetExplorer.Blockchain.StateSyncSystem,
[network: :sepolia, name: :sepolia_state_sync]},
id: :sepolia_state_sync
)
]
end

children =
[
# Start the Telemetry supervisor
Expand All @@ -50,7 +64,7 @@ defmodule StarknetExplorer.Application do
# Start a worker by calling: StarknetExplorer.Worker.start_link(arg)
# {StarknetExplorer.Worker, arg}
StarknetExplorer.IndexCache
] ++ testnet_state_sync ++ mainnet_state_sync
] ++ sepolia_state_sync ++ testnet_state_sync ++ mainnet_state_sync

# See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options
Expand Down
2 changes: 1 addition & 1 deletion lib/starknet_explorer/block/block.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule StarknetExplorer.Block do
alias StarknetExplorerWeb.Utils
alias StarknetExplorer.TransactionReceipt, as: Receipt
require Logger
@networks [:mainnet, :testnet]
@networks [:mainnet, :testnet, :sepolia]
@cast_fields [
:number,
:status,
Expand Down
4 changes: 3 additions & 1 deletion lib/starknet_explorer/cache.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ defmodule StarknetExplorer.IndexCache do
def start_link(_) do
blocks_mainnet = StarknetExplorer.Data.many_blocks_with_txs("mainnet")
blocks_testnet = StarknetExplorer.Data.many_blocks_with_txs("testnet")
blocks_sepolia = StarknetExplorer.Data.many_blocks_with_txs("sepolia")

Agent.start_link(
fn ->
%{
"mainnet" => blocks_mainnet,
"testnet" => blocks_testnet
"testnet" => blocks_testnet,
"sepolia" => blocks_sepolia
}
end,
name: __MODULE__
Expand Down
2 changes: 1 addition & 1 deletion lib/starknet_explorer/class.ex
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ defmodule StarknetExplorer.Class do
]
}

@networks [:mainnet, :testnet]
@networks [:mainnet, :testnet, :sepolia]

@fields [
:declared_by_address,
Expand Down
2 changes: 1 addition & 1 deletion lib/starknet_explorer/contract.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule StarknetExplorer.Contract do
import Ecto.Query
alias StarknetExplorer.Repo

@networks [:mainnet, :testnet]
@networks [:mainnet, :testnet, :sepolia]
@fields [
:address,
:deployed_by_address,
Expand Down
2 changes: 1 addition & 1 deletion lib/starknet_explorer/events.ex
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ defmodule StarknetExplorer.Events do
field :data, {:array, :string}
field :index_in_block, :integer
field :transaction_hash, :string
field :network, Ecto.Enum, values: [:mainnet, :testnet]
field :network, Ecto.Enum, values: [:mainnet, :testnet, :sepolia]
timestamps()
end

Expand Down
2 changes: 1 addition & 1 deletion lib/starknet_explorer/message.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ defmodule StarknetExplorer.Message do
:l1_to_l2_cancelled_on_l1
]

field :network, Ecto.Enum, values: [:mainnet, :testnet]
field :network, Ecto.Enum, values: [:mainnet, :testnet, :sepolia]
# belongs_to :transaction, Transaction, foreign_key: :transaction_hash, references: :hash
timestamps()
end
Expand Down
2 changes: 1 addition & 1 deletion lib/starknet_explorer/transaction.ex
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ defmodule StarknetExplorer.Transaction do
@deploy_account_tx_fields ++
[:network, :nonce]

@networks [:mainnet, :testnet]
@networks [:mainnet, :testnet, :sepolia]

@primary_key {:hash, :string, []}
schema "transactions" do
Expand Down
2 changes: 1 addition & 1 deletion lib/starknet_explorer/transaction_receipt.ex
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ defmodule StarknetExplorer.TransactionReceipt do
# :contract_address
# ]

@networks [:mainnet, :testnet]
@networks [:mainnet, :testnet, :sepolia]

@fields @invoke_tx_receipt_fields ++
@l1_receipt_handler ++
Expand Down
8 changes: 8 additions & 0 deletions lib/starknet_explorer_web/components/layouts/root.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,14 @@
<span>Testnet</span>
</a>
</div>
<div class="w-full py-2 hover:bg-brand">
<a class="option relative pl-10" href="/sepolia">
<span class="animate-ping bg-green-500 rounded-full w-2 h-2"></span>
<span class="absolute top-1/2 left-5 transform -translate-x-1/2 -translate-y-1/2 opacity-50 bg-green-500 rounded-full w-2 h-2">
</span>
<span>Sepolia</span>
</a>
</div>
</div>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion lib/starknet_explorer_web/live/common_assigns.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule StarknetExplorerWeb.Live.CommonAssigns do
def on_mount(:network, params, _session, socket) do
socket =
case Map.get(params, "network") do
network when network in ["testnet"] ->
network when network in ["testnet", "sepolia"] ->
network =
network
|> String.to_existing_atom()
Expand Down
5 changes: 5 additions & 0 deletions lib/starknet_explorer_web/live/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ defmodule StarknetExplorerWeb.Utils do
# This case is for mainnet
def network_path(:mainnet), do: "/"
def network_path(:testnet), do: "/testnet/"
def network_path(:sepolia), do: "/sepolia/"

def network_path(:mainnet, remaining_path) do
"/#{remaining_path}"
Expand All @@ -173,6 +174,10 @@ defmodule StarknetExplorerWeb.Utils do
"/testnet/#{remaining_path}"
end

def network_path(:sepolia, remaining_path) do
"/sepolia/#{remaining_path}"
end

def format_version(<<"0x", hex_val::binary>>), do: "Cairo " <> hex_val
def format_version(nil), do: "-"
def format_version(version), do: version
Expand Down
7 changes: 4 additions & 3 deletions lib/starknet_rpc.ex
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ defmodule StarknetExplorer.Rpc do
network
)

defp send_request(method, args, network) when network in [:mainnet, :testnet] do
defp send_request(method, args, network) when network in [:mainnet, :testnet, :sepolia] do
payload = build_payload(method, args)

case cache_lookup(method, args, network) do
Expand All @@ -107,7 +107,7 @@ defmodule StarknetExplorer.Rpc do
end

defp send_request_no_cache(method, args, network)
when network in [:mainnet, :testnet] do
when network in [:mainnet, :testnet, :sepolia] do
payload = build_payload(method, args)
host = fetch_rpc_host(network)
{:ok, rsp} = post(host, payload)
Expand All @@ -116,6 +116,7 @@ defmodule StarknetExplorer.Rpc do

defp fetch_rpc_host(:mainnet), do: Application.fetch_env!(:starknet_explorer, :rpc_host)
defp fetch_rpc_host(:testnet), do: Application.fetch_env!(:starknet_explorer, :testnet_host)
defp fetch_rpc_host(:sepolia), do: Application.fetch_env!(:starknet_explorer, :sepolia_host)

defp build_payload(method, params) do
%{
Expand Down Expand Up @@ -151,7 +152,7 @@ defmodule StarknetExplorer.Rpc do

defp do_cache_lookup(cache_type, key, network)
when cache_type in [:block_cache, :tx_cache, :request_cache] and
network in [:mainnet, :testnet] do
network in [:mainnet, :testnet, :sepolia] do
cache_name = :"#{network}_#{cache_type}"

case Cachex.get(cache_name, key) do
Expand Down

0 comments on commit 5f25dc2

Please sign in to comment.