Update block tag parsing

This commit is contained in:
Prathamesh Musale 2024-09-13 11:57:57 +05:30
parent 2bcbfac98c
commit f57ea8d3eb
2 changed files with 16 additions and 13 deletions

View File

@ -32,7 +32,8 @@ import {
readParty, readParty,
UpstreamConfig, UpstreamConfig,
fillBlocks, fillBlocks,
createGQLLogger createGQLLogger,
createEthRPCHandlers
} from '@cerc-io/util'; } from '@cerc-io/util';
import { TypeSource } from '@graphql-tools/utils'; import { TypeSource } from '@graphql-tools/utils';
import type { import type {
@ -276,8 +277,7 @@ export class ServerCmd {
gqlLogger: winston.Logger gqlLogger: winston.Logger
) => Promise<any>, ) => Promise<any>,
typeDefs: TypeSource, typeDefs: TypeSource,
paymentsManager?: PaymentsManager, paymentsManager?: PaymentsManager
createEthRPCHandlers?: (indexer: IndexerInterface, ethProvider: JsonRpcProvider) => Promise<any>
): Promise<{ ): Promise<{
app: Application, app: Application,
server: ApolloServer server: ApolloServer
@ -319,7 +319,7 @@ export class ServerCmd {
const gqlLogger = createGQLLogger(config.server.gql.logDir); const gqlLogger = createGQLLogger(config.server.gql.logDir);
const resolvers = await createResolvers(indexer, eventWatcher, gqlLogger); const resolvers = await createResolvers(indexer, eventWatcher, gqlLogger);
const ethRPCHandlers = createEthRPCHandlers ? await createEthRPCHandlers(indexer, ethProvider) : {}; const ethRPCHandlers = await createEthRPCHandlers(indexer, ethProvider);
// Create an Express app // Create an Express app
const app: Application = express(); const app: Application = express();

View File

@ -16,6 +16,9 @@ const ERROR_CONTRACT_NOT_RECOGNIZED = 'Contract not recognized';
const ERROR_CONTRACT_METHOD_NOT_FOUND = 'Contract method not found'; const ERROR_CONTRACT_METHOD_NOT_FOUND = 'Contract method not found';
const ERROR_METHOD_NOT_IMPLEMENTED = 'Method not implemented'; const ERROR_METHOD_NOT_IMPLEMENTED = 'Method not implemented';
const ERROR_INVALID_BLOCK_TAG = 'Invalid block tag'; const ERROR_INVALID_BLOCK_TAG = 'Invalid block tag';
const ERROR_BLOCK_NOT_FOUND = 'Block not found';
const DEFAULT_BLOCK_TAG = 'latest';
class ErrorWithCode extends Error { class ErrorWithCode extends Error {
code: number; code: number;
@ -47,7 +50,8 @@ export const createEthRPCHandlers = async (
throw new ErrorWithCode(CODE_INVALID_PARAMS, ERROR_CONTRACT_INSUFFICIENT_PARAMS); throw new ErrorWithCode(CODE_INVALID_PARAMS, ERROR_CONTRACT_INSUFFICIENT_PARAMS);
} }
const { to, data, blockTag } = args[0]; const { to, data } = args[0];
const blockTag = args.length > 1 ? args[1] : DEFAULT_BLOCK_TAG;
const blockHash = await parseBlockTag(indexer, ethProvider, blockTag); const blockHash = await parseBlockTag(indexer, ethProvider, blockTag);
@ -101,7 +105,7 @@ export const createEthRPCHandlers = async (
}; };
}; };
const parseBlockTag = async (indexer: IndexerInterface, ethProvider: JsonRpcProvider, blockTag: any): Promise<string> => { const parseBlockTag = async (indexer: IndexerInterface, ethProvider: JsonRpcProvider, blockTag: string): Promise<string> => {
if (utils.isHexString(blockTag)) { if (utils.isHexString(blockTag)) {
// Return value if hex string is of block hash length // Return value if hex string is of block hash length
if (utils.hexDataLength(blockTag) === 32) { if (utils.hexDataLength(blockTag) === 32) {
@ -110,21 +114,20 @@ const parseBlockTag = async (indexer: IndexerInterface, ethProvider: JsonRpcProv
// Treat hex value as a block number // Treat hex value as a block number
const block = await ethProvider.getBlock(blockTag); const block = await ethProvider.getBlock(blockTag);
if (block === null) {
throw new ErrorWithCode(CODE_INVALID_PARAMS, ERROR_BLOCK_NOT_FOUND);
}
return block.hash; return block.hash;
} }
// TODO: Handle pending, safe and finalized if (blockTag === DEFAULT_BLOCK_TAG) {
if (['earliest', 'latest', 'pending', 'safe', 'finalized', null, undefined].includes(blockTag)) {
const syncStatus = await indexer.getSyncStatus(); const syncStatus = await indexer.getSyncStatus();
if (!syncStatus) { if (!syncStatus) {
throw new ErrorWithCode(CODE_INTERNAL_ERROR, 'SyncStatus not found'); throw new ErrorWithCode(CODE_INTERNAL_ERROR, 'SyncStatus not found');
} }
if (blockTag === 'earliest') { return syncStatus.latestProcessedBlockHash;
return syncStatus.initialIndexedBlockHash;
}
return syncStatus.latestIndexedBlockHash;
} }
throw new ErrorWithCode(CODE_INVALID_PARAMS, ERROR_INVALID_BLOCK_TAG); throw new ErrorWithCode(CODE_INVALID_PARAMS, ERROR_INVALID_BLOCK_TAG);