Skip to content

Commit

Permalink
LN Address payments must use millisat amounts
Browse files Browse the repository at this point in the history
Fixes #3
  • Loading branch information
conduition committed Oct 3, 2024
1 parent 73f3eda commit 077d55d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
2 changes: 1 addition & 1 deletion examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func ExampleWallet_PayLightningAddress() {
panic(err)
}

payment, err := wallet.PayLightningAddress(ctx, lnAddress, "", 0.00000001)
payment, err := wallet.PayLightningAddress(ctx, lnAddress, "", 1000)
if err != nil {
panic(err)
}
Expand Down
20 changes: 11 additions & 9 deletions wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,8 +411,10 @@ func (wallet *Wallet) PayInvoice(ctx context.Context, invoice, description strin
})
}

// PayLightningAddress executes a payment of the given BTC amount to a
// PayLightningAddress executes a payment of the given millisat amount to a
// given lightning address. The description is stored in the WoS payment history.
// Note the amount is in millisats (thousandths of a sat). To pay 5 sats, pass
// 5000 as the amount.
//
// Returns ErrOutsideSendableRange if the amount to be sent is outside the receiver's
// acceptable min/max sendable range.
Expand All @@ -423,7 +425,7 @@ func (wallet *Wallet) PayLightningAddress(
ctx context.Context,
lnAddress LightningAddress,
description string,
amount float64,
amountMillisat uint64,
) (*Payment, error) {
respData, err := wallet.PostRequest(ctx, "/api/v1/wallet/lnurl", map[string]any{
"address": lnAddress.LNURL(),
Expand All @@ -442,20 +444,20 @@ func (wallet *Wallet) PayLightningAddress(
return nil, fmt.Errorf("PayLightningAddress: invalid response JSON: %w", err)
}

if maxSendable := fromMillisat(lnPayResponseBody.MaxSendable); amount > maxSendable {
if amountMillisat > lnPayResponseBody.MaxSendable {
return nil, fmt.Errorf(
"PayLightningAddress: %w: exceeds maxSendable (%f BTC)",
ErrOutsideSendableRange, maxSendable,
"PayLightningAddress: %w: exceeds maxSendable (%d msat)",
ErrOutsideSendableRange, lnPayResponseBody.MaxSendable,
)
} else if minSendable := fromMillisat(lnPayResponseBody.MinSendable); amount < minSendable {
} else if amountMillisat < lnPayResponseBody.MinSendable {
return nil, fmt.Errorf(
"PayLightningAddress: %w: exceeds minSendable (%f BTC)",
ErrOutsideSendableRange, minSendable,
"PayLightningAddress: %w: exceeds minSendable (%d msat)",
ErrOutsideSendableRange, lnPayResponseBody.MinSendable,
)
}

respData, err = wallet.PostRequest(ctx, "/api/v1/wallet/lnPay", map[string]any{
"amount": toMillisat(amount),
"amount": amountMillisat,
"callback": lnPayResponseBody.Callback,
})
if err != nil {
Expand Down

0 comments on commit 077d55d

Please sign in to comment.