proposals.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /*
  2. eslint-disable @typescript-eslint/naming-convention
  3. */
  4. import { SubstrateEvent, DatabaseManager, EventContext, StoreContext } from '@joystream/hydra-common'
  5. import { ProposalDetails as RuntimeProposalDetails } from '@joystream/types/augment/all'
  6. import BN from 'bn.js'
  7. import {
  8. Proposal,
  9. SignalProposalDetails,
  10. RuntimeUpgradeProposalDetails,
  11. FundingRequestProposalDetails,
  12. SetMaxValidatorCountProposalDetails,
  13. CreateWorkingGroupLeadOpeningProposalDetails,
  14. FillWorkingGroupLeadOpeningProposalDetails,
  15. UpdateWorkingGroupBudgetProposalDetails,
  16. DecreaseWorkingGroupLeadStakeProposalDetails,
  17. SlashWorkingGroupLeadProposalDetails,
  18. SetWorkingGroupLeadRewardProposalDetails,
  19. TerminateWorkingGroupLeadProposalDetails,
  20. AmendConstitutionProposalDetails,
  21. CancelWorkingGroupLeadOpeningProposalDetails,
  22. SetMembershipPriceProposalDetails,
  23. SetCouncilBudgetIncrementProposalDetails,
  24. SetCouncilorRewardProposalDetails,
  25. SetInitialInvitationBalanceProposalDetails,
  26. SetInitialInvitationCountProposalDetails,
  27. SetMembershipLeadInvitationQuotaProposalDetails,
  28. SetReferralCutProposalDetails,
  29. CreateBlogPostProposalDetails,
  30. EditBlogPostProposalDetails,
  31. LockBlogPostProposalDetails,
  32. UnlockBlogPostProposalDetails,
  33. VetoProposalDetails,
  34. ProposalDetails,
  35. FundingRequestDestinationsList,
  36. FundingRequestDestination,
  37. Membership,
  38. ProposalStatusDeciding,
  39. ProposalIntermediateStatus,
  40. ProposalStatusDormant,
  41. ProposalStatusGracing,
  42. ProposalStatusUpdatedEvent,
  43. ProposalDecisionStatus,
  44. ProposalStatusCancelled,
  45. ProposalStatusExpired,
  46. ProposalStatusRejected,
  47. ProposalStatusSlashed,
  48. ProposalStatusVetoed,
  49. ProposalDecisionMadeEvent,
  50. ProposalStatusCanceledByRuntime,
  51. ProposalStatusExecuted,
  52. ProposalStatusExecutionFailed,
  53. ProposalExecutionStatus,
  54. ProposalExecutedEvent,
  55. ProposalVotedEvent,
  56. ProposalVoteKind,
  57. ProposalCancelledEvent,
  58. ProposalCreatedEvent,
  59. RuntimeWasmBytecode,
  60. ProposalDiscussionThread,
  61. ProposalDiscussionThreadModeOpen,
  62. } from 'query-node/dist/model'
  63. import { bytesToString, genericEventFields, getWorkingGroupModuleName, MemoryCache, perpareString } from './common'
  64. import { ProposalsEngine, ProposalsCodex } from './generated/types'
  65. import { createWorkingGroupOpeningMetadata } from './workingGroups'
  66. import { blake2AsHex } from '@polkadot/util-crypto'
  67. import { Bytes } from '@polkadot/types'
  68. async function getProposal(store: DatabaseManager, id: string) {
  69. const proposal = await store.get(Proposal, { where: { id } })
  70. if (!proposal) {
  71. throw new Error(`Proposal not found by id: ${id}`)
  72. }
  73. return proposal
  74. }
  75. async function getOrCreateRuntimeWasmBytecode(store: DatabaseManager, bytecode: Bytes) {
  76. const bytecodeHash = blake2AsHex(bytecode.toU8a(true))
  77. let wasmBytecode = await store.get(RuntimeWasmBytecode, { where: { id: bytecodeHash } })
  78. if (!wasmBytecode) {
  79. wasmBytecode = new RuntimeWasmBytecode({
  80. id: bytecodeHash,
  81. bytecode: Buffer.from(bytecode.toU8a(true)),
  82. })
  83. await store.save<RuntimeWasmBytecode>(wasmBytecode)
  84. }
  85. return wasmBytecode
  86. }
  87. async function parseProposalDetails(
  88. event: SubstrateEvent,
  89. store: DatabaseManager,
  90. proposalDetails: RuntimeProposalDetails
  91. ): Promise<typeof ProposalDetails> {
  92. const eventTime = new Date(event.blockTimestamp)
  93. // SignalProposalDetails:
  94. if (proposalDetails.isSignal) {
  95. const details = new SignalProposalDetails()
  96. const specificDetails = proposalDetails.asSignal
  97. details.text = perpareString(specificDetails.toString())
  98. return details
  99. }
  100. // RuntimeUpgradeProposalDetails:
  101. else if (proposalDetails.isRuntimeUpgrade) {
  102. const details = new RuntimeUpgradeProposalDetails()
  103. const runtimeBytecode = proposalDetails.asRuntimeUpgrade
  104. details.newRuntimeBytecodeId = (await getOrCreateRuntimeWasmBytecode(store, runtimeBytecode)).id
  105. return details
  106. }
  107. // FundingRequestProposalDetails:
  108. else if (proposalDetails.isFundingRequest) {
  109. const destinationsList = new FundingRequestDestinationsList()
  110. const specificDetails = proposalDetails.asFundingRequest
  111. await store.save<FundingRequestDestinationsList>(destinationsList)
  112. await Promise.all(
  113. specificDetails.map(({ account, amount }) =>
  114. store.save(
  115. new FundingRequestDestination({
  116. createdAt: eventTime,
  117. updatedAt: eventTime,
  118. account: account.toString(),
  119. amount: new BN(amount.toString()),
  120. list: destinationsList,
  121. })
  122. )
  123. )
  124. )
  125. const details = new FundingRequestProposalDetails()
  126. details.destinationsListId = destinationsList.id
  127. return details
  128. }
  129. // SetMaxValidatorCountProposalDetails:
  130. else if (proposalDetails.isSetMaxValidatorCount) {
  131. const details = new SetMaxValidatorCountProposalDetails()
  132. const specificDetails = proposalDetails.asSetMaxValidatorCount
  133. details.newMaxValidatorCount = specificDetails.toNumber()
  134. return details
  135. }
  136. // CreateWorkingGroupLeadOpeningProposalDetails:
  137. else if (proposalDetails.isCreateWorkingGroupLeadOpening) {
  138. const details = new CreateWorkingGroupLeadOpeningProposalDetails()
  139. const specificDetails = proposalDetails.asCreateWorkingGroupLeadOpening
  140. const metadata = await createWorkingGroupOpeningMetadata(store, eventTime, specificDetails.description)
  141. details.groupId = getWorkingGroupModuleName(specificDetails.working_group)
  142. details.metadataId = metadata.id
  143. details.rewardPerBlock = new BN(specificDetails.reward_per_block.unwrapOr(0).toString())
  144. details.stakeAmount = new BN(specificDetails.stake_policy.stake_amount.toString())
  145. details.unstakingPeriod = specificDetails.stake_policy.leaving_unstaking_period.toNumber()
  146. return details
  147. }
  148. // FillWorkingGroupLeadOpeningProposalDetails:
  149. else if (proposalDetails.isFillWorkingGroupLeadOpening) {
  150. const details = new FillWorkingGroupLeadOpeningProposalDetails()
  151. const specificDetails = proposalDetails.asFillWorkingGroupLeadOpening
  152. const groupModuleName = getWorkingGroupModuleName(specificDetails.working_group)
  153. details.openingId = `${groupModuleName}-${specificDetails.opening_id.toString()}`
  154. details.applicationId = `${groupModuleName}-${specificDetails.successful_application_id.toString()}`
  155. return details
  156. }
  157. // UpdateWorkingGroupBudgetProposalDetails:
  158. else if (proposalDetails.isUpdateWorkingGroupBudget) {
  159. const details = new UpdateWorkingGroupBudgetProposalDetails()
  160. const specificDetails = proposalDetails.asUpdateWorkingGroupBudget
  161. const [amount, workingGroup, balanceKind] = specificDetails
  162. details.groupId = getWorkingGroupModuleName(workingGroup)
  163. details.amount = amount.muln(balanceKind.isNegative ? -1 : 1)
  164. return details
  165. }
  166. // DecreaseWorkingGroupLeadStakeProposalDetails:
  167. else if (proposalDetails.isDecreaseWorkingGroupLeadStake) {
  168. const details = new DecreaseWorkingGroupLeadStakeProposalDetails()
  169. const specificDetails = proposalDetails.asDecreaseWorkingGroupLeadStake
  170. const [workerId, amount, workingGroup] = specificDetails
  171. details.amount = new BN(amount.toString())
  172. details.leadId = `${getWorkingGroupModuleName(workingGroup)}-${workerId.toString()}`
  173. return details
  174. }
  175. // SlashWorkingGroupLeadProposalDetails:
  176. else if (proposalDetails.isSlashWorkingGroupLead) {
  177. const details = new SlashWorkingGroupLeadProposalDetails()
  178. const specificDetails = proposalDetails.asSlashWorkingGroupLead
  179. const [workerId, amount, workingGroup] = specificDetails
  180. details.amount = new BN(amount.toString())
  181. details.leadId = `${getWorkingGroupModuleName(workingGroup)}-${workerId.toString()}`
  182. return details
  183. }
  184. // SetWorkingGroupLeadRewardProposalDetails:
  185. else if (proposalDetails.isSetWorkingGroupLeadReward) {
  186. const details = new SetWorkingGroupLeadRewardProposalDetails()
  187. const specificDetails = proposalDetails.asSetWorkingGroupLeadReward
  188. const [workerId, reward, workingGroup] = specificDetails
  189. details.newRewardPerBlock = new BN(reward.unwrapOr(0).toString())
  190. details.leadId = `${getWorkingGroupModuleName(workingGroup)}-${workerId.toString()}`
  191. return details
  192. }
  193. // TerminateWorkingGroupLeadProposalDetails:
  194. else if (proposalDetails.isTerminateWorkingGroupLead) {
  195. const details = new TerminateWorkingGroupLeadProposalDetails()
  196. const specificDetails = proposalDetails.asTerminateWorkingGroupLead
  197. details.leadId = `${getWorkingGroupModuleName(
  198. specificDetails.working_group
  199. )}-${specificDetails.worker_id.toString()}`
  200. details.slashingAmount = specificDetails.slashing_amount.isSome
  201. ? new BN(specificDetails.slashing_amount.unwrap().toString())
  202. : undefined
  203. return details
  204. }
  205. // AmendConstitutionProposalDetails:
  206. else if (proposalDetails.isAmendConstitution) {
  207. const details = new AmendConstitutionProposalDetails()
  208. const specificDetails = proposalDetails.asAmendConstitution
  209. details.text = perpareString(specificDetails.toString())
  210. return details
  211. }
  212. // CancelWorkingGroupLeadOpeningProposalDetails:
  213. else if (proposalDetails.isCancelWorkingGroupLeadOpening) {
  214. const details = new CancelWorkingGroupLeadOpeningProposalDetails()
  215. const [openingId, workingGroup] = proposalDetails.asCancelWorkingGroupLeadOpening
  216. details.openingId = `${getWorkingGroupModuleName(workingGroup)}-${openingId.toString()}`
  217. return details
  218. }
  219. // SetCouncilBudgetIncrementProposalDetails:
  220. else if (proposalDetails.isSetCouncilBudgetIncrement) {
  221. const details = new SetCouncilBudgetIncrementProposalDetails()
  222. const specificDetails = proposalDetails.asSetCouncilBudgetIncrement
  223. console.log('SetCouncilBudgetIncrement specificDetails.toString():', new BN(specificDetails.toString()).toString())
  224. details.newAmount = new BN(specificDetails.toString())
  225. return details
  226. }
  227. // SetMembershipPriceProposalDetails:
  228. else if (proposalDetails.isSetMembershipPrice) {
  229. const details = new SetMembershipPriceProposalDetails()
  230. const specificDetails = proposalDetails.asSetMembershipPrice
  231. details.newPrice = new BN(specificDetails.toString())
  232. return details
  233. }
  234. // SetCouncilorRewardProposalDetails:
  235. else if (proposalDetails.isSetCouncilorReward) {
  236. const details = new SetCouncilorRewardProposalDetails()
  237. const specificDetails = proposalDetails.asSetCouncilorReward
  238. details.newRewardPerBlock = new BN(specificDetails.toString())
  239. return details
  240. }
  241. // SetInitialInvitationBalanceProposalDetails:
  242. else if (proposalDetails.isSetInitialInvitationBalance) {
  243. const details = new SetInitialInvitationBalanceProposalDetails()
  244. const specificDetails = proposalDetails.asSetInitialInvitationBalance
  245. details.newInitialInvitationBalance = new BN(specificDetails.toString())
  246. return details
  247. }
  248. // SetInitialInvitationCountProposalDetails:
  249. else if (proposalDetails.isSetInitialInvitationCount) {
  250. const details = new SetInitialInvitationCountProposalDetails()
  251. const specificDetails = proposalDetails.asSetInitialInvitationCount
  252. details.newInitialInvitationsCount = specificDetails.toNumber()
  253. return details
  254. }
  255. // SetMembershipLeadInvitationQuotaProposalDetails:
  256. else if (proposalDetails.isSetMembershipLeadInvitationQuota) {
  257. const details = new SetMembershipLeadInvitationQuotaProposalDetails()
  258. const specificDetails = proposalDetails.asSetMembershipLeadInvitationQuota
  259. details.newLeadInvitationQuota = specificDetails.toNumber()
  260. return details
  261. }
  262. // SetReferralCutProposalDetails:
  263. else if (proposalDetails.isSetReferralCut) {
  264. const details = new SetReferralCutProposalDetails()
  265. const specificDetails = proposalDetails.asSetReferralCut
  266. details.newReferralCut = specificDetails.toNumber()
  267. return details
  268. }
  269. // CreateBlogPostProposalDetails:
  270. else if (proposalDetails.isCreateBlogPost) {
  271. const details = new CreateBlogPostProposalDetails()
  272. const specificDetails = proposalDetails.asCreateBlogPost
  273. const [title, body] = specificDetails
  274. details.title = perpareString(title.toString())
  275. details.body = perpareString(body.toString())
  276. return details
  277. }
  278. // EditBlogPostProposalDetails:
  279. else if (proposalDetails.isEditBlogPost) {
  280. const details = new EditBlogPostProposalDetails()
  281. const specificDetails = proposalDetails.asEditBlogPost
  282. const [postId, optTitle, optBody] = specificDetails
  283. details.blogPost = postId.toString()
  284. details.newTitle = optTitle.isSome ? perpareString(optTitle.unwrap().toString()) : undefined
  285. details.newBody = optBody.isSome ? perpareString(optBody.unwrap().toString()) : undefined
  286. return details
  287. }
  288. // LockBlogPostProposalDetails:
  289. else if (proposalDetails.isLockBlogPost) {
  290. const details = new LockBlogPostProposalDetails()
  291. const postId = proposalDetails.asLockBlogPost
  292. details.blogPost = postId.toString()
  293. return details
  294. }
  295. // UnlockBlogPostProposalDetails:
  296. else if (proposalDetails.isUnlockBlogPost) {
  297. const details = new UnlockBlogPostProposalDetails()
  298. const postId = proposalDetails.asUnlockBlogPost
  299. details.blogPost = postId.toString()
  300. return details
  301. }
  302. // VetoProposalDetails:
  303. else if (proposalDetails.isVetoProposal) {
  304. const details = new VetoProposalDetails()
  305. const specificDetails = proposalDetails.asVetoProposal
  306. details.proposalId = specificDetails.toString()
  307. return details
  308. } else {
  309. throw new Error(`Unspported proposal details type: ${proposalDetails.type}`)
  310. }
  311. }
  312. export async function proposalsCodex_ProposalCreated({ store, event }: EventContext & StoreContext): Promise<void> {
  313. const [proposalId, generalProposalParameters, runtimeProposalDetails] = new ProposalsCodex.ProposalCreatedEvent(
  314. event
  315. ).params
  316. const eventTime = new Date(event.blockTimestamp)
  317. const proposalDetails = await parseProposalDetails(event, store, runtimeProposalDetails)
  318. if (!MemoryCache.lastCreatedProposalThreadId) {
  319. throw new Error('Unexpected state: MemoryCache.lastCreatedProposalThreadId is empty')
  320. }
  321. const proposal = new Proposal({
  322. id: proposalId.toString(),
  323. createdAt: eventTime,
  324. updatedAt: eventTime,
  325. details: proposalDetails,
  326. councilApprovals: 0,
  327. creator: new Membership({ id: generalProposalParameters.member_id.toString() }),
  328. title: perpareString(generalProposalParameters.title.toString()),
  329. description: perpareString(generalProposalParameters.description.toString()),
  330. exactExecutionBlock: generalProposalParameters.exact_execution_block.unwrapOr(undefined)?.toNumber(),
  331. stakingAccount: generalProposalParameters.staking_account_id.toString(),
  332. status: new ProposalStatusDeciding(),
  333. isFinalized: false,
  334. statusSetAtBlock: event.blockNumber,
  335. statusSetAtTime: eventTime,
  336. })
  337. await store.save<Proposal>(proposal)
  338. // Thread is always created along with the proposal
  339. const proposalThread = new ProposalDiscussionThread({
  340. id: MemoryCache.lastCreatedProposalThreadId.toString(),
  341. createdAt: eventTime,
  342. updatedAt: eventTime,
  343. mode: new ProposalDiscussionThreadModeOpen(),
  344. proposal,
  345. })
  346. await store.save<ProposalDiscussionThread>(proposalThread)
  347. const proposalCreatedEvent = new ProposalCreatedEvent({
  348. ...genericEventFields(event),
  349. proposal: proposal,
  350. })
  351. await store.save<ProposalCreatedEvent>(proposalCreatedEvent)
  352. }
  353. export async function proposalsEngine_ProposalStatusUpdated({
  354. store,
  355. event,
  356. }: EventContext & StoreContext): Promise<void> {
  357. const [proposalId, status] = new ProposalsEngine.ProposalStatusUpdatedEvent(event).params
  358. const proposal = await getProposal(store, proposalId.toString())
  359. const eventTime = new Date(event.blockTimestamp)
  360. let newStatus: typeof ProposalIntermediateStatus
  361. if (status.isActive) {
  362. newStatus = new ProposalStatusDeciding()
  363. } else if (status.isPendingConstitutionality) {
  364. newStatus = new ProposalStatusDormant()
  365. ++proposal.councilApprovals
  366. } else if (status.isPendingExecution) {
  367. newStatus = new ProposalStatusGracing()
  368. ++proposal.councilApprovals
  369. } else {
  370. throw new Error(`Unexpected proposal status: ${status.type}`)
  371. }
  372. const proposalStatusUpdatedEvent = new ProposalStatusUpdatedEvent({
  373. ...genericEventFields(event),
  374. newStatus,
  375. proposal,
  376. })
  377. await store.save<ProposalStatusUpdatedEvent>(proposalStatusUpdatedEvent)
  378. newStatus.proposalStatusUpdatedEventId = proposalStatusUpdatedEvent.id
  379. proposal.updatedAt = eventTime
  380. proposal.status = newStatus
  381. proposal.statusSetAtBlock = event.blockNumber
  382. proposal.statusSetAtTime = eventTime
  383. await store.save<Proposal>(proposal)
  384. }
  385. export async function proposalsEngine_ProposalDecisionMade({
  386. store,
  387. event,
  388. }: EventContext & StoreContext): Promise<void> {
  389. const [proposalId, decision] = new ProposalsEngine.ProposalDecisionMadeEvent(event).params
  390. const proposal = await getProposal(store, proposalId.toString())
  391. const eventTime = new Date(event.blockTimestamp)
  392. let decisionStatus: typeof ProposalDecisionStatus
  393. if (decision.isApproved) {
  394. if (decision.asApproved.isPendingConstitutionality) {
  395. decisionStatus = new ProposalStatusDormant()
  396. } else {
  397. decisionStatus = new ProposalStatusGracing()
  398. }
  399. } else if (decision.isCanceled) {
  400. decisionStatus = new ProposalStatusCancelled()
  401. } else if (decision.isCanceledByRuntime) {
  402. decisionStatus = new ProposalStatusCanceledByRuntime()
  403. } else if (decision.isExpired) {
  404. decisionStatus = new ProposalStatusExpired()
  405. } else if (decision.isRejected) {
  406. decisionStatus = new ProposalStatusRejected()
  407. } else if (decision.isSlashed) {
  408. decisionStatus = new ProposalStatusSlashed()
  409. } else if (decision.isVetoed) {
  410. decisionStatus = new ProposalStatusVetoed()
  411. } else {
  412. throw new Error(`Unexpected proposal decision: ${decision.type}`)
  413. }
  414. const proposalDecisionMadeEvent = new ProposalDecisionMadeEvent({
  415. ...genericEventFields(event),
  416. decisionStatus,
  417. proposal,
  418. })
  419. await store.save<ProposalDecisionMadeEvent>(proposalDecisionMadeEvent)
  420. // We don't handle Cancelled, Dormant and Gracing statuses here, since they emit separate events
  421. if (
  422. [
  423. 'ProposalStatusCanceledByRuntime',
  424. 'ProposalStatusExpired',
  425. 'ProposalStatusRejected',
  426. 'ProposalStatusSlashed',
  427. 'ProposalStatusVetoed',
  428. ].includes(decisionStatus.isTypeOf)
  429. ) {
  430. ;(decisionStatus as
  431. | ProposalStatusCanceledByRuntime
  432. | ProposalStatusExpired
  433. | ProposalStatusRejected
  434. | ProposalStatusSlashed
  435. | ProposalStatusVetoed).proposalDecisionMadeEventId = proposalDecisionMadeEvent.id
  436. proposal.status = decisionStatus
  437. proposal.isFinalized = true
  438. proposal.statusSetAtBlock = event.blockNumber
  439. proposal.statusSetAtTime = eventTime
  440. proposal.updatedAt = eventTime
  441. await store.save<Proposal>(proposal)
  442. }
  443. }
  444. export async function proposalsEngine_ProposalExecuted({ store, event }: EventContext & StoreContext): Promise<void> {
  445. const [proposalId, executionStatus] = new ProposalsEngine.ProposalExecutedEvent(event).params
  446. const proposal = await getProposal(store, proposalId.toString())
  447. const eventTime = new Date(event.blockTimestamp)
  448. let newStatus: typeof ProposalExecutionStatus
  449. if (executionStatus.isExecuted) {
  450. newStatus = new ProposalStatusExecuted()
  451. } else if (executionStatus.isExecutionFailed) {
  452. const status = new ProposalStatusExecutionFailed()
  453. status.errorMessage = executionStatus.asExecutionFailed.error.toString()
  454. newStatus = status
  455. } else {
  456. throw new Error(`Unexpected proposal execution status: ${executionStatus.type}`)
  457. }
  458. const proposalExecutedEvent = new ProposalExecutedEvent({
  459. ...genericEventFields(event),
  460. executionStatus: newStatus,
  461. proposal,
  462. })
  463. await store.save<ProposalExecutedEvent>(proposalExecutedEvent)
  464. newStatus.proposalExecutedEventId = proposalExecutedEvent.id
  465. proposal.status = newStatus
  466. proposal.isFinalized = true
  467. proposal.statusSetAtBlock = event.blockNumber
  468. proposal.statusSetAtTime = eventTime
  469. proposal.updatedAt = eventTime
  470. await store.save<Proposal>(proposal)
  471. }
  472. export async function proposalsEngine_Voted({ store, event }: EventContext & StoreContext): Promise<void> {
  473. const [memberId, proposalId, voteKind, rationaleBytes] = new ProposalsEngine.VotedEvent(event).params
  474. const proposal = await getProposal(store, proposalId.toString())
  475. let vote: ProposalVoteKind
  476. if (voteKind.isApprove) {
  477. vote = ProposalVoteKind.APPROVE
  478. } else if (voteKind.isReject) {
  479. vote = ProposalVoteKind.REJECT
  480. } else if (voteKind.isSlash) {
  481. vote = ProposalVoteKind.SLASH
  482. } else if (voteKind.isAbstain) {
  483. vote = ProposalVoteKind.ABSTAIN
  484. } else {
  485. throw new Error(`Unexpected vote kind: ${voteKind.type}`)
  486. }
  487. const votedEvent = new ProposalVotedEvent({
  488. ...genericEventFields(event),
  489. proposal,
  490. voteKind: vote,
  491. voter: new Membership({ id: memberId.toString() }),
  492. votingRound: proposal.councilApprovals + 1,
  493. rationale: bytesToString(rationaleBytes),
  494. })
  495. await store.save<ProposalVotedEvent>(votedEvent)
  496. }
  497. export async function proposalsEngine_ProposalCancelled({ store, event }: EventContext & StoreContext): Promise<void> {
  498. const [, proposalId] = new ProposalsEngine.ProposalCancelledEvent(event).params
  499. const proposal = await getProposal(store, proposalId.toString())
  500. const eventTime = new Date(event.blockTimestamp)
  501. const proposalCancelledEvent = new ProposalCancelledEvent({
  502. ...genericEventFields(event),
  503. proposal,
  504. })
  505. await store.save<ProposalCancelledEvent>(proposalCancelledEvent)
  506. proposal.status = new ProposalStatusCancelled()
  507. proposal.isFinalized = true
  508. proposal.status.cancelledInEventId = proposalCancelledEvent.id
  509. proposal.statusSetAtBlock = event.blockNumber
  510. proposal.statusSetAtTime = eventTime
  511. proposal.updatedAt = eventTime
  512. await store.save<Proposal>(proposal)
  513. }