Skip to content

Coding the API Server

Course: Building an Exchange - Part 1 - chapter 9

  • Initialize the project
mkdir orderbook-server
cd orderbook-server
npm init -y
npx tsc --init
npm install express redis uuid zod
npm install typescript ts-node @types/express @types/node @types/redis @types/uuid
  • Update rootDir and outDir
  • 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