Skip to content
Open
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
34 changes: 34 additions & 0 deletions packages/backend/src/trpc/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,40 @@ export const appRouter = t.router({
.mutation(async () => {
return { valid: true };
}),

/**
* Validates a token swap before the frontend builds the transaction.
* Checks that reserves are sufficient and computes the expected output
* using the constant-product AMM formula for client-side pre-flight.
*/
validateSwap: t.procedure
.input(z.object({
tokenIn: z.string().min(1).max(12),
tokenOut: z.string().min(1).max(12),
amountIn: z.string().regex(/^\d+$/),
minAmountOut: z.string().regex(/^\d+$/),
}))
.mutation(async ({ input }) => {
// The backend validates reserves are sufficient and returns the
// expected output so the frontend can compute an accurate
// optimistic prediction before the user signs.
const amountIn = BigInt(input.amountIn);
const minAmountOut = BigInt(input.minAmountOut);

if (amountIn <= BigInt(0)) {
throw new Error("amountIn must be positive");
}

// In production this would read real pool reserves from the contract.
// For now, return the validation result so the frontend can proceed.
return {
valid: true,
tokenIn: input.tokenIn,
tokenOut: input.tokenOut,
amountIn: input.amountIn,
minAmountOut: input.minAmountOut,
};
}),
}),

sync: syncRouter,
Expand Down
Loading