Skip to content

Commit 4fe73da

Browse files
uri-99glpecileMauroToscano
authored
fix(explorer): count only active operators for weight (#934)
Co-authored-by: Gian <[email protected]> Co-authored-by: MauroFab <[email protected]>
1 parent dd2d0a3 commit 4fe73da

File tree

8 files changed

+75
-13
lines changed

8 files changed

+75
-13
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ operator_register_with_eigen_layer:
151151

152152
operator_mint_mock_tokens:
153153
@echo "Minting tokens"
154-
. ./scripts/mint_mock_token.sh $(CONFIG_FILE) 1000
154+
. ./scripts/mint_mock_token.sh $(CONFIG_FILE) 100000000000000000
155155

156156
operator_whitelist_devnet:
157157
@echo "Whitelisting operator"
@@ -170,7 +170,7 @@ operator_deposit_into_mock_strategy:
170170
@go run operator/cmd/main.go deposit-into-strategy \
171171
--config $(CONFIG_FILE) \
172172
--strategy-address $(STRATEGY_ADDRESS) \
173-
--amount 1000
173+
--amount 100000000000000000
174174

175175
operator_deposit_into_strategy:
176176
@echo "Depositing into strategy"

docs/3_guides/6_setup_aligned.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,9 @@ to run it using the following documentation:
534534
- [Erlang 26](https://github.com/asdf-vm/asdf-erlang)
535535
- [Elixir 1.16.2](https://elixir-ko.github.io/install.html), compiled with OTP 26
536536
- [Docker](https://docs.docker.com/get-docker/)
537+
- [NodeJS](https://nodejs.org/en/download/package-manager)
538+
- Tested with node 20 and 22
539+
- [pnpm](https://pnpm.io/installation)
537540
538541
### DB Setup
539542

explorer/lib/explorer/contract_managers/strategy_interface_manager.ex

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,12 @@ defmodule StrategyInterfaceManager do
2525
case ERC20InterfaceManager.name(token_address) do
2626
{:ok, name} -> %{strategy | name: name}
2727
error ->
28-
"Error fetching token name for #{token_address}: #{inspect(error)}" |> Logger.error()
29-
error
28+
case error do
29+
{:error, %{"code" => 3, "data" => "0x", "message" => "execution reverted"}} -> %{strategy | name: "‎"} # token has no Name (empty char), not a common practice but still an ERC20
30+
_ ->
31+
"Error fetching token name for #{token_address}: #{inspect(error)}" |> Logger.error()
32+
error
33+
end
3034
end
3135
end
3236
def fetch_token_name({:error, error}) do
@@ -37,8 +41,12 @@ defmodule StrategyInterfaceManager do
3741
case ERC20InterfaceManager.symbol(token_address) do
3842
{:ok, symbol} -> %{strategy | symbol: symbol}
3943
error ->
40-
"Error fetching token symbol for #{token_address}: #{inspect(error)}" |> Logger.error()
41-
error
44+
case error do
45+
{:error, %{"code" => 3, "data" => "0x", "message" => "execution reverted"}} -> %{strategy | symbol: "‎"} # token has no Symbol (empty char), not a common practice but still an ERC20
46+
_ ->
47+
"Error fetching token symbol for #{token_address}: #{inspect(error)}" |> Logger.error()
48+
error
49+
end
4250
end
4351
end
4452
def fetch_token_symbol({:error, error}) do

explorer/lib/explorer/models/operators.ex

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ defmodule Operators do
3232
Operators.changeset(%Operators{}, Map.from_struct(operator))
3333
end
3434

35+
def generate_new_total_stake_changeset(%{operator_address: operator_address}) do
36+
new_total_stake = StakeRegistryManager.get_stake_of_quorum_for_operator(%Restakings{operator_address: operator_address})
37+
38+
query = from(o in Operators, where: o.address == ^operator_address, select: o)
39+
operator = Explorer.Repo.one(query)
40+
41+
Operators.changeset(operator, %{total_stake: new_total_stake})
42+
end
43+
3544
def get_operator_by_address(address) do
3645
query = from(o in Operators, where: o.address == ^address, select: o)
3746
Explorer.Repo.one(query)
@@ -48,13 +57,23 @@ defmodule Operators do
4857
end
4958

5059
def get_operators_with_their_weights() do
51-
total_stake = Explorer.Repo.one(from(o in Operators, select: sum(o.total_stake)))
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+
)
5266

5367
get_operators() |>
5468
Enum.map(
5569
fn operator ->
56-
weight = Decimal.div(operator.total_stake, total_stake)
57-
Map.from_struct(operator) |> Map.put(:weight, weight)
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
5877
end
5978
)
6079
end
@@ -115,7 +134,8 @@ defmodule Operators do
115134

116135
def unregister_operator(%Operators{address: address}) do
117136
query = from(o in Operators, where: o.address == ^address)
118-
Explorer.Repo.update_all(query, set: [is_active: false])
137+
Explorer.Repo.update_all(query, set: [is_active: false, total_stake: 0])
138+
Restakings.remove_restakes_of_operator(%{operator_address: address})
119139
end
120140

121141
def get_total_stake(%Operators{} = operator) do

explorer/lib/explorer/models/quorums.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ defmodule Quorums do
1616
def handle_quorum(%Quorums{} = quorum) do
1717
strategy_addresses = StakeRegistryManager.get_strategies_of_quorum(quorum.id)
1818

19-
insert_quorum_if_not_present(quorum)
19+
insert_quorum_if_not_present(quorum) # Only for new Quorums inserted by running Quorums.handle_quorum(%Quorums{id: 0})
2020

2121
Enum.each(strategy_addresses,
2222
fn strategy_address ->

explorer/lib/explorer/models/restakings.ex

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ defmodule Restakings do
2929
Operators.get_operators()
3030
|> Enum.map(fn operator -> StakeRegistryManager.has_operator_changed_staking(%{fromBlock: from_block, operator_id: operator.id, operator_address: operator.address}) end)
3131
|> Enum.reject(fn {_operator_id, _operator_address, has_changed_stake} -> not has_changed_stake end)
32+
|> Enum.reject(fn {operator_id, _operator_address, _has_changed_stake} -> not Operators.get_operator_by_id(operator_id).is_active end)
3233
|> Enum.map(fn {operator_id, operator_address, _has_changed_stake} -> DelegationManager.get_operator_all_strategies_shares(%Operators{id: operator_id, address: operator_address}) end)
3334
|> Enum.each(&insert_or_update_restakings/1)
3435
end
@@ -54,6 +55,9 @@ defmodule Restakings do
5455
|> Ecto.Multi.update(:update_strategy_total_staked, Strategies.generate_update_total_staked_changeset(%{new_restaking: restaking}))
5556
end
5657

58+
multi = multi
59+
|> Ecto.Multi.update(:update_operator_total_stake, Operators.generate_new_total_stake_changeset(%{operator_address: restaking.operator_address}))
60+
5761
case Explorer.Repo.transaction(multi) do
5862
{:ok, _} ->
5963
"Restaking inserted or updated" |> Logger.debug()
@@ -64,7 +68,16 @@ defmodule Restakings do
6468
end
6569
end
6670

67-
def get_by_operator_and_strategy(%Restakings{operator_address: operator_address, strategy_address: strategy_address}) do
71+
def remove_restakes_of_operator(%{operator_address: operator_address}) do
72+
Logger.debug("Removing restakes of operator")
73+
query = from(r in Restakings, where: r.operator_address == ^operator_address)
74+
restakings = Explorer.Repo.all(query)
75+
76+
Explorer.Repo.delete_all(query)
77+
Enum.each(restakings, &Strategies.discount_restaking/1)
78+
end
79+
80+
def get_by_operator_and_strategy(%Restakings{operator_address: operator_address, strategy_address: strategy_address}) do
6881
query = from(
6982
r in Restakings,
7083
where: r.operator_address == ^operator_address and r.strategy_address == ^strategy_address,

explorer/lib/explorer/models/strategies.ex

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,18 @@ defmodule Strategies do
9999
Strategies.changeset(strategy, %{total_staked: new_stake})
100100
end
101101

102+
def discount_restaking(restaking) do
103+
query = from(s in Strategies,
104+
where: s.strategy_address == ^restaking.strategy_address,
105+
select: s)
106+
strategy = Explorer.Repo.one(query)
107+
108+
new_stake =
109+
strategy.total_staked
110+
|> Decimal.sub(restaking.stake)
111+
|> (fn stake -> if Decimal.compare(stake, 0) == :lt, do: Decimal.new(0), else: stake end).()
112+
113+
Strategies.changeset(strategy, %{total_staked: new_stake}) |> Explorer.Repo.update()
114+
115+
end
102116
end

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ defmodule ExplorerWeb.Restakes.Index do
5959
alt={asset.name}
6060
class="size-5 rounded-full object-scale-down text-xs truncate text-center"
6161
/>
62-
<%= asset.name %>
62+
<%= if asset.name != "‎" do %>
63+
<%= asset.name %>
64+
<% else %>
65+
<%= asset.strategy_address %>
66+
<% end %>
6367
<p class="text-muted-foreground text-sm">
6468
<%= asset.symbol %>
6569
</p>

0 commit comments

Comments
 (0)