Example #1
0
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()
Example #2
0
def api_show_addr_summary(request, version, addr):
    """ show the address summary for uri: /addr/<addr>

    response
    -------
    {
    "addrStr": "muqq4M9KHTf1kVHWiciLirMybfy22gEA1K",
    "balance": 381200.0273664,
    "balanceSat": 38120002736640,
    "totalReceived": 409500.029219,
    "totalReceivedSat": 40950002921900,
    "totalSent": 28300.0018526,
    "totalSentSat": 2830000185260,
    "unconfirmedBalance": 0,
    "unconfirmedBalanceSat": 0,
    "unconfirmedTxApperances": 0,
    "txApperances": 8205
    }
    """
    try:
        tx_type = request.GET.get('type', None)
        if tx_type:
            is_internal = True
        else:
            is_internal = False
        eth_addr = addr_translation.b58check_decode(addr)
        internal_list = provider_models.InternalTransaction.objects.filter(
            to_address=eth_addr)
        if internal_list:
            has_internal = True
        else:
            has_internal = False
        account = provider_models.Account.objects.filter(
            address=eth_addr).first()
        if account:
            # caculate the txlength
            txlength = account.transactions_number + account.missing_transactions_number
            balance = account.balance
            balanceSat = int(balance) / DECIMAL_SATOSHI
            result = {
                "addrStr": addr,
                "balance": balanceSat,
                "unconfirmedBalance": 0,
                "unconfirmedBalanceSat": 0,
                "unconfirmedTxApperances": 0,
                "txApperances": txlength,
                "is_internal": is_internal,
                "has_internal": has_internal
            }
            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()
Example #3
0
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()