def plot_supply(request): from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas from matplotlib.figure import Figure from matplotlib.dates import DateFormatter currency = request.GET['currency'] genesis = crypto_data[currency.lower()]['genesis_date'] currency_name = crypto_data[currency.lower()]['name'] s = SupplyEstimator(currency) try: max_block = crypto_data[ currency.lower()]['supply_data']['reward_ends_at_block'] end = s.estimate_date_from_height(max_block) except KeyError: end = genesis + datetime.timedelta(days=365 * 50) x = list(date_generator(genesis, end)) y = [s.calculate_supply(at_time=z) / 1e6 for z in x] fig = Figure() ax = fig.add_subplot(111) ax.plot_date(x, y, '-') ax.set_title("%s Supply" % currency_name) ax.grid(True) ax.xaxis.set_label_text("Date") ax.yaxis.set_label_text("%s Units (In millions)" % currency.upper()) ax.xaxis.set_major_formatter(DateFormatter('%Y-%m-%d')) fig.autofmt_xdate() canvas = FigureCanvas(fig) response = http.HttpResponse(content_type='image/png') canvas.print_png(response) return response
def plot_supply(request): from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas from matplotlib.figure import Figure from matplotlib.dates import DateFormatter currency = request.GET['currency'] genesis = crypto_data[currency.lower()]['genesis_date'] currency_name = crypto_data[currency.lower()]['name'] s = SupplyEstimator(currency) try: max_block = crypto_data[currency.lower()]['supply_data']['reward_ends_at_block'] end = s.estimate_date_from_height(max_block) except KeyError: end = genesis + datetime.timedelta(days=365 * 50) x = list(date_generator(genesis, end)) y = [s.calculate_supply(at_time=z)/1e6 for z in x] fig = Figure() ax = fig.add_subplot(111) ax.plot_date(x, y, '-') ax.set_title("%s Supply" % currency_name) ax.grid(True) ax.xaxis.set_label_text("Date") ax.yaxis.set_label_text("%s Units (In millions)" % currency.upper()) ax.xaxis.set_major_formatter(DateFormatter('%Y-%m-%d')) fig.autofmt_xdate() canvas = FigureCanvas(fig) response = http.HttpResponse(content_type='image/png') canvas.print_png(response) return response
def test_blocktime_adjustments(): sd = { 'method': 'standard', 'start_coins_per_block': 80, 'minutes_per_block': 2, 'blocktime_adjustments': [ [4, 1], [9, 3] ], 'full_cap': 336000000, 'blocks_per_era': 2100000, } genesis = datetime.datetime(2017, 1, 1) crypto_data['tst'] = { 'genesis_date': genesis, 'supply_data': sd } for s in [SupplyEstimator('tst'), SupplyEstimator(genesis_date=genesis, supply_data=sd)]: assert s.estimate_date_from_height(10) == datetime.datetime(2017, 1, 1, 0, 16) assert s.estimate_date_from_height(11) == datetime.datetime(2017, 1, 1, 0, 19) assert s.estimate_date_from_height(4) == datetime.datetime(2017, 1, 1, 0, 8) assert s.estimate_date_from_height(9) == datetime.datetime(2017, 1, 1, 0, 13) assert s.estimate_height_from_date(datetime.datetime(2017, 1, 1, 0, 16)) == 10 assert s.estimate_height_from_date(datetime.datetime(2017, 1, 1, 0, 19)) == 11 assert s.estimate_height_from_date(datetime.datetime(2017, 1, 1, 0, 8)) == 4 assert s.estimate_height_from_date(datetime.datetime(2017, 1, 1, 0, 13)) == 9
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 fetch_full_tx(cls, crypto, txid, fiat=None): freshly_fetched = False try: tx_obj = cls.objects.get(crypto=crypto, txid=txid) if tx_obj.content == "Pending": return None if tx_obj.content == "Failed": raise cls.DoesNotExist() tx = json.loads(tx_obj.content) if tx.get('confirmations', 0) == 0: raise cls.DoesNotExist() except cls.DoesNotExist: # not cached, fetch from API service. tx_obj, c = cls.objects.get_or_create(crypto=crypto, txid=txid) tx_obj.content = "Pending" tx_obj.save() try: services, tx = get_single_transaction(crypto, txid, random=True, report_services=True) except Exception as exc: tx_obj.content = "Failed" tx_obj.service_used = str(exc) tx_obj.save() raise freshly_fetched = True tx_obj.content = json.dumps(tx, default=datetime_to_iso) su = services[0] tx_obj.service_used = "(%s) %s" % (su.service_id, su.name) tx_obj.crypto = crypto tx_obj.save() # if existing_tx_data and existing_tx_data.get('counterparty', False): # tx['inputs'] = [] # tx['outputs'] = [] # # if existing_tx_data['amount'] > 0: # # send, add amount to outputs # which = "outputs" # else: # # receive, add amount to inputs # which = "inputs" # # tx[which] = [{}] # tx[which][0]['amount'] = existing_tx_data['amount'] / 1e8 # tx[which][0]['address'] = existing_tx_data['address'] time = arrow.get(tx['time']).datetime if fiat: tx['historical_price'] = cls.get_historical_fiat(crypto, fiat, time) if not freshly_fetched: try: tx['confirmations'] = SupplyEstimator(crypto).estimate_confirmations(time.replace(tzinfo=None)) except: pass tx['memos'] = Memo.get(txid=txid, crypto=crypto) return tx