From c9fef92748122d3b49e047c3d9bf9867b624442d Mon Sep 17 00:00:00 2001 From: Mullapudi Pruthvik Date: Tue, 24 Nov 2026 14:09:12 +0530 Subject: [PATCH] feat(sdk-core): surface pendingApproval ids in defi vault results MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit depositToVault and withdrawFromVault now return the per-leg pendingApproval id when a wallet policy gates a leg, so clients can route users to approvals and later resume signing — a resumed deposit that issues a new gated leg is indistinguishable from a fresh one. Fields are only set when an id exists, so un-gated results keep their exact previous shape. Optional fields, backward compatible. Ticket: DEFI-1358 --- modules/sdk-core/src/bitgo/defi/defiVault.ts | 24 ++++- modules/sdk-core/src/bitgo/defi/iDefiVault.ts | 4 + .../test/unit/bitgo/defi/defiVault.ts | 87 ++++++++++++++++++- 3 files changed, 112 insertions(+), 3 deletions(-) diff --git a/modules/sdk-core/src/bitgo/defi/defiVault.ts b/modules/sdk-core/src/bitgo/defi/defiVault.ts index 57e1fdfdd1..d80a6a9794 100644 --- a/modules/sdk-core/src/bitgo/defi/defiVault.ts +++ b/modules/sdk-core/src/bitgo/defi/defiVault.ts @@ -191,6 +191,8 @@ export class DefiVault implements IDefiVault { ...(params.walletPassphrase ? { walletPassphrase: params.walletPassphrase } : {}), }); const depositTxRequestId = this.extractTxRequestId(depositResult); + const approvePendingApprovalId = this.extractPendingApprovalId(approveResult); + const depositPendingApprovalId = this.extractPendingApprovalId(depositResult); return { operationId, @@ -198,6 +200,12 @@ export class DefiVault implements IDefiVault { approve: approveTxRequestId, deposit: depositTxRequestId, }, + ...((approvePendingApprovalId || depositPendingApprovalId) && { + pendingApprovalIds: { + ...(approvePendingApprovalId && { approve: approvePendingApprovalId }), + ...(depositPendingApprovalId && { deposit: depositPendingApprovalId }), + }, + }), }; } @@ -241,12 +249,15 @@ export class DefiVault implements IDefiVault { ...(params.walletPassphrase ? { walletPassphrase: params.walletPassphrase } : {}), }); + const depositPendingApprovalId = this.extractPendingApprovalId(depositResult); + return { operationId: params.operationId, txRequestIds: { approve: operation.txRequestId, deposit: this.extractTxRequestId(depositResult), }, + ...(depositPendingApprovalId && { pendingApprovalIds: { deposit: depositPendingApprovalId } }), }; } @@ -332,7 +343,8 @@ export class DefiVault implements IDefiVault { throw new Error('operationId not found in withdraw txRequest response'); } - return { operationId, txRequestId }; + const pendingApprovalId = this.extractPendingApprovalId(withdrawResult); + return { operationId, txRequestId, ...(pendingApprovalId && { pendingApprovalId }) }; } /** @@ -424,6 +436,16 @@ export class DefiVault implements IDefiVault { throw new Error('txRequestId not found in sendMany response'); } + /** + * Extract the pendingApproval id from a sendMany result, if the leg was + * gated by wallet policy. Absent for un-gated legs — callers keep their + * exact un-gated result shape. + */ + private extractPendingApprovalId(sendManyResult: Record): string | undefined { + const pendingApproval = sendManyResult.pendingApproval as Record | undefined; + return pendingApproval?.id as string | undefined; + } + /** * Extract operationId from a sendMany result. * diff --git a/modules/sdk-core/src/bitgo/defi/iDefiVault.ts b/modules/sdk-core/src/bitgo/defi/iDefiVault.ts index 5dddddaf91..77657a3994 100644 --- a/modules/sdk-core/src/bitgo/defi/iDefiVault.ts +++ b/modules/sdk-core/src/bitgo/defi/iDefiVault.ts @@ -67,6 +67,8 @@ export interface ConcreteDepositResult { export interface Erc4626DepositResult { operationId: string; txRequestIds: { approve: string; deposit: string }; + /** Set when a wallet policy gated the legs; ids of the legs' pending approvals. */ + pendingApprovalIds?: { approve?: string; deposit?: string }; } /** @@ -94,6 +96,8 @@ export interface WithdrawFromVaultOptions { export interface WithdrawResult { operationId: string; txRequestId: string; + /** Set when a wallet policy gated the withdraw. */ + pendingApprovalId?: string; } export interface WrapOptions { diff --git a/modules/sdk-core/test/unit/bitgo/defi/defiVault.ts b/modules/sdk-core/test/unit/bitgo/defi/defiVault.ts index 0c7ae82392..80629d1bb0 100644 --- a/modules/sdk-core/test/unit/bitgo/defi/defiVault.ts +++ b/modules/sdk-core/test/unit/bitgo/defi/defiVault.ts @@ -324,7 +324,7 @@ describe('DefiVault', function () { (result as any).operationId.should.equal(operationId); (result as any).txRequestIds.approve.should.equal('txreq-approve-1'); - (result as any).txRequestIds.deposit.should.equal('txreq-deposit-1'); + (result as any).should.not.have.property('pendingApprovalIds'); // Verify sendMany was called with correct params for approve sendManyStub.calledTwice.should.be.true(); @@ -338,6 +338,66 @@ describe('DefiVault', function () { depositArgs.type.should.equal('defiDeposit'); depositArgs.defiParams.operationId.should.equal(operationId); }); + it('should surface pendingApproval ids per leg when a policy gates both legs', async function () { + mockBitGo.get.returns(mockRequest(makeMorphoVault('vlt-galaxy-usdc'))); + + const operationId = 'op-uuid-gated'; + + const sendManyStub = sinon.stub(wallet, 'sendMany'); + sendManyStub.onFirstCall().resolves({ + pendingApproval: { id: 'pa-approve-1' }, + txRequest: { + txRequestId: 'txreq-approve-gated', + intent: { intentType: 'defi-approve' }, + transactions: [{ unsignedTx: { coinSpecific: { operationId } } }], + }, + }); + sendManyStub.onSecondCall().resolves({ + pendingApproval: { id: 'pa-deposit-1' }, + txRequest: { + txRequestId: 'txreq-deposit-gated', + intent: { intentType: 'defi-deposit' }, + transactions: [{ unsignedTx: { coinSpecific: { operationId } } }], + }, + }); + + const rawResult = await defiVault.depositToVault({ vaultId: 'vlt-galaxy-usdc', amount: '1000000' }); + const result = rawResult as Erc4626DepositResult; + result.operationId.should.equal(operationId); + result.pendingApprovalIds!.approve!.should.equal('pa-approve-1'); + result.pendingApprovalIds!.deposit!.should.equal('pa-deposit-1'); + }); + + it('should surface only the gated leg id when a policy gates just one leg', async function () { + mockVaultGetWithNoActiveOperations(makeMorphoVault('vlt-galaxy-usdc')); + + const operationId = 'op-uuid-deposit-gated'; + + const sendManyStub = sinon.stub(wallet, 'sendMany'); + // Only the deposit leg trips a wallet policy; the approve leg (0-value + // token approval) sails through un-gated. + sendManyStub.onFirstCall().resolves({ + txRequest: { + txRequestId: 'txreq-approve-ungated', + intent: { intentType: 'defi-approve' }, + transactions: [{ unsignedTx: { coinSpecific: { operationId } } }], + }, + }); + sendManyStub.onSecondCall().resolves({ + pendingApproval: { id: 'pa-deposit-only' }, + txRequest: { + txRequestId: 'txreq-deposit-gated', + intent: { intentType: 'defi-deposit' }, + transactions: [{ unsignedTx: { coinSpecific: { operationId } } }], + }, + }); + + const rawResult = await defiVault.depositToVault({ vaultId: 'vlt-galaxy-usdc', amount: '1000000' }); + const result = rawResult as Erc4626DepositResult; + // pendingApprovalIds is per-leg: only the gated deposit leg appears, + // and the deposit leg id must not be dropped for the un-gated approve. + result.pendingApprovalIds!.should.deepEqual({ deposit: 'pa-deposit-only' }); + }); it('should extract operationId from the lite apiVersion coinSpecific', async function () { mockBitGo.get.returns(mockRequest(makeMorphoVault('vlt-galaxy-usdc'))); @@ -598,6 +658,7 @@ describe('DefiVault', function () { // deposit sendMany const sendManyStub = sinon.stub(wallet, 'sendMany'); sendManyStub.resolves({ + pendingApproval: { id: 'pa-deposit-resume' }, txRequest: { txRequestId: 'txreq-deposit-resume', intent: { intentType: 'defi-deposit', operationId: 'op-resume-1' }, @@ -611,6 +672,7 @@ describe('DefiVault', function () { result.operationId.should.equal('op-resume-1'); result.txRequestIds.approve.should.equal('txreq-approve-existing'); result.txRequestIds.deposit.should.equal('txreq-deposit-resume'); + result.pendingApprovalIds!.deposit!.should.equal('pa-deposit-resume'); // getOperation must hit the vault-scoped route with the enterprise-id header mockBitGo.get.firstCall.args[0].should.endWith( @@ -783,7 +845,7 @@ describe('DefiVault', function () { }); result.operationId.should.equal(operationId); - result.txRequestId.should.equal('txreq-withdraw-1'); + result.should.not.have.property('pendingApprovalId'); sendManyStub.calledOnce.should.be.true(); const args: any = sendManyStub.firstCall.args[0]; @@ -791,6 +853,27 @@ describe('DefiVault', function () { args.defiParams.vaultId.should.equal('vlt-galaxy-usdc'); args.defiParams.amount.should.equal('500000'); }); + it('should surface the pendingApproval id when a policy gates the withdraw', async function () { + const operationId = 'op-withdraw-gated'; + + const sendManyStub = sinon.stub(wallet, 'sendMany'); + sendManyStub.resolves({ + pendingApproval: { id: 'pa-withdraw-1' }, + txRequest: { + txRequestId: 'txreq-withdraw-gated', + transactions: [{ unsignedTx: { coinSpecific: { operationId } } }], + }, + }); + + const result = await defiVault.withdrawFromVault({ + vaultId: 'vlt-galaxy-usdc', + amount: '500000', + }); + + result.operationId.should.equal(operationId); + result.txRequestId.should.equal('txreq-withdraw-gated'); + result.pendingApprovalId!.should.equal('pa-withdraw-1'); + }); it('should extract operationId from lite apiVersion coinSpecific', async function () { const operationId = 'op-withdraw-lite';