Browse Source

refactor(lnd): update LndService.closeChannel to be generic

master
jamaljsr 5 years ago
parent
commit
cba287c000
  1. 5
      src/lib/clightning/clightningService.ts
  2. 6
      src/lib/lightning/notImplementedService.ts
  3. 2
      src/lib/lightning/types.ts
  4. 6
      src/lib/lnd/lndService.ts
  5. 16
      src/types/index.ts
  6. 1
      src/utils/tests/renderWithProviders.tsx

5
src/lib/clightning/clightningService.ts

@ -55,6 +55,11 @@ class CLightningService implements LightningService {
throw new Error(`openChannel is not implemented for ${from.implementation} nodes`); throw new Error(`openChannel is not implemented for ${from.implementation} nodes`);
} }
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async closeChannel(node: LightningNode, channelPoint: string): Promise<any> {
throw new Error(`closeChannel is not implemented for ${node.implementation} nodes`);
}
/** /**
* Helper function to continually query the node until a successful * Helper function to continually query the node until a successful
* response is received or it times out * response is received or it times out

6
src/lib/lightning/notImplementedService.ts

@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import { LightningNode } from 'shared/types'; import { LightningNode } from 'shared/types';
import { import {
LightningNodeAddress, LightningNodeAddress,
@ -31,13 +32,14 @@ class NotImplementedService implements LightningService {
} }
openChannel( openChannel(
from: LightningNode, from: LightningNode,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
to: LightningNode, to: LightningNode,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
amount: string, amount: string,
): Promise<LightningNodeChannelPoint> { ): Promise<LightningNodeChannelPoint> {
throw new Error(`getChannels is not implemented for ${from.implementation} nodes`); throw new Error(`getChannels is not implemented for ${from.implementation} nodes`);
} }
closeChannel(node: LightningNode, channelPoint: string): Promise<any> {
throw new Error(`closeChannel is not implemented for ${node.implementation} nodes`);
}
} }
export default new NotImplementedService(); export default new NotImplementedService();

2
src/lib/lightning/types.ts

@ -47,7 +47,7 @@ export interface LightningService {
to: LightningNode, to: LightningNode,
amount: string, amount: string,
) => Promise<LightningNodeChannelPoint>; ) => Promise<LightningNodeChannelPoint>;
// closeChannel: (node: LightningNode, channelPoint: string) => Promise<any>; closeChannel: (node: LightningNode, channelPoint: string) => Promise<any>;
// listChannels: (node: LightningNode) => Promise<LND.ListChannelsResponse>; // listChannels: (node: LightningNode) => Promise<LND.ListChannelsResponse>;
// pendingChannels: (node: LightningNode) => Promise<LND.PendingChannelsResponse>; // pendingChannels: (node: LightningNode) => Promise<LND.PendingChannelsResponse>;
// onNodesDeleted: (nodes: LightningNode[]) => Promise<void>; // onNodesDeleted: (nodes: LightningNode[]) => Promise<void>;

6
src/lib/lnd/lndService.ts

@ -95,7 +95,7 @@ class LndService implements LndLibrary {
}; };
} }
async closeChannel(node: LndNode, channelPoint: string): Promise<any> { async closeChannel(node: LightningNode, channelPoint: string): Promise<any> {
const [txid, txindex] = channelPoint.split(':'); const [txid, txindex] = channelPoint.split(':');
const req: LND.CloseChannelRequest = { const req: LND.CloseChannelRequest = {
channelPoint: { channelPoint: {
@ -104,7 +104,7 @@ class LndService implements LndLibrary {
outputIndex: parseInt(txindex), outputIndex: parseInt(txindex),
}, },
}; };
return await proxy.closeChannel(node, req); return await proxy.closeChannel(this.cast(node), req);
} }
async listChannels(node: LndNode): Promise<LND.ListChannelsResponse> { async listChannels(node: LndNode): Promise<LND.ListChannelsResponse> {
@ -124,7 +124,7 @@ class LndService implements LndLibrary {
* response is received or it times out * response is received or it times out
*/ */
async waitUntilOnline( async waitUntilOnline(
node: LndNode, node: LightningNode,
interval = 3 * 1000, // check every 3 seconds interval = 3 * 1000, // check every 3 seconds
timeout = 30 * 1000, // timeout after 30 seconds timeout = 30 * 1000, // timeout after 30 seconds
): Promise<void> { ): Promise<void> {

16
src/types/index.ts

@ -55,17 +55,17 @@ export interface BitcoindLibrary {
} }
export interface LndLibrary { export interface LndLibrary {
waitUntilOnline: (node: LndNode) => Promise<void>; waitUntilOnline: (node: LightningNode) => Promise<void>;
getInfo: (node: LndNode) => Promise<LightningNodeInfo>; getInfo: (node: LightningNode) => Promise<LightningNodeInfo>;
getBalances: (node: LndNode) => Promise<LightningNodeBalances>; getBalances: (node: LightningNode) => Promise<LightningNodeBalances>;
getNewAddress: (node: LndNode) => Promise<LightningNodeAddress>; getNewAddress: (node: LightningNode) => Promise<LightningNodeAddress>;
getChannels: (node: LndNode) => Promise<LightningNodeChannel[]>; getChannels: (node: LightningNode) => Promise<LightningNodeChannel[]>;
openChannel: ( openChannel: (
from: LndNode, from: LightningNode,
to: LndNode, to: LightningNode,
amount: string, amount: string,
) => Promise<LightningNodeChannelPoint>; ) => Promise<LightningNodeChannelPoint>;
closeChannel: (node: LndNode, channelPoint: string) => Promise<any>; closeChannel: (node: LightningNode, channelPoint: string) => Promise<any>;
listChannels: (node: LndNode) => Promise<LND.ListChannelsResponse>; listChannels: (node: LndNode) => Promise<LND.ListChannelsResponse>;
pendingChannels: (node: LndNode) => Promise<LND.PendingChannelsResponse>; pendingChannels: (node: LndNode) => Promise<LND.PendingChannelsResponse>;
onNodesDeleted: (nodes: LndNode[]) => Promise<void>; onNodesDeleted: (nodes: LndNode[]) => Promise<void>;

1
src/utils/tests/renderWithProviders.tsx

@ -14,6 +14,7 @@ export const lightningServiceMock: jest.Mocked<LightningService> = {
getNewAddress: jest.fn(), getNewAddress: jest.fn(),
getChannels: jest.fn(), getChannels: jest.fn(),
openChannel: jest.fn(), openChannel: jest.fn(),
closeChannel: jest.fn(),
waitUntilOnline: jest.fn(), waitUntilOnline: jest.fn(),
}; };
// injections allow you to mock the dependencies of redux store actions // injections allow you to mock the dependencies of redux store actions

Loading…
Cancel
Save