Skip to content

Commit

Permalink
clean up multinut
Browse files Browse the repository at this point in the history
  • Loading branch information
callebtc committed Mar 23, 2024
1 parent 81ba4f7 commit 1a75192
Show file tree
Hide file tree
Showing 18 changed files with 184 additions and 137 deletions.
13 changes: 7 additions & 6 deletions cashu/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ class Proof(BaseModel):
time_created: Union[None, str] = ""
time_reserved: Union[None, str] = ""
derivation_path: Union[None, str] = "" # derivation path of the proof
mint_id: Union[
None, str
] = None # holds the id of the mint operation that created this proof
melt_id: Union[
None, str
] = None # holds the id of the melt operation that destroyed this proof
mint_id: Union[None, str] = (
None # holds the id of the mint operation that created this proof
)
melt_id: Union[None, str] = (
None # holds the id of the melt operation that destroyed this proof
)

def __init__(self, **data):
super().__init__(**data)
Expand Down Expand Up @@ -307,6 +307,7 @@ class MintMeltMethodSetting(BaseModel):
unit: str
min_amount: Optional[int] = None
max_amount: Optional[int] = None
mpp: Optional[bool] = None


class GetInfoResponse(BaseModel):
Expand Down
1 change: 1 addition & 0 deletions cashu/core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ class LndRestFundingSource(MintSettings):
mint_lnd_rest_macaroon: Optional[str] = Field(default=None)
mint_lnd_rest_admin_macaroon: Optional[str] = Field(default=None)
mint_lnd_rest_invoice_macaroon: Optional[str] = Field(default=None)
mint_lnd_enable_mpp_experimental: bool = Field(default=False)


class CoreLightningRestFundingSource(MintSettings):
Expand Down
10 changes: 7 additions & 3 deletions cashu/lightning/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@

from pydantic import BaseModel

from ..core.base import Amount, MeltQuote, Unit
from ..core.base import (
Amount,
MeltQuote,
PostMeltQuoteRequest,
Unit,
)


class StatusResponse(BaseModel):
Expand Down Expand Up @@ -108,8 +113,7 @@ def get_payment_status(
@abstractmethod
async def get_payment_quote(
self,
bolt11: str,
amount: Optional[Amount] = None,
melt_quote: PostMeltQuoteRequest,
) -> PaymentQuoteResponse:
pass

Expand Down
7 changes: 5 additions & 2 deletions cashu/lightning/blink.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
)
from loguru import logger

from ..core.base import Amount, MeltQuote, Unit
from ..core.base import Amount, MeltQuote, PostMeltQuoteRequest, Unit
from ..core.settings import settings
from .base import (
InvoiceResponse,
Expand Down Expand Up @@ -375,7 +375,10 @@ async def get_payment_status(self, checking_id: str) -> PaymentStatus:
preimage=preimage,
)

async def get_payment_quote(self, bolt11: str) -> PaymentQuoteResponse:
async def get_payment_quote(
self, melt_quote: PostMeltQuoteRequest
) -> PaymentQuoteResponse:
bolt11 = melt_quote.request
variables = {
"input": {
"paymentRequest": bolt11,
Expand Down
7 changes: 5 additions & 2 deletions cashu/lightning/corelightningrest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
)
from loguru import logger

from ..core.base import Amount, MeltQuote, Unit
from ..core.base import Amount, MeltQuote, PostMeltQuoteRequest, Unit
from ..core.helpers import fee_reserve
from ..core.settings import settings
from .base import (
Expand Down Expand Up @@ -312,7 +312,10 @@ async def paid_invoices_stream(self) -> AsyncGenerator[str, None]:
)
await asyncio.sleep(0.02)

async def get_payment_quote(self, bolt11: str) -> PaymentQuoteResponse:
async def get_payment_quote(
self, melt_quote: PostMeltQuoteRequest
) -> PaymentQuoteResponse:
bolt11 = melt_quote.request
invoice_obj = decode(bolt11)
assert invoice_obj.amount_msat, "invoice has no amount."
amount_msat = int(invoice_obj.amount_msat)
Expand Down
7 changes: 5 additions & 2 deletions cashu/lightning/fake.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
encode,
)

