Skip to content

Commit

Permalink
index mainnet gas to sepolia contract
Browse files Browse the repository at this point in the history
  • Loading branch information
noahlitvin committed Aug 13, 2024
1 parent b62e1da commit f88fe2f
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
79 changes: 79 additions & 0 deletions packages/data/getBlockByTimestamp.ts
Original file line number Diff line number Diff line change
@@ -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<typeof createClient>, timestamp: number): Promise<Block> {
// 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);
3 changes: 2 additions & 1 deletion packages/data/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit f88fe2f

Please sign in to comment.