From 234422293089f42355504e7d3f58776212a4b27b Mon Sep 17 00:00:00 2001 From: Paul Tsupikoff Date: Thu, 22 Aug 2019 17:34:50 +0200 Subject: [PATCH] Fetch unremovable staking and disallow staking to empty pool (#2608) is_unremovable field is added to staking pools and filled using contract getter unremovableValidator. If such validator hasn't staked to itself yet, Stake button is disabled. Also, Remove My Pool button is disabled for such validator. Also, fetching is_validator field is optimized, and is now performed using one getValidators call instead of one isValidator call per pool. --- .../block_scout_web/controllers/stakes_controller.ex | 10 +++++++++- .../templates/stakes/_stakes_modal_move.html.eex | 4 +++- .../templates/stakes/_stakes_top.html.eex | 4 +++- .../explorer/chain/import/runner/staking_pools.ex | 1 + apps/explorer/lib/explorer/chain/staking_pool.ex | 5 ++++- .../explorer/lib/explorer/staking/contract_reader.ex | 4 ++-- apps/explorer/lib/explorer/staking/contract_state.ex | 12 ++++++++---- ...190820212841_add_unremovable_to_staking_pools.exs | 9 +++++++++ .../test/explorer/staking/contract_state_test.exs | 11 ++++------- 9 files changed, 43 insertions(+), 17 deletions(-) create mode 100644 apps/explorer/priv/repo/migrations/20190820212841_add_unremovable_to_staking_pools.exs diff --git a/apps/block_scout_web/lib/block_scout_web/controllers/stakes_controller.ex b/apps/block_scout_web/lib/block_scout_web/controllers/stakes_controller.ex index 4371f19a2f..0a3a51c282 100644 --- a/apps/block_scout_web/lib/block_scout_web/controllers/stakes_controller.ex +++ b/apps/block_scout_web/lib/block_scout_web/controllers/stakes_controller.ex @@ -92,7 +92,7 @@ defmodule BlockScoutWeb.StakesController do average_block_time: average_block_time, pools_type: filter, buttons: %{ - stake: true, + stake: stake_allowed?(pool, delegator), move: move_allowed?(delegator), withdraw: withdraw_allowed?(delegator, epoch_number) } @@ -130,6 +130,14 @@ defmodule BlockScoutWeb.StakesController do inactive_pools_path(conn, :index, params) end + defp stake_allowed?(pool, nil) do + Decimal.positive?(pool.self_staked_amount) + end + + defp stake_allowed?(pool, delegator) do + Decimal.positive?(pool.self_staked_amount) or delegator.delegator_address_hash == pool.staking_address_hash + end + defp move_allowed?(nil), do: false defp move_allowed?(delegator) do diff --git a/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_modal_move.html.eex b/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_modal_move.html.eex index e140654e95..e2e620a725 100644 --- a/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_modal_move.html.eex +++ b/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_modal_move.html.eex @@ -26,7 +26,9 @@