Skip to content
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
22 changes: 12 additions & 10 deletions modules/sdk-core/src/bitgo/defi/defiVault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ export class DefiVault implements IDefiVault {
*
* Internally issues two sendMany calls (approve + deposit) and returns the
* operationId that links them. If the deposit sendMany fails after
* the approve succeeds, the approve is auto-cancelled (fail-fast).
* the approve succeeds, the error propagates — the server-side reconciler
* handles orphaned approvals.
*
* @param params.vaultId - DeFi-service vault identifier
* @param params.amount - amount in base units of the underlying asset
Expand All @@ -67,15 +68,16 @@ export class DefiVault implements IDefiVault {
throw new Error('amount is required');
}

// Layer-1 pre-flight: reject if an active deposit already exists for this (wallet, vault)
const activeOps: DefiOperationListResult = await this.bitgo
.get(this.bitgo.microservicesUrl(this.operationsUrl()))
.query({ vaultId: params.vaultId, state: 'active' })
.result();

if (activeOps.items && activeOps.items.length > 0) {
throw new ActiveOperationExistsError(activeOps.items[0].operationId);
}
// TODO(CGD-1709): Re-enable active operation pre-flight check once the
Comment thread
kamleshmugdiya marked this conversation as resolved.
// defi-service operations endpoint is deployed and returning active state.
// const activeOps: DefiOperationListResult = await this.bitgo
// .get(this.bitgo.microservicesUrl(this.operationsUrl()))
// .query({ vaultId: params.vaultId, state: 'active' })
// .result();
//
// if (activeOps.items && activeOps.items.length > 0) {
// throw new ActiveOperationExistsError(activeOps.items[0].operationId);
// }

// Step 1: Approve txRequest via sendMany
const approveResult = await this.wallet.sendMany({
Expand Down
17 changes: 2 additions & 15 deletions modules/sdk-core/test/unit/bitgo/defi/defiVault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ describe('DefiVault', function () {
it('should call sendMany for approve and deposit on happy path', async function () {
const operationId = 'op-uuid-123';

// Pre-flight: no active operations
const preflightReq = mockRequest({ items: [] });
mockBitGo.get.onFirstCall().returns(preflightReq);

// Mock sendMany for approve and deposit
const sendManyStub = sinon.stub(wallet, 'sendMany');
sendManyStub.onFirstCall().resolves({
Expand All @@ -82,9 +78,6 @@ describe('DefiVault', function () {
result.txRequestIds.approve.should.equal('txreq-approve-1');
result.txRequestIds.deposit.should.equal('txreq-deposit-1');

// Verify pre-flight was called with correct query
preflightReq.query.calledWith({ vaultId: 'vlt-galaxy-usdc', state: 'active' }).should.be.true();

// Verify sendMany was called with correct params for approve
sendManyStub.calledTwice.should.be.true();
const approveArgs: any = sendManyStub.firstCall.args[0];
Expand All @@ -98,7 +91,8 @@ describe('DefiVault', function () {
depositArgs.defiParams.operationId.should.equal(operationId);
});

it('should reject when an active operation already exists', async function () {
// TODO(CGD-1709): Re-enable when active operation pre-flight check is restored
xit('should reject when an active operation already exists', async function () {
const preflightReq = mockRequest({
items: [{ operationId: 'existing-op-id', state: 'APPROVE_TX_REQUESTED' }],
});
Expand All @@ -117,10 +111,6 @@ describe('DefiVault', function () {
it('should propagate deposit sendMany failure without cleanup', async function () {
const operationId = 'op-uuid-456';

// Pre-flight: no active operations
const preflightReq = mockRequest({ items: [] });
mockBitGo.get.returns(preflightReq);

// Mock sendMany: approve succeeds, deposit fails
const sendManyStub = sinon.stub(wallet, 'sendMany');
sendManyStub.onFirstCall().resolves({
Expand Down Expand Up @@ -154,9 +144,6 @@ describe('DefiVault', function () {
it('should pass clientIdempotencyKey and walletPassphrase when provided', async function () {
const operationId = 'op-uuid-789';

const preflightReq = mockRequest({ items: [] });
mockBitGo.get.returns(preflightReq);

const sendManyStub = sinon.stub(wallet, 'sendMany');
sendManyStub.onFirstCall().resolves({
txRequest: {
Expand Down
Loading