|
| 1 | +/* eslint-disable @typescript-eslint/no-unused-vars */ |
| 2 | +import { utils } from 'ethers'; |
| 3 | + |
| 4 | +import { JsonRpcProvider } from '@ethersproject/providers'; |
| 5 | + |
| 6 | +import { IndexerInterface } from './types'; |
| 7 | + |
| 8 | +const CODE_INVALID_PARAMS = -32602; |
| 9 | +const CODE_INTERNAL_ERROR = -32603; |
| 10 | +const CODE_SERVER_ERROR = -32000; |
| 11 | + |
| 12 | +const ERROR_CONTRACT_MAP_NOT_SET = 'Contract map not set'; |
| 13 | +const ERROR_CONTRACT_ABI_NOT_FOUND = 'Contract ABI not found'; |
| 14 | +const ERROR_CONTRACT_INSUFFICIENT_PARAMS = 'Insufficient params'; |
| 15 | +const ERROR_CONTRACT_NOT_RECOGNIZED = 'Contract not recognized'; |
| 16 | +const ERROR_CONTRACT_METHOD_NOT_FOUND = 'Contract method not found'; |
| 17 | +const ERROR_METHOD_NOT_IMPLEMENTED = 'Method not implemented'; |
| 18 | +const ERROR_INVALID_BLOCK_TAG = 'Invalid block tag'; |
| 19 | +const ERROR_BLOCK_NOT_FOUND = 'Block not found'; |
| 20 | + |
| 21 | +const DEFAULT_BLOCK_TAG = 'latest'; |
| 22 | + |
| 23 | +class ErrorWithCode extends Error { |
| 24 | + code: number; |
| 25 | + constructor (code: number, message: string) { |
| 26 | + super(message); |
| 27 | + this.code = code; |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +export const createEthRPCHandlers = async ( |
| 32 | + indexer: IndexerInterface, |
| 33 | + ethProvider: JsonRpcProvider |
| 34 | +): Promise<any> => { |
| 35 | + return { |
| 36 | + eth_blockNumber: async (args: any, callback: any) => { |
| 37 | + const syncStatus = await indexer.getSyncStatus(); |
| 38 | + const result = syncStatus ? `0x${syncStatus.latestProcessedBlockNumber.toString(16)}` : '0x'; |
| 39 | + |
| 40 | + callback(null, result); |
| 41 | + }, |
| 42 | + |
| 43 | + eth_call: async (args: any, callback: any) => { |
| 44 | + try { |
| 45 | + if (!indexer.contractMap) { |
| 46 | + throw new ErrorWithCode(CODE_INTERNAL_ERROR, ERROR_CONTRACT_MAP_NOT_SET); |
| 47 | + } |
| 48 | + |
| 49 | + if (args.length === 0) { |
| 50 | + throw new ErrorWithCode(CODE_INVALID_PARAMS, ERROR_CONTRACT_INSUFFICIENT_PARAMS); |
| 51 | + } |
| 52 | + |
| 53 | + const { to, data } = args[0]; |
| 54 | + const blockTag = args.length > 1 ? args[1] : DEFAULT_BLOCK_TAG; |
| 55 | + |
| 56 | + const blockHash = await parseBlockTag(indexer, ethProvider, blockTag); |
| 57 | + |
| 58 | + const watchedContract = indexer.getWatchedContracts().find(contract => contract.address === to); |
| 59 | + if (!watchedContract) { |
| 60 | + throw new ErrorWithCode(CODE_INVALID_PARAMS, ERROR_CONTRACT_NOT_RECOGNIZED); |
| 61 | + } |
| 62 | + |
| 63 | + const contractInterface = indexer.contractMap.get(watchedContract.kind); |
| 64 | + if (!contractInterface) { |
| 65 | + throw new ErrorWithCode(CODE_INTERNAL_ERROR, ERROR_CONTRACT_ABI_NOT_FOUND); |
| 66 | + } |
| 67 | + |
| 68 | + // Slice out method signature from data |
| 69 | + const functionSelector = data.slice(0, 10); |
| 70 | + |
| 71 | + // Find the matching function from the ABI |
| 72 | + const functionFragment = contractInterface.getFunction(functionSelector); |
| 73 | + if (!functionFragment) { |
| 74 | + throw new ErrorWithCode(CODE_INVALID_PARAMS, ERROR_CONTRACT_METHOD_NOT_FOUND); |
| 75 | + } |
| 76 | + |
| 77 | + // Decode the data based on the matched function |
| 78 | + const decodedData = contractInterface.decodeFunctionData(functionFragment, data); |
| 79 | + |
| 80 | + const functionName = functionFragment.name; |
| 81 | + const indexerMethod = (indexer as any)[functionName].bind(indexer); |
| 82 | + if (!indexerMethod) { |
| 83 | + throw new ErrorWithCode(CODE_SERVER_ERROR, ERROR_METHOD_NOT_IMPLEMENTED); |
| 84 | + } |
| 85 | + |
| 86 | + const result = await indexerMethod(blockHash, to, ...decodedData); |
| 87 | + const encodedResult = contractInterface.encodeFunctionResult(functionFragment, Array.isArray(result.value) ? result.value : [result.value]); |
| 88 | + |
| 89 | + callback(null, encodedResult); |
| 90 | + } catch (error: any) { |
| 91 | + let callBackError; |
| 92 | + if (error instanceof ErrorWithCode) { |
| 93 | + callBackError = { code: error.code, message: error.message }; |
| 94 | + } else { |
| 95 | + callBackError = { code: CODE_SERVER_ERROR, message: error.message }; |
| 96 | + } |
| 97 | + |
| 98 | + callback(callBackError); |
| 99 | + } |
| 100 | + }, |
| 101 | + |
| 102 | + eth_getLogs: async (args: any, callback: any) => { |
| 103 | + // TODO: Implement |
| 104 | + } |
| 105 | + }; |
| 106 | +}; |
| 107 | + |
| 108 | +const parseBlockTag = async (indexer: IndexerInterface, ethProvider: JsonRpcProvider, blockTag: string): Promise<string> => { |
| 109 | + if (utils.isHexString(blockTag)) { |
| 110 | + // Return value if hex string is of block hash length |
| 111 | + if (utils.hexDataLength(blockTag) === 32) { |
| 112 | + return blockTag; |
| 113 | + } |
| 114 | + |
| 115 | + // Treat hex value as a block number |
| 116 | + const block = await ethProvider.getBlock(blockTag); |
| 117 | + if (block === null) { |
| 118 | + throw new ErrorWithCode(CODE_INVALID_PARAMS, ERROR_BLOCK_NOT_FOUND); |
| 119 | + } |
| 120 | + |
| 121 | + return block.hash; |
| 122 | + } |
| 123 | + |
| 124 | + if (blockTag === DEFAULT_BLOCK_TAG) { |
| 125 | + const syncStatus = await indexer.getSyncStatus(); |
| 126 | + if (!syncStatus) { |
| 127 | + throw new ErrorWithCode(CODE_INTERNAL_ERROR, 'SyncStatus not found'); |
| 128 | + } |
| 129 | + |
| 130 | + return syncStatus.latestProcessedBlockHash; |
| 131 | + } |
| 132 | + |
| 133 | + throw new ErrorWithCode(CODE_INVALID_PARAMS, ERROR_INVALID_BLOCK_TAG); |
| 134 | +}; |
0 commit comments