Browse Source

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

master
jamaljsr 5 years ago
parent
commit
5119cf9093
  1. 6
      src/lib/clightning/clightningService.ts
  2. 10
      src/lib/lightning/notImplementedService.ts
  3. 17
      src/lib/lightning/types.ts
  4. 8
      src/lib/lnd/lndService.ts
  5. 3
      src/types/index.ts
  6. 3
      src/utils/chart.spec.ts
  7. 36
      src/utils/chart.ts

6
src/lib/clightning/clightningService.ts

@ -1,5 +1,6 @@
import { CLightningNode, LightningNode } from 'shared/types';
import {
LightningNodeAddress,
LightningNodeBalances,
LightningNodeInfo,
LightningService,
@ -32,6 +33,11 @@ class CLightningService implements LightningService {
};
}
async getNewAddress(node: LightningNode): Promise<LightningNodeAddress> {
const address = await this.request<string>(node, 'getNewAddress');
return { address };
}
/**
* Helper function to continually query the node until a successful
* response is received or it times out

10
src/lib/lightning/notImplementedService.ts

@ -1,5 +1,10 @@
import { LightningNode } from 'shared/types';
import { LightningNodeBalances, LightningNodeInfo, LightningService } from './types';
import {
LightningNodeAddress,
LightningNodeBalances,
LightningNodeInfo,
LightningService,
} from './types';
class NotImplementedService implements LightningService {
getInfo(node: LightningNode): Promise<LightningNodeInfo> {
@ -13,6 +18,9 @@ class NotImplementedService implements LightningService {
`waitUntilOnline is not implemented for ${node.implementation} nodes`,
);
}
getNewAddress(node: LightningNode): Promise<LightningNodeAddress> {
throw new Error(`getNewAddress is not implemented for ${node.implementation} nodes`);
}
}
export default new NotImplementedService();

17
src/lib/lightning/types.ts

@ -16,11 +16,26 @@ export interface LightningNodeBalances {
unconfirmed: string;
}
export interface LightningNodeAddress {
address: string;
}
export interface LightningNodeChannel {
pending: boolean;
uniqueId: string;
channelPoint: string;
pubkey: string;
capacity: string;
localBalance: string;
remoteBalance: string;
status: string;
}
export interface LightningService {
waitUntilOnline: (node: LightningNode) => Promise<void>;
getInfo: (node: LightningNode) => Promise<LightningNodeInfo>;
getBalances: (node: LightningNode) => Promise<LightningNodeBalances>;
// getNewAddress: (node: LightningNode) => Promise<LND.NewAddressResponse>;
getNewAddress: (node: LightningNode) => Promise<LightningNodeAddress>;
// openChannel: (from: LightningNode, to: LightningNode, amount: string) => Promise<LND.ChannelPoint>;
// closeChannel: (node: LightningNode, channelPoint: string) => Promise<any>;
// listChannels: (node: LightningNode) => Promise<LND.ListChannelsResponse>;

8
src/lib/lnd/lndService.ts

@ -1,6 +1,10 @@
import * as LND from '@radar/lnrpc';
import { LndNode } from 'shared/types';
import { LightningNodeBalances, LightningNodeInfo } from 'lib/lightning/types';
import {
LightningNodeAddress,
LightningNodeBalances,
LightningNodeInfo,
} from 'lib/lightning/types';
import { LndLibrary } from 'types';
import { waitFor } from 'utils/async';
import { getContainerName } from 'utils/network';
@ -29,7 +33,7 @@ class LndService implements LndLibrary {
};
}
async getNewAddress(node: LndNode): Promise<LND.NewAddressResponse> {
async getNewAddress(node: LndNode): Promise<LightningNodeAddress> {
return await proxy.getNewAddress(node);
}

3
src/types/index.ts

@ -4,6 +4,7 @@ import { ChainInfo, WalletInfo } from 'bitcoin-core';
import { BitcoinNode, CommonNode, LightningNode, LndNode, Status } from 'shared/types';
import { IpcSender } from 'lib/ipc/ipcService';
import {
LightningNodeAddress,
LightningNodeBalances,
LightningNodeInfo,
LightningService,
@ -55,7 +56,7 @@ export interface LndLibrary {
waitUntilOnline: (node: LndNode) => Promise<void>;
getInfo: (node: LndNode) => Promise<LightningNodeInfo>;
getBalances: (node: LndNode) => Promise<LightningNodeBalances>;
getNewAddress: (node: LndNode) => Promise<LND.NewAddressResponse>;
getNewAddress: (node: LndNode) => Promise<LightningNodeAddress>;
openChannel: (from: LndNode, to: LndNode, amount: string) => Promise<LND.ChannelPoint>;
closeChannel: (node: LndNode, channelPoint: string) => Promise<any>;
listChannels: (node: LndNode) => Promise<LND.ListChannelsResponse>;

3
src/utils/chart.spec.ts

@ -2,9 +2,8 @@ import { IChart, IConfig } from '@mrblenny/react-flow-chart';
import { defaultChannel, defaultPendingChannel, defaultPendingOpenChannel } from 'shared';
import { LndNodeMapping } from 'store/models/lnd';
import { Network } from 'types';
import { defaultStateInfo } from 'utils/tests';
import { defaultStateInfo, getNetwork } from 'utils/tests';
import { initChartFromNetwork, snap, updateChartFromLnd } from './chart';
import { getNetwork } from './tests';
describe('Chart Util', () => {
let network: Network;

36
src/utils/chart.ts

@ -1,6 +1,7 @@
import { IChart, IConfig, ILink, INode, IPosition } from '@mrblenny/react-flow-chart';
import { Channel, PendingChannel } from '@radar/lnrpc';
import { BitcoinNode, LightningNode } from 'shared/types';
import { LightningNodeChannel } from 'lib/lightning/types';
import { LndNodeMapping } from 'store/models/lnd';
import { Network } from 'types';
import btclogo from 'resources/bitcoin.svg';
@ -115,18 +116,7 @@ const updateNodeSize = (node: INode) => {
};
};
interface ChannelInfo {
pending: boolean;
uniqueId: string;
channelPoint: string;
pubkey: string;
capacity: string;
localBalance: string;
remoteBalance: string;
status: string;
}
const mapOpenChannel = (chan: Channel): ChannelInfo => ({
const mapOpenChannel = (chan: Channel): LightningNodeChannel => ({
pending: false,
uniqueId: chan.channelPoint.slice(-12),
channelPoint: chan.channelPoint,
@ -137,7 +127,9 @@ const mapOpenChannel = (chan: Channel): ChannelInfo => ({
status: 'Open',
});
const mapPendingChannel = (status: string) => (chan: PendingChannel): ChannelInfo => ({
const mapPendingChannel = (status: string) => (
chan: PendingChannel,
): LightningNodeChannel => ({
pending: true,
uniqueId: chan.channelPoint.slice(-12),
channelPoint: chan.channelPoint,
@ -149,15 +141,15 @@ const mapPendingChannel = (status: string) => (chan: PendingChannel): ChannelInf
});
const updateLinksAndPorts = (
info: ChannelInfo,
chan: LightningNodeChannel,
pubkeys: Record<string, string>,
nodes: { [x: string]: INode },
fromNode: INode,
links: { [x: string]: ILink },
) => {
// use the channel point as a unique id since pending channels do not have a channel id yet
const chanId = info.uniqueId;
const toName = pubkeys[info.pubkey];
const chanId = chan.uniqueId;
const toName = pubkeys[chan.pubkey];
const toNode = nodes[toName];
const fromOnLeftSide = fromNode.position.x < toNode.position.x;
@ -184,13 +176,13 @@ const updateLinksAndPorts = (
from: { nodeId: fromNode.id, portId: chanId },
to: { nodeId: toName, portId: chanId },
properties: {
type: info.pending ? 'pending-channel' : 'open-channel',
channelPoint: info.channelPoint,
capacity: info.capacity,
fromBalance: info.localBalance,
toBalance: info.remoteBalance,
type: chan.pending ? 'pending-channel' : 'open-channel',
channelPoint: chan.channelPoint,
capacity: chan.capacity,
fromBalance: chan.localBalance,
toBalance: chan.remoteBalance,
direction: fromOnLeftSide ? 'ltr' : 'rtl',
status: info.status,
status: chan.status,
},
};
};

Loading…
Cancel
Save