Skip to content

Commit 0882a64

Browse files
chore: try to use vercel route
1 parent 63dd775 commit 0882a64

File tree

10 files changed

+827
-35
lines changed

10 files changed

+827
-35
lines changed

.gitignore

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@ coverage
1818
cypress.env.json
1919
tests/cypress/screenshots
2020

21-
# next.js
22-
.next/
23-
out/
24-
build
21+
# Vercel
22+
/apps/main/.vercel/
2523

2624
# misc
2725
.DS_Store

apps/main/_api/router.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { buildOptimalRouteResponse } from 'router-api/src/routes/optimal-route'
2+
import type { Decimal, OptimalRouteQuery } from 'router-api/src/routes/optimal-route.schemas'
3+
import type { Address } from 'viem'
4+
import type { VercelRequest, VercelResponse } from '@vercel/node'
5+
6+
export default async function handler(request: VercelRequest, response: VercelResponse) {
7+
console.info(`${request.method} ${request.url}`)
8+
const u = new URL(request.url!)
9+
// todo: validate query params
10+
const query: OptimalRouteQuery = {
11+
chainId: +(u.searchParams.get('chainId') ?? 1),
12+
tokenIn: u.searchParams.getAll('tokenIn') as [Address],
13+
tokenOut: u.searchParams.getAll('tokenOut') as [Address],
14+
amountIn: u.searchParams.get('amountIn') ? (u.searchParams.getAll('amountIn') as [Decimal]) : undefined,
15+
amountOut: u.searchParams.get('amountOut') ? (u.searchParams.getAll('amountOut') as [Decimal]) : undefined,
16+
}
17+
18+
return response.send(await buildOptimalRouteResponse(query))
19+
}

apps/main/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@
5555
"@types/node": "24.3.0",
5656
"@types/react": "*",
5757
"@types/react-dom": "*",
58-
"@vitejs/plugin-react": "^5.0.1",
58+
"@vercel/node": "^5.3.25",
59+
"@vitejs/plugin-react": "^5.0.4",
5960
"babel-plugin-styled-components": "^2.1.4",
6061
"eslint": "*",
6162
"eslint-config-custom": "*",

apps/main/vite.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ export default defineConfig(({ command }) => ({
3535
destination: '/favicon.ico',
3636
},
3737
{
38-
source: '/(.*)',
38+
// match all files except api routes
39+
source: '/((?!api/).*)',
3940
destination: '/index.html',
4041
},
4142
],

apps/router-api/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
},
99
"scripts": {
1010
"dev": "tsx watch src/dev.ts",
11-
"start": "node dist/index.js",
11+
"start": "node dist/api/index.js",
1212
"build": "tsc --project tsconfig.build.json",
1313
"lint": "eslint \"src/**/*.ts\"",
1414
"typecheck": "tsc --noEmit",

apps/router-api/src/routes/optimal-route.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export function registerOptimalRoute(server: FastifyInstance): void {
2222
},
2323
async (request: FastifyRequest<{ Querystring: OptimalRouteQuery }>) => {
2424
const query = request.query
25-
return buildOptimalRouteResponse(await loadCurve(query.chainId), query)
25+
return buildOptimalRouteResponse(query)
2626
},
2727
)
2828
}
@@ -55,14 +55,16 @@ export async function routerGetToStoredRate(routes: IRoute, curve: CurveJS, toAd
5555
] as Decimal
5656
}
5757

58-
async function buildOptimalRouteResponse(curve: CurveJS, query: OptimalRouteQuery): Promise<RouteResponse[]> {
58+
export async function buildOptimalRouteResponse(query: OptimalRouteQuery): Promise<RouteResponse[]> {
5959
const {
6060
tokenOut: [toToken],
6161
tokenIn: [fromToken],
6262
chainId,
6363
amountIn: [amountIn] = [],
6464
amountOut,
6565
} = query
66+
67+
const curve = await loadCurve(query.chainId)
6668
const fromAmount = amountIn ?? ((await curve.router.required(fromToken, toToken, amountOut?.[0] ?? '0')) as Decimal)
6769
const { route: routes, output } = await curve.router.getBestRouteAndOutput(fromToken, toToken, fromAmount)
6870
if (!routes.length) return []

apps/router-api/vercel.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
{
2+
"functions": {
3+
"api/index.ts": {
4+
"runtime": "nodejs20.x"
5+
}
6+
},
27
"rewrites": [
38
{
4-
"source": "/(.*)",
5-
"destination": "/api/index.js"
9+
"source": "/api/(.*)",
10+
"destination": "/api/index.ts"
611
}
712
]
813
}

packages/curve-ui-kit/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"@storybook/react-vite": "9.1.3",
4646
"@types/react": "*",
4747
"@types/react-dom": "*",
48-
"@vitejs/plugin-react": "^5.0.1",
48+
"@vitejs/plugin-react": "^5.0.4",
4949
"eslint": "*",
5050
"eslint-config-custom": "*",
5151
"storybook": "9.1.3",

tests/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"@cypress/vite-dev-server": "^7.0.0",
2626
"@types/cypress": "^1.1.6",
2727
"@types/wait-on": "^5.3.4",
28-
"@vitejs/plugin-react": "^5.0.1",
28+
"@vitejs/plugin-react": "^5.0.4",
2929
"@vitest/ui": "^3.2.4",
3030
"cypress": "^15.0.0",
3131
"dotenv-flow": "^4.1.0",

0 commit comments

Comments
 (0)