common.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import { Struct, Option, Text, bool, Vec, u16, u32, u64, getTypeRegistry, Null } from '@polkadot/types'
  2. import { BlockNumber, Moment } from '@polkadot/types/interfaces'
  3. import { Codec } from '@polkadot/types/types'
  4. // we get 'moment' because it is a dependency of @polkadot/util, via @polkadot/keyring
  5. import moment from 'moment'
  6. import { JoyStruct } from './JoyStruct'
  7. import { JoyEnum } from './JoyEnum'
  8. export { JoyStruct } from './JoyStruct'
  9. export { JoyEnum } from './JoyEnum'
  10. // Treat a BTreeSet as a Vec since it is encoded in the same way
  11. export class BTreeSet<T extends Codec> extends Vec<T> {}
  12. export class Credential extends u64 {}
  13. export class CredentialSet extends Vec.with(Credential) {} // BtreeSet ?
  14. // common types between Forum and Proposal Discussions modules
  15. export class ThreadId extends u64 {}
  16. export class PostId extends u64 {}
  17. export type BlockAndTimeType = {
  18. block: BlockNumber
  19. time: Moment
  20. }
  21. export class BlockAndTime extends Struct {
  22. constructor(value?: BlockAndTimeType) {
  23. super(
  24. {
  25. block: u32, // BlockNumber
  26. time: u64, // Moment
  27. },
  28. value
  29. )
  30. }
  31. get block(): BlockNumber {
  32. return this.get('block') as BlockNumber
  33. }
  34. get time(): Moment {
  35. return this.get('time') as Moment
  36. }
  37. static newEmpty(): BlockAndTime {
  38. return new BlockAndTime({} as BlockAndTime)
  39. }
  40. get momentDate(): moment.Moment {
  41. const YEAR_2000_MILLISECONDS = 946684801000
  42. // overflowing in ~270,000 years
  43. const timestamp = this.time.toNumber()
  44. // TODO: remove once https://github.com/Joystream/joystream/issues/705 is resolved
  45. // due to a bug, timestamp can be either in seconds or milliseconds
  46. let timestampInMillis = timestamp
  47. if (timestamp < YEAR_2000_MILLISECONDS) {
  48. // timestamp is in seconds
  49. timestampInMillis = timestamp * 1000
  50. }
  51. return moment(timestampInMillis)
  52. }
  53. }
  54. export function getTextPropAsString(struct: Struct, fieldName: string): string {
  55. return (struct.get(fieldName) as Text).toString()
  56. }
  57. export function getBoolPropAsBoolean(struct: Struct, fieldName: string): boolean {
  58. return (struct.get(fieldName) as bool).valueOf()
  59. }
  60. export function getOptionPropOrUndefined<T extends Codec>(struct: Struct, fieldName: string): T | undefined {
  61. return (struct.get(fieldName) as Option<T>).unwrapOr(undefined)
  62. }
  63. export class OptionText extends Option.with(Text) {
  64. static none(): OptionText {
  65. return new Option(Text, null)
  66. }
  67. static some(text: string): OptionText {
  68. return new Option(Text, text)
  69. }
  70. }
  71. export type InputValidationLengthConstraintType = {
  72. min: u16
  73. max_min_diff: u16
  74. }
  75. export class InputValidationLengthConstraint extends JoyStruct<InputValidationLengthConstraintType> {
  76. constructor(value: InputValidationLengthConstraintType) {
  77. super(
  78. {
  79. min: u16,
  80. max_min_diff: u16,
  81. },
  82. value
  83. )
  84. }
  85. static createWithMaxAllowed() {
  86. return new InputValidationLengthConstraint({
  87. min: new u16(1),
  88. max_min_diff: new u16(65534), // Max allowed without causing u16 overflow
  89. })
  90. }
  91. get min(): u16 {
  92. return this.getField('min')
  93. }
  94. get max_min_diff(): u16 {
  95. return this.getField('max_min_diff')
  96. }
  97. get max(): u16 {
  98. return new u16(this.min.add(this.max_min_diff))
  99. }
  100. }
  101. export const WorkingGroupDef = {
  102. Storage: Null,
  103. } as const
  104. export type WorkingGroupKey = keyof typeof WorkingGroupDef
  105. export class WorkingGroup extends JoyEnum(WorkingGroupDef) {}
  106. export function registerCommonTypes() {
  107. const typeRegistry = getTypeRegistry()
  108. typeRegistry.register({
  109. Credential,
  110. CredentialSet,
  111. BlockAndTime,
  112. ThreadId,
  113. PostId,
  114. InputValidationLengthConstraint,
  115. BTreeSet, // Is this even necessary?
  116. WorkingGroup,
  117. })
  118. }