def get_blocks_by_indexes(index_start: str = 0, index_end: str = 'max', db: Any = None) -> None: """ Get multiple blocks by index range. Args: index_start: Beginning index. index_end: End index. db: Read-only database instance. """ try: int_index_start = int(index_start) except ValueError: return 'Start index {} couldn\'t be parsed'.format(index_start), 400 if index_end == 'max': with open('./data/progress.txt', 'r') as f: index_end, _ = f.read().split('\n') try: int_index_end = int(index_end) except ValueError: return 'End index {} couldn\'t be parsed'.format(index_end), 400 if int_index_start > int_index_end: return 'Start index larger than end index.', 400 gatherer = DatabaseGatherer(db) blocks = gatherer.get_blocks_by_indexes(index_start, index_end) if blocks is None: return 'No blocks in index range {}-{} have been found'.format( index_start, index_end), 404 return blocks
def get_token_transactions_by_addresses(addresses: List[str], time_from: str = '0', time_to: str = '', no_tx_list: str = '', db: Any = None) -> None: """ Get token transactions of multiple addresses. Args: address: Multiple Ethereum addresses. time_from: Beginning datetime to take transactions from. time_to: Ending datetime to take transactions from. val_from: Minimum transferred currency of the transactions. val_to: Maximum transferred currency of transactions. no_tx_list: Maximum transactions to gather. db: Read-only database instance. """ try: int_time_from = int(time_from) except ValueError: return 'Start time {} couldn\'t be parsed.'.format(time_from), 400 if time_to == '': time_to = str(int(time.time()) + 1000000) try: int_time_to = int(time_to) except ValueError: return 'End time {} couldn\'t be parsed.'.format(time_to), 400 if int_time_from > int_time_to: return 'Minimum time is larger than maximum time', 400 if no_tx_list == '': no_tx_list = str(1000000000000000000000000000000) try: int_no_tx_list = int(no_tx_list) except ValueError: return 'Maximum number of transactions {} couldn\'t be parsed.'.format(no_tx_list), 400 gatherer = DatabaseGatherer(db) transactions = [] for address in addresses: new_transactions = gatherer.get_token_txs_of_address(address.lower(), int_time_from, int_time_to, int_no_tx_list) if new_transactions is None: return 'Address {} has not been found'.format(address), 400 transactions += new_transactions if transactions == []: return 'No transactions of requested addresses found', 404 return transactions
def test_get_block_index(self): """Test validity of getting block index.""" db = rocksdb.DB(DB_PATH, rocksdb.Options(create_if_missing=True, max_open_files=10000), read_only=True) block_index = '25308' block_hash = '0x31a2bdcaed45ff75bd2d4803731b4200719f6fe1c07e3661f33e3a9a2c996a6e' gatherer = DatabaseGatherer(db) gathered_data = gatherer.get_block_hash_by_index(block_index) self.assertEqual(block_hash, gathered_data)
def get_transactions_by_bindex(block_index: str, db: Any = None) -> None: """ Get transactions of a block by its index. Args: block_index: Index of the block. db: Read-only database instance. """ gatherer = DatabaseGatherer(db) transactions = gatherer.get_transactions_of_block_by_index(block_index) if transactions is None: return 'Block with index {} not found'.format(block_index), 404 return transactions
def test_get_block(self): """Test validity of block data.""" db = rocksdb.DB(DB_PATH, rocksdb.Options(create_if_missing=True, max_open_files=10000), read_only=True) with open('tests/resources/block_data.txt', 'r') as f: compare_data = json.loads(f.read()) block_hash = '0x31a2bdcaed45ff75bd2d4803731b4200719f6fe1c07e3661f33e3a9a2c996a6e' gatherer = DatabaseGatherer(db) gathered_data = gatherer.get_block_by_hash(block_hash) self.assertEqual(compare_data, gathered_data)
def read_transaction(tx_hash: str, db: Any = None) -> None: """ Get transaction by its hash. Args: tx_hash: Hash of the transaction. db: Read-only database instance. """ gatherer = DatabaseGatherer(db) transaction = gatherer.get_transaction_by_hash(tx_hash.lower()) if transaction is None: return 'Transaction with hash {} not found'.format(tx_hash), 404 return transaction
def test_get_transactions_of_block_by_hash(self): """Test validity of transactions of a block selected by hash.""" db = rocksdb.DB(DB_PATH, rocksdb.Options(create_if_missing=True, max_open_files=10000), read_only=True) with open('tests/resources/block_hash_transactions.txt', 'r') as f: compare_data = json.loads(f.read()) block_hash = '0x57f8464fa5d5f8fc4f4926963e2df38afc4f4c378e933f46c7b3f727bc0c5dcb' gatherer = DatabaseGatherer(db) gathered_data = gatherer.get_transactions_of_block_by_hash(block_hash) self.assertEqual(compare_data, gathered_data)
def read_block(block_hash: str, db: Any = None) -> None: """ Read one block by its hash. Args: block_hash: Unique hash of the block. db: Read-only database instance. """ gatherer = DatabaseGatherer(db) block = gatherer.get_block_by_hash(block_hash.lower()) if block is None: return 'Block with hash {} not found'.format(block_hash), 404 return block
def get_transactions_by_bhash(block_hash: str, db: Any = None) -> None: """ Get transactions of a block by its hash. Args: block_hash: Hash of the block. db: Read-only database instance. """ gatherer = DatabaseGatherer(db) transactions = gatherer.get_transactions_of_block_by_hash(block_hash.lower()) if transactions is None: return 'Block with hash {} not found'.format(block_hash), 404 return transactions
def test_get_transaction(self): """Test validity of transaction data.""" db = rocksdb.DB(DB_PATH, rocksdb.Options(create_if_missing=True, max_open_files=10000), read_only=True) with open('tests/resources/tx_data.txt', 'r') as f: compare_data = json.loads(f.read()) tx_hash = '0x57f281bf8792353cdab545fe439410f0f6478e272e1dcc5748d87299d32373e7' gatherer = DatabaseGatherer(db) gathered_data = gatherer.get_transaction_by_hash(tx_hash) self.assertEqual(compare_data, gathered_data)
def get_balance(addr: str, db: Any = None) -> None: """ Get balance of an address. Args: address: Ethereum address. db: Read-only database instance. """ gatherer = DatabaseGatherer(db) balance = gatherer.get_balance(addr.lower()) if balance is None: return 'Address {} found'.format(addr), 404 return balance
def test_get_blocks_indexes(self): """Test validity of block data from a selected index range.""" db = rocksdb.DB(DB_PATH, rocksdb.Options(create_if_missing=True, max_open_files=10000), read_only=True) with open('tests/resources/block_data_indexrange.txt', 'r') as f: compare_data = json.loads(f.read()) index_start = 20 index_end = 30 gatherer = DatabaseGatherer(db) gathered_data = gatherer.get_blocks_by_indexes(index_start, index_end) self.assertEqual(compare_data, gathered_data)
def test_get_token(self): """Test validity returning a token and its transactions.""" db = rocksdb.DB(DB_PATH, rocksdb.Options(create_if_missing=True, max_open_files=10000), read_only=True) with open('tests/resources/token_data.txt', 'r') as f: compare_data = json.loads(f.read()) address = '0x9724a061eaff6e34f4127272bf84cab690f98339' gatherer = DatabaseGatherer(db) gathered_data = gatherer.get_token(address, 0, 999999999999, 9999999999999999999999999999) self.assertEqual(compare_data, gathered_data)
def test_get_transactions_of_block_by_index(self): """Test validity of transactions of a block selected by index.""" db = rocksdb.DB(DB_PATH, rocksdb.Options(create_if_missing=True, max_open_files=10000), read_only=True) with open('tests/resources/block_index_transactions.txt', 'r') as f: compare_data = json.loads(f.read()) block_index = '25829' gatherer = DatabaseGatherer(db) gathered_data = gatherer.get_transactions_of_block_by_index( block_index) self.assertEqual(compare_data, gathered_data)
def test_get_blocks_timerange(self): """Test validity of block data from a selected time range.""" db = rocksdb.DB(DB_PATH, rocksdb.Options(create_if_missing=True, max_open_files=10000), read_only=True) with open('tests/resources/block_data_timerange.txt', 'r') as f: compare_data = json.loads(f.read()) time_start = 1479653257 time_end = 1479653542 gatherer = DatabaseGatherer(db) gathered_data = gatherer.get_blocks_by_datetime( 100000, time_start, time_end) self.assertEqual(compare_data, gathered_data)
def test_get_transactions_of_address_limit(self): """Test validity of transactions of an address. Limit returned amount.""" db = rocksdb.DB(DB_PATH, rocksdb.Options(create_if_missing=True, max_open_files=10000), read_only=True) with open('tests/resources/address_transactions_filter_limit.txt', 'r') as f: compare_data = json.loads(f.read()) address = '0x004b7f28a01a9f9142b2fc818b22325c4c049166' gatherer = DatabaseGatherer(db) gathered_data = gatherer.get_transactions_of_address( address, 0, 99999999999, 0, 9999999999999999999999999999, 5) self.assertEqual(compare_data, gathered_data)
def get_token_transactions_by_address(address: str, time_from: str = '0', time_to: str = '', no_tx_list: str = '', db: Any = None) -> None: """ Get token transactions of an address. Args: address: Ethereum address. time_from: Beginning datetime to take transactions from. time_to: Ending datetime to take transactions from. no_tx_list: Maximum transactions to gather. db: Read-only database instance. """ try: int_time_from = int(time_from) except ValueError: return 'Start time {} couldn\'t be parsed.'.format(time_from), 400 if time_to == '': time_to = str(int(time.time()) + 1000000) try: int_time_to = int(time_to) except ValueError: return 'End time {} couldn\'t be parsed.'.format(time_to), 400 if int_time_from > int_time_to: return 'Minimum time is larger than maximum time', 400 if no_tx_list == '': no_tx_list = str(1000000000000000000000000000000) try: int_no_tx_list = int(no_tx_list) except ValueError: return 'Maximum number of transactions {} couldn\'t be parsed.'.format(no_tx_list), 400 gatherer = DatabaseGatherer(db) transactions = gatherer.get_token_txs_of_address(address.lower(), int_time_from, int_time_to, int_no_tx_list) if transactions is None: return 'No token transactions of address {} found'.format(address), 404 return transactions
def get_hash_by_index(block_index: str, db: Any = None) -> None: """ Get block hash by its index. Args: block_index: Index of the block. db: Read-only database instance. """ try: int(block_index) except ValueError: return 'Index {} couldn\'t be parsed'.format(block_index), 400 gatherer = DatabaseGatherer(db) block_hash = gatherer.get_block_hash_by_index(block_index) if block_hash is None: return 'Block with index {} not found'.format(block_index), 404 return block_hash
def get_token(addr: str, time_from: str = '0', time_to: str = '', no_tx_list: str = '', db: Any = None) -> None: """ Get information about a token specified by its address. Args: addr: Specified token address. time_from: Beginning datetime to take token transactions from. time_to: Ending datetime to take token transactions from. no_tx_list: Maximum transactions to gather. db: Read-only database instance. """ try: int_time_from = int(time_from) except ValueError: return 'Start time {} couldn\'t be parsed.'.format(time_from), 400 if time_to == '': time_to = str(int(time.time()) + 1000000) try: int_time_to = int(time_to) except ValueError: return 'End time {} couldn\'t be parsed.'.format(time_to), 400 if no_tx_list == '': no_tx_list = str(1000000000000000000000000000000) try: int_no_tx_list = int(no_tx_list) except ValueError: return 'Maximum number of transactions {} couldn\'t be parsed.'.format( no_tx_list), 400 gatherer = DatabaseGatherer(db) token = gatherer.get_token(addr.lower(), int_time_from, int_time_to, int_no_tx_list) if token is None: return 'Token contract with address {} not found'.format(addr), 404 return token
def get_blocks_by_time(limit: str = '0', block_start: str = '0', block_end: str = '', db: Any = None) -> None: """ Get multiple blocks by datetime range. Args: limit: Maximum blocks to gether. block_start: Beginning datetime. block_end: End datetime. db: Read-only database instance. """ if block_end == '': block_end = str(int(time.time()) + 1000000) try: int_limit = int(limit) except ValueError: return 'Limit {} couldn\'t be parsed'.format(limit), 400 try: int_block_start = int(block_start) except ValueError: return 'Start datetime {} couldn\'t be parsed'.format(block_start), 400 try: int_block_end = int(block_end) except ValueError: return 'Start datetime {} couldn\'t be parsed'.format(block_end), 400 if int_block_start > int_block_end: return 'Start datetime larger than end datetime.', 400 gatherer = DatabaseGatherer(db) blocks = gatherer.get_blocks_by_datetime(int_limit, int_block_start, int_block_end) if blocks is None: return 'No blocks in timeframe {} - {} have been found'.format( block_start, block_end), 404 return blocks
def read_addresses(addrs: List[str], time_from: str = '0', time_to: str = '', val_from: str = '0', val_to: str = '', no_tx_list: str = '', db: Any = None) -> None: """ Get information about multiple addresses, including their transactions. Args: address: Ethereum addresses. time_from: Beginning datetime to take transactions from. time_to: Ending datetime to take transactions from. val_from: Minimum transferred currency of the transactions. val_to: Maximum transferred currency of transactions. no_tx_list: Maximum transactions to gather. db: Read-only database instance. """ try: int_time_from = int(time_from) except ValueError: return 'Start time {} couldn\'t be parsed.'.format(time_from), 400 if time_to == '': time_to = str(int(time.time()) + 1000000) try: int_time_to = int(time_to) except ValueError: return 'End time {} couldn\'t be parsed.'.format(time_to), 400 try: int_val_from = int(val_from) except ValueError: return 'Minimum value {} couldn\'t be parsed.'.format(val_from), 400 if val_to == '': val_to = str(1000000000000000000000000000000) try: int_val_to = int(val_to) except ValueError: return 'Maximum value {} couldn\'t be parsed.'.format(val_to), 400 if int_time_from > int_time_to: return 'Minimum time is larger than maximum time', 400 if int_val_from > int_val_to: return 'Minimum value is larger than maximum value', 400 if no_tx_list == '': no_tx_list = str(1000000000000000000000000000000) try: int_no_tx_list = int(no_tx_list) except ValueError: return 'Maximum number of transactions {} couldn\'t be parsed.'.format( no_tx_list), 400 gatherer = DatabaseGatherer(db) full_addresses = [] for address in addrs: full_addresses.append( gatherer.get_address(address.lower(), int_time_from, int_time_to, int_val_from, int_val_to, int_no_tx_list)) if full_addresses == []: return 'None of the requested addresses found', 404 return full_addresses