index.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. import { Enum, Option, Struct, Vec } from "@polkadot/types/codec";
  2. import { getTypeRegistry, Text } from "@polkadot/types";
  3. import { BlockNumber, AccountId, Balance, Hash } from "@polkadot/types/interfaces";
  4. import { u32, bool } from "@polkadot/types/primitive";
  5. import { Codec } from "@polkadot/types/types";
  6. import { registerForumTypes } from "./forum";
  7. import { registerMediaTypes } from "./media";
  8. import { registerMembershipTypes } from "./members";
  9. import { registerRolesTypes } from "./roles";
  10. import { registerDiscoveryTypes } from "./discovery";
  11. import { registerHiringTypes } from "./hiring";
  12. import { registerVersionedStoreTypes } from "./versioned-store";
  13. import { registerVersionedStorePermissionsTypes } from "./versioned-store/permissions";
  14. import { registerStakeTypes } from "./stake";
  15. import { registerMintTypes } from "./mint";
  16. import { registerRecurringRewardsTypes } from "./recurring-rewards";
  17. import { registerContentWorkingGroupTypes } from "./content-working-group";
  18. import { registerProposalTypes, ProposalStatus } from "./proposals";
  19. export function getTextPropAsString(struct: Struct, fieldName: string): string {
  20. return (struct.get(fieldName) as Text).toString();
  21. }
  22. export function getBoolPropAsBoolean(struct: Struct, fieldName: string): boolean {
  23. return (struct.get(fieldName) as bool).valueOf();
  24. }
  25. export function getOptionPropOrUndefined<T extends Codec>(struct: Struct, fieldName: string): T | undefined {
  26. return (struct.get(fieldName) as Option<T>).unwrapOr(undefined);
  27. }
  28. export class OptionText extends Option.with(Text) {
  29. static none(): OptionText {
  30. return new Option(Text, null);
  31. }
  32. static some(text: string): OptionText {
  33. return new Option(Text, text);
  34. }
  35. }
  36. export type TransferableStake = {
  37. seat: Balance;
  38. backing: Balance;
  39. };
  40. export type Stake = {
  41. new: Balance;
  42. transferred: Balance;
  43. };
  44. export type Backer = {
  45. member: AccountId;
  46. stake: Balance;
  47. };
  48. export type Seat = {
  49. member: AccountId;
  50. stake: Balance;
  51. backers: Backer[];
  52. };
  53. export type SealedVote = {
  54. voter: AccountId;
  55. commitment: Hash;
  56. stake: Stake;
  57. vote: Option<AccountId>;
  58. };
  59. export type ProposalVote = {
  60. voter: AccountId;
  61. kind: VoteKind;
  62. };
  63. export type TallyResult = {
  64. proposal_id: u32;
  65. abstentions: u32;
  66. approvals: u32;
  67. rejections: u32;
  68. slashes: u32;
  69. status: ProposalStatus;
  70. finalized_at: BlockNumber;
  71. };
  72. export class Announcing extends u32 {}
  73. export class Voting extends u32 {}
  74. export class Revealing extends u32 {}
  75. export class ElectionStage extends Enum {
  76. constructor(value?: any, index?: number) {
  77. super(
  78. {
  79. Announcing,
  80. Voting,
  81. Revealing
  82. },
  83. value,
  84. index
  85. );
  86. }
  87. /** Create a new Announcing stage. */
  88. static Announcing(endsAt: BlockNumber | number): ElectionStage {
  89. return this.newElectionStage("Announcing", endsAt);
  90. }
  91. /** Create a new Voting stage. */
  92. static Voting(endsAt: BlockNumber | number): ElectionStage {
  93. return this.newElectionStage("Voting", endsAt);
  94. }
  95. /** Create a new Revealing stage. */
  96. static Revealing(endsAt: BlockNumber | number): ElectionStage {
  97. return this.newElectionStage("Revealing", endsAt);
  98. }
  99. static newElectionStage(stageName: string, endsAt: BlockNumber | number) {
  100. return new ElectionStage({ [stageName]: endsAt });
  101. }
  102. }
  103. export type AnyElectionStage = Announcing | Voting | Revealing;
  104. export const VoteKinds: { [key: string]: string } = {
  105. Abstain: "Abstain",
  106. Approve: "Approve",
  107. Reject: "Reject",
  108. Slash: "Slash"
  109. };
  110. export class VoteKind extends Enum {
  111. constructor(value?: any) {
  112. super(["Abstain", "Approve", "Reject", "Slash"], value);
  113. }
  114. }
  115. export type ProposalVotes = [AccountId, VoteKind][];
  116. // Treat a BTreeSet as a Vec since it is encoded in the same way.
  117. export class BTreeSet<T extends Codec> extends Vec<T> {}
  118. // TODO Refactor: split this function and move to corresponding modules: election and proposals.
  119. function registerElectionAndProposalTypes() {
  120. try {
  121. const typeRegistry = getTypeRegistry();
  122. // Is this enough?
  123. typeRegistry.register({
  124. BTreeSet
  125. });
  126. typeRegistry.register({
  127. MemoText: "Text"
  128. });
  129. // Register parametrized enum ElectionStage:
  130. typeRegistry.register({
  131. ElectionStage
  132. });
  133. typeRegistry.register({
  134. ProposalStatus,
  135. VoteKind
  136. });
  137. typeRegistry.register({
  138. ElectionStake: {
  139. new: "Balance",
  140. transferred: "Balance"
  141. },
  142. SealedVote: {
  143. voter: "AccountId",
  144. commitment: "Hash",
  145. stake: "ElectionStake",
  146. vote: "Option<AccountId>"
  147. },
  148. TransferableStake: {
  149. seat: "Balance",
  150. backing: "Balance"
  151. },
  152. RuntimeUpgradeProposal: {
  153. id: "u32",
  154. proposer: "AccountId",
  155. stake: "Balance",
  156. name: "Text",
  157. description: "Text",
  158. wasm_hash: "Hash",
  159. proposed_at: "BlockNumber",
  160. status: "ProposalStatus"
  161. },
  162. "TallyResult<BlockNumber>": {
  163. proposal_id: "u32",
  164. abstentions: "u32",
  165. approvals: "u32",
  166. rejections: "u32",
  167. slashes: "u32",
  168. status: "ProposalStatus",
  169. finalized_at: "BlockNumber"
  170. },
  171. ElectionParameters: {
  172. announcing_period: "BlockNumber",
  173. voting_period: "BlockNumber",
  174. revealing_period: "BlockNumber",
  175. council_size: "u32",
  176. candidacy_limit: "u32",
  177. new_term_duration: "BlockNumber",
  178. min_council_stake: "Balance",
  179. min_voting_stake: "Balance"
  180. }
  181. });
  182. } catch (err) {
  183. console.error("Failed to register custom types of Joystream node", err);
  184. }
  185. }
  186. export function registerJoystreamTypes() {
  187. registerMembershipTypes();
  188. registerRolesTypes();
  189. registerMediaTypes();
  190. registerForumTypes();
  191. registerElectionAndProposalTypes();
  192. registerDiscoveryTypes();
  193. registerVersionedStoreTypes();
  194. registerVersionedStorePermissionsTypes();
  195. registerStakeTypes();
  196. registerMintTypes();
  197. registerRecurringRewardsTypes();
  198. registerHiringTypes();
  199. registerContentWorkingGroupTypes();
  200. registerProposalTypes();
  201. require("./bureaucracy").registerBureaucracyTypes();
  202. }