2022-01-16 12:31:25 +00:00
|
|
|
from celery import shared_task
|
|
|
|
|
|
|
|
@shared_task(name="users_cleansing")
|
|
|
|
def users_cleansing():
|
|
|
|
'''
|
|
|
|
Deletes users never used 12 hours after creation
|
|
|
|
'''
|
2022-01-17 16:41:55 +00:00
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.db.models import Q
|
|
|
|
from .logics import Logics
|
|
|
|
from datetime import timedelta
|
|
|
|
from django.utils import timezone
|
|
|
|
|
2022-01-16 12:31:25 +00:00
|
|
|
# Users who's last login has not been in the last 12 hours
|
|
|
|
active_time_range = (timezone.now() - timedelta(hours=12), timezone.now())
|
|
|
|
queryset = User.objects.filter(~Q(last_login__range=active_time_range))
|
2022-01-17 16:41:55 +00:00
|
|
|
queryset = queryset.filter(is_staff=False) # Do not delete staff users
|
2022-01-16 12:31:25 +00:00
|
|
|
|
|
|
|
# And do not have an active trade or any pass finished trade.
|
|
|
|
deleted_users = []
|
|
|
|
for user in queryset:
|
|
|
|
if not user.profile.total_contracts == 0:
|
|
|
|
continue
|
|
|
|
valid, _ = Logics.validate_already_maker_or_taker(user)
|
|
|
|
if valid:
|
|
|
|
deleted_users.append(str(user))
|
|
|
|
user.delete()
|
|
|
|
|
|
|
|
results = {
|
|
|
|
'num_deleted': len(deleted_users),
|
|
|
|
'deleted_users': deleted_users,
|
|
|
|
}
|
|
|
|
|
|
|
|
return results
|
|
|
|
|
|
|
|
|
2022-01-16 15:18:23 +00:00
|
|
|
@shared_task(name="orders_expire")
|
2022-01-16 18:32:34 +00:00
|
|
|
def orders_expire(rest_secs):
|
|
|
|
'''
|
2022-01-16 21:54:42 +00:00
|
|
|
Continuously checks order expiration times for 1 hour. If order
|
|
|
|
has expires, it calls the logics module for expiration handling.
|
2022-01-16 18:32:34 +00:00
|
|
|
'''
|
2022-01-17 16:41:55 +00:00
|
|
|
import time
|
|
|
|
from .models import Order
|
|
|
|
from .logics import Logics
|
|
|
|
from datetime import timedelta
|
|
|
|
from django.utils import timezone
|
|
|
|
|
2022-01-16 18:32:34 +00:00
|
|
|
now = timezone.now()
|
2022-01-17 16:41:55 +00:00
|
|
|
end_time = now + timedelta(minutes=60)
|
2022-01-16 18:32:34 +00:00
|
|
|
context = []
|
|
|
|
|
|
|
|
while now < end_time:
|
|
|
|
queryset = Order.objects.exclude(status=Order.Status.EXP).exclude(status=Order.Status.UCA).exclude(status= Order.Status.CCA)
|
|
|
|
queryset = queryset.filter(expires_at__lt=now) # expires at lower than now
|
|
|
|
|
|
|
|
for order in queryset:
|
2022-01-17 16:41:55 +00:00
|
|
|
try: # TODO Fix, it might fail if returning an already returned bond.
|
|
|
|
info = str(order)+ " was "+ Order.Status(order.status).label
|
|
|
|
if Logics.order_expires(order): # Order send to expire here
|
|
|
|
context.append(info)
|
|
|
|
except:
|
|
|
|
pass
|
2022-01-16 18:32:34 +00:00
|
|
|
|
|
|
|
# Allow for some thread rest.
|
|
|
|
time.sleep(rest_secs)
|
|
|
|
|
|
|
|
# Update 'now' for a new loop
|
|
|
|
now = timezone.now()
|
|
|
|
|
|
|
|
results = {
|
|
|
|
'num_expired': len(context),
|
|
|
|
'expired_orders_context': context,
|
|
|
|
'rest_param': rest_secs,
|
|
|
|
}
|
|
|
|
|
|
|
|
return results
|
2022-01-16 12:31:25 +00:00
|
|
|
|
2022-01-17 16:41:55 +00:00
|
|
|
@shared_task(name='follow_send_payment')
|
|
|
|
def follow_send_payment(lnpayment):
|
|
|
|
'''Sends sats to buyer, continuous update'''
|
|
|
|
|
|
|
|
from decouple import config
|
|
|
|
from base64 import b64decode
|
|
|
|
|
|
|
|
from api.lightning.node import LNNode
|
|
|
|
from api.models import LNPayment
|
2022-01-16 21:54:42 +00:00
|
|
|
|
2022-01-17 16:41:55 +00:00
|
|
|
MACAROON = b64decode(config('LND_MACAROON_BASE64'))
|
2022-01-16 12:31:25 +00:00
|
|
|
|
2022-01-17 16:41:55 +00:00
|
|
|
fee_limit_sat = max(lnpayment.num_satoshis * 0.0002, 10) # 200 ppm or 10 sats max
|
|
|
|
request = LNNode.routerrpc.SendPaymentRequest(
|
|
|
|
payment_request=lnpayment.invoice,
|
|
|
|
fee_limit_sat=fee_limit_sat,
|
|
|
|
timeout_seconds=60)
|
2022-01-16 21:54:42 +00:00
|
|
|
|
2022-01-17 16:41:55 +00:00
|
|
|
for response in LNNode.routerstub.SendPaymentV2(request, metadata=[('macaroon', MACAROON.hex())]):
|
|
|
|
if response.status == 0 : # Status 0 'UNKNOWN'
|
|
|
|
pass
|
|
|
|
|
|
|
|
if response.status == 1 : # Status 1 'IN_FLIGHT'
|
|
|
|
lnpayment.status = LNPayment.Status.FLIGHT
|
|
|
|
lnpayment.save()
|
|
|
|
|
|
|
|
if response.status == 3 : # Status 3 'FAILED'
|
|
|
|
lnpayment.status = LNPayment.Status.FAILRO
|
|
|
|
lnpayment.save()
|
|
|
|
context = LNNode.payment_failure_context[response.failure_reason]
|
|
|
|
return False, context
|
|
|
|
|
|
|
|
if response.status == 2 : # Status 2 'SUCCEEDED'
|
|
|
|
lnpayment.status = LNPayment.Status.SUCCED
|
|
|
|
lnpayment.save()
|
|
|
|
return True, None
|
2022-01-16 12:31:25 +00:00
|
|
|
|
2022-01-16 18:32:34 +00:00
|
|
|
@shared_task(name="cache_external_market_prices", ignore_result=True)
|
2022-01-16 12:31:25 +00:00
|
|
|
def cache_market():
|
2022-01-17 16:41:55 +00:00
|
|
|
|
|
|
|
from .models import Currency
|
|
|
|
from .utils import get_exchange_rates
|
|
|
|
|
|
|
|
from django.utils import timezone
|
|
|
|
|
2022-01-16 16:06:53 +00:00
|
|
|
exchange_rates = get_exchange_rates(list(Currency.currency_dict.values()))
|
2022-01-16 15:18:23 +00:00
|
|
|
results = {}
|
2022-01-16 16:06:53 +00:00
|
|
|
for val in Currency.currency_dict:
|
2022-01-16 15:18:23 +00:00
|
|
|
rate = exchange_rates[int(val)-1] # currecies are indexed starting at 1 (USD)
|
2022-01-16 16:06:53 +00:00
|
|
|
results[val] = {Currency.currency_dict[val], rate}
|
2022-01-16 19:16:11 +00:00
|
|
|
if str(rate) == 'nan': continue # Do not update if no new rate was found
|
2022-01-16 15:18:23 +00:00
|
|
|
|
|
|
|
# Create / Update database cached prices
|
2022-01-16 16:06:53 +00:00
|
|
|
Currency.objects.update_or_create(
|
|
|
|
id = int(val),
|
2022-01-16 15:18:23 +00:00
|
|
|
currency = int(val),
|
2022-01-16 18:32:34 +00:00
|
|
|
# if there is a Cached market prices matching that id, it updates it with defaults below
|
2022-01-16 15:18:23 +00:00
|
|
|
defaults = {
|
2022-01-16 19:16:11 +00:00
|
|
|
'exchange_rate': float(rate),
|
2022-01-16 15:18:23 +00:00
|
|
|
'timestamp': timezone.now(),
|
|
|
|
})
|
|
|
|
|
|
|
|
return results
|