Types.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. import BN from 'bn.js';
  2. import { ElectionStage, Seat } from '@joystream/types/council';
  3. import { Option, Text } from '@polkadot/types';
  4. import { Constructor } from '@polkadot/types/types';
  5. import { Struct, Vec } from '@polkadot/types/codec';
  6. import { u32 } from '@polkadot/types/primitive';
  7. import { BlockNumber, Balance, AccountId } from '@polkadot/types/interfaces';
  8. import { DerivedBalances } from '@polkadot/api-derive/types';
  9. import { KeyringPair } from '@polkadot/keyring/types';
  10. import { WorkerId } from '@joystream/types/working-group';
  11. import { Profile, MemberId } from '@joystream/types/members';
  12. import {
  13. GenericJoyStreamRoleSchema,
  14. JobSpecifics,
  15. ApplicationDetails,
  16. QuestionSections,
  17. QuestionSection,
  18. QuestionsFields,
  19. QuestionField,
  20. EntryInMembershipModuke,
  21. HiringProcess,
  22. AdditionalRolehiringProcessDetails,
  23. CreatorDetails
  24. } from '@joystream/types/hiring/schemas/role.schema.typings';
  25. import ajv from 'ajv';
  26. import { Opening, StakingPolicy, ApplicationStageKeys } from '@joystream/types/hiring';
  27. // KeyringPair type extended with mandatory "meta.name"
  28. // It's used for accounts/keys management within CLI.
  29. // If not provided in the account json file, the meta.name value is set to "Unnamed Account"
  30. export type NamedKeyringPair = KeyringPair & {
  31. meta: {
  32. name: string
  33. }
  34. }
  35. // Summary of the account information fetched from the api for "account:current" purposes (currently just balances)
  36. export type AccountSummary = {
  37. balances: DerivedBalances
  38. }
  39. // Object/Tuple containing council/councilElection information (council:info).
  40. // The tuple is useful, because that's how api.queryMulti returns the results.
  41. export type CouncilInfoTuple = Parameters<typeof createCouncilInfoObj>;
  42. export type CouncilInfoObj = ReturnType<typeof createCouncilInfoObj>;
  43. // This function allows us to easily transform the tuple into the object
  44. // and simplifies the creation of consitent Object and Tuple types (seen above).
  45. export function createCouncilInfoObj(
  46. activeCouncil: Seat[],
  47. termEndsAt: BlockNumber,
  48. autoStart: Boolean,
  49. newTermDuration: BN,
  50. candidacyLimit: BN,
  51. councilSize: BN,
  52. minCouncilStake: Balance,
  53. minVotingStake: Balance,
  54. announcingPeriod: BlockNumber,
  55. votingPeriod: BlockNumber,
  56. revealingPeriod: BlockNumber,
  57. round: BN,
  58. stage: Option<ElectionStage>
  59. ) {
  60. return {
  61. activeCouncil,
  62. termEndsAt,
  63. autoStart,
  64. newTermDuration,
  65. candidacyLimit,
  66. councilSize,
  67. minCouncilStake,
  68. minVotingStake,
  69. announcingPeriod,
  70. votingPeriod,
  71. revealingPeriod,
  72. round,
  73. stage
  74. };
  75. }
  76. // Object with "name" and "value" properties, used for rendering simple CLI tables like:
  77. // Total balance: 100 JOY
  78. // Free calance: 50 JOY
  79. export type NameValueObj = { name: string, value: string };
  80. // Working groups related types
  81. export enum WorkingGroups {
  82. StorageProviders = 'storageProviders'
  83. }
  84. // In contrast to Pioneer, currently only StorageProviders group is available in CLI
  85. export const AvailableGroups: readonly WorkingGroups[] = [
  86. WorkingGroups.StorageProviders
  87. ] as const;
  88. // Compound working group types
  89. export type GroupMember = {
  90. workerId: WorkerId;
  91. memberId: MemberId;
  92. roleAccount: AccountId;
  93. profile: Profile;
  94. stake: Balance;
  95. earned: Balance;
  96. }
  97. export type GroupApplication = {
  98. wgApplicationId: number;
  99. applicationId: number;
  100. member: Profile | null;
  101. roleAccout: AccountId;
  102. stakes: {
  103. application: number;
  104. role: number;
  105. },
  106. humanReadableText: string;
  107. stage: ApplicationStageKeys;
  108. }
  109. export enum OpeningStatus {
  110. WaitingToBegin = 'WaitingToBegin',
  111. AcceptingApplications = 'AcceptingApplications',
  112. InReview = 'InReview',
  113. Complete = 'Complete',
  114. Cancelled = 'Cancelled',
  115. Unknown = 'Unknown'
  116. }
  117. export type GroupOpeningStage = {
  118. status: OpeningStatus;
  119. block?: number;
  120. date?: Date;
  121. }
  122. export type GroupOpeningStakes = {
  123. application?: StakingPolicy;
  124. role?: StakingPolicy;
  125. }
  126. export type GroupOpening = {
  127. wgOpeningId: number;
  128. openingId: number;
  129. stage: GroupOpeningStage;
  130. opening: Opening;
  131. stakes: GroupOpeningStakes;
  132. applications: GroupApplication[];
  133. }
  134. // Some helper structs for generating human_readable_text in working group opening extrinsic
  135. // Note those types are not part of the runtime etc., we just use them to simplify prompting for values
  136. // (since there exists functionality that handles that for substrate types like: Struct, Vec etc.)
  137. interface WithJSONable<T> {
  138. toJSON: () => T;
  139. }
  140. export class HRTJobSpecificsStruct extends Struct implements WithJSONable<JobSpecifics> {
  141. constructor (value?: JobSpecifics) {
  142. super({
  143. title: "Text",
  144. description: "Text",
  145. }, value);
  146. }
  147. get title(): string {
  148. return (this.get('title') as Text).toString();
  149. }
  150. get description(): string {
  151. return (this.get('description') as Text).toString();
  152. }
  153. toJSON(): JobSpecifics {
  154. const { title, description } = this;
  155. return { title, description };
  156. }
  157. }
  158. export class HRTEntryInMembershipModukeStruct extends Struct implements WithJSONable<EntryInMembershipModuke> {
  159. constructor (value?: EntryInMembershipModuke) {
  160. super({
  161. handle: "Text",
  162. }, value);
  163. }
  164. get handle(): string {
  165. return (this.get('handle') as Text).toString();
  166. }
  167. toJSON(): EntryInMembershipModuke {
  168. const { handle } = this;
  169. return { handle };
  170. }
  171. }
  172. export class HRTCreatorDetailsStruct extends Struct implements WithJSONable<CreatorDetails> {
  173. constructor (value?: CreatorDetails) {
  174. super({
  175. membership: HRTEntryInMembershipModukeStruct,
  176. }, value);
  177. }
  178. get membership(): EntryInMembershipModuke {
  179. return (this.get('membership') as HRTEntryInMembershipModukeStruct).toJSON();
  180. }
  181. toJSON(): CreatorDetails {
  182. const { membership } = this;
  183. return { membership };
  184. }
  185. }
  186. export class HRTHiringProcessStruct extends Struct implements WithJSONable<HiringProcess> {
  187. constructor (value?: HiringProcess) {
  188. super({
  189. details: "Vec<Text>",
  190. }, value);
  191. }
  192. get details(): AdditionalRolehiringProcessDetails {
  193. return (this.get('details') as Vec<Text>).toArray().map(v => v.toString());
  194. }
  195. toJSON(): HiringProcess {
  196. const { details } = this;
  197. return { details };
  198. }
  199. }
  200. export class HRTQuestionFieldStruct extends Struct implements WithJSONable<QuestionField> {
  201. constructor (value?: QuestionField) {
  202. super({
  203. title: "Text",
  204. type: "Text"
  205. }, value);
  206. }
  207. get title(): string {
  208. return (this.get('title') as Text).toString();
  209. }
  210. get type(): string {
  211. return (this.get('type') as Text).toString();
  212. }
  213. toJSON(): QuestionField {
  214. const { title, type } = this;
  215. return { title, type };
  216. }
  217. }
  218. class HRTQuestionsFieldsVec extends Vec.with(HRTQuestionFieldStruct) implements WithJSONable<QuestionsFields> {
  219. toJSON(): QuestionsFields {
  220. return this.toArray().map(v => v.toJSON());
  221. }
  222. }
  223. export class HRTQuestionSectionStruct extends Struct implements WithJSONable<QuestionSection> {
  224. constructor (value?: QuestionSection) {
  225. super({
  226. title: "Text",
  227. questions: HRTQuestionsFieldsVec
  228. }, value);
  229. }
  230. get title(): string {
  231. return (this.get('title') as Text).toString();
  232. }
  233. get questions(): QuestionsFields {
  234. return (this.get('questions') as HRTQuestionsFieldsVec).toJSON();
  235. }
  236. toJSON(): QuestionSection {
  237. const { title, questions } = this;
  238. return { title, questions };
  239. }
  240. }
  241. export class HRTQuestionSectionsVec extends Vec.with(HRTQuestionSectionStruct) implements WithJSONable<QuestionSections> {
  242. toJSON(): QuestionSections {
  243. return this.toArray().map(v => v.toJSON());
  244. }
  245. };
  246. export class HRTApplicationDetailsStruct extends Struct implements WithJSONable<ApplicationDetails> {
  247. constructor (value?: ApplicationDetails) {
  248. super({
  249. sections: HRTQuestionSectionsVec
  250. }, value);
  251. }
  252. get sections(): QuestionSections {
  253. return (this.get('sections') as HRTQuestionSectionsVec).toJSON();
  254. }
  255. toJSON(): ApplicationDetails {
  256. const { sections } = this;
  257. return { sections };
  258. }
  259. }
  260. export class HRTStruct extends Struct implements WithJSONable<GenericJoyStreamRoleSchema> {
  261. constructor (value?: GenericJoyStreamRoleSchema) {
  262. super({
  263. version: "u32",
  264. headline: "Text",
  265. job: HRTJobSpecificsStruct,
  266. application: HRTApplicationDetailsStruct,
  267. reward: "Text",
  268. creator: HRTCreatorDetailsStruct,
  269. process: HRTHiringProcessStruct
  270. }, value);
  271. }
  272. get version(): number {
  273. return (this.get('version') as u32).toNumber();
  274. }
  275. get headline(): string {
  276. return (this.get('headline') as Text).toString();
  277. }
  278. get job(): JobSpecifics {
  279. return (this.get('job') as HRTJobSpecificsStruct).toJSON();
  280. }
  281. get application(): ApplicationDetails {
  282. return (this.get('application') as HRTApplicationDetailsStruct).toJSON();
  283. }
  284. get reward(): string {
  285. return (this.get('reward') as Text).toString();
  286. }
  287. get creator(): CreatorDetails {
  288. return (this.get('creator') as HRTCreatorDetailsStruct).toJSON();
  289. }
  290. get process(): HiringProcess {
  291. return (this.get('process') as HRTHiringProcessStruct).toJSON();
  292. }
  293. toJSON(): GenericJoyStreamRoleSchema {
  294. const { version, headline, job, application, reward, creator, process } = this;
  295. return { version, headline, job, application, reward, creator, process };
  296. }
  297. };
  298. // A mapping of argName to json struct and schemaValidator
  299. // It is used to map arguments of type "Bytes" that are in fact a json string
  300. // (and can be validated against a schema)
  301. export type JSONArgsMapping = { [argName: string]: {
  302. struct: Constructor<Struct>,
  303. schemaValidator: ajv.ValidateFunction
  304. } };