-
Notifications
You must be signed in to change notification settings - Fork 679
[SDK] Fix Universal Bridge onramp reporting success when it did not complete #8906
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1504689
[SDK] Fix Universal Bridge onramp reporting success when it did not c…
0xFirekeeper 2786b4d
[SDK] Fail fast on an incomplete onramp before follow-up transactions
0xFirekeeper 1706591
[SDK] Test onramp completion guards in useStepExecutor
0xFirekeeper 3dac8ab
[SDK] Type-check onramp test fixture; assert no false success on retry
0xFirekeeper 3ae67ca
[SDK] Recover a failed onramp with a fresh session on retry
0xFirekeeper File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "thirdweb": patch | ||
| --- | ||
|
|
||
| Fixed Universal Bridge onramp checkout incorrectly reporting success when the onramp did not complete. A failed onramp now surfaces the error instead of a false success, and retrying a failed onramp prepares a fresh payment session rather than replaying the expired one (post-onramp transaction failures still retry in place, so completed onramps are never charged twice). |
134 changes: 134 additions & 0 deletions
134
packages/thirdweb/src/react/core/hooks/useStepExecutor.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| import { act, renderHook, waitFor } from "@testing-library/react"; | ||
| import { describe, expect, it, vi } from "vitest"; | ||
| import { TEST_CLIENT } from "~test/test-clients.js"; | ||
| import type { WindowAdapter } from "../adapters/WindowAdapter.js"; | ||
| import type { BridgePrepareResult } from "./useBridgePrepare.js"; | ||
| import { useStepExecutor } from "./useStepExecutor.js"; | ||
|
|
||
| // Avoid firing analytics network calls while executing. | ||
| vi.mock("../../../analytics/track/pay.js", () => ({ | ||
| trackPayEvent: vi.fn(), | ||
| })); | ||
|
|
||
| const { onrampStatusMock } = vi.hoisted(() => ({ | ||
| onrampStatusMock: vi.fn(), | ||
| })); | ||
| vi.mock("../../../bridge/index.js", () => ({ | ||
| Onramp: { | ||
| status: (options: unknown) => onrampStatusMock(options), | ||
| }, | ||
| })); | ||
|
|
||
| // Minimal onramp quote with no follow-up transactions, so the executor's | ||
| // behaviour depends solely on the onramp outcome. | ||
| const ONRAMP_QUOTE: Extract<BridgePrepareResult, { type: "onramp" }> = { | ||
| currency: "USD", | ||
| currencyAmount: 30, | ||
| destinationAmount: 30000000n, | ||
| destinationToken: { | ||
| address: "0x0000000000000000000000000000000000000000", | ||
| chainId: 8453, | ||
| decimals: 6, | ||
| name: "USD Coin", | ||
| prices: {}, | ||
| symbol: "USDC", | ||
| }, | ||
| id: "onramp-session-id", | ||
| intent: { | ||
| chainId: 8453, | ||
| onramp: "transak", | ||
| receiver: "0x0000000000000000000000000000000000000001", | ||
| tokenAddress: "0x0000000000000000000000000000000000000000", | ||
| }, | ||
| link: "https://onramp.example.com/session", | ||
| steps: [], | ||
| type: "onramp", | ||
| }; | ||
|
|
||
| function createWindowAdapter(): WindowAdapter { | ||
| return { open: vi.fn(async () => {}) }; | ||
| } | ||
|
|
||
| describe("useStepExecutor onramp guards", () => { | ||
| it("surfaces an error when the onramp reports FAILED", async () => { | ||
| onrampStatusMock.mockResolvedValue({ status: "FAILED", transactions: [] }); | ||
| const windowAdapter = createWindowAdapter(); | ||
|
|
||
| const { result } = renderHook(() => | ||
| useStepExecutor({ | ||
| client: TEST_CLIENT, | ||
| preparedQuote: ONRAMP_QUOTE, | ||
| windowAdapter, | ||
| }), | ||
| ); | ||
|
|
||
| await act(async () => { | ||
| result.current.start(); | ||
| }); | ||
|
|
||
| await waitFor(() => expect(result.current.onrampStatus).toBe("failed")); | ||
| expect(result.current.error?.message).toBe("Payment failed"); | ||
| expect(result.current.executionState).toBe("idle"); | ||
| expect(windowAdapter.open).toHaveBeenCalledWith(ONRAMP_QUOTE.link); | ||
| }); | ||
|
|
||
| it("does not report success when a prior onramp attempt failed", async () => { | ||
| onrampStatusMock.mockResolvedValue({ status: "FAILED", transactions: [] }); | ||
| const windowAdapter = createWindowAdapter(); | ||
| const onComplete = vi.fn(); | ||
|
|
||
| const { result } = renderHook(() => | ||
| useStepExecutor({ | ||
| client: TEST_CLIENT, | ||
| onComplete, | ||
| preparedQuote: ONRAMP_QUOTE, | ||
| windowAdapter, | ||
| }), | ||
| ); | ||
|
|
||
| // First attempt fails and leaves the onramp in the "failed" state. | ||
| await act(async () => { | ||
| result.current.start(); | ||
| }); | ||
| await waitFor(() => expect(result.current.onrampStatus).toBe("failed")); | ||
|
|
||
| // Retrying must fail fast on the incomplete onramp rather than proceeding. | ||
| await act(async () => { | ||
| result.current.retry(); | ||
| }); | ||
| await waitFor(() => | ||
| expect(result.current.error?.message).toBe("Onramp did not complete"), | ||
| ); | ||
| expect(result.current.error?.statusCode).toBe(500); | ||
| // Success must never be reported for an incomplete onramp. | ||
| expect(onComplete).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("marks the onramp complete before reporting success", async () => { | ||
| onrampStatusMock.mockResolvedValue({ | ||
| status: "COMPLETED", | ||
| transactions: [], | ||
| }); | ||
| const windowAdapter = createWindowAdapter(); | ||
| const onComplete = vi.fn(); | ||
|
|
||
| const { result } = renderHook(() => | ||
| useStepExecutor({ | ||
| client: TEST_CLIENT, | ||
| onComplete, | ||
| preparedQuote: ONRAMP_QUOTE, | ||
| windowAdapter, | ||
| }), | ||
| ); | ||
|
|
||
| await act(async () => { | ||
| result.current.start(); | ||
| }); | ||
|
|
||
| await waitFor(() => expect(result.current.onrampStatus).toBe("completed"), { | ||
| timeout: 5000, | ||
| }); | ||
| expect(result.current.error).toBeUndefined(); | ||
| expect(onComplete).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
packages/thirdweb/src/react/web/ui/Bridge/StepRunner.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| import { fireEvent, render, screen, waitFor } from "@testing-library/react"; | ||
| import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
| import { TEST_CLIENT } from "~test/test-clients.js"; | ||
| import type { WindowAdapter } from "../../../core/adapters/WindowAdapter.js"; | ||
| import { CustomThemeProvider } from "../../../core/design-system/CustomThemeProvider.js"; | ||
| import type { | ||
| BridgePrepareRequest, | ||
| BridgePrepareResult, | ||
| } from "../../../core/hooks/useBridgePrepare.js"; | ||
| import { StepRunner } from "./StepRunner.js"; | ||
|
|
||
| // Controllable executor + prepare seams so the test drives the retry branch | ||
| // purely off `onrampStatus`. | ||
| const { executor, retrySpy, refetchSpy, FRESH_QUOTE } = vi.hoisted(() => ({ | ||
| executor: { | ||
| error: undefined as Error | undefined, | ||
| onrampStatus: undefined as | ||
| | "pending" | ||
| | "executing" | ||
| | "completed" | ||
| | "failed" | ||
| | undefined, | ||
| }, | ||
| FRESH_QUOTE: { | ||
| id: "fresh-session", | ||
| link: "https://onramp.example.com/fresh", | ||
| type: "onramp", | ||
| }, | ||
| refetchSpy: vi.fn(), | ||
| retrySpy: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("../../../core/hooks/useStepExecutor.js", () => ({ | ||
| useStepExecutor: () => ({ | ||
| cancel: vi.fn(), | ||
| currentStep: undefined, | ||
| error: executor.error, | ||
| executionState: "idle" as const, | ||
| onrampStatus: executor.onrampStatus, | ||
| progress: 0, | ||
| retry: retrySpy, | ||
| start: vi.fn(), | ||
| steps: [], | ||
| }), | ||
| })); | ||
|
|
||
| vi.mock("../../../core/hooks/useBridgePrepare.js", async (importOriginal) => { | ||
| const actual = | ||
| await importOriginal< | ||
| typeof import("../../../core/hooks/useBridgePrepare.js") | ||
| >(); | ||
| return { ...actual, useBridgePrepare: () => ({ refetch: refetchSpy }) }; | ||
| }); | ||
|
|
||
| const ONRAMP_REQUEST: BridgePrepareRequest = { | ||
| chainId: 8453, | ||
| client: TEST_CLIENT, | ||
| onramp: "transak", | ||
| receiver: "0x0000000000000000000000000000000000000001", | ||
| tokenAddress: "0x0000000000000000000000000000000000000000", | ||
| type: "onramp", | ||
| }; | ||
|
|
||
| const ONRAMP_QUOTE: Extract<BridgePrepareResult, { type: "onramp" }> = { | ||
| currency: "USD", | ||
| currencyAmount: 30, | ||
| destinationAmount: 30000000n, | ||
| destinationToken: { | ||
| address: "0x0000000000000000000000000000000000000000", | ||
| chainId: 8453, | ||
| decimals: 6, | ||
| name: "USD Coin", | ||
| prices: {}, | ||
| symbol: "USDC", | ||
| }, | ||
| id: "onramp-session-id", | ||
| intent: { | ||
| chainId: 8453, | ||
| onramp: "transak", | ||
| receiver: "0x0000000000000000000000000000000000000001", | ||
| tokenAddress: "0x0000000000000000000000000000000000000000", | ||
| }, | ||
| link: "https://onramp.example.com/session", | ||
| steps: [], | ||
| type: "onramp", | ||
| }; | ||
|
|
||
| function renderStepRunner() { | ||
| const onQuoteUpdate = vi.fn(); | ||
| render( | ||
| <CustomThemeProvider theme="dark"> | ||
| <StepRunner | ||
| autoStart={false} | ||
| client={TEST_CLIENT} | ||
| onBack={vi.fn()} | ||
| onCancel={vi.fn()} | ||
| onComplete={vi.fn()} | ||
| onQuoteUpdate={onQuoteUpdate} | ||
| preparedQuote={ONRAMP_QUOTE} | ||
| request={ONRAMP_REQUEST} | ||
| title={undefined} | ||
| wallet={undefined} | ||
| windowAdapter={{ open: vi.fn(async () => {}) } as WindowAdapter} | ||
| /> | ||
| </CustomThemeProvider>, | ||
| ); | ||
| return { onQuoteUpdate }; | ||
| } | ||
|
|
||
| describe("StepRunner onramp retry recovery", () => { | ||
| beforeEach(() => { | ||
| retrySpy.mockReset(); | ||
| refetchSpy.mockReset(); | ||
| refetchSpy.mockResolvedValue({ data: FRESH_QUOTE }); | ||
| executor.error = new Error("Payment failed"); | ||
| }); | ||
|
|
||
| it("re-prepares a fresh session when a failed onramp is retried", async () => { | ||
| executor.onrampStatus = "failed"; | ||
| const { onQuoteUpdate } = renderStepRunner(); | ||
|
|
||
| fireEvent.click(screen.getByText("Retry")); | ||
|
|
||
| await waitFor(() => expect(refetchSpy).toHaveBeenCalledTimes(1)); | ||
| // Must NOT replay the dead session in place. | ||
| expect(retrySpy).not.toHaveBeenCalled(); | ||
| await waitFor(() => | ||
| expect(onQuoteUpdate).toHaveBeenCalledWith(FRESH_QUOTE), | ||
| ); | ||
| }); | ||
|
|
||
| it("retries in place (never re-onramps) once the onramp has completed", async () => { | ||
| // A post-onramp transaction failed: funds already arrived, so re-onramping | ||
| // would double-charge the buyer. | ||
| executor.onrampStatus = "completed"; | ||
| renderStepRunner(); | ||
|
|
||
| fireEvent.click(screen.getByText("Retry")); | ||
|
|
||
| expect(retrySpy).toHaveBeenCalledTimes(1); | ||
| expect(refetchSpy).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.