Overview
Exchange Web uses strongly-typed TypeScript interfaces for all API requests and responses. All type definitions can be found in source/apps/web/src/utils/types.ts.
Market Data Types
KLine
Represents a candlestick (OHLCV) data point for a specific time interval.
interface KLine {
close: string;
end: string;
high: string;
low: string;
open: string;
quoteVolume: string;
start: string;
trades: string;
volume: string;
}
Opening price at the start of the period
Highest price reached during the period
Lowest price reached during the period
Closing price at the end of the period
Total volume of base asset traded during the period
Total volume of quote asset traded (price × volume)
Start timestamp of the candlestick period
End timestamp of the candlestick period
Number of individual trades that occurred during this period
Usage Example:
import { getKlines } from './utils/requests';
import { KLine } from './utils/types';
const klines: KLine[] = await getKlines('SOL_USDC', '1h', Date.now() - 86400000);
klines.forEach((kline) => {
console.log(`Period: ${kline.start} - ${kline.end}`);
console.log(`OHLC: ${kline.open}, ${kline.high}, ${kline.low}, ${kline.close}`);
console.log(`Volume: ${kline.volume}`);
});
Trade
Represents a single trade execution on the exchange.
interface Trade {
id: number;
isBuyerMaker: boolean;
price: string;
quantity: string;
quoteQuantity: string;
timestamp: number;
}
Unique identifier for the trade
true if the buyer placed the maker order (limit order that was on the book first). false if the buyer was the taker (market order or aggressive limit order).
Execution price of the trade
Amount of base asset traded
Amount of quote asset traded (price × quantity)
Unix timestamp in milliseconds when the trade was executed
Usage Example:
import { getTrades } from './utils/requests';
import { Trade } from './utils/types';
const trades: Trade[] = await getTrades('SOL_USDC');
const recentTrade: Trade = trades[0];
console.log(`Trade #${recentTrade.id}`);
console.log(`Price: $${recentTrade.price}`);
console.log(`Amount: ${recentTrade.quantity} SOL`);
console.log(`Side: ${recentTrade.isBuyerMaker ? 'Sell' : 'Buy'}`);
console.log(`Time: ${new Date(recentTrade.timestamp).toLocaleString()}`);
When isBuyerMaker is true, the trade appears as a “sell” on the tape because the buyer matched against a sell order. When false, it appears as a “buy”.
Depth
Represents the current order book depth for a market.
interface Depth {
bids: [string, string][];
asks: [string, string][];
lastUpdateId: string;
}
Array of bid (buy) orders. Each element is a tuple of [price, quantity].
Array of ask (sell) orders. Each element is a tuple of [price, quantity].
Identifier for the last order book update. Used for synchronization with WebSocket depth updates.
Usage Example:
import { getDepth } from './utils/requests';
import { Depth } from './utils/types';
const depth: Depth = await getDepth('SOL_USDC');
// Best bid (highest buy price)
const bestBid = depth.bids[0];
console.log(`Best Bid: $${bestBid[0]} (${bestBid[1]} SOL)`);
// Best ask (lowest sell price)
const bestAsk = depth.asks[0];
console.log(`Best Ask: $${bestAsk[0]} (${bestAsk[1]} SOL)`);
// Spread
const spread = parseFloat(bestAsk[0]) - parseFloat(bestBid[0]);
console.log(`Spread: $${spread}`);
// Total bid volume
const totalBidVolume = depth.bids.reduce(
(sum, [_, quantity]) => sum + parseFloat(quantity),
0
);
console.log(`Total Bid Volume: ${totalBidVolume} SOL`);
Bids are typically sorted in descending order (highest price first), while asks are sorted in ascending order (lowest price first).
Ticker
Represents 24-hour statistics for a market.
interface Ticker {
firstPrice: string;
high: string;
lastPrice: string;
low: string;
priceChange: string;
priceChangePercent: string;
quoteVolume: string;
symbol: string;
trades: string;
volume: string;
}
Market symbol (e.g., SOL_USDC, SOL_USDC)
Price 24 hours ago (start of the rolling 24-hour window)
Highest price in the last 24 hours
Lowest price in the last 24 hours
Absolute price change: lastPrice - firstPrice
Percentage price change: ((lastPrice - firstPrice) / firstPrice) * 100
Total volume of base asset traded in 24 hours
Total volume of quote asset traded in 24 hours
Number of trades executed in the last 24 hours
Usage Example:
import { getTicker } from './utils/requests';
import { Ticker } from './utils/types';
const ticker: Ticker = await getTicker('SOL_USDC');
console.log(`${ticker.symbol}`);
console.log(`Price: $${ticker.lastPrice}`);
console.log(`24h Change: ${ticker.priceChange} (${ticker.priceChangePercent}%)`);
console.log(`24h High: $${ticker.high}`);
console.log(`24h Low: $${ticker.low}`);
console.log(`24h Volume: ${ticker.volume} SOL`);
console.log(`24h Trades: ${ticker.trades}`);
// Calculate if market is up or down
const isUp = parseFloat(ticker.priceChange) > 0;
console.log(`Trend: ${isUp ? '📈 Up' : '📉 Down'}`);
Trading Types
CreateOrder
Request payload for creating a new order.
interface CreateOrder {
market: string;
side: string;
quantity: number;
price: number;
userId: string;
}
Market symbol to place the order on (e.g., SOL_USDC)
Order side: "buy" or "sell"
Amount of base asset to buy or sell
Limit price for the order
User identifier for authentication (obtained from createUser())
Usage Example:
import { createOrder, createUser } from './utils/requests';
import { CreateOrder, UserId } from './utils/types';
// First, create a user if you don't have one
const userResponse: UserId = await createUser();
const userId = userResponse.user_id;
// Create a buy order
const buyOrder: CreateOrder = {
market: 'SOL_USDC',
side: 'buy',
quantity: 0.5,
price: 50000,
userId: userId
};
const orderId: string = await createOrder(buyOrder);
console.log(`Order created: ${orderId}`);
// Create a sell order
const sellOrder: CreateOrder = {
market: 'SOL_USDC',
side: 'sell',
quantity: 0.25,
price: 51000,
userId: userId
};
const sellOrderId: string = await createOrder(sellOrder);
console.log(`Sell order created: ${sellOrderId}`);
All orders are limit orders. You must specify both quantity and price.
User Management Types
UserId
Response from user creation endpoint.
interface UserId {
status: string;
user_id: string;
}
Status of the user creation request (e.g., "success")
Unique identifier assigned to the newly created user
Usage Example:
import { createUser } from './utils/requests';
import { UserId } from './utils/types';
const userResponse: UserId = await createUser();
if (userResponse.status === 'success') {
// Store the user_id for future use
localStorage.setItem('userId', userResponse.user_id);
console.log(`User created: ${userResponse.user_id}`);
} else {
console.error('Failed to create user');
}
// Retrieve stored user_id for orders
const storedUserId = localStorage.getItem('userId');
if (storedUserId) {
// Use in order creation
const order = {
market: 'SOL_USDC',
side: 'buy',
quantity: 0.1,
price: 50000,
userId: storedUserId
};
}
Utility Types
Stat
Generic label-value pair for displaying statistics.
interface Stat {
label: string;
value: string;
}
Display label for the statistic
Usage Example:
import { getTicker } from './utils/requests';
import { Stat, Ticker } from './utils/types';
const ticker: Ticker = await getTicker('SOL_USDC');
// Convert ticker to display stats
const stats: Stat[] = [
{ label: 'Last Price', value: `$${ticker.lastPrice}` },
{ label: '24h Change', value: `${ticker.priceChangePercent}%` },
{ label: '24h High', value: `$${ticker.high}` },
{ label: '24h Low', value: `$${ticker.low}` },
{ label: '24h Volume', value: `${ticker.volume} SOL` },
{ label: 'Trades', value: ticker.trades }
];
// Render stats in UI
stats.forEach((stat) => {
console.log(`${stat.label}: ${stat.value}`);
});
Type Import Examples
All types can be imported from the types module:
import {
KLine,
Trade,
Depth,
Ticker,
CreateOrder,
UserId,
Stat
} from './utils/types';
Full typing example:
import { getDepth, getTrades, getKlines, createOrder } from './utils/requests';
import { Depth, Trade, KLine, CreateOrder } from './utils/types';
async function tradingExample() {
// Strongly typed API calls
const depth: Depth = await getDepth('SOL_USDC');
const trades: Trade[] = await getTrades('SOL_USDC');
const klines: KLine[] = await getKlines('SOL_USDC', '1h', Date.now() - 86400000);
// Type-safe order creation
const order: CreateOrder = {
market: 'SOL_USDC',
side: 'buy',
quantity: 0.1,
price: 50000,
userId: 'user-123'
};
const orderId: string = await createOrder(order);
}
Notes on Type Safety
String Numbers: Most numeric values are represented as strings to preserve precision. Parse them with parseFloat() or Number() when performing calculations.
Timestamps: Trade timestamps are Unix milliseconds (number). KLine start/end times are strings. Use new Date() for conversion.
Array Tuples: The Depth interface uses tuple types [string, string][] for bids and asks. First element is price, second is quantity.
Type Reference Location
All type definitions are located at:
source/apps/web/src/utils/types.ts:1-58