Browse Source

Map new contributions and BountyFunded events

Theophile Sandoz 3 years ago
parent
commit
d0092f9d3e
2 changed files with 41 additions and 2 deletions
  1. 2 0
      query-node/manifest.yml
  2. 39 2
      query-node/mappings/src/bounty.ts

+ 2 - 0
query-node/manifest.yml

@@ -863,6 +863,8 @@ mappings:
       handler: bounty_BountyCanceled
     - event: bounty.BountyVetoed
       handler: bounty_BountyVetoed
+    - event: bounty.BountyFunded
+      handler: bounty_BountyFunded
   extrinsicHandlers:
     # infer defaults here
     #- extrinsic: Balances.Transfer

+ 39 - 2
query-node/mappings/src/bounty.ts

@@ -1,12 +1,14 @@
 import { DatabaseManager, EventContext, StoreContext } from '@joystream/hydra-common'
 import { BountyMetadata } from '@joystream/metadata-protobuf'
-import { AssuranceContractType, BountyActor, FundingType } from '@joystream/types/augment'
+import { AssuranceContractType, BountyActor, BountyId, FundingType } from '@joystream/types/augment'
 import {
   Bounty,
   BountyCanceledEvent,
   BountyContractClosed,
   BountyContractOpen,
+  BountyContribution,
   BountyCreatedEvent,
+  BountyFundedEvent,
   BountyFundingLimited,
   BountyFundingPerpetual,
   BountyStage,
@@ -19,9 +21,17 @@ import { deserializeMetadata, genericEventFields } from './common'
 import { scheduleAtBlock } from './scheduler'
 
 /**
- * Commons helpers
+ * Common helpers
  */
 
+async function getBounty(store: DatabaseManager, bountyId: BountyId): Promise<Bounty> {
+  const bounty = await store.get(Bounty, { where: { id: bountyId } })
+  if (!bounty) {
+    throw new Error(`Bounty not found by id: ${bountyId}`)
+  }
+  return bounty
+}
+
 function bountyActorToMembership(actor: BountyActor): Membership | undefined {
   if (actor.isMember) {
     return new Membership({ id: String(actor.asMember) })
@@ -196,3 +206,30 @@ export async function bounty_BountyVetoed({ event, store }: EventContext & Store
   await store.save<BountyVetoedEvent>(vetoedEvent)
 }
 
+// Store new contributions and their BountyFunded events
+export async function bounty_BountyFunded({ event, store }: EventContext & StoreContext): Promise<void> {
+  const bountyFundedEvent = new BountyEvents.BountyFundedEvent(event)
+  const [bountyId, contributorActor, amount] = bountyFundedEvent.params
+  const eventTime = new Date(event.blockTimestamp)
+
+  const bounty = await getBounty(store, bountyId)
+
+  bounty.updatedAt = eventTime
+  bounty.totalFunding = bounty.totalFunding.add(amount)
+
+  await store.save<Bounty>(bounty)
+
+  const contribution = new BountyContribution({
+    createdAt: eventTime,
+    updatedAt: eventTime,
+    bounty,
+    contributor: bountyActorToMembership(contributorActor),
+    amount,
+  })
+
+  await store.save<BountyContribution>(contribution)
+
+  const fundedEvent = new BountyFundedEvent({ ...genericEventFields(event), contribution })
+
+  await store.save<BountyFundedEvent>(fundedEvent)
+}