def api_show_newblock(request, version): """Show the new block for uri: /newblock/ response ------- """ try: result = {} stats = provider_models.Statistics.objects.filter( sync_type=codes.SyncType.SYNC_PROGRAM.value).first() if not stats: result['height'] = 0 return http.JsonResponse(result) if stats.block_height: new_height = stats.block_height else: new_height = provider_services.get_current_height() if new_height: result['height'] = new_height return http.JsonResponse(result) return http.HttpResponseNotFound() except Exception, inst: print inst logger.exception("fail to show new block:%s" % str(inst)) return http.HttpResponseServerError()
def api_show_transactions_by_addresses(request, version, addrs=None): """Show the transaction list of multiple addressed for uri: /addrs/<addrs>/txs response ------- """ try: result = {} items = [] no_spent = False start_pos = None end_pos = None if request.method != 'POST': raise Exception("Require POST") addrs = request.POST.get('addrs') if not addrs: try: data = json.loads(request.body) if data: addrs = data['addrs'] except: pass if not addrs: logger.error("api_show_transactions_by_addresses:no addrs") raise Exception("no addrs") # other parameters no_spent = True if request.REQUEST.get('noSpent') == "1" else False start_pos = request.REQUEST.get('from') if start_pos: start_pos = int(start_pos) end_pos = request.REQUEST.get('to') if end_pos: end_pos = int(end_pos) addrs = addrs.split(',') current_height = provider_services.get_current_height() for addr in addrs: if no_spent: rs = provider_models.Transaction.objects.filter(to_address=addr) else: rs = provider_models.Transaction.objects.filter(Q(from_address=addr) | Q(to_address=addr)) objs = rs.skip(start_pos).limit(end_pos - start_pos) for obj in objs: tmp = __convert_transaction_to_json(obj) tmp['confirmations'] = current_height - obj.blockheight items.append(tmp) if start_pos != None and end_pos != None: result['from'] = start_pos result['to'] = end_pos else: result['from'] = 0 result['to'] = len(items) result['totalItems'] = len(items) result['items'] = items return http.JsonResponse(result) except Exception, inst: print inst logger.exception("fail to show transactions by addresses:%s" % str(inst)) return http.HttpResponseServerError()
def api_show_client_transactions(request, version): try: addr = request.GET.get('addr') if not addr: addr = request.GET.get('address') page_id = int(request.GET.get('pageNum', 1)) #if page_id > settings.MAX_PAGE_NUM: # page_id = settings.MAX_PAGE_NUM limit = int(request.GET.get('limit', settings.PAGE_SIZE)) if limit > settings.PAGE_SIZE: limit = settings.PAGE_SIZE total_page = 0 if addr: addr = addr.lower() tx_objs = provider_models.Transaction.objects.filter( Q(from_address=addr) | Q(to_address=addr)).order_by('-time').max_time_ms( settings.MAX_SELERY_TIME) account = provider_models.Account.objects.filter( address=addr).first() if not account: result = { "total": 0, "limit": limit, "page": page_id, "pages": 1, "docs": [] } return http.JsonResponse(result) cnt = account.transactions_number + account.missing_transactions_number if cnt == 0: total_page = 1 else: if cnt % limit != 0: total_page = (cnt / limit) + 1 else: total_page = (cnt / limit) objs = tx_objs.skip((page_id - 1) * limit).limit(limit) else: raise Exception("invalid parameter") txs = [] current_height = provider_services.get_current_height() for obj in objs: item = __convert_transaction_to_client_json(obj) item['confirmations'] = current_height - obj.blockheight txs.append(item) result = { "total": cnt, "limit": limit, "page": page_id, "pages": total_page, "docs": txs } return http.JsonResponse(result) except Exception, inst: print inst logger.exception("fail to show client transactions:%s" % str(inst)) return http.HttpResponseServerError()
def api_show_client_transaction(request, version): try: txid = request.GET.get('txid') if not txid: logger.error("no txid") return http.HttpResponseServerError() tx = provider_models.Transaction.objects.filter(txid=txid).first() if not tx: logger.error("no tx") return http.HttpResponseServerError() current_height = provider_services.get_current_height() item = __convert_transaction_to_client_json(tx) item['confirmations'] = current_height - tx.blockheight result = {"tx": item} return http.JsonResponse(result) except Exception, inst: logger.exception("fail to show client transaction:%s" % str(inst)) return http.HttpResponseServerError()
def api_get_sync(request, version): """ show the sync status summary for uri: /sync """ try: current_height = provider_services.get_current_height() today = datetime.datetime.now() + datetime.timedelta(minutes=-2) start_ts = (today - datetime.datetime(1970, 1, 1)).total_seconds() result = { "blockChainHeight": current_height, "height": current_height, "status": _("finished"), "syncPercentage": 100, "type": _("newton supernode"), "startTs": int(start_ts) * 1000, "syncedBlocks": current_height, } return http.JsonResponse(result) except Exception, inst: print inst logger.exception("fail to get sync:%s" % str(inst)) return http.HttpResponseServerError()
def api_show_block_info(request, version, blockhash): """Show the block info by block hash key for uri: /block/<hash> response ------ { "hash":"00000000086267f5c9cf3d6d44ade95203a3cf713139d98a7b0a3e98947d4ded", "size":201, "height":8118, "version":536870912, "merkleroot":"bf04109823a6dedda7e07e47b421c36f116cff5b89e13e3c96ab4bfbcad87b33", "tx":["bf04109823a6dedda7e07e47b421c36f116cff5b89e13e3c96ab4bfbcad87b33"], "time":1508238076, "nonce":2073973374, "bits":"1c14c83f", "difficulty":12.31803921, "chainwork":"00000000000000000000000000000000000000000000000000004a1dfddcdea5", "confirmations":1, "previousblockhash":"000000000b3a33446a9feddc9462312e2fa4f5ba0316672c4b6ab4a37ad90aa4", "reward":50, "isMainChain":True, "poolInfo":{} } """ try: current_height = provider_services.get_current_height() obj = provider_models.Block.objects.get(blockhash=blockhash) result = json.loads(obj.to_json()) result['hash'] = obj.id result['isMainChain'] = True result['confirmations'] = current_height - obj.height result['current_net'] = settings.CURRENT_NET validator_name, validator_url = handle_validator(result['validator']) result['validator_name'] = validator_name result['validator_url'] = validator_url return http.JsonResponse(result) except Exception, inst: print inst logger.exception("fail to show block info by hash:%s" % str(inst)) return http.HttpResponseServerError()
def api_show_newtx(request, version): """Show the new transaction for uri: /newtx/ response ------- """ try: objs = provider_models.Transaction.objects.order_by("-time")[0: 10] current_height = provider_services.get_current_height() txs = [] for obj in objs: item = __convert_transaction_to_json(obj) item['confirmations'] = current_height - obj.blockheight value_issac = item['value'] value = Decimal(value_issac) / 1000000000000000000 item['value'] = value txs.append(item) return http.JsonResponse({'txs': txs}) except Exception, inst: print inst logger.exception("fail to show new transaction:%s" % str(inst)) return http.HttpResponseServerError()
def api_get_status(request, version): """ show the status info for uri: /status """ current_height = provider_services.get_current_height() current_net = settings.CURRENT_NET result = { "info": { "blocks": current_height, "connections": 1, "difficulty": 0, "errors": "", "network": _("mainnet"), "protocolversion": 1, "proxy": "", "relayfee": 0.00001, "testnet": False, "timeoffset": 0, "version": 1, 'current_net': current_net } } return http.JsonResponse(result)
def api_show_transaction(request, version, txid): """ show the transcation for uri: /tx response ------- { "blockhash": "0000000009ca522f5611ecf15a46f808394b58b9d811951a5d62f9e94f7e7c81", "blockheight": 7642, "blocktime": 1508167763, "confirmations": 477, "isCoinBase": True, "locktime": 0, "size": 120, "time": 1508167763, "txid": "fb6b050d695bba36c00097cc74bd4744253e0ca8075dc7361f45c985c98412da", "valueOut": 50, "version": 1, "vin": [ { "coinbase": "02da1d00000000006266676d696e657220352e312e302d756e6b6e6f776e0004000000", "n": 0, "sequence": 4294967295 }, ], "vout": [ { "n": 0, "scriptPubKey": { "addresses": ["muqq4M9KHTf1kVHWiciLirMybfy22gEA1K"], "asm": "OP_DUP OP_HASH160 9d239fa987bbf55f160fd6cb370be033e4311cbe OP_EQUALVERIFY OP_CHECKSIG", "hex": "76a9149d239fa987bbf55f160fd6cb370be033e4311cbe88ac", "type": "pubkeyhash" }, "value": "50.00000000" }, ] } """ try: obj = provider_models.Transaction.objects.filter(txid=txid).first() if obj: current_height = provider_services.get_current_height() result = __convert_transaction_to_json(obj) result['confirmations'] = current_height - obj.blockheight from_contract = False to_contract = False from_contract_obj = provider_models.Contract.objects.filter(contract_address=result['from_address']) to_contract_obj = provider_models.Contract.objects.filter(contract_address=result['to_address']) if from_contract_obj: from_contract = True if to_contract_obj: to_contract = True result['from_contract'] = from_contract result['to_contract'] = to_contract from_address = addr_translation.address_encode(result['from_address']) to_address = addr_translation.address_encode(result['to_address']) result['from_addr'] = from_address result['to_addr'] = to_address value_issac = result['value'] value = Decimal(value_issac) / 1000000000000000000 result['value'] = value final_fees = result['fees'] * result['fees_price'] result['fees'] = final_fees res = get_internal_transaction_info(txid) if res['is_internal']: result['is_internal'] = True result['internal_info'] = res['internal_info'] else: result['is_internal'] = False return http.JsonResponse(result) else: return http.HttpResponseNotFound() except Exception, inst: print inst logger.exception("fail to show transaction:%s" % str(inst)) return http.HttpResponseServerError()
def api_show_transactions(request, version): """ show the transcations for uri: /txs response ------ { "pagesTotal": 1, "txs": [ { "blockhash": "0000000009ca522f5611ecf15a46f808394b58b9d811951a5d62f9e94f7e7c81", "blockheight": 7642, "blocktime": 1508167763, "confirmations": 477, "isCoinBase": True, "locktime": 0, "size": 120, "time": 1508167763, "txid": "fb6b050d695bba36c00097cc74bd4744253e0ca8075dc7361f45c985c98412da", "valueOut": 50, "version": 1, "vin": [ { "coinbase": "02da1d00000000006266676d696e657220352e312e302d756e6b6e6f776e0004000000", "n": 0, "sequence": 4294967295 },], "vout": [ { "n": 0, "scriptPubKey": { "addresses": ["muqq4M9KHTf1kVHWiciLirMybfy22gEA1K"], "asm": "OP_DUP OP_HASH160 9d239fa987bbf55f160fd6cb370be033e4311cbe OP_EQUALVERIFY OP_CHECKSIG", "hex": "76a9149d239fa987bbf55f160fd6cb370be033e4311cbe88ac", "type": "pubkeyhash" }, "value": "50.00000000"},] }, ] } """ try: tx_type = request.GET.get('type', None) has_internal = False blockhash = request.GET.get('block') addr = request.GET.get('addr') contract = request.GET.get('contract') if not addr: addr = request.GET.get('address') if addr: addr = addr_translation.b58check_decode(addr) page_id = int(request.GET.get('pageNum', 1)) if page_id > settings.MAX_PAGE_NUM: page_id = settings.MAX_PAGE_NUM limit = int(request.GET.get('limit', settings.PAGE_SIZE)) if limit > settings.PAGE_SIZE: limit = settings.PAGE_SIZE if not addr and contract: contract = addr_translation.b58check_decode(contract) addr = contract total_page = 0 if blockhash: objs = provider_models.Transaction.objects.filter(blockhash=blockhash).max_time_ms(settings.MAX_SELERY_TIME) block_obj = provider_models.Block.objects.filter(blockhash=blockhash).first() cnt = block_obj.txlength if cnt == 0: total_page = 1 else: if cnt % settings.PAGE_SIZE != 0: total_page = (cnt / settings.PAGE_SIZE) + 1 else: total_page = (cnt / settings.PAGE_SIZE) objs = objs.skip(page_id * settings.PAGE_SIZE).limit(limit) elif addr: tx_objs = provider_models.InternalTransaction.objects.filter(to_address=addr).order_by('-time').max_time_ms( settings.MAX_SELERY_TIME) if tx_objs: has_internal = True if has_internal and tx_type == 'internal': cnt = len(tx_objs) if cnt == 0: total_page = 1 else: if cnt % settings.PAGE_SIZE != 0: total_page = (cnt / settings.PAGE_SIZE) + 1 else: total_page = (cnt / settings.PAGE_SIZE) objs = tx_objs.skip(page_id * settings.PAGE_SIZE).limit(limit) else: tx_objs = provider_models.Transaction.objects.filter(Q(from_address=addr) | Q(to_address=addr)).order_by('-time').max_time_ms(settings.MAX_SELERY_TIME) account = provider_models.Account.objects.filter(address=addr).first() cnt = account.missing_transactions_number + account.transactions_number if cnt == 0: total_page = 1 else: if cnt % settings.PAGE_SIZE != 0: total_page = (cnt / settings.PAGE_SIZE) + 1 else: total_page = (cnt / settings.PAGE_SIZE) objs = tx_objs.skip(page_id * settings.PAGE_SIZE).limit(limit) else: raise Exception("invalid parameter") txs = [] current_height = provider_services.get_current_height() for obj in objs: if has_internal and tx_type == 'internal': item = {} from_contract_obj = provider_models.Contract.objects.filter(contract_address=obj.contract_address) to_contract_obj = provider_models.Contract.objects.filter(contract_address=obj.to_address) if from_contract_obj: item['from_contract'] = True else: item['from_contract'] = False if to_contract_obj: item['to_contract'] = True else: item['to_contract'] = False item['contract_address'] = addr_translation.address_encode(obj.contract_address) item['txid'] = obj.txid item['to_address'] = addr_translation.address_encode(obj.to_address) item['value'] = Decimal(obj.value) / DECIMAL_SATOSHI item['time'] = obj.time else: item = __convert_transaction_to_json(obj) item['confirmations'] = current_height - obj.blockheight from_contract = False to_contract = False from_contract_obj = provider_models.Contract.objects.filter(contract_address=item['from_address']) to_contract_obj = provider_models.Contract.objects.filter(contract_address=item['to_address']) if from_contract_obj: from_contract = True if to_contract_obj: to_contract = True item['from_contract'] = from_contract item['to_contract'] = to_contract from_address = addr_translation.address_encode(item['from_address']) to_address = addr_translation.address_encode(item['to_address']) item['from_addr'] = from_address item['to_addr'] = to_address value_issac = item['value'] value = Decimal(value_issac) / 1000000000000000000 item['value'] = value final_fees = item['fees'] * item['fees_price'] item['fees'] = final_fees item['is_internal'] = False res = get_internal_transaction_info(item['txid']) if res['is_internal']: item['is_internal_list'] = True else: item['is_internal_list'] = False txs.append(item) result = { "pagesTotal": total_page, "txs": txs } return http.JsonResponse(result) except Exception, inst: print inst logger.exception("fail to show transactions:%s" % str(inst)) return http.HttpResponseServerError()