Skip to content

Commit e691194

Browse files
committed
feat: wei_to_usd_sf
1 parent b0330db commit e691194

File tree

4 files changed

+38
-6
lines changed

4 files changed

+38
-6
lines changed

explorer/lib/explorer_web/components/batches_table.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ defmodule ExplorerWeb.BatchesTable do
3131
</:col>
3232
3333
<:col :let={batch} label="Fee per proof">
34-
<%= case EthConverter.wei_to_usd(batch.fee_per_proof, 6) do %>
34+
<%= case EthConverter.wei_to_usd_sf(batch.fee_per_proof, 2) do %>
3535
<% {:ok, usd} -> %>
3636
<%= "#{usd} USD" %>
3737
<% {:error, _} -> %>

explorer/lib/explorer_web/live/eth_converter.ex

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,38 @@ defmodule EthConverter do
4343
end
4444
end
4545

46+
# rounds to significant figures, instead of decimal places
47+
def wei_to_usd_sf(wei, significant_figures \\ 3) do
48+
with eth_amount <- wei_to_eth(wei, 18),
49+
{:ok, eth_price} <- get_eth_price_usd() do
50+
usd_value =
51+
Decimal.mult(Decimal.new(to_string(eth_amount)), Decimal.new(to_string(eth_price)))
52+
53+
rounded_value = round_to_sf(usd_value, significant_figures)
54+
55+
{:ok, Decimal.to_string(rounded_value, :normal)}
56+
else
57+
{:error, reason} -> {:error, reason}
58+
end
59+
end
60+
61+
defp round_to_sf(value, significant_figures) do
62+
# Convert the value to a float and calculate the magnitude
63+
value_float = Decimal.to_float(value)
64+
magnitude = :math.log10(abs(value_float)) |> floor()
65+
66+
# Calculate the factor to multiply by for shifting the decimal point
67+
factor = :math.pow(10, significant_figures - 1 - magnitude)
68+
69+
# Round, then shift back
70+
rounded_value = value_float
71+
|> Kernel.*(factor)
72+
|> round()
73+
|> Kernel./(factor)
74+
75+
Decimal.new(Float.to_string(rounded_value))
76+
end
77+
4678
def multiply_eth_by_usd(eth, usd_price) do
4779
eth_float = to_float(eth)
4880
usd_float = to_float(usd_price)

explorer/lib/explorer_web/live/pages/batch/index.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ defmodule ExplorerWeb.Batch.Index do
1212
:empty
1313

1414
%Batches{fee_per_proof: fee_per_proof} = batch ->
15-
{_, fee_per_proof_usd} = EthConverter.wei_to_usd(fee_per_proof, 2)
15+
{_, fee_per_proof_usd} = EthConverter.wei_to_usd_sf(fee_per_proof, 2)
1616

1717
%{
1818
batch
@@ -58,7 +58,7 @@ defmodule ExplorerWeb.Batch.Index do
5858
:empty
5959

6060
%{fee_per_proof: fee_per_proof} = batch ->
61-
{_, fee_per_proof_usd} = EthConverter.wei_to_usd(fee_per_proof, 2)
61+
{_, fee_per_proof_usd} = EthConverter.wei_to_usd_sf(fee_per_proof, 2)
6262

6363
%{
6464
batch

explorer/lib/explorer_web/live/pages/home/index.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ defmodule ExplorerWeb.Home.Index do
99
avg_fee_per_proof = Batches.get_avg_fee_per_proof()
1010

1111
avg_fee_per_proof_usd =
12-
case EthConverter.wei_to_usd(avg_fee_per_proof, 2) do
12+
case EthConverter.wei_to_usd_sf(avg_fee_per_proof, 2) do
1313
{:ok, value} -> value
1414
_ -> 0
1515
end
@@ -90,7 +90,7 @@ defmodule ExplorerWeb.Home.Index do
9090
points =
9191
Enum.map(batches, fn b ->
9292
fee_per_proof =
93-
case EthConverter.wei_to_usd(b.fee_per_proof, 2) do
93+
case EthConverter.wei_to_usd_sf(b.fee_per_proof, 2) do
9494
{:ok, value} ->
9595
value
9696

@@ -117,7 +117,7 @@ defmodule ExplorerWeb.Home.Index do
117117
merkle_root: Enum.map(batches, fn b -> b.merkle_root end),
118118
fee_per_proof:
119119
Enum.map(batches, fn b ->
120-
case EthConverter.wei_to_usd(b.fee_per_proof, 2) do
120+
case EthConverter.wei_to_usd_sf(b.fee_per_proof, 2) do
121121
{:ok, value} ->
122122
value
123123

0 commit comments

Comments
 (0)