Skip to content
Draft
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
24 changes: 23 additions & 1 deletion modules/sdk-core/src/bitgo/defi/defiVault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,21 @@ 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,
txRequestIds: {
approve: approveTxRequestId,
deposit: depositTxRequestId,
},
...((approvePendingApprovalId || depositPendingApprovalId) && {
pendingApprovalIds: {
...(approvePendingApprovalId && { approve: approvePendingApprovalId }),
...(depositPendingApprovalId && { deposit: depositPendingApprovalId }),
},
}),
};
}

Expand Down Expand Up @@ -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 } }),
};
}

Expand Down Expand Up @@ -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 }) };
}

/**
Expand Down Expand Up @@ -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, unknown>): string | undefined {
const pendingApproval = sendManyResult.pendingApproval as Record<string, unknown> | undefined;
return pendingApproval?.id as string | undefined;
}

/**
* Extract operationId from a sendMany result.
*
Expand Down
4 changes: 4 additions & 0 deletions modules/sdk-core/src/bitgo/defi/iDefiVault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}

/**
Expand Down Expand Up @@ -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 {
Expand Down
87 changes: 85 additions & 2 deletions modules/sdk-core/test/unit/bitgo/defi/defiVault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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')));
Expand Down Expand Up @@ -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' },
Expand All @@ -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(
Expand Down Expand Up @@ -783,14 +845,35 @@ 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];
args.type.should.equal('defiWithdraw');
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';
Expand Down