-
-
Notifications
You must be signed in to change notification settings - Fork 98
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
REFACTOR: paid
flag -> PaymentResult
object
#619
Conversation
cashu/lightning/base.py
Outdated
def __str__(self): | ||
return self.name | ||
|
||
# We assume `None` is `FAILED` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that None
should be PENDING
, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe you are right. But we also used None
to signal what now would be UNKNOWN
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't matter too much, though. One shouldn't be using that. I only ever created it to use it in FakeWallet
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then I suggest to change this code to assume that None
is PENDING
.
That way it is consistent with rest of the old and new code.
I am having trouble with LNBits. The code for Also the docs don't mention what values the fields might take. For example |
cashu/lightning/base.py
Outdated
|
||
class PaymentResponse(BaseModel): | ||
result: PaymentResult | ||
ok: Optional[bool] = None # True: paid, False: failed, None: pending or unknown |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok: Optional[bool] = None # True: paid, False: failed, None: pending or unknown |
I suggest we remove all legacy flags
cashu/lightning/base.py
Outdated
# We assume `None` is `PENDING` | ||
@classmethod | ||
def from_paid_flag(cls, paid: Optional[bool]): | ||
if paid is None: | ||
return cls.PENDING | ||
elif not paid: | ||
return cls.FAILED | ||
elif paid: | ||
return cls.SETTLED |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# We assume `None` is `PENDING` | |
@classmethod | |
def from_paid_flag(cls, paid: Optional[bool]): | |
if paid is None: | |
return cls.PENDING | |
elif not paid: | |
return cls.FAILED | |
elif paid: | |
return cls.SETTLED |
We can get rid of the old paid
flag altogether.
cashu/lightning/base.py
Outdated
|
||
class PaymentStatus(BaseModel): | ||
result: PaymentResult | ||
paid: Optional[bool] = None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
paid: Optional[bool] = None |
This will be used to fix #614 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! Left some suggestions...
except Exception as e: | ||
print(e) | ||
return PaymentStatus(paid=False) | ||
return PaymentStatus(result=PaymentResult.FAILED) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it a given that the status is FAILED
here?
@@ -135,7 +136,7 @@ async def test_blink_pay_invoice_failure(): | |||
state=MeltQuoteState.unpaid, | |||
) | |||
payment = await blink.pay_invoice(quote, 1000) | |||
assert not payment.ok | |||
assert payment.result != PaymentResult.SETTLED |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert payment.result != PaymentResult.SETTLED | |
assert payment.settled |
@@ -155,7 +156,7 @@ async def test_blink_get_invoice_status(): | |||
} | |||
respx.post(blink.endpoint).mock(return_value=Response(200, json=mock_response)) | |||
status = await blink.get_invoice_status("123") | |||
assert status.paid | |||
assert status.result == PaymentResult.SETTLED |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert status.result == PaymentResult.SETTLED | |
assert status.settled |
|
||
t1.start() | ||
t2.start() | ||
t1.join() | ||
t2.join() | ||
|
||
assert wallet.balance <= 256 - 32 | ||
assert wallet.balance == 64 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert wallet.balance == 64 | |
assert wallet.balance == 128 - 32 |
We have 128 in the wallet, but we only pay 32 from the wallet. The other 32 is paid from another node.
@@ -183,7 +184,7 @@ async def test_blink_get_payment_status(): | |||
} | |||
respx.post(blink.endpoint).mock(return_value=Response(200, json=mock_response)) | |||
status = await blink.get_payment_status(payment_request) | |||
assert status.paid | |||
assert status.result == PaymentResult.SETTLED |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert status.result == PaymentResult.SETTLED | |
assert status.settled |
@@ -102,7 +103,7 @@ async def test_blink_pay_invoice(): | |||
state=MeltQuoteState.unpaid, | |||
) | |||
payment = await blink.pay_invoice(quote, 1000) | |||
assert payment.ok | |||
assert payment.result == PaymentResult.SETTLED |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert payment.result == PaymentResult.SETTLED | |
assert payment.settled |
states = await ledger.db_read.get_proofs_states([p.Y for p in send_proofs]) | ||
assert all([s.unspent for s in states]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's awesome!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we only check this on startup?
Co-authored-by: lollerfirst <[email protected]>
I think this is not needed anymore. |
This PR is to deprecate the
paid
flag inPaymentResponse
andPaymentStatus
in favor of a more structuredPaymentResult
, that allows for more flexibility.I have modified the backends to fill in this new field. The main difference is we DO NOT return
SETTLED
when paying an invoice unless we get an explicit confirmation, instead we returnPENDING
and thenmelt
notices this and checks the payment status.I have also adapted the code in
get_mint_quote
andmelt
to rely on the newly addedresult
field.