Beispiel #1
0
    def fee(self, value=None, unit='satoshi'):
        """
        Set the miner fee, if unit is not set, assumes value is satoshi.
        If using 'optimal', make sure you have already added all outputs.
        """
        convert = None
        if not value:
            # no fee was specified, use $0.02 as default.
            convert = self.price_getter.action(self.crypto, "usd")
            self.fee_satoshi = int(0.02 / convert * 1e8)
            verbose = "Using default fee of:"

        elif value == 'optimal':
            self.fee_satoshi = get_optimal_fee(
                self.crypto, self.estimate_size(), verbose=self.verbose
            )
            verbose = "Using optimal fee of:"
        else:
            self.fee_satoshi = self.from_unit_to_satoshi(value, unit)
            verbose = "Using manually set fee of:"

        if self.verbose:
            if not convert:
                convert = self.price_getter.action(self.crypto, "usd")
            fee_dollar = convert * self.fee_satoshi / 1e8
            print(verbose + " %s satoshis ($%.2f)" % (self.fee_satoshi, fee_dollar))
Beispiel #2
0
    def fee(self, value=None, unit='satoshi'):
        """
        Set the miner fee, if unit is not set, assumes value is satoshi.
        If using 'optimal', make sure you have already added all outputs.
        """
        convert = None
        if not value:
            # no fee was specified, use $0.02 as default.
            convert = get_current_price(self.crypto, "usd")
            self.fee_satoshi = int(0.02 / convert * 1e8)
            verbose = "Using default fee of:"

        elif value == 'optimal':
            self.fee_satoshi = get_optimal_fee(self.crypto,
                                               self.estimate_size(),
                                               verbose=self.verbose)
            verbose = "Using optimal fee of:"
        else:
            self.fee_satoshi = self.from_unit_to_satoshi(value, unit)
            verbose = "Using manually set fee of:"

        if self.verbose:
            if not convert:
                convert = get_current_price(self.crypto, "usd")
            fee_dollar = convert * self.fee_satoshi / 1e8
            print(verbose + " %s satoshis ($%.2f)" %
                  (self.fee_satoshi, fee_dollar))
Beispiel #3
0
def _make_moneywagon_fetch(Service,
                           service_mode,
                           service_id,
                           address,
                           addresses,
                           xpub,
                           currency,
                           currency_name,
                           block_args,
                           fiat=None,
                           txid=None,
                           random_mode=False,
                           **k):

    if Service:
        if Service.supported_cryptos and currency not in Service.supported_cryptos:
            raise Exception("%s not supported for %s with %s" %
                            (currency_name, service_mode, Service.name))
        services = [Service]
    else:
        services = []  # fallback mode

    modes = dict(report_services=True,
                 services=services,
                 random=random_mode,
                 timeout=10.0,
                 verbose=settings.DEBUG)

    if service_id.startswith("paranoid"):
        modes['paranoid'] = int(service_id[8:])
    elif service_id.startswith("average"):
        modes['average'] = int(service_id[7:])
    elif service_id.startswith("private"):
        modes['private'] = int(service_id[7:])
        if modes['private'] > 30:
            raise Exception("Private mode maximum of 30")

    if address:
        modes['address'] = address
    elif addresses:
        modes['addresses'] = addresses.split(',')

    if service_mode == 'current_price':
        used_services, price = get_current_price(currency, fiat, **modes)
        PriceTick.record_price(price, currency, fiat, used_services[0].name)
        ret = {'current_price': price}
    elif service_mode == 'address_balance':
        used_services, balance = get_address_balance(currency, **modes)
        ret = {'balance': balance}
    elif service_mode == 'unspent_outputs':
        used_services, utxos = get_unspent_outputs(currency, **modes)
        ret = {'utxos': sorted(utxos, key=lambda x: x['output'])}
    elif service_mode == 'historical_transactions':
        used_services, txs = get_historical_transactions(currency, **modes)
        ret = {'transactions': sorted(txs, key=lambda x: x['txid'])}
    elif service_mode == 'single_transaction':
        used_services = None
        tx = CachedTransaction.fetch_full_tx(currency, txid=txid, fiat=fiat)
        ret = {'transaction': tx}
    elif service_mode == 'get_block':
        if block_args['block_number']:
            block_args['block_number'] = int(block_args['block_number'])
        modes.update(block_args)
        used_services, block_data = get_block(currency, **modes)
        ret = {'block': block_data}
    elif service_mode == 'optimal_fee':
        used_services, fee = get_optimal_fee(currency, 1024, **modes)
        ret = {'optimal_fee_per_KiB': fee}
    else:
        raise Exception("Unsupported Service mode")

    if not used_services:
        pass  # private mode does not return services
    elif len(used_services) == 1:
        s = used_services[0]
        if s:
            ret['url'] = s.last_url
            ret['raw_response'] = s.last_raw_response.json()
            ret['service_name'] = s.name
            ret['service_id'] = s.service_id
    else:
        ret['services'] = [{
            'name': s.name,
            'id': s.service_id,
            'raw_response': s.last_raw_response.json()
        } for s in used_services]

    return ret
