@@ -51,6 +51,8 @@ import { nestedCountersInstance } from './utils/nestedCounters'
5151import { trySpendServicePoints } from './utils/servicePoints'
5252import { archiverAPI } from './external/Archiver'
5353import { TTLMap } from './utils/TTLMap'
54+ import { buildGetTransactionByBlockHashAndIndex } from './eth-handlers/eth_getTransactionByBlockHashAndIndex'
55+ import { buildGetTransactionByBlockNumberAndIndex } from './eth-handlers/eth_getTransactionByBlockNumberAndIndex'
5456
5557export const verbose = config . verbose
5658export const firstLineLogs = config . firstLineLogs
@@ -2381,162 +2383,32 @@ export const methods = {
23812383 callback ( null , result )
23822384 countSuccessResponse ( api_name , 'success' , 'TBD' )
23832385 } ,
2384- eth_getTransactionByBlockHashAndIndex : async function ( args : RequestParamsLike , callback : JSONRPCCallbackTypePlain ) {
2385- const api_name = 'eth_getTransactionByBlockHashAndIndex'
2386- nestedCountersInstance . countEvent ( 'endpoint' , api_name )
2387- if ( ! ensureArrayArgs ( args , callback ) ) {
2388- countFailedResponse ( api_name , 'Invalid params: non-array args' )
2389- return
2390- }
2391- const ticket = crypto
2392- . createHash ( 'sha1' )
2393- . update ( api_name + Math . random ( ) + Date . now ( ) )
2394- . digest ( 'hex' )
2395- logEventEmitter . emit ( 'fn_start' , ticket , api_name , performance . now ( ) )
2396- /* prettier-ignore */ if ( firstLineLogs ) { console . log ( 'Running eth_getTransactionByBlockHashAndIndex' , args ) }
2397-
2398- logEventEmitter . emit ( 'fn_end' , ticket , { success : true } , performance . now ( ) )
2399- let result : string | completeReadableReceipt | null | undefined = null
2400-
2401- try {
2402- const blockResp = await collectorAPI . getBlock ( args [ 0 ] , 'hash' , true )
2403- result = blockResp ?. transactions [ Number ( args [ 1 ] ) ]
2404- if ( result ) {
2405- callback ( null , result )
2406- countSuccessResponse ( api_name , 'success' , 'collector' )
2407- logEventEmitter . emit ( 'fn_end' , ticket , { success : true } , performance . now ( ) )
2408- return
2409- }
2410- } catch ( e ) {
2411- callback ( errorBusy )
2412- countFailedResponse ( api_name , 'exception in collectorAPI.getBlock' )
2413- logEventEmitter . emit ( 'fn_end' , ticket , { success : false } , performance . now ( ) )
2414- }
2415- const blockHash = args [ 0 ]
2416- const index = parseInt ( args [ 1 ] , 16 )
2417- //if (blockHash !== 'latest') blockHash = parseInt(blockHash, 16)
2418- if ( config . queryFromValidator && config . queryFromExplorer ) {
2419- const explorerUrl = config . explorerUrl
2420- try {
2421- const res = await axios . get ( `${ explorerUrl } /api/transaction?blockHash=${ blockHash } ` )
2422- if ( verbose ) {
2423- console . log ( 'url' , `${ explorerUrl } /api/transaction?blockHash=${ blockHash } ` )
2424- console . log ( 'res' , JSON . stringify ( res . data ) )
2425- }
2426-
2427- let result
2428- if ( res . data . success ) {
2429- if ( typeof index === 'number' && index >= 0 && index < res . data . transactions . length ) {
2430- // eslint-disable-next-line security/detect-object-injection
2431- result = extractTransactionObject ( res . data . transactions [ index ] , index )
2432- }
2433- } else result = null
2434-
2435- const nodeUrl = config . explorerUrl
2436- if ( verbose ) console . log ( 'TRANSACTION DETAIL' , result )
2437- callback ( null , result )
2438- countSuccessResponse ( api_name , 'success' , 'explorer' )
2439- logEventEmitter . emit (
2440- 'fn_end' ,
2441- ticket ,
2442- { nodeUrl, success : res . data . transactions . length ? true : false } ,
2443- performance . now ( )
2444- )
2445- } catch ( error ) {
2446- /* prettier-ignore */ if ( verbose ) console . log ( 'Error: eth_getTransactionByBlockHashAndIndex' , ( error as AxiosError ) . message )
2447- callback ( null , null )
2448- countFailedResponse ( api_name , 'exception in axios.get' )
2449- logEventEmitter . emit ( 'fn_end' , ticket , { success : false } , performance . now ( ) )
2450- }
2451- } else {
2452- console . log ( 'queryFromValidator and/or queryFromExplorer turned off. Could not process request' )
2453- callback ( null , null )
2454- countFailedResponse ( api_name , 'queryFromValidator and/or queryFromExplorer turned off' )
2455- logEventEmitter . emit ( 'fn_end' , ticket , { success : true } , performance . now ( ) )
2456- }
2457- callback ( null , result )
2458- countSuccessResponse ( api_name , 'success' , 'fallback' )
2459- logEventEmitter . emit ( 'fn_end' , ticket , { success : true } , performance . now ( ) )
2460- } ,
2461- eth_getTransactionByBlockNumberAndIndex : async function (
2462- args : RequestParamsLike ,
2463- callback : JSONRPCCallbackTypePlain
2464- ) {
2465- const api_name = 'eth_getTransactionByBlockNumberAndIndex'
2466- nestedCountersInstance . countEvent ( 'endpoint' , api_name )
2467- if ( ! ensureArrayArgs ( args , callback ) ) {
2468- countFailedResponse ( api_name , 'Invalid params: non-array args' )
2469- return
2470- }
2471- const ticket = crypto
2472- . createHash ( 'sha1' )
2473- . update ( api_name + Math . random ( ) + Date . now ( ) )
2474- . digest ( 'hex' )
2475- logEventEmitter . emit ( 'fn_start' , ticket , api_name , performance . now ( ) )
2476-
2477- let result : string | completeReadableReceipt | null | undefined = null
2478- try {
2479- const blockResp = await collectorAPI . getBlock ( args [ 0 ] , 'hex_num' , true )
2480- result = blockResp ?. transactions [ Number ( args [ 1 ] ) ]
2481- if ( result ) {
2482- callback ( null , result )
2483- countSuccessResponse ( api_name , 'success' , 'collector' )
2484- logEventEmitter . emit ( 'fn_end' , ticket , { success : true } , performance . now ( ) )
2485- return
2486- }
2487- } catch ( e ) {
2488- callback ( errorBusy )
2489- countFailedResponse ( api_name , 'exception in collectorAPI.getBlock' )
2490- logEventEmitter . emit ( 'fn_end' , ticket , { success : false } , performance . now ( ) )
2491- }
2492- /* prettier-ignore */ if ( firstLineLogs ) { console . log ( 'Running eth_getTransactionByBlockNumberAndIndex' , args ) }
2493- let blockNumber = args [ 0 ]
2494- const index = parseInt ( args [ 1 ] , 16 )
2495- if ( blockNumber !== 'latest' && blockNumber !== 'earliest' ) blockNumber = parseInt ( blockNumber , 16 )
2496- if ( blockNumber === 'earliest' ) blockNumber = 0
2497- if ( config . queryFromExplorer ) {
2498- const explorerUrl = config . explorerUrl
2499- try {
2500- const res = await axios . get ( `${ explorerUrl } /api/transaction?blockNumber=${ blockNumber } ` )
2501- if ( verbose ) {
2502- console . log ( 'url' , `${ explorerUrl } /api/transaction?blockNumber=${ blockNumber } ` )
2503- console . log ( 'res' , JSON . stringify ( res . data ) )
2504- }
2505-
2506- let result
2507- if ( res . data . success ) {
2508- if ( typeof index === 'number' && index >= 0 && index < res . data . transactions . length ) {
2509- // eslint-disable-next-line security/detect-object-injection
2510- result = extractTransactionObject ( res . data . transactions [ index ] , index )
2511- }
2512- } else result = null
2513-
2514- const nodeUrl = config . explorerUrl
2515- if ( verbose ) console . log ( 'TRANSACTION DETAIL' , result )
2516- callback ( null , result )
2517- countSuccessResponse ( api_name , 'success' , 'explorer' )
2518- logEventEmitter . emit (
2519- 'fn_end' ,
2520- ticket ,
2521- { nodeUrl, success : res . data . transactions . length ? true : false } ,
2522- performance . now ( )
2523- )
2524- } catch ( error ) {
2525- /* prettier-ignore */ if ( verbose ) console . log ( 'Error: eth_getTransactionByBlockNumberAndIndex' , ( error as AxiosError ) . message )
2526- callback ( null , null )
2527- countFailedResponse ( api_name , 'exception in axios.get' )
2528- logEventEmitter . emit ( 'fn_end' , ticket , { success : false } , performance . now ( ) )
2529- }
2530- } else {
2531- console . log ( 'queryFromExplorer turned off. Could not process request' )
2532- callback ( null , null )
2533- countFailedResponse ( api_name , 'queryFromExplorer turned off' )
2534- logEventEmitter . emit ( 'fn_end' , ticket , { success : true } , performance . now ( ) )
2535- }
2536- callback ( null , result )
2537- countSuccessResponse ( api_name , 'success' , 'fallback' )
2538- logEventEmitter . emit ( 'fn_end' , ticket , { success : true } , performance . now ( ) )
2539- } ,
2386+ eth_getTransactionByBlockHashAndIndex : buildGetTransactionByBlockHashAndIndex ( {
2387+ nestedCountersInstance,
2388+ ensureArrayArgs,
2389+ countFailedResponse,
2390+ logEventEmitter,
2391+ firstLineLogs,
2392+ collectorAPI,
2393+ extractTransactionObject,
2394+ countSuccessResponse,
2395+ config,
2396+ verbose,
2397+ errorBusy,
2398+ } ) ,
2399+ eth_getTransactionByBlockNumberAndIndex : buildGetTransactionByBlockNumberAndIndex ( {
2400+ nestedCountersInstance,
2401+ ensureArrayArgs,
2402+ countFailedResponse,
2403+ logEventEmitter,
2404+ firstLineLogs,
2405+ collectorAPI,
2406+ extractTransactionObject,
2407+ countSuccessResponse,
2408+ config,
2409+ verbose,
2410+ errorBusy,
2411+ } ) ,
25402412 eth_getTransactionReceipt : async function ( args : RequestParamsLike , callback : JSONRPCCallbackTypePlain ) {
25412413 const api_name = 'eth_getTransactionReceipt'
25422414 nestedCountersInstance . countEvent ( 'endpoint' , api_name )
0 commit comments