Skip to content

Commit

Permalink
feat: Update event-handler to support erc-1155 and refactor summary +…
Browse files Browse the repository at this point in the history
… total handled.
  • Loading branch information
brunomenezes committed Feb 18, 2024
1 parent 3d0c5df commit b1a4540
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
25 changes: 25 additions & 0 deletions src/handlers/EventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import { Store } from '@subsquid/typeorm-store';
import {
Application,
ApplicationFactory,
Erc1155Deposit,
Erc20Deposit,
Erc721Deposit,
Input,
MultiToken,
NFT,
Token,
} from '../model';
Expand All @@ -22,6 +24,8 @@ export default class EventHandler {
private readonly factories: Map<string, ApplicationFactory>;
private readonly nfts: Map<string, NFT>;
private readonly erc721Deposits: Map<string, Erc721Deposit>;
private readonly multiTokens: Map<string, MultiToken>;
private readonly erc1155Deposits: Map<string, Erc1155Deposit>;
private readonly applicationCreated: Handler;
private readonly inputAdded: Handler;
private readonly ownershipTransferred: Handler;
Expand All @@ -34,6 +38,8 @@ export default class EventHandler {
this.factories = new Map();
this.nfts = new Map();
this.erc721Deposits = new Map();
this.multiTokens = new Map();
this.erc1155Deposits = new Map();
this.applicationCreated = new ApplicationCreated(
this.factories,
this.applications,
Expand All @@ -46,6 +52,8 @@ export default class EventHandler {
this.inputs,
this.nfts,
this.erc721Deposits,
this.multiTokens,
this.erc1155Deposits,
);

this.ownershipTransferred = new OwnershipTransferred(this.applications);
Expand All @@ -67,6 +75,23 @@ export default class EventHandler {
inputs: this.inputs,
nfts: this.nfts,
erc721Deposits: this.erc721Deposits,
multiTokens: this.multiTokens,
erc1155Deposits: this.erc1155Deposits,
};
}

getTotalHandled() {
return Object.values(this.getValues()).reduce(
(acc, entityMap) => acc + entityMap.size,
0,
);
}

getSummary() {
return Object.entries(this.getValues())
.map(
([entityName, entityMap]) => `${entityName}: ${entityMap.size}`,
)
.join(', ');
}
}
28 changes: 8 additions & 20 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,37 +35,25 @@ processor.run(new TypeormDatabase({ supportHotBlocks: true }), async (ctx) => {
inputs,
nfts,
erc721Deposits,
multiTokens,
erc1155Deposits,
} = eventHandler.getValues();

const total =
tokens.size +
applications.size +
factories.size +
deposits.size +
inputs.size +
nfts.size +
erc721Deposits.size;
const total = eventHandler.getTotalHandled();

if (total > 0) {
const summary = Object.entries({
tokens: tokens.size,
applications: applications.size,
factories: factories.size,
deposits: deposits.size,
inputs: inputs.size,
nfts: nfts.size,
erc721Deposits: erc721Deposits.size,
})
.map(([entity, count]) => `${entity}: ${count}`)
.join(', ');
ctx.log.info(`Flushing ${total} entities: ${summary}`);
ctx.log.info(
`Flushing ${total} entities: ${eventHandler.getSummary()}`,
);
}

await ctx.store.upsert([...multiTokens.values()]);
await ctx.store.upsert([...tokens.values()]);
await ctx.store.upsert([...nfts.values()]);
await ctx.store.upsert([...factories.values()]);
await ctx.store.upsert([...applications.values()]);
await ctx.store.upsert([...deposits.values()]);
await ctx.store.upsert([...erc721Deposits.values()]);
await ctx.store.upsert([...erc1155Deposits.values()]);
await ctx.store.upsert([...inputs.values()]);
});

0 comments on commit b1a4540

Please sign in to comment.