Add eth_call API handler

This commit is contained in:
Prathamesh Musale 2024-09-12 15:09:27 +05:30
parent 87cc0fec04
commit 8b70c22d17
2 changed files with 45 additions and 2 deletions

View File

@ -13,7 +13,49 @@ export const createEthRPCHandlers = async (
},
eth_call: async (args: any, callback: any) => {
// TODO: Implement
// TODO: Handle empty args
// TODO: Set errors in response
// TODO: Parse blockTag
const { to, data, blockTag } = args[0];
// For values other than blockHash, resolve value from block_progress table
const latestBlock = await indexer.getLatestCanonicalBlock();
const blockHash = latestBlock?.blockHash;
const watchedContract = indexer.getWatchedContracts().find(contract => contract.address === to);
if (!watchedContract) {
throw new Error('Contract not recognized');
}
if (!indexer.contractMap) {
throw new Error('Contract map not found');
}
const contractInterface = indexer.contractMap.get(watchedContract.kind);
if (!contractInterface) {
throw new Error('Contract ABI not found');
}
// Slice out method signature
const functionSelector = data.slice(0, 10);
// Find the matching function from the ABI
const functionFragment = contractInterface.getFunction(functionSelector);
if (!functionFragment) {
throw new Error('Method not found');
}
// Decode the data based on the matched function
const decodedData = contractInterface.decodeFunctionData(functionFragment, data);
const functionName = functionFragment.name;
const indexerMethod = (indexer as any)[functionName].bind(indexer);
if (indexerMethod && typeof indexerMethod === 'function') {
const result = await indexerMethod(blockHash, to, ...decodedData);
const encodedResult = contractInterface.encodeFunctionResult(functionFragment, [result.value]);
callback(null, encodedResult);
}
},
eth_getLogs: async (args: any, callback: any) => {

View File

@ -3,7 +3,7 @@
//
import { Connection, DeepPartial, EntityTarget, FindConditions, FindManyOptions, ObjectLiteral, QueryRunner } from 'typeorm';
import { Transaction } from 'ethers';
import { ethers, Transaction } from 'ethers';
import { MappingKey, StorageLayout } from '@cerc-io/solidity-mapper';
@ -161,6 +161,7 @@ export interface IndexerInterface {
readonly serverConfig: ServerConfig
readonly upstreamConfig: UpstreamConfig
readonly storageLayoutMap: Map<string, StorageLayout>
readonly contractMap: Map<string, ethers.utils.Interface>
// eslint-disable-next-line no-use-before-define
readonly graphWatcher?: GraphWatcherInterface
init (): Promise<void>