2022-01-16 15:31:25 +03:00
|
|
|
from celery import shared_task
|
|
|
|
|
2022-10-20 12:56:10 +03:00
|
|
|
|
2022-01-16 15:31:25 +03:00
|
|
|
@shared_task(name="users_cleansing")
|
|
|
|
def users_cleansing():
|
2022-02-17 22:50:10 +03:00
|
|
|
"""
|
2022-01-16 15:31:25 +03:00
|
|
|
Deletes users never used 12 hours after creation
|
2022-02-17 22:50:10 +03:00
|
|
|
"""
|
2022-10-25 21:04:12 +03:00
|
|
|
from datetime import timedelta
|
|
|
|
|
2022-01-17 19:41:55 +03:00
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.db.models import Q
|
|
|
|
from django.utils import timezone
|
|
|
|
|
2022-10-25 21:04:12 +03:00
|
|
|
from api.logics import Logics
|
|
|
|
|
2022-01-18 20:52:48 +03:00
|
|
|
# Users who's last login has not been in the last 6 hours
|
|
|
|
active_time_range = (timezone.now() - timedelta(hours=6), timezone.now())
|
2022-01-16 15:31:25 +03:00
|
|
|
queryset = User.objects.filter(~Q(last_login__range=active_time_range))
|
2022-02-17 22:50:10 +03:00
|
|
|
queryset = queryset.filter(is_staff=False) # Do not delete staff users
|
|
|
|
|
2022-03-05 21:43:15 +03:00
|
|
|
# And do not have an active trade, any past contract or any reward.
|
2022-01-16 15:31:25 +03:00
|
|
|
deleted_users = []
|
|
|
|
for user in queryset:
|
2022-03-06 22:12:47 +03:00
|
|
|
# Try an except, due to unknown cause for users lacking profiles.
|
|
|
|
try:
|
2022-10-20 12:56:10 +03:00
|
|
|
if (
|
|
|
|
user.profile.pending_rewards > 0
|
|
|
|
or user.profile.earned_rewards > 0
|
|
|
|
or user.profile.claimed_rewards > 0
|
2023-03-17 23:01:59 +03:00
|
|
|
or user.profile.telegram_enabled is True
|
2022-10-20 12:56:10 +03:00
|
|
|
):
|
2022-03-06 22:12:47 +03:00
|
|
|
continue
|
|
|
|
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()
|
2022-10-20 23:53:51 +03:00
|
|
|
except Exception:
|
2022-03-06 22:12:47 +03:00
|
|
|
pass
|
2022-01-16 15:31:25 +03:00
|
|
|
|
|
|
|
results = {
|
2022-02-17 22:50:10 +03:00
|
|
|
"num_deleted": len(deleted_users),
|
|
|
|
"deleted_users": deleted_users,
|
2022-01-16 15:31:25 +03:00
|
|
|
}
|
2022-01-16 21:32:34 +03:00
|
|
|
return results
|
2022-01-16 15:31:25 +03:00
|
|
|
|
2022-10-20 12:56:10 +03:00
|
|
|
|
2022-03-05 23:51:16 +03:00
|
|
|
@shared_task(name="give_rewards")
|
2022-05-01 16:41:55 +03:00
|
|
|
def give_rewards():
|
2022-03-05 23:51:16 +03:00
|
|
|
"""
|
|
|
|
Referral rewards go from pending to earned.
|
|
|
|
Happens asynchronously so the referral program cannot be easily used to spy.
|
|
|
|
"""
|
|
|
|
from api.models import Profile
|
|
|
|
|
|
|
|
# Users who's last login has not been in the last 6 hours
|
|
|
|
queryset = Profile.objects.filter(pending_rewards__gt=0)
|
|
|
|
|
|
|
|
# And do not have an active trade, any past contract or any reward.
|
|
|
|
results = {}
|
|
|
|
for profile in queryset:
|
|
|
|
given_reward = profile.pending_rewards
|
|
|
|
profile.earned_rewards += given_reward
|
|
|
|
profile.pending_rewards = 0
|
|
|
|
profile.save()
|
|
|
|
|
2022-10-20 12:56:10 +03:00
|
|
|
results[profile.user.username] = {
|
|
|
|
"given_reward": given_reward,
|
|
|
|
"earned_rewards": profile.earned_rewards,
|
|
|
|
}
|
2022-03-05 23:51:16 +03:00
|
|
|
|
|
|
|
return results
|
2022-02-17 22:50:10 +03:00
|
|
|
|
2022-10-20 12:56:10 +03:00
|
|
|
|
2022-02-17 22:50:10 +03:00
|
|
|
@shared_task(name="follow_send_payment")
|
2022-04-16 02:26:39 +03:00
|
|
|
def follow_send_payment(hash):
|
2022-02-17 22:50:10 +03:00
|
|
|
"""Sends sats to buyer, continuous update"""
|
2022-01-17 19:41:55 +03:00
|
|
|
|
2022-10-25 21:04:12 +03:00
|
|
|
from datetime import timedelta
|
|
|
|
|
2022-01-17 19:41:55 +03:00
|
|
|
from decouple import config
|
2022-01-25 01:53:55 +03:00
|
|
|
from django.utils import timezone
|
2022-01-17 19:41:55 +03:00
|
|
|
|
2022-10-25 21:04:12 +03:00
|
|
|
from api.lightning.node import MACAROON, LNNode
|
2022-01-18 16:20:19 +03:00
|
|
|
from api.models import LNPayment, Order
|
2022-01-17 00:54:42 +03:00
|
|
|
|
2022-04-16 02:26:39 +03:00
|
|
|
lnpayment = LNPayment.objects.get(payment_hash=hash)
|
2023-03-16 03:53:37 +03:00
|
|
|
lnpayment.last_routing_time = timezone.now()
|
|
|
|
lnpayment.save()
|
2023-02-10 16:34:35 +03:00
|
|
|
|
|
|
|
# Default is 0ppm. Set by the user over API. Client's default is 1000 ppm.
|
|
|
|
fee_limit_sat = int(
|
|
|
|
float(lnpayment.num_satoshis) * float(lnpayment.routing_budget_ppm) / 1000000
|
|
|
|
)
|
2022-06-06 23:37:51 +03:00
|
|
|
timeout_seconds = int(config("PAYOUT_TIMEOUT_SECONDS"))
|
2022-06-05 19:15:40 +03:00
|
|
|
|
2022-01-17 19:41:55 +03:00
|
|
|
request = LNNode.routerrpc.SendPaymentRequest(
|
|
|
|
payment_request=lnpayment.invoice,
|
|
|
|
fee_limit_sat=fee_limit_sat,
|
2022-06-05 19:15:40 +03:00
|
|
|
timeout_seconds=timeout_seconds,
|
2023-03-14 20:23:11 +03:00
|
|
|
allow_self_payment=True,
|
2022-06-05 19:15:40 +03:00
|
|
|
)
|
2022-01-17 00:54:42 +03:00
|
|
|
|
2022-06-17 01:45:36 +03:00
|
|
|
order = lnpayment.order_paid_LN
|
2023-03-16 03:53:37 +03:00
|
|
|
if order.trade_escrow.num_satoshis < lnpayment.num_satoshis:
|
|
|
|
print(f"Order: {order.id} Payout is larger than collateral !?")
|
|
|
|
return
|
|
|
|
|
|
|
|
def handle_response(response):
|
|
|
|
lnpayment.status = LNPayment.Status.FLIGHT
|
|
|
|
lnpayment.in_flight = True
|
|
|
|
lnpayment.save()
|
|
|
|
order.status = Order.Status.PAY
|
|
|
|
order.save()
|
|
|
|
|
|
|
|
if response.status == 0: # Status 0 'UNKNOWN'
|
|
|
|
# Not sure when this status happens
|
|
|
|
print(f"Order: {order.id} UNKNOWN. Hash {hash}")
|
|
|
|
lnpayment.in_flight = False
|
|
|
|
lnpayment.save()
|
|
|
|
|
|
|
|
if response.status == 1: # Status 1 'IN_FLIGHT'
|
|
|
|
print(f"Order: {order.id} IN_FLIGHT. Hash {hash}")
|
|
|
|
|
|
|
|
if response.status == 3: # Status 3 'FAILED'
|
|
|
|
lnpayment.status = LNPayment.Status.FAILRO
|
|
|
|
lnpayment.last_routing_time = timezone.now()
|
|
|
|
lnpayment.routing_attempts += 1
|
|
|
|
lnpayment.failure_reason = response.failure_reason
|
|
|
|
lnpayment.in_flight = False
|
|
|
|
if lnpayment.routing_attempts > 2:
|
|
|
|
lnpayment.status = LNPayment.Status.EXPIRE
|
|
|
|
lnpayment.routing_attempts = 0
|
|
|
|
lnpayment.save()
|
|
|
|
|
|
|
|
order.status = Order.Status.FAI
|
|
|
|
order.expires_at = timezone.now() + timedelta(
|
|
|
|
seconds=order.t_to_expire(Order.Status.FAI)
|
|
|
|
)
|
|
|
|
order.save()
|
|
|
|
print(
|
|
|
|
f"Order: {order.id} FAILED. Hash: {hash} Reason: {LNNode.payment_failure_context[response.failure_reason]}"
|
|
|
|
)
|
|
|
|
return {
|
|
|
|
"succeded": False,
|
|
|
|
"context": f"payment failure reason: {LNNode.payment_failure_context[response.failure_reason]}",
|
|
|
|
}
|
|
|
|
|
|
|
|
if response.status == 2: # Status 2 'SUCCEEDED'
|
2023-03-16 21:11:39 +03:00
|
|
|
print(f"Order: {order.id} SUCCEEDED. Hash: {hash}")
|
2023-03-16 03:53:37 +03:00
|
|
|
lnpayment.status = LNPayment.Status.SUCCED
|
|
|
|
lnpayment.fee = float(response.fee_msat) / 1000
|
|
|
|
lnpayment.preimage = response.payment_preimage
|
|
|
|
lnpayment.save()
|
|
|
|
order.status = Order.Status.SUC
|
|
|
|
order.expires_at = timezone.now() + timedelta(
|
|
|
|
seconds=order.t_to_expire(Order.Status.SUC)
|
|
|
|
)
|
|
|
|
order.save()
|
|
|
|
results = {"succeded": True}
|
|
|
|
return results
|
|
|
|
|
2022-01-25 01:53:55 +03:00
|
|
|
try:
|
2022-10-20 12:56:10 +03:00
|
|
|
for response in LNNode.routerstub.SendPaymentV2(
|
|
|
|
request, metadata=[("macaroon", MACAROON.hex())]
|
|
|
|
):
|
|
|
|
|
2023-03-16 03:53:37 +03:00
|
|
|
handle_response(response)
|
2022-01-25 01:53:55 +03:00
|
|
|
|
|
|
|
except Exception as e:
|
2023-03-16 03:53:37 +03:00
|
|
|
|
2022-01-25 01:53:55 +03:00
|
|
|
if "invoice expired" in str(e):
|
2023-03-16 03:53:37 +03:00
|
|
|
print(f"Order: {order.id}. INVOICE EXPIRED. Hash: {hash}")
|
2022-01-25 01:53:55 +03:00
|
|
|
lnpayment.status = LNPayment.Status.EXPIRE
|
|
|
|
lnpayment.last_routing_time = timezone.now()
|
2022-04-18 16:57:03 +03:00
|
|
|
lnpayment.in_flight = False
|
2022-01-17 19:41:55 +03:00
|
|
|
lnpayment.save()
|
2022-01-18 16:20:19 +03:00
|
|
|
order.status = Order.Status.FAI
|
2022-02-17 22:50:10 +03:00
|
|
|
order.expires_at = timezone.now() + timedelta(
|
2022-10-20 12:56:10 +03:00
|
|
|
seconds=order.t_to_expire(Order.Status.FAI)
|
|
|
|
)
|
2022-01-18 16:20:19 +03:00
|
|
|
order.save()
|
2023-03-16 03:53:37 +03:00
|
|
|
results = {"succeded": False, "context": "The payout invoice has expired"}
|
|
|
|
return results
|
|
|
|
|
2023-03-16 21:11:39 +03:00
|
|
|
elif "payment is in transition" in str(e):
|
|
|
|
print(f"Order: {order.id} ALREADY IN TRANSITION. Hash: {hash}.")
|
2023-03-16 03:53:37 +03:00
|
|
|
|
|
|
|
request = LNNode.routerrpc.TrackPaymentRequest(
|
|
|
|
payment_hash=bytes.fromhex(hash)
|
|
|
|
)
|
|
|
|
|
|
|
|
for response in LNNode.routerstub.TrackPaymentV2(
|
|
|
|
request, metadata=[("macaroon", MACAROON.hex())]
|
|
|
|
):
|
|
|
|
handle_response(response)
|
2022-01-17 19:41:55 +03:00
|
|
|
|
2023-03-16 21:11:39 +03:00
|
|
|
elif "invoice is already paid" in str(e):
|
|
|
|
print(f"Order: {order.id} ALREADY PAID. Hash: {hash}.")
|
|
|
|
|
|
|
|
request = LNNode.routerrpc.TrackPaymentRequest(
|
|
|
|
payment_hash=bytes.fromhex(hash)
|
|
|
|
)
|
|
|
|
|
|
|
|
for response in LNNode.routerstub.TrackPaymentV2(
|
|
|
|
request, metadata=[("macaroon", MACAROON.hex())]
|
|
|
|
):
|
|
|
|
handle_response(response)
|
|
|
|
|
|
|
|
else:
|
|
|
|
print(str(e))
|
|
|
|
|
2022-10-20 12:56:10 +03:00
|
|
|
|
2022-06-20 20:56:08 +03:00
|
|
|
@shared_task(name="payments_cleansing")
|
|
|
|
def payments_cleansing():
|
2022-05-31 13:35:36 +03:00
|
|
|
"""
|
2022-10-20 12:56:10 +03:00
|
|
|
Deletes cancelled payments (hodl invoices never locked) that
|
2022-06-20 20:56:08 +03:00
|
|
|
belong to orders expired more than 3 days ago.
|
2022-10-20 12:56:10 +03:00
|
|
|
Deletes 'cancelled' or 'create' onchain_payments
|
2022-05-31 13:35:36 +03:00
|
|
|
"""
|
|
|
|
|
|
|
|
from datetime import timedelta
|
2022-10-25 21:04:12 +03:00
|
|
|
|
|
|
|
from django.db.models import Q
|
2022-05-31 13:35:36 +03:00
|
|
|
from django.utils import timezone
|
|
|
|
|
2022-10-25 21:04:12 +03:00
|
|
|
from api.models import LNPayment, OnchainPayment
|
|
|
|
|
2022-05-31 13:35:36 +03:00
|
|
|
# Orders that have expired more than -3 days ago
|
|
|
|
# Usually expiry is 1 day for every finished order. So ~4 days until
|
|
|
|
# a never locked hodl invoice is removed.
|
|
|
|
finished_time = timezone.now() - timedelta(days=3)
|
2022-10-20 12:56:10 +03:00
|
|
|
queryset = LNPayment.objects.filter(
|
|
|
|
Q(status=LNPayment.Status.CANCEL),
|
|
|
|
Q(order_made__expires_at__lt=finished_time)
|
|
|
|
| Q(order_taken__expires_at__lt=finished_time),
|
|
|
|
)
|
2022-05-31 13:35:36 +03:00
|
|
|
|
|
|
|
# And do not have an active trade, any past contract or any reward.
|
|
|
|
deleted_lnpayments = []
|
|
|
|
for lnpayment in queryset:
|
2022-06-20 20:56:08 +03:00
|
|
|
# Try and except. In case some payment is already missing.
|
2022-05-31 13:35:36 +03:00
|
|
|
try:
|
|
|
|
name = str(lnpayment)
|
|
|
|
lnpayment.delete()
|
|
|
|
deleted_lnpayments.append(name)
|
2022-10-20 23:53:51 +03:00
|
|
|
except Exception:
|
2022-05-31 13:35:36 +03:00
|
|
|
pass
|
2022-10-20 12:56:10 +03:00
|
|
|
|
2022-06-20 20:56:08 +03:00
|
|
|
# same for onchain payments
|
2022-10-20 12:56:10 +03:00
|
|
|
queryset = OnchainPayment.objects.filter(
|
|
|
|
Q(status__in=[OnchainPayment.Status.CANCE, OnchainPayment.Status.CREAT]),
|
|
|
|
Q(order_paid_TX__expires_at__lt=finished_time) | Q(order_paid_TX__isnull=True),
|
|
|
|
)
|
2022-06-20 20:56:08 +03:00
|
|
|
|
|
|
|
# And do not have an active trade, any past contract or any reward.
|
|
|
|
deleted_onchainpayments = []
|
|
|
|
for onchainpayment in queryset:
|
|
|
|
# Try and except. In case some payment is already missing.
|
|
|
|
try:
|
|
|
|
name = str(onchainpayment)
|
|
|
|
onchainpayment.delete()
|
|
|
|
deleted_onchainpayments.append(name)
|
2022-10-20 23:53:51 +03:00
|
|
|
except Exception:
|
2022-06-20 20:56:08 +03:00
|
|
|
pass
|
2022-05-31 13:35:36 +03:00
|
|
|
|
|
|
|
results = {
|
2022-06-20 20:56:08 +03:00
|
|
|
"num_lnpayments_deleted": len(deleted_lnpayments),
|
2022-05-31 13:35:36 +03:00
|
|
|
"deleted_lnpayments": deleted_lnpayments,
|
2022-06-20 20:56:08 +03:00
|
|
|
"num_onchainpayments_deleted": len(deleted_onchainpayments),
|
|
|
|
"deleted_onchainpayments": deleted_onchainpayments,
|
2022-05-31 13:35:36 +03:00
|
|
|
}
|
|
|
|
return results
|
|
|
|
|
2022-10-20 12:56:10 +03:00
|
|
|
|
2022-01-16 21:32:34 +03:00
|
|
|
@shared_task(name="cache_external_market_prices", ignore_result=True)
|
2022-01-16 15:31:25 +03:00
|
|
|
def cache_market():
|
2022-01-17 19:41:55 +03:00
|
|
|
|
2022-10-25 21:04:12 +03:00
|
|
|
from django.utils import timezone
|
|
|
|
|
2022-01-17 19:41:55 +03:00
|
|
|
from .models import Currency
|
|
|
|
from .utils import get_exchange_rates
|
|
|
|
|
2022-02-17 01:02:21 +03:00
|
|
|
currency_codes = list(Currency.currency_dict.values())
|
|
|
|
exchange_rates = get_exchange_rates(currency_codes)
|
|
|
|
|
2022-01-16 18:18:23 +03:00
|
|
|
results = {}
|
2022-10-20 12:56:10 +03:00
|
|
|
for i in range(
|
|
|
|
len(Currency.currency_dict.values())
|
|
|
|
): # currencies are indexed starting at 1 (USD)
|
2022-02-17 01:02:21 +03:00
|
|
|
|
2022-02-17 22:50:10 +03:00
|
|
|
rate = exchange_rates[i]
|
2022-02-17 01:02:21 +03:00
|
|
|
results[i] = {currency_codes[i], rate}
|
|
|
|
|
|
|
|
# Do not update if no new rate was found
|
2022-02-17 22:50:10 +03:00
|
|
|
if str(rate) == "nan":
|
|
|
|
continue
|
2022-01-16 18:18:23 +03:00
|
|
|
|
|
|
|
# Create / Update database cached prices
|
2022-02-17 01:02:21 +03:00
|
|
|
currency_key = list(Currency.currency_dict.keys())[i]
|
2022-01-16 19:06:53 +03:00
|
|
|
Currency.objects.update_or_create(
|
2022-02-17 22:50:10 +03:00
|
|
|
id=int(currency_key),
|
|
|
|
currency=int(currency_key),
|
2022-01-16 21:32:34 +03:00
|
|
|
# if there is a Cached market prices matching that id, it updates it with defaults below
|
2022-02-17 22:50:10 +03:00
|
|
|
defaults={
|
|
|
|
"exchange_rate": float(rate),
|
|
|
|
"timestamp": timezone.now(),
|
|
|
|
},
|
|
|
|
)
|
2022-01-16 18:18:23 +03:00
|
|
|
|
2022-02-17 22:50:10 +03:00
|
|
|
return results
|
2022-02-23 19:15:48 +03:00
|
|
|
|
2022-10-20 12:56:10 +03:00
|
|
|
|
2022-02-23 19:15:48 +03:00
|
|
|
@shared_task(name="send_message", ignore_result=True)
|
|
|
|
def send_message(order_id, message):
|
|
|
|
|
|
|
|
from api.models import Order
|
2022-10-20 12:56:10 +03:00
|
|
|
|
2022-02-23 19:15:48 +03:00
|
|
|
order = Order.objects.get(id=order_id)
|
2023-01-31 21:58:11 +03:00
|
|
|
taker_enabled = (
|
|
|
|
False if order.taker is None else order.taker.profile.telegram_enabled
|
|
|
|
)
|
|
|
|
if not (order.maker.profile.telegram_enabled or taker_enabled):
|
2022-02-23 19:15:48 +03:00
|
|
|
return
|
|
|
|
|
|
|
|
from api.messages import Telegram
|
2022-10-20 12:56:10 +03:00
|
|
|
|
2022-02-23 19:15:48 +03:00
|
|
|
telegram = Telegram()
|
|
|
|
|
2022-10-20 12:56:10 +03:00
|
|
|
if message == "welcome":
|
2022-03-19 00:21:13 +03:00
|
|
|
telegram.welcome(order)
|
2022-10-20 12:56:10 +03:00
|
|
|
|
|
|
|
elif message == "order_expired_untaken":
|
2022-02-23 19:15:48 +03:00
|
|
|
telegram.order_expired_untaken(order)
|
|
|
|
|
2022-10-20 12:56:10 +03:00
|
|
|
elif message == "trade_successful":
|
2022-02-23 19:15:48 +03:00
|
|
|
telegram.trade_successful(order)
|
|
|
|
|
2022-10-20 12:56:10 +03:00
|
|
|
elif message == "public_order_cancelled":
|
2022-02-23 19:15:48 +03:00
|
|
|
telegram.public_order_cancelled(order)
|
|
|
|
|
2022-10-20 12:56:10 +03:00
|
|
|
elif message == "taker_expired_b4bond":
|
2022-02-23 19:15:48 +03:00
|
|
|
telegram.taker_expired_b4bond(order)
|
|
|
|
|
2022-10-20 12:56:10 +03:00
|
|
|
elif message == "order_published":
|
2022-03-01 16:38:04 +03:00
|
|
|
telegram.order_published(order)
|
2022-10-20 12:56:10 +03:00
|
|
|
|
|
|
|
elif message == "order_taken_confirmed":
|
2022-03-11 18:24:39 +03:00
|
|
|
telegram.order_taken_confirmed(order)
|
2022-10-20 12:56:10 +03:00
|
|
|
|
|
|
|
elif message == "fiat_exchange_starts":
|
2022-04-29 21:54:20 +03:00
|
|
|
telegram.fiat_exchange_starts(order)
|
2022-05-01 16:41:55 +03:00
|
|
|
|
2022-10-20 12:56:10 +03:00
|
|
|
elif message == "dispute_opened":
|
2022-06-03 01:32:01 +03:00
|
|
|
telegram.dispute_opened(order)
|
|
|
|
|
2022-10-20 12:56:10 +03:00
|
|
|
elif message == "collaborative_cancelled":
|
2022-06-03 01:32:01 +03:00
|
|
|
telegram.collaborative_cancelled(order)
|
|
|
|
|
2022-10-20 12:56:10 +03:00
|
|
|
return
|