Skip to content

Commit

Permalink
feat: explorer usd conversion (#1591)
Browse files Browse the repository at this point in the history
Co-authored-by: Urix <[email protected]>
  • Loading branch information
MarcosNicolau and uri-99 authored Dec 10, 2024
1 parent 399c50c commit 3094722
Show file tree
Hide file tree
Showing 14 changed files with 293 additions and 205 deletions.
45 changes: 27 additions & 18 deletions explorer/lib/explorer/models/operators.ex
Original file line number Diff line number Diff line change
Expand Up @@ -57,25 +57,34 @@ defmodule Operators do
end

def get_operators_with_their_weights() do
total_stake = Explorer.Repo.one(
from(
o in Operators,
where: o.is_active == true,
select: sum(o.total_stake))
)

get_operators() |>
Enum.map(
fn operator ->
case operator.is_active do
false ->
Map.from_struct(operator) |> Map.put(:weight, 0)
true ->
weight = Decimal.div(operator.total_stake, total_stake)
Map.from_struct(operator) |> Map.put(:weight, weight)
end
end
total_stake =
Explorer.Repo.one(
from(
o in Operators,
where: o.is_active == true,
select: sum(o.total_stake)
)
)

get_operators()
|> Enum.map(fn operator ->
case operator.is_active do
false ->
Map.from_struct(operator) |> Map.put(:weight, 0)

true ->
weight = Decimal.div(operator.total_stake, total_stake)
total_stake_eth = operator.total_stake |> EthConverter.wei_to_eth(2)

{_, total_stake_usd} =
operator.total_stake |> EthConverter.wei_to_usd(0)

Map.from_struct(operator)
|> Map.put(:total_stake_eth, total_stake_eth)
|> Map.put(:total_stake_usd, total_stake_usd)
|> Map.put(:weight, weight)
end
end)
end

def get_amount_of_operators do
Expand Down
17 changes: 17 additions & 0 deletions explorer/lib/explorer/models/restakings.ex
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,21 @@ defmodule Restakings do
|> EthConverter.wei_to_eth(2)
end
end

def get_restaked_amount_usd() do
restaked_amount_wei =
Restakings.get_aggregated_restakings()
|> Map.get(:total_stake)

case restaked_amount_wei do
nil ->
nil

_ ->
case EthConverter.wei_to_usd(restaked_amount_wei, 0) do
{:ok, usd_value} -> usd_value
_ -> nil
end
end
end
end
15 changes: 10 additions & 5 deletions explorer/lib/explorer_web/components/assets_cta.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ defmodule AssetsCTAComponent do
~H"""
<header>
<.card_background class="min-h-24 flex flex-col md:flex-row gap-y-1 justify-between p-4">
<.link navigate={~p"/operators"} class="flex flex-col justify-start gap-0.5 group">
<.link navigate={~p"/operators"} class="flex-1 flex flex-col justify-start gap-0.5 group">
<div class="text-muted-foreground font-semibold flex gap-2 items-center">
<h2>
Registered Active Operators
Expand All @@ -25,16 +25,21 @@ defmodule AssetsCTAComponent do
View all active operators
</.tooltip>
</.link>
<.link navigate={~p"/restake"} class="flex flex-col justify-start gap-0.5 group">
<.link navigate={~p"/restake"} class="flex-1 flex flex-col justify-start gap-0.5 group">
<div class="text-muted-foreground font-semibold flex gap-2 items-center">
<h2>
Total Restaked
</h2>
<.right_arrow />
</div>
<span class={["text-4xl font-bold slashed-zero"]}>
<%= @total_staked |> Helpers.format_number() %> ETH
</span>
<div class="flex items-center justify-between flex-wrap">
<span class="text-4xl font-bold slashed-zero">
<%= @total_staked_usd |> Helpers.format_number() %> USD
</span>
<p class="text-s slashed-zero text-gray-500 mt-2">
(<%= @total_staked_eth |> Helpers.format_number() %> ETH)
</p>
</div>
<.tooltip>
View all restaked assets
</.tooltip>
Expand Down
Loading

0 comments on commit 3094722

Please sign in to comment.