Record expiry height into lnpayment and admin panel

This commit is contained in:
Reckless_Satoshi
2022-01-25 06:46:02 -08:00
parent 2d1a2e4c5c
commit 5a677067f7
10 changed files with 75 additions and 52 deletions

View File

@ -25,14 +25,14 @@ 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_link','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 = ('id','type','maker_link','taker_link','status','amount','currency_link','t0_satoshis','is_disputed','is_fiat_sent','created_at','expires_at', 'payout_link','maker_bond_link','taker_bond_link','trade_escrow_link')
list_display_links = ('id','type') list_display_links = ('id','type')
change_links = ('maker','taker','currency','buyer_invoice','maker_bond','taker_bond','trade_escrow') change_links = ('maker','taker','currency','payout','maker_bond','taker_bond','trade_escrow')
list_filter = ('is_disputed','is_fiat_sent','type','currency','status') list_filter = ('is_disputed','is_fiat_sent','type','currency','status')
@admin.register(LNPayment) @admin.register(LNPayment)
class LNPaymentAdmin(AdminChangeLinksMixin, admin.ModelAdmin): class LNPaymentAdmin(AdminChangeLinksMixin, admin.ModelAdmin):
list_display = ('hash','concept','status','num_satoshis','type','expires_at','sender_link','receiver_link','order_made_link','order_taken_link','order_escrow_link','order_paid_link') list_display = ('hash','concept','status','num_satoshis','type','expires_at','expiry_height','sender_link','receiver_link','order_made_link','order_taken_link','order_escrow_link','order_paid_link')
list_display_links = ('hash','concept') list_display_links = ('hash','concept')
change_links = ('sender','receiver','order_made','order_taken','order_escrow','order_paid') change_links = ('sender','receiver','order_made','order_taken','order_escrow','order_paid')
list_filter = ('type','concept','status') list_filter = ('type','concept','status')

View File

