Retrieve epoch numbers for Claim Reward dialog popup

staking
Vadim 5 years ago committed by Victor Baranov
parent b2767d0b22
commit 8db83a720a
  1. 44
      apps/block_scout_web/lib/block_scout_web/channels/stakes_channel.ex
  2. 14
      apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_modal_claim_reward_content.html.eex
  3. 6
      apps/explorer/lib/explorer/staking/contract_reader.ex
  4. 23
      apps/explorer/priv/contracts_abi/posdao/BlockRewardAuRa.json

@ -378,9 +378,25 @@ defmodule BlockScoutWeb.StakesChannel do
pools = if error != nil do pools = if error != nil do
%{} %{}
else else
block_reward_contract = ContractState.get(:block_reward_contract)
Enum.map(pools_amounts, fn {_, amounts} -> amounts end) Enum.map(pools_amounts, fn {_, amounts} -> amounts end)
|> Enum.zip(pools) |> Enum.zip(pools)
|> Enum.filter(fn {amounts, _} -> amounts.token_reward_sum > 0 || amounts.native_reward_sum > 0 end) |> Enum.filter(fn {amounts, _} -> amounts.token_reward_sum > 0 || amounts.native_reward_sum > 0 end)
|> Enum.map(fn {amounts, pool_staking_address} ->
responses =
ContractReader.epochs_to_claim_reward_from_request(pool_staking_address, staker)
|> ContractReader.perform_requests(%{block_reward: block_reward_contract.address}, block_reward_contract.abi)
epochs =
array_to_ranges(responses[:epochs])
|> Enum.map(fn {first, last} ->
Integer.to_string(first) <> (if first != last, do: "-" <> Integer.to_string(last), else: "")
end)
data = Map.put(amounts, :epochs, Enum.join(epochs, ","))
{data, pool_staking_address}
end)
|> Map.new(fn {val, key} -> {key, val} end) |> Map.new(fn {val, key} -> {key, val} end)
end end
@ -434,6 +450,22 @@ defmodule BlockScoutWeb.StakesChannel do
|> String.pad_leading(64, ["0"]) |> String.pad_leading(64, ["0"])
end end
defp array_to_ranges(numbers, prev_ranges \\ []) do
length = Enum.count(numbers)
if length > 0 do
{first, last, next_index} = get_range(numbers)
ranges = prev_ranges ++ [{first, last}]
if next_index == 0 || next_index >= length do
ranges
else
Enum.slice(numbers, next_index, length - next_index)
|> array_to_ranges(ranges)
end
else
[]
end
end
defp error_reason_to_string(reason) do defp error_reason_to_string(reason) do
if is_map(reason) && Map.has_key?(reason, :message) && String.length(String.trim(reason.message)) > 0 do if is_map(reason) && Map.has_key?(reason, :message) && String.length(String.trim(reason.message)) > 0 do
reason.message reason.message
@ -442,6 +474,18 @@ defmodule BlockScoutWeb.StakesChannel do
end end
end end
defp get_range(numbers) do
last_index =
Enum.with_index(numbers)
|> Enum.find_index(fn {n, i} ->
if i > 0, do: n != Enum.at(numbers, i - 1) + 1, else: false
end)
next_index = if last_index == nil, do: Enum.count(numbers), else: last_index
first = Enum.at(numbers, 0)
last = Enum.at(numbers, next_index - 1)
{first, last, next_index}
end
defp push_staking_contract(socket) do defp push_staking_contract(socket) do
if socket.assigns[:contract_sent] do if socket.assigns[:contract_sent] do
socket socket

@ -4,14 +4,18 @@
<div class="input-group form-group"> <div class="input-group form-group">
<select pool-select class="form-control"> <select pool-select class="form-control">
<option disabled selected><%= gettext("Choose Pool") %></option> <option disabled selected><%= gettext("Choose Pool") %></option>
<%= for {pool_staking_address, amounts} <- @pools do %> <%= for {pool_staking_address, data} <- @pools do %>
<option value="<%= pool_staking_address %>"> <%
token_reward_sum = format_token_amount(data.token_reward_sum, @token, digits: 5, ellipsize: false, symbol: false)
native_reward_sum = format_token_amount(data.native_reward_sum, @coin, digits: 5, ellipsize: false, symbol: false)
%>
<option value="<%= pool_staking_address %>" data-token-reward-sum="<%= token_reward_sum %>" data-native-reward-sum="<%= native_reward_sum %>" data-epochs="<%= data.epochs %>">
<%= <%=
BlockScoutWeb.AddressView.trimmed_hash(pool_staking_address) <> BlockScoutWeb.AddressView.trimmed_hash(pool_staking_address) <>
" (" <> " (" <>
format_token_amount(amounts.token_reward_sum, @token, digits: 5, ellipsize: false, symbol: true) <> token_reward_sum <> " " <> @token.symbol <>
", " <> ", " <>
format_token_amount(amounts.native_reward_sum, @coin, digits: 5, ellipsize: false, symbol: true) <> native_reward_sum <> " " <> @coin.symbol <>
")" ")"
%> %>
</option> </option>
@ -24,7 +28,7 @@
<%= if @error do %> <%= if @error do %>
<%= @error %> <%= @error %>
<% else %> <% else %>
<%= gettext("Unable to find any pools you staked into.") %> <%= gettext("Unable to find any pools you could claim a reward from.") %>
<% end %> <% end %>
</p> </p>
<% end %> <% end %>

@ -99,6 +99,12 @@ defmodule Explorer.Staking.ContractReader do
] ]
end end
def epochs_to_claim_reward_from_request(staking_address, staker) do
[
epochs: {:block_reward, "epochsToClaimRewardFrom", [staking_address, staker]}
]
end
def pool_staking_requests(staking_address, block_number) do def pool_staking_requests(staking_address, block_number) do
[ [
active_delegators: active_delegators_request(staking_address, block_number)[:active_delegators], active_delegators: active_delegators_request(staking_address, block_number)[:active_delegators],

@ -1,4 +1,27 @@
[ [
{
"constant": true,
"inputs": [
{
"name": "_poolStakingAddress",
"type": "address"
},
{
"name": "_staker",
"type": "address"
}
],
"name": "epochsToClaimRewardFrom",
"outputs": [
{
"name": "epochsToClaimFrom",
"type": "uint256[]"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{ {
"constant": true, "constant": true,
"inputs": [], "inputs": [],

Loading…
Cancel
Save