Skip to content

Commit 62deba2

Browse files
author
fiatjaf
committed
use lnurlpay.currency when creating invoices.
1 parent 7f0e9a6 commit 62deba2

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

src/lnurl.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import bech32 from 'bech32'
22
import wrap from './lib/promise-wrap'
3+
import { toMsat } from './lib/exchange-rate'
34

45
const debug = require('debug')('lightning-charge')
56

@@ -62,10 +63,14 @@ module.exports = (app, payListen, model, auth, ln) => async {
6263
return
6364
}
6465

66+
const current = endpoint.currency
67+
const min = currency ? await toMsat(currency, endpoint.min) : endpoint.min
68+
const max = currency ? await toMsat(currency, endpoint.max) : endpoint.max
69+
6570
res.status(200).send({
6671
tag: 'payRequest'
67-
, minSendable: endpoint.min
68-
, maxSendable: endpoint.max
72+
, minSendable: min
73+
, maxSendable: max
6974
, metadata: makeMetadata(endpoint)
7075
, commentAllowed: endpoint.comment_length
7176
, callback: `https://${req.hostname}/lnurl/${lnurlpay.id}/callback`
@@ -78,10 +83,18 @@ module.exports = (app, payListen, model, auth, ln) => async {
7883

7984
if (!amount)
8085
return res.send({status: 'ERROR', reason: `invalid amount '${req.query.amount}'`})
81-
if (amount > endpoint.max)
82-
return res.send({status: 'ERROR', reason: `amount must be smaller than ${Math.floor(endpoint.max / 1000)} sat`})
83-
if (amount < endpoint.min)
84-
return res.send({status: 'ERROR', reason: `amount must be greater than ${Math.ceil(endpoint.min / 1000)} sat`})
86+
87+
const current = endpoint.currency
88+
let min = currency ? await toMsat(currency, endpoint.min) : endpoint.min
89+
let max = currency ? await toMsat(currency, endpoint.max) : endpoint.max
90+
// account for currency variation
91+
min = min * 0.99
92+
max = max * 1.01
93+
94+
if (amount > max)
95+
return res.send({status: 'ERROR', reason: `amount must be smaller than ${Math.floor(max / 1000)} sat`})
96+
if (amount < min)
97+
return res.send({status: 'ERROR', reason: `amount must be greater than ${Math.ceil(min / 1000)} sat`})
8598

8699
let invoiceMetadata = {...req.query}
87100
delete invoiceMetadata.amount
@@ -104,6 +117,7 @@ module.exports = (app, payListen, model, auth, ln) => async {
104117
, metadata: invoiceMetadata
105118
, webhook: endpoint.webhook
106119
, lnurlpay_endpoint: endpoint.id
120+
, currency: endpoint.currency
107121
})
108122

109123
let successAction

0 commit comments

Comments
 (0)