@pod-network/trade-sdk 0.6.0 → 0.8.0

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.
Files changed (42) hide show
  1. package/dist/codec/decode.d.ts.map +1 -1
  2. package/dist/codec/decode.js +10 -3
  3. package/dist/codec/decode.js.map +1 -1
  4. package/dist/codec/orders-v2.js +2 -1
  5. package/dist/codec/orders-v2.js.map +1 -1
  6. package/dist/codec/units.d.ts +6 -0
  7. package/dist/codec/units.d.ts.map +1 -1
  8. package/dist/codec/units.js +8 -0
  9. package/dist/codec/units.js.map +1 -1
  10. package/dist/index.d.ts +1 -1
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +1 -1
  13. package/dist/index.js.map +1 -1
  14. package/dist/sync/order-preview.d.ts +4 -1
  15. package/dist/sync/order-preview.d.ts.map +1 -1
  16. package/dist/sync/order-preview.js +9 -5
  17. package/dist/sync/order-preview.js.map +1 -1
  18. package/dist/sync/positions-live.d.ts.map +1 -1
  19. package/dist/sync/positions-live.js +1 -0
  20. package/dist/sync/positions-live.js.map +1 -1
  21. package/dist/sync/sources.d.ts.map +1 -1
  22. package/dist/sync/sources.js +2 -1
  23. package/dist/sync/sources.js.map +1 -1
  24. package/dist/types/public.d.ts +7 -2
  25. package/dist/types/public.d.ts.map +1 -1
  26. package/dist/types/wire.d.ts +8 -2
  27. package/dist/types/wire.d.ts.map +1 -1
  28. package/dist/write/index.d.ts +28 -6
  29. package/dist/write/index.d.ts.map +1 -1
  30. package/dist/write/index.js +42 -15
  31. package/dist/write/index.js.map +1 -1
  32. package/package.json +1 -1
  33. package/src/codec/decode.ts +12 -3
  34. package/src/codec/orders-v2.ts +2 -1
  35. package/src/codec/units.ts +9 -0
  36. package/src/index.ts +2 -1
  37. package/src/sync/order-preview.ts +13 -6
  38. package/src/sync/positions-live.ts +1 -0
  39. package/src/sync/sources.ts +2 -1
  40. package/src/types/public.ts +7 -2
  41. package/src/types/wire.ts +9 -3
  42. package/src/write/index.ts +56 -17
