Skip to content

Commit 01d4538

Browse files
authored
Merge pull request #38 from lightsparkdev/feat/idempotency
Add idempotency keys where needed in Client and queries
2 parents 6ccee4c + adaeae9 commit 01d4538

File tree

5 files changed

+64
-7
lines changed

5 files changed

+64
-7
lines changed

lightspark/lightspark_client.py

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@
9696
from lightspark.scripts.lightning_fee_estimate_for_node import (
9797
LIGHTNING_FEE_ESTIMATE_FOR_NODE_QUERY,
9898
)
99+
from lightspark.scripts.outgoing_payment_for_idempotency_key import (
100+
OUTGOING_PAYMENT_FOR_IDEMPOTENCY_KEY_QUERY,
101+
)
99102
from lightspark.scripts.outgoing_payments_for_invoice import (
100103
OUTGOING_PAYMENTS_FOR_INVOICE_QUERY,
101104
)
@@ -497,6 +500,7 @@ def pay_invoice(
497500
timeout_secs: int,
498501
maximum_fees_msats: int,
499502
amount_msats: Optional[int] = None,
503+
idempotency_key: Optional[str] = None,
500504
) -> OutgoingPayment:
501505
variables = {
502506
"node_id": node_id,
@@ -506,6 +510,8 @@ def pay_invoice(
506510
}
507511
if amount_msats is not None:
508512
variables["amount_msats"] = amount_msats
513+
if idempotency_key is not None:
514+
variables["idempotency_key"] = idempotency_key
509515
json = self._requester.execute_graphql(
510516
PAY_INVOICE_MUTATION,
511517
variables,
@@ -522,6 +528,7 @@ def pay_uma_invoice(
522528
timeout_secs: int,
523529
maximum_fees_msats: int,
524530
amount_msats: Optional[int] = None,
531+
idempotency_key: Optional[str] = None,
525532
) -> OutgoingPayment:
526533
variables = {
527534
"node_id": node_id,
@@ -531,6 +538,8 @@ def pay_uma_invoice(
531538
}
532539
if amount_msats is not None:
533540
variables["amount_msats"] = amount_msats
541+
if idempotency_key is not None:
542+
variables["idempotency_key"] = idempotency_key
534543
json = self._requester.execute_graphql(
535544
PAY_UMA_INVOICE_MUTATION,
536545
variables,
@@ -615,21 +624,24 @@ def request_withdrawal(
615624
amount_sats: int,
616625
bitcoin_address: str,
617626
withdrawal_mode: WithdrawalMode,
627+
idempotency_key: Optional[str] = None,
618628
) -> WithdrawalRequest:
619629
"""Withdraws funds from the account and sends it to the requested bitcoin address.
620630
621631
Depending on the chosen mode, it will first take the funds from the wallet, and if applicable, close channels appropriately to recover enough funds and reopen channels with the remaining funds.
622632
The process is asynchronous and may take up to a few minutes. You can check the progress by polling the `WithdrawalRequest` that is created, or by subscribing to a webhook.
623633
"""
624-
634+
variables = {
635+
"node_id": node_id,
636+
"amount_sats": amount_sats,
637+
"bitcoin_address": bitcoin_address,
638+
"withdrawal_mode": withdrawal_mode,
639+
}
640+
if idempotency_key is not None:
641+
variables["idempotency_key"] = idempotency_key
625642
json = self._requester.execute_graphql(
626643
REQUEST_WITHDRAWAL_MUTATION,
627-
{
628-
"node_id": node_id,
629-
"amount_sats": amount_sats,
630-
"bitcoin_address": bitcoin_address,
631-
"withdrawal_mode": withdrawal_mode,
632-
},
644+
variables,
633645
self.get_signing_key(node_id),
634646
)
635647
return WithdrawalRequest_from_json(
@@ -723,6 +735,26 @@ def outgoing_payments_for_payment_hash(
723735
for payment in json["outgoing_payments_for_payment_hash"]["payments"]
724736
]
725737

738+
def outgoing_payment_for_idempotency_key(
739+
self,
740+
idempotency_key: str,
741+
) -> Optional[OutgoingPayment]:
742+
"""
743+
Fetches the outgoing payment (if any) which have been made for a given idempotency key.
744+
"""
745+
746+
json = self._requester.execute_graphql(
747+
OUTGOING_PAYMENT_FOR_IDEMPOTENCY_KEY_QUERY,
748+
{"idempotency_key": idempotency_key},
749+
)
750+
if "outgoing_payment_for_idempotency_key" not in json:
751+
return None
752+
if "payment" not in json["outgoing_payment_for_idempotency_key"]:
753+
return None
754+
return OutgoingPayment_from_json(
755+
self._requester, json["outgoing_payment_for_idempotency_key"]["payment"]
756+
)
757+
726758
def incoming_payments_for_invoice(
727759
self,
728760
invoice_id: str,
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright ©, 2022-present, Lightspark Group, Inc. - All Rights Reserved
2+
3+
from lightspark.objects.OutgoingPayment import FRAGMENT as OutgoingPaymentFragment
4+
5+
OUTGOING_PAYMENT_FOR_IDEMPOTENCY_KEY_QUERY = f"""
6+
query OutgoingPaymentForIdempotencyKey(
7+
$idempotency_key: String!
8+
) {{
9+
outgoing_payment_for_idempotency_key(input: {{
10+
idempotency_key: $idempotency_key
11+
}}) {{
12+
payment {{
13+
...OutgoingPaymentFragment
14+
}}
15+
}}
16+
}}
17+
18+
{OutgoingPaymentFragment}
19+
"""

lightspark/scripts/pay_invoice.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99
$timeout_secs: Int!
1010
$maximum_fees_msats: Long!
1111
$amount_msats: Long
12+
$idempotency_key: String
1213
) {{
1314
pay_invoice(input: {{
1415
node_id: $node_id
1516
encoded_invoice: $encoded_invoice
1617
timeout_secs: $timeout_secs
1718
maximum_fees_msats: $maximum_fees_msats
1819
amount_msats: $amount_msats
20+
idempotency_key: $idempotency_key
1921
}}) {{
2022
payment {{
2123
...OutgoingPaymentFragment

lightspark/scripts/pay_uma_invoice.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99
$timeout_secs: Int!
1010
$maximum_fees_msats: Long!
1111
$amount_msats: Long
12+
$idempotency_key: String
1213
) {{
1314
pay_uma_invoice(input: {{
1415
node_id: $node_id
1516
encoded_invoice: $encoded_invoice
1617
timeout_secs: $timeout_secs
1718
maximum_fees_msats: $maximum_fees_msats
1819
amount_msats: $amount_msats
20+
idempotency_key: $idempotency_key
1921
}}) {{
2022
payment {{
2123
...OutgoingPaymentFragment

lightspark/scripts/request_withdrawal.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
$amount_sats: Long!
99
$bitcoin_address: String!
1010
$withdrawal_mode: WithdrawalMode!
11+
$idempotency_key: String
1112
) {{
1213
request_withdrawal(input: {{
1314
node_id: $node_id
1415
amount_sats: $amount_sats
1516
bitcoin_address: $bitcoin_address
1617
withdrawal_mode: $withdrawal_mode
18+
idempotency_key: $idempotency_key
1719
}}) {{
1820
request {{
1921
...WithdrawalRequestFragment

0 commit comments

Comments
 (0)