Skip to content

Commit

Permalink
Set correct blink min fee (#701)
Browse files Browse the repository at this point in the history
* min fee

* fix tests

* amount_split test
  • Loading branch information
callebtc authored Feb 13, 2025
1 parent ec3db81 commit 23a706d
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 6 deletions.
2 changes: 2 additions & 0 deletions cashu/core/split.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

def amount_split(amount: int) -> List[int]:
"""Given an amount returns a list of amounts returned e.g. 13 is [1, 4, 8]."""
if amount <= 0:
return []
bits_amt = bin(amount)[::-1][:-2]
rv = []
for pos, bit in enumerate(bits_amt):
Expand Down
4 changes: 3 additions & 1 deletion cashu/lightning/blink.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@

# according to https://github.com/GaloyMoney/galoy/blob/7e79cc27304de9b9c2e7d7f4fdd3bac09df23aac/core/api/src/domain/bitcoin/index.ts#L59
BLINK_MAX_FEE_PERCENT = 0.5
# according to https://github.com/GaloyMoney/blink/blob/7e79cc27304de9b9c2e7d7f4fdd3bac09df23aac/core/api/src/domain/bitcoin/index.ts#L60C1-L60C41
MINIMUM_FEE_MSAT = 10_000

DIRECTION_SEND = "SEND"
DIRECTION_RECEIVE = "RECEIVE"
PROBE_FEE_TIMEOUT_SEC = 1
MINIMUM_FEE_MSAT = 2000


INVOICE_RESULT_MAP = {
"PENDING": PaymentResult.PENDING,
Expand Down
6 changes: 5 additions & 1 deletion cashu/mint/ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,11 @@ async def _generate_change_promises(
# we make sure that the fee is positive
overpaid_fee = fee_provided - fee_paid

if overpaid_fee == 0 or outputs is None:
if overpaid_fee <= 0 or outputs is None:
if overpaid_fee < 0:
logger.error(
f"Overpaid fee is negative ({overpaid_fee}). This should not happen."
)
return []

logger.debug(
Expand Down
2 changes: 2 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

def test_get_output_split():
assert amount_split(13) == [1, 4, 8]
assert amount_split(0) == []
assert amount_split(-8) == []


def test_tokenv3_deserialize_get_attributes():
Expand Down
8 changes: 4 additions & 4 deletions tests/test_mint_lightning_blink.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ async def test_blink_get_payment_status():
@respx.mock
@pytest.mark.asyncio
async def test_blink_get_payment_quote():
# response says 1 sat fees but invoice (1000 sat) * 0.5% is 5 sat so we expect 5 sat
# response says 1 sat fees but invoice (1000 sat) * 0.5% is 5 sat so we expect MINIMUM_FEE_MSAT/1000 sat
mock_response = {"data": {"lnInvoiceFeeProbe": {"amount": 1}}}
respx.post(blink.endpoint).mock(return_value=Response(200, json=mock_response))
melt_quote_request = PostMeltQuoteRequest(
Expand All @@ -199,7 +199,7 @@ async def test_blink_get_payment_quote():
quote = await blink.get_payment_quote(melt_quote_request)
assert quote.checking_id == payment_request
assert quote.amount == Amount(Unit.sat, 1000) # sat
assert quote.fee == Amount(Unit.sat, 5) # sat
assert quote.fee == Amount(Unit.sat, MINIMUM_FEE_MSAT // 1000) # msat

# response says 10 sat fees but invoice (1000 sat) * 0.5% is 5 sat so we expect 10 sat
mock_response = {"data": {"lnInvoiceFeeProbe": {"amount": 10}}}
Expand Down Expand Up @@ -238,7 +238,7 @@ async def test_blink_get_payment_quote():
@respx.mock
@pytest.mark.asyncio
async def test_blink_get_payment_quote_backend_error():
# response says error but invoice (1000 sat) * 0.5% is 5 sat so we expect 10 sat
# response says error but invoice (1000 sat) * 0.5% is 5 sat so we expect 10 sat (MINIMUM_FEE_MSAT)
mock_response = {"data": {"lnInvoiceFeeProbe": {"errors": [{"message": "error"}]}}}
respx.post(blink.endpoint).mock(return_value=Response(200, json=mock_response))
melt_quote_request = PostMeltQuoteRequest(
Expand All @@ -247,4 +247,4 @@ async def test_blink_get_payment_quote_backend_error():
quote = await blink.get_payment_quote(melt_quote_request)
assert quote.checking_id == payment_request
assert quote.amount == Amount(Unit.sat, 1000) # sat
assert quote.fee == Amount(Unit.sat, 5) # sat
assert quote.fee == Amount(Unit.sat, MINIMUM_FEE_MSAT // 1000) # msat

0 comments on commit 23a706d

Please sign in to comment.