forked from pmxt-dev/pmxt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetchTrades.ts
More file actions
34 lines (32 loc) · 1019 Bytes
/
fetchTrades.ts
File metadata and controls
34 lines (32 loc) · 1019 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import axios from "axios";
import { HistoryFilterParams, TradesParams } from "../../BaseExchange";
import { Trade } from "../../types";
import { kalshiErrorMapper } from "./errors";
import { getMarketsUrl } from "./config";
import { fromKalshiCents } from "./price";
export async function fetchTrades(
baseUrl: string,
id: string,
params: TradesParams | HistoryFilterParams,
): Promise<Trade[]> {
try {
const ticker = id.replace(/-NO$/, "");
const url = getMarketsUrl(baseUrl, undefined, ["trades"]);
const response = await axios.get(url, {
params: {
ticker: ticker,
limit: params.limit || 100,
},
});
const trades = response.data.trades || [];
return trades.map((t: any) => ({
id: t.trade_id,
timestamp: new Date(t.created_time).getTime(),
price: fromKalshiCents(t.yes_price),
amount: t.count,
side: t.taker_side === "yes" ? "buy" : "sell",
}));
} catch (error: any) {
throw kalshiErrorMapper.mapError(error);
}
}