parent
8d2ecb9f61
commit
4728d4696a
@ -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 |
Loading…
Reference in new issue