Skip to content

Commit

Permalink
Update block tag parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
prathamesh0 committed Sep 13, 2024
1 parent 2bcbfac commit f57ea8d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
8 changes: 4 additions & 4 deletions packages/cli/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ import {
readParty,
UpstreamConfig,
fillBlocks,
createGQLLogger
createGQLLogger,
createEthRPCHandlers
} from '@cerc-io/util';
import { TypeSource } from '@graphql-tools/utils';
import type {
Expand Down Expand Up @@ -276,8 +277,7 @@ export class ServerCmd {
gqlLogger: winston.Logger
) => Promise<any>,
typeDefs: TypeSource,
paymentsManager?: PaymentsManager,
createEthRPCHandlers?: (indexer: IndexerInterface, ethProvider: JsonRpcProvider) => Promise<any>
paymentsManager?: PaymentsManager
): Promise<{
app: Application,
server: ApolloServer
Expand Down Expand Up @@ -319,7 +319,7 @@ export class ServerCmd {
const gqlLogger = createGQLLogger(config.server.gql.logDir);
const resolvers = await createResolvers(indexer, eventWatcher, gqlLogger);

const ethRPCHandlers = createEthRPCHandlers ? await createEthRPCHandlers(indexer, ethProvider) : {};
const ethRPCHandlers = await createEthRPCHandlers(indexer, ethProvider);

// Create an Express app
const app: Application = express();
Expand Down
21 changes: 12 additions & 9 deletions packages/util/src/eth-rpc-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ const ERROR_CONTRACT_NOT_RECOGNIZED = 'Contract not recognized';
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_BLOCK_NOT_FOUND = 'Block not found';

const DEFAULT_BLOCK_TAG = 'latest';

class ErrorWithCode extends Error {
code: number;
Expand Down Expand Up @@ -47,7 +50,8 @@ export const createEthRPCHandlers = async (
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);

Expand Down Expand Up @@ -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)) {
// Return value if hex string is of block hash length
if (utils.hexDataLength(blockTag) === 32) {
Expand All @@ -110,21 +114,20 @@ const parseBlockTag = async (indexer: IndexerInterface, ethProvider: JsonRpcProv

// Treat hex value as a block number
const block = await ethProvider.getBlock(blockTag);
if (block === null) {
throw new ErrorWithCode(CODE_INVALID_PARAMS, ERROR_BLOCK_NOT_FOUND);
}

return block.hash;
}

// TODO: Handle pending, safe and finalized
if (['earliest', 'latest', 'pending', 'safe', 'finalized', null, undefined].includes(blockTag)) {
if (blockTag === DEFAULT_BLOCK_TAG) {
const syncStatus = await indexer.getSyncStatus();
if (!syncStatus) {
throw new ErrorWithCode(CODE_INTERNAL_ERROR, 'SyncStatus not found');
}

if (blockTag === 'earliest') {
return syncStatus.initialIndexedBlockHash;
}

return syncStatus.latestIndexedBlockHash;
return syncStatus.latestProcessedBlockHash;
}

throw new ErrorWithCode(CODE_INVALID_PARAMS, ERROR_INVALID_BLOCK_TAG);
Expand Down

0 comments on commit f57ea8d

Please sign in to comment.