@@ -41,36 +41,48 @@ export async function estimateRouteFee ({ lnd, destination, tokens, mtokens, req
4141 }
4242 }
4343
44- return await new Promise ( ( resolve , reject ) => {
45- const params = { }
46-
47- if ( request ) {
48- console . log ( 'estimateRouteFee using payment request' )
49- params . payment_request = request
50- } else {
51- console . log ( 'estimateRouteFee using destination and amount' )
52- params . dest = Buffer . from ( destination , 'hex' )
53- params . amt_sat = tokens ? toPositiveNumber ( tokens ) : toPositiveNumber ( BigInt ( mtokens ) / BigInt ( 1e3 ) )
54- }
55-
56- lnd . router . estimateRouteFee ( {
57- ...params ,
58- timeout
59- } , ( err , res ) => {
60- if ( err ) {
61- return reject ( err )
44+ let failureReason
45+ try {
46+ return await new Promise ( ( resolve , reject ) => {
47+ const params = { }
48+
49+ if ( request ) {
50+ console . log ( 'estimateRouteFee using payment request' )
51+ params . payment_request = request
52+ } else {
53+ console . log ( 'estimateRouteFee using destination and amount' )
54+ params . dest = Buffer . from ( destination , 'hex' )
55+ params . amt_sat = tokens ? toPositiveNumber ( tokens ) : toPositiveNumber ( BigInt ( mtokens ) / BigInt ( 1e3 ) )
6256 }
6357
64- if ( res . failure_reason !== 'FAILURE_REASON_NONE' || res . routing_fee_msat < 0 || res . time_lock_delay <= 0 ) {
65- return reject ( new Error ( `Unable to estimate route: ${ res . failure_reason } ` ) )
66- }
58+ lnd . router . estimateRouteFee ( {
59+ ...params ,
60+ timeout
61+ } , ( err , res ) => {
62+ if ( err ) {
63+ return reject ( err )
64+ }
65+
66+ if ( res . failure_reason !== 'FAILURE_REASON_NONE' || res . routing_fee_msat < 0 || res . time_lock_delay <= 0 ) {
67+ failureReason = res . failure_reason
68+ return reject ( new Error ( `Unable to estimate route: ${ failureReason } ` ) )
69+ }
6770
68- resolve ( {
69- routingFeeMsat : toPositiveNumber ( res . routing_fee_msat ) ,
70- timeLockDelay : toPositiveNumber ( res . time_lock_delay )
71+ resolve ( {
72+ routingFeeMsat : toPositiveNumber ( res . routing_fee_msat ) ,
73+ timeLockDelay : toPositiveNumber ( res . time_lock_delay )
74+ } )
7175 } )
7276 } )
73- } )
77+ } catch ( err ) {
78+ if ( request && failureReason === 'FAILURE_REASON_ERROR' ) {
79+ // try again without the payment request
80+ // there appears to be a compatibility bug when probing ldk nodes with payment requests
81+ // https://github.com/lightningnetwork/lnd/discussions/10427
82+ return await estimateRouteFee ( { lnd, destination, tokens, mtokens, timeout } )
83+ }
84+ throw err
85+ }
7486}
7587
7688// created_height is the accepted_height, timeout is the expiry height
0 commit comments