feat: add GRPC wrapper classes for lnd & loop

This commit is contained in:
jamaljsr 2020-04-09 12:50:13 -04:00
parent f4754f4154
commit b183036e6a
3 changed files with 110 additions and 0 deletions

39
app/src/api/grpc.ts Normal file
View file

@ -0,0 +1,39 @@
import { grpc } from '@improbable-eng/grpc-web';
import { ProtobufMessage } from '@improbable-eng/grpc-web/dist/typings/message';
import { Metadata } from '@improbable-eng/grpc-web/dist/typings/metadata';
import { UnaryMethodDefinition } from '@improbable-eng/grpc-web/dist/typings/service';
import { DEV_HOST } from 'config';
/**
* Executes a single GRPC request and returns a promise which will resolve with the response
* @param methodDescriptor the GRPC method to call on the service
* @param request The GRPC request message to send
* @param metadata headers to include with the request
*/
export const grpcRequest = <TReq extends ProtobufMessage, TRes extends ProtobufMessage>(
methodDescriptor: UnaryMethodDefinition<TReq, TRes>,
request: TReq,
metadata?: Metadata.ConstructorArg,
): Promise<TRes> => {
return new Promise((resolve, reject) => {
grpc.unary(methodDescriptor, {
host: DEV_HOST,
request,
metadata,
onEnd: ({ status, statusMessage, headers, message, trailers }) => {
console.log(
`GRPC Request: ${methodDescriptor.service.serviceName}.${methodDescriptor.methodName}`,
);
console.log(' - status', status, statusMessage);
console.log(' - headers', headers);
if (status === grpc.Code.OK && message) {
resolve(message as TRes);
console.log(' - message', message.toObject());
} else {
reject(new Error(statusMessage));
}
console.log(' - trailers', trailers);
},
});
});
};

45
app/src/api/lnd.ts Normal file
View file

@ -0,0 +1,45 @@
import { GetInfoResponse, ListChannelsRequest } from 'types/generated/lnd_pb';
import { Lightning } from 'types/generated/lnd_pb_service';
import { Channel, NodeInfo } from 'types/state';
import { DEV_MACAROON } from 'config';
import { grpcRequest } from './grpc';
/**
* An API wrapper to communicate with the LND node via GRPC
*/
class LndApi {
private _meta = {
'X-Grpc-Backend': 'lnd',
macaroon: DEV_MACAROON,
};
/**
* call the LND `GetInfo` RPC and return the response
*/
async getInfo(): Promise<NodeInfo> {
const res = await grpcRequest(Lightning.GetInfo, new GetInfoResponse(), this._meta);
return res.toObject();
}
/**
* call the LND `ListChannels` RPC and return the response
*/
async listChannels(): Promise<Channel[]> {
const res = await grpcRequest(
Lightning.ListChannels,
new ListChannelsRequest(),
this._meta,
);
return res.toObject().channelsList.map(c => ({
chanId: c.chanId,
remotePubkey: c.remotePubkey,
capacity: c.capacity,
localBalance: c.localBalance,
remoteBalance: c.remoteBalance,
uptime: c.uptime,
active: c.active,
}));
}
}
export default LndApi;

26
app/src/api/loop.ts Normal file
View file

@ -0,0 +1,26 @@
import { ListSwapsRequest, ListSwapsResponse } from 'types/generated/loop_pb';
import { SwapClient } from 'types/generated/loop_pb_service';
import { grpcRequest } from './grpc';
/**
* An API wrapper to communicate with the Loop daemon via GRPC
*/
class LoopApi {
private _meta = {
'X-Grpc-Backend': 'loop',
};
/**
* call the LND `ListSwaps` RPC and return the response
*/
async listSwaps(): Promise<ListSwapsResponse.AsObject> {
const res = await grpcRequest(
SwapClient.ListSwaps,
new ListSwapsRequest(),
this._meta,
);
return res.toObject();
}
}
export default LoopApi;