|
| 1 | +import {attemptHandling, messageId, unprocessedEventHandler} from "../utils"; |
| 2 | +import {CosmosEvent} from "@subql/types-cosmos"; |
| 3 | +import {AlmanacRecord, AlmanacRegistration} from "../../types"; |
| 4 | + |
| 5 | +export async function handleAlmanacRegistration(event: CosmosEvent): Promise<void> { |
| 6 | + await attemptHandling(event, _handleAlmanacRegistration, unprocessedEventHandler); |
| 7 | +} |
| 8 | + |
| 9 | +async function _handleAlmanacRegistration(event: CosmosEvent): Promise<void> { |
| 10 | + const id = messageId(event); |
| 11 | + logger.info(`[handleAlmanacRegistration] (tx ${event.tx.hash}): indexing AlmanacRegistration ${id}`); |
| 12 | + logger.debug(`[handleAlmanacRegistration] (event.log.log): ${JSON.stringify(event.log.log, null, 2)}`); |
| 13 | + |
| 14 | + const attributes = event.event.attributes.reduce((acc, curr) => { |
| 15 | + acc[curr.key] = curr.value; |
| 16 | + return acc; |
| 17 | + }, {}); |
| 18 | + |
| 19 | + const {address, record: recordStr, expiry_height} = attributes as unknown as any; |
| 20 | + if (!address) { |
| 21 | + logger.warn("[handleAlmanacRegistration]: missing address"); |
| 22 | + return; |
| 23 | + } |
| 24 | + |
| 25 | + if (!expiry_height) { |
| 26 | + logger.warn("[handleAlmanacRegistration]: missing expiry_height"); |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + if (!recordStr) { |
| 31 | + logger.warn("[handleAlmanacRegistration]: missing record"); |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + const record = JSON.parse(recordStr); |
| 36 | + if (!record || !record.Service) { |
| 37 | + logger.warn("[handleAlmanacRegistration]: missing record service"); |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + const recordEntity = AlmanacRecord.create({ |
| 42 | + id, |
| 43 | + service: record.service, |
| 44 | + // eventId: id, |
| 45 | + transactionId: event.tx.hash, |
| 46 | + blockId: event.block.block.id, |
| 47 | + }); |
| 48 | + await recordEntity.save(); |
| 49 | + |
| 50 | + const registrationEntity = AlmanacRegistration.create({ |
| 51 | + id, |
| 52 | + expiryHeight: expiry_height, |
| 53 | + accountId: address, |
| 54 | + recordId: id, |
| 55 | + // eventId: id, |
| 56 | + transactionId: event.tx.hash, |
| 57 | + blockId: event.block.block.id, |
| 58 | + }); |
| 59 | + await registrationEntity.save(); |
| 60 | +} |
0 commit comments