-
Notifications
You must be signed in to change notification settings - Fork 2
/
started.ts
65 lines (58 loc) · 2.3 KB
/
started.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { toHex } from '@subsquid/substrate-processor'
import { EventHandlerContext } from '../../types/contexts'
import { DemocracyStartedEvent } from '../../../types/events'
import { StorageNotExistsWarn, UnknownVersionError } from '../../../common/errors'
import { EventContext } from '../../../types/support'
import { ProposalStatus, ProposalType, ReferendumThresholdType } from '../../../model'
import { storage } from '../../../storage'
import { createReferendum } from '../../utils/proposals'
import { BatchContext, SubstrateBlock } from '@subsquid/substrate-processor'
import { EventItem } from '@subsquid/substrate-processor/lib/interfaces/dataSelection'
import { Store } from '@subsquid/typeorm-store'
import { Event } from '../../../types/support'
interface ReferendumEventData {
index: number
threshold: string
}
function getEventData(ctx: BatchContext<Store, unknown>, itemEvent: Event): ReferendumEventData {
const event = new DemocracyStartedEvent(ctx, itemEvent)
if (event.isV49) {
const [index, threshold] = event.asV49
return {
index,
threshold: threshold.__kind,
}
} else if (event.isV1201) {
const { refIndex: index, threshold } = event.asV1201
return {
index,
threshold: threshold.__kind,
}
} else {
throw new UnknownVersionError(event.constructor.name)
}
}
export async function handleStarted(ctx: BatchContext<Store, unknown>,
item: EventItem<'Democracy.Started', { event: { args: true; extrinsic: { hash: true } } }>,
header: SubstrateBlock) {
const { index, threshold } = getEventData(ctx, item.event)
const storageData = await storage.democracy.getReferendumInfoOf(ctx, index, header)
if (!storageData) {
ctx.log.warn(StorageNotExistsWarn(ProposalType.Referendum, index))
return
}
if (storageData.status === 'Finished') {
ctx.log.warn(`Referendum with index ${index} has already finished at block ${header.height}`)
return
}
const { hash, end, delay } = storageData
const hexHash = toHex(hash)
await createReferendum(ctx, header, {
index,
threshold: threshold as ReferendumThresholdType,
status: ProposalStatus.Started,
hash: hexHash,
end: end,
delay: delay,
})
}