Rename some DB fields

staking
Vadim 5 years ago committed by Victor Baranov
parent 3372226ad2
commit e6dc4e073c
  1. 6
      apps/block_scout_web/lib/block_scout_web/channels/stakes_channel.ex
  2. 4
      apps/block_scout_web/lib/block_scout_web/controllers/stakes_controller.ex
  3. 4
      apps/block_scout_web/lib/block_scout_web/templates/stakes/_rows.html.eex
  4. 10
      apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_modal_delegators_list.html.eex
  5. 2
      apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_modal_move.html.eex
  6. 2
      apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_modal_pool_info.html.eex
  7. 22
      apps/explorer/lib/explorer/chain.ex
  8. 6
      apps/explorer/lib/explorer/chain/import/runner/staking_pools_delegators.ex
  9. 2
      apps/explorer/lib/explorer/chain/staking_pool.ex
  10. 14
      apps/explorer/lib/explorer/chain/staking_pools_delegator.ex
  11. 4
      apps/explorer/lib/explorer/staking/contract_state.ex
  12. 4
      apps/explorer/lib/explorer/staking/stake_snapshotting.ex
  13. 8
      apps/explorer/priv/repo/migrations/20190523112839_create_staking_pools_delegators.exs
  14. 4
      apps/explorer/priv/repo/migrations/20190807111216_remove_duplicate_indexes.exs
  15. 2
      apps/explorer/test/explorer/chain/staking_pools_delegator_test.exs
  16. 4
      apps/explorer/test/support/factory.ex

