Skip to content

Commit cdb01ca

Browse files
committed
refactor: tag token IDs in bindings
Use a shared TokenId schema across binding models so CTF position identifiers stay distinct from generic strings as the SDK surface grows.
1 parent ffea1ad commit cdb01ca

5 files changed

Lines changed: 38 additions & 20 deletions

File tree

packages/bindings/src/clob/market-data.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { z } from 'zod';
2+
import { TokenIdSchema } from '../shared';
23
import { OrderSideSchema } from './order';
34

45
export enum PriceHistoryInterval {
@@ -24,10 +25,7 @@ export const PriceSchema = z.looseObject({
2425
});
2526
export type Price = z.infer<typeof PriceSchema>;
2627

27-
const PricesBySideSchema = z.object({
28-
BUY: z.string().optional(),
29-
SELL: z.string().optional(),
30-
});
28+
const PricesBySideSchema = z.record(OrderSideSchema, z.string().optional());
3129
export type PricesBySide = z.infer<typeof PricesBySideSchema>;
3230

3331
export const PricesSchema = z.record(z.string(), PricesBySideSchema);
@@ -47,15 +45,23 @@ export const LastTradePriceSchema = z.looseObject({
4745
});
4846
export type LastTradePrice = z.infer<typeof LastTradePriceSchema>;
4947

50-
const LastTradePriceForTokenResponseSchema = LastTradePriceSchema.extend({
51-
token_id: z.string(),
52-
});
48+
const LastTradePriceForTokenResponseSchema = z
49+
.object({
50+
price: z.string(),
51+
side: OrderSideSchema,
52+
token_id: TokenIdSchema,
53+
})
54+
.transform(({ token_id, price, side }) => ({
55+
tokenId: token_id,
56+
price,
57+
side,
58+
}));
5359
export type LastTradePriceForTokenResponse = z.infer<
5460
typeof LastTradePriceForTokenResponseSchema
5561
>;
5662

5763
const LastTradePriceForTokenSchema = z.object({
58-
tokenId: z.string(),
64+
tokenId: TokenIdSchema,
5965
price: z.string(),
6066
side: OrderSideSchema,
6167
});

packages/bindings/src/gamma/comment.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { z } from 'zod';
2+
import { TokenIdSchema } from '../shared';
23
import { ImageOptimizationSchema } from './common';
34

45
export const CommentPositionSchema = z.looseObject({
5-
tokenId: z.string().nullish(),
6+
tokenId: TokenIdSchema.nullish(),
67
positionSize: z.string().nullish(),
78
});
89

packages/bindings/src/gamma/market.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { z } from 'zod';
2-
import { MarketIdSchema, TickSizeValueSchema } from '../shared';
2+
import { MarketIdSchema, TickSizeValueSchema, TokenIdSchema } from '../shared';
33
import {
44
CategoryReferenceSchema,
55
ClobRewardsSchema,
@@ -11,11 +11,21 @@ import {
1111
TagReferenceSchema,
1212
} from './common';
1313

14-
const TwoStringTupleSchema = z
14+
const OutcomePairSchema = z
1515
.string()
1616
.transform((val) => JSON.parse(val))
1717
.pipe(z.tuple([z.string(), z.string()]));
1818

19+
const OutcomePricePairSchema = z
20+
.string()
21+
.transform((val) => JSON.parse(val))
22+
.pipe(z.tuple([z.string(), z.string()]));
23+
24+
const TokenIdPairSchema = z
25+
.string()
26+
.transform((val) => JSON.parse(val))
27+
.pipe(z.tuple([TokenIdSchema, TokenIdSchema]));
28+
1929
export const MarketSchema = z.looseObject({
2030
id: MarketIdSchema,
2131
question: z.string().nullish(),
@@ -39,8 +49,8 @@ export const MarketSchema = z.looseObject({
3949
lowerBound: z.string().nullish(),
4050
upperBound: z.string().nullish(),
4151
description: z.string().nullish(),
42-
outcomes: TwoStringTupleSchema.nullish(),
43-
outcomePrices: TwoStringTupleSchema.nullish(),
52+
outcomes: OutcomePairSchema.nullish(),
53+
outcomePrices: OutcomePricePairSchema.nullish(),
4454
volume: z.string().nullish(),
4555
active: z.boolean().nullish(),
4656
marketType: z.string().nullish(),
@@ -92,7 +102,7 @@ export const MarketSchema = z.looseObject({
92102
volume1yr: z.number().nullish(),
93103
gameStartTime: z.string().nullish(),
94104
secondsDelay: z.number().int().nullish(),
95-
clobTokenIds: TwoStringTupleSchema.nullish(),
105+
clobTokenIds: TokenIdPairSchema.nullish(),
96106
disqusThread: z.string().nullish(),
97107
shortOutcomes: z.string().nullish(),
98108
teamAID: z.string().nullish(),

packages/bindings/src/shared.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export type SportId = Tagged<number, 'SportId'>;
4343
export type TagId = Tagged<string, 'TagId'>;
4444
export type TeamId = Tagged<number, 'TeamId'>;
4545
export type TemplateId = Tagged<string, 'TemplateId'>;
46+
export type TokenId = Tagged<string, 'TokenId'>;
4647

4748
export function toBestLineId(value: string): BestLineId {
4849
return toTaggedString<BestLineId>(value);
@@ -126,6 +127,10 @@ export function toTemplateId(value: string): TemplateId {
126127
return toTaggedString<TemplateId>(value);
127128
}
128129

130+
export function toTokenId(value: string): TokenId {
131+
return toTaggedString<TokenId>(value);
132+
}
133+
129134
export const CategoryIdSchema = z.string().transform(toCategoryId);
130135
export const ApiKeySchema = z.string().transform(toApiKey);
131136
export const ClobRewardIdSchema = z.string().transform(toClobRewardId);
@@ -153,6 +158,7 @@ export const NotificationIdSchema = z
153158
.int()
154159
.transform(toNotificationId);
155160
export const TagIdSchema = z.string().transform(toTagId);
161+
export const TokenIdSchema = z.string().transform(toTokenId);
156162
export const TxHashSchema = z.string().transform(toTxHash);
157163

158164
export type ISODateString = z.output<typeof ISODateStringSchema>;

packages/client/src/actions/clob.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -600,18 +600,13 @@ export async function fetchLastTradePrices(
600600
): Promise<LastTradePriceForToken[]> {
601601
const params = parseUserInput(request, FetchLastTradePricesRequestSchema);
602602

603-
const response = await unwrap(
603+
return unwrap(
604604
client.clob
605605
.post('last-trades-prices', {
606606
json: toTokenRequestPayload(params),
607607
})
608608
.andThen(validateWith(LastTradePricesSchema)),
609609
);
610-
611-
return response.map(({ token_id, ...trade }) => ({
612-
...trade,
613-
tokenId: token_id,
614-
}));
615610
}
616611

617612
const ListPriceHistoryRequestSchema = z.object({

0 commit comments

Comments
 (0)