-
Notifications
You must be signed in to change notification settings - Fork 18
Add "remove and withdraw all" option to order removal (#2024) #2722
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
Open
thedavidmeister
wants to merge
8
commits into
main
Choose a base branch
from
2026-06-13-issue-2024
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+483
−6
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1dea738
Add "remove and withdraw all" option to order removal
thedavidmeister 235e3ce
Merge branch 'main' into 2026-06-13-issue-2024
thedavidmeister 83a09ed
Rewrite process/mutation comments to current-behavior-only
thedavidmeister 76e0701
Merge remote-tracking branch 'origin/main' into 2026-06-13-issue-2024
thedavidmeister 1da79a7
merge(main): resolve conflicts [merge-update]
thedavidmeister 710f986
merge(main): resolve LocalDbSyncGate wrapper conflict [merge-update]
thedavidmeister 073f41a
Merge remote-tracking branch 'origin/2026-06-13-issue-2024' into 2026…
thedavidmeister 4d47d87
Merge remote-tracking branch 'origin/main' into 2026-06-13-issue-2024
thedavidmeister 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
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
219 changes: 219 additions & 0 deletions
219
packages/webapp/src/__tests__/handleRemoveOrderAndWithdrawAll.test.ts
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,219 @@ | ||
| import { describe, it, expect, vi, beforeEach } from "vitest"; | ||
| import { | ||
| handleRemoveOrderAndWithdrawAll, | ||
| type HandleRemoveOrderAndWithdrawAllDependencies, | ||
| } from "../lib/services/handleRemoveOrderAndWithdrawAll"; | ||
| import type { | ||
| RaindexClient, | ||
| RaindexOrder, | ||
| RaindexVaultsList, | ||
| } from "@rainlanguage/raindex"; | ||
| import type { Hex } from "viem"; | ||
| import { decodeFunctionData } from "viem"; | ||
| import type { TransactionManager } from "@rainlanguage/ui-components"; | ||
|
|
||
| const MULTICALL_ABI = [ | ||
| { | ||
| type: "function", | ||
| name: "multicall", | ||
| inputs: [{ name: "data", type: "bytes[]" }], | ||
| outputs: [{ name: "results", type: "bytes[]" }], | ||
| stateMutability: "nonpayable", | ||
| }, | ||
| ] as const; | ||
|
|
||
| const REMOVE_CALLDATA = "0xdeadbeef" as Hex; | ||
| const WITHDRAW_CALLDATA = "0xcafe" as Hex; | ||
|
|
||
| // Pre-computed via viem: multicall([REMOVE_CALLDATA, WITHDRAW_CALLDATA]). | ||
| const EXPECTED_BOTH = | ||
| "0xac9650d800000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000004deadbeef000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002cafe000000000000000000000000000000000000000000000000000000000000"; | ||
| // Pre-computed via viem: multicall([REMOVE_CALLDATA]). | ||
| const EXPECTED_REMOVE_ONLY = | ||
| "0xac9650d80000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000004deadbeef00000000000000000000000000000000000000000000000000000000"; | ||
|
|
||
| const mockHandleTransactionConfirmationModal = vi.fn(); | ||
| const mockErrToast = vi.fn(); | ||
| const mockCreateRemoveOrderTransaction = vi.fn(); | ||
|
|
||
| const mockManager = { | ||
| createRemoveOrderTransaction: mockCreateRemoveOrderTransaction, | ||
| } as unknown as TransactionManager; | ||
|
|
||
| const mockRaindexClient = {} as unknown as RaindexClient; | ||
|
|
||
| const orderHash = "0xorderhash" as Hex; | ||
| const raindex = "0xraindexaddress" as Hex; | ||
| const chainId = 137; | ||
|
|
||
| const makeOrder = (overrides: Partial<RaindexOrder> = {}) => | ||
| ({ | ||
| orderHash, | ||
| raindex, | ||
| chainId, | ||
| getRemoveCalldata: vi | ||
| .fn() | ||
| .mockReturnValue({ value: REMOVE_CALLDATA, error: undefined }), | ||
| ...overrides, | ||
| }) as unknown as RaindexOrder; | ||
|
|
||
| const makeVaultsList = (overrides: Partial<RaindexVaultsList> = {}) => | ||
| ({ | ||
| getWithdrawableVaults: vi | ||
| .fn() | ||
| .mockReturnValue({ value: [{ id: "0xvault" }], error: undefined }), | ||
| getWithdrawCalldata: vi | ||
| .fn() | ||
| .mockResolvedValue({ value: WITHDRAW_CALLDATA, error: undefined }), | ||
| ...overrides, | ||
| }) as unknown as RaindexVaultsList; | ||
|
|
||
| const makeDeps = ( | ||
| order: RaindexOrder, | ||
| vaultsList: RaindexVaultsList, | ||
| ): HandleRemoveOrderAndWithdrawAllDependencies => ({ | ||
| raindexClient: mockRaindexClient, | ||
| order, | ||
| vaultsList, | ||
| handleTransactionConfirmationModal: mockHandleTransactionConfirmationModal, | ||
| errToast: mockErrToast, | ||
| manager: mockManager, | ||
| }); | ||
|
|
||
| describe("handleRemoveOrderAndWithdrawAll", () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| mockHandleTransactionConfirmationModal.mockResolvedValue({ success: true }); | ||
| }); | ||
|
|
||
| it("batches removeOrder + withdraw into a single multicall in that exact order", async () => { | ||
| const order = makeOrder(); | ||
| const vaultsList = makeVaultsList(); | ||
|
|
||
| await handleRemoveOrderAndWithdrawAll(makeDeps(order, vaultsList)); | ||
|
|
||
| expect(mockHandleTransactionConfirmationModal).toHaveBeenCalledOnce(); | ||
| const args = mockHandleTransactionConfirmationModal.mock.calls[0][0].args; | ||
|
|
||
| // The calldata is exactly the multicall of removeOrder then withdraw, | ||
| // in that order, under the multicall selector. | ||
| expect(args.calldata).toBe(EXPECTED_BOTH); | ||
|
|
||
| // Decode to prove it is a flat multicall containing the two calls in order. | ||
| const decoded = decodeFunctionData({ | ||
| abi: MULTICALL_ABI, | ||
| data: args.calldata, | ||
| }); | ||
| expect(decoded.functionName).toBe("multicall"); | ||
| expect(decoded.args[0]).toEqual([REMOVE_CALLDATA, WITHDRAW_CALLDATA]); | ||
|
|
||
| expect(args.toAddress).toBe(raindex); | ||
| expect(args.chainId).toBe(chainId); | ||
| expect(args.entity).toBe(order); | ||
| expect(mockErrToast).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("removes the order alone (no withdraw call) when there are no withdrawable vaults", async () => { | ||
| const order = makeOrder(); | ||
| const vaultsList = makeVaultsList({ | ||
| getWithdrawableVaults: vi | ||
| .fn() | ||
| .mockReturnValue({ value: [], error: undefined }), | ||
| }); | ||
|
|
||
| await handleRemoveOrderAndWithdrawAll(makeDeps(order, vaultsList)); | ||
|
|
||
| const args = mockHandleTransactionConfirmationModal.mock.calls[0][0].args; | ||
| expect(args.calldata).toBe(EXPECTED_REMOVE_ONLY); | ||
|
|
||
| const decoded = decodeFunctionData({ | ||
| abi: MULTICALL_ABI, | ||
| data: args.calldata, | ||
| }); | ||
| expect(decoded.args[0]).toEqual([REMOVE_CALLDATA]); | ||
|
|
||
| // Must not even ask for withdraw calldata when nothing is withdrawable. | ||
| expect(vaultsList.getWithdrawCalldata).not.toHaveBeenCalled(); | ||
| expect(mockErrToast).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("uses the combined-action modal title", async () => { | ||
| await handleRemoveOrderAndWithdrawAll( | ||
| makeDeps(makeOrder(), makeVaultsList()), | ||
| ); | ||
|
|
||
| expect(mockHandleTransactionConfirmationModal).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| modalTitle: "Removing order and withdrawing all vaults", | ||
| }), | ||
| ); | ||
| }); | ||
|
|
||
| it("creates a remove-order transaction on confirmation", async () => { | ||
| const order = makeOrder(); | ||
| const txHash = "0xtxhash" as Hex; | ||
|
|
||
| await handleRemoveOrderAndWithdrawAll(makeDeps(order, makeVaultsList())); | ||
|
|
||
| const onConfirm = | ||
| mockHandleTransactionConfirmationModal.mock.calls[0][0].args.onConfirm; | ||
| onConfirm(txHash); | ||
|
|
||
| expect(mockCreateRemoveOrderTransaction).toHaveBeenCalledWith({ | ||
| raindexClient: mockRaindexClient, | ||
| txHash, | ||
| queryKey: orderHash, | ||
| chainId, | ||
| entity: order, | ||
| }); | ||
| }); | ||
|
|
||
| it("toasts the remove-calldata error and never opens the modal", async () => { | ||
| const order = makeOrder({ | ||
| getRemoveCalldata: vi.fn().mockReturnValue({ | ||
| value: undefined, | ||
| error: { msg: "remove failed", readableMsg: "Remove calldata failed" }, | ||
| }), | ||
| }); | ||
|
|
||
| await handleRemoveOrderAndWithdrawAll(makeDeps(order, makeVaultsList())); | ||
|
|
||
| expect(mockErrToast).toHaveBeenCalledWith("Remove calldata failed"); | ||
| expect(mockHandleTransactionConfirmationModal).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("toasts the withdraw-calldata error and never opens the modal", async () => { | ||
| const vaultsList = makeVaultsList({ | ||
| getWithdrawCalldata: vi.fn().mockResolvedValue({ | ||
| value: undefined, | ||
| error: { | ||
| msg: "withdraw failed", | ||
| readableMsg: "Withdraw calldata failed", | ||
| }, | ||
| }), | ||
| }); | ||
|
|
||
| await handleRemoveOrderAndWithdrawAll(makeDeps(makeOrder(), vaultsList)); | ||
|
|
||
| expect(mockErrToast).toHaveBeenCalledWith( | ||
| "Failed to generate withdraw calldata: Withdraw calldata failed", | ||
| ); | ||
| expect(mockHandleTransactionConfirmationModal).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("toasts the withdrawable-vaults lookup error and never opens the modal", async () => { | ||
| const vaultsList = makeVaultsList({ | ||
| getWithdrawableVaults: vi.fn().mockReturnValue({ | ||
| value: undefined, | ||
| error: { msg: "lookup failed", readableMsg: "Lookup failed" }, | ||
| }), | ||
| }); | ||
|
|
||
| await handleRemoveOrderAndWithdrawAll(makeDeps(makeOrder(), vaultsList)); | ||
|
|
||
| expect(mockErrToast).toHaveBeenCalledWith( | ||
| "Failed to get withdrawable vaults: Lookup failed", | ||
| ); | ||
| expect(mockHandleTransactionConfirmationModal).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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Only show the withdraw-all action when the order actually has withdrawable balance.
This branch only checks whether
onRemoveAndWithdrawAllexists, so a zero-balancevaultsListstill advertises "Remove and withdraw all vaults" even though the downstream handler degrades to a plain remove. The newOrderDetail.test.tsfixture uses that exact all-zero shape, so this misleading path is already being asserted. Gate the extra item on a withdrawable-balance check (or disable/rename it) and keep the plain remove action for empty vaults.💡 Suggested change
📝 Committable suggestion
🤖 Prompt for AI Agents