@velora-dex/sdk 9.5.4-dev.0 → 9.5.4-dev.2

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 (98) hide show
  1. package/README.md +156 -0
  2. package/dist/examples/helpers/delta.d.ts +1 -1
  3. package/dist/examples/helpers/delta.d.ts.map +1 -1
  4. package/dist/examples/helpers/deltaV2.d.ts +3 -3
  5. package/dist/examples/helpers/deltaV2.d.ts.map +1 -1
  6. package/dist/examples/wagmi.d.ts.map +1 -1
  7. package/dist/index.d.ts +4 -17
  8. package/dist/index.d.ts.map +1 -1
  9. package/dist/methods/delta/helpers/orders.d.ts +16 -1
  10. package/dist/methods/delta/helpers/orders.d.ts.map +1 -1
  11. package/dist/methods/delta/helpers/types.d.ts +48 -5
  12. package/dist/methods/delta/helpers/types.d.ts.map +1 -1
  13. package/dist/methods/deltaV2/{buildDeltaOrderV2.d.ts → buildDeltaOrder.d.ts} +10 -9
  14. package/dist/methods/deltaV2/buildDeltaOrder.d.ts.map +1 -0
  15. package/dist/methods/deltaV2/{buildExternalDeltaOrderV2.d.ts → buildExternalDeltaOrder.d.ts} +10 -9
  16. package/dist/methods/deltaV2/buildExternalDeltaOrder.d.ts.map +1 -0
  17. package/dist/methods/deltaV2/{buildTWAPDeltaOrderV2.d.ts → buildTWAPDeltaOrder.d.ts} +13 -12
  18. package/dist/methods/deltaV2/buildTWAPDeltaOrder.d.ts.map +1 -0
  19. package/dist/methods/deltaV2/cancelDeltaOrder.d.ts +21 -0
  20. package/dist/methods/deltaV2/cancelDeltaOrder.d.ts.map +1 -0
  21. package/dist/methods/deltaV2/getAgentsList.d.ts +10 -0
  22. package/dist/methods/deltaV2/getAgentsList.d.ts.map +1 -0
  23. package/dist/methods/deltaV2/getBridgeRoutes.d.ts +3 -3
  24. package/dist/methods/deltaV2/getBridgeRoutes.d.ts.map +1 -1
  25. package/dist/methods/deltaV2/getDeltaOrders.d.ts +40 -0
  26. package/dist/methods/deltaV2/getDeltaOrders.d.ts.map +1 -0
  27. package/dist/methods/deltaV2/{getDeltaPriceV2.d.ts → getDeltaPrice.d.ts} +7 -7
  28. package/dist/methods/deltaV2/getDeltaPrice.d.ts.map +1 -0
  29. package/dist/methods/deltaV2/helpers/orders.d.ts +191 -0
  30. package/dist/methods/deltaV2/helpers/orders.d.ts.map +1 -0
  31. package/dist/methods/deltaV2/index.d.ts +51 -38
  32. package/dist/methods/deltaV2/index.d.ts.map +1 -1
  33. package/dist/methods/deltaV2/isTokenSupportedInDelta.d.ts +8 -0
  34. package/dist/methods/deltaV2/isTokenSupportedInDelta.d.ts.map +1 -0
  35. package/dist/methods/deltaV2/{postDeltaOrderV2.d.ts → postDeltaOrder.d.ts} +9 -8
  36. package/dist/methods/deltaV2/postDeltaOrder.d.ts.map +1 -0
  37. package/dist/methods/deltaV2/postExternalDeltaOrder.d.ts +11 -0
  38. package/dist/methods/deltaV2/postExternalDeltaOrder.d.ts.map +1 -0
  39. package/dist/methods/deltaV2/postTWAPDeltaOrder.d.ts +17 -0
  40. package/dist/methods/deltaV2/postTWAPDeltaOrder.d.ts.map +1 -0
  41. package/dist/methods/deltaV2/types.d.ts +26 -20
  42. package/dist/methods/deltaV2/types.d.ts.map +1 -1
  43. package/dist/sdk/full.d.ts +1 -1
  44. package/dist/sdk/full.d.ts.map +1 -1
  45. package/dist/sdk/simple.d.ts +3 -9
  46. package/dist/sdk/simple.d.ts.map +1 -1
  47. package/dist/sdk.cjs.development.js +1602 -1281
  48. package/dist/sdk.cjs.development.js.map +1 -1
  49. package/dist/sdk.cjs.production.min.js +1 -1
  50. package/dist/sdk.cjs.production.min.js.map +1 -1
  51. package/dist/sdk.esm.js +1586 -1249
  52. package/dist/sdk.esm.js.map +1 -1
  53. package/package.json +1 -1
  54. package/src/constants.js +6 -1
  55. package/src/examples/deltaV2.ts +21 -25
  56. package/src/examples/helpers/delta.ts +6 -1
  57. package/src/examples/helpers/deltaV2.ts +9 -4
  58. package/src/index.js +167 -83
  59. package/src/index.ts +32 -143
  60. package/src/methods/delta/helpers/orders.ts +31 -1
  61. package/src/methods/delta/helpers/types.ts +59 -19
  62. package/src/methods/deltaV2/{buildDeltaOrderV2.ts → buildDeltaOrder.ts} +16 -16
  63. package/src/methods/deltaV2/{buildExternalDeltaOrderV2.ts → buildExternalDeltaOrder.ts} +16 -16
  64. package/src/methods/deltaV2/{buildTWAPDeltaOrderV2.ts → buildTWAPDeltaOrder.ts} +22 -22
  65. package/src/methods/deltaV2/{cancelDeltaOrderV2.ts → cancelDeltaOrder.ts} +17 -20
  66. package/src/methods/deltaV2/getAgentsList.ts +32 -0
  67. package/src/methods/deltaV2/getBridgeRoutes.ts +7 -7
  68. package/src/methods/deltaV2/getDeltaOrders.ts +149 -0
  69. package/src/methods/deltaV2/{getDeltaPriceV2.ts → getDeltaPrice.ts} +15 -15
  70. package/src/methods/deltaV2/helpers/orders.ts +363 -0
  71. package/src/methods/deltaV2/index.ts +148 -139
  72. package/src/methods/deltaV2/{isTokenSupportedInDeltaV2.ts → isTokenSupportedInDelta.ts} +7 -7
  73. package/src/methods/deltaV2/{postDeltaOrderV2.ts → postDeltaOrder.ts} +13 -16
  74. package/src/methods/deltaV2/postExternalDeltaOrder.ts +45 -0
  75. package/src/methods/deltaV2/{postTWAPDeltaOrderV2.ts → postTWAPDeltaOrder.ts} +14 -19
  76. package/src/methods/deltaV2/types.ts +35 -22
  77. package/src/sdk/full.ts +2 -2
  78. package/src/sdk/simple.ts +17 -43
  79. package/dist/methods/deltaV2/buildDeltaOrderV2.d.ts.map +0 -1
  80. package/dist/methods/deltaV2/buildExternalDeltaOrderV2.d.ts.map +0 -1
  81. package/dist/methods/deltaV2/buildTWAPDeltaOrderV2.d.ts.map +0 -1
  82. package/dist/methods/deltaV2/cancelDeltaOrderV2.d.ts +0 -21
  83. package/dist/methods/deltaV2/cancelDeltaOrderV2.d.ts.map +0 -1
  84. package/dist/methods/deltaV2/getAgentsListV2.d.ts +0 -10
  85. package/dist/methods/deltaV2/getAgentsListV2.d.ts.map +0 -1
  86. package/dist/methods/deltaV2/getDeltaOrdersV2.d.ts +0 -33
  87. package/dist/methods/deltaV2/getDeltaOrdersV2.d.ts.map +0 -1
  88. package/dist/methods/deltaV2/getDeltaPriceV2.d.ts.map +0 -1
  89. package/dist/methods/deltaV2/isTokenSupportedInDeltaV2.d.ts +0 -8
  90. package/dist/methods/deltaV2/isTokenSupportedInDeltaV2.d.ts.map +0 -1
  91. package/dist/methods/deltaV2/postDeltaOrderV2.d.ts.map +0 -1
  92. package/dist/methods/deltaV2/postExternalDeltaOrderV2.d.ts +0 -11
  93. package/dist/methods/deltaV2/postExternalDeltaOrderV2.d.ts.map +0 -1
  94. package/dist/methods/deltaV2/postTWAPDeltaOrderV2.d.ts +0 -16
  95. package/dist/methods/deltaV2/postTWAPDeltaOrderV2.d.ts.map +0 -1
  96. package/src/methods/deltaV2/getAgentsListV2.ts +0 -34
  97. package/src/methods/deltaV2/getDeltaOrdersV2.ts +0 -119
  98. package/src/methods/deltaV2/postExternalDeltaOrderV2.ts +0 -45
