proposals.ts 21 KB

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