|
| 1 | +import { AxiosError } from 'axios' |
| 2 | +import { RequestParamsLike } from 'jayson' |
| 3 | +import crypto from 'crypto' |
| 4 | +import { JSONRPCCallbackTypePlain } from 'jayson' |
| 5 | +import axios from 'axios' |
| 6 | +import NestedCounters from '../utils/nestedCounters' |
| 7 | +import { RequestMethod, requestWithRetry } from '../utils' |
| 8 | + |
| 9 | +interface BuildGetBlockTransactionCountByNumber { |
| 10 | + nestedCountersInstance: NestedCounters |
| 11 | + ensureArrayArgs: (args: RequestParamsLike, callback: JSONRPCCallbackTypePlain) => boolean |
| 12 | + countFailedResponse: (api: string, reason: string) => void |
| 13 | + logEventEmitter: any |
| 14 | + firstLineLogs: boolean |
| 15 | + collectorAPI: any |
| 16 | + countSuccessResponse: any |
| 17 | + config: any |
| 18 | + verbose: boolean |
| 19 | +} |
| 20 | + |
| 21 | +export const buildGetBlockTransactionCountByNumber = ({ |
| 22 | + nestedCountersInstance, |
| 23 | + ensureArrayArgs, |
| 24 | + countFailedResponse, |
| 25 | + logEventEmitter, |
| 26 | + firstLineLogs, |
| 27 | + collectorAPI, |
| 28 | + countSuccessResponse, |
| 29 | + config, |
| 30 | + verbose, |
| 31 | +}: BuildGetBlockTransactionCountByNumber): (( |
| 32 | + args: RequestParamsLike, |
| 33 | + callback: JSONRPCCallbackTypePlain |
| 34 | +) => Promise<void>) => { |
| 35 | + const eth_getBlockTransactionCountByNumber = async ( |
| 36 | + args: RequestParamsLike, |
| 37 | + callback: JSONRPCCallbackTypePlain |
| 38 | + ): Promise<void> => { |
| 39 | + const api_name = 'eth_getBlockTransactionCountByNumber' |
| 40 | + nestedCountersInstance.countEvent('endpoint', api_name) |
| 41 | + if (!ensureArrayArgs(args, callback)) { |
| 42 | + countFailedResponse(api_name, 'Invalid params: non-array args') |
| 43 | + return |
| 44 | + } |
| 45 | + const ticket = crypto |
| 46 | + .createHash('sha1') |
| 47 | + .update(api_name + Math.random() + Date.now()) |
| 48 | + .digest('hex') |
| 49 | + logEventEmitter.emit('fn_start', ticket, api_name, performance.now()) |
| 50 | + /* prettier-ignore */ if (firstLineLogs) { console.log('Running eth_getBlockTransactionCountByNumber', args) } |
| 51 | + |
| 52 | + let blockNumber = (args as any[])[0] |
| 53 | + |
| 54 | + // Check for unsupported block identifiers |
| 55 | + if (['pending', 'safe', 'finalized'].includes(blockNumber)) { |
| 56 | + const errorMessage = `Block identifier '${blockNumber}' is not supported` |
| 57 | + console.log(errorMessage) |
| 58 | + callback( |
| 59 | + { |
| 60 | + code: -32000, |
| 61 | + message: errorMessage, |
| 62 | + }, |
| 63 | + null |
| 64 | + ) |
| 65 | + countFailedResponse(api_name, errorMessage) |
| 66 | + logEventEmitter.emit('fn_end', ticket, { success: false, error: errorMessage }, performance.now()) |
| 67 | + return |
| 68 | + } |
| 69 | + |
| 70 | + if (!config.collectorSourcing.enabled && !config.queryFromExplorer) |
| 71 | + console.log('Both collectorSourcing and queryFromExplorer turned off. Could not process request') |
| 72 | + |
| 73 | + if (config.collectorSourcing.enabled || config.queryFromExplorer) { |
| 74 | + if (blockNumber !== 'latest' && blockNumber !== 'earliest') blockNumber = parseInt(blockNumber, 16).toString() |
| 75 | + if (blockNumber === 'latest' || blockNumber === 'earliest') { |
| 76 | + const res = await requestWithRetry(RequestMethod.Get, `/eth_getBlockByNumber?blockNumber=${blockNumber}`) |
| 77 | + if (res.data.block) blockNumber = res.data.block.number |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + if (config.collectorSourcing.enabled) { |
| 82 | + const res = await collectorAPI.getTransactionByBlock({ blockNumber, countOnly: true }) |
| 83 | + if (res !== null) { |
| 84 | + const result = '0x' + (res as number).toString(16) |
| 85 | + if (verbose) console.log('BLOCK TRANSACTIONS COUNT DETAIL', result) |
| 86 | + callback(null, result) |
| 87 | + countSuccessResponse(api_name, 'success', 'collector') |
| 88 | + logEventEmitter.emit('fn_end', ticket, { success: true }, performance.now()) |
| 89 | + return |
| 90 | + } |
| 91 | + } |
| 92 | + if (config.queryFromExplorer) { |
| 93 | + const explorerUrl = config.explorerUrl |
| 94 | + try { |
| 95 | + const url = `${explorerUrl}/api/transaction?blockNumber=${blockNumber}&countOnly=true` |
| 96 | + const res = await axios.get(url) |
| 97 | + if (verbose) { |
| 98 | + console.log('url', url) |
| 99 | + console.log('res', JSON.stringify(res.data)) |
| 100 | + } |
| 101 | + if (res.data.error) console.log('error', res.data.error) |
| 102 | + if (res.data.totalTransactions || res.data.totalTransactions === 0) { |
| 103 | + const result = '0x' + res.data.totalTransactions.toString(16) |
| 104 | + |
| 105 | + const nodeUrl = config.explorerUrl |
| 106 | + if (verbose) console.log('BLOCK TRANSACTIONS COUNT DETAIL', result) |
| 107 | + callback(null, result) |
| 108 | + countSuccessResponse(api_name, 'success', 'explorer') |
| 109 | + logEventEmitter.emit( |
| 110 | + 'fn_end', |
| 111 | + ticket, |
| 112 | + { nodeUrl, success: res.data.totalTransactions ? true : false }, |
| 113 | + performance.now() |
| 114 | + ) |
| 115 | + return |
| 116 | + } |
| 117 | + } catch (e) { |
| 118 | + if (verbose) console.log((e as AxiosError).message) |
| 119 | + } |
| 120 | + } |
| 121 | + callback(null, null) |
| 122 | + logEventEmitter.emit('fn_end', ticket, { success: false }, performance.now()) |
| 123 | + } |
| 124 | + |
| 125 | + return eth_getBlockTransactionCountByNumber |
| 126 | +} |
0 commit comments