Beispiel #4
0
def _make_moneywagon_fetch(Service, service_mode, service_id, address, addresses,
    xpub, currency, currency_name, block_args, fiat=None, txid=None, random_mode=False, **k):

    if Service:
        if Service.supported_cryptos and currency not in Service.supported_cryptos:
            raise Exception("%s not supported for %s with %s" % (
                currency_name, service_mode, Service.name
            ))
        services = [Service]
    else:
        services = [] # fallback mode

    modes = dict(
        report_services=True,
        services=services,
        random=random_mode,
        timeout=10.0,
        verbose=settings.DEBUG
    )

    if service_id.startswith("paranoid"):
        modes['paranoid'] = int(service_id[8:])
    elif service_id.startswith("average"):
        modes['average'] = int(service_id[7:])
    elif service_id.startswith("private"):
        modes['private'] = int(service_id[7:])
        if modes['private'] > 30:
            raise Exception("Private mode maximum of 30")

    if address:
        modes['address'] = address
    elif addresses:
        modes['addresses'] = addresses.split(',')

    if service_mode == 'current_price':
        used_services, price = get_current_price(currency, fiat, **modes)
        PriceTick.record_price(price, currency, fiat, used_services[0].name)
        ret = {'current_price': price}
    elif service_mode == 'address_balance':
        used_services, balance = get_address_balance(currency, **modes)
        ret = {'balance': balance}
    elif service_mode == 'unspent_outputs':
        used_services, utxos = get_unspent_outputs(currency, **modes)
        ret = {'utxos': sorted(utxos, key=lambda x: x['output'])}
    elif service_mode == 'historical_transactions':
        used_services, txs = get_historical_transactions(currency, **modes)
        ret = {'transactions': sorted(txs, key=lambda x: x['txid'])}
    elif service_mode == 'single_transaction':
        used_services = None
        tx = CachedTransaction.fetch_full_tx(currency, txid=txid, fiat=fiat)
        ret = {'transaction': tx}
    elif service_mode == 'get_block':
        if block_args['block_number']:
            block_args['block_number'] = int(block_args['block_number'])
        modes.update(block_args)
        used_services, block_data = get_block(currency, **modes)
        ret = {'block': block_data}
    elif service_mode == 'optimal_fee':
        used_services, fee = get_optimal_fee(currency, 1024, **modes)
        ret = {'optimal_fee_per_KiB': fee}
    else:
        raise Exception("Unsupported Service mode")

    if not used_services:
        pass # private mode does not return services
    elif len(used_services) == 1:
        s = used_services[0]
        if s:
            ret['url'] = s.last_url
            ret['raw_response'] = s.last_raw_response.json()
            ret['service_name'] = s.name
            ret['service_id'] = s.service_id
    else:
        ret['services'] = [
            {'name': s.name, 'id': s.service_id, 'raw_response': s.last_raw_response.json()}
            for s in used_services
        ]

    return ret