From 3288f27e37cb60095c9ff39f1b63218d5fd1b5f8 Mon Sep 17 00:00:00 2001 From: Femi John Date: Thu, 25 Jun 2026 14:09:30 +0100 Subject: [PATCH 1/2] feat: add GET /spreads/:asset route to calculate bid-ask spreads for given assets --- src/routes/spreads.ts | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/routes/spreads.ts diff --git a/src/routes/spreads.ts b/src/routes/spreads.ts new file mode 100644 index 0000000..b1c92d7 --- /dev/null +++ b/src/routes/spreads.ts @@ -0,0 +1,32 @@ +import type { FastifyInstance } from 'fastify'; +import { pgPool } from '../db'; + +/** + * GET /spreads/:asset - Return current bid, ask, and spread (bps) for the given asset. + * The bid is taken as the highest price among price points where the asset appears. + * The ask is the lowest price among those points. + * Spread basis points = ((ask - bid) / ask) * 10_000. + */ +export async function registerSpreadsRoutes(app: FastifyInstance) { + app.get('/spreads/:asset', async (req, reply) => { + const { asset } = req.params as { asset: string }; + if (!asset) { + return reply.status(400).send({ error: 'Asset parameter is required' }); + } + + const result = await pgPool.query( + `SELECT MAX(price::numeric) AS bid, MIN(price::numeric) AS ask + FROM price_points + WHERE assetA = $1 OR assetB = $1`, + [asset] + ); + const row = result.rows[0]; + const bid = row.bid !== null ? parseFloat(row.bid) : null; + const ask = row.ask !== null ? parseFloat(row.ask) : null; + let spreadBps: number | null = null; + if (bid !== null && ask !== null && ask !== 0) { + spreadBps = ((ask - bid) / ask) * 10_000; + } + return { asset, bid, ask, spreadBps }; + }); +} From c5dc120f4ea8df3e0eaa831a1deae49c6be34d01 Mon Sep 17 00:00:00 2001 From: Femi John Date: Sat, 27 Jun 2026 19:36:50 +0100 Subject: [PATCH 2/2] fix: correct bid/ask logic and column identifiers in spread calculation query --- src/routes/spreads.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/routes/spreads.ts b/src/routes/spreads.ts index b1c92d7..f548e57 100644 --- a/src/routes/spreads.ts +++ b/src/routes/spreads.ts @@ -3,9 +3,9 @@ import { pgPool } from '../db'; /** * GET /spreads/:asset - Return current bid, ask, and spread (bps) for the given asset. - * The bid is taken as the highest price among price points where the asset appears. - * The ask is the lowest price among those points. - * Spread basis points = ((ask - bid) / ask) * 10_000. + * The bid is the minimum price among price points where the asset appears. + * The ask is the maximum price among those points. + * Spread basis points = ((ask - bid) / ask) * 10_000 (non‑negative). */ export async function registerSpreadsRoutes(app: FastifyInstance) { app.get('/spreads/:asset', async (req, reply) => { @@ -15,9 +15,9 @@ export async function registerSpreadsRoutes(app: FastifyInstance) { } const result = await pgPool.query( - `SELECT MAX(price::numeric) AS bid, MIN(price::numeric) AS ask - FROM price_points - WHERE assetA = $1 OR assetB = $1`, + `SELECT MIN(price::numeric) AS bid, MAX(price::numeric) AS ask + FROM price_points + WHERE "asset_a" = $1 OR "asset_b" = $1`, [asset] ); const row = result.rows[0];