Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions app/src/api/grpc.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 45 additions & 0 deletions app/src/api/lnd.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 26 additions & 0 deletions app/src/api/loop.ts
Original file line number Diff line number Diff line change
@@ -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;