package/src/types/wire.ts CHANGED
@@ -14,6 +14,8 @@ export interface WireStatus {
14
14
  export interface WireMarketStatic {
15
15
  id: Hex;
16
16
  name: string;
17
+ /** Open, not an enum: a newer node's value must not be a type error. */
18
+ status: "active" | (string & {});
17
19
  base_token_address: Hex;
18
20
  quote_token_address: Hex;
19
21
  base_token_symbol: string;
@@ -25,7 +27,8 @@ export interface WireMarketStatic {
25
27
  maker_fee: WireDecimal;
26
28
  taker_fee: WireDecimal;
27
29
  tick_precision: WireDecimal; // 1e18-scaled price increment (decimal string)
28
- lot_size: WireDecimal;
30
+ lot_size: WireDecimal; // 1e18-scaled size increment
31
+ min_notional: WireDecimal; // 1e18-scaled notional floor ("0" = none)
29
32
  max_leverage: number;
30
33
  funding_window_us: number; // funding-accrual divisor (micros)
31
34
  }
@@ -355,8 +358,11 @@ export interface WireOrderEvent {
355
358
  tq?: WireDecimal;
356
359
  /** Total fee charged so far; travels with `tb`. There is no per-fill counterpart. */
357
360
  tf?: WireDecimal;
358
- /** `fill`: present only on the fill that closed the order, carrying its terminal status. */
359
- st?: "filled" | "canceled" | "margin_canceled" | "expired";
361
+ /**
362
+ * The terminal status an event closed the order with. On `fill`, only on the fill that
363
+ * closed it. On `cancel`, only when the engine removed the order rather than the owner.
364
+ */
365
+ st?: "filled" | "canceled" | "margin_canceled" | "expired" | "post_only_refused";
360
366
  /**
361
367
  * `fill` on a perp market: the owner's position after this fill, signed and
362
368
  * 1e18-scaled. Omitted on spot, which has no position.
@@ -14,6 +14,10 @@ import { decodeAbiParameters, encodeAbiParameters, encodeFunctionData, keccak256
14
14
  import { generatePrivateKey, privateKeyToAccount } from "viem/accounts";
15
15
  import type { Address, Hash, Hex, MarketId } from "../types/public.js";
16
16
  import { rpc } from "../transport/jsonrpc.js";
17
+ // In codec/ so the read entry reaches it too; re-exported so all three align* are here.
18
+ import { alignSize } from "../codec/units.js";
19
+
20
+ export { alignSize };
17
21
 
18
22
  /** The CLOB precompile (chain id 1293 / 0x50d). */
19
23
  export const CLOB_ADDRESS: Address = "0x50d0000000000000000000000000000000000002";
@@ -51,6 +55,11 @@ export interface SubmitOrderParams {
51
55
  * like 49999.99999999999). Pass `market.tickPrecision`.
52
56
  */
53
57
  tickPrecision?: bigint;
58
+ /**
59
+ * Market `lot_size`. When given, `size` is floored to a multiple of it — the CLOB
60
+ * rejects a size off the lot grid (`size_off_lot`). Pass `market.lotSize`.
61
+ */
62
+ lotSize?: bigint;
54
63
  /**
55
64
  * Market auction interval in microseconds (`market.auctionIntervalMs * 1000`).
56
65
  * The CLOB requires `deadline` to be a multiple of it ("Deadline is not aligned
@@ -96,7 +105,9 @@ function us(ms: number | undefined, fallback: bigint): bigint {
96
105
 
97
106
  export function buildSubmitOrder(p: SubmitOrderParams): PodTxRequest {
98
107
  const nowUs = BigInt(Date.now()) * 1000n;
99
- const signedSize = p.side === "sell" ? -p.size : p.size;
108
+ // Align the magnitude before the sign: flooring a negative rounds it the wrong way.
109
+ const size = p.lotSize ? alignSize(p.size, p.lotSize) : p.size;
110
+ const signedSize = p.side === "sell" ? -size : size;
100
111
  const price = p.tickPrecision ? alignPrice(p.price, p.tickPrecision) : p.price;
101
112
  const interval = p.auctionIntervalUs ? BigInt(p.auctionIntervalUs) : 0n;
102
113
  const deadline = alignDeadline(us(p.deadline, nowUs + FAR_US), interval);
@@ -149,9 +160,13 @@ export function buildCancelOrder(
149
160
  /** Solidity `Error(string)` selector — `bytes4(keccak256("Error(string)"))`. */
150
161
  const ERROR_SELECTOR = "0x08c379a0";
151
162
 
152
- /** A reverted pod transaction, carrying the decoded CLOB reason. */
163
+ /**
164
+ * A rejected pod transaction. `data`'s shape follows `code`: −32000 validation (string),
165
+ * 3 execution revert (`Error(string)`, decoded into `reason`), −32003 rejection quorum
166
+ * (array), 999 account locked (object).
167
+ */
153
168
  export class PodTxRevertError extends Error {
154
- constructor(public reason: string, public data?: string) {
169
+ constructor(public reason: string, public data?: unknown, public code?: number) {
155
170
  super(reason);
156
171
  this.name = "PodTxRevertError";
157
172
  }
@@ -162,9 +177,11 @@ export class PodTxRevertError extends Error {
162
177
  * "Deadline is not aligned to auction interval"). Returns undefined when `data`
163
178
  * isn't an `Error(string)` payload — use this directly if you broadcast through
164
179
  * your own provider and just want to translate the revert data.
180
+ *
181
+ * `unknown` because only code 3 sends a string; the other shapes reach this too.
165
182
  */
166
- export function decodeRevertReason(data: string | null | undefined): string | undefined {
167
- if (!data || !data.startsWith(ERROR_SELECTOR)) return undefined;
183
+ export function decodeRevertReason(data: unknown): string | undefined {
184
+ if (typeof data !== "string" || !data.startsWith(ERROR_SELECTOR)) return undefined;
168
185
  try {
169
186
  return decodeAbiParameters([{ type: "string" }], `0x${data.slice(10)}` as Hex)[0];
170
187
  } catch {
@@ -189,10 +206,10 @@ export async function sendRawTransaction(
189
206
  headers: { "content-type": "application/json" },
190
207
  body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "eth_sendRawTransaction", params: [signedTx] }),
191
208
  });
192
- const json = (await res.json()) as { result?: Hash; error?: { message?: string; data?: string } };
209
+ const json = (await res.json()) as { result?: Hash; error?: { code?: number; message?: string; data?: unknown } };
193
210
  if (json.error) {
194
211
  const reason = decodeRevertReason(json.error.data) ?? json.error.message ?? "transaction failed";
195
- throw new PodTxRevertError(reason, json.error.data);
212
+ throw new PodTxRevertError(reason, json.error.data, json.error.code);
196
213
  }
197
214
  return json.result!;
198
215
  }
@@ -215,6 +232,7 @@ export interface UpdateOrderParams {
215
232
  */
216
233
  token: Address;
217
234
  tickPrecision?: bigint;
235
+ lotSize?: bigint;
218
236
  auctionIntervalUs?: number;
219
237
  deadline?: number; // ms; default far future
220
238
  }
@@ -222,13 +240,14 @@ export interface UpdateOrderParams {
222
240
  /**
223
241
  * Amend a resting order's price and/or size in place via the CLOB's native
224
242
  * `update` (no cancel+resubmit, so the order keeps its id and queue identity).
225
- * Aligns price to the tick and deadline to the auction interval, like
226
- * {@link buildSubmitOrder}.
243
+ * Aligns price to the tick, size to the lot and deadline to the auction interval,
244
+ * like {@link buildSubmitOrder}.
227
245
  */
228
246
  export function buildUpdateOrder(p: UpdateOrderParams): PodTxRequest {
229
247
  const nowUs = BigInt(Date.now()) * 1000n;
230
248
  const price = p.tickPrecision ? alignPrice(p.price, p.tickPrecision) : p.price;
231
- const size = p.size < 0n ? -p.size : p.size; // contract takes an unsigned magnitude
249
+ const mag = p.size < 0n ? -p.size : p.size; // contract takes an unsigned magnitude
250
+ const size = p.lotSize ? alignSize(mag, p.lotSize) : mag;
232
251
  const interval = p.auctionIntervalUs ? BigInt(p.auctionIntervalUs) : 0n;
233
252
  const deadline = alignDeadline(us(p.deadline, nowUs + FAR_US), interval);
234
253
  const data = encodeFunctionData({
@@ -271,6 +290,7 @@ export interface SubmitTriggerParams {
271
290
  reduceOnly?: boolean; // default true
272
291
  ioc?: boolean;
273
292
  tickPrecision?: bigint;
293
+ lotSize?: bigint;
274
294
  auctionIntervalUs?: number;
275
295
  deadline?: number; // ms; default far future
276
296
  /** Armed lifetime as a duration in ms (capped at ~30d by the CLOB); default 29d. */
@@ -279,7 +299,8 @@ export interface SubmitTriggerParams {
279
299
 
280
300
  export function buildSubmitTrigger(p: SubmitTriggerParams): PodTxRequest {
281
301
  const nowUs = BigInt(Date.now()) * 1000n;
282
- const signedSize = p.side === "sell" ? -p.size : p.size;
302
+ const size = p.lotSize ? alignSize(p.size, p.lotSize) : p.size;
303
+ const signedSize = p.side === "sell" ? -size : size;
283
304
  const align = (v: bigint) => (p.tickPrecision ? alignPrice(v, p.tickPrecision) : v);
284
305
  const triggerPrice = align(p.triggerPrice);
285
306
  const limitPrice = align(p.limitPrice ?? p.triggerPrice);
@@ -330,13 +351,15 @@ export interface UpdateTriggerParams {
330
351
  triggerPrice: bigint;
331
352
  limitPrice?: bigint;
332
353
  tickPrecision?: bigint;
354
+ lotSize?: bigint;
333
355
  auctionIntervalUs?: number;
334
356
  deadline?: number;
335
357
  }
336
358
 
337
359
  export function buildUpdateTrigger(p: UpdateTriggerParams): PodTxRequest {
338
360
  const nowUs = BigInt(Date.now()) * 1000n;
339
- const signedSize = p.side === "sell" ? -p.size : p.size;
361
+ const size = p.lotSize ? alignSize(p.size, p.lotSize) : p.size;
362
+ const signedSize = p.side === "sell" ? -size : size;
340
363
  const align = (v: bigint) => (p.tickPrecision ? alignPrice(v, p.tickPrecision) : v);
341
364
  const interval = p.auctionIntervalUs ? BigInt(p.auctionIntervalUs) : 0n;
342
365
  const deadline = alignDeadline(us(p.deadline, nowUs + FAR_US), interval);
@@ -360,10 +383,15 @@ export function buildUpdateTrigger(p: UpdateTriggerParams): PodTxRequest {
360
383
  /** ~10 years in ms — default far-future deadline shared by batched legs. */
361
384
  const FAR_MS = 10 * 365 * 24 * 3600 * 1000;
362
385
 
363
- /** Scale a 1e18 size by a 0..1 fraction (e.g. close 50% of a position). */
364
- function scaleSize(size: bigint, fraction: number): bigint {
386
+ /**
387
+ * Scale a 1e18 size by a 0..1 fraction (e.g. close 50% of a position), floored to the lot.
388
+ * The fraction's own 1e-6 resolution is no substitute — that is a proportion, the lot is
389
+ * an absolute step, and only the lot is what the CLOB checks.
390
+ */
391
+ function scaleSize(size: bigint, fraction: number, lot?: bigint): bigint {
365
392
  const f = Math.max(0, Math.min(1, fraction));
366
- return (size * BigInt(Math.round(f * 1_000_000))) / 1_000_000n;
393
+ const scaled = (size * BigInt(Math.round(f * 1_000_000))) / 1_000_000n;
394
+ return lot ? alignSize(scaled, lot) : scaled;
367
395
  }
368
396
 
369
397
  /** Wrap already-built legs into one `submitBatch` tx. Pass leg requests (their
@@ -414,6 +442,7 @@ export interface OrderWithTriggersParams {
414
442
  takeProfit?: OrderTrigger;
415
443
  stopLoss?: OrderTrigger;
416
444
  tickPrecision?: bigint;
445
+ lotSize?: bigint;
417
446
  auctionIntervalUs?: number;
418
447
  reduceOnly?: boolean; // entry reduceOnly (rare); triggers are always reduceOnly
419
448
  ioc?: boolean;
@@ -433,13 +462,17 @@ export interface OrderWithTriggersParams {
433
462
  */
434
463
  export function buildOrderWithTriggers(p: OrderWithTriggersParams): PodTxRequest {
435
464
  const deadline = p.deadline ?? Date.now() + FAR_MS;
465
+ // Align once and hand the same size to entry and triggers: flooring only inside
466
+ // `buildSubmitOrder` would leave a whole-size trigger one lot over the position.
467
+ const size = p.lotSize ? alignSize(p.size, p.lotSize) : p.size;
436
468
  const order = buildSubmitOrder({
437
469
  orderbookId: p.orderbookId,
438
470
  side: p.side,
439
471
  orderType: p.orderType,
440
472
  price: p.price,
441
- size: p.size,
473
+ size,
442
474
  tickPrecision: p.tickPrecision,
475
+ lotSize: p.lotSize,
443
476
  auctionIntervalUs: p.auctionIntervalUs,
444
477
  reduceOnly: p.reduceOnly,
445
478
  ioc: p.ioc,
@@ -451,13 +484,14 @@ export function buildOrderWithTriggers(p: OrderWithTriggersParams): PodTxRequest
451
484
  buildSubmitTrigger({
452
485
  orderbookId: p.orderbookId,
453
486
  side: closeSide,
454
- size: scaleSize(p.size, t.sizeFraction ?? 1),
487
+ size: scaleSize(size, t.sizeFraction ?? 1, p.lotSize),
455
488
  triggerType,
456
489
  triggerPrice: t.triggerPrice,
457
490
  limitPrice: t.limitPrice,
458
491
  grouping: "position",
459
492
  reduceOnly: true,
460
493
  tickPrecision: p.tickPrecision,
494
+ lotSize: p.lotSize,
461
495
  auctionIntervalUs: p.auctionIntervalUs,
462
496
  deadline,
463
497
  ttlMs: p.triggerTtlMs,
@@ -491,6 +525,10 @@ export interface ClosePositionParams {
491
525
  /** Slippage bound in basis points for the market close. Default 500 (5%). */
492
526
  slippageBps?: number;
493
527
  tickPrecision?: bigint;
528
+ /** Market `lot_size`. A partial close is a fraction of the position and lands off the
529
+ * lot grid for most fractions; flooring leaves at most one lot open, which `reduceOnly`
530
+ * already makes the safe direction. */
531
+ lotSize?: bigint;
494
532
  auctionIntervalUs?: number;
495
533
  deadline?: number; // ms; pass a shared value when batching closes
496
534
  }
@@ -514,6 +552,7 @@ export function buildClosePosition(p: ClosePositionParams): PodTxRequest {
514
552
  size: p.size < 0n ? -p.size : p.size,
515
553
  reduceOnly: true,
516
554
  tickPrecision: p.tickPrecision,
555
+ lotSize: p.lotSize,
517
556
  auctionIntervalUs: p.auctionIntervalUs,
518
557
  deadline: p.deadline,
519
558
  });