members.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import {
  2. Enum,
  3. getTypeRegistry,
  4. Option,
  5. Struct,
  6. Null,
  7. bool,
  8. u32,
  9. u64,
  10. u128,
  11. Text,
  12. GenericAccountId,
  13. } from '@polkadot/types'
  14. import { BlockNumber, Moment, BalanceOf } from '@polkadot/types/interfaces'
  15. import { JoyStruct } from './common'
  16. import AccountId from '@polkadot/types/primitive/Generic/AccountId'
  17. export class MemberId extends u64 {}
  18. export class PaidTermId extends u64 {}
  19. export class SubscriptionId extends u64 {}
  20. export class ActorId extends u64 {}
  21. export class Paid extends PaidTermId {}
  22. export class Screening extends GenericAccountId {}
  23. export class Genesis extends Null {}
  24. export class EntryMethod extends Enum {
  25. constructor(value?: any, index?: number) {
  26. super(
  27. {
  28. Paid,
  29. Screening,
  30. Genesis,
  31. },
  32. value,
  33. index
  34. )
  35. }
  36. }
  37. export type IMembership = {
  38. handle: Text
  39. avatar_uri: Text
  40. about: Text
  41. registered_at_block: BlockNumber
  42. registered_at_time: Moment
  43. entry: EntryMethod
  44. suspended: bool
  45. subscription: Option<SubscriptionId>
  46. root_account: AccountId
  47. controller_account: AccountId
  48. }
  49. export class Membership extends JoyStruct<IMembership> {
  50. constructor(value?: IMembership) {
  51. super(
  52. {
  53. handle: Text,
  54. avatar_uri: Text,
  55. about: Text,
  56. registered_at_block: u32,
  57. registered_at_time: u64,
  58. entry: EntryMethod,
  59. suspended: bool,
  60. subscription: Option.with(SubscriptionId),
  61. root_account: AccountId,
  62. controller_account: AccountId,
  63. },
  64. value
  65. )
  66. }
  67. get handle(): Text {
  68. return this.get('handle') as Text
  69. }
  70. get avatar_uri(): Text {
  71. return this.get('avatar_uri') as Text
  72. }
  73. get about(): Text {
  74. return this.get('about') as Text
  75. }
  76. get registered_at_block(): u32 {
  77. return this.get('registered_at_block') as u32
  78. }
  79. get registered_at_time(): u64 {
  80. return this.get('registered_at_time') as u64
  81. }
  82. get entry(): EntryMethod {
  83. return this.get('entry') as EntryMethod
  84. }
  85. get suspended(): bool {
  86. return this.get('suspended') as bool
  87. }
  88. get subscription(): Option<SubscriptionId> {
  89. return this.get('subscription') as Option<SubscriptionId>
  90. }
  91. get root_account(): AccountId {
  92. return this.get('root_account') as AccountId
  93. }
  94. get controller_account(): AccountId {
  95. return this.get('controller_account') as AccountId
  96. }
  97. }
  98. export class PaidMembershipTerms extends Struct {
  99. constructor(value?: any) {
  100. super(
  101. {
  102. fee: u128, // BalanceOf
  103. text: Text,
  104. },
  105. value
  106. )
  107. }
  108. get fee(): BalanceOf {
  109. return this.get('fee') as BalanceOf
  110. }
  111. get text(): Text {
  112. return this.get('text') as Text
  113. }
  114. }
  115. export function registerMembershipTypes() {
  116. try {
  117. const typeRegistry = getTypeRegistry()
  118. typeRegistry.register({
  119. EntryMethod,
  120. MemberId,
  121. PaidTermId,
  122. SubscriptionId,
  123. Membership,
  124. PaidMembershipTerms: {
  125. fee: 'BalanceOf',
  126. text: 'Text',
  127. },
  128. ActorId,
  129. })
  130. } catch (err) {
  131. console.error('Failed to register custom types of membership module', err)
  132. }
  133. }