예제 #1
0
    def _find_addresses(self, seed, index, count):
        """
    Find addresses matching the command parameters.
    """
        # type: (Seed, int, Optional[int]) -> List[Address]
        generator = AddressGenerator(seed)

        if count is None:
            # Connect to Tangle and find the first address without any
            # transactions.
            for addy in generator.create_iterator(start=index):
                response = FindTransactionsCommand(
                    self.adapter)(addresses=[addy])

                if not response.get('hashes'):
                    return [addy]

        return generator.get_addresses(start=index, count=count)
예제 #2
0
    def _find_addresses(self, seed, index, count, security_level, checksum):
        # type: (Seed, int, Optional[int], int, bool) -> List[Address]
        """
    Find addresses matching the command parameters.
    """
        generator = AddressGenerator(seed, security_level, checksum)

        if count is None:
            # Connect to Tangle and find the first address without any
            # transactions.
            for addy in generator.create_iterator(start=index):
                # We use addy.address here because FindTransactions does
                # not work on an address with a checksum
                response = FindTransactionsCommand(
                    self.adapter)(addresses=[addy.address])

                if not response.get('hashes'):
                    return [addy]

        return generator.get_addresses(start=index, count=count)
예제 #3
0
    def _find_transactions(self, **kwargs):
        # type: (dict) -> List[Transaction]
        """
    Finds transactions matching the specified criteria, fetches the
    corresponding trytes and converts them into Transaction objects.
    """
        ft_response = FindTransactionsCommand(self.adapter)(**kwargs)

        hashes = ft_response.get('hashes') or []

        if hashes:
            gt_response = GetTrytesCommand(self.adapter)(hashes=hashes)

            return list(
                map(
                    Transaction.from_tryte_string,
                    gt_response.get('trytes') or [],
                ))  # type: List[Transaction]

        return []
예제 #4
0
  def _find_addresses(self, seed, index, count, security_level, checksum):
    # type: (Seed, int, Optional[int], int, bool) -> List[Address]
    """
    Find addresses matching the command parameters.
    """
    generator = AddressGenerator(seed, security_level, checksum)

    if count is None:
      # Connect to Tangle and find the first address without any
      # transactions.
      for addy in generator.create_iterator(start=index):
        # We use addy.address here because FindTransactions does
        # not work on an address with a checksum
        response = FindTransactionsCommand(self.adapter)(
           addresses=[addy.address]
        )

        if not response.get('hashes'):
          return [addy]

    return generator.get_addresses(start=index, count=count)
예제 #5
0
    def _execute(self, request):
        stop = request['stop']  # type: Optional[int]
        seed = request['seed']  # type: Seed
        start = request['start']  # type: int
        threshold = request['threshold']  # type: Optional[int]

        generator = AddressGenerator(seed)

        # Determine the addresses we will be scanning.
        if stop is None:
            # This is similar to the ``getNewAddresses`` command, except it
            # is interested in all the addresses that `getNewAddresses`
            # skips.
            addresses = []  # type: List[Address]
            for addy in generator.create_generator(start):
                ft_response = FindTransactionsCommand(
                    self.adapter)(addresses=[addy])

                if ft_response.get('hashes'):
                    addresses.append(addy)
                else:
                    break
        else:
            addresses = generator.get_addresses(start, stop)

        # Load balances for the addresses that we generated.
        gb_response = GetBalancesCommand(self.adapter)(addresses=addresses)

        result = {
            'inputs': [],
            'totalBalance': 0,
        }

        threshold_met = threshold is None

        for i, balance in enumerate(gb_response['balances']):
            addresses[i].balance = balance

            if balance:
                result['inputs'].append(addresses[i])
                result['totalBalance'] += balance

                if (threshold
                        is not None) and (result['totalBalance'] >= threshold):
                    threshold_met = True
                    break

        if threshold_met:
            return result
        else:
            # This is an exception case, but note that we attach the result
            # to the exception context so that it can be used for
            # troubleshooting.
            raise with_context(
                exc=BadApiResponse(
                    'Accumulated balance {balance} is less than threshold {threshold} '
                    '(``exc.context`` contains more information).'.format(
                        threshold=threshold,
                        balance=result['totalBalance'],
                    ), ),
                context={
                    'inputs': result['inputs'],
                    'request': request,
                    'total_balance': result['totalBalance'],
                },
            )