Skip to content

Commit

Permalink
mint melt externally different units
Browse files Browse the repository at this point in the history
  • Loading branch information
callebtc committed Oct 30, 2024
1 parent 40039bb commit 83cbdd4
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 26 deletions.
2 changes: 1 addition & 1 deletion cashu/lightning/fake.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@


class FakeWallet(LightningBackend):
unit: Unit
fake_btc_price = 1e8 / 1337
paid_invoices_queue: asyncio.Queue[Bolt11] = asyncio.Queue(0)
payment_secrets: Dict[str, str] = dict()
Expand All @@ -46,7 +47,6 @@ class FakeWallet(LightningBackend):
).hex()

supported_units = {Unit.sat, Unit.msat, Unit.usd, Unit.eur}
unit = Unit.sat

supports_incoming_payment_stream: bool = True
supports_description: bool = True
Expand Down
22 changes: 12 additions & 10 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
settings.tor = False
settings.wallet_unit = "sat"
settings.mint_backend_bolt11_sat = settings.mint_backend_bolt11_sat or "FakeWallet"
settings.mint_backend_bolt11_usd = settings.mint_backend_bolt11_usd or "FakeWallet"
settings.fakewallet_brr = True
settings.fakewallet_delay_outgoing_payment = 0
settings.fakewallet_delay_incoming_payment = 1
Expand All @@ -42,7 +43,7 @@
), "Test database is the same as the main database"
settings.mint_database = settings.mint_test_database
settings.mint_derivation_path = "m/0'/0'/0'"
settings.mint_derivation_path_list = []
settings.mint_derivation_path_list = ["m/0'/2'/0'"] # USD
settings.mint_private_key = "TEST_PRIVATE_KEY"
settings.mint_seed_decryption_key = ""
settings.mint_max_balance = 0
Expand Down Expand Up @@ -85,13 +86,6 @@ def run(self, *args, **kwargs):
async def ledger():
async def start_mint_init(ledger: Ledger) -> Ledger:
await migrate_databases(ledger.db, migrations_mint)
ledger = Ledger(
db=Database("mint", settings.mint_database),
seed=settings.mint_private_key,
derivation_path=settings.mint_derivation_path,
backends=backends,
crud=LedgerCrudSqlite(),
)
await ledger.startup_ledger()
return ledger

Expand All @@ -110,9 +104,17 @@ async def start_mint_init(ledger: Ledger) -> Ledger:
await db.engine.dispose()

wallets_module = importlib.import_module("cashu.lightning")
lightning_backend = getattr(wallets_module, settings.mint_backend_bolt11_sat)()
lightning_backend_sat = getattr(wallets_module, settings.mint_backend_bolt11_sat)(
unit=Unit.sat
)
lightning_backend_usd = getattr(wallets_module, settings.mint_backend_bolt11_usd)(
unit=Unit.usd
)
backends = {
Method.bolt11: {Unit.sat: lightning_backend},
Method.bolt11: {
Unit.sat: lightning_backend_sat,
Unit.usd: lightning_backend_usd,
},
}
ledger = Ledger(
db=Database("mint", settings.mint_database),
Expand Down
6 changes: 6 additions & 0 deletions tests/test_mint_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ async def test_api_keysets(ledger: Ledger):
"active": True,
"input_fee_ppk": 0,
},
{
"id": "00c074b96c7e2b0e",
"unit": "usd",
"active": True,
"input_fee_ppk": 0,
},
]
}
assert response.json() == expected
Expand Down
2 changes: 1 addition & 1 deletion tests/test_mint_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ async def wallet(ledger: Ledger):
async def test_init_keysets(ledger: Ledger):
ledger.keysets = {}
await ledger.init_keysets()
assert len(ledger.keysets) == 1
assert len(ledger.keysets) == 2


@pytest.mark.asyncio
Expand Down
21 changes: 7 additions & 14 deletions tests/test_mint_melt.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,35 +343,28 @@ async def test_mint_melt_different_units(ledger: Ledger, wallet: Wallet):

amount = 32

# create a mint quote that is unpaid
# mint quote in sat
sat_mint_quote = await ledger.mint_quote(
quote_request=PostMintQuoteRequest(amount=amount, unit="sat")
)
sat_invoice = sat_mint_quote.request
assert sat_mint_quote.paid is False

# create a melt quote with above invoice in different unit
# melt quote in usd
usd_melt_quote = await ledger.melt_quote(
PostMeltQuoteRequest(unit="usd", request=sat_invoice)
)
assert usd_melt_quote.paid is False

melt_resp = await ledger.melt(proofs=wallet.proofs, quote=usd_melt_quote.quote)

assert_err(
melt_resp.state == MeltQuoteState.unpaid.value,
f"Expected state to be paid, got {melt_resp.state}",
)
# pay melt quote with usd
await ledger.melt(proofs=wallet.proofs, quote=usd_melt_quote.quote)

output_amounts = [32]

secrets, rs, derivation_paths = await wallet.generate_n_secrets(len(output_amounts))
outputs, rs = wallet._construct_outputs(output_amounts, secrets, rs)
mint_resp = await ledger.mint(outputs=outputs, quote_id=invoice.id)

assert_err(len(mint_resp) > 0, f"Expected promises, got {len(mint_resp)} promises")
# mint in sat
mint_resp = await ledger.mint(outputs=outputs, quote_id=sat_mint_quote.quote)

await assert_err(
ledger.mint(outputs=outputs, quote_id=invoice.id),
"outputs have already been signed before.",
)
assert len(mint_resp) == len(outputs)

0 comments on commit 83cbdd4

Please sign in to comment.