From 6fff314814c01b5018cb83e238bba5bfaa5ae5a1 Mon Sep 17 00:00:00 2001 From: goodsoft Date: Tue, 30 Jul 2019 22:02:34 +0300 Subject: [PATCH] Show "Withdraw stake" and "Claim withdraw" modal windows (#2465) If there is available ordered withdraw for claiming, we generate both withdraw and claim modal windows, and present user a question modal. Co-Authored-By: Anatoly Nikiforov --- .../block_scout_web/assets/js/pages/stakes.js | 2 + .../assets/js/pages/stakes/withdraw_stake.js | 77 +++++++++++++++++++ .../channels/stakes_channel.ex | 32 ++++++++ .../templates/stakes/_rows.html.eex | 3 + .../stakes/_stakes_btn_withdraw.html.eex | 9 +++ .../stakes/_stakes_control_withdraw.html.eex | 6 ++ .../stakes/_stakes_modal_claim.html.eex | 31 ++++++++ .../stakes/_stakes_modal_withdraw.html.eex | 51 ++++++++++++ 8 files changed, 211 insertions(+) create mode 100644 apps/block_scout_web/assets/js/pages/stakes/withdraw_stake.js create mode 100644 apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_btn_withdraw.html.eex create mode 100644 apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_control_withdraw.html.eex create mode 100644 apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_modal_claim.html.eex create mode 100644 apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_modal_withdraw.html.eex diff --git a/apps/block_scout_web/assets/js/pages/stakes.js b/apps/block_scout_web/assets/js/pages/stakes.js index 4ed457d7c2..5d39bdf79b 100644 --- a/apps/block_scout_web/assets/js/pages/stakes.js +++ b/apps/block_scout_web/assets/js/pages/stakes.js @@ -11,6 +11,7 @@ import { openBecomeCandidateModal } from './stakes/become_candidate' import { openRemovePoolModal } from './stakes/remove_pool' import { openMakeStakeModal } from './stakes/make_stake' import { openMoveStakeModal } from './stakes/move_stake' +import { openWithdrawStakeModal } from './stakes/withdraw_stake' export const initialState = { channel: null, @@ -88,6 +89,7 @@ if ($stakesPage.length) { .on('click', '.js-remove-pool', () => openRemovePoolModal(store)) .on('click', '.js-make-stake', event => openMakeStakeModal(event, store)) .on('click', '.js-move-stake', event => openMoveStakeModal(event, store)) + .on('click', '.js-withdraw-stake', event => openWithdrawStakeModal(event, store)) initializeWeb3(store) } diff --git a/apps/block_scout_web/assets/js/pages/stakes/withdraw_stake.js b/apps/block_scout_web/assets/js/pages/stakes/withdraw_stake.js new file mode 100644 index 0000000000..6497a2fafd --- /dev/null +++ b/apps/block_scout_web/assets/js/pages/stakes/withdraw_stake.js @@ -0,0 +1,77 @@ +import $ from 'jquery' +import { BigNumber } from 'bignumber.js' +import { openModal, openQuestionModal, lockModal } from '../../lib/modals' +import { makeContractCall, setupChart } from './utils' + +export function openWithdrawStakeModal (event, store) { + const address = $(event.target).closest('[data-address]').data('address') + + store.getState().channel + .push('render_withdraw_stake', { address }) + .receive('ok', msg => { + if (msg.claim_html) { + openQuestionModal( + 'Claim or order', 'Do you want withdraw or claim ordered withdraw?', + () => setupClaimWithdrawModal(address, store, msg), + () => setupWithdrawStakeModal(address, store, msg), + 'Claim', 'Withdraw' + ) + } else { + setupWithdrawStakeModal(address, store, msg) + } + }) +} + +function setupClaimWithdrawModal (address, store, msg) { + const $modal = $(msg.claim_html) + setupChart($modal.find('.js-stakes-progress'), msg.self_staked_amount, msg.staked_amount) + $modal.find('form').submit(() => { + claimWithdraw($modal, address, store) + return false + }) + openModal($modal) +} + +function setupWithdrawStakeModal (address, store, msg) { + const $modal = $(msg.html) + setupChart($modal.find('.js-stakes-progress'), msg.self_staked_amount, msg.staked_amount) + $modal.find('.btn-full-primary.withdraw').click(() => { + withdrawStake($modal, address, store) + return false + }) + $modal.find('.btn-full-primary.order-withdraw').click(() => { + orderWithdraw($modal, address, store) + return false + }) + openModal($modal) +} + +function claimWithdraw ($modal, address, store) { + lockModal($modal) + + const stakingContract = store.getState().stakingContract + + makeContractCall(stakingContract.methods.claimOrderedWithdraw(address), store) +} + +function withdrawStake ($modal, address, store) { + lockModal($modal) + + const stakingContract = store.getState().stakingContract + const decimals = store.getState().tokenDecimals + + const amount = new BigNumber($modal.find('[amount]').val().replace(',', '.').trim()).shiftedBy(decimals).integerValue() + + makeContractCall(stakingContract.methods.withdraw(address, amount.toString()), store) +} + +function orderWithdraw ($modal, address, store) { + lockModal($modal) + + const stakingContract = store.getState().stakingContract + const decimals = store.getState().tokenDecimals + + const amount = new BigNumber($modal.find('[amount]').val().replace(',', '.').trim()).shiftedBy(decimals).integerValue() + + makeContractCall(stakingContract.methods.orderWithdraw(address, amount.toString()), store) +} 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 7d3af891ba..0f81e906d5 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 @@ -115,6 +115,38 @@ defmodule BlockScoutWeb.StakesChannel do {:reply, {:ok, result}, socket} end + def handle_in("render_withdraw_stake", %{"address" => staking_address}, socket) do + pool = Chain.staking_pool(staking_address) + token = ContractState.get(:token) + delegator = Chain.staking_pool_delegator(staking_address, socket.assigns.account) + epoch_number = ContractState.get(:epoch_number, 0) + + claim_html = + if Decimal.positive?(delegator.ordered_withdraw) and delegator.ordered_withdraw_epoch < epoch_number do + View.render_to_string(StakesView, "_stakes_modal_claim.html", + token: token, + delegator: delegator, + pool: pool + ) + end + + html = + View.render_to_string(StakesView, "_stakes_modal_withdraw.html", + token: token, + delegator: delegator, + pool: pool + ) + + result = %{ + claim_html: claim_html, + html: html, + self_staked_amount: pool.self_staked_amount, + staked_amount: pool.staked_amount + } + + {:reply, {:ok, result}, socket} + end + def handle_out("staking_update", _data, socket) do push(socket, "staking_update", %{ top_html: StakesController.render_top(socket) diff --git a/apps/block_scout_web/lib/block_scout_web/templates/stakes/_rows.html.eex b/apps/block_scout_web/lib/block_scout_web/templates/stakes/_rows.html.eex index 4b13a442c5..63919b9635 100644 --- a/apps/block_scout_web/lib/block_scout_web/templates/stakes/_rows.html.eex +++ b/apps/block_scout_web/lib/block_scout_web/templates/stakes/_rows.html.eex @@ -37,6 +37,9 @@ <%= if @buttons.move do %> <%= render BlockScoutWeb.StakesView, "_stakes_control_move.html", address: @pool.staking_address_hash %> <% end %> + <%= if @buttons.withdraw do %> + <%= render BlockScoutWeb.StakesView, "_stakes_control_withdraw.html", address: @pool.staking_address_hash %> + <% end %> <%= if @buttons.stake do %> <%= render BlockScoutWeb.StakesView, "_stakes_control_stake.html", address: @pool.staking_address_hash %> <% end %> diff --git a/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_btn_withdraw.html.eex b/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_btn_withdraw.html.eex new file mode 100644 index 0000000000..3be7ca95d3 --- /dev/null +++ b/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_btn_withdraw.html.eex @@ -0,0 +1,9 @@ + \ No newline at end of file diff --git a/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_control_withdraw.html.eex b/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_control_withdraw.html.eex new file mode 100644 index 0000000000..d828b70b32 --- /dev/null +++ b/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_control_withdraw.html.eex @@ -0,0 +1,6 @@ + + + + + Withdraw + \ No newline at end of file diff --git a/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_modal_claim.html.eex b/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_modal_claim.html.eex new file mode 100644 index 0000000000..a5b131b473 --- /dev/null +++ b/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_modal_claim.html.eex @@ -0,0 +1,31 @@ + \ No newline at end of file diff --git a/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_modal_withdraw.html.eex b/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_modal_withdraw.html.eex new file mode 100644 index 0000000000..a6bd2bec9f --- /dev/null +++ b/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_modal_withdraw.html.eex @@ -0,0 +1,51 @@ + \ No newline at end of file