Ver código fonte

Update entries status when the judgment period expires

Theophile Sandoz 3 anos atrás
pai
commit
3eb4e04d55
1 arquivos alterados com 48 adições e 30 exclusões
  1. 48 30
      query-node/mappings/src/bounty.ts

+ 48 - 30
query-node/mappings/src/bounty.ts

@@ -1,6 +1,13 @@
 import { DatabaseManager, EventContext, StoreContext, SubstrateEvent } from '@joystream/hydra-common'
 import { BountyMetadata, BountyWorkData } from '@joystream/metadata-protobuf'
-import { AssuranceContractType, BountyActor, BountyId, EntryId, FundingType } from '@joystream/types/augment'
+import {
+  AssuranceContractType,
+  BountyActor,
+  BountyId,
+  EntryId,
+  FundingType,
+  OracleWorkEntryJudgment,
+} from '@joystream/types/augment'
 import { MemberId } from '@joystream/types/common'
 import { BN } from '@polkadot/util'
 import {
@@ -153,11 +160,11 @@ export const bountyScheduleWorkSubmissionEnd = scheduleBountyStageEnd(BountyStag
   'entries',
 ])
 
-export const bountyScheduleJudgmentEnd = scheduleBountyStageEnd(BountyStage.Judgment, async (store, bounty) => {
-  bounty.updatedAt = new Date()
-  bounty.stage = BountyStage.Failed
-  await store.save<Bounty>(bounty)
-})
+export const bountyScheduleJudgmentEnd = scheduleBountyStageEnd(
+  BountyStage.Judgment,
+  (store, bounty) => endJudgmentPeriod(store, bounty, []),
+  ['entries']
+)
 
 function endFundingPeriod(
   store: DatabaseManager,
@@ -187,6 +194,38 @@ function endWorkingPeriod(store: DatabaseManager, bounty: Bounty, blockNumber: n
   return store.save<Bounty>(bounty)
 }
 
+type JudgmentEntries = [EntryId, OracleWorkEntryJudgment][]
+async function endJudgmentPeriod(
+  store: DatabaseManager,
+  bounty: Bounty,
+  entryJudgments: JudgmentEntries
+): Promise<void> {
+  // Update the bounty status
+  const hasWinners = entryJudgments.some(([, judgment]) => judgment.isWinner)
+  bounty.updatedAt = new Date()
+  bounty.stage = BountyStage[hasWinners ? 'Successful' : 'Failed']
+  await store.save<Bounty>(bounty)
+
+  // Update winner entries status
+  await Promise.all(
+    bounty.entries?.map((entry) => {
+      const judgment = entryJudgments.find(([entryId]) => String(entryId) === entry.id)?.[1]
+
+      if (!judgment) {
+        entry.status = new BountyEntryStatusPassed()
+      } else if (judgment?.isWinner) {
+        const status = new BountyEntryStatusWinner()
+        status.reward = asBN(judgment.asWinner.reward)
+        entry.status = status
+      } else {
+        entry.status = new BountyEntryStatusRejected()
+      }
+
+      return store.save<BountyEntry>(entry)
+    }) ?? []
+  )
+}
+
 /**
  * Event handlers
  */
@@ -448,32 +487,11 @@ export async function bounty_OracleJudgmentSubmitted({ event, store }: EventCont
   const judgmentSubmittedEvent = new BountyEvents.OracleJudgmentSubmittedEvent(event)
   const [bountyId, , bountyJudgment, rationale] = judgmentSubmittedEvent.params
 
+  const bounty = await getBounty(store, bountyId, ['entries'])
   const entryJudgments = Array.from(bountyJudgment.entries())
 
-  // Update the bounty status
-  const hasWinners = entryJudgments.some(([, judgment]) => judgment.isWinner)
-  const bounty = await updateBounty(store, event, bountyId, ['entries'], () => ({
-    stage: BountyStage[hasWinners ? 'Successful' : 'Failed'],
-  }))
-
-  // Update winner entries status
-  await Promise.all(
-    bounty.entries?.map((entry) => {
-      const judgment = entryJudgments.find(([entryId]) => String(entryId) === entry.id)?.[1]
-
-      if (!judgment) {
-        entry.status = new BountyEntryStatusPassed()
-      } else if (judgment?.isWinner) {
-        const status = new BountyEntryStatusWinner()
-        status.reward = asBN(judgment.asWinner.reward)
-        entry.status = status
-      } else {
-        entry.status = new BountyEntryStatusRejected()
-      }
-
-      return store.save<BountyEntry>(entry)
-    }) ?? []
-  )
+  // Update the bounty status and winner entries status
+  endJudgmentPeriod(store, bounty, entryJudgments)
 
   // Record the event
   const judgmentEvent = new OracleJudgmentSubmittedEvent({