def get_block_range_for_date(provider_uri, date, output): """Outputs start and end blocks for given date.""" provider = get_provider_from_uri(provider_uri) web3 = Web3(provider) eth_service = EthService(web3) start_block, end_block = eth_service.get_block_range_for_date(date) with smart_open(output, 'w') as output_file: output_file.write('{},{}\n'.format(start_block, end_block))
def get_block_range_for_date(provider_uri, date, output, chain='ethereum'): """Outputs start and end blocks for given date.""" provider_uri = check_classic_provider_uri(chain, provider_uri) provider = get_provider_from_uri(provider_uri) web3 = build_web3(provider) eth_service = EthService(web3) start_block, end_block = eth_service.get_block_range_for_date(date) with smart_open(output, 'w') as output_file: output_file.write('{},{}\n'.format(start_block, end_block))
def get_partitions(start, end, partition_batch_size, provider_uri): """Yield partitions based on input data type.""" if is_date_range(start, end) or is_unix_time_range(start, end): if is_date_range(start, end): start_date = datetime.strptime(start, '%Y-%m-%d').date() end_date = datetime.strptime(end, '%Y-%m-%d').date() elif is_unix_time_range(start, end): if len(start) == 10 and len(end) == 10: start_date = datetime.utcfromtimestamp(int(start)).date() end_date = datetime.utcfromtimestamp(int(end)).date() elif len(start) == 13 and len(end) == 13: start_date = datetime.utcfromtimestamp(int(start) / 1e3).date() end_date = datetime.utcfromtimestamp(int(end) / 1e3).date() day = timedelta(days=1) provider = get_provider_from_uri(provider_uri) web3 = Web3(provider) eth_service = EthService(web3) while start_date <= end_date: batch_start_block, batch_end_block = eth_service.get_block_range_for_date( start_date) partition_dir = '/date={start_date!s}/'.format( start_date=start_date) yield batch_start_block, batch_end_block, partition_dir start_date += day elif is_block_range(start, end): start_block = int(start) end_block = int(end) for batch_start_block in range(start_block, end_block + 1, partition_batch_size): batch_end_block = batch_start_block + partition_batch_size - 1 if batch_end_block > end_block: batch_end_block = end_block padded_batch_start_block = str(batch_start_block).zfill(8) padded_batch_end_block = str(batch_end_block).zfill(8) partition_dir = '/start_block={padded_batch_start_block}/end_block={padded_batch_end_block}'.format( padded_batch_start_block=padded_batch_start_block, padded_batch_end_block=padded_batch_end_block, ) yield batch_start_block, batch_end_block, partition_dir else: raise ValueError( 'start and end must be either block numbers or ISO dates or Unix times' )
def get_new_eth_service(): web3 = Web3(HTTPProvider('https://mainnet.infura.io')) return EthService(web3)
parser = argparse.ArgumentParser( description='Outputs the start block and end block for a given date.') parser.add_argument( '-p', '--provider-uri', default=None, type=str, help='The URI of the web3 provider e.g. ' 'file://$HOME/Library/Ethereum/geth.ipc or https://mainnet.infura.io/') parser.add_argument('-d', '--date', required=True, type=lambda d: datetime.strptime(d, '%Y-%m-%d'), help='The date e.g. 2018-01-01.') parser.add_argument('-o', '--output', default='-', type=str, help='The output file. If not specified stdout is used.') args = parser.parse_args() provider = get_provider_from_uri(args.provider_uri) web3 = Web3(provider) eth_service = EthService(web3) start_block, end_block = eth_service.get_block_range_for_date(args.date) with smart_open(args.output, 'w') as output_file: output_file.write('{},{}'.format(start_block, end_block))
def get_new_eth_service(): provider_url = os.environ.get('PROVIDER_URL', 'https://mainnet.infura.io/v3/7aef3f0cd1f64408b163814b22cc643c') web3 = Web3(HTTPProvider(provider_url)) return EthService(web3)
type=str, help='The URI of the web3 provider e.g. ' 'file://$HOME/Library/Ethereum/geth.ipc or https://mainnet.infura.io') parser.add_argument('-s', '--start-timestamp', required=True, type=int, help='Start unix timestamp, in seconds.') parser.add_argument('-e', '--end-timestamp', required=True, type=int, help='End unix timestamp, in seconds.') parser.add_argument('-o', '--output', default='-', type=str, help='The output file. If not specified stdout is used.') args = parser.parse_args() provider = get_provider_from_uri(args.provider_uri) web3 = Web3(provider) eth_service = EthService(web3) start_block, end_block = eth_service.get_block_range_for_timestamps( args.start_timestamp, args.end_timestamp) with smart_open(args.output, 'w') as output_file: output_file.write('{},{}\n'.format(start_block, end_block))