Types.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. import BN from 'bn.js'
  2. import { ElectionStage, Seat } from '@joystream/types/council'
  3. import { Option } from '@polkadot/types'
  4. import { Codec } from '@polkadot/types/types'
  5. import { BlockNumber, Balance, AccountId } from '@polkadot/types/interfaces'
  6. import { DeriveBalancesAll } from '@polkadot/api-derive/types'
  7. import { KeyringPair } from '@polkadot/keyring/types'
  8. import { WorkerId, OpeningType } from '@joystream/types/working-group'
  9. import { Membership, MemberId } from '@joystream/types/members'
  10. import { Opening, StakingPolicy, ApplicationStageKeys } from '@joystream/types/hiring'
  11. import { Validator } from 'inquirer'
  12. import {
  13. VideoMetadata,
  14. ChannelMetadata,
  15. ChannelCategoryMetadata,
  16. VideoCategoryMetadata,
  17. } from '@joystream/content-metadata-protobuf'
  18. import { ContentId, ContentParameters } from '@joystream/types/storage'
  19. import { JSONSchema7, JSONSchema7Definition } from 'json-schema'
  20. // KeyringPair type extended with mandatory "meta.name"
  21. // It's used for accounts/keys management within CLI.
  22. // If not provided in the account json file, the meta.name value is set to "Unnamed Account"
  23. export type NamedKeyringPair = KeyringPair & {
  24. meta: {
  25. name: string
  26. }
  27. }
  28. // Summary of the account information fetched from the api for "account:current" purposes (currently just balances)
  29. export type AccountSummary = {
  30. balances: DeriveBalancesAll
  31. }
  32. // This function allows us to easily transform the tuple into the object
  33. // and simplifies the creation of consitent Object and Tuple types (seen below).
  34. export function createCouncilInfoObj(
  35. activeCouncil: Seat[],
  36. termEndsAt: BlockNumber,
  37. autoStart: boolean,
  38. newTermDuration: BN,
  39. candidacyLimit: BN,
  40. councilSize: BN,
  41. minCouncilStake: Balance,
  42. minVotingStake: Balance,
  43. announcingPeriod: BlockNumber,
  44. votingPeriod: BlockNumber,
  45. revealingPeriod: BlockNumber,
  46. round: BN,
  47. stage: Option<ElectionStage>
  48. ) {
  49. return {
  50. activeCouncil,
  51. termEndsAt,
  52. autoStart,
  53. newTermDuration,
  54. candidacyLimit,
  55. councilSize,
  56. minCouncilStake,
  57. minVotingStake,
  58. announcingPeriod,
  59. votingPeriod,
  60. revealingPeriod,
  61. round,
  62. stage,
  63. }
  64. }
  65. // Object/Tuple containing council/councilElection information (council:info).
  66. // The tuple is useful, because that's how api.queryMulti returns the results.
  67. export type CouncilInfoTuple = Parameters<typeof createCouncilInfoObj>
  68. export type CouncilInfoObj = ReturnType<typeof createCouncilInfoObj>
  69. // Object with "name" and "value" properties, used for rendering simple CLI tables like:
  70. // Total balance: 100 JOY
  71. // Free calance: 50 JOY
  72. export type NameValueObj = { name: string; value: string }
  73. // Working groups related types
  74. export enum WorkingGroups {
  75. StorageProviders = 'storageProviders',
  76. Curators = 'curators',
  77. Operations = 'operations',
  78. Gateway = 'gateway',
  79. }
  80. // In contrast to Pioneer, currently only StorageProviders group is available in CLI
  81. export const AvailableGroups: readonly WorkingGroups[] = [
  82. WorkingGroups.StorageProviders,
  83. WorkingGroups.Curators,
  84. WorkingGroups.Operations,
  85. ] as const
  86. export type Reward = {
  87. totalRecieved: Balance
  88. value: Balance
  89. interval?: number
  90. nextPaymentBlock: number // 0 = no incoming payment
  91. }
  92. // Compound working group types
  93. export type GroupMember = {
  94. workerId: WorkerId
  95. memberId: MemberId
  96. roleAccount: AccountId
  97. profile: Membership
  98. stake?: Balance
  99. reward?: Reward
  100. }
  101. export type GroupApplication = {
  102. wgApplicationId: number
  103. applicationId: number
  104. wgOpeningId: number
  105. member: Membership | null
  106. roleAccout: AccountId
  107. stakes: {
  108. application: number
  109. role: number
  110. }
  111. humanReadableText: string
  112. stage: ApplicationStageKeys
  113. }
  114. export enum OpeningStatus {
  115. WaitingToBegin = 'WaitingToBegin',
  116. AcceptingApplications = 'AcceptingApplications',
  117. InReview = 'InReview',
  118. Complete = 'Complete',
  119. Cancelled = 'Cancelled',
  120. Unknown = 'Unknown',
  121. }
  122. export type GroupOpeningStage = {
  123. status: OpeningStatus
  124. block?: number
  125. date?: Date
  126. }
  127. export type GroupOpeningStakes = {
  128. application?: StakingPolicy
  129. role?: StakingPolicy
  130. }
  131. export const stakingPolicyUnstakingPeriodKeys = [
  132. 'crowded_out_unstaking_period_length',
  133. 'review_period_expired_unstaking_period_length',
  134. ] as const
  135. export type StakingPolicyUnstakingPeriodKey = typeof stakingPolicyUnstakingPeriodKeys[number]
  136. export const openingPolicyUnstakingPeriodsKeys = [
  137. 'fill_opening_failed_applicant_application_stake_unstaking_period',
  138. 'fill_opening_failed_applicant_role_stake_unstaking_period',
  139. 'fill_opening_successful_applicant_application_stake_unstaking_period',
  140. 'terminate_application_stake_unstaking_period',
  141. 'terminate_role_stake_unstaking_period',
  142. 'exit_role_application_stake_unstaking_period',
  143. 'exit_role_stake_unstaking_period',
  144. ] as const
  145. export type OpeningPolicyUnstakingPeriodsKey = typeof openingPolicyUnstakingPeriodsKeys[number]
  146. export type UnstakingPeriodsKey =
  147. | OpeningPolicyUnstakingPeriodsKey
  148. | 'crowded_out_application_stake_unstaking_period_length'
  149. | 'crowded_out_role_stake_unstaking_period_length'
  150. | 'review_period_expired_application_stake_unstaking_period_length'
  151. | 'review_period_expired_role_stake_unstaking_period_length'
  152. export type UnstakingPeriods = {
  153. [k in UnstakingPeriodsKey]: number
  154. }
  155. export type GroupOpening = {
  156. wgOpeningId: number
  157. openingId: number
  158. stage: GroupOpeningStage
  159. opening: Opening
  160. stakes: GroupOpeningStakes
  161. applications: GroupApplication[]
  162. type: OpeningType
  163. unstakingPeriods: UnstakingPeriods
  164. }
  165. // Api-related
  166. // Additional options that can be passed to ApiCommandBase.promptForParam in order to override
  167. // its default behaviour, change param name, add validation etc.
  168. export type ApiParamOptions<ParamType = Codec> = {
  169. forcedName?: string
  170. value?: {
  171. default: ParamType
  172. locked?: boolean
  173. }
  174. validator?: Validator
  175. nestedOptions?: ApiParamsOptions // For more complex params, like structs
  176. }
  177. export type ApiParamsOptions = {
  178. [paramName: string]: ApiParamOptions
  179. }
  180. export type ApiMethodArg = Codec
  181. export type ApiMethodNamedArg = {
  182. name: string
  183. value: ApiMethodArg
  184. }
  185. export type ApiMethodNamedArgs = ApiMethodNamedArg[]
  186. // Content-related
  187. export enum AssetType {
  188. AnyAsset = 1,
  189. }
  190. export type InputAsset = {
  191. path: string
  192. contentId: ContentId
  193. }
  194. export type InputAssetDetails = InputAsset & {
  195. parameters: ContentParameters
  196. }
  197. export type VideoFFProbeMetadata = {
  198. width?: number
  199. height?: number
  200. codecName?: string
  201. codecFullName?: string
  202. duration?: number
  203. }
  204. export type VideoFileMetadata = VideoFFProbeMetadata & {
  205. size: number
  206. container: string
  207. mimeType: string
  208. }
  209. export type VideoInputParameters = Omit<VideoMetadata.AsObject, 'video' | 'thumbnailPhoto'> & {
  210. videoPath?: string
  211. thumbnailPhotoPath?: string
  212. }
  213. export type ChannelInputParameters = Omit<ChannelMetadata.AsObject, 'coverPhoto' | 'avatarPhoto'> & {
  214. coverPhotoPath?: string
  215. avatarPhotoPath?: string
  216. rewardAccount?: string
  217. }
  218. export type ChannelCategoryInputParameters = ChannelCategoryMetadata.AsObject
  219. export type VideoCategoryInputParameters = VideoCategoryMetadata.AsObject
  220. // JSONSchema utility types
  221. export type JSONTypeName<T> = T extends string
  222. ? 'string' | ['string', 'null']
  223. : T extends number
  224. ? 'number' | ['number', 'null']
  225. : T extends any[]
  226. ? 'array' | ['array', 'null']
  227. : T extends Record<string, unknown>
  228. ? 'object' | ['object', 'null']
  229. : T extends boolean
  230. ? 'boolean' | ['boolean', 'null']
  231. : never
  232. export type PropertySchema<P> = Omit<
  233. JSONSchema7Definition & {
  234. type: JSONTypeName<P>
  235. properties: P extends Record<string, unknown> ? JsonSchemaProperties<P> : never
  236. },
  237. P extends Record<string, unknown> ? '' : 'properties'
  238. >
  239. export type JsonSchemaProperties<T extends Record<string, unknown>> = {
  240. [K in keyof Required<T>]: PropertySchema<Required<T>[K]>
  241. }
  242. export type JsonSchema<T extends Record<string, unknown>> = JSONSchema7 & {
  243. type: 'object'
  244. properties: JsonSchemaProperties<T>
  245. }