Skip to content

Commit a5d912c

Browse files
authored
fix: creating transfer before sending tx
2 parents 1facd02 + c7e30c5 commit a5d912c

File tree

3 files changed

+149
-6
lines changed

3 files changed

+149
-6
lines changed

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

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,16 @@ export class LightningWallet implements ILightningWallet {
292292
};
293293
}
294294

295+
const transfer: { id: string } = await this.wallet.bitgo
296+
.post(
297+
this.wallet.bitgo.url(
298+
'/wallet/' + this.wallet.id() + '/txrequests/' + transactionRequestCreate.txRequestId + '/transfers',
299+
2
300+
)
301+
)
302+
.send()
303+
.result();
304+
295305
const transactionRequestSend = await commonTssMethods.sendTxRequest(
296306
this.wallet.bitgo,
297307
this.wallet.id(),
@@ -301,17 +311,15 @@ export class LightningWallet implements ILightningWallet {
301311
);
302312

303313
const coinSpecific = transactionRequestSend.transactions?.[0]?.unsignedTx?.coinSpecific;
304-
let transfer;
305-
if (coinSpecific && coinSpecific.paymentHash && typeof coinSpecific.paymentHash === 'string') {
306-
transfer = await this.wallet.getTransfer({ id: coinSpecific.paymentHash });
307-
}
314+
const updatedTransfer = await this.wallet.getTransfer({ id: transfer.id });
315+
308316
return {
309317
txRequestId: transactionRequestCreate.txRequestId,
310318
txRequestState: transactionRequestSend.state,
311319
paymentStatus: coinSpecific
312320
? t.exact(LndCreatePaymentResponse).encode(coinSpecific as LndCreatePaymentResponse)
313321
: undefined,
314-
transfer,
322+
transfer: updatedTransfer,
315323
};
316324
}
317325

@@ -344,6 +352,16 @@ export class LightningWallet implements ILightningWallet {
344352
};
345353
}
346354

355+
const transfer: { id: string } = await this.wallet.bitgo
356+
.post(
357+
this.wallet.bitgo.url(
358+
'/wallet/' + this.wallet.id() + '/txrequests/' + transactionRequestCreate.txRequestId + '/transfers',
359+
2
360+
)
361+
)
362+
.send()
363+
.result();
364+
347365
const transactionRequestSend = await commonTssMethods.sendTxRequest(
348366
this.wallet.bitgo,
349367
this.wallet.id(),
@@ -352,9 +370,12 @@ export class LightningWallet implements ILightningWallet {
352370
reqId
353371
);
354372

373+
const updatedTransfer = await this.wallet.getTransfer({ id: transfer.id });
374+
355375
return {
356376
txRequestId: transactionRequestCreate.txRequestId,
357377
txRequestState: transactionRequestSend.state,
378+
transfer: updatedTransfer,
358379
};
359380
}
360381

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

Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,12 @@ describe('Lightning wallets', function () {
395395
],
396396
};
397397

