curatorGroup.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { fixBlockTimestamp } from '../eventFix'
  2. import { SubstrateEvent } from '@dzlzv/hydra-common'
  3. import { DatabaseManager } from '@dzlzv/hydra-db-utils'
  4. import { FindConditions } from 'typeorm'
  5. import { CuratorGroup } from 'query-node'
  6. import { Content } from '../../../generated/types'
  7. import { inconsistentState, logger } from '../common'
  8. export async function content_CuratorGroupCreated(db: DatabaseManager, event: SubstrateEvent) {
  9. // read event data
  10. const { curatorGroupId } = new Content.CuratorGroupCreatedEvent(event).data
  11. // create new curator group
  12. const curatorGroup = new CuratorGroup({
  13. // main data
  14. id: curatorGroupId.toString(),
  15. curatorIds: [],
  16. isActive: false, // runtime creates inactive curator groups by default
  17. // fill in auto-generated fields
  18. createdAt: new Date(fixBlockTimestamp(event.blockTimestamp).toNumber()),
  19. updatedAt: new Date(fixBlockTimestamp(event.blockTimestamp).toNumber()),
  20. })
  21. // save curator group
  22. await db.save<CuratorGroup>(curatorGroup)
  23. // emit log event
  24. logger.info('Curator group has been created', { id: curatorGroupId })
  25. }
  26. export async function content_CuratorGroupStatusSet(db: DatabaseManager, event: SubstrateEvent) {
  27. // read event data
  28. const { curatorGroupId, bool: isActive } = new Content.CuratorGroupStatusSetEvent(event).data
  29. // load curator group
  30. const curatorGroup = await db.get(CuratorGroup, {
  31. where: { id: curatorGroupId.toString() } as FindConditions<CuratorGroup>,
  32. })
  33. // ensure curator group exists
  34. if (!curatorGroup) {
  35. return inconsistentState('Non-existing curator group status set requested', curatorGroupId)
  36. }
  37. // update curator group
  38. curatorGroup.isActive = isActive.isTrue
  39. // set last update time
  40. curatorGroup.updatedAt = new Date(fixBlockTimestamp(event.blockTimestamp).toNumber())
  41. // save curator group
  42. await db.save<CuratorGroup>(curatorGroup)
  43. // emit log event
  44. logger.info('Curator group status has been set', { id: curatorGroupId, isActive })
  45. }
  46. export async function content_CuratorAdded(db: DatabaseManager, event: SubstrateEvent) {
  47. // read event data
  48. const { curatorGroupId, curatorId } = new Content.CuratorAddedEvent(event).data
  49. // load curator group
  50. const curatorGroup = await db.get(CuratorGroup, {
  51. where: { id: curatorGroupId.toString() } as FindConditions<CuratorGroup>,
  52. })
  53. // ensure curator group exists
  54. if (!curatorGroup) {
  55. return inconsistentState('Curator add to non-existing curator group requested', curatorGroupId)
  56. }
  57. // update curator group
  58. curatorGroup.curatorIds.push(curatorId.toNumber())
  59. // set last update time
  60. curatorGroup.updatedAt = new Date(fixBlockTimestamp(event.blockTimestamp).toNumber())
  61. // save curator group
  62. await db.save<CuratorGroup>(curatorGroup)
  63. // emit log event
  64. logger.info('Curator has been added to curator group', { id: curatorGroupId, curatorId })
  65. }
  66. export async function content_CuratorRemoved(db: DatabaseManager, event: SubstrateEvent) {
  67. // read event data
  68. const { curatorGroupId, curatorId } = new Content.CuratorAddedEvent(event).data
  69. // load curator group
  70. const curatorGroup = await db.get(CuratorGroup, {
  71. where: { id: curatorGroupId.toString() } as FindConditions<CuratorGroup>,
  72. })
  73. // ensure curator group exists
  74. if (!curatorGroup) {
  75. return inconsistentState('Non-existing curator group removal requested', curatorGroupId)
  76. }
  77. const curatorIndex = curatorGroup.curatorIds.indexOf(curatorId.toNumber())
  78. // ensure curator group exists
  79. if (curatorIndex < 0) {
  80. return inconsistentState('Non-associated curator removal from curator group requested', curatorId)
  81. }
  82. // update curator group
  83. curatorGroup.curatorIds.splice(curatorIndex, 1)
  84. // save curator group
  85. await db.save<CuratorGroup>(curatorGroup)
  86. // emit log event
  87. logger.info('Curator has been removed from curator group', { id: curatorGroupId, curatorId })
  88. }