Skip to content

Commit

Permalink
feat(rpc): deposit & withdraw calls using lnd
Browse files Browse the repository at this point in the history
This adds new rpc calls that allow to get new deposit addresses and
withdraw coins via xud. Currently these calls only support lnd.

Closes #1062.
  • Loading branch information
sangaman committed Apr 15, 2020
1 parent 5d0f626 commit e25b751
Show file tree
Hide file tree
Showing 14 changed files with 1,831 additions and 275 deletions.
70 changes: 70 additions & 0 deletions docs/api.md

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

20 changes: 20 additions & 0 deletions lib/cli/commands/deposit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Arguments } from 'yargs';
import { DepositRequest } from '../../proto/xudrpc_pb';
import { callback, loadXudClient } from '../command';

export const command = 'deposit <currency>';

export const describe = 'gets an address to deposit funds to xud';

export const builder = {
currency: {
description: 'the ticker symbol of the currency to deposit.',
type: 'string',
},
};

export const handler = (argv: Arguments<any>) => {
const request = new DepositRequest();
request.setCurrency(argv.currency);
loadXudClient(argv).deposit(request, callback(argv));
};
44 changes: 44 additions & 0 deletions lib/cli/commands/withdraw.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Arguments } from 'yargs';
import { WithdrawRequest } from '../../proto/xudrpc_pb';
import { callback, loadXudClient } from '../command';

export const command = 'withdraw <amount> <currency> <destination> [fee]';

export const describe = 'withdraws on-chain funds from xud';

export const builder = {
amount: {
description: 'the amount to withdraw',
type: 'number',
},
currency: {
description: 'the ticker symbol of the currency to withdraw.',
type: 'string',
},
destination: {
description: 'the address to send withdrawn funds to',
type: 'string',
},
fee: {
description: 'the fee in satoshis (or equivalent) per byte',
type: 'number',
},
all: {
description: 'whether to withdraw all available funds for the specified currency',
type: 'boolean',
},
};

export const handler = (argv: Arguments<any>) => {
const request = new WithdrawRequest();
request.setCurrency(argv.currency);
if (argv.all) {
request.setAll(argv.all);
} else {
request.setAmount(argv.amount);
}
request.setDestination(argv.destination);
request.setFee(argv.fee);

loadXudClient(argv).withdraw(request, callback(argv));
};
34 changes: 33 additions & 1 deletion lib/grpc/GrpcService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,39 @@ class GrpcService {
try {
const { nodeUri } = call.request.toObject();
await this.service.connect({ nodeUri, retryConnecting: false });
const response = new xudrpc.ConnectResponse();
const response = new xudrpc.WithdrawResponse();
callback(null, response);
} catch (err) {
callback(getGrpcError(err), null);
}
}

/**
* See [[Service.deposit]]
*/
public deposit: grpc.handleUnaryCall<xudrpc.DepositRequest, xudrpc.DepositResponse> = async (call, callback) => {
if (!this.isReady(this.service, callback)) {
return;
}
try {
await this.service.deposit(call.request.toObject());
const response = new xudrpc.DepositResponse();
callback(null, response);
} catch (err) {
callback(getGrpcError(err), null);
}
}

/**
* See [[Service.withdraw]]
*/
public withdraw: grpc.handleUnaryCall<xudrpc.WithdrawRequest, xudrpc.WithdrawResponse> = async (call, callback) => {
if (!this.isReady(this.service, callback)) {
return;
}
try {
await this.service.withdraw(call.request.toObject());
const response = new xudrpc.WithdrawResponse();
callback(null, response);
} catch (err) {
callback(getGrpcError(err), null);
Expand Down
25 changes: 23 additions & 2 deletions lib/lndclient/LndClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,26 @@ class LndClient extends SwapClient {
return this.unaryCall<lndrpc.ClosedChannelsRequest, lndrpc.ClosedChannelsResponse>('closedChannels', new lndrpc.ClosedChannelsRequest());
}

public withdraw = async ({ amount, destination, all = false, fee }: {
amount: number,
destination: string,
all?: boolean,
fee?: number,
}) => {
const request = new lndrpc.SendCoinsRequest();
request.setAddr(destination);
if (fee) {
request.setSatPerByte(fee);
}
if (all) {
request.setSendAll(all);
} else {
request.setAmount(amount);
}
const withdrawResponse = await this.unaryCall<lndrpc.SendCoinsRequest, lndrpc.SendCoinsResponse>('sendCoins', request);
return withdrawResponse.getTxid();
}

public sendSmallestAmount = async (rHash: string, destination: string): Promise<string> => {
const request = this.buildSendRequest({
rHash,
Expand Down Expand Up @@ -633,10 +653,11 @@ class LndClient extends SwapClient {
/**
* Gets a new address for the internal lnd wallet.
*/
public newAddress = (addressType: lndrpc.AddressType): Promise<lndrpc.NewAddressResponse> => {
public newAddress = async (addressType = lndrpc.AddressType.WITNESS_PUBKEY_HASH) => {
const request = new lndrpc.NewAddressRequest();
request.setType(addressType);
return this.unaryCall<lndrpc.NewAddressRequest, lndrpc.NewAddressResponse>('newAddress', request);
const newAddressResponse = await this.unaryCall<lndrpc.NewAddressRequest, lndrpc.NewAddressResponse>('newAddress', request);
return newAddressResponse.getAddress();
}

/**
Expand Down
109 changes: 109 additions & 0 deletions lib/proto/xudrpc.swagger.json

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

Loading

0 comments on commit e25b751

Please sign in to comment.