diff --git a/packages/data/getBlockByTimestamp.ts b/packages/data/getBlockByTimestamp.ts new file mode 100644 index 00000000..d6662cd5 --- /dev/null +++ b/packages/data/getBlockByTimestamp.ts @@ -0,0 +1,79 @@ +// Example usage: +// pnpm ts-node-dev getBlockByTimestamp.ts https://ethereum-rpc.publicnode.com 1722270000 + +import { createPublicClient, http, Block, Chain } from 'viem'; + +// Function to create a custom chain configuration +function createCustomChain(rpcUrl: string): Chain { + return { + id: 0, + name: 'Custom', + rpcUrls: { + default: { http: [rpcUrl] }, + public: { http: [rpcUrl] } + }, + nativeCurrency: { + decimals: 18, + name: 'Ether', + symbol: 'ETH' + } + }; +} + +// Function to create a public client using the provided RPC URL +function createClient(rpcUrl: string) { + const customChain = createCustomChain(rpcUrl); + return createPublicClient({ + chain: customChain, + transport: http() + }); +} + +async function getBlockByTimestamp(client: ReturnType, timestamp: number): Promise { + // Get the latest block number + const latestBlockNumber = await client.getBlockNumber(); + + // Get the latest block using the block number + const latestBlock = await client.getBlock({ blockNumber: latestBlockNumber }); + + // Initialize the binary search range + let low = 0n; + let high = latestBlock.number; + let closestBlock = latestBlock; + + // Binary search for the block with the closest timestamp + while (low <= high) { + const mid = (low + high) / 2n; + const block = await client.getBlock({ blockNumber: mid }); + + if (block.timestamp < timestamp) { + low = mid + 1n; + } else { + high = mid - 1n; + closestBlock = block; + } + } + + return closestBlock; +} + +// Get the RPC URL and timestamp from the command line arguments +const args = process.argv.slice(2); +if (args.length !== 2) { + console.error('Please provide both an RPC URL and a timestamp as arguments.'); + process.exit(1); +} + +const rpcUrl = args[0]; +const timestamp = Number(args[1]); + +if (isNaN(timestamp)) { + console.error('Invalid timestamp provided. Please provide a valid number.'); + process.exit(1); +} + +const client = createClient(rpcUrl); + +getBlockByTimestamp(client, timestamp).then(block => { + console.log(`Block number corresponding to timestamp ${timestamp} is ${block.number?.toString()}`); +}).catch(console.error); \ No newline at end of file diff --git a/packages/data/src/worker.ts b/packages/data/src/worker.ts index e348df09..ff9db70c 100644 --- a/packages/data/src/worker.ts +++ b/packages/data/src/worker.ts @@ -80,7 +80,8 @@ if(process.argv.length < 3) { } else { const args = process.argv.slice(2); if (args[0] === 'index-sepolia') { - indexBaseFeePerGasRangeCommand(6300000, 6500000, 'https://ethereum-rpc.publicnode.com', `${sepolia.id}:${FoilSepolia.address}`) + // Index mainnet gas to sepolia contract + indexBaseFeePerGasRangeCommand(20413376, 20428947, 'https://ethereum-rpc.publicnode.com', `${sepolia.id}:${FoilSepolia.address}`) //indexMarketEventsRangeCommand(1722270000, 1722458027, 'https://ethereum-rpc.publicnode.com', FoilSepolia.address, FoilSepolia.abi as Abi) } else if (args[0] === 'index-base-fee-per-gas') { const [start, end, rpcUrl, contractAddress] = args.slice(1);