Ejemplo n.º 1
0
def wait_for_confirmation(client: algod.AlgodClient,
                          transaction_id: str,
                          timeout: int = 100) -> Dict[str, str or int]:
    """
    Check for when the transaction is confirmed by the network. Once confirmed, return
    the transaction information.

    :param client -> ``algod.AlgodClient``: an algorand client object.
    :param transaction_id -> ``str``: id for the transaction.
    """
    start_round = client.status()["last-round"] + 1
    current_round = start_round

    while current_round < start_round + timeout:
        try:
            pending_txn = client.pending_transaction_info(transaction_id)
        except Exception:
            return
        if pending_txn.get("confirmed-round", 0) > 0:
            return pending_txn
        elif pending_txn["pool-error"]:
            raise Exception('pool error: {}'.format(pending_txn["pool-error"]))
        client.status_after_block(current_round)
        current_round += 1
    raise Exception(
        'pending tx not found in timeout rounds, timeout value = : {}'.format(
            timeout))
Ejemplo n.º 2
0
def order_summary(algod_client: algod.AlgodClient, trade_gtxn: list):

    current_round = algod_client.status()["last-round"]
    last_valid_round = trade_gtxn[2].transaction.last_valid_round
    remaning_rounds = last_valid_round - current_round
    if remaning_rounds <= 0:
        remaning_rounds = 'Buy order expired!'

    return f"""
Ejemplo n.º 3
0
def check_from_algod(args):
    global graceful_stop
    token, addr = token_addr_from_args(args)
    algod = AlgodClient(token, addr)
    if args.genesis or args.algod:
        gpath = args.genesis or os.path.join(args.algod, 'genesis.json')
        getGenesisVars(gpath)
    else:
        process_genesis(algod.algod_request("GET", "/genesis"))
    status = algod.status()
    iloadstart = time.time()
    indexer_headers = None
    gtaddr = args.gtaddr
    maxaddr = None
    if args.shard:
        minaddr, maxaddr = shard_bounds(args.shard)
        if minaddr is not None:
            gtaddr = algosdk.encoding.encode_address(minaddr)
    if args.indexer_token:
        indexer_headers = {'X-Indexer-API-Token': token}

    lastlog = time.time()
    i2a_checker = CheckContext(sys.stderr)
    # for each account in indexer, get it from algod, and maybe re-get it from indexer at the round algod had
    ia_queue = queue.Queue(maxsize=10)
    threads = []
    for i in range(args.threads):
        t = threading.Thread(target=compare_thread, args=(ia_queue, i2a_checker, algod, args.indexer, indexer_headers))
        t.start()
        threads.append(t)
    lastaddr = None
    for indexer_account in indexerAccounts(args.indexer, addrlist=(args.accounts and args.accounts.split(',')), headers=indexer_headers, gtaddr=gtaddr):
        lastaddr = indexer_account['address']
        if maxaddr is not None:
            la = algosdk.encoding.decode_address(lastaddr)
            if la > maxaddr:
                break
        ia_queue.put(indexer_account)
        if graceful_stop:
            break
        now = time.time()
        if (now - lastlog) > 5:
            with i2a_checker.lock:
                logger.info('%d match, %d neq, through about %s', i2a_checker.match, i2a_checker.neq, lastaddr)
            lastlog = now
    for t in threads:
        ia_queue.put(None) # signal end
    for t in threads:
        t.join()
    if graceful_stop:
        logger.info('last addr %s', lastaddr)
    return i2a_checker
def check_from_algod(args):
    gpath = args.genesis or os.path.join(args.algod, 'genesis.json')
    getGenesisVars(gpath)
    token, addr = token_addr_from_args(args)
    algod = AlgodClient(token, addr)
    status = algod.status()
    #logger.debug('status %r', status)
    iloadstart = time.time()
    if args.indexer:
        i2a = indexerAccounts(args.indexer,
                              addrlist=(args.accounts
                                        and args.accounts.split(',')))
        i2a_checker = CheckContext(i2a, sys.stderr)
    else:
        logger.warn('no indexer, nothing to do')
        return None, None
    iloadtime = time.time() - iloadstart
    logger.info('loaded %d accounts from indexer in %.1f seconds, %.1f a/s',
                len(i2a), iloadtime,
                len(i2a) / iloadtime)
    aloadstart = time.time()
    rawad = []
    for address in i2a.keys():
        niceaddr = algosdk.encoding.encode_address(address)
        rawad.append(algod.account_info(niceaddr))
    aloadtime = time.time() - aloadstart
    logger.info('loaded %d accounts from algod in %.1f seconds, %.1f a/s',
                len(rawad), aloadtime,
                len(rawad) / aloadtime)
    lastlog = time.time()
    count = 0
    for ad in rawad:
        niceaddr = ad['address']
        # fetch indexer account at round algod got it at
        na = indexerAccounts(args.indexer,
                             blockround=ad['round'],
                             addrlist=[niceaddr])
        i2a.update(na)
        microalgos = ad['amount-without-pending-rewards']
        address = algosdk.encoding.decode_address(niceaddr)
        i2a_checker.check(address, niceaddr, microalgos, ad.get('assets'),
                          ad.get('created-assets'), ad.get('created-apps'),
                          ad.get('apps-local-state'))
        count += 1
        now = time.time()
        if (now - lastlog) > 5:
            logger.info('%d accounts checked, %d mismatches', count,
                        len(i2a_checker.mismatches))
            lastlog = now
    return i2a_checker
Ejemplo n.º 5
0
def wait_for_confirmation(client: algod.AlgodClient, txid: str):
    """
    Utility function to wait until the transaction is confirmed before
    proceeding.
    """
    last_round = client.status().get("last-round")
    txinfo = client.pending_transaction_info(txid)

    while not txinfo.get("confirmed-round", -1) > 0:
        print(f"Waiting for transaction {txid} confirmation.")
        last_round += 1
        client.status_after_block(last_round)
        txinfo = client.pending_transaction_info(txid)

    print(
        f"Transaction {txid} confirmed in round {txinfo.get('confirmed-round')}."
    )
    return txinfo