|
| 1 | +import { |
| 2 | + attemptHandling, |
| 3 | + messageId, |
| 4 | + parseAttributes, |
| 5 | + unprocessedEventHandler, |
| 6 | + WasmdEventAttributesI |
| 7 | +} from "../utils"; |
| 8 | +import {CosmosEvent} from "@subql/types-cosmos"; |
| 9 | +import {Agent, AlmanacRecord, AlmanacRegistration, Contract} from "../../types"; |
| 10 | + |
| 11 | +export async function handleAlmanacRegistration(event: CosmosEvent): Promise<void> { |
| 12 | + await attemptHandling(event, _handleAlmanacRegistration, unprocessedEventHandler); |
| 13 | +} |
| 14 | + |
| 15 | +export interface RegisterAttributes extends WasmdEventAttributesI { |
| 16 | + expiry_height?: string, |
| 17 | + sequence?: number; |
| 18 | + signature?: string; |
| 19 | + agent_address?: string; |
| 20 | + record?: string, |
| 21 | +} |
| 22 | + |
| 23 | +async function _handleAlmanacRegistration(event: CosmosEvent): Promise<void> { |
| 24 | + const id = messageId(event); |
| 25 | + logger.info(`[handleAlmanacRegistration] (tx ${event.tx.hash}): indexing AlmanacRegistration ${id}`); |
| 26 | + logger.debug(`[handleAlmanacRegistration] (event.log.log): ${JSON.stringify(event.log.log, null, 2)}`); |
| 27 | + |
| 28 | + /* NB: signature verification is handled by the almanac contract! |
| 29 | + * This handler assumes that the contract will only emit events |
| 30 | + * with valid signatures corresponding to the agent address. |
| 31 | + */ |
| 32 | + |
| 33 | + const { |
| 34 | + _contract_address, |
| 35 | + agent_address, |
| 36 | + expiry_height, |
| 37 | + sequence, |
| 38 | + signature, |
| 39 | + record: recordStr, |
| 40 | + } = parseAttributes<RegisterAttributes>(event.event.attributes); |
| 41 | + |
| 42 | + if (!agent_address) { |
| 43 | + logger.warn("[handleAlmanacRegistration]: missing address"); |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + if (!signature) { |
| 48 | + logger.warn("[handleAlmanacRegistration]: missing signature"); |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + if (!sequence) { |
| 53 | + logger.warn("[handleAlmanacRegistration]: missing sequence"); |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + if (!expiry_height) { |
| 58 | + logger.warn("[handleAlmanacRegistration]: missing expiry_height"); |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + if (!recordStr) { |
| 63 | + logger.warn("[handleAlmanacRegistration]: missing record"); |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + const record = JSON.parse(recordStr); |
| 68 | + if (!record || !record.service) { |
| 69 | + logger.warn("[handleAlmanacRegistration]: missing record service"); |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + // NB: ensure agent exists |
| 74 | + const agent = await Agent.get(agent_address); |
| 75 | + if (!agent) { |
| 76 | + await Agent.create({ |
| 77 | + id: agent_address, |
| 78 | + }).save(); |
| 79 | + } |
| 80 | + |
| 81 | + // NB: ensure contract exists |
| 82 | + const contract = await Contract.get(_contract_address); |
| 83 | + if (!contract) { |
| 84 | + logger.warn(`[handleAlmanacRegistration]: unable to find contract with address: ${_contract_address}`); |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + const recordEntity = AlmanacRecord.create({ |
| 89 | + id, |
| 90 | + service: record.service, |
| 91 | + // eventId: id, |
| 92 | + transactionId: event.tx.hash, |
| 93 | + blockId: event.block.block.id, |
| 94 | + }); |
| 95 | + await recordEntity.save(); |
| 96 | + |
| 97 | + const registrationEntity = AlmanacRegistration.create({ |
| 98 | + id, |
| 99 | + expiryHeight: BigInt(expiry_height), |
| 100 | + signature, |
| 101 | + sequence, |
| 102 | + agentId: agent_address, |
| 103 | + recordId: id, |
| 104 | + contractId: _contract_address, |
| 105 | + // eventId: id, |
| 106 | + transactionId: event.tx.hash, |
| 107 | + blockId: event.block.block.id, |
| 108 | + }); |
| 109 | + await registrationEntity.save(); |
| 110 | +} |
0 commit comments