From 58bb1ddbe9d0d94203e739b72c2a9b44c787ea6e Mon Sep 17 00:00:00 2001 From: Prathamesh Musale Date: Thu, 29 Aug 2024 10:23:58 +0530 Subject: [PATCH] Export curl requests for given params to a file --- packages/test/README.md | 8 ++++ packages/test/src/eth-get-logs.ts | 75 +++++++++++++++++++++++++------ 2 files changed, 69 insertions(+), 14 deletions(-) diff --git a/packages/test/README.md b/packages/test/README.md index 74d67367..09619098 100644 --- a/packages/test/README.md +++ b/packages/test/README.md @@ -74,3 +74,11 @@ ```bash yarn get-storage-at -e http://127.0.0.1:8545 -c 0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f -s 0x1 -b 0xB5FFFF ``` + +## Get Logs Requests + +* Run: + + ```bash + yarn eth-get-logs -i -o -c -e http://127.0.0.1:1234/rpc/v1 + ``` diff --git a/packages/test/src/eth-get-logs.ts b/packages/test/src/eth-get-logs.ts index 2b100dce..826106ed 100644 --- a/packages/test/src/eth-get-logs.ts +++ b/packages/test/src/eth-get-logs.ts @@ -3,6 +3,7 @@ import * as fs from 'fs'; import * as path from 'path'; import debug from 'debug'; import yargs from 'yargs'; +import assert from 'assert'; const log = debug('vulcanize:test'); @@ -14,21 +15,43 @@ interface LogParams { blockHash?: string; } -// Function to format milliseconds into minutes and seconds +// Format time in milliseconds into minutes and seconds function formatTime(ms: number): string { const minutes = Math.floor(ms / 60000); const seconds = ((ms % 60000) / 1000).toFixed(0); return `${minutes}m${seconds}s`; } -async function getLogs(provider: providers.JsonRpcProvider, logParams: LogParams[], outputFilePath: string) { +async function generateCurlCommand(rpcEndpoint: string, params: LogParams): Promise { + const curlParams: any = { + address: params.address, + topics: params.topics + }; + + if (params.blockHash) { + curlParams.blockHash = params.blockHash; + } else { + curlParams.fromBlock = params.fromBlock; + curlParams.toBlock = params.toBlock; + } + + const requestBody = { + jsonrpc: "2.0", + method: "eth_getLogs", + params: [curlParams], + id: 1 + }; + + const curlCommand = `time curl -X POST -H "Content-Type: application/json" \\\n-d '${JSON.stringify(requestBody, null, 2)}' \\\n${rpcEndpoint}`; + return curlCommand; +} + +async function getLogs(provider: providers.JsonRpcProvider, logParams: LogParams[], outputFilePath: string, curlRequestsOutputFilePath: string) { for (const params of logParams) { + // Result object let result: any = {}; try { - // Record the start time - const startTime = Date.now(); - // Build the filter object const filter: any = { address: params.address.map(address => address.toLowerCase()), @@ -43,20 +66,30 @@ async function getLogs(provider: providers.JsonRpcProvider, logParams: LogParams let blockNumber: number; if (params.blockHash) { filter.blockHash = params.blockHash; + result.blockHash = params.blockHash; const block = await provider.getBlock(params.blockHash); blockNumber = block.number; + result.blockNumber = blockNumber; } else { + assert(params.toBlock && params.fromBlock, 'fromBlock or toBlock not found'); + filter.fromBlock = params.fromBlock; filter.toBlock = params.toBlock; - blockNumber = parseInt(params.toBlock!, 16); - result.blocksRange = parseInt(params.toBlock!, 16) - parseInt(params.fromBlock!, 16); + result.fromBlock = params.fromBlock; + result.toBlock = params.toBlock; + + blockNumber = parseInt(params.toBlock, 16); + result.blocksRange = parseInt(params.toBlock, 16) - parseInt(params.fromBlock, 16); } const latestBlockNumber = await provider.getBlockNumber(); result.blocksBehindHead = latestBlockNumber - blockNumber + // Record the start time + const startTime = Date.now(); + // Fetch logs using the filter const ethLogs = await provider.send( 'eth_getLogs', @@ -80,9 +113,10 @@ async function getLogs(provider: providers.JsonRpcProvider, logParams: LogParams } finally { let existingData = []; + // Read existing outputfile if (fs.existsSync(outputFilePath)) { const data = fs.readFileSync(outputFilePath, 'utf-8'); - existingData = JSON.parse(data); + existingData = JSON.parse(data || '[]'); } // Append new result to existing data @@ -90,6 +124,10 @@ async function getLogs(provider: providers.JsonRpcProvider, logParams: LogParams // Write the updated data back to the JSON file fs.writeFileSync(outputFilePath, JSON.stringify(existingData, null, 2)); + + // Generate the curl command and write it to a file + const curlCommand = await generateCurlCommand('http://localhost:1234/rpc/v1', params); + fs.appendFileSync(curlRequestsOutputFilePath, curlCommand + '\n\n'); } } } @@ -116,20 +154,29 @@ async function main() { describe: 'Output file path', type: 'string' }, + curlRequestsOutput: { + alias: 'c', + demandOption: true, + describe: 'Output file path for curl requests', + type: 'string' + }, }).argv; - // Read the JSON file + const outputFilePath = path.resolve(argv.output); + const curlRequestsOutputFilePath = path.resolve(argv.curlRequestsOutput); + + // Read the input json file const logParams: LogParams[] = JSON.parse(fs.readFileSync(path.resolve(argv.input), 'utf-8')); - const outputFilePath = path.resolve(argv.output); - - // Get logs and measure performance + // Create a provider with sufficient timeout const timeout = 10 * 60 * 1000; // 10mins const provider = new providers.JsonRpcProvider({ url: argv.endpoint, timeout }); - await getLogs(provider, logParams, outputFilePath); + // Get logs and measure performance + await getLogs(provider, logParams, outputFilePath, curlRequestsOutputFilePath); - console.log(`Results written to ${outputFilePath}`); + log(`Results written to ${outputFilePath}`); + log(`CURL requests written to ${curlRequestsOutputFilePath}`); } main().catch(err => {