Skip to content

Commit 3094722

Browse files
feat: explorer usd conversion (#1591)
Co-authored-by: Urix <[email protected]>
1 parent 399c50c commit 3094722

File tree

14 files changed

+293
-205
lines changed

14 files changed

+293
-205
lines changed

explorer/lib/explorer/models/operators.ex

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -57,25 +57,34 @@ defmodule Operators do
5757
end
5858

5959
def get_operators_with_their_weights() do
60-
total_stake = Explorer.Repo.one(
61-
from(
62-
o in Operators,
63-
where: o.is_active == true,
64-
select: sum(o.total_stake))
65-
)
66-
67-
get_operators() |>
68-
Enum.map(
69-
fn operator ->
70-
case operator.is_active do
71-
false ->
72-
Map.from_struct(operator) |> Map.put(:weight, 0)
73-
true ->
74-
weight = Decimal.div(operator.total_stake, total_stake)
75-
Map.from_struct(operator) |> Map.put(:weight, weight)
76-
end
77-
end
60+
total_stake =
61+
Explorer.Repo.one(
62+
from(
63+
o in Operators,
64+
where: o.is_active == true,
65+
select: sum(o.total_stake)
66+
)
7867
)
68+
69+
get_operators()
70+
|> Enum.map(fn operator ->
71+
case operator.is_active do
72+
false ->
73+
Map.from_struct(operator) |> Map.put(:weight, 0)
74+
75+
true ->
76+
weight = Decimal.div(operator.total_stake, total_stake)
77+
total_stake_eth = operator.total_stake |> EthConverter.wei_to_eth(2)
78+
79+
{_, total_stake_usd} =
80+
operator.total_stake |> EthConverter.wei_to_usd(0)
81+
82+
Map.from_struct(operator)
83+
|> Map.put(:total_stake_eth, total_stake_eth)
84+
|> Map.put(:total_stake_usd, total_stake_usd)
85+
|> Map.put(:weight, weight)
86+
end
87+
end)
7988
end
8089

8190
def get_amount_of_operators do

explorer/lib/explorer/models/restakings.ex

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,21 @@ defmodule Restakings do
126126
|> EthConverter.wei_to_eth(2)
127127
end
128128
end
129+
130+
def get_restaked_amount_usd() do
131+
restaked_amount_wei =
132+
Restakings.get_aggregated_restakings()
133+
|> Map.get(:total_stake)
134+
135+
case restaked_amount_wei do
136+
nil ->
137+
nil
138+
139+
_ ->
140+
case EthConverter.wei_to_usd(restaked_amount_wei, 0) do
141+
{:ok, usd_value} -> usd_value
142+
_ -> nil
143+
end
144+
end
145+
end
129146
end

explorer/lib/explorer_web/components/assets_cta.ex

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ defmodule AssetsCTAComponent do
1111
~H"""
1212
<header>
1313
<.card_background class="min-h-24 flex flex-col md:flex-row gap-y-1 justify-between p-4">
14-
<.link navigate={~p"/operators"} class="flex flex-col justify-start gap-0.5 group">
14+
<.link navigate={~p"/operators"} class="flex-1 flex flex-col justify-start gap-0.5 group">
1515
<div class="text-muted-foreground font-semibold flex gap-2 items-center">
1616
<h2>
1717
Registered Active Operators
@@ -25,16 +25,21 @@ defmodule AssetsCTAComponent do
2525
View all active operators
2626
</.tooltip>
2727
</.link>
28-
<.link navigate={~p"/restake"} class="flex flex-col justify-start gap-0.5 group">
28+
<.link navigate={~p"/restake"} class="flex-1 flex flex-col justify-start gap-0.5 group">
2929
<div class="text-muted-foreground font-semibold flex gap-2 items-center">
3030
<h2>
3131
Total Restaked
3232
</h2>
3333
<.right_arrow />
3434
</div>
35-
<span class={["text-4xl font-bold slashed-zero"]}>
36-
<%= @total_staked |> Helpers.format_number() %> ETH
37-
</span>
35+
<div class="flex items-center justify-between flex-wrap">
36+
<span class="text-4xl font-bold slashed-zero">
37+
<%= @total_staked_usd |> Helpers.format_number() %> USD
38+
</span>
39+
<p class="text-s slashed-zero text-gray-500 mt-2">
40+
(<%= @total_staked_eth |> Helpers.format_number() %> ETH)
41+
</p>
42+
</div>
3843
<.tooltip>
3944
View all restaked assets
4045
</.tooltip>

0 commit comments

Comments
 (0)