398+
const createTransferData = {
399+
id: 'fake_id',
400+
coin: 'tlnbtc',
401+
state: 'initialized',
402+
};
403+
398404
const transferData = {
399405
id: 'fake_id',
400406
coin: 'tlnbtc',
@@ -403,13 +409,17 @@ describe('Lightning wallets', function () {
403409
};
404410

405411
const getTransferNock = nock(bgUrl)
406-
.get(`/api/v2/${coinName}/wallet/${wallet.wallet.id()}/transfer/${lndResponse.paymentHash}`)
412+
.get(`/api/v2/${coinName}/wallet/${wallet.wallet.id()}/transfer/fake_id`)
407413
.reply(200, transferData);
408414

409415
const createTxRequestNock = nock(bgUrl)
410416
.post(`/api/v2/wallet/${wallet.wallet.id()}/txrequests`)
411417
.reply(200, txRequestResponse);
412418

419+
const createTransferNock = nock(bgUrl)
420+
.post(`/api/v2/wallet/${wallet.wallet.id()}/txrequests/${txRequestResponse.txRequestId}/transfers`)
421+
.reply(200, createTransferData);
422+
413423
const sendTxRequestNock = nock(bgUrl)
414424
.post(`/api/v2/wallet/${wallet.wallet.id()}/txrequests/${txRequestResponse.txRequestId}/transactions/0/send`)
415425
.reply(200, finalPaymentResponse);
@@ -424,6 +434,7 @@ describe('Lightning wallets', function () {
424434
const response = await wallet.payInvoice(params);
425435
assert.strictEqual(response.txRequestId, 'txReq123');
426436
assert.strictEqual(response.txRequestState, 'delivered');
437+
assert.strictEqual(response.transfer.id, transferData.id);
427438
assert(response.paymentStatus);
428439
assert.strictEqual(
429440
response.paymentStatus.status,
@@ -449,6 +460,7 @@ describe('Lightning wallets', function () {
449460
getTransferNock.done();
450461
createTxRequestNock.done();
451462
sendTxRequestNock.done();
463+
createTransferNock.done();
452464
userAuthKeyNock.done();
453465
nodeAuthKeyNock.done();
454466
});
@@ -821,20 +833,119 @@ describe('Lightning wallets', function () {
821833
state: 'delivered',
822834
};
823835

836+
const transferResponse = {
837+
entries: [
838+
{
839+
address: 'tb1qr54zqkf0sjwdycygulkcuw0audm8n6r77xj3q6',
840+
wallet: '6846918381f118cc42c335b037929665',
841+
value: -1000000,
842+
valueString: '-1000000',
843+
},
844+
{
845+
address: 'tb1qmddle8nhnjcv93t0qnr2tmhvvxfem4ppxq4rzc',
846+
value: 498590,
847+
valueString: '498590',
848+
isChange: true,
849+
isPayGo: false,
850+
},
851+
{
852+
address: 'tb1qjq48cqk2u80hewdcndf539m8nnnvt8453kr23h',
853+
value: 500000,
854+
valueString: '500000',
855+
isChange: false,
856+
isPayGo: false,
857+
},
858+
],
859+
id: '6846918481f118cc42c33611410906e5',
860+
coin: 'tlnbtc',
861+
wallet: 'walletId',
862+
walletType: 'hot',
863+
enterprise: '6846918381f118cc42c3354ff05d72d8',
864+
organization: '6846918381f118cc42c3355f6807d11e',
865+
txidType: 'transactionHash',
866+
txRequestId: 'txReq123',
867+
height: 999999999,
868+
heightId: '999999999-6846918481f118cc42c33611410906e5',
869+
date: '2025-06-09T07:47:16.090Z',
870+
type: 'send',
871+
value: -1000000,
872+
valueString: '-1000000',
873+
intendedValueString: '-1000000',
874+
baseValue: -998590,
875+
baseValueString: '-998590',
876+
baseValueWithoutFees: -998590,
877+
baseValueWithoutFeesString: '-998590',
878+
feeString: '1410',
879+
payGoFee: 0,
880+
payGoFeeString: '0',
881+
usd: -1.23456,
882+
usdRate: 123456,
883+
state: 'initialized',
884+
instant: false,
885+
isReward: false,
886+
isUnlock: false,
887+
isFee: false,
888+
senderInformationVerified: false,
889+
tags: ['6846918381f118cc42c335b037929665', '6846918381f118cc42c3354ff05d72d8'],
890+
history: [
891+
{
892+
date: '2025-06-09T07:47:16.102Z',
893+
user: '6846918281f118cc42c33352779df88f',
894+
action: 'created',
895+
},
896+
],
897+
coinSpecific: {
898+
isOffchain: false,
899+
},
900+
metadata: [],
901+
createdTime: '2025-06-09T07:47:16.102Z',
902+
};
903+
904+
const updatedTransferResponse = {
905+
...transferResponse,
906+
txid: '211b8fb30990632751a83d1dc4f0323ff7d2fd3cad88084de13c9be2ae1c6426',
907+
date: '2025-06-09T08:50:55.063Z',
908+
state: 'signed',
909+
history: [
910+
{
911+
date: '2025-06-09T07:50:55.063Z',
912+
user: '6846918281f118cc42c33352779df88f',
913+
action: 'signed',
914+
},
915+
{
916+
date: '2025-06-09T07:47:16.102Z',
917+
user: '6846918281f118cc42c33352779df88f',
918+
action: 'created',
919+
},
920+
],
921+
signedTime: '2025-06-09T08:50:55.063Z',
922+
};
923+
824924
const createTxRequestNock = nock(bgUrl)
825925
.post(`/api/v2/wallet/${wallet.wallet.id()}/txrequests`)
826926
.reply(200, txRequestResponse);
827927

928+
const createTransferNock = nock(bgUrl)
929+
.post(`/api/v2/wallet/${wallet.wallet.id()}/txrequests/${txRequestResponse.txRequestId}/transfers`)
930+
.reply(200, transferResponse);
931+
828932
const sendTxRequestNock = nock(bgUrl)
829933
.post(`/api/v2/wallet/${wallet.wallet.id()}/txrequests/${txRequestResponse.txRequestId}/transactions/0/send`)
830934
.reply(200, finalPaymentResponse);
831935

936+
const getTransferNock = nock(bgUrl)
937+
.get(`/api/v2/${coinName}/wallet/${wallet.wallet.id()}/transfer/${transferResponse.id}`)
938+
.reply(200, updatedTransferResponse);
939+
832940
const response = await wallet.withdrawOnchain(params);
833941
assert.strictEqual(response.txRequestId, 'txReq123');
834942
assert.strictEqual(response.txRequestState, 'delivered');
943+
assert.deepStrictEqual(response.transfer, updatedTransferResponse);
835944

836945
createTxRequestNock.done();
946+
createTransferNock.done();
837947
sendTxRequestNock.done();
948+
getTransferNock.done();
838949
});
839950

840951
it('should handle pending approval when withdrawing onchain', async function () {

modules/sdk-core/src/bitgo/pendingApproval/pendingApproval.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,17 @@ export class PendingApproval implements IPendingApproval {
435435
assert(this._pendingApproval.txRequestId, 'Missing txRequestId');
436436
// this.populateWallet is called before this so we should be good here
437437
assert(this.wallet?.id(), 'Missing wallet id');
438+
// todo: add test case for this
439+
// add new transfer before sending tx
440+
await this.bitgo
441+
.post(
442+
this.bitgo.url(
443+
'/wallet/' + this.wallet?.id() + '/txrequests/' + this._pendingApproval.txRequestId + '/transfers',
444+
2
445+
)
446+
)
447+
.send()
448+
.result();
438449
await sendTxRequest(
439450
this.bitgo,
440451
this.wallet?.id() as string,

0 commit comments

Comments
 (0)