Coding the API Server
Course: Building an Exchange - Part 1 - chapter 9
- Initialize the project
mkdir orderbook-servercd orderbook-servernpm init -ynpx tsc --initnpm install express redis uuid zodnpm install typescript ts-node @types/express @types/node @types/redis @types/uuid- Update
rootDirandoutDir - Create
src/orderbook.ts
interface Order { price: number; quantity: number; orderId: string;}
interface Bid extends Order { side: 'bid';}
interface Ask extends Order { side: 'ask';}
interface Orderbook { bids: Bid[]; asks: Ask[];}
export const orderbook: Orderbook = { bids: [
], asks: [
]}
export const bookWithQuantity: {bids: {[price: number]: number}; asks: {[price: number]: number}} = { bids: {}, asks: {}}- Create
src/types.ts
import { z } from "zod";
export const OrderInputSchema = z.object({ baseAsset: z.string(), quoteAsset: z.string(), price: z.number(), quantity: z.number(), side: z.enum(['buy', 'sell']), type: z.enum(['limit', 'market']), kind: z.enum(['ioc']).optional(),});- Create
src/index.ts
$1b