Skip to content

Commit

Permalink
feat: track almanac registrations
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanchriswhite committed Dec 1, 2022
1 parent 6379b3e commit 8e55180
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
7 changes: 7 additions & 0 deletions project.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,10 @@ dataSources:
kind: cosmos/MessageHandler
filter:
type: "/cosmos.authz.v1beta1.MsgExec"
- handler: handleAlmanacRegistration
kind: cosmos/EventHandler
filter:
type: "wasm"
messageFilter:
type: "/cosmwasm.wasm.v1.MsgExecuteContract"
contractCall: "register"
1 change: 1 addition & 0 deletions src/mappings/almanac/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./registrations";
60 changes: 60 additions & 0 deletions src/mappings/almanac/registrations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import {attemptHandling, messageId, unprocessedEventHandler} from "../utils";
import {CosmosEvent} from "@subql/types-cosmos";
import {AlmanacRecord, AlmanacRegistration} from "../../types";

export async function handleAlmanacRegistration(event: CosmosEvent): Promise<void> {
await attemptHandling(event, _handleAlmanacRegistration, unprocessedEventHandler);
}

async function _handleAlmanacRegistration(event: CosmosEvent): Promise<void> {
const id = messageId(event);
logger.info(`[handleAlmanacRegistration] (tx ${event.tx.hash}): indexing AlmanacRegistration ${id}`);
logger.debug(`[handleAlmanacRegistration] (event.log.log): ${JSON.stringify(event.log.log, null, 2)}`);

const attributes = event.event.attributes.reduce((acc, curr) => {
acc[curr.key] = curr.value;
return acc;
}, {});

const {address, record: recordStr, expiry_height} = attributes as unknown as any;
if (!address) {
logger.warn("[handleAlmanacRegistration]: missing address");
return;
}

if (!expiry_height) {
logger.warn("[handleAlmanacRegistration]: missing expiry_height");
return;
}

if (!recordStr) {
logger.warn("[handleAlmanacRegistration]: missing record");
return;
}

const record = JSON.parse(recordStr);
if (!record || !record.Service) {
logger.warn("[handleAlmanacRegistration]: missing record service");
return;
}

const recordEntity = AlmanacRecord.create({
id,
service: record.service,
// eventId: id,
transactionId: event.tx.hash,
blockId: event.block.block.id,
});
await recordEntity.save();

const registrationEntity = AlmanacRegistration.create({
id,
expiryHeight: expiry_height,
accountId: address,
recordId: id,
// eventId: id,
transactionId: event.tx.hash,
blockId: event.block.block.id,
});
await registrationEntity.save();
}
1 change: 1 addition & 0 deletions src/mappings/mappingHandlers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from "./authz";
export * from "./almanac";
export * from "./bank";
export * from "./dist";
export * from "./gov";
Expand Down

0 comments on commit 8e55180

Please sign in to comment.