Jelajahi Sumber

query node - council & referendum mappings VII

ondratra 3 tahun lalu
induk
melakukan
d08de54900

+ 18 - 42
query-node/mappings/council.ts

@@ -84,7 +84,7 @@ async function getCandidate(
     where.electionRound = electionRound
   }
 
-  const candidate = await store.get(Candidate, { where, order: { id: 'DESC' } })
+  const candidate = await store.get(Candidate, { where, order: { createdAt: 'DESC' } })
 
   if (!candidate) {
     throw new Error(`Candidate not found. memberId '${memberId}' electionRound '${electionRound?.id}'`)
@@ -99,7 +99,7 @@ async function getCandidate(
 async function getCouncilMember(store: DatabaseManager, memberId: string): Promise<CouncilMember> {
   const councilMember = await store.get(CouncilMember, {
     where: { memberId: memberId },
-    order: { id: 'DESC' },
+    order: { createdAt: 'DESC' },
   })
 
   if (!councilMember) {
@@ -113,41 +113,20 @@ async function getCouncilMember(store: DatabaseManager, memberId: string): Promi
   Returns the current election round record.
 */
 async function getCurrentElectionRound(store: DatabaseManager): Promise<ElectionRound> {
-  const electionRound = await store.get(ElectionRound, { order: { id: 'DESC' } })
+  const electionRound = await store.get(ElectionRound, { order: { cycleId: 'DESC' } })
 
-  if (electionRound) {
-    return electionRound
+  if (!electionRound) {
+    throw new Error(`No election round found`)
   }
 
-  // create first election round as it's not prepopulated (this shouldn't happen in any other sitaution)
-
-  const electedCouncil = new ElectedCouncil({
-    councilMembers: [],
-    updates: [],
-    electedAtBlock: 0,
-    councilElections: [],
-    nextCouncilElections: [],
-    isResigned: false,
-  })
-  await store.save<ElectedCouncil>(electedCouncil)
-
-  const initialElectionRound = new ElectionRound({
-    cycleId: 0,
-    isFinished: false,
-    castVotes: [],
-    electedCouncil,
-    candidates: [],
-  })
-  await store.save<ElectionRound>(initialElectionRound)
-
-  return initialElectionRound
+  return electionRound
 }
 
 /*
   Returns the last council stage update.
 */
 async function getCurrentStageUpdate(store: DatabaseManager): Promise<CouncilStageUpdate> {
-  const stageUpdate = await store.get(CouncilStageUpdate, { order: { id: 'DESC' } })
+  const stageUpdate = await store.get(CouncilStageUpdate, { order: { changedAt: 'DESC' } })
 
   if (!stageUpdate) {
     throw new Error('No stage update found.')
@@ -163,7 +142,7 @@ async function getCurrentElectedCouncil(
   store: DatabaseManager,
   canFail: boolean = false
 ): Promise<ElectedCouncil | undefined> {
-  const electedCouncil = await store.get(ElectedCouncil, { order: { id: 'DESC' } })
+  const electedCouncil = await store.get(ElectedCouncil, { order: { electedAtBlock: 'DESC' } })
 
   if (!electedCouncil && !canFail) {
     throw new Error('No council is elected.')
@@ -186,7 +165,7 @@ async function getAccountCastVote(
     where.electionRound = electionRound
   }
 
-  const castVote = await store.get(CastVote, { where, order: { id: 'DESC' } })
+  const castVote = await store.get(CastVote, { where, order: { createdAt: 'DESC' } })
 
   if (!castVote) {
     throw new Error(
@@ -212,14 +191,6 @@ function isCouncilStageAnnouncing(councilStage: typeof CouncilStage): councilSta
   return councilStage.isTypeOf == 'CouncilStageAnnouncing'
 }
 
-/*
-  Custom typeguard for council stage - election.
-* /
-function isCouncilStageElection(councilStage: typeof CouncilStage): councilStage is CouncilStageElection {
-  return councilStage.isTypeOf == 'CouncilStageElection'
-}
-*/
-
 /////////////////// Common /////////////////////////////////////////////////////
 
 /*
@@ -380,7 +351,7 @@ export async function council_VotingPeriodStarted({ event, store }: EventContext
 
   // add stage update record
   const stage = new CouncilStageElection()
-  stage.candidatesCount = new BN(0)
+  stage.candidatesCount = numOfCandidates
 
   await updateCouncilStage(store, stage, event.blockNumber)
 }
@@ -474,7 +445,6 @@ export async function council_NewCouncilElected({ event, store }: EventContext &
     councilMembers: await convertCandidatesToCouncilMembers(store, candidates, event.blockNumber),
     updates: [],
     electedAtBlock: event.blockNumber,
-    endedAtBlock: event.blockNumber,
     councilElections: oldElectedCouncil?.nextCouncilElections || [],
     nextCouncilElections: [],
     isResigned: false,
@@ -624,6 +594,7 @@ export async function council_RewardPayment({ event, store }: EventContext & Sto
   const councilMember = await getCouncilMember(store, memberId.toString())
   councilMember.accumulatedReward = councilMember.accumulatedReward.add(paidBalance)
   councilMember.unpaidReward = missingBalance
+  councilMember.lastPaymentBlock = new BN(event.blockNumber)
   await store.save<CouncilMember>(councilMember)
 }
 
@@ -667,10 +638,11 @@ export async function council_BudgetRefill({ event, store }: EventContext & Stor
 export async function council_BudgetRefillPlanned({ event, store }: EventContext & StoreContext): Promise<void> {
   // common event processing
 
-  const [] = new Council.BudgetRefillPlannedEvent(event).params
+  const [nextRefillInBlock] = new Council.BudgetRefillPlannedEvent(event).params
 
   const budgetRefillPlannedEvent = new BudgetRefillPlannedEvent({
     ...genericEventFields(event),
+    nextRefillInBlock: nextRefillInBlock.toNumber(),
   })
 
   await store.save<BudgetRefillPlannedEvent>(budgetRefillPlannedEvent)
@@ -809,7 +781,7 @@ export async function referendum_ReferendumFinished({ event, store }: EventConte
       (item, index) =>
         new ReferendumStageRevealingOptionResult({
           votePower: item.vote_power,
-          optionId: members[index],
+          option: members[index],
         })
     ),
   })
@@ -877,6 +849,10 @@ export async function referendum_VoteRevealed({ event, store }: EventContext & S
   const electionRound = await getCurrentElectionRound(store)
   const castVote = await getAccountCastVote(store, account.toString(), electionRound)
 
+  // update cast vote's voteFor info
+  castVote.voteFor = member
+  await store.save<CastVote>(castVote)
+
   const candidate = await getCandidate(store, memberId.toString(), electionRound)
   candidate.votePower = candidate.votePower.add(castVote.votePower)
   await store.save<Candidate>(candidate)

+ 34 - 5
query-node/mappings/genesis.ts

@@ -1,10 +1,19 @@
-import { StoreContext } from '@dzlzv/hydra-common'
+import { StoreContext, DatabaseManager } from '@dzlzv/hydra-common'
 import BN from 'bn.js'
-import { MembershipSystemSnapshot, WorkingGroup } from 'query-node/dist/model'
+import { MembershipSystemSnapshot, WorkingGroup, ElectedCouncil, ElectionRound } from 'query-node/dist/model'
 import { membershipSystem, workingGroups } from './genesis-data'
 
 export async function loadGenesisData({ store }: StoreContext): Promise<void> {
-  // Membership system
+  await initMembershipSystem(store)
+
+  await initWorkingGroups(store)
+
+  await initFirstElectionRound(store)
+
+  // TODO: members, workers
+}
+
+async function initMembershipSystem(store: DatabaseManager) {
   await store.save<MembershipSystemSnapshot>(
     new MembershipSystemSnapshot({
       createdAt: new Date(0),
@@ -15,8 +24,9 @@ export async function loadGenesisData({ store }: StoreContext): Promise<void> {
       invitedInitialBalance: new BN(membershipSystem.invitedInitialBalance),
     })
   )
+}
 
-  // Working groups
+async function initWorkingGroups(store: DatabaseManager) {
   await Promise.all(
     workingGroups.map(async (group) =>
       store.save<WorkingGroup>(
@@ -30,6 +40,25 @@ export async function loadGenesisData({ store }: StoreContext): Promise<void> {
       )
     )
   )
+}
 
-  // TODO: members, workers
+async function initFirstElectionRound(store: DatabaseManager) {
+  const electedCouncil = new ElectedCouncil({
+    councilMembers: [],
+    updates: [],
+    electedAtBlock: 0,
+    councilElections: [],
+    nextCouncilElections: [],
+    isResigned: false,
+  })
+  await store.save<ElectedCouncil>(electedCouncil)
+
+  const initialElectionRound = new ElectionRound({
+    cycleId: 0,
+    isFinished: false,
+    castVotes: [],
+    electedCouncil,
+    candidates: [],
+  })
+  await store.save<ElectionRound>(initialElectionRound)
 }

+ 1 - 1
query-node/schemas/council.graphql

@@ -164,7 +164,7 @@ type ReferendumStageRevealing @variant {
 
 type ReferendumStageRevealingOptionResult @entity {
   "Member that received votes."
-  optionId: Membership!
+  option: Membership!
 
   "Sum of votes' power received."
   votePower: BigInt!

+ 2 - 0
query-node/schemas/councilEvents.graphql

@@ -319,6 +319,8 @@ type BudgetRefillPlannedEvent implements Event @entity {
   indexInBlock: Int!
 
   ### SPECIFIC DATA ###
+
+  nextRefillInBlock: Int!
 }
 
 type BudgetIncrementUpdatedEvent implements Event @entity {

+ 1 - 1
query-node/schemas/membership.graphql

@@ -93,7 +93,7 @@ type Membership @entity {
   councilMembers: [CouncilMember!] @derivedFrom(field: "member")
 
   "Referendum results for the member."
-  referendumStageRevealingOptionResults: [ReferendumStageRevealingOptionResult!] @derivedFrom(field: "optionId")
+  referendumStageRevealingOptionResults: [ReferendumStageRevealingOptionResult!] @derivedFrom(field: "option")
 
   "Votes recieved in referendums by this member."
   votesRecieved: [CastVote!] @derivedFrom(field: "voteFor")