Exemplo n.º 1
0
 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})
Exemplo n.º 2
0
 def list(self, request):
     results = []
     count = 0
     page = 1
     try:
         # blocks
         all_blocks = block_collection.find(
             projection={'_id': False, 'dpor': False}).sort('number', DESCENDING)
         count = block_collection.estimated_document_count()
         limit = min(int(request.GET.get('limit', 25)), 100)
         page = int(request.GET.get('page', 1))
         p = Paginator(all_blocks, limit, request=request)
         blocks = p.page(page)
         blocks.object_list = list(blocks.object_list)
         for b in blocks.object_list:
             if b['miner'].endswith('000000'):
                 b['impeach'] = True
             else:
                 b['impeach'] = False
             b['txs_cnt'] = len(b['transactions'])
             del b['transactions']
         results = blocks.object_list
     except Exception as e:
         log.error(e)
     return Response({'results': results, 'count': count, 'page': page})
Exemplo n.º 3
0
def committeeHistory(req):
    all_historys = proposer_history_collection.find().sort('Term', -1)
    try:
        page = req.GET.get('page', 1)
    except PageNotAnInteger:
        page = 1
    p = Paginator(all_historys, 3, request=req)
    historys = p.page(page)
    historys.object_list = list(historys.object_list)

    return render(req, 'explorer/ProposerHistory.html', {'historys': historys})
Exemplo n.º 4
0
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
                                                  })
Exemplo n.º 5
0
def blocks(req):
    # blocks
    all_blocks = block_collection.find().sort('number', DESCENDING)
    try:
        page = req.GET.get('page', 1)
    except PageNotAnInteger:
        page = 1
    p = Paginator(all_blocks, 25, request=req)
    blocks = p.page(page)
    blocks.object_list = list(blocks.object_list)
    for b in blocks.object_list:
        if b['miner'].endswith('000000'):
            b['impeach'] = True
        else:
            b['impeach'] = False

    return render(req, 'explorer/block_list.html', {'blocks': blocks})
Exemplo n.º 6
0
def proposer_history(req, address):
    address = address.lower()
    blocks_by_proposer = block_collection.find(
        {'miner': address}).sort('_id', DESCENDING)
    blocks_count = blocks_by_proposer.count()

    try:
        page = req.GET.get('page', 1)
    except PageNotAnInteger:
        page = 1
    p = Paginator(blocks_by_proposer, 25, request=req, fix_count=blocks_count)
    blocks = p.page(page)
    blocks.object_list = list(blocks.object_list)
    current = {'begin': (int(page) - 1) * 25 + 1,
               'end': (int(page) - 1) * 25 + len(blocks.object_list)}
    return render(req, 'explorer/proposer_history_list.html',
                  {'blocks': blocks, 'current': current, 'address': address, 'blocks_count': blocks_count})
Exemplo n.º 7
0
 def retrieve(self, request, pk):
     """ 获取 proposer 的出块历史
     """
     address = pk.lower()
     blocks_by_proposer = block_collection.find(
         {'miner': address}, projection={'_id': False, 'dpor': False}).sort('_id', DESCENDING)
     blocks_count = blocks_by_proposer.count()
     limit = int(request.GET.get('limit', 25))
     page = int(request.GET.get('page', 1))
     p = Paginator(blocks_by_proposer, limit,
                   request=request, fix_count=blocks_count)
     blocks = p.page(page)
     blocks.object_list = list(blocks.object_list)
     for b in blocks.object_list:
         b['txs_cnt'] = len(b['transactions'])
         del b['transactions']
     return Response({'results': blocks.object_list, 'page': page, 'address': address, 'count': blocks_count})
Exemplo n.º 8
0
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,
                                                      })