Browse Source

refactor(lnd): update LndService.openChannel to return generic state

master
jamaljsr 5 years ago
parent
commit
6ecffd4e4c
  1. 11
      src/lib/clightning/clightningService.ts
  2. 10
      src/lib/lightning/notImplementedService.ts
  3. 11
      src/lib/lightning/types.ts
  4. 10
      src/lib/lnd/lndService.spec.ts
  5. 24
      src/lib/lnd/lndService.ts
  6. 7
      src/types/index.ts
  7. 1
      src/utils/tests/renderWithProviders.tsx

11
src/lib/clightning/clightningService.ts

@ -4,6 +4,7 @@ import {
LightningNodeAddress,
LightningNodeBalances,
LightningNodeChannel,
LightningNodeChannelPoint,
LightningNodeInfo,
LightningService,
} from 'lib/lightning/types';
@ -44,6 +45,16 @@ class CLightningService implements LightningService {
throw new Error(`getChannels is not implemented for ${node.implementation} nodes`);
}
async openChannel(
from: LightningNode,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
to: LightningNode,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
amount: string,
): Promise<LightningNodeChannelPoint> {
throw new Error(`openChannel is not implemented for ${from.implementation} nodes`);
}
/**
* Helper function to continually query the node until a successful
* response is received or it times out

10
src/lib/lightning/notImplementedService.ts

@ -3,6 +3,7 @@ import {
LightningNodeAddress,
LightningNodeBalances,
LightningNodeChannel,
LightningNodeChannelPoint,
LightningNodeInfo,
LightningService,
} from './types';
@ -28,6 +29,15 @@ class NotImplementedService implements LightningService {
getChannels(node: LightningNode): Promise<LightningNodeChannel[]> {
throw new Error(`getChannels is not implemented for ${node.implementation} nodes`);
}
openChannel(
from: LightningNode,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
to: LightningNode,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
amount: string,
): Promise<LightningNodeChannelPoint> {
throw new Error(`getChannels is not implemented for ${from.implementation} nodes`);
}
}
export default new NotImplementedService();

11
src/lib/lightning/types.ts

@ -31,13 +31,22 @@ export interface LightningNodeChannel {
status: 'Open' | 'Opening' | 'Closing' | 'Force Closing' | 'Waiting to Close';
}
export interface LightningNodeChannelPoint {
txid: string;
index: number;
}
export interface LightningService {
waitUntilOnline: (node: LightningNode) => Promise<void>;
getInfo: (node: LightningNode) => Promise<LightningNodeInfo>;
getBalances: (node: LightningNode) => Promise<LightningNodeBalances>;
getNewAddress: (node: LightningNode) => Promise<LightningNodeAddress>;
getChannels: (node: LightningNode) => Promise<LightningNodeChannel[]>;
// openChannel: (from: LightningNode, to: LightningNode, amount: string) => Promise<LND.ChannelPoint>;
openChannel: (
from: LightningNode,
to: LightningNode,
amount: string,
) => Promise<LightningNodeChannelPoint>;
// closeChannel: (node: LightningNode, channelPoint: string) => Promise<any>;
// listChannels: (node: LightningNode) => Promise<LND.ListChannelsResponse>;
// pendingChannels: (node: LightningNode) => Promise<LND.PendingChannelsResponse>;

10
src/lib/lnd/lndService.spec.ts

@ -68,8 +68,9 @@ describe('LndService', () => {
lndProxyClient.listPeers = jest.fn().mockResolvedValue({
peers: [{ pubKey: 'asdf' }],
});
const expected = { fundingTxidStr: 'xyz' };
lndProxyClient.openChannel = jest.fn().mockResolvedValue(expected);
const expected = { txid: 'xyz', index: 0 };
const mocked = { fundingTxidStr: 'xyz', outputIndex: 0 };
lndProxyClient.openChannel = jest.fn().mockResolvedValue(mocked);
const actual = await lndService.openChannel(node, node2, '1000');
expect(actual).toEqual(expected);
expect(lndProxyClient.getInfo).toBeCalledTimes(1);
@ -82,8 +83,9 @@ describe('LndService', () => {
lndProxyClient.listPeers = jest.fn().mockResolvedValue({
peers: [{ pubKey: 'fdsa' }],
});
const expected = { fundingTxidStr: 'xyz' };
lndProxyClient.openChannel = jest.fn().mockResolvedValue(expected);
const expected = { txid: 'xyz', index: 0 };
const mocked = { fundingTxidStr: 'xyz', outputIndex: 0 };
lndProxyClient.openChannel = jest.fn().mockResolvedValue(mocked);
const actual = await lndService.openChannel(node, node2, '1000');
expect(actual).toEqual(expected);
expect(lndProxyClient.getInfo).toBeCalledTimes(1);

24
src/lib/lnd/lndService.ts

@ -4,6 +4,7 @@ import {
LightningNodeAddress,
LightningNodeBalances,
LightningNodeChannel,
LightningNodeChannelPoint,
LightningNodeInfo,
} from 'lib/lightning/types';
import { LndLibrary } from 'types';
@ -13,7 +14,7 @@ import { lndProxyClient as proxy } from './';
import { mapOpenChannel, mapPendingChannel } from './mappers';
class LndService implements LndLibrary {
async getInfo(node: LndNode): Promise<LightningNodeInfo> {
async getInfo(node: LightningNode): Promise<LightningNodeInfo> {
const info = await proxy.getInfo(this.cast(node));
return {
pubkey: info.identityPubkey,
@ -26,7 +27,7 @@ class LndService implements LndLibrary {
};
}
async getBalances(node: LndNode): Promise<LightningNodeBalances> {
async getBalances(node: LightningNode): Promise<LightningNodeBalances> {
const balances = await proxy.getWalletBalance(this.cast(node));
return {
total: balances.totalBalance,
@ -60,15 +61,16 @@ class LndService implements LndLibrary {
}
async openChannel(
from: LndNode,
to: LndNode,
from: LightningNode,
to: LightningNode,
amount: string,
): Promise<LND.ChannelPoint> {
): Promise<LightningNodeChannelPoint> {
// get pubkey of dest node
const { identityPubkey: toPubKey } = await proxy.getInfo(to);
const { pubkey: toPubKey } = await this.getInfo(to);
const lndFrom = this.cast(from);
// get peers of source node
const { peers } = await proxy.listPeers(from);
const { peers } = await proxy.listPeers(lndFrom);
// check if already connected
const peer = peers.find(p => p.pubKey === toPubKey);
@ -78,7 +80,7 @@ class LndService implements LndLibrary {
pubkey: toPubKey,
host: getContainerName(to),
};
await proxy.connectPeer(from, { addr });
await proxy.connectPeer(lndFrom, { addr });
}
// open channel
@ -86,7 +88,11 @@ class LndService implements LndLibrary {
nodePubkeyString: toPubKey,
localFundingAmount: amount,
};
return await proxy.openChannel(from, req);
const res = await proxy.openChannel(lndFrom, req);
return {
txid: res.fundingTxidStr as string,
index: res.outputIndex,
};
}
async closeChannel(node: LndNode, channelPoint: string): Promise<any> {

7
src/types/index.ts

@ -7,6 +7,7 @@ import {
LightningNodeAddress,
LightningNodeBalances,
LightningNodeChannel,
LightningNodeChannelPoint,
LightningNodeInfo,
LightningService,
} from 'lib/lightning/types';
@ -59,7 +60,11 @@ export interface LndLibrary {
getBalances: (node: LndNode) => Promise<LightningNodeBalances>;
getNewAddress: (node: LndNode) => Promise<LightningNodeAddress>;
getChannels: (node: LndNode) => Promise<LightningNodeChannel[]>;
openChannel: (from: LndNode, to: LndNode, amount: string) => Promise<LND.ChannelPoint>;
openChannel: (
from: LndNode,
to: LndNode,
amount: string,
) => Promise<LightningNodeChannelPoint>;
closeChannel: (node: LndNode, channelPoint: string) => Promise<any>;
listChannels: (node: LndNode) => Promise<LND.ListChannelsResponse>;
pendingChannels: (node: LndNode) => Promise<LND.PendingChannelsResponse>;

1
src/utils/tests/renderWithProviders.tsx

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

Loading…
Cancel
Save