@usherlabs/cex-broker 0.2.29 → 0.2.31

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.
@@ -330710,6 +330710,7 @@ var CreateOrderPayloadSchema = exports_external.object({
330710
330710
  toToken: exports_external.string().min(1),
330711
330711
  price: exports_external.coerce.number().positive(),
330712
330712
  marketType: marketTypeSchema,
330713
+ clientOrderId: exports_external.string().min(1).optional(),
330713
330714
  params: exports_external.preprocess(parseJsonString, stringNumberRecordSchema).default({})
330714
330715
  });
330715
330716
  var GetPerpConfigStatePayloadSchema = exports_external.object({
@@ -331481,7 +331482,14 @@ async function handleCreateOrder(ctx) {
331481
331482
  const orderValue = parsePayloadForAction(ctx, CreateOrderPayloadSchema);
331482
331483
  if (orderValue === null)
331483
331484
  return;
331485
+ const createOrderParams = {
331486
+ ...orderValue.params,
331487
+ ...orderValue.clientOrderId !== undefined && {
331488
+ clientOrderId: orderValue.clientOrderId
331489
+ }
331490
+ };
331484
331491
  let resolvedOrderTelemetry = {};
331492
+ let marketMetadataHash;
331485
331493
  try {
331486
331494
  if (!broker) {
331487
331495
  return ctx.wrappedCallback({
@@ -331504,9 +331512,9 @@ async function handleCreateOrder(ctx) {
331504
331512
  if (selectedBrokerAccount?.label) {
331505
331513
  orderActivityTracker?.record(cex3, selectedBrokerAccount.label, resolution.symbol);
331506
331514
  }
331507
- const telemetryIds = extractOrderTelemetryIds(orderValue.params);
331515
+ const telemetryIds = extractOrderTelemetryIds(createOrderParams);
331508
331516
  const submissionTimestamp = new Date().toISOString();
331509
- const marketMetadataHash = await captureMarketMetadataSnapshot(brokerArchiver, broker, {
331517
+ marketMetadataHash = await captureMarketMetadataSnapshot(brokerArchiver, broker, {
331510
331518
  exchange: cex3,
331511
331519
  accountSelector: selectedBrokerAccount?.label,
331512
331520
  symbol: resolution.symbol,
@@ -331514,7 +331522,7 @@ async function handleCreateOrder(ctx) {
331514
331522
  brokerObservedTimestamp: submissionTimestamp,
331515
331523
  ...telemetryIds
331516
331524
  });
331517
- const order = await broker.createOrder(resolution.symbol, orderValue.orderType, resolution.side, resolution.amountBase ?? orderValue.amount, orderValue.price, orderValue.params ?? {});
331525
+ const order = await broker.createOrder(resolution.symbol, orderValue.orderType, resolution.side, resolution.amountBase ?? orderValue.amount, orderValue.price, createOrderParams);
331518
331526
  const createOrderContext = {
331519
331527
  action: "CreateOrder",
331520
331528
  cex: cex3,
@@ -331542,10 +331550,10 @@ async function handleCreateOrder(ctx) {
331542
331550
  orderType: orderValue.orderType,
331543
331551
  requestedQuantity: resolvedOrderTelemetry.requestedQuantity ?? orderValue.amount,
331544
331552
  requestedNotional: orderValue.amount * orderValue.price,
331545
- ...extractOrderTelemetryIds(orderValue.params)
331553
+ ...extractOrderTelemetryIds(createOrderParams)
331546
331554
  };
331547
331555
  emitOrderExecutionTelemetryInBackground(otelMetrics, failedCreateContext, undefined, error48);
331548
- archiveOrderExecutionInBackground(brokerArchiver, failedCreateContext, undefined, error48);
331556
+ archiveOrderExecutionInBackground(brokerArchiver, failedCreateContext, undefined, error48, { marketMetadataHash });
331549
331557
  ctx.wrappedCallback({
331550
331558
  code: grpc6.status.INTERNAL,
331551
331559
  message: `Order Creation failed: ${sanitizeErrorDetail(error48)}`
@@ -332243,6 +332251,8 @@ async function handleTreasuryCall(ctx) {
332243
332251
  const callValue = parsePayloadForAction(ctx, CallPayloadSchema);
332244
332252
  if (callValue === null)
332245
332253
  return;
332254
+ let createOrderContext;
332255
+ let marketMetadataHash;
332246
332256
  try {
332247
332257
  if (callValue.functionName.startsWith("_") || callValue.functionName.includes("constructor") || callValue.functionName.includes("prototype")) {
332248
332258
  return ctx.wrappedCallback({
@@ -332265,8 +332275,41 @@ async function handleTreasuryCall(ctx) {
332265
332275
  message: `Function not found on broker: ${callValue.functionName}`
332266
332276
  }, null);
332267
332277
  }
332278
+ if (callValue.functionName === "createOrder") {
332279
+ const [symbol2, orderType, side, quantity, price] = callValue.args;
332280
+ const requestedQuantity = asFiniteNumber(quantity);
332281
+ const requestedPrice = asFiniteNumber(price);
332282
+ const requestedNotional = requestedQuantity !== undefined && requestedPrice !== undefined ? asFiniteNumber(requestedQuantity * requestedPrice) : undefined;
332283
+ const telemetryIds = extractOrderTelemetryIds(callValue.params);
332284
+ const submissionTimestamp = new Date().toISOString();
332285
+ createOrderContext = {
332286
+ action: "CreateOrder",
332287
+ cex: ctx.cex,
332288
+ accountLabel: ctx.selectedBrokerAccount?.label,
332289
+ symbol: asNonEmptyString(symbol2),
332290
+ orderType: asNonEmptyString(orderType),
332291
+ side: asNonEmptyString(side),
332292
+ requestedQuantity,
332293
+ requestedNotional,
332294
+ brokerObservedTimestamp: submissionTimestamp,
332295
+ ...telemetryIds
332296
+ };
332297
+ if (createOrderContext.symbol !== undefined) {
332298
+ marketMetadataHash = await captureMarketMetadataSnapshot(ctx.brokerArchiver, broker, {
332299
+ exchange: ctx.cex,
332300
+ accountSelector: ctx.selectedBrokerAccount?.label,
332301
+ symbol: createOrderContext.symbol,
332302
+ action: "CreateOrder",
332303
+ brokerObservedTimestamp: submissionTimestamp,
332304
+ ...telemetryIds
332305
+ });
332306
+ }
332307
+ }
332268
332308
  const result = await fn.apply(broker, argsArray);
332269
- if (callValue.functionName === "fetchWithdrawals") {
332309
+ if (createOrderContext !== undefined) {
332310
+ emitOrderExecutionTelemetryInBackground(ctx.otelMetrics, createOrderContext, result);
332311
+ archiveOrderExecutionInBackground(ctx.brokerArchiver, createOrderContext, result, undefined, { marketMetadataHash });
332312
+ } else if (callValue.functionName === "fetchWithdrawals") {
332270
332313
  archiveWithdrawalObservationsInBackground(ctx.brokerArchiver, ctx.withdrawalObservationTracker, {
332271
332314
  exchange: ctx.normalizedCex,
332272
332315
  accountSelector: ctx.selectedBrokerAccount?.label,
@@ -332278,6 +332321,11 @@ async function handleTreasuryCall(ctx) {
332278
332321
  result: JSON.stringify(result)
332279
332322
  });
332280
332323
  } catch (error48) {
332324
+ if (createOrderContext !== undefined) {
332325
+ rethrowArchiveDurabilityError(error48);
332326
+ emitOrderExecutionTelemetryInBackground(ctx.otelMetrics, createOrderContext, undefined, error48);
332327
+ archiveOrderExecutionInBackground(ctx.brokerArchiver, createOrderContext, undefined, error48, { marketMetadataHash });
332328
+ }
332281
332329
  safeLogError("Call failed", error48);
332282
332330
  rejectWithGrpcError(ctx, error48, {
332283
332331
  message: getErrorMessage(error48),
@@ -332286,6 +332334,19 @@ async function handleTreasuryCall(ctx) {
332286
332334
  });
332287
332335
  }
332288
332336
  }
332337
+ function asNonEmptyString(value) {
332338
+ return typeof value === "string" && value.trim() ? value : undefined;
332339
+ }
332340
+ function asFiniteNumber(value) {
332341
+ if (typeof value === "number") {
332342
+ return Number.isFinite(value) ? value : undefined;
332343
+ }
332344
+ if (typeof value !== "string" || !value.trim()) {
332345
+ return;
332346
+ }
332347
+ const parsed = Number(value);
332348
+ return Number.isFinite(parsed) ? parsed : undefined;
332349
+ }
332289
332350
 
332290
332351
  // src/handlers/execute-action/withdraw.ts
332291
332352
  var grpc10 = __toESM(require_src3(), 1);
@@ -45,4 +45,4 @@ export type OrderExecutionTelemetry = {
45
45
  export declare function emitOrderExecutionTelemetry(otelMetrics: OtelMetrics | undefined, context: OrderTelemetryContext, order: unknown, error?: unknown): Promise<OrderExecutionTelemetry | undefined>;
46
46
  export declare function buildOrderExecutionTelemetry(context: OrderTelemetryContext, order: unknown, error?: unknown): OrderExecutionTelemetry;
47
47
  export declare function emitOrderExecutionTelemetryInBackground(otelMetrics: OtelMetrics | undefined, context: OrderTelemetryContext, order: unknown, error?: unknown): void;
48
- export declare function extractOrderTelemetryIds(params: Record<string, string | number> | undefined): Pick<OrderTelemetryContext, "clientOrderId" | "idempotencyId" | "makerActionId">;
48
+ export declare function extractOrderTelemetryIds(params: Record<string, unknown> | undefined): Pick<OrderTelemetryContext, "clientOrderId" | "idempotencyId" | "makerActionId">;
package/dist/index.js CHANGED
@@ -306224,6 +306224,7 @@ var CreateOrderPayloadSchema = exports_external.object({
306224
306224
  toToken: exports_external.string().min(1),
306225
306225
  price: exports_external.coerce.number().positive(),
306226
306226
  marketType: marketTypeSchema,
306227
+ clientOrderId: exports_external.string().min(1).optional(),
306227
306228
  params: exports_external.preprocess(parseJsonString, stringNumberRecordSchema).default({})
306228
306229
  });
306229
306230
  var GetPerpConfigStatePayloadSchema = exports_external.object({
@@ -306995,7 +306996,14 @@ async function handleCreateOrder(ctx) {
306995
306996
  const orderValue = parsePayloadForAction(ctx, CreateOrderPayloadSchema);
306996
306997
  if (orderValue === null)
306997
306998
  return;
306999
+ const createOrderParams = {
307000
+ ...orderValue.params,
307001
+ ...orderValue.clientOrderId !== undefined && {
307002
+ clientOrderId: orderValue.clientOrderId
307003
+ }
307004
+ };
306998
307005
  let resolvedOrderTelemetry = {};
307006
+ let marketMetadataHash;
306999
307007
  try {
307000
307008
  if (!broker) {
307001
307009
  return ctx.wrappedCallback({
@@ -307018,9 +307026,9 @@ async function handleCreateOrder(ctx) {
307018
307026
  if (selectedBrokerAccount?.label) {
307019
307027
  orderActivityTracker?.record(cex3, selectedBrokerAccount.label, resolution.symbol);
307020
307028
  }
307021
- const telemetryIds = extractOrderTelemetryIds(orderValue.params);
307029
+ const telemetryIds = extractOrderTelemetryIds(createOrderParams);
307022
307030
  const submissionTimestamp = new Date().toISOString();
307023
- const marketMetadataHash = await captureMarketMetadataSnapshot(brokerArchiver, broker, {
307031
+ marketMetadataHash = await captureMarketMetadataSnapshot(brokerArchiver, broker, {
307024
307032
  exchange: cex3,
307025
307033
  accountSelector: selectedBrokerAccount?.label,
307026
307034
  symbol: resolution.symbol,
@@ -307028,7 +307036,7 @@ async function handleCreateOrder(ctx) {
307028
307036
  brokerObservedTimestamp: submissionTimestamp,
307029
307037
  ...telemetryIds
307030
307038
  });
307031
- const order = await broker.createOrder(resolution.symbol, orderValue.orderType, resolution.side, resolution.amountBase ?? orderValue.amount, orderValue.price, orderValue.params ?? {});
307039
+ const order = await broker.createOrder(resolution.symbol, orderValue.orderType, resolution.side, resolution.amountBase ?? orderValue.amount, orderValue.price, createOrderParams);
307032
307040
  const createOrderContext = {
307033
307041
  action: "CreateOrder",
307034
307042
  cex: cex3,
@@ -307056,10 +307064,10 @@ async function handleCreateOrder(ctx) {
307056
307064
  orderType: orderValue.orderType,
307057
307065
  requestedQuantity: resolvedOrderTelemetry.requestedQuantity ?? orderValue.amount,
307058
307066
  requestedNotional: orderValue.amount * orderValue.price,
307059
- ...extractOrderTelemetryIds(orderValue.params)
307067
+ ...extractOrderTelemetryIds(createOrderParams)
307060
307068
  };
307061
307069
  emitOrderExecutionTelemetryInBackground(otelMetrics, failedCreateContext, undefined, error48);
307062
- archiveOrderExecutionInBackground(brokerArchiver, failedCreateContext, undefined, error48);
307070
+ archiveOrderExecutionInBackground(brokerArchiver, failedCreateContext, undefined, error48, { marketMetadataHash });
307063
307071
  ctx.wrappedCallback({
307064
307072
  code: grpc6.status.INTERNAL,
307065
307073
  message: `Order Creation failed: ${sanitizeErrorDetail(error48)}`
@@ -307757,6 +307765,8 @@ async function handleTreasuryCall(ctx) {
307757
307765
  const callValue = parsePayloadForAction(ctx, CallPayloadSchema);
307758
307766
  if (callValue === null)
307759
307767
  return;
307768
+ let createOrderContext;
307769
+ let marketMetadataHash;
307760
307770
  try {
307761
307771
  if (callValue.functionName.startsWith("_") || callValue.functionName.includes("constructor") || callValue.functionName.includes("prototype")) {
307762
307772
  return ctx.wrappedCallback({
@@ -307779,8 +307789,41 @@ async function handleTreasuryCall(ctx) {
307779
307789
  message: `Function not found on broker: ${callValue.functionName}`
307780
307790
  }, null);
307781
307791
  }
307792
+ if (callValue.functionName === "createOrder") {
307793
+ const [symbol2, orderType, side, quantity, price] = callValue.args;
307794
+ const requestedQuantity = asFiniteNumber(quantity);
307795
+ const requestedPrice = asFiniteNumber(price);
307796
+ const requestedNotional = requestedQuantity !== undefined && requestedPrice !== undefined ? asFiniteNumber(requestedQuantity * requestedPrice) : undefined;
307797
+ const telemetryIds = extractOrderTelemetryIds(callValue.params);
307798
+ const submissionTimestamp = new Date().toISOString();
307799
+ createOrderContext = {
307800
+ action: "CreateOrder",
307801
+ cex: ctx.cex,
307802
+ accountLabel: ctx.selectedBrokerAccount?.label,
307803
+ symbol: asNonEmptyString(symbol2),
307804
+ orderType: asNonEmptyString(orderType),
307805
+ side: asNonEmptyString(side),
307806
+ requestedQuantity,
307807
+ requestedNotional,
307808
+ brokerObservedTimestamp: submissionTimestamp,
307809
+ ...telemetryIds
307810
+ };
307811
+ if (createOrderContext.symbol !== undefined) {
307812
+ marketMetadataHash = await captureMarketMetadataSnapshot(ctx.brokerArchiver, broker, {
307813
+ exchange: ctx.cex,
307814
+ accountSelector: ctx.selectedBrokerAccount?.label,
307815
+ symbol: createOrderContext.symbol,
307816
+ action: "CreateOrder",
307817
+ brokerObservedTimestamp: submissionTimestamp,
307818
+ ...telemetryIds
307819
+ });
307820
+ }
307821
+ }
307782
307822
  const result = await fn.apply(broker, argsArray);
307783
- if (callValue.functionName === "fetchWithdrawals") {
307823
+ if (createOrderContext !== undefined) {
307824
+ emitOrderExecutionTelemetryInBackground(ctx.otelMetrics, createOrderContext, result);
307825
+ archiveOrderExecutionInBackground(ctx.brokerArchiver, createOrderContext, result, undefined, { marketMetadataHash });
307826
+ } else if (callValue.functionName === "fetchWithdrawals") {
307784
307827
  archiveWithdrawalObservationsInBackground(ctx.brokerArchiver, ctx.withdrawalObservationTracker, {
307785
307828
  exchange: ctx.normalizedCex,
307786
307829
  accountSelector: ctx.selectedBrokerAccount?.label,
@@ -307792,6 +307835,11 @@ async function handleTreasuryCall(ctx) {
307792
307835
  result: JSON.stringify(result)
307793
307836
  });
307794
307837
  } catch (error48) {
307838
+ if (createOrderContext !== undefined) {
307839
+ rethrowArchiveDurabilityError(error48);
307840
+ emitOrderExecutionTelemetryInBackground(ctx.otelMetrics, createOrderContext, undefined, error48);
307841
+ archiveOrderExecutionInBackground(ctx.brokerArchiver, createOrderContext, undefined, error48, { marketMetadataHash });
307842
+ }
307795
307843
  safeLogError("Call failed", error48);
307796
307844
  rejectWithGrpcError(ctx, error48, {
307797
307845
  message: getErrorMessage(error48),
@@ -307800,6 +307848,19 @@ async function handleTreasuryCall(ctx) {
307800
307848
  });
307801
307849
  }
307802
307850
  }
307851
+ function asNonEmptyString(value) {
307852
+ return typeof value === "string" && value.trim() ? value : undefined;
307853
+ }
307854
+ function asFiniteNumber(value) {
307855
+ if (typeof value === "number") {
307856
+ return Number.isFinite(value) ? value : undefined;
307857
+ }
307858
+ if (typeof value !== "string" || !value.trim()) {
307859
+ return;
307860
+ }
307861
+ const parsed = Number(value);
307862
+ return Number.isFinite(parsed) ? parsed : undefined;
307863
+ }
307803
307864
 
307804
307865
  // src/handlers/execute-action/withdraw.ts
307805
307866
  import * as grpc10 from "@grpc/grpc-js";
@@ -310119,4 +310180,4 @@ export {
310119
310180
  CEXBroker as default
310120
310181
  };
310121
310182
 
310122
- //# debugId=56B5CD966A27D60564756E2164756E21
310183
+ //# debugId=1AF34CD02AB617AA64756E2164756E21