def get_rates(fiat, cryptos): rates = {} for data in crypto_data: if data['code'] not in cryptos: continue try: services, response = _cached_fetch(service_mode="current_price", service_id="fallback", fiat=fiat, currency=data['code'], currency_name=data['name']) PriceTick.record_price( crypto=data['code'], fiat=fiat, source_name=response['service_name'], price=response['current_price'], ) rates[data['code']] = { 'rate': response['current_price'], 'provider': response['service_name'] } except NoService as exc: rates[data['code']] = { 'error': str(exc), } return rates
def get_rates(fiat, cryptos): rates = {} for data in crypto_data: if data['code'] not in cryptos: continue try: services, response = _cached_fetch( service_mode="current_price", service_id="fallback", fiat=fiat, currency=data['code'], currency_name=data['name'] ) PriceTick.record_price( crypto=data['code'], fiat=fiat, source_name=response['service_name'], price=response['current_price'], ) rates[data['code']] = { 'rate': response['current_price'], 'provider': response['service_name'] } except NoService as exc: rates[data['code']] = { 'error': str(exc), } return rates
def historical_price(request): fiat = request.GET['fiat'].upper() crypto = request.GET['currency'].upper() try: time = arrow.get(request.GET['time']).datetime except: return http.JsonResponse({'error': "Invalid Time argument"}, status=400) try: price = PriceTick.nearest(crypto, fiat, time) except PriceTick.DoesNotExist: return http.JsonResponse( { 'error': "Can't get historical price for %s->%s" % (fiat, crypto) }, status=400) try: naive_time = time.replace(tzinfo=None) price['estimated_supply'] = SupplyEstimator(crypto).calculate_supply( at_time=naive_time) except NotImplementedError: pass price['currency'] = crypto return http.JsonResponse(price)
def historical_price(request): fiat = request.GET['fiat'].upper() crypto = request.GET['currency'].upper() try: time = arrow.get(request.GET['time']).datetime except: return http.JsonResponse({'error': "Invalid Time argument"}, status=400) try: price = PriceTick.nearest(crypto, fiat, time) except PriceTick.DoesNotExist: return http.JsonResponse( {'error': "Can't get historical price for %s->%s" % (fiat, crypto)}, status=400 ) try: naive_time = time.replace(tzinfo=None) price['estimated_supply'] = SupplyEstimator(crypto).calculate_supply(at_time=naive_time) except NotImplementedError: pass price['currency'] = crypto return http.JsonResponse(price)
def get_historical_fiat(cls, crypto, fiat, time): try: return PriceTick.nearest(crypto, fiat, time) except PriceTick.DoesNotExist: return None
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
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