thunderhub/server/schema/route/resolvers.ts

87 lines
2.2 KiB
TypeScript
Raw Normal View History

2020-06-06 18:04:19 +02:00
import {
getRouteToDestination,
getWalletInfo,
probeForRoute,
} from 'ln-service';
import { ContextType } from 'server/types/apiTypes';
import { logger } from 'server/helpers/logger';
import { requestLimiter } from 'server/helpers/rateLimiter';
2020-06-07 13:33:47 +02:00
import { toWithError, to } from 'server/helpers/async';
import { LndObject, ProbeForRouteType } from 'server/types/ln-service.types';
type RouteParent = {
lnd: LndObject;
destination: string;
tokens: number;
};
export const routeResolvers = {
Query: {
getRoutes: async (_: undefined, params: any, context: ContextType) => {
await requestLimiter(context.ip, 'getRoutes');
const { lnd } = context;
const { public_key } = await getWalletInfo({ lnd });
2020-06-07 13:33:47 +02:00
const { route } = await to(
getRouteToDestination({
lnd,
outgoing_channel: params.outgoing,
incoming_peer: params.incoming,
destination: public_key,
tokens: params.tokens,
...(params.maxFee && { max_fee: params.maxFee }),
})
);
if (!route) {
throw new Error('NoRouteFound');
}
2020-06-07 13:33:47 +02:00
return route;
},
},
2020-06-06 18:04:19 +02:00
ProbeRoute: {
route: async (parent: RouteParent) => {
2020-06-06 18:04:19 +02:00
const { lnd, destination, tokens } = parent;
if (!lnd) {
logger.debug('ExpectedLNDToProbeForRoute');
return null;
}
if (!destination) {
logger.debug('ExpectedDestinationToProbeForRoute');
return null;
}
2020-06-21 21:18:50 +02:00
const [info, error] = await toWithError(
2020-06-06 18:04:19 +02:00
probeForRoute({ lnd, destination, tokens })
);
2020-06-21 21:37:29 +02:00
if (!info || error) {
2020-06-06 18:04:19 +02:00
logger.debug(
2020-06-21 21:18:50 +02:00
`Error probing route to destination ${destination} for ${tokens} tokens`
2020-06-06 18:04:19 +02:00
);
return null;
}
if (!(info as ProbeForRouteType).route) {
2020-06-06 18:04:19 +02:00
logger.debug(
`No route found to destination ${destination} for ${tokens} tokens`
);
return null;
}
const hopsWithNodes =
(info as ProbeForRouteType).route?.hops.map(h => ({
...h,
node: { lnd, publicKey: h.public_key },
})) || [];
2020-06-06 18:04:19 +02:00
return { ...(info as ProbeForRouteType).route, hops: hopsWithNodes };
2020-06-06 18:04:19 +02:00
},
},
};