@ -62,7 +62,7 @@ defmodule BlockScoutWeb.StakesChannel do
pool_staking_address pool_staking_address
|> Chain.staking_pool_delegators() |> Chain.staking_pool_delegators()
|> Enum.sort_by(fn staker -> |> Enum.sort_by(fn staker ->
staker_address = to_string(staker.delegator_address_hash) staker_address = to_string(staker.address_hash)
cond do cond do
staker_address == pool_staking_address -> 0 staker_address == pool_staking_address -> 0
@ -148,7 +148,7 @@ defmodule BlockScoutWeb.StakesChannel do
token = ContractState.get(:token) token = ContractState.get(:token)
min_from_stake = min_from_stake =
if delegator_from.delegator_address_hash == delegator_from.pool_address_hash do if delegator_from.address_hash == delegator_from.staking_address_hash do
ContractState.get(:min_candidate_stake) ContractState.get(:min_candidate_stake)
else else
ContractState.get(:min_delegator_stake) ContractState.get(:min_delegator_stake)
@ -201,7 +201,7 @@ defmodule BlockScoutWeb.StakesChannel do
delegator = Chain.staking_pool_delegator(staking_address, socket.assigns.account) delegator = Chain.staking_pool_delegator(staking_address, socket.assigns.account)
min_stake = min_stake =
if delegator.delegator_address_hash == delegator.pool_address_hash do if delegator.address_hash == delegator.staking_address_hash do
ContractState.get(:min_candidate_stake) ContractState.get(:min_candidate_stake)
else else
ContractState.get(:min_delegator_stake) ContractState.get(:min_delegator_stake)

@ -25,7 +25,7 @@ defmodule BlockScoutWeb.StakesController do
account = account =
if account_address = conn.assigns[:account] do if account_address = conn.assigns[:account] do
account_address account_address
|> Chain.get_total_staked() |> Chain.get_total_staked_and_ordered()
|> Map.merge(%{ |> Map.merge(%{
address: account_address, address: account_address,
balance: Chain.fetch_last_token_balance(account_address, token.contract_address_hash), balance: Chain.fetch_last_token_balance(account_address, token.contract_address_hash),
@ -141,7 +141,7 @@ defmodule BlockScoutWeb.StakesController do
end end
defp stake_allowed?(pool, delegator) do defp stake_allowed?(pool, delegator) do
Decimal.positive?(pool.self_staked_amount) or delegator.delegator_address_hash == pool.staking_address_hash Decimal.positive?(pool.self_staked_amount) or delegator.address_hash == pool.staking_address_hash
end end
defp move_allowed?(nil), do: false defp move_allowed?(nil), do: false

@ -36,7 +36,7 @@
<span class="stakes-td-banned-info"> <span class="stakes-td-banned-info">
<%= gettext("Banned until block #%{banned_until} (%{estimated_unban_day})", banned_until: @pool.banned_until, estimated_unban_day: estimated_unban_day(@pool.banned_until, @average_block_time)) %> <%= gettext("Banned until block #%{banned_until} (%{estimated_unban_day})", banned_until: @pool.banned_until, estimated_unban_day: estimated_unban_day(@pool.banned_until, @average_block_time)) %>
<%= if @delegator && <%= if @delegator &&
@delegator.delegator_address_hash != @pool.staking_address_hash && @delegator.address_hash != @pool.staking_address_hash &&
@pool.are_delegators_banned && @pool.are_delegators_banned &&
@pool.banned_until != @pool.banned_delegators_until do %> @pool.banned_until != @pool.banned_delegators_until do %>
<%= raw(".<br />") %> <%= raw(".<br />") %>
@ -47,7 +47,7 @@
<%= if !@pool.is_banned || <%= if !@pool.is_banned ||
(!@pool.are_delegators_banned && (!@pool.are_delegators_banned &&
@delegator && @delegator &&
@delegator.delegator_address_hash != @pool.staking_address_hash @delegator.address_hash != @pool.staking_address_hash
) do %> ) do %>
<div class="stakes-controls"> <div class="stakes-controls">
<%= if @buttons.move do %> <%= if @buttons.move do %>

@ -63,13 +63,13 @@
<span class="stakes-address"> <span class="stakes-address">
<%= <%=
link( link(
BlockScoutWeb.AddressView.trimmed_hash(staker.delegator_address_hash), BlockScoutWeb.AddressView.trimmed_hash(staker.address_hash),
to: address_path(@conn, :show, staker.delegator_address_hash), to: address_path(@conn, :show, staker.address_hash),
target: "_blank" target: "_blank"
) )
%> %>
</span> </span>
<%= if staker.delegator_address_hash == @pool.staking_address_hash do %> <%= if staker.address_hash == @pool.staking_address_hash do %>
<%= <%=
render( render(
BlockScoutWeb.CommonComponentsView, BlockScoutWeb.CommonComponentsView,
@ -78,7 +78,7 @@
) )
%> %>
<% end %> <% end %>
<%= if to_string(staker.delegator_address_hash) == @account do %> <%= if to_string(staker.address_hash) == @account do %>
<div <div
class="me-tooltip" class="me-tooltip"
data-boundary="window" data-boundary="window"
@ -110,7 +110,7 @@
<div class="col-3 stakes-td stakes-cell"> <div class="col-3 stakes-td stakes-cell">
<% <%
reward = reward =
if staker.delegator_address_hash == @pool.staking_address_hash do if staker.address_hash == @pool.staking_address_hash do
%{ %{
reward_ratio: @pool.validator_reward_ratio, reward_ratio: @pool.validator_reward_ratio,
snapshotted_reward_ratio: @pool.snapshotted_validator_reward_ratio snapshotted_reward_ratio: @pool.snapshotted_validator_reward_ratio

@ -22,7 +22,7 @@
<option disabled <%= unless @pool_to do "selected" end %>><%= gettext("Choose Pool") %></option> <option disabled <%= unless @pool_to do "selected" end %>><%= gettext("Choose Pool") %></option>
<%= for %{pool: pool} <- @pools, <%= for %{pool: pool} <- @pools,
pool.staking_address_hash != @pool_from.staking_address_hash, pool.staking_address_hash != @pool_from.staking_address_hash,
Decimal.positive?(pool.self_staked_amount) or pool.staking_address_hash == @delegator_from.delegator_address_hash do %> Decimal.positive?(pool.self_staked_amount) or pool.staking_address_hash == @delegator_from.address_hash do %>
<option <option
value="<%= pool.staking_address_hash %>" value="<%= pool.staking_address_hash %>"
<%= if @pool_to && pool.staking_address_hash == @pool_to.staking_address_hash do "selected" end %> <%= if @pool_to && pool.staking_address_hash == @pool_to.staking_address_hash do "selected" end %>

@ -15,7 +15,7 @@
<%= gettext("This pool is banned until block #%{banned_until} (%{estimated_unban_day})", banned_until: @validator.banned_until, estimated_unban_day: estimated_unban_day(@validator.banned_until, @average_block_time)) %><br /> <%= gettext("This pool is banned until block #%{banned_until} (%{estimated_unban_day})", banned_until: @validator.banned_until, estimated_unban_day: estimated_unban_day(@validator.banned_until, @average_block_time)) %><br />
<%= gettext("Reason for Ban: %{ban_reason}", ban_reason: String.capitalize(@validator.ban_reason)) %> <%= gettext("Reason for Ban: %{ban_reason}", ban_reason: String.capitalize(@validator.ban_reason)) %>
<%= if @delegator && <%= if @delegator &&
@delegator.delegator_address_hash != @validator.staking_address_hash && @delegator.address_hash != @validator.staking_address_hash &&
@validator.are_delegators_banned && @validator.are_delegators_banned &&
@validator.banned_until != @validator.banned_delegators_until do %> @validator.banned_until != @validator.banned_delegators_until do %>
<br /> <br />

@ -4727,14 +4727,14 @@ defmodule Explorer.Chain do
@spec staking_pools( @spec staking_pools(
filter :: :validator | :active | :inactive, filter :: :validator | :active | :inactive,
paging_options :: PagingOptions.t() | :all, paging_options :: PagingOptions.t() | :all,
delegator_address_hash :: Hash.t() | nil, address_hash :: Hash.t() | nil,
filter_banned :: boolean() | nil, filter_banned :: boolean() | nil,
filter_my :: boolean() | nil filter_my :: boolean() | nil
) :: [map()] ) :: [map()]
def staking_pools( def staking_pools(
filter, filter,
paging_options \\ @default_paging_options, paging_options \\ @default_paging_options,
delegator_address_hash \\ nil, address_hash \\ nil,
filter_banned \\ false, filter_banned \\ false,
filter_my \\ false filter_my \\ false
) do ) do
@ -4745,11 +4745,11 @@ defmodule Explorer.Chain do
|> staking_pools_paging_query(paging_options) |> staking_pools_paging_query(paging_options)
delegator_query = delegator_query =
if delegator_address_hash do if address_hash do
base_query base_query
|> join(:left, [p], pd in StakingPoolsDelegator, |> join(:left, [p], pd in StakingPoolsDelegator,
on: on:
p.staking_address_hash == pd.pool_address_hash and pd.delegator_address_hash == ^delegator_address_hash and p.staking_address_hash == pd.staking_address_hash and pd.address_hash == ^address_hash and
not pd.is_deleted not pd.is_deleted
) )
|> select([p, pd], %{pool: p, delegator: pd}) |> select([p, pd], %{pool: p, delegator: pd})
@ -4766,7 +4766,7 @@ defmodule Explorer.Chain do
end end
filtered_query = filtered_query =
if delegator_address_hash && filter_my do if address_hash && filter_my do
where(banned_query, [..., pd], not is_nil(pd)) where(banned_query, [..., pd], not is_nil(pd))
else else
banned_query banned_query
@ -4824,22 +4824,22 @@ defmodule Explorer.Chain do
def staking_pool_delegators(staking_address_hash) do def staking_pool_delegators(staking_address_hash) do
StakingPoolsDelegator StakingPoolsDelegator
|> where(pool_address_hash: ^staking_address_hash, is_active: true) |> where(staking_address_hash: ^staking_address_hash, is_active: true)
|> order_by(desc: :stake_amount) |> order_by(desc: :stake_amount)
|> Repo.all() |> Repo.all()
end end
def staking_pool_delegator(pool_address_hash, delegator_address_hash) do def staking_pool_delegator(staking_address_hash, address_hash) do
Repo.get_by(StakingPoolsDelegator, Repo.get_by(StakingPoolsDelegator,
pool_address_hash: pool_address_hash, staking_address_hash: staking_address_hash,
delegator_address_hash: delegator_address_hash, address_hash: address_hash,
is_deleted: false is_deleted: false
) )
end end
def get_total_staked(address_hash) do def get_total_staked_and_ordered(address_hash) do
StakingPoolsDelegator StakingPoolsDelegator
|> where([delegator], delegator.delegator_address_hash == ^address_hash and not delegator.is_deleted) |> where([delegator], delegator.address_hash == ^address_hash and not delegator.is_deleted)
|> select([delegator], %{ |> select([delegator], %{
stake_amount: coalesce(sum(delegator.stake_amount), 0), stake_amount: coalesce(sum(delegator.stake_amount), 0),
ordered_withdraw: coalesce(sum(delegator.ordered_withdraw), 0) ordered_withdraw: coalesce(sum(delegator.ordered_withdraw), 0)

@ -85,16 +85,16 @@ defmodule Explorer.Chain.Import.Runner.StakingPoolsDelegators do
on_conflict = Map.get_lazy(options, :on_conflict, &default_on_conflict/0) on_conflict = Map.get_lazy(options, :on_conflict, &default_on_conflict/0)
# Enforce StackingPoolDelegator ShareLocks order (see docs: sharelocks.md) # Enforce StackingPoolDelegator ShareLocks order (see docs: sharelocks.md)
ordered_changes_list = Enum.sort_by(changes_list, &{&1.delegator_address_hash, &1.pool_address_hash}) ordered_changes_list = Enum.sort_by(changes_list, &{&1.address_hash, &1.staking_address_hash})
{:ok, _} = {:ok, _} =
Import.insert_changes_list( Import.insert_changes_list(
repo, repo,
ordered_changes_list, ordered_changes_list,
conflict_target: [:pool_address_hash, :delegator_address_hash], conflict_target: [:staking_address_hash, :address_hash],
on_conflict: on_conflict, on_conflict: on_conflict,
for: StakingPoolsDelegator, for: StakingPoolsDelegator,
returning: [:pool_address_hash, :delegator_address_hash], returning: [:staking_address_hash, :address_hash],
timeout: timeout, timeout: timeout,
timestamps: timestamps timestamps: timestamps
) )

@ -72,7 +72,7 @@ defmodule Explorer.Chain.StakingPool do
field(:was_banned_count, :integer) field(:was_banned_count, :integer)
field(:was_validator_count, :integer) field(:was_validator_count, :integer)
field(:is_deleted, :boolean, default: false) field(:is_deleted, :boolean, default: false)
has_many(:delegators, StakingPoolsDelegator, foreign_key: :pool_address_hash) has_many(:delegators, StakingPoolsDelegator, foreign_key: :staking_address_hash)
belongs_to( belongs_to(
:staking_address, :staking_address,

@ -13,8 +13,8 @@ defmodule Explorer.Chain.StakingPoolsDelegator do
} }
@type t :: %__MODULE__{ @type t :: %__MODULE__{
pool_address_hash: Hash.Address.t(), staking_address_hash: Hash.Address.t(),
delegator_address_hash: Hash.Address.t(), address_hash: Hash.Address.t(),
max_ordered_withdraw_allowed: Decimal.t(), max_ordered_withdraw_allowed: Decimal.t(),
max_withdraw_allowed: Decimal.t(), max_withdraw_allowed: Decimal.t(),
ordered_withdraw: Decimal.t(), ordered_withdraw: Decimal.t(),
@ -28,13 +28,13 @@ defmodule Explorer.Chain.StakingPoolsDelegator do
} }
@attrs ~w( @attrs ~w(
pool_address_hash delegator_address_hash max_ordered_withdraw_allowed staking_address_hash address_hash max_ordered_withdraw_allowed
max_withdraw_allowed ordered_withdraw stake_amount snapshotted_stake_amount ordered_withdraw_epoch max_withdraw_allowed ordered_withdraw stake_amount snapshotted_stake_amount ordered_withdraw_epoch
reward_ratio snapshotted_reward_ratio is_active is_deleted reward_ratio snapshotted_reward_ratio is_active is_deleted
)a )a
@req_attrs ~w( @req_attrs ~w(
pool_address_hash delegator_address_hash max_ordered_withdraw_allowed staking_address_hash address_hash max_ordered_withdraw_allowed
max_withdraw_allowed ordered_withdraw stake_amount ordered_withdraw_epoch max_withdraw_allowed ordered_withdraw stake_amount ordered_withdraw_epoch
)a )a
@ -53,7 +53,7 @@ defmodule Explorer.Chain.StakingPoolsDelegator do
belongs_to( belongs_to(
:staking_pool, :staking_pool,
StakingPool, StakingPool,
foreign_key: :pool_address_hash, foreign_key: :staking_address_hash,
references: :staking_address_hash, references: :staking_address_hash,
type: Hash.Address type: Hash.Address
) )
@ -61,7 +61,7 @@ defmodule Explorer.Chain.StakingPoolsDelegator do
belongs_to( belongs_to(
:delegator_address, :delegator_address,
Address, Address,
foreign_key: :delegator_address_hash, foreign_key: :address_hash,
references: :hash, references: :hash,
type: Hash.Address type: Hash.Address
) )
@ -74,6 +74,6 @@ defmodule Explorer.Chain.StakingPoolsDelegator do
staking_pools_delegator staking_pools_delegator
|> cast(attrs, @attrs) |> cast(attrs, @attrs)
|> validate_required(@req_attrs) |> validate_required(@req_attrs)
|> unique_constraint(:pool_address_hash, name: :pools_delegator_index) |> unique_constraint(:staking_address_hash, name: :pools_delegator_index)
end end
end end

@ -254,8 +254,8 @@ defmodule Explorer.Staking.ContractState do
delegator_reward_response = delegator_reward_responses[{pool_address, delegator_address, is_active}] delegator_reward_response = delegator_reward_responses[{pool_address, delegator_address, is_active}]
Map.merge(response, %{ Map.merge(response, %{
delegator_address_hash: delegator_address, address_hash: delegator_address,
pool_address_hash: pool_address, staking_address_hash: pool_address,
is_active: is_active, is_active: is_active,
reward_ratio: Float.floor(delegator_reward_response.delegator_share / 10_000, 2) reward_ratio: Float.floor(delegator_reward_response.delegator_share / 10_000, 2)
}) })

@ -122,8 +122,8 @@ defmodule Explorer.Staking.StakeSnapshotting do
delegator_reward_response = delegator_reward_responses[{pool_address, delegator_address, is_active}] delegator_reward_response = delegator_reward_responses[{pool_address, delegator_address, is_active}]
Map.merge(response, %{ Map.merge(response, %{
delegator_address_hash: delegator_address, address_hash: delegator_address,
pool_address_hash: pool_address, staking_address_hash: pool_address,
is_active: is_active, is_active: is_active,
snapshotted_reward_ratio: Float.floor(delegator_reward_response.delegator_share / 10_000, 2) snapshotted_reward_ratio: Float.floor(delegator_reward_response.delegator_share / 10_000, 2)
}) })

@ -3,8 +3,8 @@ defmodule Explorer.Repo.Migrations.CreateStakingPoolsDelegator do
def change do def change do
create table(:staking_pools_delegators) do create table(:staking_pools_delegators) do
add(:delegator_address_hash, :bytea) add(:address_hash, :bytea)
add(:pool_address_hash, :bytea) add(:staking_address_hash, :bytea)
add(:stake_amount, :numeric, precision: 100) add(:stake_amount, :numeric, precision: 100)
add(:ordered_withdraw, :numeric, precision: 100) add(:ordered_withdraw, :numeric, precision: 100)
add(:max_withdraw_allowed, :numeric, precision: 100) add(:max_withdraw_allowed, :numeric, precision: 100)
@ -14,10 +14,10 @@ defmodule Explorer.Repo.Migrations.CreateStakingPoolsDelegator do
timestamps(null: false, type: :utc_datetime_usec) timestamps(null: false, type: :utc_datetime_usec)
end end
create(index(:staking_pools_delegators, [:delegator_address_hash])) create(index(:staking_pools_delegators, [:address_hash]))
create( create(
index(:staking_pools_delegators, [:delegator_address_hash, :pool_address_hash], index(:staking_pools_delegators, [:address_hash, :staking_address_hash],
unique: true, unique: true,
name: :pools_delegator_index name: :pools_delegator_index
) )

@ -7,8 +7,8 @@ defmodule Explorer.Repo.Migrations.RemoveDuplicateIndexes do
) )
drop_if_exists( drop_if_exists(
index(:staking_pools_delegators, [:delegator_address_hash], index(:staking_pools_delegators, [:address_hash],
name: "staking_pools_delegators_delegator_address_hash_index" name: "staking_pools_delegators_address_hash_index"
) )
) )

@ -11,7 +11,7 @@ defmodule Explorer.Chain.StakingPoolsDelegatorTest do
end end
test "with invalid attributes" do test "with invalid attributes" do
changeset = StakingPoolsDelegator.changeset(%StakingPoolsDelegator{}, %{pool_address_hash: 0}) changeset = StakingPoolsDelegator.changeset(%StakingPoolsDelegator{}, %{staking_address_hash: 0})
refute changeset.valid? refute changeset.valid?
end end
end end

@ -674,8 +674,8 @@ defmodule Explorer.Factory do
wei_per_ether = 1_000_000_000_000_000_000 wei_per_ether = 1_000_000_000_000_000_000
%StakingPoolsDelegator{ %StakingPoolsDelegator{
pool_address_hash: address_hash(), staking_address_hash: address_hash(),
delegator_address_hash: address_hash(), address_hash: address_hash(),
max_ordered_withdraw_allowed: wei_per_ether * 100, max_ordered_withdraw_allowed: wei_per_ether * 100,
max_withdraw_allowed: wei_per_ether * 50, max_withdraw_allowed: wei_per_ether * 50,
ordered_withdraw: wei_per_ether * 600, ordered_withdraw: wei_per_ether * 600,

Loading…
Cancel
Save