Skip to content

Commit

Permalink
store mint url in invoices table
Browse files Browse the repository at this point in the history
  • Loading branch information
callebtc committed Oct 28, 2024
1 parent 48b0368 commit fa8c72d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions cashu/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ class Invoice(BaseModel):
paid: Union[None, bool] = False
time_created: Union[None, str, int, float] = ""
time_paid: Union[None, str, int, float] = ""
mint: Union[None, str] = None


class MeltQuoteState(Enum):
Expand Down
10 changes: 8 additions & 2 deletions cashu/wallet/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,8 @@ async def store_lightning_invoice(
await (conn or db).execute(
"""
INSERT INTO invoices
(amount, bolt11, id, payment_hash, preimage, paid, time_created, time_paid, out)
VALUES (:amount, :bolt11, :id, :payment_hash, :preimage, :paid, :time_created, :time_paid, :out)
(amount, bolt11, id, payment_hash, preimage, paid, time_created, time_paid, out, mint)
VALUES (:amount, :bolt11, :id, :payment_hash, :preimage, :paid, :time_created, :time_paid, :out, :mint)
""",
{
"amount": invoice.amount,
Expand All @@ -259,6 +259,7 @@ async def store_lightning_invoice(
"time_created": invoice.time_created,
"time_paid": invoice.time_paid,
"out": invoice.out,
"mint": invoice.mint,
},
)

Expand Down Expand Up @@ -301,6 +302,7 @@ async def get_lightning_invoices(
db: Database,
paid: Optional[bool] = None,
pending: Optional[bool] = None,
mint: Optional[str] = None,
conn: Optional[Connection] = None,
) -> List[Invoice]:
clauses = []
Expand All @@ -316,6 +318,10 @@ async def get_lightning_invoices(
clauses.append("out = :out")
values["out"] = False

if mint:
clauses.append("mint = :mint")
values["mint"] = mint

where = ""
if clauses:
where = f"WHERE {' AND '.join(clauses)}"
Expand Down
6 changes: 6 additions & 0 deletions cashu/wallet/migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,12 @@ async def m012_add_fee_to_keysets(db: Database):
await conn.execute("UPDATE keysets SET input_fee_ppk = 0")


async def m013_add_mint_column_invoices(db: Database):
async with db.connect() as conn:
# add column for storing the mint of an invoice
await conn.execute("ALTER TABLE invoices ADD COLUMN mint TEXT")


# # async def m020_add_state_to_mint_and_melt_quotes(db: Database):
# # async with db.connect() as conn:
# # await conn.execute(
Expand Down
4 changes: 4 additions & 0 deletions cashu/wallet/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ async def request_mint_with_callback(
id=mint_qoute.quote,
out=False,
time_created=int(time.time()),
mint=self.url,
)
await store_lightning_invoice(db=self.db, invoice=invoice)
return invoice, subscriptions
Expand All @@ -435,6 +436,7 @@ async def request_mint(self, amount: int, memo: Optional[str] = None) -> Invoice
id=mint_quote_response.quote,
out=False,
time_created=int(time.time()),
mint=self.url,
)
await store_lightning_invoice(db=self.db, invoice=invoice)
return invoice
Expand Down Expand Up @@ -502,6 +504,7 @@ async def mint_quote(self, amount: int, memo: Optional[str] = None) -> Invoice:
id=mint_quote_response.quote,
out=False,
time_created=int(time.time()),
mint=self.url,
)
await store_lightning_invoice(db=self.db, invoice=invoice)
return invoice
Expand Down Expand Up @@ -775,6 +778,7 @@ async def melt(
time_paid=int(time.time()),
id=quote_id, # store the same ID in the invoice
out=True, # outgoing invoice
mint=self.url,
)
# store invoice in db as not paid yet
await store_lightning_invoice(db=self.db, invoice=invoice_obj)
Expand Down

0 comments on commit fa8c72d

Please sign in to comment.