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
67 changes: 64 additions & 3 deletions src/background/services/privacy-provider-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ export class PrivacyProviderClient {
"bundle.operations_count": String(params.operationsMLXDR.length),
"bundle.channel_contract_id": params.channelContractId,
});
let operationsBundleId: string;
try {
const response = await axios.post<{
status: number;
Expand All @@ -186,16 +187,16 @@ export class PrivacyProviderClient {
},
},
);
const operationsBundleId = response.data.data?.operationsBundleId;
if (!operationsBundleId || typeof operationsBundleId !== "string") {
const id = response.data.data?.operationsBundleId;
if (!id || typeof id !== "string") {
throw new Error(
`Invalid bundle response: operationsBundleId not found in ${
JSON.stringify(response.data)
}`,
);
}
operationsBundleId = id;
if (span) endSpan(span, { code: 0 });
return { id: operationsBundleId };
} catch (error: unknown) {
if (span) endSpan(span, { code: 2, message: String(error) });
// Check if it's an authentication error
Expand All @@ -207,5 +208,65 @@ export class PrivacyProviderClient {
// Re-throw other errors as-is
throw error;
}

// POST returns immediately with status:"PENDING" — poll until the
// executor settles the bundle on chain. Mirrors the e2e suite's
// waitForBundle. Without this, the popup goes home before settlement
// and the home view's balance polling races the executor.
await this.waitForBundle(token, operationsBundleId);
return { id: operationsBundleId };
}

private async waitForBundle(
token: string,
bundleId: string,
timeoutMs = 180_000,
pollIntervalMs = 5_000,
): Promise<void> {
const span = this.startSpan("GET /api/v1/bundle/:id (poll)", {
"bundle.id": bundleId,
});
const start = Date.now();
try {
while (Date.now() - start < timeoutMs) {
await new Promise((r) => setTimeout(r, pollIntervalMs));
const res = await axios.get<{
status: number;
message: string;
data: { status: string };
}>(
`${this.baseUrl}/api/v1/bundle/${bundleId}`,
{
headers: {
Authorization: `Bearer ${token}`,
...(span ? this.traceHeaders(span.spanId) : {}),
},
},
);
const status = res.data.data?.status;
if (status === "COMPLETED") {
if (span) endSpan(span, { code: 0 });
return;
}
if (status === "FAILED" || status === "EXPIRED") {
const err = new Error(`Bundle ${bundleId} ${status}`);
if (span) endSpan(span, { code: 2, message: err.message });
throw err;
}
// PENDING / SUBMITTED / other intermediate → keep polling
}
const err = new Error(
`Bundle ${bundleId} timed out after ${timeoutMs}ms`,
);
if (span) endSpan(span, { code: 2, message: err.message });
throw err;
} catch (error: unknown) {
if (this.isAuthError(error)) {
throw new PrivacyProviderAuthError(
"Provider session expired or invalid. Please reconnect to the provider.",
);
}
throw error;
}
}
}
4 changes: 2 additions & 2 deletions src/popup/api/deposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const prepareDeposit = async (
return await callBackground<MessageType.PrepareDeposit>({
type: MessageType.PrepareDeposit,
...params,
});
}, { timeoutMs: 240_000 });
};

export const deposit = async (
Expand All @@ -22,5 +22,5 @@ export const deposit = async (
return await callBackground<MessageType.Deposit>({
type: MessageType.Deposit,
...params,
});
}, { timeoutMs: 240_000 });
};
4 changes: 2 additions & 2 deletions src/popup/api/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const send = async (
return await callBackground<MessageType.Send>({
type: MessageType.Send,
...params,
});
}, { timeoutMs: 240_000 });
};

export const prepareSend = async (
Expand All @@ -22,5 +22,5 @@ export const prepareSend = async (
return await callBackground<MessageType.PrepareSend>({
type: MessageType.PrepareSend,
...params,
});
}, { timeoutMs: 240_000 });
};
4 changes: 2 additions & 2 deletions src/popup/api/withdraw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const withdraw = async (
return await callBackground<MessageType.Withdraw>({
type: MessageType.Withdraw,
...params,
});
}, { timeoutMs: 240_000 });
};

export const prepareWithdraw = async (
Expand All @@ -22,5 +22,5 @@ export const prepareWithdraw = async (
return await callBackground<MessageType.PrepareWithdraw>({
type: MessageType.PrepareWithdraw,
...params,
});
}, { timeoutMs: 240_000 });
};
1 change: 1 addition & 0 deletions src/popup/pages/send-confirmation-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ export function SendConfirmationPage() {

<CardFooter className="px-0 space-y-2">
<Button
id="send-execute-btn"
className="w-full"
disabled={busy}
onClick={handleExecute}
Expand Down
1 change: 1 addition & 0 deletions src/popup/pages/withdraw-confirmation-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ export function WithdrawConfirmationPage() {

<CardFooter className="px-0 space-y-2">
<Button
id="withdraw-execute-btn"
className="w-full"
disabled={busy}
onClick={handleExecute}
Expand Down
1 change: 1 addition & 0 deletions src/popup/templates/deposit-review-template.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ export function DepositReviewTemplate(props: DepositReviewTemplateProps) {
{/* Action Buttons */}
<div className="space-y-2">
<Button
id="deposit-execute-btn"
className="w-full"
disabled={props.busy}
onClick={() => props.onSubmit()}
Expand Down
Loading