Skip to content

Commit

Permalink
Sort logs result by log index
Browse files Browse the repository at this point in the history
  • Loading branch information
prathamesh0 committed Sep 18, 2024
1 parent cbedd9e commit 3446eae
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 20 deletions.
34 changes: 14 additions & 20 deletions packages/util/src/eth-rpc-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,13 @@ export const createEthRPCHandlers = async (
// Fetch events from the db
// Load block relation
const resultLimit = indexer.serverConfig.ethRPC.getLogsResultLimit || DEFAULT_ETH_GET_LOGS_RESULT_LIMIT;
const events = await indexer.getEvents({ where, relations: ['block'], take: resultLimit + 1 });
const events = await indexer.getEvents({
where,
relations: ['block'],
// TODO: Use querybuilder to order by block number
order: { block: 'ASC', index: 'ASC' },
take: resultLimit + 1
});

// Limit number of results can be returned by a single query
if (events.length > resultLimit) {
Expand Down Expand Up @@ -229,7 +235,7 @@ const buildAddressFilter = (address: any, where: FindConditions<EventInterface>)
// Validate input addresses
address.forEach((add: string) => {
if (!utils.isHexString(add, 20)) {
throw new ErrorWithCode(CODE_INVALID_PARAMS, ERROR_INVALID_CONTRACT_ADDRESS);
throw new ErrorWithCode(CODE_INVALID_PARAMS, `${ERROR_INVALID_CONTRACT_ADDRESS}: expected hex string of size 20`);
}
});

Expand All @@ -239,13 +245,15 @@ const buildAddressFilter = (address: any, where: FindConditions<EventInterface>)
} else {
// Validate input address
if (!utils.isHexString(address, 20)) {
throw new ErrorWithCode(CODE_INVALID_PARAMS, ERROR_INVALID_CONTRACT_ADDRESS);
throw new ErrorWithCode(CODE_INVALID_PARAMS, `${ERROR_INVALID_CONTRACT_ADDRESS}: expected hex string of size 20`);
}

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

type TopicColumn = 'topic0' | 'topic1' | 'topic2' | 'topic3';

const buildTopicsFilter = (topics: any, where: FindConditions<EventInterface>): void => {
// Check that topics is an array of size <= 4
if (!Array.isArray(topics)) {
Expand All @@ -256,28 +264,14 @@ const buildTopicsFilter = (topics: any, where: FindConditions<EventInterface>):
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);
for (let i = 0; i < topics.length; i++) {
addTopicCondition(topics[i], `topic${i}` as TopicColumn, where);
}
};

const addTopicCondition = (
topicFilter: string[] | string,
topicIndex: 'topic0' | 'topic1' | 'topic2' | 'topic3',
topicIndex: TopicColumn,
where: FindConditions<EventInterface>
): any => {
if (Array.isArray(topicFilter)) {
Expand Down
1 change: 1 addition & 0 deletions packages/util/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export const createAndStartServer = async (
});

const rpcPath = serverConfig.ethRPC?.path ?? DEFAULT_ETH_RPC_PATH;

if (serverConfig.ethRPC?.enabled) {
// Create a JSON-RPC server to handle ETH RPC calls
const rpcServer = jayson.Server(ethRPCHandlers);
Expand Down

0 comments on commit 3446eae

Please sign in to comment.