From bd7c82fc11ed05d1f8caeddffd2498ea4b196517 Mon Sep 17 00:00:00 2001 From: Reckless_Satoshi Date: Wed, 12 Jan 2022 06:26:26 -0800 Subject: [PATCH] Add currencies. Fix info view --- api/lightning/node.py | 44 ++++++++++++++++++++++---- api/logics.py | 18 ++++++++--- api/views.py | 3 +- frontend/src/components/TradeBox.js | 4 +-- frontend/static/assets/currencies.json | 33 ++++++++++++++++--- 5 files changed, 83 insertions(+), 19 deletions(-) diff --git a/api/lightning/node.py b/api/lightning/node.py index 6da8f0eb..41359139 100644 --- a/api/lightning/node.py +++ b/api/lightning/node.py @@ -28,6 +28,14 @@ class LNNode(): invoicesstub = invoicesstub.InvoicesStub(channel) routerstub = routerstub.RouterStub(channel) + payment_failure_context = { + 0: "Payment isn't failed (yet)", + 1: "There are more routes to try, but the payment timeout was exceeded.", + 2: "All possible routes were tried and failed permanently. Or were no routes to the destination at all.", + 3: "A non-recoverable error has occured.", + 4: "Payment details incorrect (unknown hash, invalid amt or invalid final cltv delta)", + 5: "Insufficient local balance."} + @classmethod def decode_payreq(cls, invoice): '''Decodes a lightning payment request (invoice)''' @@ -85,7 +93,18 @@ class LNNode(): response = cls.invoicesstub.LookupInvoiceV2(request, metadata=[('macaroon', MACAROON.hex())]) print('status here') print(response.state) - return response.state == 3 # True if hold invoice is accepted. + + # TODO ERROR HANDLING + if response.state == 0: # OPEN + print('STATUS: OPEN') + pass + if response.state == 1: # SETTLED + pass + if response.state == 2: # CANCELLED + pass + if response.state == 3: # ACCEPTED (LOCKED) + print('STATUS: ACCEPTED') + return True @classmethod def check_until_invoice_locked(cls, payment_hash, expiration): @@ -100,7 +119,7 @@ class LNNode(): print(invoice) if timezone.now > expiration: break - if invoice.state == 'ACCEPTED': + if invoice.state == 3: # True if hold invoice is accepted. return True return False @@ -160,13 +179,26 @@ class LNNode(): for response in cls.routerstub.SendPaymentV2(request, metadata=[('macaroon', MACAROON.hex())]): print(response) print(response.status) - print(response.grpc_status) + + # TODO ERROR HANDLING + if response.status == 0 : # Status 0 'UNKNOWN' + pass if response.status == 1 : # Status 1 'IN_FLIGHT' - pass # LIVE UPDATE THE order.lnpayment.status - if response.status == 'FAILED': - pass # LIVE UPDATE THE order.lnpayment.status + pass + if response.status == 3 : # 4 'FAILED' ?? + '''0 Payment isn't failed (yet). + 1 There are more routes to try, but the payment timeout was exceeded. + 2 All possible routes were tried and failed permanently. Or were no routes to the destination at all. + 3 A non-recoverable error has occured. + 4 Payment details incorrect (unknown hash, invalid amt or invalid final cltv delta) + 5 Insufficient local balance. + ''' + context = cls.payment_failure_context[response.failure_reason] + pass if response.status == 2 : # STATUS 'SUCCEEDED' return True + + # How to catch the errors like:"grpc_message":"invoice is already paid","grpc_status":6} # These are not in the response only printed to commandline diff --git a/api/logics.py b/api/logics.py index 66f6cc6e..ab4063c3 100644 --- a/api/logics.py +++ b/api/logics.py @@ -1,4 +1,4 @@ -from datetime import timedelta +from datetime import time, timedelta from django.utils import timezone from .lightning.node import LNNode @@ -345,6 +345,10 @@ class Logics(): order.last_satoshis = cls.satoshis_now(order) order.taker_bond.status = LNPayment.Status.LOCKED order.taker_bond.save() + + # Log a market tick + MarketTick.log_a_tick(order) + # With the bond confirmation the order is extended 'public_order_duration' hours order.expires_at = timezone.now() + timedelta(minutes=INVOICE_AND_ESCROW_DURATION) order.status = Order.Status.WF2 @@ -512,13 +516,19 @@ class Logics(): # 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 INVOICE !!! - order.status = Order.Status.PAY - order.buyer_invoice.status = LNPayment.Status.SETLED + is_payed, context = cls.pay_buyer_invoice(order) ##### !!! KEY LINE - PAYS THE BUYER INVOICE !!! + if is_payed: + order.status = Order.Status.SUC + order.buyer_invoice.status = LNPayment.Status.SUCCED + order.expires_at = timezone.now() + timedelta(days=1) # One day to rate / see this order. + order.save() # RETURN THE BONDS cls.return_bond(order.taker_bond) cls.return_bond(order.maker_bond) + else: + # error handling here + pass else: return False, {'bad_request':'You cannot confirm the fiat payment at this stage'} diff --git a/api/views.py b/api/views.py index 755a654e..bf8940c6 100644 --- a/api/views.py +++ b/api/views.py @@ -227,7 +227,6 @@ class OrderView(viewsets.ViewSet): # 2) If action is 'update invoice' if action == 'update_invoice' and invoice: - print('AAAAAAAAAAAAAAAAAAAAAAAAAAAA') valid, context = Logics.update_invoice(order,request.user,invoice) if not valid: return Response(context, status.HTTP_400_BAD_REQUEST) @@ -407,7 +406,7 @@ class InfoView(ListAPIView): 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_public_sell_orders'] = len(Order.objects.filter(type=Order.Types.SELL, status=Order.Status.PUB)) # Number of active users (logged in in last 30 minutes) active_user_time_range = (timezone.now() - timedelta(minutes=30), timezone.now()) diff --git a/frontend/src/components/TradeBox.js b/frontend/src/components/TradeBox.js index 5a9ceb2b..ba322944 100644 --- a/frontend/src/components/TradeBox.js +++ b/frontend/src/components/TradeBox.js @@ -435,8 +435,8 @@ handleRatingChange=(e)=>{ {this.props.data.isSeller & this.props.data.statusCode == 10 ? this.showChat(false,true,true) : ""} {/* Trade Finished */} - {this.props.data.isSeller & this.props.data.statusCode > 12 & this.props.data.statusCode < 15 ? this.showRateSelect() : ""} - {this.props.data.isBuyer & this.props.data.statusCode == 14 ? this.showRateSelect() : ""} + {(this.props.data.isSeller & this.props.data.statusCode > 12 & this.props.data.statusCode < 15) ? this.showRateSelect() : ""} + {(this.props.data.isBuyer & this.props.data.statusCode == 14) ? this.showRateSelect() : ""} {/* Trade Finished - Payment Routing Failed */} {this.props.data.isBuyer & this.props.data.statusCode == 15 ? this.showUpdateInvoice() : ""} diff --git a/frontend/static/assets/currencies.json b/frontend/static/assets/currencies.json index f246908d..b187e3b1 100644 --- a/frontend/static/assets/currencies.json +++ b/frontend/static/assets/currencies.json @@ -1,8 +1,31 @@ { "1":"USD", "2":"EUR", - "3":"ETH", - "4":"AUD", - "5":"BRL", - "6":"CAD" -} + "3":"JPY", + "4":"GBP", + "5":"AUD", + "6":"CAD", + "7":"CHF", + "8":"CNY", + "9":"HKD", + "10":"NZD", + "11":"SEK", + "12":"KRW", + "13":"SGD", + "14":"NOK", + "15":"MXN", + "16":"KRW", + "17":"RUB", + "18":"ZAR", + "19":"TRY", + "20":"BRL", + "21": "CLP", + "22": "CZK", + "23": "DKK", + "24": "HKR", + "25": "HUF", + "26": "INR", + "27": "ISK", + "28": "PLN", + "29": "RON" +} \ No newline at end of file