From 8db83a720a3b535c23270290ef1a637460c83a1d Mon Sep 17 00:00:00 2001 From: Vadim Date: Mon, 16 Dec 2019 17:59:09 +0300 Subject: [PATCH] Retrieve epoch numbers for Claim Reward dialog popup --- .../channels/stakes_channel.ex | 44 +++++++++++++++++++ ...stakes_modal_claim_reward_content.html.eex | 14 +++--- .../lib/explorer/staking/contract_reader.ex | 6 +++ .../contracts_abi/posdao/BlockRewardAuRa.json | 23 ++++++++++ 4 files changed, 82 insertions(+), 5 deletions(-) diff --git a/apps/block_scout_web/lib/block_scout_web/channels/stakes_channel.ex b/apps/block_scout_web/lib/block_scout_web/channels/stakes_channel.ex index ae0ad159c7..297040584f 100644 --- a/apps/block_scout_web/lib/block_scout_web/channels/stakes_channel.ex +++ b/apps/block_scout_web/lib/block_scout_web/channels/stakes_channel.ex @@ -378,9 +378,25 @@ defmodule BlockScoutWeb.StakesChannel do pools = if error != nil do %{} else + block_reward_contract = ContractState.get(:block_reward_contract) + Enum.map(pools_amounts, fn {_, amounts} -> amounts end) |> Enum.zip(pools) |> 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) end @@ -434,6 +450,22 @@ defmodule BlockScoutWeb.StakesChannel do |> String.pad_leading(64, ["0"]) 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 if is_map(reason) && Map.has_key?(reason, :message) && String.length(String.trim(reason.message)) > 0 do reason.message @@ -442,6 +474,18 @@ defmodule BlockScoutWeb.StakesChannel do 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 if socket.assigns[:contract_sent] do socket diff --git a/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_modal_claim_reward_content.html.eex b/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_modal_claim_reward_content.html.eex index 4c0a088568..1dfec38f00 100644 --- a/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_modal_claim_reward_content.html.eex +++ b/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_modal_claim_reward_content.html.eex @@ -4,14 +4,18 @@