1
1
import { cachedFetcher } from '@/lib/fetch'
2
2
import { toPositiveNumber } from '@/lib/format'
3
3
import { authenticatedLndGrpc } from '@/lib/lnd'
4
- import { getIdentity , getHeight , getWalletInfo , getNode , getPayment } from 'ln-service'
4
+ import { getIdentity , getHeight , getWalletInfo , getNode , getPayment , parsePaymentRequest } from 'ln-service'
5
5
import { datePivot } from '@/lib/time'
6
6
import { LND_PATHFINDING_TIMEOUT_MS } from '@/lib/constants'
7
7
@@ -23,11 +23,34 @@ getWalletInfo({ lnd }, (err, result) => {
23
23
} )
24
24
25
25
export async function estimateRouteFee ( { lnd, destination, tokens, mtokens, request, timeout } ) {
26
+ // if the payment request includes us as route hint, we needd to use the destination and amount
27
+ // otherwise, this will fail with a self-payment error
28
+ if ( request ) {
29
+ const inv = parsePaymentRequest ( { request } )
30
+ const ourPubkey = await getOurPubkey ( { lnd } )
31
+ if ( Array . isArray ( inv . routes ) ) {
32
+ for ( const route of inv . routes ) {
33
+ if ( Array . isArray ( route ) ) {
34
+ for ( const hop of route ) {
35
+ if ( hop . public_key === ourPubkey ) {
36
+ console . log ( 'estimateRouteFee ignoring self-payment route' )
37
+ request = false
38
+ break
39
+ }
40
+ }
41
+ }
42
+ }
43
+ }
44
+ }
45
+
26
46
return await new Promise ( ( resolve , reject ) => {
27
47
const params = { }
48
+
28
49
if ( request ) {
50
+ console . log ( 'estimateRouteFee using payment request' )
29
51
params . payment_request = request
30
52
} else {
53
+ console . log ( 'estimateRouteFee using destination and amount' )
31
54
params . dest = Buffer . from ( destination , 'hex' )
32
55
params . amt_sat = tokens ? toPositiveNumber ( tokens ) : toPositiveNumber ( BigInt ( mtokens ) / BigInt ( 1e3 ) )
33
56
}
0 commit comments