Skip to content

Commit 2ad9e03

Browse files
authored
Merge pull request #6378 from BitGo/BTC-2234-withdraw-failure-reason
feat: added withdrawStatus in response lightning
2 parents 2f0c31c + 8666309 commit 2ad9e03

File tree

4 files changed

+63
-9
lines changed

4 files changed

+63
-9
lines changed

modules/abstract-lightning/src/codecs/api/withdraw.ts

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import { LightningOnchainRecipient } from '@bitgo/public-types';
33
import { PendingApprovalData, TxRequestState } from '@bitgo/sdk-core';
44
import { BigIntFromString } from 'io-ts-types';
55

6-
// todo:(current) which to keep here which to take to common types
6+
export const WithdrawStatusDelivered = 'delivered';
7+
export const WithdrawStatusFailed = 'failed';
8+
9+
export const WithdrawStatus = t.union([t.literal(WithdrawStatusDelivered), t.literal(WithdrawStatusFailed)]);
10+
711
export const LightningOnchainWithdrawParams = t.type({
812
recipients: t.array(LightningOnchainRecipient),
913
satsPerVbyte: BigIntFromString,
@@ -13,6 +17,20 @@ export const LightningOnchainWithdrawParams = t.type({
1317

1418
export type LightningOnchainWithdrawParams = t.TypeOf<typeof LightningOnchainWithdrawParams>;
1519

20+
export const LndCreateWithdrawResponse = t.intersection(
21+
[
22+
t.type({
23+
status: WithdrawStatus,
24+
}),
25+
t.partial({
26+
txid: t.string,
27+
failureReason: t.string,
28+
}),
29+
],
30+
'LndCreateWithdrawResponse'
31+
);
32+
export type LndCreateWithdrawResponse = t.TypeOf<typeof LndCreateWithdrawResponse>;
33+
1634
export type LightningOnchainWithdrawResponse = {
1735
/**
1836
* Unique identifier for withdraw request submitted to BitGo.
@@ -32,6 +50,14 @@ export type LightningOnchainWithdrawResponse = {
3250
*/
3351
pendingApproval?: PendingApprovalData;
3452

53+
/**
54+
* Current snapshot of withdraw status (if available).
55+
* - **`'delivered'`**: Withdraw request is delivered to the blockchain.
56+
* - **`'failed'`**: Withdraw failed.
57+
* This field is absent if approval is required before processing.
58+
*/
59+
withdrawStatus?: LndCreateWithdrawResponse;
60+
3561
/**
3662
* Latest transfer details for this withdraw request (if available).
3763
* - Provides the current state of the transfer.
@@ -77,10 +103,16 @@ export const SendPsbtRequest = t.type(
77103
);
78104
export type SendPsbtRequest = t.TypeOf<typeof SendPsbtRequest>;
79105

80-
export const SendPsbtResponse = t.type(
81-
{
82-
label: t.string,
83-
},
106+
export const SendPsbtResponse = t.intersection(
107+
[
108+
t.type({
109+
label: t.string,
110+
}),
111+
t.partial({
112+
status: WithdrawStatus,
113+
failureReason: t.string,
114+
}),
115+
],
84116
'SendPsbtResponse'
85117
);
86118
export type SendPsbtResponse = t.TypeOf<typeof SendPsbtResponse>;

modules/abstract-lightning/src/wallet/lightning.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
LightningOnchainWithdrawResponse,
2828
ListInvoicesResponse,
2929
ListPaymentsResponse,
30+
LndCreateWithdrawResponse,
3031
} from '../codecs';
3132
import { LightningPaymentIntent, LightningPaymentRequest } from '@bitgo/public-types';
3233

@@ -378,6 +379,7 @@ export class LightningWallet implements ILightningWallet {
378379
reqId
379380
);
380381

382+
const coinSpecific = transactionRequestSend.transactions?.[0]?.unsignedTx?.coinSpecific;
381383
let updatedTransfer: any = undefined;
382384
try {
383385
updatedTransfer = await this.wallet.getTransfer({ id: transfer.id });
@@ -391,6 +393,10 @@ export class LightningWallet implements ILightningWallet {
391393
txRequestId: transactionRequestCreate.txRequestId,
392394
txRequestState: transactionRequestSend.state,
393395
transfer: updatedTransfer,
396+
withdrawStatus:
397+
coinSpecific && 'status' in coinSpecific
398+
? t.exact(LndCreateWithdrawResponse).encode(coinSpecific as LndCreateWithdrawResponse)
399+
: undefined,
394400
};
395401
}
396402

modules/bitgo/test/v2/unit/lightning/lightningWallets.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -832,9 +832,19 @@ describe('Lightning wallets', function () {
832832
state: 'pendingDelivery',
833833
};
834834

835-
const finalPaymentResponse = {
835+
const finalWithdrawResponse = {
836836
txRequestId: 'txReq123',
837837
state: 'delivered',
838+
transactions: [
839+
{
840+
unsignedTx: {
841+
coinSpecific: {
842+
status: 'delivered',
843+
txid: 'tx123',
844+
},
845+
},
846+
},
847+
],
838848
};
839849

840850
const transferResponse = {
@@ -935,7 +945,7 @@ describe('Lightning wallets', function () {
935945

936946
const sendTxRequestNock = nock(bgUrl)
937947
.post(`/api/v2/wallet/${wallet.wallet.id()}/txrequests/${txRequestResponse.txRequestId}/transactions/0/send`)
938-
.reply(200, finalPaymentResponse);
948+
.reply(200, finalWithdrawResponse);
939949

940950
const getTransferNock = nock(bgUrl)
941951
.get(`/api/v2/${coinName}/wallet/${wallet.wallet.id()}/transfer/${transferResponse.id}`)
@@ -944,6 +954,8 @@ describe('Lightning wallets', function () {
944954
const response = await wallet.withdrawOnchain(params);
945955
assert.strictEqual(response.txRequestId, 'txReq123');
946956
assert.strictEqual(response.txRequestState, 'delivered');
957+
assert.strictEqual(response.withdrawStatus?.status, 'delivered');
958+
assert.strictEqual(response.withdrawStatus?.txid, 'tx123');
947959
assert.deepStrictEqual(response.transfer, updatedTransferResponse);
948960

949961
createTxRequestNock.done();

modules/express/test/unit/lightning/lightningWithdrawRoutes.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as sinon from 'sinon';
22
import * as should from 'should';
33
import * as express from 'express';
4-
import { PayInvoiceResponse } from '@bitgo/abstract-lightning';
4+
import { LightningOnchainWithdrawResponse } from '@bitgo/abstract-lightning';
55
import { BitGo } from 'bitgo';
66
import { handleLightningWithdraw } from '../../../src/lightning/lightningWithdrawRoutes';
77

@@ -34,9 +34,13 @@ describe('Lightning Withdraw Routes', () => {
3434
satsPerVbyte: '15',
3535
};
3636

37-
const expectedResponse: PayInvoiceResponse = {
37+
const expectedResponse: LightningOnchainWithdrawResponse = {
3838
txRequestState: 'delivered',
3939
txRequestId: '123',
40+
withdrawStatus: {
41+
status: 'delivered',
42+
txid: 'tx123',
43+
},
4044
};
4145

4246
const onchainWithdrawStub = sinon.stub().resolves(expectedResponse);

0 commit comments

Comments
 (0)