def post(self, request, *args, **kwargs): try: context = self.get_context_data(**kwargs) except Exception as e: logger.exception(e) raise invoice_pre_validation = request.POST.get("invoice") sign_pre_validation = request.POST.get("signature") if not invoice_pre_validation: # Invoice not provided, back to first form to get invoice form1 = self.form_class1(request.POST) context['invoice_form'] = form1 context['errors_detected'] = True elif not sign_pre_validation: # Time to sign form2 = self.form_class2(initial={ "invoice": invoice_pre_validation, "signature": "" }) context['sign_form'] = form2 context['invoice'] = invoice_pre_validation elif invoice_pre_validation and sign_pre_validation: # Got everything form2 = self.form_class2(request.POST) context['sign_form'] = form2 context['invoice'] = invoice_pre_validation error_summary_list = [] if not form2.is_valid(): context['errors_detected'] = True else: # make API call and report back the results, potentially populating error_summary_list award_id = int(context["award_id"]) node_id = int(context["node_id"]) result = ln.payaward(node_id=node_id, award_id=award_id, invoice=invoice_pre_validation, sig=sign_pre_validation) if result["payment_successful"] == True: return render(request, "payment_successful.html", context) else: error_summary_list.append("Payment failed: {}".format( result["failure_message"])) context['errors_detected'] = True context["show_error_summary"] = True context["error_summary_list"] = error_summary_list else: raise ln.LNUtilError("Invalid state") return render(request, self.template_name, context)
def gen_invoice(publish_url, memo, node_id): context = {} nodes_list = ln.get_nodes_list() if len(nodes_list) == 0: raise ln.LNUtilError("No nodes found") if not node_id: node_with_top_score = nodes_list[0] for node in nodes_list: if node["qos_score"] > node_with_top_score["qos_score"]: node_with_top_score = node node_id = node_with_top_score["id"] else: node_id = int(node_id) context["node_id"] = str(node_id) # Lookup the node name node_name = "Unknown" list_pos = 0 for pos, n in enumerate(nodes_list): if n["id"] == node_id: node_name = n["node_name"] list_pos = pos context["node_name"] = node_name next_node_id = nodes_list[(list_pos + 1) % len(nodes_list)]["id"] context["next_node_url"] = reverse( publish_url, kwargs=dict( memo=memo, node_id=next_node_id ) ) context["open_channel_url"] = reverse( "open-channel-node-selected", kwargs=dict( node_id=next_node_id ) ) try: details = ln.add_invoice(memo, node_id=node_id) except ln.LNUtilError as e: logger.exception(e) raise context['pay_req'] = details['pay_req'] deserialized_memo = json_util.deserialize_memo(memo) context['payment_amount'] = deserialized_memo.get("amt", settings.PAYMENT_AMOUNT) return context
def get_context_data(self, **kwargs): context = super(TakeCustodyView, self).get_context_data(**kwargs) award_id = int(context["award_id"]) nodes_list = ln.get_nodes_list() if len(nodes_list) == 0: raise ln.LNUtilError("No nodes found") # TODO: modularize getting best node and name lookup if 'node_id' not in context: # best node node_with_top_score = nodes_list[0] for node in nodes_list: if node["qos_score"] > node_with_top_score["qos_score"]: node_with_top_score = node node_id = node_with_top_score["id"] context["node_id"] = str(node_id) else: node_id = int(context["node_id"]) # Lookup the node name node_name = "Unknown" list_pos = 0 for pos, n in enumerate(nodes_list): if n["id"] == node_id: node_name = n["node_name"] list_pos = pos context["node_name"] = node_name next_node_id = nodes_list[(list_pos + 1) % len(nodes_list)]["id"] context["next_node_url"] = reverse("take-custody-node-selected", kwargs=dict(node_id=next_node_id, award_id=award_id)) # Lookup author and other bounty details award = BountyAward.objects.get(id=award_id) context["post"] = award.post # TODO: put into a shard function get_bounty_sats bounty_sats = 0 for b in Bounty.objects.filter(post_id=award.post.parent.id, is_active=True, is_payed=False): bounty_sats += b.amt context["amt"] = bounty_sats return context
def check_invoice(memo, node_id): if not memo: msg = "memo was not provided" logger.error(msg) raise ln.LNUtilError(msg) # Check payment and redirect if payment is confirmed node_id = int(node_id) result = ln.check_payment(memo, node_id=node_id) checkpoint_value = result["checkpoint_value"] conclusion = ln.gen_check_conclusion(checkpoint_value, node_id=node_id, memo=memo) if conclusion == ln.CHECKPOINT_DONE: post_id = result["performed_action_id"] return post_id
def get_context_data(self, **kwargs): context = super(ChannelOpenView, self).get_context_data(**kwargs) nodes_list = ln.get_nodes_list() if len(nodes_list) == 0: raise ln.LNUtilError("No nodes found") # TODO: modularize getting best node and name lookup if 'node_id' not in context: # best node node_with_top_score = nodes_list[0] for node in nodes_list: if node["qos_score"] > node_with_top_score["qos_score"]: node_with_top_score = node node_id = node_with_top_score["id"] context["node_id"] = str(node_id) else: node_id = int(context["node_id"]) # Lookup the node name node_name = "Unknown" node_key = "Unknown" connect = "Unknown" list_pos = 0 for pos, n in enumerate(nodes_list): if n["id"] == node_id: node_name = n["node_name"] list_pos = pos node_key = n["node_key"] if n["is_tor"]: connect = n["connect_tor"] else: connect = n["connect_ip"] context["node_name"] = node_name context["node_key"] = node_key context["connect"] = connect next_node_id = nodes_list[(list_pos + 1) % len(nodes_list)]["id"] context["next_node_url"] = reverse("open-channel-node-selected", kwargs=dict(node_id=next_node_id)) return context