Consider a pool to be a delegator for itself and fetch its delegator data (#2614)

When a pool is created, it adds own funds to itself. As the pool is removed,
the funds don't get withdrawn automatically, and this has to be done by user.

As we rely on maxWithdrawAllowed of a delegator to show Withdraw button and
amount available for withdraw, we have to perform this query for pool itself.
This also allows rendering delegator list without special-casing validator
and correctly showing both tooltips for currently logged-in pool.
staking
Paul Tsupikoff 5 years ago committed by Victor Baranov
parent 2344222930
commit f4ac5acb2c
  1. 10
      apps/block_scout_web/lib/block_scout_web/channels/stakes_channel.ex
  2. 21
      apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_modal_delegators_list.html.eex
  3. 9
      apps/explorer/lib/explorer/chain.ex
  4. 3
      apps/explorer/lib/explorer/staking/contract_state.ex
  5. 24
      apps/explorer/test/explorer/staking/contract_state_test.exs

@ -55,7 +55,15 @@ defmodule BlockScoutWeb.StakesChannel do
delegators =
staking_address
|> Chain.staking_pool_delegators()
|> Enum.sort_by(&(to_string(&1.delegator_address_hash) != socket.assigns[:account]))
|> Enum.sort_by(fn delegator ->
delegator_address = to_string(delegator.delegator_address_hash)
cond do
delegator_address == staking_address -> 0
delegator_address == socket.assigns[:account] -> 1
true -> 2
end
end)
html =
View.render_to_string(StakesView, "_stakes_modal_delegators_list.html",

@ -24,23 +24,7 @@
</thead>
<tbody>
<tr>
<td class="stakes-td"><div class="stakes-td-order">1</div></td>
<td class="stakes-td">
<%=
tooltip = if @pool.is_validator do "This is a validator" else false end
render BlockScoutWeb.StakesView,
"_stakes_address.html",
address: @pool.staking_address_hash,
tooltip: tooltip
%>
</td>
<td class="stakes-td"><%= format_according_to_decimals(@pool.self_staked_amount, @token.decimals) %></td>
<td class="stakes-td"><%= @pool.block_reward_ratio %>%</td>
</tr>
<%= for {delegator, index} <- Enum.with_index(@delegators, 2) do %>
<%= for {delegator, index} <- Enum.with_index(@delegators, 1) do %>
<tr>
<td class="stakes-td"><div class="stakes-td-order"><%= index %></div></td>
<td class="stakes-td">
@ -48,6 +32,9 @@
<span class="stakes-address">
<%= BlockScoutWeb.AddressView.trimmed_hash(delegator.delegator_address_hash) %>
</span>
<%= if delegator.delegator_address_hash == @pool.staking_address_hash and @pool.is_validator do %>
<%= render BlockScoutWeb.CommonComponentsView, "_check_tooltip.html", text: "This is a validator" %>
<% end %>
<%= if to_string(delegator.delegator_address_hash) == @account do %>
<div
class="me-tooltip"

@ -4842,14 +4842,7 @@ defmodule Explorer.Chain do
select: sum(delegator.stake_amount)
)
self_staked_query =
from(
pool in StakingPool,
where: pool.staking_address_hash == ^address_hash and pool.is_active,
select: sum(pool.self_staked_amount)
)
Decimal.add(Repo.one(staked_query) || Decimal.new(0), Repo.one(self_staked_query) || Decimal.new(0))
Repo.one(staked_query) || Decimal.new(0)
end
defp with_decompiled_code_flag(query, _hash, false), do: query

@ -143,7 +143,8 @@ defmodule Explorer.Staking.ContractState do
delegators =
Enum.flat_map(pool_staking_responses, fn {pool_address, responses} ->
Enum.map(responses.active_delegators, &{pool_address, &1, true}) ++
[{pool_address, pool_address, true}] ++
Enum.map(responses.active_delegators, &{pool_address, &1, true}) ++
Enum.map(responses.inactive_delegators, &{pool_address, &1, false})
end)

@ -48,7 +48,7 @@ defmodule Explorer.Staking.ContractStateTest do
assert ContractState.get(:token_contract_address) == "0x6f7a73c96bd56f8b0debc795511eda135e105ea3"
assert Repo.aggregate(StakingPool, :count, :id) == 4
assert Repo.aggregate(StakingPoolsDelegator, :count, :id) == 3
assert Repo.aggregate(StakingPoolsDelegator, :count, :id) == 7
end
defp set_init_mox() do
@ -176,10 +176,30 @@ defmodule Explorer.Staking.ContractStateTest do
EthereumJSONRPC.Mox,
:json_rpc,
fn requests, _opts ->
assert length(requests) == 15
assert length(requests) == 35
{:ok,
format_responses([
"0x0000000000000000000000000000000000000000000000000de0b6b3a7640000",
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000de0b6b3a7640000",
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000de0b6b3a7640000",
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000de0b6b3a7640000",
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000de0b6b3a7640000",
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000000000",

Loading…
Cancel
Save