Skip to content

Commit

Permalink
Support topics filter in eth_getLogs RPC API
Browse files Browse the repository at this point in the history
  • Loading branch information
prathamesh0 committed Sep 18, 2024
1 parent d0d6f68 commit 10c8581
Showing 1 changed file with 94 additions and 15 deletions.
109 changes: 94 additions & 15 deletions packages/util/src/eth-rpc-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ const ERROR_CONTRACT_METHOD_NOT_FOUND = 'Contract method not found';
const ERROR_METHOD_NOT_IMPLEMENTED = 'Method not implemented';
const ERROR_INVALID_BLOCK_TAG = 'Invalid block tag';
const ERROR_INVALID_BLOCK_HASH = 'Invalid block hash';
const ERROR_INVALID_CONTRACT_ADDRESS = 'Invalid contract address';
const ERROR_INVALID_TOPICS = 'Invalid topics';
const ERROR_BLOCK_NOT_FOUND = 'Block not found';
const ERROR_TOPICS_FILTER_NOT_SUPPORTED = 'Topics filter not supported';
const ERROR_LIMIT_EXCEEDED = 'Query results exceeds limit';

const DEFAULT_BLOCK_TAG = 'latest';
Expand Down Expand Up @@ -114,20 +115,14 @@ export const createEthRPCHandlers = async (
// Parse arg params into where options
const where: FindConditions<EventInterface> = {};

// TODO: Support topics filter
if (params.topics) {
throw new ErrorWithCode(CODE_INVALID_PARAMS, ERROR_TOPICS_FILTER_NOT_SUPPORTED);
}

// Address filter, address or a list of addresses
if (params.address) {
if (Array.isArray(params.address)) {
if (params.address.length > 0) {
where.contract = In(params.address);
}
} else {
where.contract = Equal(params.address);
}
buildAddressFilter(params.address, where);
}

// Topics filter
if (params.topics) {
buildTopicsFilter(params.topics, where);
}

// Block hash takes precedence over fromBlock / toBlock if provided
Expand Down Expand Up @@ -229,19 +224,103 @@ const parseEthGetLogsBlockTag = async (indexer: IndexerInterface, blockTag: stri
throw new ErrorWithCode(CODE_INVALID_PARAMS, ERROR_INVALID_BLOCK_TAG);
};

const buildAddressFilter = (address: any, where: FindConditions<EventInterface>): void => {
if (Array.isArray(address)) {
// Validate input addresses
address.forEach((add: string) => {
if (!utils.isHexString(add, 20)) {
throw new ErrorWithCode(CODE_INVALID_PARAMS, ERROR_INVALID_CONTRACT_ADDRESS);
}
});

if (address.length > 0) {
where.contract = In(address);
}
} else {
// Validate input address
if (!utils.isHexString(address, 20)) {
throw new ErrorWithCode(CODE_INVALID_PARAMS, ERROR_INVALID_CONTRACT_ADDRESS);
}

where.contract = Equal(address);
}
};

const buildTopicsFilter = (topics: any, where: FindConditions<EventInterface>): void => {
// Check that topics is an array of size <= 4
if (!Array.isArray(topics)) {
throw new ErrorWithCode(CODE_INVALID_PARAMS, ERROR_INVALID_TOPICS);
}

if (topics.length > 4) {
throw new ErrorWithCode(CODE_INVALID_PARAMS, `${ERROR_INVALID_TOPICS}: exceeds max topics`);
}

const topicsFilterLength = topics.length;

if (topicsFilterLength > 0) {
addTopicCondition(topics[0], 'topic0', where);
}

if (topicsFilterLength > 1) {
addTopicCondition(topics[1], 'topic1', where);
}

if (topicsFilterLength > 2) {
addTopicCondition(topics[2], 'topic2', where);
}

if (topicsFilterLength > 3) {
addTopicCondition(topics[3], 'topic3', where);
}
};

const addTopicCondition = (
topicFilter: string[] | string,
topicIndex: 'topic0' | 'topic1' | 'topic2' | 'topic3',
where: FindConditions<EventInterface>
): any => {
if (Array.isArray(topicFilter)) {
// Validate input topics
topicFilter.forEach((topic: string) => {
if (!utils.isHexString(topic, 32)) {
throw new ErrorWithCode(CODE_INVALID_PARAMS, `${ERROR_INVALID_TOPICS}: expected hex string of size 32 for ${topicIndex}`);
}
});

if (topicFilter.length > 0) {
where[topicIndex] = In(topicFilter);
}
} else {
// Validate input address
if (!utils.isHexString(topicFilter, 32)) {
throw new ErrorWithCode(CODE_INVALID_PARAMS, `${ERROR_INVALID_TOPICS}: expected hex string of size 32 for ${topicIndex}`);
}

where[topicIndex] = Equal(topicFilter);
}
};

const transformEventsToLogs = async (events: Array<EventInterface>): Promise<any[]> => {
return events.map(event => {
const parsedExtraInfo = JSON.parse(event.extraInfo);

const topics: string[] = [];
[event.topic0, event.topic1, event.topic2, event.topic3].forEach(topic => {
if (topic) {
topics.push(topic);
}
});

return {
address: event.contract.toLowerCase(),
blockHash: event.block.blockHash,
blockNumber: `0x${event.block.blockNumber.toString(16)}`,
transactionHash: event.txHash,
transactionIndex: `0x${parsedExtraInfo.tx.index.toString(16)}`,
logIndex: `0x${parsedExtraInfo.logIndex.toString(16)}`,
data: parsedExtraInfo.data,
topics: parsedExtraInfo.topics,
data: event.data,
topics,
removed: event.block.isPruned
};
});
Expand Down

0 comments on commit 10c8581

Please sign in to comment.