Skip to content

Commit

Permalink
Merge pull request #1467 from ExchangeUnion/rpc/channel-push
Browse files Browse the repository at this point in the history
feat(rpc): push amount in OpenChannel call
  • Loading branch information
sangaman authored Apr 13, 2020
2 parents d19c370 + 6bf82f9 commit 06fb107
Show file tree
Hide file tree
Showing 11 changed files with 313 additions and 234 deletions.
3 changes: 2 additions & 1 deletion docs/api.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion lib/cli/commands/openchannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { callback, loadXudClient } from '../command';
import { OpenChannelRequest } from '../../proto/xudrpc_pb';
import { coinsToSats } from '../utils';

export const command = 'openchannel <node_identifier> <currency> <amount>';
export const command = 'openchannel <node_identifier> <currency> <amount> [push_amount]';

export const describe = 'open a payment channel with a peer';

Expand All @@ -20,12 +20,17 @@ export const builder = {
type: 'number',
description: 'the amount to be deposited into the channel',
},
push_amount: {
type: 'number',
description: 'the amount to be pushed to the remote side of the channel',
},
};

export const handler = (argv: Arguments<any>) => {
const request = new OpenChannelRequest();
request.setNodeIdentifier(argv.node_identifier);
request.setCurrency(argv.currency.toUpperCase());
request.setAmount(coinsToSats(argv.amount));
request.setPushAmount(coinsToSats(argv.amount));
loadXudClient(argv).openChannel(request, callback(argv));
};
13 changes: 7 additions & 6 deletions lib/lndclient/LndClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -718,12 +718,12 @@ class LndClient extends SwapClient {
* Opens a channel given peerPubKey and amount.
*/
public openChannel = async (
{ peerIdentifier: peerPubKey, units, lndUris }:
{ peerIdentifier: string, units: number, lndUris: string[] },
{ peerIdentifier: peerPubKey, units, lndUris, pushUnits = 0 }:
{ peerIdentifier: string, units: number, lndUris: string[], pushUnits?: number },
): Promise<void> => {
const connectionEstablished = await this.connectPeerAddreses(lndUris);
if (connectionEstablished) {
await this.openChannelSync(peerPubKey, units);
await this.openChannelSync(peerPubKey, units, pushUnits);
} else {
throw new Error(`could not connect to lnd uris for ${peerPubKey}`);
}
Expand Down Expand Up @@ -762,10 +762,11 @@ class LndClient extends SwapClient {
/**
* Opens a channel with a connected lnd node.
*/
private openChannelSync = (node_pubkey_string: string, local_funding_amount: number): Promise<lndrpc.ChannelPoint> => {
private openChannelSync = (nodePubkeyString: string, localFundingAmount: number, pushSat = 0): Promise<lndrpc.ChannelPoint> => {
const request = new lndrpc.OpenChannelRequest;
request.setNodePubkeyString(node_pubkey_string);
request.setLocalFundingAmount(local_funding_amount);
request.setNodePubkeyString(nodePubkeyString);
request.setLocalFundingAmount(localFundingAmount);
request.setPushSat(pushSat);
return this.unaryCall<lndrpc.OpenChannelRequest, lndrpc.ChannelPoint>('openChannelSync', request);
}

Expand Down
4 changes: 4 additions & 0 deletions lib/proto/xudrpc_pb.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 28 additions & 1 deletion lib/proto/xudrpc_pb.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions lib/service/Service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,9 @@ class Service {
* Opens a payment channel to a specified node, currency and amount.
*/
public openChannel = async (
args: { nodeIdentifier: string, amount: number, currency: string },
args: { nodeIdentifier: string, amount: number, currency: string, pushAmount?: number },
) => {
const { nodeIdentifier, amount, currency } = args;
const { nodeIdentifier, amount, currency, pushAmount } = args;
argChecks.HAS_NODE_IDENTIFIER({ nodeIdentifier });
argChecks.POSITIVE_AMOUNT({ amount });
argChecks.VALID_CURRENCY({ currency });
Expand All @@ -253,6 +253,7 @@ class Service {
peer,
amount,
currency,
pushAmount,
});
} catch (e) {
const errorMessage = e.message || 'unknown';
Expand Down
3 changes: 2 additions & 1 deletion lib/swaps/SwapClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,13 @@ abstract class SwapClient extends EventEmitter {
* optional currency and optional lndUris.
*/
public abstract async openChannel(
{ peerIdentifier, units, currency, lndUris }:
{ peerIdentifier, units, currency, lndUris, pushUnits }:
{
peerIdentifier: string,
units: number,
currency?: string,
lndUris?: string[],
pushUnits?: number,
},
): Promise<void>;

Expand Down
11 changes: 8 additions & 3 deletions lib/swaps/SwapClientManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,8 @@ class SwapClientManager extends EventEmitter {
* @returns Nothing upon success, throws otherwise.
*/
public openChannel = async (
{ peer, amount, currency }:
{ peer: Peer, amount: number, currency: string },
{ peer, amount, currency, pushAmount = 0 }:
{ peer: Peer, amount: number, currency: string, pushAmount?: number },
): Promise<void> => {
const swapClient = this.get(currency);
if (!swapClient) {
Expand All @@ -408,12 +408,17 @@ class SwapClientManager extends EventEmitter {
amount,
currency,
});
const pushUnits = this.unitConverter.amountToUnits({
currency,
amount: pushAmount,
});

if (isLndClient(swapClient)) {
const lndUris = peer.getLndUris(currency);
if (!lndUris) {
throw new Error('unable to get lnd listening uris');
}
await swapClient.openChannel({ peerIdentifier, units, lndUris });
await swapClient.openChannel({ peerIdentifier, units, lndUris, pushUnits });
return;
}
// fallback to raiden for all non-lnd currencies
Expand Down
4 changes: 3 additions & 1 deletion proto/xudrpc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -507,8 +507,10 @@ message OpenChannelRequest {
string node_identifier = 1 [json_name = "node_identifier"];
// The ticker symbol of the currency to open the channel for.
string currency = 2 [json_name = "currency"];
// The amount of the channel denominated in satoshis.
// The amount to be deposited into the channel denominated in satoshis.
int64 amount = 3 [json_name = "amount"];
// The balance amount to be pushed to the remote side of the channel denominated in satoshis.
uint64 push_amount = 4 [json_name = "push_amount"];
}

message OpenChannelResponse {}
Expand Down
24 changes: 23 additions & 1 deletion test/jest/LndClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,29 @@ describe('LndClient', () => {
.toHaveBeenCalledWith(peerPubKey, externalIp1);
expect(lnd['openChannelSync']).toHaveBeenCalledTimes(1);
expect(lnd['openChannelSync'])
.toHaveBeenCalledWith(peerPubKey, units);
.toHaveBeenCalledWith(peerPubKey, units, 0);
});

test('it pushes satoshis to the peer when specified', async () => {
expect.assertions(4);
const pushUnits = 481824;
lnd['openChannelSync'] = jest.fn().mockReturnValue(Promise.resolve());
lnd['connectPeer'] = jest.fn()
.mockImplementationOnce(() => {
return Promise.resolve();
});
await lnd.openChannel({
units,
pushUnits,
peerIdentifier: peerPubKey,
lndUris: lndListeningUris,
});
expect(lnd['connectPeer']).toHaveBeenCalledTimes(1);
expect(lnd['connectPeer'])
.toHaveBeenCalledWith(peerPubKey, externalIp1);
expect(lnd['openChannelSync']).toHaveBeenCalledTimes(1);
expect(lnd['openChannelSync'])
.toHaveBeenCalledWith(peerPubKey, units, pushUnits);
});

test('it stops trying to connect to lnd uris when first once succeeds', async () => {
Expand Down
Loading

0 comments on commit 06fb107

Please sign in to comment.