@@ -0,0 +1,363 @@
1
+ import type { NonEmptyArray } from 'ts-essentials';
2
+ import { OrderHelpers as DeltaV1OrderHelpers } from '../../delta/helpers/orders';
3
+ import type {
4
+ SwapSideUnion,
5
+ UnifiedDeltaOrderData,
6
+ } from '../../delta/helpers/types';
7
+ import type {
8
+ DeltaAuction,
9
+ DeltaOrderStatus,
10
+ DeltaTokenSide,
11
+ DeltaTransaction,
12
+ } from '../types';
13
+
14
+ /**
15
+ * v2 order helpers.
16
+ *
17
+ * The on-chain order structs (`auction.order`) are identical between v1 and v2
18
+ * — they come from the same `OnChainOrderMap` — so every order-struct guard and
19
+ * order-level getter is reused directly from v1's `OrderHelpers`. Likewise the
20
+ * auction discriminant guards only look at `onChainOrderType` (same union in
21
+ * both versions), so those are reused too.
22
+ *
23
+ * What differs in v2 is the *auction* envelope:
24
+ * - `status` is the integrator-facing `DeltaOrderStatus`
25
+ * (PENDING/ACTIVE/COMPLETED/… — not v1's `DeltaAuctionStatus`),
26
+ * - amounts live on `input`/`output` (`DeltaTokenSide`) and `transactions`
27
+ * (`DeltaTransaction`) instead of v1's order/transaction shapes,
28
+ * - `side` is carried explicitly on the auction.
29
+ *
30
+ * Those pieces are reimplemented here against the v2 shapes.
31
+ */
32
+
33
+ const v1Checks = DeltaV1OrderHelpers.checks;
34
+ const v1Getters = DeltaV1OrderHelpers.getters;
35
+
36
+ ///// CHECKS //////
37
+
38
+ /**
39
+ * @description Checks whether an auction is fully executed (settled on every chain).
40
+ */
41
+ function isCompletedAuction<T extends Pick<DeltaAuction, 'status'>>(
42
+ auction: T
43
+ ): auction is T & { status: 'COMPLETED' } {
44
+ return auction.status === 'COMPLETED';
45
+ }
46
+
47
+ const failedAuctionStatuses = [
48
+ 'FAILED',
49
+ 'EXPIRED',
50
+ 'CANCELLED',
51
+ 'REFUNDED',
52
+ ] as const;
53
+
54
+ const failedAuctionStatusesSet = new Set<DeltaOrderStatus>(
55
+ failedAuctionStatuses
56
+ );
57
+
58
+ /**
59
+ * @description Checks whether an auction is in a terminal failure state
60
+ * (failed, expired, cancelled, or refunded).
61
+ */
62
+ function isFailedAuction<T extends Pick<DeltaAuction, 'status'>>(
63
+ auction: T
64
+ ): auction is T & { status: (typeof failedAuctionStatuses)[number] } {
65
+ return failedAuctionStatusesSet.has(auction.status);
66
+ }
67
+
68
+ /**
69
+ * @description Checks whether an auction status is cancelled.
70
+ */
71
+ function isCanceledAuction<T extends Pick<DeltaAuction, 'status'>>(
72
+ auction: T
73
+ ): auction is T & { status: 'CANCELLED' } {
74
+ return auction.status === 'CANCELLED';
75
+ }
76
+
77
+ /**
78
+ * @description Checks whether an auction status is expired.
79
+ */
80
+ function isExpiredAuction<T extends Pick<DeltaAuction, 'status'>>(
81
+ auction: T
82
+ ): auction is T & { status: 'EXPIRED' } {
83
+ return auction.status === 'EXPIRED';
84
+ }
85
+
86
+ const pendingAuctionStatuses = [
87
+ 'PENDING',
88
+ 'AWAITING_SIGNATURE',
89
+ 'ACTIVE',
90
+ 'BRIDGING',
91
+ ] as const;
92
+
93
+ const pendingAuctionStatusesSet = new Set<DeltaOrderStatus>(
94
+ pendingAuctionStatuses
95
+ );
96
+
97
+ /**
98
+ * @description Checks whether an auction is still in flight (not yet settled
99
+ * and not failed): awaiting signature, pending, actively executing, or bridging.
100
+ */
101
+ function isPendingAuction<T extends Pick<DeltaAuction, 'status'>>(
102
+ auction: T
103
+ ): auction is T & { status: (typeof pendingAuctionStatuses)[number] } {
104
+ return pendingAuctionStatusesSet.has(auction.status);
105
+ }
106
+
107
+ /**
108
+ * @description Checks whether an auction is a Fillable auction.
109
+ * `FillableOrder` is the `onChainOrderType` the server reports for a
110
+ * `partiallyFillable` Standard order; it carries the same order struct as
111
+ * `Order`. Consumers that don't distinguish the two should treat
112
+ * `isDeltaAuction(a) || isFillableAuction(a)` as "is a standard order".
113
+ */
114
+ function isFillableAuction<T extends Pick<DeltaAuction, 'onChainOrderType'>>(
115
+ auction: T
116
+ ): auction is T & { onChainOrderType: 'FillableOrder' } {
117
+ return auction.onChainOrderType === 'FillableOrder';
118
+ }
119
+
120
+ /**
121
+ * @description Checks whether an auction has been partially executed:
122
+ * it has at least one transaction and an overall filled percent strictly
123
+ * between 0 and 100.
124
+ */
125
+ function isPartiallyExecutedAuction<
126
+ T extends Pick<DeltaAuction, 'order' | 'transactions'>,
127
+ >(
128
+ auction: T
129
+ ): auction is T & { transactions: NonEmptyArray<DeltaTransaction> } {
130
+ if (auction.transactions.length === 0) return false;
131
+
132
+ const filledPercent = getFilledPercent(auction);
133
+
134
+ return filledPercent > 0 && filledPercent < 100;
135
+ }
136
+
137
+ const checks = {
138
+ // order-struct guards — order structs are shared with v1, reused as-is.
139
+ isTWAPOrder: v1Checks.isTWAPOrder,
140
+ isTWAPSellOrder: v1Checks.isTWAPSellOrder,
141
+ isTWAPBuyOrder: v1Checks.isTWAPBuyOrder,
142
+ isExternalOrder: v1Checks.isExternalOrder,
143
+ isDeltaOrder: v1Checks.isDeltaOrder,
144
+ isProductiveOrder: v1Checks.isProductiveOrder,
145
+
146
+ // auction discriminant guards — `onChainOrderType` union is shared, reused as-is.
147
+ isTWAPAuction: v1Checks.isTWAPAuction,
148
+ isTWAPSellAuction: v1Checks.isTWAPSellAuction,
149
+ isTWAPBuyAuction: v1Checks.isTWAPBuyAuction,
150
+ isDeltaAuction: v1Checks.isDeltaAuction,
151
+ isExternalAuction: v1Checks.isExternalAuction,
152
+ isProductiveAuction: v1Checks.isProductiveAuction,
153
+ isFillableAuction,
154
+
155
+ // status / execution guards — v2 status enum & transaction shape.
156
+ isCompletedAuction,
157
+ isFailedAuction,
158
+ isCanceledAuction,
159
+ isExpiredAuction,
160
+ isPendingAuction,
161
+ isPartiallyExecutedAuction,
162
+ };
163
+
164
+ ///// GETTERS //////
165
+
166
+ /**
167
+ * @description Reads an amount off a v2 token side. A SELL input / BUY output
168
+ * carries an explicit `amount`; the opposite side carries
169
+ * `expectedAmount`/`executedAmount`. `prefer` chooses which to read on the
170
+ * expected/executed variant.
171
+ */
172
+ function getTokenSideAmount(
173
+ side: DeltaTokenSide,
174
+ prefer: 'expected' | 'executed'
175
+ ): string {
176
+ if ('amount' in side) return side.amount;
177
+
178
+ const value =
179
+ prefer === 'executed' ? side.executedAmount : side.expectedAmount;
180
+
181
+ return value ?? '0';
182
+ }
183
+
184
+ /**
185
+ * @description Returns the source chain id for the auction (the input side's chain).
186
+ */
187
+ function getAuctionSrcChainId(auction: Pick<DeltaAuction, 'input'>): number {
188
+ return auction.input.chainId;
189
+ }
190
+
191
+ /**
192
+ * @description Returns the destination chain id for the auction (the output side's chain).
193
+ * Equals the source chain id for same-chain orders.
194
+ */
195
+ function getAuctionDestChainId(auction: Pick<DeltaAuction, 'output'>): number {
196
+ return auction.output.chainId;
197
+ }
198
+
199
+ /**
200
+ * @description Returns the swap side for any auction. v2 carries `side` on the
201
+ * auction directly, so no order introspection is needed.
202
+ */
203
+ function getAuctionSwapSide(
204
+ auction: Pick<DeltaAuction, 'side'>
205
+ ): SwapSideUnion {
206
+ return auction.side;
207
+ }
208
+
209
+ /**
210
+ * @description Returns source and destination token addresses for the auction,
211
+ * read from the input/output sides (already resolved to the dest-chain token
212
+ * for cross-chain orders).
213
+ */
214
+ function getAuctionTokenAddresses(
215
+ auction: Pick<DeltaAuction, 'input' | 'output'>
216
+ ) {
217
+ return {
218
+ srcToken: auction.input.token,
219
+ destToken: auction.output.token,
220
+ };
221
+ }
222
+
223
+ /**
224
+ * @description Aggregates transaction amounts into total spent (src) and
225
+ * received (dest) values.
226
+ */
227
+ function getTransactionAmounts(transactions: DeltaTransaction[]) {
228
+ const { srcAmount, destAmount } = transactions.reduce(
229
+ (acc, { spentAmount, receivedAmount }) => ({
230
+ srcAmount: acc.srcAmount + BigInt(spentAmount ?? 0),
231
+ destAmount: acc.destAmount + BigInt(receivedAmount ?? 0),
232
+ }),
233
+ { srcAmount: 0n, destAmount: 0n }
234
+ );
235
+
236
+ return {
237
+ srcAmount: srcAmount.toString(),
238
+ destAmount: destAmount.toString(),
239
+ };
240
+ }
241
+
242
+ /**
243
+ * @description Calculates the overall filled percent (0–100) from the
244
+ * per-transaction `filledPercent` values. For TWAP orders each transaction is
245
+ * a slice (0–100 of that slice), so the slice values are averaged across
246
+ * `numSlices`; for single-fill orders the values sum directly.
247
+ */
248
+ function getFilledPercent(
249
+ auction: Pick<DeltaAuction, 'order' | 'transactions'>
250
+ ): number {
251
+ if (auction.transactions.length === 0) return 0;
252
+
253
+ const total = auction.transactions.reduce(
254
+ (acc, { filledPercent }) => acc + filledPercent,
255
+ 0
256
+ );
257
+
258
+ if (checks.isTWAPOrder(auction.order) && auction.order.numSlices > 0) {
259
+ return total / auction.order.numSlices;
260
+ }
261
+
262
+ return total;
263
+ }
264
+
265
+ /**
266
+ * @description Returns expected amounts and, once the auction is completed,
267
+ * executed amounts. Executed amounts prefer the `executedAmount` baked onto the
268
+ * token sides and fall back to summing transactions.
269
+ */
270
+ function getAuctionAmounts(
271
+ auction: Pick<
272
+ DeltaAuction,
273
+ 'status' | 'order' | 'input' | 'output' | 'transactions'
274
+ >
275
+ ) {
276
+ const expected = {
277
+ srcAmount: getTokenSideAmount(auction.input, 'expected'),
278
+ destAmount: getTokenSideAmount(auction.output, 'expected'),
279
+ };
280
+
281
+ if (!isCompletedAuction(auction)) {
282
+ return { expected };
283
+ }
284
+
285
+ const txAmounts = getTransactionAmounts(auction.transactions);
286
+
287
+ const executed = {
288
+ srcAmount: getExecutedAmount(auction.input, txAmounts.srcAmount),
289
+ destAmount: getExecutedAmount(auction.output, txAmounts.destAmount),
290
+ };
291
+
292
+ return { expected, executed };
293
+ }
294
+
295
+ /**
296
+ * @description Returns the executed amount of a token side when present,
297
+ * otherwise the provided fallback (typically summed from transactions).
298
+ */
299
+ function getExecutedAmount(side: DeltaTokenSide, fallback: string): string {
300
+ if ('executedAmount' in side && side.executedAmount != null) {
301
+ return side.executedAmount;
302
+ }
303
+
304
+ return fallback;
305
+ }
306
+
307
+ /**
308
+ * @description Returns unified order data with normalized amounts, tokens,
309
+ * chain ids, and side — the v2 counterpart of v1's `getUnifiedDeltaOrderData`,
310
+ * built from the v2 auction envelope.
311
+ */
312
+ function getUnifiedDeltaOrderData(
313
+ auction: DeltaAuction
314
+ ): UnifiedDeltaOrderData {
315
+ const { srcToken, destToken } = getAuctionTokenAddresses(auction);
316
+ const { expected, executed } = getAuctionAmounts(auction);
317
+
318
+ const srcChainId = getAuctionSrcChainId(auction);
319
+ const destChainId = getAuctionDestChainId(auction);
320
+ const swapSide = getAuctionSwapSide(auction);
321
+ const filledPercent = getFilledPercent(auction);
322
+
323
+ return {
324
+ srcChainId,
325
+ destChainId,
326
+ srcAmount: executed?.srcAmount || expected.srcAmount,
327
+ destAmount: executed?.destAmount || expected.destAmount,
328
+ amounts: {
329
+ expected,
330
+ final: executed,
331
+ },
332
+ srcToken,
333
+ destToken,
334
+ swapSide,
335
+ filledPercent,
336
+ };
337
+ }
338
+
339
+ const getters = {
340
+ getUnifiedDeltaOrderData,
341
+
342
+ // auction-level getters — v2 envelope shape.
343
+ getAuctionTokenAddresses,
344
+ getAuctionSrcChainId,
345
+ getAuctionDestChainId,
346
+ getAuctionSwapSide,
347
+ getTransactionAmounts,
348
+ getAuctionAmounts,
349
+ getFilledPercent,
350
+
351
+ // order-level getters — order structs are shared with v1, reused as-is.
352
+ getOrderTokenAddresses: v1Getters.getOrderTokenAddresses,
353
+ getSwapSideFromDeltaOrder: v1Getters.getSwapSideFromDeltaOrder,
354
+ getSwapSideFromTwapOrderType: v1Getters.getSwapSideFromTwapOrderType,
355
+ getExpectedTwapSrcAmount: v1Getters.getExpectedTwapSrcAmount,
356
+ getExpectedTwapDestAmount: v1Getters.getExpectedTwapDestAmount,
357
+ getExpectedTwapOrderAmounts: v1Getters.getExpectedTwapOrderAmounts,
358
+ };
359
+
360
+ export const OrderHelpers = {
361
+ checks,
362
+ getters,
363
+ };