Fix: wallet include fees in swap outputs for inputs of successive melt (#630)

* wallet: add fees to outputs for melt that requires a split

* add test that requires a swap

* verify test fails, will revert

* revert true

* hopefully fix the tests

* fix default fee selection

* cleanup and renamings

* cleanup coinselect function, estimate fees

* fix test

* add comments

* weird error
This commit is contained in:
callebtc
2024-10-03 09:50:23 +02:00
committed by GitHub
parent f8f061f810
commit 700320a8c4
7 changed files with 112 additions and 56 deletions

View File

@@ -96,19 +96,47 @@ async def test_get_fees_for_proofs(wallet1: Wallet, ledger: Ledger):
@pytest.mark.asyncio
@pytest.mark.skipif(is_regtest, reason="only works with FakeWallet")
async def test_wallet_fee(wallet1: Wallet, ledger: Ledger):
# THIS TEST IS A FAKE, WE SET THE WALLET FEES MANUALLY IN set_ledger_keyset_fees
# It would be better to test if the wallet can get the fees from the mint itself
# but the ledger instance does not update the responses from the `mint` that is running in the background
# so we just pretend here and test really nothing...
async def test_wallet_selection_with_fee(wallet1: Wallet, ledger: Ledger):
# set fees to 100 ppk
set_ledger_keyset_fees(100, ledger, wallet1)
# THIS TEST IS A FAKE, WE SET THE WALLET FEES MANUALLY IN set_ledger_keyset_fees
# check if all wallet keysets have the correct fees
for keyset in wallet1.keysets.values():
assert keyset.input_fee_ppk == 100
invoice = await wallet1.request_mint(64)
await pay_if_regtest(invoice.bolt11)
await wallet1.mint(64, id=invoice.id)
send_proofs, _ = await wallet1.select_to_send(wallet1.proofs, 10)
assert sum_proofs(send_proofs) == 10
send_proofs_with_fees, _ = await wallet1.select_to_send(
wallet1.proofs, 10, include_fees=True
)
assert sum_proofs(send_proofs_with_fees) == 11
@pytest.mark.asyncio
@pytest.mark.skipif(is_regtest, reason="only works with FakeWallet")
async def test_wallet_swap_to_send_with_fee(wallet1: Wallet, ledger: Ledger):
# set fees to 100 ppk
set_ledger_keyset_fees(100, ledger, wallet1)
invoice = await wallet1.request_mint(64)
await pay_if_regtest(invoice.bolt11)
await wallet1.mint(64, id=invoice.id, split=[32, 32]) # make sure we need to swap
# quirk: this should call a `/v1/swap` with the mint but the mint will
# throw an error since the fees are only changed in the `ledger` instance, not in the uvicorn API server
# this *should* succeed if the fees were set in the API server
# at least, we can verify that the wallet is correctly computing the fees
# by asserting for this super specific error message from the (API server) mint
await assert_err(
wallet1.select_to_send(wallet1.proofs, 10),
"Mint Error: inputs (32) - fees (0) vs outputs (31) are not balanced.",
)
@pytest.mark.asyncio
async def test_split_with_fees(wallet1: Wallet, ledger: Ledger):

View File

@@ -254,7 +254,7 @@ async def test_swap_to_send(wallet1: Wallet):
assert_amt(send_proofs, 32)
assert_amt(keep_proofs, 0)
spendable_proofs = await wallet1._select_proofs_to_send(wallet1.proofs, 32)
spendable_proofs = wallet1.coinselect(wallet1.proofs, 32)
assert sum_proofs(spendable_proofs) == 32
assert sum_proofs(send_proofs) == 32