diff --git a/lib/kalshi/rest.js b/lib/kalshi/rest.js new file mode 100644 index 0000000..4dbec36 --- /dev/null +++ b/lib/kalshi/rest.js @@ -0,0 +1,65 @@ +import { signRequest, KALSHI_API_BASE } from './auth.js'; + +async function kalshiFetch(method, path, body = null) { + const headers = signRequest(method, path); + const opts = { method, headers }; + if (body) opts.body = JSON.stringify(body); + + const res = await fetch(`${KALSHI_API_BASE}${path}`, opts); + if (!res.ok) { + const text = await res.text(); + throw new Error(`Kalshi API ${method} ${path} → ${res.status}: ${text}`); + } + return res.json(); +} + +/** + * Get events for the BTC 15-min series. + * Returns the currently active event + its markets. + */ +export async function getActiveBTCEvent() { + const data = await kalshiFetch('GET', '/trade-api/v2/events?series_ticker=KXBTC15M&status=open&limit=1'); + const event = data.events?.[0]; + if (!event) return null; + return event; +} + +/** + * Get markets for a specific event ticker. + */ +export async function getEventMarkets(eventTicker) { + const data = await kalshiFetch('GET', `/trade-api/v2/events/${eventTicker}`); + return data.event?.markets || []; +} + +/** + * Get orderbook for a specific market ticker. + */ +export async function getOrderbook(ticker) { + const data = await kalshiFetch('GET', `/trade-api/v2/markets/${ticker}/orderbook`); + return data.orderbook || data; +} + +/** + * Get single market details. + */ +export async function getMarket(ticker) { + const data = await kalshiFetch('GET', `/trade-api/v2/markets/${ticker}`); + return data.market || data; +} + +/** + * Place a real order on Kalshi. NOT used in paper mode. + */ +export async function placeOrder(params) { + return kalshiFetch('POST', '/trade-api/v2/portfolio/orders', params); +} + +/** + * Get wallet balance. + */ +export async function getBalance() { + return kalshiFetch('GET', '/trade-api/v2/portfolio/balance'); +} + +export { kalshiFetch };