from ..core.base import Amount, MeltQuote, Unit
from ..core.base import Amount, MeltQuote, PostMeltQuoteRequest, Unit
from ..core.helpers import fee_reserve
from ..core.settings import settings
from .base import (
Expand Down Expand Up @@ -152,7 +152,10 @@ async def paid_invoices_stream(self) -> AsyncGenerator[str, None]:
# amount = invoice_obj.amount_msat
# return InvoiceQuoteResponse(checking_id="", amount=amount)

async def get_payment_quote(self, bolt11: str) -> PaymentQuoteResponse:
async def get_payment_quote(
self, melt_quote: PostMeltQuoteRequest
) -> PaymentQuoteResponse:
bolt11 = melt_quote.request
invoice_obj = decode(bolt11)
assert invoice_obj.amount_msat, "invoice has no amount."

Expand Down
7 changes: 5 additions & 2 deletions cashu/lightning/lnbits.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
decode,
)

from ..core.base import Amount, MeltQuote, Unit
from ..core.base import Amount, MeltQuote, PostMeltQuoteRequest, Unit
from ..core.helpers import fee_reserve
from ..core.settings import settings
from .base import (
Expand Down Expand Up @@ -157,7 +157,10 @@ async def get_payment_status(self, checking_id: str) -> PaymentStatus:
preimage=data["preimage"],
)

async def get_payment_quote(self, bolt11: str) -> PaymentQuoteResponse:
async def get_payment_quote(
self, melt_quote: PostMeltQuoteRequest
) -> PaymentQuoteResponse:
bolt11 = melt_quote.request
invoice_obj = decode(bolt11)
assert invoice_obj.amount_msat, "invoice has no amount."
amount_msat = int(invoice_obj.amount_msat)
Expand Down
15 changes: 12 additions & 3 deletions cashu/lightning/lndrest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
)
from loguru import logger

