Skip to content

Commit

Permalink
Mint: lookup internal quote settlement by request (#478)
Browse files Browse the repository at this point in the history
  • Loading branch information
callebtc authored Mar 16, 2024
1 parent e7b1e0c commit 86b8f58
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
12 changes: 6 additions & 6 deletions cashu/mint/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,10 @@ async def get_mint_quote(
...

@abstractmethod
async def get_mint_quote_by_checking_id(
async def get_mint_quote_by_request(
self,
*,
checking_id: str,
request: str,
db: Database,
conn: Optional[Connection] = None,
) -> Optional[MintQuote]:
Expand Down Expand Up @@ -403,19 +403,19 @@ async def get_mint_quote(
)
return MintQuote.from_row(row) if row else None

async def get_mint_quote_by_checking_id(
async def get_mint_quote_by_request(
self,
*,
checking_id: str,
request: str,
db: Database,
conn: Optional[Connection] = None,
) -> Optional[MintQuote]:
row = await (conn or db).fetchone(
f"""
SELECT * from {table_with_schema(db, 'mint_quotes')}
WHERE checking_id = ?
WHERE request = ?
""",
(checking_id,),
(request,),
)
return MintQuote.from_row(row) if row else None

Expand Down
12 changes: 6 additions & 6 deletions cashu/mint/ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,8 +475,8 @@ async def melt_quote(
# check if there is a mint quote with the same payment request
# so that we can handle the transaction internally without lightning
# and respond with zero fees
mint_quote = await self.crud.get_mint_quote_by_checking_id(
checking_id=invoice_obj.payment_hash, db=self.db
mint_quote = await self.crud.get_mint_quote_by_request(
request=melt_quote.request, db=self.db
)
if mint_quote:
# internal transaction, validate and return amount from
Expand Down Expand Up @@ -561,8 +561,8 @@ async def get_melt_quote(

# we only check the state with the backend if there is no associated internal
# mint quote for this melt quote
mint_quote = await self.crud.get_mint_quote_by_checking_id(
checking_id=melt_quote.checking_id, db=self.db
mint_quote = await self.crud.get_mint_quote_by_request(
request=melt_quote.request, db=self.db
)

if not melt_quote.paid and not mint_quote and check_quote_with_backend:
Expand Down Expand Up @@ -600,8 +600,8 @@ async def melt_mint_settle_internally(self, melt_quote: MeltQuote) -> MeltQuote:
"""
# first we check if there is a mint quote with the same payment request
# so that we can handle the transaction internally without the backend
mint_quote = await self.crud.get_mint_quote_by_checking_id(
checking_id=melt_quote.checking_id, db=self.db
mint_quote = await self.crud.get_mint_quote_by_request(
request=melt_quote.request, db=self.db
)
if not mint_quote:
return melt_quote
Expand Down
16 changes: 16 additions & 0 deletions tests/test_mint_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ async def test_mint_quote(wallet1: Wallet, ledger: Ledger):
assert quote.created_time


@pytest.mark.asyncio
async def test_get_mint_quote_by_request(wallet1: Wallet, ledger: Ledger):
invoice = await wallet1.request_mint(128)
assert invoice is not None
quote = await ledger.crud.get_mint_quote_by_request(
request=invoice.bolt11, db=ledger.db
)
assert quote is not None
assert quote.quote == invoice.id
assert quote.amount == 128
assert quote.unit == "sat"
assert not quote.paid
assert quote.paid_time is None
assert quote.created_time


@pytest.mark.asyncio
async def test_melt_quote(wallet1: Wallet, ledger: Ledger):
invoice = await wallet1.request_mint(128)
Expand Down

0 comments on commit 86b8f58

Please sign in to comment.