Types.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. import BN from 'bn.js'
  2. import { ElectionStage, Seat } from '@joystream/types/council'
  3. import { Option, Text } from '@polkadot/types'
  4. import { Constructor, Codec } 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, OpeningType } 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. import { Validator } from 'inquirer'
  28. // KeyringPair type extended with mandatory "meta.name"
  29. // It's used for accounts/keys management within CLI.
  30. // If not provided in the account json file, the meta.name value is set to "Unnamed Account"
  31. export type NamedKeyringPair = KeyringPair & {
  32. meta: {
  33. name: string
  34. }
  35. }
  36. // Summary of the account information fetched from the api for "account:current" purposes (currently just balances)
  37. export type AccountSummary = {
  38. balances: DerivedBalances
  39. }
  40. // This function allows us to easily transform the tuple into the object
  41. // and simplifies the creation of consitent Object and Tuple types (seen below).
  42. export function createCouncilInfoObj(
  43. activeCouncil: Seat[],
  44. termEndsAt: BlockNumber,
  45. autoStart: boolean,
  46. newTermDuration: BN,
  47. candidacyLimit: BN,
  48. councilSize: BN,
  49. minCouncilStake: Balance,
  50. minVotingStake: Balance,
  51. announcingPeriod: BlockNumber,
  52. votingPeriod: BlockNumber,
  53. revealingPeriod: BlockNumber,
  54. round: BN,
  55. stage: Option<ElectionStage>
  56. ) {
  57. return {
  58. activeCouncil,
  59. termEndsAt,
  60. autoStart,
  61. newTermDuration,
  62. candidacyLimit,
  63. councilSize,
  64. minCouncilStake,
  65. minVotingStake,
  66. announcingPeriod,
  67. votingPeriod,
  68. revealingPeriod,
  69. round,
  70. stage,
  71. }
  72. }
  73. // Object/Tuple containing council/councilElection information (council:info).
  74. // The tuple is useful, because that's how api.queryMulti returns the results.
  75. export type CouncilInfoTuple = Parameters<typeof createCouncilInfoObj>
  76. export type CouncilInfoObj = ReturnType<typeof createCouncilInfoObj>
  77. // Object with "name" and "value" properties, used for rendering simple CLI tables like:
  78. // Total balance: 100 JOY
  79. // Free calance: 50 JOY
  80. export type NameValueObj = { name: string; value: string }
  81. // Working groups related types
  82. export enum WorkingGroups {
  83. StorageProviders = 'storageProviders',
  84. }
  85. // In contrast to Pioneer, currently only StorageProviders group is available in CLI
  86. export const AvailableGroups: readonly WorkingGroups[] = [WorkingGroups.StorageProviders] as const
  87. export type Reward = {
  88. totalRecieved: Balance
  89. value: Balance
  90. interval?: number
  91. nextPaymentBlock: number // 0 = no incoming payment
  92. }
  93. // Compound working group types
  94. export type GroupMember = {
  95. workerId: WorkerId
  96. memberId: MemberId
  97. roleAccount: AccountId
  98. profile: Profile
  99. stake?: Balance
  100. reward?: Reward
  101. }
  102. export type GroupApplication = {
  103. wgApplicationId: number
  104. applicationId: number
  105. wgOpeningId: number
  106. member: Profile | null
  107. roleAccout: AccountId
  108. stakes: {
  109. application: number
  110. role: number
  111. }
  112. humanReadableText: string
  113. stage: ApplicationStageKeys
  114. }
  115. export enum OpeningStatus {
  116. WaitingToBegin = 'WaitingToBegin',
  117. AcceptingApplications = 'AcceptingApplications',
  118. InReview = 'InReview',
  119. Complete = 'Complete',
  120. Cancelled = 'Cancelled',
  121. Unknown = 'Unknown',
  122. }
  123. export type GroupOpeningStage = {
  124. status: OpeningStatus
  125. block?: number
  126. date?: Date
  127. }
  128. export type GroupOpeningStakes = {
  129. application?: StakingPolicy
  130. role?: StakingPolicy
  131. }
  132. export type GroupOpening = {
  133. wgOpeningId: number
  134. openingId: number
  135. stage: GroupOpeningStage
  136. opening: Opening
  137. stakes: GroupOpeningStakes
  138. applications: GroupApplication[]
  139. type: OpeningType
  140. }
  141. // Some helper structs for generating human_readable_text in working group opening extrinsic
  142. // Note those types are not part of the runtime etc., we just use them to simplify prompting for values
  143. // (since there exists functionality that handles that for substrate types like: Struct, Vec etc.)
  144. interface WithJSONable<T> {
  145. toJSON: () => T
  146. }
  147. export class HRTJobSpecificsStruct extends Struct implements WithJSONable<JobSpecifics> {
  148. constructor(value?: JobSpecifics) {
  149. super(
  150. {
  151. title: 'Text',
  152. description: 'Text',
  153. },
  154. value
  155. )
  156. }
  157. get title(): string {
  158. return (this.get('title') as Text).toString()
  159. }
  160. get description(): string {
  161. return (this.get('description') as Text).toString()
  162. }
  163. toJSON(): JobSpecifics {
  164. const { title, description } = this
  165. return { title, description }
  166. }
  167. }
  168. export class HRTEntryInMembershipModukeStruct extends Struct implements WithJSONable<EntryInMembershipModuke> {
  169. constructor(value?: EntryInMembershipModuke) {
  170. super(
  171. {
  172. handle: 'Text',
  173. },
  174. value
  175. )
  176. }
  177. get handle(): string {
  178. return (this.get('handle') as Text).toString()
  179. }
  180. toJSON(): EntryInMembershipModuke {
  181. const { handle } = this
  182. return { handle }
  183. }
  184. }
  185. export class HRTCreatorDetailsStruct extends Struct implements WithJSONable<CreatorDetails> {
  186. constructor(value?: CreatorDetails) {
  187. super(
  188. {
  189. membership: HRTEntryInMembershipModukeStruct,
  190. },
  191. value
  192. )
  193. }
  194. get membership(): EntryInMembershipModuke {
  195. return (this.get('membership') as HRTEntryInMembershipModukeStruct).toJSON()
  196. }
  197. toJSON(): CreatorDetails {
  198. const { membership } = this
  199. return { membership }
  200. }
  201. }
  202. export class HRTHiringProcessStruct extends Struct implements WithJSONable<HiringProcess> {
  203. constructor(value?: HiringProcess) {
  204. super(
  205. {
  206. details: 'Vec<Text>',
  207. },
  208. value
  209. )
  210. }
  211. get details(): AdditionalRolehiringProcessDetails {
  212. return (this.get('details') as Vec<Text>).toArray().map((v) => v.toString())
  213. }
  214. toJSON(): HiringProcess {
  215. const { details } = this
  216. return { details }
  217. }
  218. }
  219. export class HRTQuestionFieldStruct extends Struct implements WithJSONable<QuestionField> {
  220. constructor(value?: QuestionField) {
  221. super(
  222. {
  223. title: 'Text',
  224. type: 'Text',
  225. },
  226. value
  227. )
  228. }
  229. get title(): string {
  230. return (this.get('title') as Text).toString()
  231. }
  232. get type(): string {
  233. return (this.get('type') as Text).toString()
  234. }
  235. toJSON(): QuestionField {
  236. const { title, type } = this
  237. return { title, type }
  238. }
  239. }
  240. class HRTQuestionsFieldsVec extends Vec.with(HRTQuestionFieldStruct) implements WithJSONable<QuestionsFields> {
  241. toJSON(): QuestionsFields {
  242. return this.toArray().map((v) => v.toJSON())
  243. }
  244. }
  245. export class HRTQuestionSectionStruct extends Struct implements WithJSONable<QuestionSection> {
  246. constructor(value?: QuestionSection) {
  247. super(
  248. {
  249. title: 'Text',
  250. questions: HRTQuestionsFieldsVec,
  251. },
  252. value
  253. )
  254. }
  255. get title(): string {
  256. return (this.get('title') as Text).toString()
  257. }
  258. get questions(): QuestionsFields {
  259. return (this.get('questions') as HRTQuestionsFieldsVec).toJSON()
  260. }
  261. toJSON(): QuestionSection {
  262. const { title, questions } = this
  263. return { title, questions }
  264. }
  265. }
  266. export class HRTQuestionSectionsVec extends Vec.with(HRTQuestionSectionStruct)
  267. implements WithJSONable<QuestionSections> {
  268. toJSON(): QuestionSections {
  269. return this.toArray().map((v) => v.toJSON())
  270. }
  271. }
  272. export class HRTApplicationDetailsStruct extends Struct implements WithJSONable<ApplicationDetails> {
  273. constructor(value?: ApplicationDetails) {
  274. super(
  275. {
  276. sections: HRTQuestionSectionsVec,
  277. },
  278. value
  279. )
  280. }
  281. get sections(): QuestionSections {
  282. return (this.get('sections') as HRTQuestionSectionsVec).toJSON()
  283. }
  284. toJSON(): ApplicationDetails {
  285. const { sections } = this
  286. return { sections }
  287. }
  288. }
  289. export class HRTStruct extends Struct implements WithJSONable<GenericJoyStreamRoleSchema> {
  290. constructor(value?: GenericJoyStreamRoleSchema) {
  291. super(
  292. {
  293. version: 'u32',
  294. headline: 'Text',
  295. job: HRTJobSpecificsStruct,
  296. application: HRTApplicationDetailsStruct,
  297. reward: 'Text',
  298. creator: HRTCreatorDetailsStruct,
  299. process: HRTHiringProcessStruct,
  300. },
  301. value
  302. )
  303. }
  304. get version(): number {
  305. return (this.get('version') as u32).toNumber()
  306. }
  307. get headline(): string {
  308. return (this.get('headline') as Text).toString()
  309. }
  310. get job(): JobSpecifics {
  311. return (this.get('job') as HRTJobSpecificsStruct).toJSON()
  312. }
  313. get application(): ApplicationDetails {
  314. return (this.get('application') as HRTApplicationDetailsStruct).toJSON()
  315. }
  316. get reward(): string {
  317. return (this.get('reward') as Text).toString()
  318. }
  319. get creator(): CreatorDetails {
  320. return (this.get('creator') as HRTCreatorDetailsStruct).toJSON()
  321. }
  322. get process(): HiringProcess {
  323. return (this.get('process') as HRTHiringProcessStruct).toJSON()
  324. }
  325. toJSON(): GenericJoyStreamRoleSchema {
  326. const { version, headline, job, application, reward, creator, process } = this
  327. return { version, headline, job, application, reward, creator, process }
  328. }
  329. }
  330. // Api-related
  331. // Additional options that can be passed to ApiCommandBase.promptForParam in order to override
  332. // its default behaviour, change param name, add validation etc.
  333. export type ApiParamOptions<ParamType = Codec> = {
  334. forcedName?: string
  335. value?: {
  336. default: ParamType
  337. locked?: boolean
  338. }
  339. jsonSchema?: {
  340. struct: Constructor<Struct>
  341. schemaValidator: ajv.ValidateFunction
  342. }
  343. validator?: Validator
  344. nestedOptions?: ApiParamsOptions // For more complex params, like structs
  345. }
  346. export type ApiParamsOptions = {
  347. [paramName: string]: ApiParamOptions
  348. }
  349. export type ApiMethodArg = Codec
  350. export type ApiMethodNamedArg = {
  351. name: string
  352. value: ApiMethodArg
  353. }
  354. export type ApiMethodNamedArgs = ApiMethodNamedArg[]