from ..core.base import Amount, MeltQuote, Unit
from ..core.base import Amount, MeltQuote, PostMeltQuoteRequest, Unit
from ..core.helpers import fee_reserve
from ..core.settings import settings
from .base import (
Expand All @@ -29,7 +29,7 @@
class LndRestWallet(LightningBackend):
"""https://api.lightning.community/rest/index.html#lnd-rest-api-reference"""

supports_mpp = True
supports_mpp = settings.mint_lnd_enable_mpp_experimental
supported_units = set([Unit.sat, Unit.msat])
unit = Unit.sat

Expand Down Expand Up @@ -73,6 +73,8 @@ def __init__(self, unit: Unit = Unit.sat, **kwargs):
self.client = httpx.AsyncClient(
base_url=self.endpoint, headers=self.auth, verify=self.cert
)
if self.supports_mpp:
logger.info("LNDRestWallet enabling MPP experimental feature")

async def status(self) -> StatusResponse:
try:
Expand Down Expand Up @@ -363,8 +365,15 @@ async def paid_invoices_stream(self) -> AsyncGenerator[str, None]:
await asyncio.sleep(5)

async def get_payment_quote(
self, bolt11: str, amount: Optional[Amount] = None
self, melt_quote: PostMeltQuoteRequest
) -> PaymentQuoteResponse:
# get amount from melt_quote or from bolt11
amount = (
Amount(Unit(melt_quote.unit), melt_quote.amount)
if melt_quote.amount
else None
)

invoice_obj = decode(bolt11)
assert invoice_obj.amount_msat, "invoice has no amount."

Expand Down
7 changes: 5 additions & 2 deletions cashu/lightning/strike.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import httpx

from ..core.base import Amount, MeltQuote, Unit
from ..core.base import Amount, MeltQuote, PostMeltQuoteRequest, Unit
from ..core.settings import settings
from .base import (
InvoiceResponse,
Expand Down Expand Up @@ -118,7 +118,10 @@ async def create_invoice(
error_message=None,
)

async def get_payment_quote(self, bolt11: str) -> PaymentQuoteResponse:
async def get_payment_quote(
self, melt_quote: PostMeltQuoteRequest
) -> PaymentQuoteResponse:
bolt11 = melt_quote.request
try:
r = await self.client.post(
url=f"{self.endpoint}/v1/payment-quotes/lightning",
Expand Down
3 changes: 1 addition & 2 deletions cashu/mint/ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,9 +505,8 @@ async def melt_quote(
)
else:
# not internal, get quote by backend
amount = Amount(unit, melt_quote.amount) if melt_quote.amount else None
payment_quote = await self.backends[method][unit].get_payment_quote(
melt_quote.request, amount=amount
melt_quote=melt_quote
)
assert payment_quote.checking_id, "quote has no checking id"
# make sure the backend returned the amount with a correct unit
Expand Down
4 changes: 2 additions & 2 deletions cashu/wallet/api/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,15 @@ async def swap(

# pay invoice from outgoing mint
await outgoing_wallet.load_proofs(reload=True)
quote = await outgoing_wallet.get_pay_amount_with_fees(invoice.bolt11)
quote = await outgoing_wallet.request_melt(invoice.bolt11)
total_amount = quote.amount + quote.fee_reserve
if outgoing_wallet.available_balance < total_amount:
raise Exception("balance too low")

_, send_proofs = await outgoing_wallet.split_to_send(
outgoing_wallet.proofs, total_amount, set_reserved=True
)
await outgoing_wallet.pay_lightning(
await outgoing_wallet.melt(
send_proofs, invoice.bolt11, quote.fee_reserve, quote.quote
)

Expand Down
18 changes: 12 additions & 6 deletions cashu/wallet/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,17 +177,23 @@ async def cli(ctx: Context, host: str, walletname: str, unit: str, tests: bool):

@cli.command("pay", help="Pay Lightning invoice.")
@click.argument("invoice", type=str)
@click.argument("amount", type=int, required=False)
@click.argument(
"amount",
type=int,
required=False,
)
@click.option(
"--yes", "-y", default=False, is_flag=True, help="Skip confirmation.", type=bool
)
@click.pass_context
@coro
async def pay(ctx: Context, invoice: str, amount: Optional[int], yes: bool):
async def pay(
ctx: Context, invoice: str, amount: Optional[int] = None, yes: bool = False
):
wallet: Wallet = ctx.obj["WALLET"]
await wallet.load_mint()
await print_balance(ctx)
quote = await wallet.get_pay_amount_with_fees(invoice, amount)
quote = await wallet.request_melt(invoice, amount)
logger.debug(f"Quote: {quote}")
total_amount = quote.amount + quote.fee_reserve
if not yes:
Expand All @@ -210,7 +216,7 @@ async def pay(ctx: Context, invoice: str, amount: Optional[int], yes: bool):
return
_, send_proofs = await wallet.split_to_send(wallet.proofs, total_amount)
try:
melt_response = await wallet.pay_lightning(
melt_response = await wallet.melt(
send_proofs, invoice, quote.fee_reserve, quote.quote
)
except Exception as e:
Expand Down Expand Up @@ -335,14 +341,14 @@ async def swap(ctx: Context):
invoice = await incoming_wallet.request_mint(amount)

# pay invoice from outgoing mint
quote = await outgoing_wallet.get_pay_amount_with_fees(invoice.bolt11)
quote = await outgoing_wallet.request_melt(invoice.bolt11)
total_amount = quote.amount + quote.fee_reserve
if outgoing_wallet.available_balance < total_amount:
raise Exception("balance too low")
_, send_proofs = await outgoing_wallet.split_to_send(
outgoing_wallet.proofs, total_amount, set_reserved=True
)
await outgoing_wallet.pay_lightning(
await outgoing_wallet.melt(
send_proofs, invoice.bolt11, quote.fee_reserve, quote.quote
)

Expand Down
6 changes: 2 additions & 4 deletions cashu/wallet/lightning/lightning.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,15 @@ async def pay_invoice(self, pr: str) -> PaymentResponse:
Returns:
bool: True if successful
"""
quote = await self.get_pay_amount_with_fees(pr)
quote = await self.request_melt(pr)
total_amount = quote.amount + quote.fee_reserve
assert total_amount > 0, "amount is not positive"
if self.available_balance < total_amount:
print("Error: Balance too low.")
return PaymentResponse(ok=False)
_, send_proofs = await self.split_to_send(self.proofs, total_amount)
try:
resp = await self.pay_lightning(
send_proofs, pr, quote.fee_reserve, quote.quote
)
resp = await self.melt(send_proofs, pr, quote.fee_reserve, quote.quote)
if resp.change:
fees_paid_sat = quote.fee_reserve - sum_promises(resp.change)
else:
Expand Down
Loading

0 comments on commit 1a75192

Please sign in to comment.