Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a cancel_invoice function. #12

Merged
merged 1 commit into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions examples/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,16 @@
print(f"Simulated payment to the invoice done with ID = {payment.id}")
print("")

# Create and cancel an invoice
invoice = client.create_invoice(
node_id=node_id,
amount_msats=42000,
memo="Cancelable Pizza!",
)
print(f"Invoice created from {node_name}: {invoice.id}")
cancelled_invoice = client.cancel_invoice(invoice_id=invoice.id)
print(f"Cancelled invoice {cancelled_invoice.id}")

# Pay invoice sample
#
test_invoice = client.create_test_mode_invoice(
Expand Down
2 changes: 2 additions & 0 deletions lightspark/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
from lightspark.objects.Balances import Balances
from lightspark.objects.BitcoinNetwork import BitcoinNetwork
from lightspark.objects.BlockchainBalance import BlockchainBalance
from lightspark.objects.CancelInvoiceInput import CancelInvoiceInput
from lightspark.objects.CancelInvoiceOutput import CancelInvoiceOutput
from lightspark.objects.Channel import Channel
from lightspark.objects.ChannelClosingTransaction import ChannelClosingTransaction
from lightspark.objects.ChannelFees import ChannelFees
Expand Down
21 changes: 21 additions & 0 deletions lightspark/lightspark_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
)
from lightspark.requests.requester import Requester
from lightspark.scripts.bitcoin_fee_estimate import BITCOIN_FEE_ESTIMATE_QUERY
from lightspark.scripts.cancel_invoice import CANCEL_INVOICE_MUTATION
from lightspark.scripts.claim_uma_invitation import (
CLAIM_UMA_INVITATION_MUTATION,
CLAIM_UMA_INVITATION_WITH_INCENTIVES_MUTATION,
Expand Down Expand Up @@ -210,6 +211,26 @@ def create_lnurl_invoice(
self._requester, json["create_lnurl_invoice"]["invoice"]
)

def cancel_invoice(
self,
invoice_id: str,
) -> Invoice:
"""Cancels an existing unpaid invoice and returns that invoice. Cancelled invoices cannot be paid.

Args:
invoice_id (str): The ID of the invoice to cancel.

Returns:
Invoice: An `Invoice` object representing the cancelled invoice.
"""
logger.info("Canceling an invoice with id %s.", invoice_id)
json = self._requester.execute_graphql(
CANCEL_INVOICE_MUTATION,
{"invoice_id": invoice_id},
)

return Invoice_from_json(self._requester, json["cancel_invoice"]["invoice"])

def create_node_wallet_address(
self,
node_id: str,
Expand Down
20 changes: 20 additions & 0 deletions lightspark/objects/CancelInvoiceInput.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright ©, 2022-present, Lightspark Group, Inc. - All Rights Reserved

from dataclasses import dataclass
from typing import Any, Mapping


@dataclass
class CancelInvoiceInput:
invoice_id: str

def to_json(self) -> Mapping[str, Any]:
return {
"cancel_invoice_input_invoice_id": self.invoice_id,
}


def from_json(obj: Mapping[str, Any]) -> CancelInvoiceInput:
return CancelInvoiceInput(
invoice_id=obj["cancel_invoice_input_invoice_id"],
)
35 changes: 35 additions & 0 deletions lightspark/objects/CancelInvoiceOutput.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Copyright ©, 2022-present, Lightspark Group, Inc. - All Rights Reserved

from dataclasses import dataclass
from typing import Any, Mapping

from lightspark.requests.requester import Requester


@dataclass
class CancelInvoiceOutput:
requester: Requester

invoice_id: str

def to_json(self) -> Mapping[str, Any]:
return {
"cancel_invoice_output_invoice": {"id": self.invoice_id},
}


FRAGMENT = """
fragment CancelInvoiceOutputFragment on CancelInvoiceOutput {
__typename
cancel_invoice_output_invoice: invoice {
id
}
}
"""


def from_json(requester: Requester, obj: Mapping[str, Any]) -> CancelInvoiceOutput:
return CancelInvoiceOutput(
requester=requester,
invoice_id=obj["cancel_invoice_output_invoice"]["id"],
)
19 changes: 19 additions & 0 deletions lightspark/scripts/cancel_invoice.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright ©, 2022-present, Lightspark Group, Inc. - All Rights Reserved

from lightspark.objects.Invoice import FRAGMENT as InvoiceFragment

CANCEL_INVOICE_MUTATION = f"""
mutation CancelInvoice(
$invoice_id: ID!
) {{
cancel_invoice(input: {{
invoice_id: $invoice_id
}}) {{
invoice {{
...InvoiceFragment
}}
}}
}}

{InvoiceFragment}
"""