create staking_pool schema

pull/2036/head
saneery 6 years ago
parent 60ceb4bb84
commit 03fd6f9540
  1. 73
      apps/explorer/lib/explorer/chain/staking_pool.ex
  2. 28
      apps/explorer/priv/repo/migrations/20190521104412_create_staking_pools.exs
  3. 18
      apps/explorer/test/explorer/chain/staking_pool_test.exs
  4. 27
      apps/explorer/test/support/factory.ex

@ -0,0 +1,73 @@
defmodule Explorer.Chain.StakingPool do
@moduledoc """
The representation of staking pool from POSDAO network.
Staking pools might be candidate or validator.
"""
use Ecto.Schema
import Ecto.Changeset
alias Explorer.Chain.{
Wei,
Address,
Hash
}
@attrs ~w(
is_active delegators_count staked_amount self_staked_amount is_validator
was_validator_count is_banned was_banned_count banned_until likelihood
staked_ratio min_delegators_stake min_candidate_stake
staking_address_hash mining_address_hash
)a
schema "staking_pools" do
field(:banned_until, :integer)
field(:delegators_count, :integer)
field(:is_active, :boolean, default: false)
field(:is_banned, :boolean, default: false)
field(:is_validator, :boolean, default: false)
field(:likelihood, :decimal)
field(:staked_ratio, :decimal)
field(:min_candidate_stake, Wei)
field(:min_delegators_stake, Wei)
field(:self_staked_amount, Wei)
field(:staked_amount, Wei)
field(:was_banned_count, :integer)
field(:was_validator_count, :integer)
belongs_to(
:staking_address,
Address,
foreign_key: :staking_address_hash,
references: :hash,
type: Hash.Address
)
belongs_to(
:mining_address,
Address,
foreign_key: :mining_address_hash,
references: :hash,
type: Hash.Address
)
timestamps()
end
@doc false
def changeset(staking_pool, attrs) do
staking_pool
|> cast(attrs, @attrs)
|> validate_required(@attrs)
|> validate_staked_amount()
end
defp validate_staked_amount(%{valid?: false} = c), do: c
defp validate_staked_amount(changeset) do
if get_field(changeset, :staked_amount) < get_field(changeset, :self_staked_amount) do
add_error(changeset, :staked_amount, "must be greater than self_staked_amount")
else
changeset
end
end
end

@ -0,0 +1,28 @@
defmodule Explorer.Repo.Migrations.CreateStakingPools do
use Ecto.Migration
def change do
create table(:staking_pools) do
add(:is_active, :boolean, default: false, null: false)
add(:delegators_count, :integer)
add(:staked_amount, :numeric, precision: 100)
add(:self_staked_amount, :numeric, precision: 100)
add(:is_validator, :boolean, default: false, null: false)
add(:was_validator_count, :integer)
add(:is_banned, :boolean, default: false, null: false)
add(:was_banned_count, :integer)
add(:banned_until, :bigint)
add(:likelihood, :decimal)
add(:staked_ratio, :decimal)
add(:min_delegators_stake, :numeric, precision: 100)
add(:min_candidate_stake, :numeric, precision: 100)
add(:staking_address_hash, references(:addresses, on_delete: :nothing, column: :hash, type: :bytea))
add(:mining_address_hash, references(:addresses, on_delete: :nothing, column: :hash, type: :bytea))
timestamps()
end
create(index(:staking_pools, [:staking_address_hash]))
create(index(:staking_pools, [:mining_address_hash]))
end
end

@ -0,0 +1,18 @@
defmodule Explorer.Chain.StakingPoolTest do
use Explorer.DataCase
alias Explorer.Chain.StakingPool
describe "changeset/2" do
test "with valid attributes" do
params = params_for(:staking_pool)
changeset = StakingPool.changeset(%StakingPool{}, params) |> IO.inspect()
assert changeset.valid?
end
test "with invalid attributes" do
changeset = StakingPool.changeset(%StakingPool{}, %{staking_address_hash: 0})
refute changeset.valid?
end
end
end

@ -26,7 +26,8 @@ defmodule Explorer.Factory do
SmartContract, SmartContract,
Token, Token,
TokenTransfer, TokenTransfer,
Transaction Transaction,
StakingPool
} }
alias Explorer.Market.MarketHistory alias Explorer.Market.MarketHistory
@ -611,22 +612,24 @@ defmodule Explorer.Factory do
end end
def staking_pool_factory do def staking_pool_factory do
%{ wei_per_ether = 1_000_000_000_000_000_000
address_hash: address_hash(),
metadata: %{ %StakingPool{
banned_unitil: 0, staking_address_hash: address_hash(),
mining_address_hash: address_hash(),
banned_until: 0,
delegators_count: 0, delegators_count: 0,
is_active: true, is_active: true,
is_banned: false, is_banned: false,
is_validator: true, is_validator: true,
mining_address: address_hash(), staked_amount: wei_per_ether * 500,
retries_count: 1, self_staked_amount: wei_per_ether * 300,
staked_amount: 25,
was_banned_count: 0, was_banned_count: 0,
was_validator_count: 1 was_validator_count: 1,
}, min_delegators_stake: wei_per_ether * 100,
name: "anonymous", min_candidate_stake: wei_per_ether * 200,
primary: true staked_ratio: 0,
likelihood: 0,
} }
end end
end end

Loading…
Cancel
Save