Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rpc): deposit & withdraw calls using lnd #1481

Merged
merged 1 commit into from
Apr 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
};
32 changes: 32 additions & 0 deletions lib/grpc/GrpcService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,38 @@ class GrpcService {
}
}

/**
* 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);
}
}

/**
* See [[Service.ban]]
*/
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