index.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { u32, u64, u128, Null, BTreeMap, bool } from '@polkadot/types'
  2. import { BlockNumber, Balance } from '@polkadot/types/interfaces'
  3. import { RegistryTypes } from '@polkadot/types/types'
  4. import { JoyEnum, JoyStructDecorated } from '../common'
  5. export class StakeId extends u64 {}
  6. export class SlashId extends u64 {}
  7. export type ISlash = {
  8. started_at_block: BlockNumber
  9. is_active: bool
  10. blocks_remaining_in_active_period_for_slashing: BlockNumber
  11. slash_amount: Balance
  12. }
  13. export class Slash
  14. extends JoyStructDecorated({
  15. started_at_block: u32,
  16. is_active: bool,
  17. blocks_remaining_in_active_period_for_slashing: u32,
  18. slash_amount: u128,
  19. })
  20. implements ISlash {}
  21. export type IUnstakingState = {
  22. started_at_block: BlockNumber
  23. is_active: bool
  24. blocks_remaining_in_active_period_for_unstaking: BlockNumber
  25. }
  26. export class UnstakingState
  27. extends JoyStructDecorated({
  28. started_at_block: u32,
  29. is_active: bool,
  30. blocks_remaining_in_active_period_for_unstaking: u32,
  31. })
  32. implements IUnstakingState {}
  33. export class Normal extends Null {}
  34. export class Unstaking extends UnstakingState {}
  35. export class StakedStatus extends JoyEnum({
  36. Normal,
  37. Unstaking,
  38. } as const) {}
  39. export type IStakedState = {
  40. staked_amount: Balance
  41. staked_status: StakedStatus
  42. next_slash_id: SlashId
  43. ongoing_slashes: BTreeMap<SlashId, Slash>
  44. }
  45. export class StakedState
  46. extends JoyStructDecorated({
  47. staked_amount: u128,
  48. staked_status: StakedStatus,
  49. next_slash_id: SlashId,
  50. ongoing_slashes: BTreeMap.with(SlashId, Slash),
  51. })
  52. implements IStakedState {}
  53. export class NotStaked extends Null {}
  54. export class Staked extends StakedState {}
  55. export class StakingStatus extends JoyEnum({
  56. NotStaked,
  57. Staked,
  58. } as const) {}
  59. export type IStake = {
  60. created: BlockNumber
  61. staking_status: StakingStatus
  62. }
  63. export class Stake
  64. extends JoyStructDecorated({
  65. created: u32,
  66. staking_status: StakingStatus,
  67. })
  68. implements IStake {
  69. get value(): Balance {
  70. if (this.staking_status.isOfType('Staked')) {
  71. return this.staking_status.asType('Staked').staked_amount
  72. }
  73. return this.registry.createType('Balance', 0)
  74. }
  75. }
  76. export const stakeTypes: RegistryTypes = {
  77. StakeId: 'u64',
  78. Stake,
  79. // Expose in registry for api.createType purposes:
  80. StakingStatus,
  81. Staked,
  82. StakedStatus,
  83. Unstaking,
  84. Slash,
  85. }
  86. export default stakeTypes