mirror of
https://github.com/RoboSats/robosats.git
synced 2025-02-21 12:49:02 +00:00
Work on more logics. Rough draft finished
This commit is contained in:
parent
8a55383761
commit
9ab52853d5
@ -24,7 +24,7 @@ class EUserAdmin(UserAdmin):
|
|||||||
|
|
||||||
@admin.register(Order)
|
@admin.register(Order)
|
||||||
class OrderAdmin(AdminChangeLinksMixin, admin.ModelAdmin):
|
class OrderAdmin(AdminChangeLinksMixin, admin.ModelAdmin):
|
||||||
list_display = ('id','type','maker_link','taker_link','status','amount','currency','t0_satoshis','created_at','expires_at', 'buyer_invoice_link','maker_bond_link','taker_bond_link','trade_escrow_link')
|
list_display = ('id','type','maker_link','taker_link','status','amount','currency','t0_satoshis','is_disputed','is_fiat_sent','created_at','expires_at', 'buyer_invoice_link','maker_bond_link','taker_bond_link','trade_escrow_link')
|
||||||
list_display_links = ('id','type')
|
list_display_links = ('id','type')
|
||||||
change_links = ('maker','taker','buyer_invoice','maker_bond','taker_invoice','taker_bond','trade_escrow')
|
change_links = ('maker','taker','buyer_invoice','maker_bond','taker_invoice','taker_bond','trade_escrow')
|
||||||
|
|
||||||
|
@ -23,8 +23,6 @@ ESCROW_EXPIRY = int(config('ESCROW_EXPIRY'))
|
|||||||
|
|
||||||
class Logics():
|
class Logics():
|
||||||
|
|
||||||
# escrow_user = User.objects.get(username=ESCROW_USERNAME)
|
|
||||||
|
|
||||||
def validate_already_maker_or_taker(user):
|
def validate_already_maker_or_taker(user):
|
||||||
'''Checks if the user is already partipant of an order'''
|
'''Checks if the user is already partipant of an order'''
|
||||||
queryset = Order.objects.filter(maker=user)
|
queryset = Order.objects.filter(maker=user)
|
||||||
@ -130,16 +128,23 @@ class Logics():
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def rate_counterparty(cls, order, user, rating):
|
def rate_counterparty(cls, order, user, rating):
|
||||||
# if maker, rates taker
|
|
||||||
if order.maker == user:
|
# If the trade is finished
|
||||||
order.taker.profile.total_ratings = order.taker.profile.total_ratings + 1
|
if order.status > Order.Status.PAY:
|
||||||
last_ratings = list(order.taker.profile.last_ratings).append(rating)
|
|
||||||
order.taker.profile.total_ratings = sum(last_ratings) / len(last_ratings)
|
# if maker, rates taker
|
||||||
# if taker, rates maker
|
if order.maker == user:
|
||||||
if order.taker == user:
|
order.taker.profile.total_ratings = order.taker.profile.total_ratings + 1
|
||||||
order.maker.profile.total_ratings = order.maker.profile.total_ratings + 1
|
last_ratings = list(order.taker.profile.last_ratings).append(rating)
|
||||||
last_ratings = list(order.maker.profile.last_ratings).append(rating)
|
order.taker.profile.total_ratings = sum(last_ratings) / len(last_ratings)
|
||||||
order.maker.profile.total_ratings = sum(last_ratings) / len(last_ratings)
|
|
||||||
|
# if taker, rates maker
|
||||||
|
if order.taker == user:
|
||||||
|
order.maker.profile.total_ratings = order.maker.profile.total_ratings + 1
|
||||||
|
last_ratings = list(order.maker.profile.last_ratings).append(rating)
|
||||||
|
order.maker.profile.total_ratings = sum(last_ratings) / len(last_ratings)
|
||||||
|
else:
|
||||||
|
return False, {'bad_request':'You cannot rate your counterparty yet.'}
|
||||||
|
|
||||||
order.save()
|
order.save()
|
||||||
return True, None
|
return True, None
|
||||||
@ -291,4 +296,49 @@ class Logics():
|
|||||||
expires_at = expires_at)
|
expires_at = expires_at)
|
||||||
|
|
||||||
order.save()
|
order.save()
|
||||||
return True, {'escrow_invoice':invoice,'escrow_satoshis': escrow_satoshis}
|
return True, {'escrow_invoice':invoice,'escrow_satoshis': escrow_satoshis}
|
||||||
|
|
||||||
|
def settle_escrow(order):
|
||||||
|
''' Settles the trade escrow HTLC'''
|
||||||
|
# TODO ERROR HANDLING
|
||||||
|
|
||||||
|
valid = LNNode.settle_hodl_htlcs(order.trade_escrow.payment_hash)
|
||||||
|
return valid
|
||||||
|
|
||||||
|
def pay_buyer_invoice(order):
|
||||||
|
''' Settles the trade escrow HTLC'''
|
||||||
|
# TODO ERROR HANDLING
|
||||||
|
|
||||||
|
valid = LNNode.pay_invoice(order.buyer_invoice.payment_hash)
|
||||||
|
return valid
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def confirm_fiat(cls, order, user):
|
||||||
|
''' If Order is in the CHAT states:
|
||||||
|
If user is buyer: mark the FIAT SENT andettle escrow!
|
||||||
|
If User is the seller and FIAT was already sent: Pay buyer invoice!'''
|
||||||
|
|
||||||
|
if order.status == Order.Status.CHA or order.status == Order.Status.FSE: # TODO Alternatively, if all collateral is locked? test out
|
||||||
|
|
||||||
|
# If buyer, settle escrow and mark fiat sent
|
||||||
|
if cls.is_buyer(order, user):
|
||||||
|
if cls.settle_escrow(order): # KEY LINE - SETTLES THE TRADE ESCROW !!
|
||||||
|
order.trade_escrow.status = LNPayment.Status.SETLED
|
||||||
|
order.status = Order.Status.FSE
|
||||||
|
order.is_fiat_sent = True
|
||||||
|
|
||||||
|
# If seller and fiat sent, pay buyer invoice
|
||||||
|
elif cls.is_seller(order, user):
|
||||||
|
if not order.is_fiat_sent:
|
||||||
|
return False, {'bad_request':'You cannot confirm to have received the fiat before it is confirmed to be sent by the buyer.'}
|
||||||
|
|
||||||
|
# Double check the escrow is settled.
|
||||||
|
if LNNode.double_check_htlc_is_settled(order.trade_escrow.payment_hash):
|
||||||
|
if cls.pay_buyer_invoice(order): # KEY LINE - PAYS THE BUYER !!
|
||||||
|
order.status = Order.Status.PAY
|
||||||
|
order.buyer_invoice.status = LNPayment.Status.PAYING
|
||||||
|
else:
|
||||||
|
return False, {'bad_request':'You cannot confirm the fiat payment at this stage'}
|
||||||
|
|
||||||
|
order.save()
|
||||||
|
return True, None
|
@ -38,7 +38,8 @@ class LNPayment(models.Model):
|
|||||||
RETNED = 3, 'Returned'
|
RETNED = 3, 'Returned'
|
||||||
MISSNG = 4, 'Missing'
|
MISSNG = 4, 'Missing'
|
||||||
VALIDI = 5, 'Valid'
|
VALIDI = 5, 'Valid'
|
||||||
INFAIL = 6, 'Failed routing'
|
PAYING = 6, 'Paying ongoing'
|
||||||
|
FAILRO = 7, 'Failed routing'
|
||||||
|
|
||||||
# payment use details
|
# payment use details
|
||||||
type = models.PositiveSmallIntegerField(choices=Types.choices, null=False, default=Types.HODL)
|
type = models.PositiveSmallIntegerField(choices=Types.choices, null=False, default=Types.HODL)
|
||||||
@ -78,20 +79,20 @@ class Order(models.Model):
|
|||||||
DEL = 2, 'Deleted'
|
DEL = 2, 'Deleted'
|
||||||
TAK = 3, 'Waiting for taker bond'
|
TAK = 3, 'Waiting for taker bond'
|
||||||
UCA = 4, 'Cancelled'
|
UCA = 4, 'Cancelled'
|
||||||
WF2 = 5, 'Waiting for trade collateral and buyer invoice'
|
EXP = 5, 'Expired'
|
||||||
WFE = 6, 'Waiting only for seller trade collateral'
|
WF2 = 6, 'Waiting for trade collateral and buyer invoice'
|
||||||
WFI = 7, 'Waiting only for buyer invoice'
|
WFE = 7, 'Waiting only for seller trade collateral'
|
||||||
CHA = 8, 'Sending fiat - In chatroom'
|
WFI = 8, 'Waiting only for buyer invoice'
|
||||||
CCA = 9, 'Collaboratively cancelled'
|
CHA = 9, 'Sending fiat - In chatroom'
|
||||||
FSE = 10, 'Fiat sent - In chatroom'
|
FSE = 10, 'Fiat sent - In chatroom'
|
||||||
FCO = 11, 'Fiat confirmed'
|
DIS = 11, 'In dispute'
|
||||||
SUC = 12, 'Sucessfully settled'
|
CCA = 12, 'Collaboratively cancelled'
|
||||||
FAI = 13, 'Failed lightning network routing'
|
PAY = 13, 'Sending satoshis to buyer'
|
||||||
UPI = 14, 'Updated invoice'
|
SUC = 14, 'Sucessfully settled'
|
||||||
DIS = 15, 'In dispute'
|
FAI = 15, 'Failed lightning network routing'
|
||||||
MLD = 16, 'Maker lost dispute'
|
MLD = 16, 'Maker lost dispute'
|
||||||
TLD = 17, 'Taker lost dispute'
|
TLD = 17, 'Taker lost dispute'
|
||||||
EXP = 18, 'Expired'
|
|
||||||
|
|
||||||
# order info
|
# order info
|
||||||
status = models.PositiveSmallIntegerField(choices=Status.choices, null=False, default=Status.WFB)
|
status = models.PositiveSmallIntegerField(choices=Status.choices, null=False, default=Status.WFB)
|
||||||
@ -102,7 +103,7 @@ class Order(models.Model):
|
|||||||
type = models.PositiveSmallIntegerField(choices=Types.choices, null=False)
|
type = models.PositiveSmallIntegerField(choices=Types.choices, null=False)
|
||||||
currency = models.PositiveSmallIntegerField(choices=Currencies.choices, null=False)
|
currency = models.PositiveSmallIntegerField(choices=Currencies.choices, null=False)
|
||||||
amount = models.DecimalField(max_digits=9, decimal_places=4, validators=[MinValueValidator(0.00001)])
|
amount = models.DecimalField(max_digits=9, decimal_places=4, validators=[MinValueValidator(0.00001)])
|
||||||
payment_method = models.CharField(max_length=30, null=False, default="not specified", blank=True)
|
payment_method = models.CharField(max_length=50, null=False, default="not specified", blank=True)
|
||||||
|
|
||||||
# order pricing method. A explicit amount of sats, or a relative premium above/below market.
|
# order pricing method. A explicit amount of sats, or a relative premium above/below market.
|
||||||
is_explicit = models.BooleanField(default=False, null=False)
|
is_explicit = models.BooleanField(default=False, null=False)
|
||||||
@ -118,8 +119,11 @@ class Order(models.Model):
|
|||||||
maker = models.ForeignKey(User, related_name='maker', on_delete=models.CASCADE, null=True, default=None) # unique = True, a maker can only make one order
|
maker = models.ForeignKey(User, related_name='maker', on_delete=models.CASCADE, null=True, default=None) # unique = True, a maker can only make one order
|
||||||
taker = models.ForeignKey(User, related_name='taker', on_delete=models.SET_NULL, null=True, default=None, blank=True) # unique = True, a taker can only take one order
|
taker = models.ForeignKey(User, related_name='taker', on_delete=models.SET_NULL, null=True, default=None, blank=True) # unique = True, a taker can only take one order
|
||||||
is_pending_cancel = models.BooleanField(default=False, null=False) # When collaborative cancel is needed and one partner has cancelled.
|
is_pending_cancel = models.BooleanField(default=False, null=False) # When collaborative cancel is needed and one partner has cancelled.
|
||||||
|
is_disputed = models.BooleanField(default=False, null=False)
|
||||||
|
is_fiat_sent = models.BooleanField(default=False, null=False)
|
||||||
|
|
||||||
# order collateral
|
# HTLCs
|
||||||
|
# Order collateral
|
||||||
maker_bond = models.ForeignKey(LNPayment, related_name='maker_bond', on_delete=models.SET_NULL, null=True, default=None, blank=True)
|
maker_bond = models.ForeignKey(LNPayment, related_name='maker_bond', on_delete=models.SET_NULL, null=True, default=None, blank=True)
|
||||||
taker_bond = models.ForeignKey(LNPayment, related_name='taker_bond', on_delete=models.SET_NULL, null=True, default=None, blank=True)
|
taker_bond = models.ForeignKey(LNPayment, related_name='taker_bond', on_delete=models.SET_NULL, null=True, default=None, blank=True)
|
||||||
trade_escrow = models.ForeignKey(LNPayment, related_name='trade_escrow', on_delete=models.SET_NULL, null=True, default=None, blank=True)
|
trade_escrow = models.ForeignKey(LNPayment, related_name='trade_escrow', on_delete=models.SET_NULL, null=True, default=None, blank=True)
|
||||||
@ -127,6 +131,10 @@ class Order(models.Model):
|
|||||||
# buyer payment LN invoice
|
# buyer payment LN invoice
|
||||||
buyer_invoice = models.ForeignKey(LNPayment, related_name='buyer_invoice', on_delete=models.SET_NULL, null=True, default=None, blank=True)
|
buyer_invoice = models.ForeignKey(LNPayment, related_name='buyer_invoice', on_delete=models.SET_NULL, null=True, default=None, blank=True)
|
||||||
|
|
||||||
|
# cancel LN invoice // these are only needed to charge lower-than-bond amounts. E.g., a taken order has a small cost if cancelled, to avoid DDOSing.
|
||||||
|
maker_cancel = models.ForeignKey(LNPayment, related_name='maker_cancel', on_delete=models.SET_NULL, null=True, default=None, blank=True)
|
||||||
|
taker_cancel = models.ForeignKey(LNPayment, related_name='taker_cancel', on_delete=models.SET_NULL, null=True, default=None, blank=True)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
# Make relational back to ORDER
|
# Make relational back to ORDER
|
||||||
return (f'Order {self.id}: {self.Types(self.type).label} BTC for {self.amount} {self.Currencies(self.currency).label}')
|
return (f'Order {self.id}: {self.Types(self.type).label} BTC for {self.amount} {self.Currencies(self.currency).label}')
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
from django.urls import path
|
from django.urls import path
|
||||||
from .views import OrderMakerView, OrderView, UserView, BookView
|
from .views import MakerView, OrderView, UserView, BookView, InfoView
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('make/', OrderMakerView.as_view()),
|
path('make/', MakerView.as_view()),
|
||||||
path('order/', OrderView.as_view({'get':'get','post':'take_update_confirm_dispute_cancel'})),
|
path('order/', OrderView.as_view({'get':'get','post':'take_update_confirm_dispute_cancel'})),
|
||||||
path('usergen/', UserView.as_view()),
|
path('usergen/', UserView.as_view()),
|
||||||
path('book/', BookView.as_view()),
|
path('book/', BookView.as_view()),
|
||||||
|
path('info/', InfoView.as_view()),
|
||||||
]
|
]
|
64
api/views.py
64
api/views.py
@ -22,13 +22,14 @@ from django.utils import timezone
|
|||||||
from decouple import config
|
from decouple import config
|
||||||
|
|
||||||
EXP_MAKER_BOND_INVOICE = int(config('EXP_MAKER_BOND_INVOICE'))
|
EXP_MAKER_BOND_INVOICE = int(config('EXP_MAKER_BOND_INVOICE'))
|
||||||
|
FEE = float(config('FEE'))
|
||||||
|
|
||||||
avatar_path = Path('frontend/static/assets/avatars')
|
avatar_path = Path('frontend/static/assets/avatars')
|
||||||
avatar_path.mkdir(parents=True, exist_ok=True)
|
avatar_path.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
# Create your views here.
|
# Create your views here.
|
||||||
|
|
||||||
class OrderMakerView(CreateAPIView):
|
class MakerView(CreateAPIView):
|
||||||
serializer_class = MakeOrderSerializer
|
serializer_class = MakeOrderSerializer
|
||||||
|
|
||||||
def post(self,request):
|
def post(self,request):
|
||||||
@ -121,7 +122,19 @@ class OrderView(viewsets.ViewSet):
|
|||||||
data['is_seller'] = Logics.is_seller(order,request.user)
|
data['is_seller'] = Logics.is_seller(order,request.user)
|
||||||
data['maker_nick'] = str(order.maker)
|
data['maker_nick'] = str(order.maker)
|
||||||
data['taker_nick'] = str(order.taker)
|
data['taker_nick'] = str(order.taker)
|
||||||
data['status_message'] = Order.Status(order.status).label
|
data['status_message'] = Order.Status(order.status).label
|
||||||
|
data['is_fiat_sent'] = order.is_fiat_sent
|
||||||
|
data['is_disputed'] = order.is_disputed
|
||||||
|
|
||||||
|
# If both bonds are locked, participants can see the trade in sats is also final.
|
||||||
|
if order.taker_bond:
|
||||||
|
if order.maker_bond.status == order.taker_bond.status == order.trade_escrow.status == LNPayment.Status.LOCKED:
|
||||||
|
# Seller sees the amount he pays
|
||||||
|
if data['is_seller']:
|
||||||
|
data['trade_satoshis'] = order.last_satoshis
|
||||||
|
# Buyer sees the amount he receives
|
||||||
|
elif data['is_buyer']:
|
||||||
|
data['trade_satoshis'] = order.last_satoshis * (1-FEE)
|
||||||
|
|
||||||
# 5) If status is 'waiting for maker bond' and user is MAKER, reply with a MAKER HODL invoice.
|
# 5) If status is 'waiting for maker bond' and user is MAKER, reply with a MAKER HODL invoice.
|
||||||
if order.status == Order.Status.WFB and data['is_maker']:
|
if order.status == Order.Status.WFB and data['is_maker']:
|
||||||
@ -166,7 +179,11 @@ class OrderView(viewsets.ViewSet):
|
|||||||
if order.maker_bond.status == order.taker_bond.status == order.trade_escrow.status == LNPayment.Status.LOCKED:
|
if order.maker_bond.status == order.taker_bond.status == order.trade_escrow.status == LNPayment.Status.LOCKED:
|
||||||
# add whether a collaborative cancel is pending
|
# add whether a collaborative cancel is pending
|
||||||
data['pending_cancel'] = order.is_pending_cancel
|
data['pending_cancel'] = order.is_pending_cancel
|
||||||
|
|
||||||
|
# 9) if buyer confirmed FIAT SENT
|
||||||
|
elif order.status == Order.Status.FSE:
|
||||||
|
data['buyer_confirmed']
|
||||||
|
|
||||||
return Response(data, status.HTTP_200_OK)
|
return Response(data, status.HTTP_200_OK)
|
||||||
|
|
||||||
def take_update_confirm_dispute_cancel(self, request, format=None):
|
def take_update_confirm_dispute_cancel(self, request, format=None):
|
||||||
@ -195,32 +212,42 @@ class OrderView(viewsets.ViewSet):
|
|||||||
Logics.take(order, request.user)
|
Logics.take(order, request.user)
|
||||||
else: Response({'bad_request':'This order is not public anymore.'}, status.HTTP_400_BAD_REQUEST)
|
else: Response({'bad_request':'This order is not public anymore.'}, status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
# 2) If action is update (invoice)
|
# Any other action is only allowed if the user is a participant
|
||||||
elif action == 'update_invoice' and invoice:
|
if not (order.maker == request.user or order.taker == request.user):
|
||||||
|
return Response({'bad_request':'You are not a participant in this order'}, status.HTTP_403_FORBIDDEN)
|
||||||
|
|
||||||
|
# 2) If action is 'update invoice'
|
||||||
|
if action == 'update_invoice' and invoice:
|
||||||
valid, context = Logics.update_invoice(order,request.user,invoice)
|
valid, context = Logics.update_invoice(order,request.user,invoice)
|
||||||
if not valid: return Response(context,status.HTTP_400_BAD_REQUEST)
|
if not valid: return Response(context, status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
# 3) If action is cancel
|
# 3) If action is cancel
|
||||||
elif action == 'cancel':
|
elif action == 'cancel':
|
||||||
valid, context = Logics.cancel_order(order,request.user)
|
valid, context = Logics.cancel_order(order,request.user)
|
||||||
if not valid: return Response(context,status.HTTP_400_BAD_REQUEST)
|
if not valid: return Response(context, status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
# 4) If action is confirm
|
# 4) If action is confirm
|
||||||
elif action == 'confirm':
|
elif action == 'confirm':
|
||||||
pass
|
valid, context = Logics.confirm_fiat(order,request.user)
|
||||||
|
if not valid: return Response(context, status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
# 5) If action is dispute
|
# 5) If action is dispute
|
||||||
elif action == 'dispute':
|
elif action == 'dispute':
|
||||||
pass
|
valid, context = Logics.open_dispute(order,request.user, rating)
|
||||||
|
if not valid: return Response(context, status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
# 6) If action is dispute
|
# 6) If action is rate
|
||||||
elif action == 'rate' and rating:
|
elif action == 'rate' and rating:
|
||||||
valid, context = Logics.rate_counterparty(order,request.user, rating)
|
valid, context = Logics.rate_counterparty(order,request.user, rating)
|
||||||
if not valid: return Response(context,status.HTTP_400_BAD_REQUEST)
|
if not valid: return Response(context, status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
# If nothing... something else is going on. Probably not allowed!
|
# If nothing of the above... something else is going on. Probably not allowed!
|
||||||
else:
|
else:
|
||||||
return Response({'bad_request':'The Robotic Satoshis working in the warehouse did not understand you'})
|
return Response(
|
||||||
|
{'bad_request':
|
||||||
|
'The Robotic Satoshis working in the warehouse did not understand you. ' +
|
||||||
|
'Please, fill a Bug Issue in Github https://github.com/Reckless-Satoshi/robosats/issues'},
|
||||||
|
status.HTTP_501_NOT_IMPLEMENTED)
|
||||||
|
|
||||||
return self.get(request)
|
return self.get(request)
|
||||||
|
|
||||||
@ -337,6 +364,17 @@ class BookView(ListAPIView):
|
|||||||
book_data.append(data)
|
book_data.append(data)
|
||||||
|
|
||||||
return Response(book_data, status=status.HTTP_200_OK)
|
return Response(book_data, status=status.HTTP_200_OK)
|
||||||
|
|
||||||
|
class InfoView(ListAPIView):
|
||||||
|
|
||||||
|
def get(self, request, format = None):
|
||||||
|
context = {}
|
||||||
|
context['num_public_buy_orders'] = len(Order.objects.filter(type=Order.Types.BUY, status=Order.Status.PUB))
|
||||||
|
context['num_public_sell_orders'] = len(Order.objects.filter(type=Order.Types.BUY, status=Order.Status.PUB))
|
||||||
|
context['num_active_robots'] = None # Todo
|
||||||
|
context['total_volume'] = None
|
||||||
|
|
||||||
|
return Response(context, status.HTTP_200_ok)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -177,7 +177,8 @@ export default class MakerPage extends Component {
|
|||||||
type="text"
|
type="text"
|
||||||
require={true}
|
require={true}
|
||||||
inputProps={{
|
inputProps={{
|
||||||
style: {textAlign:"center"}
|
style: {textAlign:"center"},
|
||||||
|
maxLength: 50
|
||||||
}}
|
}}
|
||||||
onChange={this.handlePaymentMethodChange}
|
onChange={this.handlePaymentMethodChange}
|
||||||
/>
|
/>
|
||||||
|
Loading…
Reference in New Issue
Block a user