1
1
import bech32 from 'bech32'
2
2
import wrap from './lib/promise-wrap'
3
+ import { toMsat } from './lib/exchange-rate'
3
4
4
5
const debug = require ( 'debug' ) ( 'lightning-charge' )
5
6
@@ -62,10 +63,14 @@ module.exports = (app, payListen, model, auth, ln) => async {
62
63
return
63
64
}
64
65
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
+
65
70
res . status ( 200 ) . send ( {
66
71
tag : 'payRequest'
67
- , minSendable : endpoint . min
68
- , maxSendable : endpoint . max
72
+ , minSendable : min
73
+ , maxSendable : max
69
74
, metadata : makeMetadata ( endpoint )
70
75
, commentAllowed : endpoint . comment_length
71
76
, callback : `https://${ req . hostname } /lnurl/${ lnurlpay . id } /callback`
@@ -78,10 +83,18 @@ module.exports = (app, payListen, model, auth, ln) => async {
78
83
79
84
if ( ! amount )
80
85
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` } )
85
98
86
99
let invoiceMetadata = { ...req . query }
87
100
delete invoiceMetadata . amount
@@ -104,6 +117,7 @@ module.exports = (app, payListen, model, auth, ln) => async {
104
117
, metadata : invoiceMetadata
105
118
, webhook : endpoint . webhook
106
119
, lnurlpay_endpoint : endpoint . id
120
+ , currency : endpoint . currency
107
121
} )
108
122
109
123
let successAction
0 commit comments