common.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. export { JoyStruct } from './JoyStruct';
  8. import { JoyEnum } from './JoyEnum';
  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. block: u32, // BlockNumber
  25. time: u64, // Moment
  26. }, value);
  27. }
  28. get block (): BlockNumber {
  29. return this.get('block') as BlockNumber;
  30. }
  31. get time (): Moment {
  32. return this.get('time') as Moment;
  33. }
  34. static newEmpty (): BlockAndTime {
  35. return new BlockAndTime({} as BlockAndTime);
  36. }
  37. get momentDate (): moment.Moment {
  38. const YEAR_2000_MILLISECONDS = 946684801000;
  39. // overflowing in ~270,000 years
  40. const timestamp = this.time.toNumber();
  41. // TODO: remove once https://github.com/Joystream/joystream/issues/705 is resolved
  42. // due to a bug, timestamp can be either in seconds or milliseconds
  43. let timestampInMillis = timestamp;
  44. if (timestamp < YEAR_2000_MILLISECONDS) {
  45. // timestamp is in seconds
  46. timestampInMillis = timestamp * 1000;
  47. }
  48. return moment(timestampInMillis);
  49. }
  50. }
  51. export function getTextPropAsString(struct: Struct, fieldName: string): string {
  52. return (struct.get(fieldName) as Text).toString();
  53. }
  54. export function getBoolPropAsBoolean(struct: Struct, fieldName: string): boolean {
  55. return (struct.get(fieldName) as bool).valueOf();
  56. }
  57. export function getOptionPropOrUndefined<T extends Codec>(struct: Struct, fieldName: string): T | undefined {
  58. return (struct.get(fieldName) as Option<T>).unwrapOr(undefined);
  59. }
  60. export class OptionText extends Option.with(Text) {
  61. static none(): OptionText {
  62. return new Option(Text, null);
  63. }
  64. static some(text: string): OptionText {
  65. return new Option(Text, text);
  66. }
  67. }
  68. export type InputValidationLengthConstraintType = {
  69. min: u16,
  70. max_min_diff: u16
  71. };
  72. export class InputValidationLengthConstraint extends JoyStruct<InputValidationLengthConstraintType> {
  73. constructor (value: InputValidationLengthConstraintType) {
  74. super({
  75. min: u16,
  76. max_min_diff: u16
  77. }, value);
  78. }
  79. get min (): u16 {
  80. return this.getField('min');
  81. }
  82. get max_min_diff (): u16 {
  83. return this.getField('max_min_diff');
  84. }
  85. get max (): u16 {
  86. return new u16(this.min.add(this.max_min_diff));
  87. }
  88. }
  89. export const WorkingGroupDef = {
  90. Storage: Null
  91. } as const;
  92. export type WorkingGroupKeys = keyof typeof WorkingGroupDef;
  93. export class WorkingGroup extends JoyEnum(WorkingGroupDef) { };
  94. export function registerCommonTypes() {
  95. const typeRegistry = getTypeRegistry();
  96. typeRegistry.register({
  97. Credential,
  98. CredentialSet,
  99. BlockAndTime,
  100. ThreadId,
  101. PostId,
  102. InputValidationLengthConstraint,
  103. BTreeSet, // Is this even necessary?
  104. WorkingGroup
  105. });
  106. }