add delegators table and schema

pull/2036/head
saneery 6 years ago
parent 8d2ecb9f61
commit 4728d4696a
  1. 64
      apps/explorer/lib/explorer/chain/staking_pools_delegators.ex
  2. 26
      apps/explorer/priv/repo/migrations/20190523112839_create_staking_pools_delegators.exs
  3. 18
      apps/explorer/test/explorer/chain/staking_pools_delegators_test.exs
  4. 17
      apps/explorer/test/support/factory.ex

@ -0,0 +1,64 @@
defmodule Explorer.Chain.StakingPoolsDelegators do
@moduledoc """
The representation of delegators from POSDAO network.
Delegators make stakes on staking pools and withdraw from them.
"""
use Ecto.Schema
import Ecto.Changeset
alias Explorer.Chain.{
Address,
Hash,
StakingPool,
Wei
}
@type t :: %__MODULE__{
pool_address_hash: Hash.Address.t(),
delegator_address_hash: Hash.Address.t(),
max_ordered_withdraw_allowed: Wei.t(),
max_withdraw_allowed: Wei.t(),
ordered_withdraw: Wei.t(),
stake_amount: Wei.t(),
ordered_withdraw_epoch: integer()
}
@attrs ~w(
pool_address_hash delegator_address_hash max_ordered_withdraw_allowed
max_withdraw_allowed ordered_withdraw stake_amount ordered_withdraw_epoch
)a
schema "staking_pools_delegators" do
field(:max_ordered_withdraw_allowed, Wei)
field(:max_withdraw_allowed, Wei)
field(:ordered_withdraw, Wei)
field(:ordered_withdraw_epoch, :integer)
field(:stake_amount, Wei)
belongs_to(
:staking_pool,
StakingPool,
foreign_key: :pool_address_hash,
references: :staking_address_hash,
type: Hash.Address
)
belongs_to(
:delegator_address,
Address,
foreign_key: :delegator_address_hash,
references: :hash,
type: Hash.Address
)
timestamps(null: false, type: :utc_datetime_usec)
end
@doc false
def changeset(staking_pools_delegators, attrs) do
staking_pools_delegators
|> cast(attrs, @attrs)
|> validate_required(@attrs)
|> unique_constraint(:pool_address_hash, name: :pools_delegator_index)
end
end

@ -0,0 +1,26 @@
defmodule Explorer.Repo.Migrations.CreateStakingPoolsDelegators do
use Ecto.Migration
def change do
create table(:staking_pools_delegators) do
add(:delegator_address_hash, :bytea)
add(:pool_address_hash, :bytea)
add(:stake_amount, :numeric, precision: 100)
add(:ordered_withdraw, :numeric, precision: 100)
add(:max_withdraw_allowed, :numeric, precision: 100)
add(:max_ordered_withdraw_allowed, :numeric, precision: 100)
add(:ordered_withdraw_epoch, :integer)
timestamps(null: false, type: :utc_datetime_usec)
end
create(index(:staking_pools_delegators, [:delegator_address_hash]))
create(
index(:staking_pools_delegators, [:delegator_address_hash, :pool_address_hash],
unique: true,
name: :pools_delegator_index
)
)
end
end

@ -0,0 +1,18 @@
defmodule Explorer.Chain.StakingPoolsDelegatorsTest do
use Explorer.DataCase
alias Explorer.Chain.StakingPoolsDelegators
describe "changeset/2" do
test "with valid attributes" do
params = params_for(:staking_pools_delegators)
changeset = StakingPoolsDelegators.changeset(%StakingPoolsDelegators{}, params)
assert changeset.valid?
end
test "with invalid attributes" do
changeset = StakingPoolsDelegators.changeset(%StakingPoolsDelegators{}, %{pool_address_hash: 0})
refute changeset.valid?
end
end
end

@ -27,7 +27,8 @@ defmodule Explorer.Factory do
Token, Token,
TokenTransfer, TokenTransfer,
Transaction, Transaction,
StakingPool StakingPool,
StakingPoolsDelegators
} }
alias Explorer.Market.MarketHistory alias Explorer.Market.MarketHistory
@ -628,4 +629,18 @@ defmodule Explorer.Factory do
was_validator_count: 1 was_validator_count: 1
} }
end end
def staking_pools_delegators_factory do
wei_per_ether = 1_000_000_000_000_000_000
%StakingPoolsDelegators{
pool_address_hash: address_hash(),
delegator_address_hash: address_hash(),
max_ordered_withdraw_allowed: wei_per_ether * 100,
max_withdraw_allowed: wei_per_ether * 50,
ordered_withdraw: wei_per_ether * 600,
stake_amount: wei_per_ether * 200,
ordered_withdraw_epoch: 2
}
end
end end

Loading…
Cancel
Save