def get(self): value = request.args.get('value', type=str) if not value: return {}, 400 # Block or transaction hash if len(value) == 66: # Normalize hash value = value.lower() # Check if it is a transaction transaction = self.database.transactions.find_one({'hash': value}) if transaction: transaction = get_clean_transaction(transaction) # Retrieve the timestamp of block it belongs to block = self.database.blocks.find_one({'number': transaction['blockNumber']}) block_timestamp = block['timestamp'] transaction = {'timestamp': block_timestamp, **transaction} return get_output(transaction, 'transaction'), 200 # Check if it is a block block = self.database.blocks.find_one({'hash': value}) if block: block = get_clean_block(block) return get_output(block, 'block'), 200 return {}, 404 # Account address elif len(value) == 42: # Normalize hash value = value.lower() account = self.database.accounts.find_one({'address': value}) if account: account = get_clean_account(account) return get_output(account, 'account'), 200 return {}, 404 # Block height try: value = int(value) except ValueError: return {}, 400 block = self.database.blocks.find_one({'number': value}) if block: block = get_clean_block(block) return get_output(block, 'block'), 200 return {}, 404
def get(self): value = request.args.get('value', type=str) if not value: return {}, 400 # Block or transaction hash if len(value) == 66: # Normalize hash value = value.lower() # Check if it is a transaction transaction = self.database.transactions.find_one({'hash': value}) if transaction: clean_transaction(transaction) return get_output(transaction, 'transaction'), 200 # Check if it is a block block = self.database.blocks.find_one({'hash': value}) if block: clean_block(block) return get_output(block, 'block'), 200 return {}, 404 # Account address elif len(value) == 42: # Normalize hash value = value.lower() account = self.database.accounts.find_one({'address': value}) if account: clean_account(account) return get_output(account, 'account'), 200 return {}, 404 # Block height try: value = int(value) except ValueError: return {}, 400 block = self.database.blocks.find_one({'number': value}) if block: clean_block(block) return get_output(block, 'block'), 200 return {}, 404
def get(self): value = request.args.get('value', type=str) if not value: return {}, 400 transaction = self.database.transactions.find_one({'hash': value}) if transaction: clean_transaction(transaction) return get_output(transaction, 'transaction'), 200 return {}, 404
def get(self): value = request.args.get('value', type=str) if not value: return {}, 400 # Normalize hash value = value.lower() account = self.database.accounts.find_one({'address': value}) if not account: return {}, 404 clean_account(account) return get_output(account, 'account'), 200
def get(self): value = request.args.get('value', type=str) try: value = int(value) field = 'number' except ValueError: field = 'hash' if len(value) != 66: return {}, 400 block = self.database.blocks.find_one({field: value}) if block: block = get_clean_block(block, self.extra_data_format) return get_output(block, 'block'), 200 return {}, 404
def get(self): value = request.args.get('value', type=str) if not value: return {}, 400 transaction = self.database.transactions.find_one({'hash': value}) if transaction: transaction = get_clean_transaction(transaction) # Retrieve the timestamp of block it belongs to block = self.database.blocks.find_one( {'number': transaction['blockNumber']}) block_timestamp = block['timestamp'] transaction = {'timestamp': block_timestamp, **transaction} return get_output(transaction, 'transaction'), 200 return {}, 404
def get(self): limit = request.args.get('limit', type=int, default=25) if limit < 1: limit = 25 elif limit > 50: limit = 50 query = {} order = request.args.get('order', type=str, default='desc') from_ = request.args.get('from', type=int) if from_: if order == 'desc': query['number'] = {'$lt': from_} order = -1 elif order == 'asc': query['number'] = {'$gt': from_} order = 1 else: order = -1 collection = self.database.blocks result = collection.find(query, sort=[('number', order)]).limit(limit) blocks = [] for block in result: blocks.append({ 'timestamp': block['timestamp'], 'number': block['number'], 'hash': block['hash'], 'transactions': len(block['transactions']) }) # Reverse list if asc if order == 1: blocks = blocks[::-1] return get_output(blocks, 'latest blocks'), 200
def get(self): limit = request.args.get('limit', type=int, default=25) if limit < 1: limit = 25 elif limit > 50: limit = 50 query = {} order = request.args.get('order', type=str, default='desc') from_ = request.args.get('from', type=ObjectId) if from_: if order == 'desc': query['_id'] = {'$lt': from_} order = -1 elif order == 'asc': query['_id'] = {'$gt': from_} order = 1 else: order = -1 address = request.args.get('address', type=str) if address: # Normalize hash address = address.lower() query['$or'] = [{ 'from': address }, { 'to': address }, { 'contractAddress': address }] block = request.args.get('block', type=str) if block: # Block hash if len(block) == 66: # Normalize hash block = block.lower() query['blockHash'] = block else: try: block = int(block) query['blockNumber'] = block except ValueError: return {}, 400 result = self.database.transactions.find(query, sort=[('_id', order) ]).limit(limit) block_timestamps = {} transactions = [] for transaction in result: block = self.database.blocks.find_one( {'number': transaction['blockNumber']}) if not block['number'] in block_timestamps: block_timestamps[block['number']] = block['timestamp'] transaction['timestamp'] = block_timestamps[ transaction['blockNumber']] transaction = get_clean_transaction_row(transaction) transactions.append(transaction) # Reverse list if asc if order == 1: blocks = blocks[::-1] return get_output(transactions, 'latest transactions'), 200