From 4728d4696a302c6d3663c10759b288f926a7fb3e Mon Sep 17 00:00:00 2001 From: saneery Date: Thu, 23 May 2019 15:02:15 +0300 Subject: [PATCH] add delegators table and schema --- .../chain/staking_pools_delegators.ex | 64 +++++++++++++++++++ ...112839_create_staking_pools_delegators.exs | 26 ++++++++ .../chain/staking_pools_delegators_test.exs | 18 ++++++ apps/explorer/test/support/factory.ex | 17 ++++- 4 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 apps/explorer/lib/explorer/chain/staking_pools_delegators.ex create mode 100644 apps/explorer/priv/repo/migrations/20190523112839_create_staking_pools_delegators.exs create mode 100644 apps/explorer/test/explorer/chain/staking_pools_delegators_test.exs diff --git a/apps/explorer/lib/explorer/chain/staking_pools_delegators.ex b/apps/explorer/lib/explorer/chain/staking_pools_delegators.ex new file mode 100644 index 0000000000..d40edfaad8 --- /dev/null +++ b/apps/explorer/lib/explorer/chain/staking_pools_delegators.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 diff --git a/apps/explorer/priv/repo/migrations/20190523112839_create_staking_pools_delegators.exs b/apps/explorer/priv/repo/migrations/20190523112839_create_staking_pools_delegators.exs new file mode 100644 index 0000000000..1f243771fb --- /dev/null +++ b/apps/explorer/priv/repo/migrations/20190523112839_create_staking_pools_delegators.exs @@ -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 diff --git a/apps/explorer/test/explorer/chain/staking_pools_delegators_test.exs b/apps/explorer/test/explorer/chain/staking_pools_delegators_test.exs new file mode 100644 index 0000000000..a27dada2eb --- /dev/null +++ b/apps/explorer/test/explorer/chain/staking_pools_delegators_test.exs @@ -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 diff --git a/apps/explorer/test/support/factory.ex b/apps/explorer/test/support/factory.ex index 97a449f33e..58a16c97b9 100644 --- a/apps/explorer/test/support/factory.ex +++ b/apps/explorer/test/support/factory.ex @@ -27,7 +27,8 @@ defmodule Explorer.Factory do Token, TokenTransfer, Transaction, - StakingPool + StakingPool, + StakingPoolsDelegators } alias Explorer.Market.MarketHistory @@ -628,4 +629,18 @@ defmodule Explorer.Factory do was_validator_count: 1 } 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