From f5817805580be8fe934e71021e2787eb80ea14ef Mon Sep 17 00:00:00 2001 From: Vadim Date: Tue, 17 Dec 2019 16:46:15 +0300 Subject: [PATCH] Add general elements to Claim Reward dialog popup --- .../stakes/_modal_claim_reward.scss | 39 +++++++++++ apps/block_scout_web/assets/css/stakes.scss | 1 + .../assets/js/pages/stakes/claim_reward.js | 44 +++++++++--- .../channels/stakes_channel.ex | 70 ++++++++++++++----- .../stakes/_stakes_btn_stake.html.eex | 2 +- .../stakes/_stakes_btn_withdraw.html.eex | 2 +- .../_stakes_modal_claim_reward.html.eex | 3 +- ...stakes_modal_claim_reward_content.html.eex | 35 +++++++++- .../lib/explorer/staking/contract_reader.ex | 57 +++++++++++++++ 9 files changed, 219 insertions(+), 34 deletions(-) create mode 100644 apps/block_scout_web/assets/css/components/stakes/_modal_claim_reward.scss diff --git a/apps/block_scout_web/assets/css/components/stakes/_modal_claim_reward.scss b/apps/block_scout_web/assets/css/components/stakes/_modal_claim_reward.scss new file mode 100644 index 0000000000..dc32cf9ea5 --- /dev/null +++ b/apps/block_scout_web/assets/css/components/stakes/_modal_claim_reward.scss @@ -0,0 +1,39 @@ +.modal-claim-reward { + max-width: 400px; + width: 100%; + + @include media-breakpoint-down(sm) { + margin-left: auto; + margin-right: auto; + } +} + +.modal-claim-reward { + label { + margin-bottom: 0; + } + + p { + margin-bottom: 0.3rem; + width: 100%; + + &.m-b-0 { + margin-bottom: 0; + } + + &.left { + width: 49%; + float: left; + } + + &.right { + width: 49%; + float: right; + } + } + + textarea { + background: #fff!important; + height: 38px; + } +} diff --git a/apps/block_scout_web/assets/css/stakes.scss b/apps/block_scout_web/assets/css/stakes.scss index ba0454e036..40ce92b118 100644 --- a/apps/block_scout_web/assets/css/stakes.scss +++ b/apps/block_scout_web/assets/css/stakes.scss @@ -16,6 +16,7 @@ @import "components/stakes/stakes_progress"; @import "components/stakes/modal_stake"; @import "components/stakes/modal_become_candidate"; +@import "components/stakes/modal_claim_reward"; @import "components/stakes/modal_validator_info"; @import "components/stakes/modal_delegators_info"; @import "components/stakes/modal_bottom_disclaimer"; diff --git a/apps/block_scout_web/assets/js/pages/stakes/claim_reward.js b/apps/block_scout_web/assets/js/pages/stakes/claim_reward.js index c676c61616..106ed2b374 100644 --- a/apps/block_scout_web/assets/js/pages/stakes/claim_reward.js +++ b/apps/block_scout_web/assets/js/pages/stakes/claim_reward.js @@ -10,29 +10,30 @@ export function openClaimRewardModal(store) { channel.push('render_claim_reward', { preload: true }).receive('ok', msg => { const $modal = $(msg.html) - const closeButton = $modal.find('.close-modal') - const modalBody = $('.modal-body', $modal) - const waitingMessageContainer = modalBody.find('p') + const $closeButton = $modal.find('.close-modal') + const $modalBody = $('.modal-body', $modal) + const $waitingMessageContainer = $modalBody.find('p') let dotCounter = 0 const dotCounterInterval = setInterval(() => { - let waitingMessage = $.trim(waitingMessageContainer.text()) + let waitingMessage = $.trim($waitingMessageContainer.text()) if (!waitingMessage.endsWith('.')) { waitingMessage = waitingMessage + '.' } waitingMessage = waitingMessage.replace(/\.+$/g, " " + ".".repeat(dotCounter)) - waitingMessageContainer.text(waitingMessage) + $waitingMessageContainer.text(waitingMessage) dotCounter = (dotCounter + 1) % 4 }, 500) - closeButton.hide() + $closeButton.hide() lockModal($modal) channel.on('claim_reward_pools', msg_pools => { channel.off('claim_reward_pools') - closeButton.show() + $closeButton.show() unlockModal($modal) clearInterval(dotCounterInterval) - modalBody.html(msg_pools.html) + $modalBody.html(msg_pools.html) + onPoolsFound($modal, $modalBody) }) $modal.on('shown.bs.modal', () => { channel.push('render_claim_reward', {}).receive('error', (error) => { @@ -40,7 +41,7 @@ export function openClaimRewardModal(store) { }) }) $modal.on('hidden.bs.modal', () => { - $(this).remove() + $modal.remove() }) openModal($modal); @@ -48,3 +49,28 @@ export function openClaimRewardModal(store) { openErrorModal('Claim Reward', error.reason) }) } + +function onPoolsFound($modal, $modalBody) { + const $poolsDropdown = $('[pool-select]', $modalBody) + const $epochChoiceRadio = $('input[name="epoch_choice"]', $modalBody) + const $specifiedEpochsText = $('.specified-epochs', $modalBody) + + $poolsDropdown.on('change', () => { + const data = $('option:selected', this).data() + const $poolInfo = $('.selected-pool-info', $modalBody) + + $poolsDropdown.blur() + $('textarea', $poolInfo).val(data.epochs ? data.epochs : '') + $('#token-reward-sum', $poolInfo).html(data.tokenRewardSum ? data.tokenRewardSum : '0') + $('#native-reward-sum', $poolInfo).html(data.nativeRewardSum ? data.nativeRewardSum : '0') + $('#tx-gas-limit', $poolInfo).html(data.gasLimit ? '~' + data.gasLimit : '0') + $('#epoch-choice-all', $poolInfo).click() + $specifiedEpochsText.val('') + $poolInfo.removeClass('hidden') + $('.modal-bottom-disclaimer', $modal).removeClass('hidden') + }) + + $epochChoiceRadio.on('change', () => { + $specifiedEpochsText.toggleClass('hidden') + }) +} 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 297040584f..daae4283c9 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 @@ -375,29 +375,61 @@ defmodule BlockScoutWeb.StakesChannel do end end) - pools = if error != nil do - %{} + {error, pools} = if error != nil do + {error, %{}} 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} + pools = + pools_amounts + |> Enum.map(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) + + pools_gas_estimates = Enum.map(pools, fn {_data, pool_staking_address} -> + result = ContractReader.claim_reward_estimate_gas( + staking_contract_address, + [], + pool_staking_address, + staker, + json_rpc_named_arguments + ) + {pool_staking_address, result} end) - |> Map.new(fn {val, key} -> {key, val} end) + + error = Enum.find_value(pools_gas_estimates, fn {_, result} -> + case result do + {:error, reason} -> error_reason_to_string(reason) + _ -> nil + end + end) + + pools = if error == nil do + pools_gas_estimates = Map.new(pools_gas_estimates) + Map.new(pools, fn {data, pool_staking_address} -> + {:ok, estimate} = pools_gas_estimates[pool_staking_address] + data = Map.put(data, :gas_estimate, estimate) + {pool_staking_address, data} + end) + else + %{} + end + + {error, pools} end {error, pools} diff --git a/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_btn_stake.html.eex b/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_btn_stake.html.eex index b02bde6fea..02c9ce292d 100644 --- a/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_btn_stake.html.eex +++ b/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_btn_stake.html.eex @@ -1,5 +1,5 @@