Filter logs by events specified in subgraph config (#473)

* Use topic hash of events from config file

* Add eventSignaturesMap field in IndexerInterface

* Add eventSignaturesMap property in Indexer class

---------

Co-authored-by: neeraj <neeraj.rtly@gmail.com>
This commit is contained in:
Nabarun Gogoi 2023-11-16 11:54:49 +05:30 committed by GitHub
parent 8e9d5092fc
commit 5b95335fde
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 7 deletions

View File

@ -102,7 +102,7 @@ export class Indexer implements IndexerInterface {
_abiMap: Map<string, JsonFragment[]>;
_storageLayoutMap: Map<string, StorageLayout>;
_contractMap: Map<string, ethers.utils.Interface>;
_eventSignaturesMap: Map<string, string[]>;
eventSignaturesMap: Map<string, string[]>;
{{#if (subgraphPath)}}
_entityTypesMap: Map<string, { [key: string]: string }>;
@ -141,7 +141,7 @@ export class Indexer implements IndexerInterface {
this._abiMap = new Map();
this._storageLayoutMap = new Map();
this._contractMap = new Map();
this._eventSignaturesMap = new Map();
this.eventSignaturesMap = new Map();
let contractInterface: ethers.utils.Interface;
let eventSignatures: string[];
{{#each contracts as | contract |}}
@ -159,7 +159,7 @@ export class Indexer implements IndexerInterface {
eventSignatures = Object.values(contractInterface.events).map(value => {
return contractInterface.getEventTopic(value);
});
this._eventSignaturesMap.set(KIND_{{capitalize contract.contractName}}, eventSignatures);
this.eventSignaturesMap.set(KIND_{{capitalize contract.contractName}}, eventSignatures);
{{#if contract.contractStorageLayout}}
assert({{contract.contractName}}StorageLayout);
@ -689,7 +689,7 @@ export class Indexer implements IndexerInterface {
}
async fetchEventsAndSaveBlocks (blocks: DeepPartial<BlockProgress>[]): Promise<{ blockProgress: BlockProgress, events: DeepPartial<Event>[] }[]> {
return this._baseIndexer.fetchEventsAndSaveBlocks(blocks, this._eventSignaturesMap, this.parseEventNameAndArgs.bind(this));
return this._baseIndexer.fetchEventsAndSaveBlocks(blocks, this.eventSignaturesMap, this.parseEventNameAndArgs.bind(this));
}
async fetchAndSaveFilteredEventsAndBlocks (startBlock: number, endBlock: number): Promise<{
@ -698,11 +698,11 @@ export class Indexer implements IndexerInterface {
ethFullBlock: EthFullBlock;
ethFullTransactions: EthFullTransaction[];
}[]> {
return this._baseIndexer.fetchAndSaveFilteredEventsAndBlocks(startBlock, endBlock, this._eventSignaturesMap, this.parseEventNameAndArgs.bind(this));
return this._baseIndexer.fetchAndSaveFilteredEventsAndBlocks(startBlock, endBlock, this.eventSignaturesMap, this.parseEventNameAndArgs.bind(this));
}
async fetchEventsForContracts (blockHash: string, blockNumber: number, addresses: string[]): Promise<DeepPartial<Event>[]> {
return this._baseIndexer.fetchEventsForContracts(blockHash, blockNumber, addresses, this._eventSignaturesMap, this.parseEventNameAndArgs.bind(this));
return this._baseIndexer.fetchEventsForContracts(blockHash, blockNumber, addresses, this.eventSignaturesMap, this.parseEventNameAndArgs.bind(this));
}
async saveBlockAndFetchEvents (block: DeepPartial<BlockProgress>): Promise<[
@ -843,7 +843,7 @@ export class Indexer implements IndexerInterface {
assert(blockHash);
assert(blockNumber);
const { events: dbEvents, transactions } = await this._baseIndexer.fetchEvents(blockHash, blockNumber, this._eventSignaturesMap, this.parseEventNameAndArgs.bind(this));
const { events: dbEvents, transactions } = await this._baseIndexer.fetchEvents(blockHash, blockNumber, this.eventSignaturesMap, this.parseEventNameAndArgs.bind(this));
const dbTx = await this._db.createTransactionRunner();
try {

View File

@ -125,6 +125,21 @@ export class GraphWatcher {
return acc;
}, {});
this.fillEventSignatureMap();
}
fillEventSignatureMap () {
this._dataSources.forEach(contract => {
if (contract.kind === 'ethereum/contract' && contract.mapping.kind === 'ethereum/events') {
const contractName = contract.name;
const topicHashes = contract.mapping.eventHandlers.map((handler: any) => {
return this._dataSourceMap[contractName].contractInterface.getEventTopic(utils.EventFragment.from(handler.event).name);
});
assert(this._indexer);
this._indexer.eventSignaturesMap.set(contractName, topicHashes);
}
});
}
get dataSources (): any[] {

View File

@ -27,6 +27,7 @@ import { GetStorageAt, getStorageValue, MappingKey, StorageLayout } from '@cerc-
export class Indexer implements IndexerInterface {
_getStorageAt: GetStorageAt;
_storageLayoutMap: Map<string, StorageLayout> = new Map();
eventSignaturesMap: Map<string, string[]> = new Map();
constructor (ethClient: EthClient, storageLayoutMap?: Map<string, StorageLayout>) {
this._getStorageAt = ethClient.getStorageAt.bind(ethClient);

View File

@ -156,6 +156,7 @@ export interface EthClient {
}
export interface IndexerInterface {
eventSignaturesMap: Map<string, string[]>
readonly serverConfig: ServerConfig
readonly upstreamConfig: UpstreamConfig
readonly storageLayoutMap: Map<string, StorageLayout>