Skip to content

Commit

Permalink
wip: resolutions
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanchriswhite committed Dec 7, 2022
1 parent fc0e6a9 commit e3302c5
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/mappings/almanac/registrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from "../utils";
import {CosmosEvent} from "@subql/types-cosmos";
import {Agent, AlmanacRecord, AlmanacRegistration, Contract} from "../../types";
import {cacheAlmanacResolution} from "./resolutions";

export async function handleAlmanacRegistration(event: CosmosEvent): Promise<void> {
await attemptHandling(event, _handleAlmanacRegistration, unprocessedEventHandler);
Expand Down Expand Up @@ -107,4 +108,5 @@ async function _handleAlmanacRegistration(event: CosmosEvent): Promise<void> {
blockId: event.block.block.id,
});
await registrationEntity.save();
await cacheAlmanacResolution(_contract_address, agent_address, recordEntity);
}
55 changes: 55 additions & 0 deletions src/mappings/almanac/resolutions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import {AlmanacRecord, AlmanacResolution, Block, Event, Transaction} from "../../types";
import {parseAttributes, WasmdEventAttributesI} from "../utils";

export async function cacheAlmanacResolution(contractId: string, agentId: string, record: AlmanacRecord): Promise<void> {
try {
logger.info(`[cacheAlmanacResolution] (recordId ${record.id}): caching record`);
const resolutionEntity = AlmanacResolution.create({
id: record.id,
agentId,
contractId,
recordId: record.id,
});
await resolutionEntity.save();
} catch (error) {
logger.error(`[cacheAlmanacResolution] (recordId: ${record.id}): ${error.stack}`);
}
}

export async function expireAlmanacResolutionsRelativeToHeight(height: bigint): Promise<void> {
/* for contract in almanac contracts {
* expiryHeight = contract.expiryHeight
* }
*/

const blocks = await Block.getByHeight(BigInt(height));
// const blocks = await Block.getByHeight(BigInt(expiryHeight));
if (!blocks || blocks.length !== 1) {
logger.error(`[expireAlmanacResolutionAtHeight] (height: ${height}): found ${blocks?.length ?? 0} blocks at that height`);
}

const events = await Event.getByBlockId(blocks[0].id);
if (!events || events.length !== 1) {
logger.error(`[expireAlmanacResolutionAtHeight] (height: ${height}): found ${events?.length ?? 0} events at that height`);
}

for (const event of events) {
const attributes = parseAttributes<WasmdEventAttributesI>(event.attributes);
if (!attributes.action || attributes.action !== "register") {
continue;
}

// NB: recordId is equivalent to ID of register event.
await expireAlmanacResolution(event.id);
}
}

export async function expireAlmanacResolution(recordId: string): Promise<void> {
try {
logger.info(`[expireAlmanacResolution] (recordId ${recordId}): expiring record`);
await AlmanacResolution.remove(recordId);
} catch (error) {
logger.warn(`[expireAlmanacResolution] (recordId: ${recordId}): ${error.stack}`);
}
}

7 changes: 5 additions & 2 deletions src/mappings/primitives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from "./utils";
import {createHash} from "crypto";
import {toBech32} from "@cosmjs/encoding";
import {expireAlmanacResolutionsRelativeToHeight} from "./almanac/resolutions";

export async function handleBlock(block: CosmosBlock): Promise<void> {
await attemptHandling(block, _handleBlock, _handleBlockError);
Expand All @@ -30,16 +31,18 @@ export async function handleEvent(event: CosmosEvent): Promise<void> {
async function _handleBlock(block: CosmosBlock): Promise<void> {
logger.info(`[handleBlock] (block.header.height): indexing block ${block.block.header.height}`);

const {id, header: {chainId, height, time}} = block.block;
const {id, header: {chainId, height: _height, time}} = block.block;
const timestamp = new Date(time);
const height = BigInt(_height);
const blockEntity = Block.create({
id,
chainId,
height: BigInt(height),
height,
timestamp
});

await blockEntity.save();
await expireAlmanacResolutionsAtHeight(height);
}

async function _handleTransaction(tx: CosmosTransaction): Promise<void> {
Expand Down

0 comments on commit e3302c5

Please sign in to comment.