@ -96,9 +96,9 @@ class LNNode():
return hold_payment return hold_payment
@classmethod @classmethod
def validate_hold_invoice_locked(cls, payment_hash): def validate_hold_invoice_locked(cls, lnpayment):
'''Checks if hold invoice is locked''' '''Checks if hold invoice is locked'''
request = invoicesrpc.LookupInvoiceMsg(payment_hash=bytes.fromhex(payment_hash)) request = invoicesrpc.LookupInvoiceMsg(payment_hash=bytes.fromhex(lnpayment.payment_hash))
response = cls.invoicesstub.LookupInvoiceV2(request, metadata=[('macaroon', MACAROON.hex())]) response = cls.invoicesstub.LookupInvoiceV2(request, metadata=[('macaroon', MACAROON.hex())])
print('status here') print('status here')
print(response.state) print(response.state)
@ -116,6 +116,8 @@ class LNNode():
pass pass
if response.state == 3: # ACCEPTED (LOCKED) if response.state == 3: # ACCEPTED (LOCKED)
print('STATUS: ACCEPTED') print('STATUS: ACCEPTED')
lnpayment.expiry_height = response.htlcs[0].expiry_height
lnpayment.save()
return True return True
@classmethod @classmethod
@ -140,7 +142,7 @@ class LNNode():
def validate_ln_invoice(cls, invoice, num_satoshis): def validate_ln_invoice(cls, invoice, num_satoshis):
'''Checks if the submited LN invoice comforms to expectations''' '''Checks if the submited LN invoice comforms to expectations'''
buyer_invoice = { payout = {
'valid': False, 'valid': False,
'context': None, 'context': None,
'description': None, 'description': None,
@ -153,30 +155,30 @@ class LNNode():
payreq_decoded = cls.decode_payreq(invoice) payreq_decoded = cls.decode_payreq(invoice)
print(payreq_decoded) print(payreq_decoded)
except: except:
buyer_invoice['context'] = {'bad_invoice':'Does not look like a valid lightning invoice'} payout['context'] = {'bad_invoice':'Does not look like a valid lightning invoice'}
return buyer_invoice return payout
if payreq_decoded.num_satoshis == 0: if payreq_decoded.num_satoshis == 0:
buyer_invoice['context'] = {'bad_invoice':'The invoice provided has no explicit amount'} payout['context'] = {'bad_invoice':'The invoice provided has no explicit amount'}
return buyer_invoice return payout
if not payreq_decoded.num_satoshis == num_satoshis: if not payreq_decoded.num_satoshis == num_satoshis:
buyer_invoice['context'] = {'bad_invoice':'The invoice provided is not for '+'{:,}'.format(num_satoshis)+ ' Sats'} payout['context'] = {'bad_invoice':'The invoice provided is not for '+'{:,}'.format(num_satoshis)+ ' Sats'}
return buyer_invoice return payout
buyer_invoice['created_at'] = timezone.make_aware(datetime.fromtimestamp(payreq_decoded.timestamp)) payout['created_at'] = timezone.make_aware(datetime.fromtimestamp(payreq_decoded.timestamp))
buyer_invoice['expires_at'] = buyer_invoice['created_at'] + timedelta(seconds=payreq_decoded.expiry) payout['expires_at'] = payout['created_at'] + timedelta(seconds=payreq_decoded.expiry)
if buyer_invoice['expires_at'] < timezone.now(): if payout['expires_at'] < timezone.now():
buyer_invoice['context'] = {'bad_invoice':f'The invoice provided has already expired'} payout['context'] = {'bad_invoice':f'The invoice provided has already expired'}
return buyer_invoice return payout
buyer_invoice['valid'] = True payout['valid'] = True
buyer_invoice['description'] = payreq_decoded.description payout['description'] = payreq_decoded.description
buyer_invoice['payment_hash'] = payreq_decoded.payment_hash payout['payment_hash'] = payreq_decoded.payment_hash
return buyer_invoice return payout
@classmethod @classmethod
def pay_invoice(cls, invoice, num_satoshis): def pay_invoice(cls, invoice, num_satoshis):

View File

@ -265,7 +265,7 @@ class Logics():
return True, None return True, None
@classmethod @classmethod
def buyer_invoice_amount(cls, order, user): def payout_amount(cls, order, user):
''' Computes buyer invoice amount. Uses order.last_satoshis, ''' Computes buyer invoice amount. Uses order.last_satoshis,
that is the final trade amount set at Taker Bond time''' that is the final trade amount set at Taker Bond time'''
@ -285,27 +285,27 @@ class Logics():
if not (order.taker_bond.status == order.maker_bond.status == LNPayment.Status.LOCKED): if not (order.taker_bond.status == order.maker_bond.status == LNPayment.Status.LOCKED):
return False, {'bad_request':'You cannot submit a invoice while bonds are not locked.'} return False, {'bad_request':'You cannot submit a invoice while bonds are not locked.'}
num_satoshis = cls.buyer_invoice_amount(order, user)[1]['invoice_amount'] num_satoshis = cls.payout_amount(order, user)[1]['invoice_amount']
buyer_invoice = LNNode.validate_ln_invoice(invoice, num_satoshis) payout = LNNode.validate_ln_invoice(invoice, num_satoshis)
if not buyer_invoice['valid']: if not payout['valid']:
return False, buyer_invoice['context'] return False, payout['context']
order.buyer_invoice, _ = LNPayment.objects.update_or_create( order.payout, _ = LNPayment.objects.update_or_create(
concept = LNPayment.Concepts.PAYBUYER, concept = LNPayment.Concepts.PAYBUYER,
type = LNPayment.Types.NORM, type = LNPayment.Types.NORM,
sender = User.objects.get(username=ESCROW_USERNAME), sender = User.objects.get(username=ESCROW_USERNAME),
order_paid = order, # In case this user has other buyer_invoices, update the one related to this order. order_paid = order, # In case this user has other payouts, update the one related to this order.
receiver= user, receiver= user,
# if there is a LNPayment matching these above, it updates that one with defaults below. # if there is a LNPayment matching these above, it updates that one with defaults below.
defaults={ defaults={
'invoice' : invoice, 'invoice' : invoice,
'status' : LNPayment.Status.VALIDI, 'status' : LNPayment.Status.VALIDI,
'num_satoshis' : num_satoshis, 'num_satoshis' : num_satoshis,
'description' : buyer_invoice['description'], 'description' : payout['description'],
'payment_hash' : buyer_invoice['payment_hash'], 'payment_hash' : payout['payment_hash'],
'created_at' : buyer_invoice['created_at'], 'created_at' : payout['created_at'],
'expires_at' : buyer_invoice['expires_at']} 'expires_at' : payout['expires_at']}
) )
# If the order status is 'Waiting for invoice'. Move forward to 'chat' # If the order status is 'Waiting for invoice'. Move forward to 'chat'
@ -324,7 +324,9 @@ class Logics():
# If the order status is 'Failed Routing'. Retry payment. # If the order status is 'Failed Routing'. Retry payment.
if order.status == Order.Status.FAI: if order.status == Order.Status.FAI:
follow_send_payment(order.buyer_invoice) # Double check the escrow is settled.
if LNNode.double_check_htlc_is_settled(order.trade_escrow.payment_hash):
follow_send_payment(order.payout)
order.save() order.save()
return True, None return True, None
@ -476,7 +478,7 @@ class Logics():
def is_maker_bond_locked(cls, order): def is_maker_bond_locked(cls, order):
if order.maker_bond.status == LNPayment.Status.LOCKED: if order.maker_bond.status == LNPayment.Status.LOCKED:
return True return True
elif LNNode.validate_hold_invoice_locked(order.maker_bond.payment_hash): elif LNNode.validate_hold_invoice_locked(order.maker_bond):
order.maker_bond.status = LNPayment.Status.LOCKED order.maker_bond.status = LNPayment.Status.LOCKED
order.maker_bond.save() order.maker_bond.save()
cls.publish_order(order) cls.publish_order(order)
@ -558,7 +560,7 @@ class Logics():
def is_taker_bond_locked(cls, order): def is_taker_bond_locked(cls, order):
if order.taker_bond.status == LNPayment.Status.LOCKED: if order.taker_bond.status == LNPayment.Status.LOCKED:
return True return True
elif LNNode.validate_hold_invoice_locked(order.taker_bond.payment_hash): elif LNNode.validate_hold_invoice_locked(order.taker_bond):
cls.finalize_contract(order) cls.finalize_contract(order)
return True return True
return False return False
@ -625,7 +627,7 @@ class Logics():
def is_trade_escrow_locked(cls, order): def is_trade_escrow_locked(cls, order):
if order.trade_escrow.status == LNPayment.Status.LOCKED: if order.trade_escrow.status == LNPayment.Status.LOCKED:
return True return True
elif LNNode.validate_hold_invoice_locked(order.trade_escrow.payment_hash): elif LNNode.validate_hold_invoice_locked(order.trade_escrow):
order.trade_escrow.status = LNPayment.Status.LOCKED order.trade_escrow.status = LNPayment.Status.LOCKED
order.trade_escrow.save() order.trade_escrow.save()
cls.trade_escrow_received(order) cls.trade_escrow_received(order)
@ -761,7 +763,7 @@ class Logics():
return False, {'bad_request':'You cannot confirm to have received the fiat before it is confirmed to be sent by the buyer.'} return False, {'bad_request':'You cannot confirm to have received the fiat before it is confirmed to be sent by the buyer.'}
# Make sure the trade escrow is at least as big as the buyer invoice # Make sure the trade escrow is at least as big as the buyer invoice
if order.trade_escrow.num_satoshis <= order.buyer_invoice.num_satoshis: if order.trade_escrow.num_satoshis <= order.payout.num_satoshis:
return False, {'bad_request':'Woah, something broke badly. Report in the public channels, or open a Github Issue.'} return False, {'bad_request':'Woah, something broke badly. Report in the public channels, or open a Github Issue.'}
if cls.settle_escrow(order): ##### !!! KEY LINE - SETTLES THE TRADE ESCROW !!! if cls.settle_escrow(order): ##### !!! KEY LINE - SETTLES THE TRADE ESCROW !!!
@ -769,7 +771,7 @@ class Logics():
# Double check the escrow is settled. # Double check the escrow is settled.
if LNNode.double_check_htlc_is_settled(order.trade_escrow.payment_hash): if LNNode.double_check_htlc_is_settled(order.trade_escrow.payment_hash):
is_payed, context = follow_send_payment(order.buyer_invoice) ##### !!! KEY LINE - PAYS THE BUYER INVOICE !!! is_payed, context = follow_send_payment(order.payout) ##### !!! KEY LINE - PAYS THE BUYER INVOICE !!!
if is_payed: if is_payed:
# RETURN THE BONDS // Probably best also do it even if payment failed # RETURN THE BONDS // Probably best also do it even if payment failed
cls.return_bond(order.taker_bond) cls.return_bond(order.taker_bond)

View File

@ -70,6 +70,7 @@ class Command(BaseCommand):
request = LNNode.invoicesrpc.LookupInvoiceMsg(payment_hash=bytes.fromhex(hold_lnpayment.payment_hash)) request = LNNode.invoicesrpc.LookupInvoiceMsg(payment_hash=bytes.fromhex(hold_lnpayment.payment_hash))
response = stub.LookupInvoiceV2(request, metadata=[('macaroon', MACAROON.hex())]) response = stub.LookupInvoiceV2(request, metadata=[('macaroon', MACAROON.hex())])
hold_lnpayment.status = lnd_state_to_lnpayment_status[response.state] hold_lnpayment.status = lnd_state_to_lnpayment_status[response.state]
hold_lnpayment.expiry_height = response.htlcs[0].expiry_height
except Exception as e: except Exception as e:
# If it fails at finding the invoice: it has been canceled. # If it fails at finding the invoice: it has been canceled.

View File

@ -72,6 +72,7 @@ class LNPayment(models.Model):
created_at = models.DateTimeField() created_at = models.DateTimeField()
expires_at = models.DateTimeField() expires_at = models.DateTimeField()
cltv_expiry = models.PositiveSmallIntegerField(null=True, default=None, blank=True) cltv_expiry = models.PositiveSmallIntegerField(null=True, default=None, blank=True)
expiry_height = models.PositiveBigIntegerField(null=True, default=None, blank=True)
# routing # routing
routing_attempts = models.PositiveSmallIntegerField(null=False, default=0) routing_attempts = models.PositiveSmallIntegerField(null=False, default=0)
@ -161,7 +162,7 @@ class Order(models.Model):
taker_bond = models.OneToOneField(LNPayment, related_name='order_taken', on_delete=models.SET_NULL, null=True, default=None, blank=True) taker_bond = models.OneToOneField(LNPayment, related_name='order_taken', on_delete=models.SET_NULL, null=True, default=None, blank=True)
trade_escrow = models.OneToOneField(LNPayment, related_name='order_escrow', on_delete=models.SET_NULL, null=True, default=None, blank=True) trade_escrow = models.OneToOneField(LNPayment, related_name='order_escrow', on_delete=models.SET_NULL, null=True, default=None, blank=True)
# buyer payment LN invoice # buyer payment LN invoice
buyer_invoice = models.OneToOneField(LNPayment, related_name='order_paid', on_delete=models.SET_NULL, null=True, default=None, blank=True) payout = models.OneToOneField(LNPayment, related_name='order_paid', on_delete=models.SET_NULL, null=True, default=None, blank=True)
# ratings # ratings
maker_rated = models.BooleanField(default=False, null=False) maker_rated = models.BooleanField(default=False, null=False)
@ -195,7 +196,7 @@ class Order(models.Model):
@receiver(pre_delete, sender=Order) @receiver(pre_delete, sender=Order)
def delete_lnpayment_at_order_deletion(sender, instance, **kwargs): def delete_lnpayment_at_order_deletion(sender, instance, **kwargs):
to_delete = (instance.maker_bond, instance.buyer_invoice, instance.taker_bond, instance.trade_escrow) to_delete = (instance.maker_bond, instance.payout, instance.taker_bond, instance.trade_escrow)
for lnpayment in to_delete: for lnpayment in to_delete:
try: try:

View File

@ -84,8 +84,5 @@ def compute_premium_percentile(order):
rates.append(float(similar_order.last_satoshis) / float(similar_order.amount)) rates.append(float(similar_order.last_satoshis) / float(similar_order.amount))
rates = np.array(rates) rates = np.array(rates)
print(rates)
print(order_rate)
print(np.sum(rates < order_rate))
return round(np.sum(rates < order_rate) / len(rates),2) return round(np.sum(rates < order_rate) / len(rates),2)

View File

@ -170,7 +170,7 @@ class OrderView(viewsets.ViewSet):
data['trade_satoshis'] = order.last_satoshis data['trade_satoshis'] = order.last_satoshis
# Buyer sees the amount he receives # Buyer sees the amount he receives
elif data['is_buyer']: elif data['is_buyer']:
data['trade_satoshis'] = Logics.buyer_invoice_amount(order, request.user)[1]['invoice_amount'] data['trade_satoshis'] = Logics.payout_amount(order, request.user)[1]['invoice_amount']
# 5) If status is 'waiting for maker bond' and user is MAKER, reply with a MAKER hold invoice. # 5) If status is 'waiting for maker bond' and user is MAKER, reply with a MAKER hold invoice.
if order.status == Order.Status.WFB and data['is_maker']: if order.status == Order.Status.WFB and data['is_maker']:
@ -203,7 +203,7 @@ class OrderView(viewsets.ViewSet):
# If the two bonds are locked, reply with an AMOUNT so he can send the buyer invoice. # If the two bonds are locked, reply with an AMOUNT so he can send the buyer invoice.
if order.maker_bond.status == order.taker_bond.status == LNPayment.Status.LOCKED: if order.maker_bond.status == order.taker_bond.status == LNPayment.Status.LOCKED:
valid, context = Logics.buyer_invoice_amount(order, request.user) valid, context = Logics.payout_amount(order, request.user)
if valid: if valid:
data = {**data, **context} data = {**data, **context}
else: else:
@ -233,11 +233,11 @@ class OrderView(viewsets.ViewSet):
data['statement_submitted'] = (order.taker_statement != None and order.maker_statement != "") data['statement_submitted'] = (order.taker_statement != None and order.maker_statement != "")
# 9) If status is 'Failed routing', reply with retry amounts, time of next retry and ask for invoice at third. # 9) If status is 'Failed routing', reply with retry amounts, time of next retry and ask for invoice at third.
elif order.status == Order.Status.FAI: elif order.status == Order.Status.FAI and order.payout.receiver == request.user: # might not be the buyer if after a dispute where winner wins
data['retries'] = order.buyer_invoice.routing_attempts data['retries'] = order.payout.routing_attempts
data['next_retry_time'] = order.buyer_invoice.last_routing_time + timedelta(minutes=RETRY_TIME) data['next_retry_time'] = order.payout.last_routing_time + timedelta(minutes=RETRY_TIME)
if order.buyer_invoice.status == LNPayment.Status.EXPIRE: if order.payout.status == LNPayment.Status.EXPIRE:
data['invoice_expired'] = True data['invoice_expired'] = True
# Add invoice amount once again if invoice was expired. # Add invoice amount once again if invoice was expired.
data['invoice_amount'] = int(order.last_satoshis * (1-FEE)) data['invoice_amount'] = int(order.last_satoshis * (1-FEE))

View File

@ -71,7 +71,7 @@ export default class BottomBar extends Component {
<Divider/> <Divider/>
<ListItem> <ListItem>
<ListItemIcon><GitHubIcon/></ListItemIcon> <ListItemIcon><GitHubIcon/></ListItemIcon>
<ListItemText secondary="Currently running commit height"> <ListItemText secondary="Currently running commit hash">
<a href={"https://github.com/Reckless-Satoshi/robosats/tree/" <a href={"https://github.com/Reckless-Satoshi/robosats/tree/"
+ this.state.robosats_running_commit_hash}>{this.state.robosats_running_commit_hash} + this.state.robosats_running_commit_hash}>{this.state.robosats_running_commit_hash}
</a> </a>

View File

@ -285,7 +285,7 @@ export default class OrderPage extends Component {
</DialogContent> </DialogContent>
<DialogActions> <DialogActions>
<Button onClick={this.handleClickCloseCollaborativeCancelDialog} autoFocus>Go back</Button> <Button onClick={this.handleClickCloseCollaborativeCancelDialog} autoFocus>Go back</Button>
<Button onClick={this.handleClickConfirmCollaborativeCancelButton}> Collaborative Cancel </Button> <Button onClick={this.handleClickConfirmCollaborativeCancelButton}> Proceed and Ask for Cancel </Button>
</DialogActions> </DialogActions>
</Dialog> </Dialog>
) )

View File

@ -451,6 +451,25 @@ export default class TradeBox extends Component {
)} )}
} }
showWaitForDisputeResolution=()=>{
return (
<Grid container spacing={1}>
<Grid item xs={12} align="center">
<Typography color="primary" component="subtitle1" variant="subtitle1">
<b> We have the statements </b>
</Typography>
</Grid>
<Grid item xs={12} align="left">
<Typography component="body2" variant="body2">
Wait for the staff to resolve the dispute. The dispute winner
will be asked to submit a LN invoice.
</Typography>
</Grid>
{this.showBondIsLocked()}
</Grid>
)
}
showWaitingForEscrow(){ showWaitingForEscrow(){
return( return(
<Grid container spacing={1}> <Grid container spacing={1}>
@ -764,6 +783,7 @@ handleRatingChange=(e)=>{
{/* Trade Finished - TODO Needs more planning */} {/* Trade Finished - TODO Needs more planning */}
{this.props.data.status == 11 ? this.showInDisputeStatement() : ""} {this.props.data.status == 11 ? this.showInDisputeStatement() : ""}
{this.props.data.status == 16 ? this.showWaitForDisputeResolution() : ""}
{/* Order has expired */} {/* Order has expired */}
{this.props.data.status == 5 ? this.showOrderExpired() : ""} {this.props.data.status == 5 ? this.showOrderExpired() : ""}