From 3446eaece9c0461fab62a43c46348486e1e890d0 Mon Sep 17 00:00:00 2001 From: Prathamesh Musale Date: Wed, 18 Sep 2024 15:27:29 +0530 Subject: [PATCH] Sort logs result by log index --- packages/util/src/eth-rpc-handlers.ts | 34 +++++++++++---------------- packages/util/src/server.ts | 1 + 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/packages/util/src/eth-rpc-handlers.ts b/packages/util/src/eth-rpc-handlers.ts index dc78ab05..a66552b3 100644 --- a/packages/util/src/eth-rpc-handlers.ts +++ b/packages/util/src/eth-rpc-handlers.ts @@ -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) { @@ -229,7 +235,7 @@ const buildAddressFilter = (address: any, where: FindConditions) // 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`); } }); @@ -239,13 +245,15 @@ const buildAddressFilter = (address: any, where: FindConditions) } 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): void => { // Check that topics is an array of size <= 4 if (!Array.isArray(topics)) { @@ -256,28 +264,14 @@ const buildTopicsFilter = (topics: any, where: FindConditions): 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 ): any => { if (Array.isArray(topicFilter)) { diff --git a/packages/util/src/server.ts b/packages/util/src/server.ts index df97f75d..c9e0f47b 100644 --- a/packages/util/src/server.ts +++ b/packages/util/src/server.ts @@ -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);