@silvana-one/orderbook 1.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/grpc.ts ADDED
@@ -0,0 +1,356 @@
1
+ import { createGrpcTransport } from "@connectrpc/connect-node";
2
+ import { createClient, ConnectError } from "@connectrpc/connect";
3
+ import { create } from "@bufbuild/protobuf";
4
+ import { TimestampSchema } from "@bufbuild/protobuf/wkt";
5
+
6
+ // Export all types and schemas from the generated protobuf file
7
+ export * from "./proto/silvana/orderbook/v1/orderbook_pb.js";
8
+
9
+ import {
10
+ OrderbookService,
11
+ type GetOrdersResponse,
12
+ type GetOrderbookDepthResponse,
13
+ type GetSettlementProposalsResponse,
14
+ type GetInstrumentsResponse,
15
+ type GetMarketsResponse,
16
+ type GetOrderHistoryResponse,
17
+ type GetMarketDataResponse,
18
+ type GetSettlementsResponse,
19
+ type SubmitOrderResponse,
20
+ type CancelOrderResponse,
21
+ GetOrdersRequestSchema,
22
+ GetOrderbookDepthRequestSchema,
23
+ GetSettlementProposalsRequestSchema,
24
+ GetInstrumentsRequestSchema,
25
+ GetMarketsRequestSchema,
26
+ GetOrderHistoryRequestSchema,
27
+ GetMarketDataRequestSchema,
28
+ GetSettlementsRequestSchema,
29
+ SubmitOrderRequestSchema,
30
+ CancelOrderRequestSchema,
31
+ SubscribeOrderbookRequestSchema,
32
+ SubscribeOrdersRequestSchema,
33
+ SubscribeSettlementsRequestSchema,
34
+ JWTAuthSchema,
35
+ OrderType,
36
+ OrderStatus,
37
+ TimeInForce,
38
+ MarketType,
39
+ SettlementStatus,
40
+ } from "./proto/silvana/orderbook/v1/orderbook_pb.js";
41
+
42
+ // Re-export commonly used enums for convenience
43
+ export { OrderType, OrderStatus, TimeInForce, MarketType, SettlementStatus };
44
+
45
+ /**
46
+ * Converts a Date to a Timestamp message
47
+ */
48
+ function dateToTimestamp(date: Date) {
49
+ const seconds = BigInt(Math.floor(date.getTime() / 1000));
50
+ const nanos = (date.getTime() % 1000) * 1000000;
51
+ return create(TimestampSchema, { seconds, nanos });
52
+ }
53
+
54
+ /**
55
+ * Custom error class for Orderbook client errors
56
+ */
57
+ export class OrderbookError extends Error {
58
+ constructor(
59
+ message: string,
60
+ public code?: string,
61
+ public details?: any
62
+ ) {
63
+ super(message);
64
+ this.name = 'OrderbookError';
65
+ }
66
+ }
67
+
68
+ /**
69
+ * Orderbook client configuration
70
+ */
71
+ export interface OrderbookClientConfig {
72
+ /** Base URL of the orderbook service (e.g., "http://localhost:50052") */
73
+ baseUrl: string;
74
+ /** JWT token for authentication */
75
+ token: string;
76
+ }
77
+
78
+ /**
79
+ * Orderbook client for interacting with the Silvana Orderbook Service
80
+ */
81
+ export class OrderbookClient {
82
+ private client: ReturnType<typeof createClient<typeof OrderbookService>>;
83
+ private token: string;
84
+
85
+ /**
86
+ * Creates a new OrderbookClient instance
87
+ * @param config Client configuration
88
+ */
89
+ constructor(config: OrderbookClientConfig) {
90
+ const transport = createGrpcTransport({
91
+ baseUrl: config.baseUrl,
92
+ });
93
+
94
+ this.client = createClient(OrderbookService, transport);
95
+ this.token = config.token;
96
+ }
97
+
98
+ /**
99
+ * Creates JWT auth object
100
+ */
101
+ private createAuth() {
102
+ return create(JWTAuthSchema, { token: this.token });
103
+ }
104
+
105
+ /**
106
+ * Wraps async calls with error handling
107
+ */
108
+ private async wrapCall<T>(
109
+ operation: () => Promise<T>,
110
+ operationName: string
111
+ ): Promise<T> {
112
+ try {
113
+ return await operation();
114
+ } catch (error) {
115
+ if (error instanceof ConnectError) {
116
+ throw new OrderbookError(
117
+ `${operationName} failed: ${error.message}`,
118
+ String(error.code),
119
+ error.metadata
120
+ );
121
+ }
122
+ throw new OrderbookError(
123
+ `${operationName} failed: ${error instanceof Error ? error.message : 'Unknown error'}`,
124
+ 'UNKNOWN',
125
+ error
126
+ );
127
+ }
128
+ }
129
+
130
+ /**
131
+ * Get orders for the authenticated user
132
+ */
133
+ async getOrders(params?: {
134
+ marketId?: string;
135
+ status?: OrderStatus;
136
+ orderType?: OrderType;
137
+ limit?: number;
138
+ offset?: number;
139
+ }): Promise<GetOrdersResponse> {
140
+ return await this.wrapCall(async () => {
141
+ const request = create(GetOrdersRequestSchema, {
142
+ auth: this.createAuth(),
143
+ ...params,
144
+ });
145
+ return await this.client.getOrders(request);
146
+ }, 'getOrders');
147
+ }
148
+
149
+ /**
150
+ * Get orderbook depth for a market
151
+ */
152
+ async getOrderbookDepth(params: {
153
+ marketId: string;
154
+ depth?: number;
155
+ }): Promise<GetOrderbookDepthResponse> {
156
+ const request = create(GetOrderbookDepthRequestSchema, {
157
+ auth: this.createAuth(),
158
+ ...params,
159
+ });
160
+ return await this.client.getOrderbookDepth(request);
161
+ }
162
+
163
+ /**
164
+ * Get settlement proposals for the authenticated user
165
+ */
166
+ async getSettlementProposals(params?: {
167
+ marketId?: string;
168
+ status?: SettlementStatus;
169
+ limit?: number;
170
+ offset?: number;
171
+ }): Promise<GetSettlementProposalsResponse> {
172
+ return await this.wrapCall(async () => {
173
+ const request = create(GetSettlementProposalsRequestSchema, {
174
+ auth: this.createAuth(),
175
+ ...params,
176
+ });
177
+ return await this.client.getSettlementProposals(request);
178
+ }, 'getSettlementProposals');
179
+ }
180
+
181
+ /**
182
+ * Get available instruments
183
+ */
184
+ async getInstruments(params: {
185
+ instrumentType?: string;
186
+ limit?: number;
187
+ offset?: number;
188
+ }): Promise<GetInstrumentsResponse> {
189
+ const request = create(GetInstrumentsRequestSchema, {
190
+ auth: this.createAuth(),
191
+ ...params,
192
+ });
193
+ return await this.client.getInstruments(request);
194
+ }
195
+
196
+ /**
197
+ * Get available markets
198
+ */
199
+ async getMarkets(params?: {
200
+ marketType?: MarketType;
201
+ baseInstrument?: string;
202
+ quoteInstrument?: string;
203
+ activeOnly?: boolean;
204
+ limit?: number;
205
+ offset?: number;
206
+ }): Promise<GetMarketsResponse> {
207
+ return await this.wrapCall(async () => {
208
+ const request = create(GetMarketsRequestSchema, {
209
+ auth: this.createAuth(),
210
+ ...params,
211
+ });
212
+ return await this.client.getMarkets(request);
213
+ }, 'getMarkets');
214
+ }
215
+
216
+ /**
217
+ * Get order history for the authenticated user
218
+ */
219
+ async getOrderHistory(params: {
220
+ marketId?: string;
221
+ fromTime?: Date;
222
+ toTime?: Date;
223
+ limit?: number;
224
+ offset?: number;
225
+ }): Promise<GetOrderHistoryResponse> {
226
+ const request = create(GetOrderHistoryRequestSchema, {
227
+ auth: this.createAuth(),
228
+ marketId: params.marketId,
229
+ fromTime: params.fromTime ? dateToTimestamp(params.fromTime) : undefined,
230
+ toTime: params.toTime ? dateToTimestamp(params.toTime) : undefined,
231
+ limit: params.limit,
232
+ offset: params.offset,
233
+ });
234
+ return await this.client.getOrderHistory(request);
235
+ }
236
+
237
+ /**
238
+ * Get market data
239
+ */
240
+ async getMarketData(params: {
241
+ marketIds?: string[];
242
+ }): Promise<GetMarketDataResponse> {
243
+ const request = create(GetMarketDataRequestSchema, {
244
+ auth: this.createAuth(),
245
+ ...params,
246
+ });
247
+ return await this.client.getMarketData(request);
248
+ }
249
+
250
+ /**
251
+ * Get settlements (completed) for the authenticated user
252
+ */
253
+ async getSettlements(params: {
254
+ marketId?: string;
255
+ fromTime?: Date;
256
+ toTime?: Date;
257
+ limit?: number;
258
+ offset?: number;
259
+ }): Promise<GetSettlementsResponse> {
260
+ const request = create(GetSettlementsRequestSchema, {
261
+ auth: this.createAuth(),
262
+ marketId: params.marketId,
263
+ fromTime: params.fromTime ? dateToTimestamp(params.fromTime) : undefined,
264
+ toTime: params.toTime ? dateToTimestamp(params.toTime) : undefined,
265
+ limit: params.limit,
266
+ offset: params.offset,
267
+ });
268
+ return await this.client.getSettlements(request);
269
+ }
270
+
271
+ /**
272
+ * Submit a new order
273
+ */
274
+ async submitOrder(params: {
275
+ marketId: string;
276
+ orderType: OrderType;
277
+ price: string;
278
+ quantity: string;
279
+ timeInForce: TimeInForce;
280
+ expiresAt?: Date;
281
+ traderOrderRef?: string;
282
+ credentials?: any;
283
+ requirements?: any;
284
+ metadata?: any;
285
+ }): Promise<SubmitOrderResponse> {
286
+ return await this.wrapCall(async () => {
287
+ const request = create(SubmitOrderRequestSchema, {
288
+ auth: this.createAuth(),
289
+ marketId: params.marketId,
290
+ orderType: params.orderType,
291
+ price: params.price,
292
+ quantity: params.quantity,
293
+ timeInForce: params.timeInForce,
294
+ expiresAt: params.expiresAt ? dateToTimestamp(params.expiresAt) : undefined,
295
+ traderOrderRef: params.traderOrderRef,
296
+ credentials: params.credentials,
297
+ requirements: params.requirements,
298
+ metadata: params.metadata,
299
+ });
300
+ return await this.client.submitOrder(request);
301
+ }, 'submitOrder');
302
+ }
303
+
304
+ /**
305
+ * Cancel an existing order
306
+ */
307
+ async cancelOrder(params: {
308
+ orderId: bigint;
309
+ }): Promise<CancelOrderResponse> {
310
+ const request = create(CancelOrderRequestSchema, {
311
+ auth: this.createAuth(),
312
+ ...params,
313
+ });
314
+ return await this.client.cancelOrder(request);
315
+ }
316
+
317
+ /**
318
+ * Subscribe to orderbook updates (streaming)
319
+ */
320
+ subscribeOrderbook(params: {
321
+ marketId: string;
322
+ depth?: number;
323
+ }) {
324
+ const request = create(SubscribeOrderbookRequestSchema, {
325
+ auth: this.createAuth(),
326
+ ...params,
327
+ });
328
+ return this.client.subscribeOrderbook(request);
329
+ }
330
+
331
+ /**
332
+ * Subscribe to order updates (streaming)
333
+ */
334
+ subscribeOrders(params: {
335
+ marketId?: string;
336
+ }) {
337
+ const request = create(SubscribeOrdersRequestSchema, {
338
+ auth: this.createAuth(),
339
+ ...params,
340
+ });
341
+ return this.client.subscribeOrders(request);
342
+ }
343
+
344
+ /**
345
+ * Subscribe to settlement updates (streaming)
346
+ */
347
+ subscribeSettlements(params: {
348
+ marketId?: string;
349
+ }) {
350
+ const request = create(SubscribeSettlementsRequestSchema, {
351
+ auth: this.createAuth(),
352
+ ...params,
353
+ });
354
+ return this.client.subscribeSettlements(request);
355
+ }
356
+ }
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from "./grpc.js";