2022-01-07 22:46:30 +00:00
|
|
|
|
2022-01-11 14:36:43 +00:00
|
|
|
import requests, ring, os
|
2022-01-07 22:46:30 +00:00
|
|
|
from decouple import config
|
2022-01-16 15:18:23 +00:00
|
|
|
import numpy as np
|
2022-01-15 12:00:11 +00:00
|
|
|
|
2022-01-18 18:24:45 +00:00
|
|
|
from api.models import Order
|
|
|
|
|
2022-01-11 14:36:43 +00:00
|
|
|
market_cache = {}
|
2022-01-10 01:12:58 +00:00
|
|
|
|
2022-01-18 13:20:19 +00:00
|
|
|
@ring.dict(market_cache, expire=3) #keeps in cache for 3 seconds
|
2022-01-16 15:18:23 +00:00
|
|
|
def get_exchange_rates(currencies):
|
2022-01-14 14:57:56 +00:00
|
|
|
'''
|
2022-01-16 15:18:23 +00:00
|
|
|
Params: list of currency codes.
|
2022-01-14 14:57:56 +00:00
|
|
|
Checks for exchange rates in several public APIs.
|
2022-01-16 15:18:23 +00:00
|
|
|
Returns the median price list.
|
2022-01-14 14:57:56 +00:00
|
|
|
'''
|
2022-01-15 00:28:19 +00:00
|
|
|
|
2022-01-14 14:57:56 +00:00
|
|
|
APIS = config('MARKET_PRICE_APIS', cast=lambda v: [s.strip() for s in v.split(',')])
|
|
|
|
|
2022-01-16 15:18:23 +00:00
|
|
|
api_rates = []
|
2022-01-14 14:57:56 +00:00
|
|
|
for api_url in APIS:
|
2022-01-16 15:18:23 +00:00
|
|
|
try: # If one API is unavailable pass
|
2022-01-14 14:57:56 +00:00
|
|
|
if 'blockchain.info' in api_url:
|
|
|
|
blockchain_prices = requests.get(api_url).json()
|
2022-01-16 15:18:23 +00:00
|
|
|
blockchain_rates = []
|
|
|
|
for currency in currencies:
|
|
|
|
try: # If a currency is missing place a None
|
|
|
|
blockchain_rates.append(float(blockchain_prices[currency]['last']))
|
|
|
|
except:
|
|
|
|
blockchain_rates.append(np.nan)
|
|
|
|
api_rates.append(blockchain_rates)
|
|
|
|
|
2022-01-14 14:57:56 +00:00
|
|
|
elif 'yadio.io' in api_url:
|
|
|
|
yadio_prices = requests.get(api_url).json()
|
2022-01-16 15:18:23 +00:00
|
|
|
yadio_rates = []
|
|
|
|
for currency in currencies:
|
|
|
|
try:
|
|
|
|
yadio_rates.append(float(yadio_prices['BTC'][currency]))
|
|
|
|
except:
|
|
|
|
yadio_rates.append(np.nan)
|
|
|
|
api_rates.append(yadio_rates)
|
2022-01-14 14:57:56 +00:00
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2022-01-16 15:18:23 +00:00
|
|
|
if len(api_rates) == 0:
|
|
|
|
return None # Wops there is not API available!
|
|
|
|
|
|
|
|
exchange_rates = np.array(api_rates)
|
|
|
|
median_rates = np.nanmedian(exchange_rates, axis=0)
|
|
|
|
|
|
|
|
return median_rates.tolist()
|
2022-01-11 14:36:43 +00:00
|
|
|
|
|
|
|
lnd_v_cache = {}
|
|
|
|
@ring.dict(lnd_v_cache, expire=3600) #keeps in cache for 3600 seconds
|
|
|
|
def get_lnd_version():
|
|
|
|
|
|
|
|
stream = os.popen('lnd --version')
|
|
|
|
lnd_version = stream.read()[:-1]
|
|
|
|
|
|
|
|
return lnd_version
|
|
|
|
|
|
|
|
robosats_commit_cache = {}
|
|
|
|
@ring.dict(robosats_commit_cache, expire=3600)
|
|
|
|
def get_commit_robosats():
|
|
|
|
|
|
|
|
stream = os.popen('git log -n 1 --pretty=format:"%H"')
|
|
|
|
lnd_version = stream.read()
|
|
|
|
|
|
|
|
return lnd_version
|
2022-01-18 18:24:45 +00:00
|
|
|
|
|
|
|
premium_percentile = {}
|
|
|
|
@ring.dict(premium_percentile, expire=300)
|
|
|
|
def compute_premium_percentile(order):
|
|
|
|
|
2022-01-19 20:55:24 +00:00
|
|
|
queryset = Order.objects.filter(currency=order.currency, status=Order.Status.PUB).exclude(id=order.id)
|
2022-01-18 18:24:45 +00:00
|
|
|
|
|
|
|
print(len(queryset))
|
|
|
|
if len(queryset) <= 1:
|
|
|
|
return 0.5
|
|
|
|
|
|
|
|
order_rate = float(order.last_satoshis) / float(order.amount)
|
|
|
|
rates = []
|
|
|
|
for similar_order in queryset:
|
|
|
|
rates.append(float(similar_order.last_satoshis) / float(similar_order.amount))
|
|
|
|
|
|
|
|
rates = np.array(rates)
|
|
|
|
return round(np.sum(rates < order_rate) / len(rates),2)
|
2022-01-11 14:36:43 +00:00
|
|
|
|