Skip to content

Commit 281b246

Browse files
JulianVenturaJulian Ventura
andauthored
fix(explorer): Batch stats endpoint should return an array of summarized batches information (#1721)
Co-authored-by: Julian Ventura <[email protected]>
1 parent 1423522 commit 281b246

File tree

6 files changed

+51
-34
lines changed

6 files changed

+51
-34
lines changed

explorer/config/config.exs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77
# General application configuration
88
import Config
99

10+
config :explorer,
11+
batcher_submission_gas_cost: 125000,
12+
aggregator_gas_cost: 330000,
13+
additional_submission_gas_cost_per_proof: 2000,
14+
aggregator_fee_percentage_multiplier: 125,
15+
percentage_divider: 100
16+
1017
config :explorer,
1118
generators: [timestamp_type: :utc_datetime],
1219
tracker_api_url: System.get_env("TRACKER_API_URL")

explorer/lib/explorer/models/batches.ex

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -164,23 +164,26 @@ defmodule Batches do
164164
end
165165
end
166166

167-
def get_last_24h_verified_proof_stats() do
168-
minutes_in_a_day = 1440
169-
threshold_datetime = DateTime.utc_now() |> DateTime.add(-1 * minutes_in_a_day, :minute) # Last 24 hours
167+
# The following query was built by reading:
168+
# - https://hexdocs.pm/ecto/Ecto.Query.html#module-fragments
169+
# - https://www.postgresql.org/docs/9.1/functions-datetime.html#FUNCTIONS-DATETIME-TABLE
170+
# - https://stackoverflow.com/questions/43288914/how-to-use-date-trunc-with-timezone-in-ecto-fragment-statement
171+
# - https://glennjon.es/2016/09/24/elixir-ecto-how-to-group-and-count-records-by-week.html
172+
def get_daily_verified_batches_summary() do
173+
submission_constant_cost = Utils.constant_batch_submission_gas_cost()
174+
additional_cost_per_proof = Utils.additional_batch_submission_gas_cost_per_proof()
175+
176+
query = Batches
177+
|> where([b], b.is_verified == true)
178+
|> group_by([b], fragment("DATE_TRUNC('day', ?)", b.response_timestamp))
179+
|> select([b], %{
180+
date: fragment("DATE_TRUNC('day', ?)::date", b.response_timestamp),
181+
amount_of_proofs: sum(b.amount_of_proofs),
182+
gas_cost: sum(fragment("? + ? * ?", ^submission_constant_cost, ^additional_cost_per_proof, b.amount_of_proofs))
183+
})
184+
|> order_by([b], fragment("DATE_TRUNC('day', ?)", b.response_timestamp))
170185

171-
query = from(b in Batches,
172-
where: b.is_verified == true and b.submission_timestamp > ^threshold_datetime,
173-
select: {sum(b.amount_of_proofs), avg(b.fee_per_proof)})
174-
175-
{amount_of_proofs, avg_fee_per_proof} = case Explorer.Repo.one(query) do
176-
nil -> {0, 0.0}
177-
result -> result
178-
end
179-
180-
%{
181-
amount_of_proofs: amount_of_proofs,
182-
avg_fee_per_proof: avg_fee_per_proof
183-
}
186+
Explorer.Repo.all(query)
184187
end
185188

186189
def insert_or_update(batch_changeset, proofs) do
Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,12 @@
11
defmodule ExplorerWeb.DataController do
22
use ExplorerWeb, :controller
33

4-
def verified_proofs_in_last_24_hours(conn, _params) do
5-
%{
6-
amount_of_proofs: amount_of_proofs,
7-
avg_fee_per_proof: avg_fee_per_proof
8-
} = Batches.get_last_24h_verified_proof_stats()
9-
10-
avg_fee_per_proof_usd =
11-
case EthConverter.wei_to_usd_sf(avg_fee_per_proof, 2) do
12-
{:ok, value} -> value
13-
_ -> 0
14-
end
4+
def verified_batches_summary(conn, _params) do
5+
batches_summary =
6+
Batches.get_daily_verified_batches_summary()
157

168
render(conn, :show, %{
17-
amount_of_proofs: amount_of_proofs,
18-
avg_fee_per_proof_usd: avg_fee_per_proof_usd
9+
batches: batches_summary,
1910
})
2011
end
2112
end
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
defmodule ExplorerWeb.DataJSON do
22
def show(%{
3-
amount_of_proofs: amount_of_proofs,
4-
avg_fee_per_proof_usd: avg_fee_per_proof_usd
3+
batches: batches,
54
}) do
65
%{
7-
amount_of_proofs: amount_of_proofs,
8-
avg_fee_per_proof_usd: avg_fee_per_proof_usd
6+
batches: batches,
97
}
108
end
119
end

explorer/lib/explorer_web/live/utils.ex

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,12 @@ defmodule Utils do
205205
_ -> 268_435_456
206206
end)
207207

208+
@batcher_submission_gas_cost Application.compile_env(:explorer, :batcher_submission_gas_cost)
209+
@aggregator_gas_cost Application.compile_env(:explorer, :aggregator_gas_cost)
210+
@aggregator_fee_percentage_multiplier Application.compile_env(:explorer, :aggregator_fee_percentage_multiplier)
211+
@percentage_divider Application.compile_env(:explorer, :percentage_divider)
212+
@additional_submission_gas_cost_per_proof Application.compile_env(:explorer, :additional_submission_gas_cost_per_proof)
213+
208214
def string_to_bytes32(hex_string) do
209215
# Remove the '0x' prefix
210216
hex =
@@ -409,4 +415,16 @@ defmodule Utils do
409415
def random_id(prefix) do
410416
prefix <> "_" <> (:crypto.strong_rand_bytes(8) |> Base.url_encode64(padding: false))
411417
end
418+
419+
def constant_batch_submission_gas_cost() do
420+
trunc(
421+
@aggregator_gas_cost * @aggregator_fee_percentage_multiplier /
422+
@percentage_divider +
423+
@batcher_submission_gas_cost
424+
)
425+
end
426+
427+
def additional_batch_submission_gas_cost_per_proof() do
428+
@additional_submission_gas_cost_per_proof
429+
end
412430
end

explorer/lib/explorer_web/router.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ defmodule ExplorerWeb.Router do
3434

3535
scope "/api", ExplorerWeb do
3636
pipe_through :api
37-
get "/proofs_verified_last_24h", DataController, :verified_proofs_in_last_24_hours
37+
get "/verified_batches_summary", DataController, :verified_batches_summary
3838
end
3939

4040
scope "/", ExplorerWeb do

0 commit comments

Comments
 (0)