Skip to content

Commit

Permalink
✨ Feat: fetches and upserts subnets
Browse files Browse the repository at this point in the history
  • Loading branch information
PsicoThePato committed Oct 7, 2024
1 parent 54adaeb commit 35fe41a
Showing 1 changed file with 21 additions and 24 deletions.
45 changes: 21 additions & 24 deletions apps/commune-worker/src/workers/subnet-fetcher.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,38 @@
import {
// queryLastBlock,
queryRegisteredSubnetsInfo,
queryLastBlock,
querySubnetParams,
} from "@commune-ts/subspace/queries";

import type { WorkerProps } from "../types";
import {
BLOCK_TIME,
// isNewBlock,
log,
sleep,
sleepUntilNewBlock,
} from "../common";

// import { upsertSubnetData } from "../db";
import { BLOCK_TIME, isNewBlock, log, sleep } from "../common";
import { upsertSubnetData } from "../db";
import { SubnetToDatabase } from "../db/type-transformations.js";

export async function subnetFetcherWorker(props: WorkerProps) {
while (true) {
try {
const currentTime = new Date();
const lastBlock = await sleepUntilNewBlock(props);
const lastBlock = await queryLastBlock(props.api);
if (!isNewBlock(props.lastBlock.blockNumber, lastBlock.blockNumber)) {
await sleep(BLOCK_TIME);
continue;
}
props.lastBlock = lastBlock;
await queryRegisteredSubnetsInfo(props.api);
log(`Block ${props.lastBlock.blockNumber}: processing`);

// const modules = await queryRegisteredSubnetsInfo(
// props.lastBlock.apiAtBlock,
// ["name", "address", "metadata", "registrationBlock"],
// [NETUID_ZERO],
// );
// log(
// `Block ${props.lastBlock.blockNumber}: upserting ${subnet.length} subnets`,
// );
log(`Block ${props.lastBlock.blockNumber}: processing`);

// await upsertSubnetData(subnet, props.lastBlock.blockNumber);
// TODO: do this in a better way
const subnets = await querySubnetParams(props.lastBlock.apiAtBlock);
const subnetData = subnets.map((subnet) =>
SubnetToDatabase(subnet, props.lastBlock.blockNumber),
);
log(
`Block ${props.lastBlock.blockNumber}: upserting ${subnetData.length} modules`,
);
await upsertSubnetData(subnetData);

log(
`Block ${props.lastBlock.blockNumber}: subnet data upserted in ${(new Date().getTime() - currentTime.getTime()) / 1000} seconds`,
`Block ${props.lastBlock.blockNumber}: module data upserted in ${(new Date().getTime() - currentTime.getTime()) / 1000} seconds`,
);
} catch (e) {
log("UNEXPECTED ERROR: ", e);
Expand Down

0 comments on commit 35fe41a

Please sign in to comment.