Add validator_reward_ratio field and fix staked_ratio value

staking
Eduard Sachava 5 years ago committed by Victor Baranov
parent 303ece8180
commit 227d3857fd
  1. 22
      apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_modal_delegators_list.html.eex
  2. 3
      apps/explorer/lib/explorer/chain/import/runner/staking_pools.ex
  3. 8
      apps/explorer/lib/explorer/chain/staking_pool.ex
  4. 2
      apps/explorer/lib/explorer/staking/contract_reader.ex
  5. 10
      apps/explorer/lib/explorer/staking/contract_state.ex
  6. 21
      apps/explorer/lib/explorer/staking/stake_snapshotting.ex
  7. 3
      apps/explorer/priv/repo/migrations/20191024135401_add_snapshotting_fields_to_staking_pools.exs

@ -82,13 +82,27 @@
<% end %>
</div>
<div class="col-3 stakes-td stakes-cell">
<%= 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

@ -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,

@ -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)

@ -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}
]

@ -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,

@ -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,

@ -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

Loading…
Cancel
Save