Skip to content

Commit 29a2ac6

Browse files
committed
fix(integrations): keep every legacy kalshi order key on v1 write outputs
1 parent 8fff4d4 commit 29a2ac6

7 files changed

Lines changed: 101 additions & 18 deletions

File tree

‎apps/sim/tools/kalshi/amend_order.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,23 @@ describe('Kalshi amend order on the V2 event-order endpoint', () => {
126126
expect(request.body).toMatchObject({ side: 'ask', price: '0.5700', count: '8.00' })
127127
})
128128

129+
it('keeps every legacy order key on the v1 amend output', async () => {
130+
const params = { ...BASE, count: '8', yesPrice: '57' }
131+
const result = await kalshiAmendOrderTool.transformResponse?.(
132+
Response.json(AMEND_V2_RESPONSE_EXAMPLE),
133+
params
134+
)
135+
expect(result?.output.order).toMatchObject({
136+
order_id: ORDER_ID,
137+
count: 8,
138+
remaining_count: 8,
139+
event_ticker: null,
140+
status: null,
141+
created_time: null,
142+
place_count: null,
143+
})
144+
})
145+
129146
it.each([
130147
['kalshi_amend_order', kalshiAmendOrderTool],
131148
['kalshi_amend_order_v2', kalshiAmendOrderV2Tool],

‎apps/sim/tools/kalshi/amend_order.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
kalshiAmendNeedsCurrentOrder,
77
kalshiAmendParamsFromOrder,
88
kalshiCountNumber,
9+
kalshiLegacyOrder,
910
kalshiOrderPath,
1011
kalshiOrderReadPath,
1112
kalshiWrittenOrder,
@@ -211,7 +212,7 @@ export const kalshiAmendOrderTool: ToolConfig<KalshiAmendOrderParams, KalshiAmen
211212
return {
212213
success: true,
213214
output: {
214-
order: amendedOrder(data, params),
215+
order: kalshiLegacyOrder(amendedOrder(data, params)),
215216
},
216217
}
217218
},

‎apps/sim/tools/kalshi/cancel_order.test.ts‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,15 @@ describe('Kalshi cancel order routes to the order shard by ticker', () => {
139139
Response.json(CANCEL_V2_EXAMPLE),
140140
params
141141
)
142-
expect(v1?.output).toEqual({
143-
order: {
144-
order_id: ORDER_ID,
145-
client_order_id: CANCEL_V2_EXAMPLE.client_order_id,
146-
ticker: TICKER,
147-
},
148-
reducedBy: 10,
142+
expect(v1?.output.reducedBy).toBe(10)
143+
expect(v1?.output.order).toMatchObject({
144+
order_id: ORDER_ID,
145+
client_order_id: CANCEL_V2_EXAMPLE.client_order_id,
146+
ticker: TICKER,
147+
// Keys the legacy order object had stay present, as null, so saved references resolve.
148+
event_ticker: null,
149+
status: null,
150+
count: null,
149151
})
150152

151153
const v2 = await kalshiCancelOrderV2Tool.transformResponse?.(

‎apps/sim/tools/kalshi/cancel_order.ts‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { getErrorMessage } from '@sim/utils/errors'
22
import { toRecord } from '@sim/utils/object'
3-
import { kalshiCountNumber, kalshiOrderPath, kalshiOrderReadPath } from '@/tools/kalshi/orders'
3+
import {
4+
kalshiCountNumber,
5+
kalshiLegacyOrder,
6+
kalshiOrderPath,
7+
kalshiOrderReadPath,
8+
} from '@/tools/kalshi/orders'
49
import type { KalshiAuthParams } from '@/tools/kalshi/types'
510
import { buildKalshiAuthHeaders, buildKalshiUrl, handleKalshiError } from '@/tools/kalshi/types'
611
import { KALSHI_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials'
@@ -80,7 +85,7 @@ export interface KalshiCancelOrderResponse {
8085
success: boolean
8186
error?: string
8287
output: {
83-
order: { order_id: string | null; client_order_id: string | null; ticker: string | null }
88+
order: Record<string, unknown>
8489
reducedBy: number
8590
}
8691
}
@@ -131,19 +136,19 @@ export const kalshiCancelOrderTool: ToolConfig<KalshiCancelOrderParams, KalshiCa
131136
const data = await response.json()
132137

133138
if (!response.ok) {
134-
handleKalshiError(data, 'cancel_order')
139+
handleKalshiError(data, params && cancelNeedsTicker(params) ? 'order read' : 'cancel_order')
135140
}
136141

137142
// The order read that precedes a cancel with no ticker: postProcess cancels with its ticker.
138143
if (params && cancelNeedsTicker(params)) {
139144
return {
140145
success: true,
141146
output: {
142-
order: {
147+
order: kalshiLegacyOrder({
143148
order_id: params.orderId,
144149
client_order_id: null,
145150
ticker: readOrderTicker(data),
146-
},
151+
}),
147152
reducedBy: 0,
148153
},
149154
}
@@ -153,11 +158,11 @@ export const kalshiCancelOrderTool: ToolConfig<KalshiCancelOrderParams, KalshiCa
153158
return {
154159
success: true,
155160
output: {
156-
order: {
161+
order: kalshiLegacyOrder({
157162
order_id: data.order_id ?? null,
158163
client_order_id: data.client_order_id ?? null,
159164
ticker: params?.ticker?.trim() || null,
160-
},
165+
}),
161166
reducedBy: kalshiCountNumber(data.reduced_by) ?? 0,
162167
},
163168
}
@@ -283,7 +288,10 @@ export const kalshiCancelOrderV2Tool: ToolConfig<
283288
const data = await response.json()
284289

285290
if (!response.ok) {
286-
handleKalshiError(data, 'cancel_order_v2')
291+
handleKalshiError(
292+
data,
293+
params && cancelNeedsTicker(params) ? 'order read' : 'cancel_order_v2'
294+
)
287295
}
288296

289297
// The order read that precedes a cancel with no ticker: postProcess cancels with its ticker.

‎apps/sim/tools/kalshi/create_order.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,24 @@ describe('Kalshi create order on the V2 event-order endpoint', () => {
128128
).toBe(true)
129129
})
130130

131+
it('keeps every legacy order key on the v1 create output', async () => {
132+
const result = await kalshiCreateOrderTool.transformResponse?.(
133+
Response.json(CREATE_V2_RESPONSE_EXAMPLE, { status: 201 }),
134+
{ ...BASE, count: '10', yesPrice: '56' }
135+
)
136+
expect(result?.output.order).toMatchObject({
137+
order_id: CREATE_V2_RESPONSE_EXAMPLE.order_id,
138+
ticker: 'HIGHNY-24JAN01-T60',
139+
count: 10,
140+
remaining_count: 10,
141+
event_ticker: null,
142+
status: null,
143+
yes_price: null,
144+
expiration_time: null,
145+
taker_fees: null,
146+
})
147+
})
148+
131149
it('maps the CreateOrderV2Response example onto the order outputs', async () => {
132150
const params = { ...BASE, side: 'no', action: 'buy', noPrice: '44' }
133151
const result = await kalshiCreateOrderV2Tool.transformResponse?.(

‎apps/sim/tools/kalshi/create_order.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
buildKalshiCreateOrderBody,
33
kalshiCountNumber,
4+
kalshiLegacyOrder,
45
kalshiOrderPath,
56
kalshiWrittenOrder,
67
} from '@/tools/kalshi/orders'
@@ -32,7 +33,7 @@ export interface KalshiCreateOrderParams extends KalshiAuthParams {
3233
export interface KalshiCreateOrderResponse {
3334
success: boolean
3435
output: {
35-
order: KalshiCreatedOrder
36+
order: Record<string, unknown>
3637
}
3738
}
3839

@@ -208,7 +209,10 @@ export const kalshiCreateOrderTool: ToolConfig<KalshiCreateOrderParams, KalshiCr
208209
return {
209210
success: true,
210211
output: {
211-
order: createdOrder(data, params),
212+
order: kalshiLegacyOrder({
213+
...createdOrder(data, params),
214+
count: kalshiCountNumber(buildKalshiCreateOrderBody(params).count),
215+
}),
212216
},
213217
}
214218
},

‎apps/sim/tools/kalshi/orders.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,3 +327,36 @@ export function kalshiWrittenOrder(
327327
typeof body.cancel_order_on_pause === 'boolean' ? body.cancel_order_on_pause : null,
328328
}
329329
}
330+
331+
/** Every key of the order object the legacy write endpoints returned (`KalshiOrder`). */
332+
const LEGACY_ORDER_KEYS = [
333+
'order_id',
334+
'ticker',
335+
'event_ticker',
336+
'status',
337+
'side',
338+
'type',
339+
'yes_price',
340+
'no_price',
341+
'action',
342+
'count',
343+
'remaining_count',
344+
'created_time',
345+
'expiration_time',
346+
'place_count',
347+
'decrease_count',
348+
'maker_fill_count',
349+
'taker_fill_count',
350+
'taker_fees',
351+
] as const
352+
353+
/**
354+
* The legacy `order` output for a V2 write: every key the removed endpoints returned stays present,
355+
* as `null` when Kalshi's V2 response no longer reports it, so a saved reference resolves to null
356+
* rather than disappearing.
357+
*/
358+
export function kalshiLegacyOrder(order: Record<string, unknown>): Record<string, unknown> {
359+
const legacy: Record<string, unknown> = {}
360+
for (const key of LEGACY_ORDER_KEYS) legacy[key] = null
361+
return { ...legacy, ...order }
362+
}

0 commit comments

Comments
 (0)