def list(self, request): results = [] address = request.GET.get('address') if not address: raise Exception('address can not be None') start_date = request.GET.get('start_date') if not start_date: raise Exception('start date can not be None') response = HttpResponse(content_type='text/csv') name = f"cpchain-export-address-{address}" response['Content-Disposition'] = f'attachment; filename="{name}.csv"' writer = csv.writer(response) writer.writerow(['TxHash', 'Block', 'Timestamp', 'From', 'To', 'Value', 'TxFee']) # query filters filters = parse_txs_filter(request) # txs all_txs = txs_collection.find( filters, projection={'_id': False}).sort('_id', DESCENDING) for tx in all_txs: tx['value'] = currency.from_wei(tx['value'], 'ether') ts = dt.strftime(dt.fromtimestamp(tx['timestamp']), '%Y-%m-%d %H:%M:%S') writer.writerow([tx['hash'], tx['blockNumber'], ts, tx['from'], tx['to'], tx['value'], tx['txfee']]) return response
def retrieve(self, request, pk): """ 根据 hash 获取交易 """ search = pk.strip().lower() filters = {} if not search.startswith('0x'): search = '0x' + search filters = {"hash": search} if txs_collection.count_documents(filters) == 0: return Response({"error": "not found"}, status=404) tx_dict = txs_collection.find(filters, projection={'_id': False})[0] tx_dict['gasLimit'] = block_collection.find( {'number': tx_dict['blockNumber']})[0]['gasLimit'] tx_dict['gasPrice'] = format(tx_dict['gasPrice'] / 1e18, '.20f') tx_dict['txfee'] = format(tx_dict['txfee'], '.20f') tx_dict['value'] = currency.from_wei(tx_dict['value'], 'ether') try: input_data = cf.toText(hexstr=tx_dict['input']) input_data = input_data.replace('\\', r'\\') input_data = input_data.replace('`', r'\`') except Exception as e: input_data = tx_dict['input'] tx_dict['input_data'] = input_data if not tx_dict['to']: tx_dict['contract_address'] = contract_collection.find( {'txhash': search}, projection={'_id': False})[0]['address'] return Response(tx_dict)
def list(self, request): results = [] count = 0 page = 1 limit = 25 try: # query filters filters = parse_txs_filter(request) # blocks all_txs = txs_collection.find( filters, projection={'_id': False}).sort('_id', DESCENDING) count = txs_collection.count(filters) limit = min(int(request.GET.get('limit', 25)), 50) page = int(request.GET.get('page', 1)) p = Paginator(all_txs, limit, request=request) txs = p.page(page) txs.object_list = list(txs.object_list) for tx in txs.object_list: if not tx['to']: tx['contract'] = contract_collection.find( {'txhash': tx['hash']})[0]['address'] tx['value'] = currency.from_wei(tx['value'], 'ether') results = txs.object_list except Exception as e: log.error(e) return Response({'results': results, 'count': count, 'page': page, 'limit': limit})
def retrieve(self, request, pk): """ 通过指定 Number 或 Hash 获取区块信息 """ search = pk.strip().lower() filters = {} if len(search) < ADD_SIZE - 2: # search by number filters = {'number': int(search)} else: if not search.startswith('0x'): search = '0x' + search filters = {"hash": search} if block_collection.count_documents(filters) == 0: return Response({"error": "not found"}, status=404) block_dict = block_collection.find( filters, projection={'_id': False})[0] block_dict['txs_cnt'] = len(block_dict['transactions']) del block_dict['transactions'] block_dict['transactions'] = [] if block_dict['txs_cnt'] > 0: txs_from_block = txs_collection.find( {'blockNumber': int(block_dict['number'])}, projection={'_id': False}) txs = [] for tx in txs_from_block: if not tx['to']: tx['contract_address'] = contract_collection.find( {'txhash': tx['hash']})[0]['address'] tx['value'] = currency.from_wei(tx['value'], 'ether') txs.append(tx) block_dict['transactions'] = txs return Response(block_dict)
def retrieve(self, request, pk): """ 根据地址获取信息 """ address = pk raw_address = address try: raw_address = cf.toChecksumAddress(address.strip()) address = raw_address.lower() code = contract_collection.find( {'address': raw_address})[0]['code'] # code = cf.toHex(code) except Exception as e: code = '0x' try: txs_count = address_collection.find( {'address': address})[0]['txs_count'] except: txs_count = 0 balance = 'N/A' is_rnode = False try: if not NO_CHAIN_NODE: balance = currency.from_wei( cf.cpc.getBalance(raw_address), 'ether') # check if the address have locked 200k cpc in RNode contract if rnode_collection.find({"Address": address}).count() > 0: balance += 200000 is_rnode = True except Exception as e: print('cf connection error', e) balance = 'N/A' if code == '0x': proposer_history = block_collection.count( {'miner': address}) return Response({ 'address': raw_address, 'balance': balance, 'txs_count': txs_count, 'is_rnode': is_rnode, 'proposer_history': proposer_history }) else: creator = contract_collection.find( {'address': raw_address})[0]['creator'] return Response({ 'address': raw_address, 'balance': balance, 'txs_count': txs_count, 'code': code, 'creator': creator, })
def txs(req): # txs block = req.GET.get('block') if block == None: try: page = req.GET.get('page', 1) except PageNotAnInteger: page = 1 all_txs = txs_collection.find().sort('_id', DESCENDING) p = Paginator(all_txs, 25, request=req) txs = p.page(page) txs.object_list = list(txs.object_list) for tx in txs.object_list: if not tx['to']: tx['contract'] = contract_collection.find( {'txhash': tx['hash']})[0]['address'] tx['value'] = currency.from_wei(tx['value'], 'ether') return render(req, 'explorer/txs_list.html', {'txs': txs}) # block's type is string txs_from_block = txs_collection.find({'blockNumber': int(block)}) # page try: page = req.GET.get('page', 1) except PageNotAnInteger: page = 1 txs_count = txs_from_block.count() p = Paginator(txs_from_block, 25, request=req) txs = p.page(page) txs.object_list = list(txs.object_list) for tx in txs.object_list: if not tx['to']: tx['contract'] = contract_collection.find( {'txhash': tx['hash']})[0]['address'] tx['value'] = currency.from_wei(tx['value'], 'ether') return render(req, 'explorer/txs_list.html', {'txs': txs, 'blockNumber': block, 'txs_count': txs_count })
def tx(req, tx_hash): # tx from hash search = tx_hash.strip().lower() tx_dict = list(txs_collection.find({"hash": search}))[0] tx_dict['gasLimit'] = block_collection.find( {'number': tx_dict['blockNumber']})[0]['gasLimit'] tx_dict['gasPrice'] = format(tx_dict['gasPrice'] / 1e18, '.20f') tx_dict['txfee'] = format(tx_dict['txfee'], '.20f') tx_dict['value'] = currency.from_wei(tx_dict['value'], 'ether') try: input_data = cf.toText(hexstr=tx_dict['input']) input_data = input_data.replace('\\', r'\\') input_data = input_data.replace('`', r'\`') except Exception as e: input_data = tx_dict['input'] if not tx_dict['to']: contract = contract_collection.find({'txhash': tx_hash})[0]['address'] return render(req, 'explorer/tx_info.html', {'tx_dict': tx_dict, 'contract': contract, 'input': input_data}) return render(req, 'explorer/tx_info.html', {'tx_dict': tx_dict, 'input': input_data})
def address(req, address): raw_address = address try: raw_address = cf.toChecksumAddress(address.strip()) address = raw_address.lower() code = contract_collection.find({'address': raw_address})[0]['code'] # code = cf.toHex(code) except Exception as e: code = '0x' try: txs_count = address_collection.find( {'address': address})[0]['txs_count'] except: txs_count = 0 try: page = req.GET.get('page', 1) except PageNotAnInteger: page = 1 txs = txs_collection.find({'$or': [{'from': address}, {'to': address}]}).sort( 'timestamp', DESCENDING) p = Paginator(txs, 25, request=req, fix_count=txs_count, restrain=True) txs = p.page(page) txs.object_list = list(txs.object_list) timenow = int(time.time()) # set flag for d in txs.object_list: if d['from'] == d['to']: d['flag'] = 'self' elif d['from'] == address: d['flag'] = 'out' else: d['flag'] = 'in' # add contract address if not d['to']: d['contract'] = contract_collection.find( {'txhash': d['hash']})[0]['address'] d['value'] = currency.from_wei(d['value'], 'ether') d['timesince'] = timenow - d['timestamp'] # txs.sort(key=lambda x: x['timestamp'], reverse=True) balance = 'N/A' is_rnode = False try: if not NO_CHAIN_NODE: balance = currency.from_wei( cf.cpc.getBalance(raw_address), 'ether') # check if the address have locked 200k cpc in RNode contract if rnode_collection.find({"Address": address}).count() > 0: balance += 200000 is_rnode = True except Exception as e: print('cf connection error', e) balance = 'N/A' # latest 25 txs current = {'begin': (int(page) - 1) * 25 + 1, 'end': (int(page) - 1) * 25 + len(txs.object_list)} # current =1 if code == '0x': proposer_history = block_collection.count( {'miner': address}) return render(req, 'explorer/address.html', {'txs': txs, 'current': current, 'address': raw_address, 'balance': balance, 'txs_count': txs_count, 'is_rnode': is_rnode, 'proposer_history': proposer_history }) else: creator = contract_collection.find( {'address': raw_address})[0]['creator'] return render(req, 'explorer/contract.html', {'txs': txs, 'current': current, 'address': raw_address, 'balance': balance, 'txs_count': txs_count, 'code': code, 'creator': creator, })