From 227d3857fd61fc34d9a373fbf21ff51817c9d7d8 Mon Sep 17 00:00:00 2001 From: Eduard Sachava Date: Wed, 13 Nov 2019 15:35:56 +0200 Subject: [PATCH] Add validator_reward_ratio field and fix staked_ratio value --- .../_stakes_modal_delegators_list.html.eex | 22 +++++++++++++++---- .../chain/import/runner/staking_pools.ex | 3 ++- .../lib/explorer/chain/staking_pool.ex | 8 ++++--- .../lib/explorer/staking/contract_reader.ex | 2 +- .../lib/explorer/staking/contract_state.ex | 10 +++++++-- .../explorer/staking/stake_snapshotting.ex | 21 ++++-------------- ...d_snapshotting_fields_to_staking_pools.exs | 3 ++- 7 files changed, 40 insertions(+), 29 deletions(-) diff --git a/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_modal_delegators_list.html.eex b/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_modal_delegators_list.html.eex index 44c8129aed..6572a842c3 100644 --- a/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_modal_delegators_list.html.eex +++ b/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_modal_delegators_list.html.eex @@ -82,13 +82,27 @@ <% end %>
- <%= if delegator.reward_ratio do %> - <%= delegator.reward_ratio %>% + <% + reward = + if delegator.delegator_address_hash == @pool.staking_address_hash do + %{ + reward_ratio: @pool.validator_reward_ratio, + snapshotted_reward_ratio: @pool.snapshotted_validator_reward_ratio + } + else + %{ + reward_ratio: delegator.reward_ratio, + snapshotted_reward_ratio: delegator.snapshotted_reward_ratio + } + end + %> + <%= if reward.reward_ratio do %> + <%= reward.reward_ratio %>% <%= if @is_show_snapshotted_data do %> ( <%= - if delegator.snapshotted_reward_ratio do - "#{delegator.snapshotted_reward_ratio}%" + if reward.snapshotted_reward_ratio do + "#{reward.snapshotted_reward_ratio}%" else "0%" end diff --git a/apps/explorer/lib/explorer/chain/import/runner/staking_pools.ex b/apps/explorer/lib/explorer/chain/import/runner/staking_pools.ex index 67a9799e37..d7a3b7c46b 100644 --- a/apps/explorer/lib/explorer/chain/import/runner/staking_pools.ex +++ b/apps/explorer/lib/explorer/chain/import/runner/staking_pools.ex @@ -132,7 +132,8 @@ defmodule Explorer.Chain.Import.Runner.StakingPools do likelihood: fragment("EXCLUDED.likelihood"), block_reward_ratio: fragment("EXCLUDED.block_reward_ratio"), staked_ratio: fragment("EXCLUDED.staked_ratio"), - snapshotted_staked_ratio: pool.snapshotted_staked_ratio, + validator_reward_ratio: fragment("EXCLUDED.validator_reward_ratio"), + snapshotted_validator_reward_ratio: pool.snapshotted_validator_reward_ratio, self_staked_amount: fragment("EXCLUDED.self_staked_amount"), total_staked_amount: fragment("EXCLUDED.total_staked_amount"), snapshotted_self_staked_amount: pool.snapshotted_self_staked_amount, diff --git a/apps/explorer/lib/explorer/chain/staking_pool.ex b/apps/explorer/lib/explorer/chain/staking_pool.ex index 5dd0f6e9b3..7b51c07bce 100644 --- a/apps/explorer/lib/explorer/chain/staking_pool.ex +++ b/apps/explorer/lib/explorer/chain/staking_pool.ex @@ -26,7 +26,8 @@ defmodule Explorer.Chain.StakingPool do likelihood: Decimal.t(), block_reward_ratio: Decimal.t(), staked_ratio: Decimal.t(), - snapshotted_staked_ratio: Decimal.t(), + validator_reward_ratio: Decimal.t(), + snapshotted_validator_reward_ratio: Decimal.t(), self_staked_amount: Decimal.t(), total_staked_amount: Decimal.t(), snapshotted_self_staked_amount: Decimal.t(), @@ -40,7 +41,7 @@ defmodule Explorer.Chain.StakingPool do @attrs ~w( is_active delegators_count total_staked_amount self_staked_amount snapshotted_total_staked_amount snapshotted_self_staked_amount is_validator was_validator_count is_banned are_delegators_banned ban_reason was_banned_count banned_until banned_delegators_until likelihood - staked_ratio snapshotted_staked_ratio staking_address_hash mining_address_hash block_reward_ratio + staked_ratio validator_reward_ratio snapshotted_validator_reward_ratio staking_address_hash mining_address_hash block_reward_ratio is_unremovable )a @req_attrs ~w( @@ -61,7 +62,8 @@ defmodule Explorer.Chain.StakingPool do field(:likelihood, :decimal) field(:block_reward_ratio, :decimal) field(:staked_ratio, :decimal) - field(:snapshotted_staked_ratio, :decimal) + field(:validator_reward_ratio, :decimal) + field(:snapshotted_validator_reward_ratio, :decimal) field(:self_staked_amount, :decimal) field(:total_staked_amount, :decimal) field(:snapshotted_self_staked_amount, :decimal) diff --git a/apps/explorer/lib/explorer/staking/contract_reader.ex b/apps/explorer/lib/explorer/staking/contract_reader.ex index cc319fe747..9b62f394d4 100644 --- a/apps/explorer/lib/explorer/staking/contract_reader.ex +++ b/apps/explorer/lib/explorer/staking/contract_reader.ex @@ -61,7 +61,7 @@ defmodule Explorer.Staking.ContractReader do end # args = [staking_epoch, validator_staked, total_staked, pool_reward \\ 10_00000] - def pool_reward_requests(args) do + def validator_reward_requests(args) do [ validator_share: {:block_reward, "validatorShare", args} ] diff --git a/apps/explorer/lib/explorer/staking/contract_state.ex b/apps/explorer/lib/explorer/staking/contract_state.ex index b3aa3864c3..16e47f3de4 100644 --- a/apps/explorer/lib/explorer/staking/contract_state.ex +++ b/apps/explorer/lib/explorer/staking/contract_state.ex @@ -168,6 +168,8 @@ defmodule Explorer.Staking.ContractState do end) |> ContractReader.perform_grouped_requests(delegators, contracts, abi) + staked_total = Enum.sum(for {_, pool} <- pool_staking_responses, pool.is_active, do: pool.total_staked_amount) + [likelihood_values, total_likelihood] = global_responses.pools_likelihood likelihood = @@ -180,7 +182,7 @@ defmodule Explorer.Staking.ContractState do pool_reward_responses = pool_staking_responses |> Enum.map(fn {_address, response} -> - ContractReader.pool_reward_requests([ + ContractReader.validator_reward_requests([ global_responses.epoch_number, response.self_staked_amount, response.total_staked_amount, @@ -215,7 +217,11 @@ defmodule Explorer.Staking.ContractState do %{ staking_address_hash: staking_address, delegators_count: length(staking_response.active_delegators), - staked_ratio: pool_reward_response.validator_share / 10_000, + staked_ratio: + if staking_response.is_active do + ratio(staking_response.total_staked_amount, staked_total) + end, + validator_reward_ratio: pool_reward_response.validator_share / 10_000, likelihood: ratio(likelihood[staking_address] || 0, total_likelihood), block_reward_ratio: staking_response.block_reward / 10_000, is_deleted: false, diff --git a/apps/explorer/lib/explorer/staking/stake_snapshotting.ex b/apps/explorer/lib/explorer/staking/stake_snapshotting.ex index 872d638760..ca479a9b2e 100644 --- a/apps/explorer/lib/explorer/staking/stake_snapshotting.ex +++ b/apps/explorer/lib/explorer/staking/stake_snapshotting.ex @@ -60,7 +60,7 @@ defmodule Explorer.Staking.StakeSnapshotting do pool_reward_responses = pool_staking_responses |> Enum.map(fn {_address, response} -> - ContractReader.pool_reward_requests([ + ContractReader.validator_reward_requests([ global_responses.epoch_number, response.snapshotted_self_staked_amount, response.snapshotted_total_staked_amount, @@ -95,7 +95,7 @@ defmodule Explorer.Staking.StakeSnapshotting do %{ staking_address_hash: staking_address, delegators_count: length(staking_response.active_delegators), - snapshotted_staked_ratio: pool_reward_response.validator_share / 10_000 + snapshotted_validator_reward_ratio: pool_reward_response.validator_share / 10_000 } |> Map.merge( Map.take(staking_response, [ @@ -163,20 +163,6 @@ defmodule Explorer.Staking.StakeSnapshotting do ] end - # args = [staking_epoch, validator_staked, total_staked, pool_reward \\ 10_00000] - def pool_reward_requests(args, block_number) do - [ - validator_share: {:block_reward, "validatorShare", args, block_number - 1} - ] - end - - # args = [staking_epoch, delegator_staked, validator_staked, total_staked, pool_reward \\ 10_00000] - def delegator_reward_requests(args, block_number) do - [ - delegator_share: {:block_reward, "delegatorShare", args, block_number - 1} - ] - end - defp staking_pool_on_conflict do from( pool in StakingPool, @@ -184,7 +170,7 @@ defmodule Explorer.Staking.StakeSnapshotting do set: [ mining_address_hash: fragment("EXCLUDED.mining_address_hash"), delegators_count: fragment("EXCLUDED.delegators_count"), - snapshotted_staked_ratio: fragment("EXCLUDED.snapshotted_staked_ratio"), + snapshotted_validator_reward_ratio: fragment("EXCLUDED.snapshotted_validator_reward_ratio"), self_staked_amount: fragment("EXCLUDED.self_staked_amount"), total_staked_amount: fragment("EXCLUDED.total_staked_amount"), snapshotted_self_staked_amount: fragment("EXCLUDED.snapshotted_self_staked_amount"), @@ -197,6 +183,7 @@ defmodule Explorer.Staking.StakeSnapshotting do likelihood: pool.likelihood, block_reward_ratio: pool.block_reward_ratio, staked_ratio: pool.staked_ratio, + validator_reward_ratio: pool.validator_reward_ratio, ban_reason: pool.ban_reason, was_banned_count: pool.was_banned_count, was_validator_count: pool.was_validator_count, diff --git a/apps/explorer/priv/repo/migrations/20191024135401_add_snapshotting_fields_to_staking_pools.exs b/apps/explorer/priv/repo/migrations/20191024135401_add_snapshotting_fields_to_staking_pools.exs index 9d0f923fd9..ecc44951f5 100644 --- a/apps/explorer/priv/repo/migrations/20191024135401_add_snapshotting_fields_to_staking_pools.exs +++ b/apps/explorer/priv/repo/migrations/20191024135401_add_snapshotting_fields_to_staking_pools.exs @@ -5,7 +5,8 @@ defmodule Explorer.Repo.Migrations.AddSnapshottingFieldsToStakingPools do alter table(:staking_pools) do add(:snapshotted_total_staked_amount, :numeric, precision: 100) add(:snapshotted_self_staked_amount, :numeric, precision: 100) - add(:snapshotted_staked_ratio, :decimal, precision: 5, scale: 2) + add(:validator_reward_ratio, :decimal, precision: 5, scale: 2) + add(:snapshotted_validator_reward_ratio, :decimal, precision: 5, scale: 2) end end end