def index_base_info(request): """ Trias Blockchain BaseInfo :param request: """ transactions_history = OrderedDict() data = {} try: # 24-hour trading rate and 7-day trading history transactionRate = 0 for i in range(7): nowtime = time.time() end = int( time.mktime( datetime.datetime.fromtimestamp( nowtime).date().timetuple()) - 86400 * i + 86400) # i days ago timestamp start = int( time.mktime( datetime.datetime.fromtimestamp( nowtime).date().timetuple()) - 86400 * i + 1) # i+1 days ago timestamp x = time.strftime("%m/%d", time.localtime(start)) tx_count = TransactionInfo.objects.filter( Q(timestamp__lte=end) & Q(timestamp__gte=start)).count() if i == 0: transactionRate = round(tx_count / 24, 2) transactions_history[x] = tx_count data['transactionsHistory'] = transactions_history data['transactionRate'] = transactionRate data['addresses'] = Address.objects.count() data['transactions'] = TransactionInfo.objects.count() data['lastBlock'] = Block.objects.last( ).number if Block.objects.exists() else 0 data['lastBlockFees'] = 'None' data['lastTransactionFees'] = 'None' data['totalDifficulty'] = 'None' # The number of active addresses in the last 7 days addr_end = int(time.time()) addr_satrt = int(time.time() - 86400 * 7) data['unconfirmed'] = Address.objects.filter( Q(time__lte=addr_end) & Q(time__gte=addr_satrt)).count() # Address balance ranking richList = [] addresses = Address.objects.all().order_by('-time', '-id') if addresses.exists(): richList = list(addresses.values('address', 'balance', 'time'))[:8] for addr in richList: addr['time'] = stamp2datetime(addr['time']) data['richList'] = richList except Exception as e: logger.error(e) return JsonResponse({"code": 200, "return_data": data})
def address_info(request): """ Address Detail Info :param address: address id :return: """ address = request.GET.get('address') if not address: return JsonResponse({"code": 201, "message": "Need Address ID"}) try: data = list(Address.objects.filter(address=address).values())[0] data['time'] = stamp2datetime(data['time']) data['txCount'] = TransactionInfo.objects.filter( Q(source=address) | Q(to=address)).count() sent = 0 received = 0 sent_list = TransactionInfo.objects.filter(source=address).values_list( 'value', flat=True) for sent_value in sent_list: sent += int(sent_value) received_list = TransactionInfo.objects.filter(to=address).values_list( 'value', flat=True) for rec_value in received_list: received += int(rec_value) data['sent'] = sent data['received'] = received except Exception as e: logger.error(e) return JsonResponse({"code": 201, "message": "Address Error"}) return JsonResponse({"code": 200, "return_data": data})
def address_transactions(request): """ Transactions Of Address :param address: :param size: :param page: :return: """ size = request.GET.get("size", 3) page = request.GET.get("page", 1) address = request.GET.get("address") if not address: return JsonResponse({"code": 201, "message": "Need Address ID"}) try: size = int(size) page = int(page) if size <= 0 or page <= 0: size = 3 page = 1 except Exception as e: size = 3 page = 1 try: transactions = TransactionInfo.objects.filter( Q(source=address) | Q(to=address)).order_by('-blockNumber') pag = Paginator(transactions, size) if page > pag.num_pages: page = 1 data = list( pag.page(page).object_list.values('hash', 'gasPrice', 'source', 'value', 'to', 'gasUsed', 'blockNumber')) for item in data: item['time'] = stamp2datetime( Block.objects.get(number=item['blockNumber']).timestamp) item['fees'] = item['gasPrice'] * item['gasUsed'] item['confirmations'] = Block.objects.last().number - \ item['blockNumber'] except Exception as e: logger.error(e) return JsonResponse({"code": 201, "message": "ERROR"}) return JsonResponse({ "code": 200, "total_size": len(transactions), "page": page, "total_page": pag.num_pages, "return_data": data })
def block_info(request): """ Block Detail Info :param block_hash: current block hash :return: """ block_hash = request.GET.get('block_hash') if not block_hash: return JsonResponse({"code": 201, "message": "Need Block Hash"}) try: block_info = Block.objects.filter(hash=block_hash).values('number', 'transactionsCount', 'timestamp', 'size', 'difficulty', 'nonce', 'parentHash', 'miner', 'gasLimit', 'gasUsed', 'blockReward') if block_info.exists(): block_info = list(block_info)[0] block_info['time'] = stamp2datetime(block_info['timestamp']) number = block_info['number'] else: block_info = url_data(url, "eth_getBlockByHash", [block_hash, True])['result'] block_info['time'] = stamp2datetime(hex2int(block_info['timestamp'])) block_info['transactionsCount'] = len(block_info['transactions']) number = hex2int(block_info['number']) block_info['number'] = number block_info['blockReward'] = 3 * 10 ** 18 block_info['confirmations'] = IndexInfo.objects.last().lastBlock - number if block_info['confirmations'] > 0: next_block_number = number + 1 block_info['nextHash'] = url_data(url, "eth_getBlockByNumber", [hex(next_block_number), True])['result'][ 'hash'] else: block_info['nextHash'] = 'N/A' except Exception as e: logger.error(e) return JsonResponse({"code": 201, "message": "ERROR"}) return JsonResponse({"code": 200, "return_data": block_info})
def all_transactions(request): """ Transactions List :param size: per page count :param page: current page :return: """ size = request.GET.get("size", 50) page = request.GET.get("page", 1) try: size = int(size) page = int(page) if size <= 0 or page <= 0: size = 50 page = 1 except Exception as e: size = 50 page = 1 try: total_data = TransactionInfo.objects.all().order_by( '-blockNumber', '-timestamp') pag = Paginator(total_data, size) if page > pag.num_pages: page = 1 data = list( pag.page(page).object_list.values('hash', 'source', 'to', 'value', 'blockNumber', 'blockHash', 'tx_str', 'type')) for item in data: number = item['blockNumber'] item['time'] = stamp2datetime( Block.objects.get(number=number).timestamp) except Exception as e: logger.error(e) return JsonResponse({"code": 201, "message": "ERROR"}) return JsonResponse({ "code": 200, "total_size": total_data.count(), "page": page, "total_page": pag.num_pages, "return_data": data })
def all_blocks(request): """ Blocks List :param size: per page count :param page: current page :return: """ size = request.GET.get("size", 50) page = request.GET.get("page", 1) try: size = int(size) page = int(page) if size <= 0 or page <= 0: size = 50 page = 1 except Exception as e: size = 50 page = 1 try: total_data = Block.objects.all().order_by('-number') pag = Paginator(total_data, size) if page > pag.num_pages: page = 1 data = list( pag.page(page).object_list.values('hash', 'number', 'transactionsCount', 'size', 'blockReward', 'timestamp')) for item in data: item['time'] = stamp2datetime(item['timestamp']) item['avgFee'] = 0.00332 except Exception as e: logger.error(e) return JsonResponse({"code": 201, "message": "ERROR"}) return JsonResponse({ "code": 200, "total_size": total_data.count(), "page": page, "total_page": pag.num_pages, "return_data": data })
def index_latest_blocks(request): """ Latest Blocks 20 Numbers :param request: :return: """ data = [] try: blocks = Block.objects.all().order_by('-number')[:20] if blocks.exists(): data = list( blocks.values('number', 'size', 'timestamp', 'hash', 'blockReward')) for item in data: item['time'] = stamp2datetime(item['timestamp']) except Exception as e: logger.error(e) return JsonResponse({"code": 200, "size": len(data), "return_data": data})
def address_info(request): """ Address Detail Info :param address: address id :return: """ address = request.GET.get('address') if not address: return JsonResponse({"code": 201, "message": "Need Address ID"}) try: data = list(Address.objects.filter(address=address).values())[0] data['time'] = stamp2datetime(data['time']) data['txCount'] = TransactionInfo.objects.filter( Q(source=address) | Q(to=address)).count() sent = 0 received = 0 sent_list = TransactionInfo.objects.filter(source=address).values_list( 'value', flat=True) for sent_value in sent_list: sent += int(sent_value) received_list = TransactionInfo.objects.filter(to=address).values_list( 'value', flat=True) for rec_value in received_list: received += int(rec_value) data['sent'] = sent data['received'] = received except Exception as e: logger.error(e) return JsonResponse({"code": 201, "message": "Address Error"}) # try: # url = 'http://' + jc.eth_ip + '/api/' # chain = simple_request(url, params={"method":"personal_listAccounts","params":"aa"}) # data['chain'] = chain.get('chain_id', '') # except Exception as e: # logger.error(e) return JsonResponse({"code": 200, "return_data": data})
def transaction_info(request): """ Transaction Detail Info :param tx_hash: current transaction hash :return: """ tx_hash = request.GET.get('tx_hash') if not tx_hash: return JsonResponse({"code": 201, "message": "Need Transaction Hash"}) data = [] try: data = list(TransactionInfo.objects.filter(hash=tx_hash).values())[0] number = data['blockNumber'] block = Block.objects.get(number=number) data['gasLimit'] = block.gasLimit data['time'] = stamp2datetime(block.timestamp) data['confirmations'] = IndexInfo.objects.last().lastBlock - number except Exception as e: logger.error(e) return JsonResponse({"code": 200, "return_data": data})
def index_recent_transactions(request): """ Recent Transactions 20 Numbers :param request: :return: """ data = [] try: transactions = TransactionInfo.objects.all().order_by( '-blockNumber')[:20] if transactions.exists(): data = list( transactions.values('source', 'to', 'hash', 'blockNumber')) for item in data: block_number = item['blockNumber'] item['time'] = stamp2datetime( Block.objects.get(number=block_number).timestamp) except Exception as e: logger.error(e) return JsonResponse({"code": 200, "size": len(data), "return_data": data})
def block_info(request): """ Block Detail Info :param block_hash: current block hash :return: """ block_hash = request.GET.get('block_hash') if not block_hash: return JsonResponse({"code": 201, "message": "Need Block Hash"}) try: block_info = Block.objects.filter(hash=block_hash).values( 'number', 'transactionsCount', 'timestamp', 'size', 'difficulty', 'nonce', 'parentHash', 'miner', 'gasLimit', 'gasUsed', 'blockReward') if not block_info.exists(): return JsonResponse({ "code": 201, "message": "The block doesn't exist" }) block_info = list(block_info)[0] block_info['time'] = stamp2datetime(block_info['timestamp']) number = block_info['number'] block_info['confirmations'] = Block.objects.last().number - number if block_info['confirmations'] > 0: next_block_number = number + 1 block_info['nextHash'] = Block.objects.get(number=number + 1).hash else: block_info['nextHash'] = 'N/A' except Exception as e: logger.error(e) return JsonResponse({"code": 201, "message": "ERROR"}) return JsonResponse({"code": 200, "return_data": block_info})