@velora-dex/sdk 9.5.4-dev.1 → 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 (96) 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/index.d.ts +4 -17
  7. package/dist/index.d.ts.map +1 -1
  8. package/dist/methods/delta/helpers/orders.d.ts.map +1 -1
  9. package/dist/methods/delta/helpers/types.d.ts +11 -5
  10. package/dist/methods/delta/helpers/types.d.ts.map +1 -1
  11. package/dist/methods/deltaV2/{buildDeltaOrderV2.d.ts → buildDeltaOrder.d.ts} +10 -9
  12. package/dist/methods/deltaV2/buildDeltaOrder.d.ts.map +1 -0
  13. package/dist/methods/deltaV2/{buildExternalDeltaOrderV2.d.ts → buildExternalDeltaOrder.d.ts} +10 -9
  14. package/dist/methods/deltaV2/buildExternalDeltaOrder.d.ts.map +1 -0
  15. package/dist/methods/deltaV2/{buildTWAPDeltaOrderV2.d.ts → buildTWAPDeltaOrder.d.ts} +13 -12
  16. package/dist/methods/deltaV2/buildTWAPDeltaOrder.d.ts.map +1 -0
  17. package/dist/methods/deltaV2/cancelDeltaOrder.d.ts +21 -0
  18. package/dist/methods/deltaV2/cancelDeltaOrder.d.ts.map +1 -0
  19. package/dist/methods/deltaV2/getAgentsList.d.ts +10 -0
  20. package/dist/methods/deltaV2/getAgentsList.d.ts.map +1 -0
  21. package/dist/methods/deltaV2/getBridgeRoutes.d.ts +3 -3
  22. package/dist/methods/deltaV2/getBridgeRoutes.d.ts.map +1 -1
  23. package/dist/methods/deltaV2/getDeltaOrders.d.ts +40 -0
  24. package/dist/methods/deltaV2/getDeltaOrders.d.ts.map +1 -0
  25. package/dist/methods/deltaV2/{getDeltaPriceV2.d.ts → getDeltaPrice.d.ts} +7 -7
  26. package/dist/methods/deltaV2/getDeltaPrice.d.ts.map +1 -0
  27. package/dist/methods/deltaV2/helpers/orders.d.ts +191 -0
  28. package/dist/methods/deltaV2/helpers/orders.d.ts.map +1 -0
  29. package/dist/methods/deltaV2/index.d.ts +51 -38
  30. package/dist/methods/deltaV2/index.d.ts.map +1 -1
  31. package/dist/methods/deltaV2/isTokenSupportedInDelta.d.ts +8 -0
  32. package/dist/methods/deltaV2/isTokenSupportedInDelta.d.ts.map +1 -0
  33. package/dist/methods/deltaV2/{postDeltaOrderV2.d.ts → postDeltaOrder.d.ts} +9 -8
  34. package/dist/methods/deltaV2/postDeltaOrder.d.ts.map +1 -0
  35. package/dist/methods/deltaV2/postExternalDeltaOrder.d.ts +11 -0
  36. package/dist/methods/deltaV2/postExternalDeltaOrder.d.ts.map +1 -0
  37. package/dist/methods/deltaV2/postTWAPDeltaOrder.d.ts +17 -0
  38. package/dist/methods/deltaV2/postTWAPDeltaOrder.d.ts.map +1 -0
  39. package/dist/methods/deltaV2/types.d.ts +26 -20
  40. package/dist/methods/deltaV2/types.d.ts.map +1 -1
  41. package/dist/sdk/full.d.ts +1 -1
  42. package/dist/sdk/full.d.ts.map +1 -1
  43. package/dist/sdk/simple.d.ts +3 -9
  44. package/dist/sdk/simple.d.ts.map +1 -1
  45. package/dist/sdk.cjs.development.js +1600 -1298
  46. package/dist/sdk.cjs.development.js.map +1 -1
  47. package/dist/sdk.cjs.production.min.js +1 -1
  48. package/dist/sdk.cjs.production.min.js.map +1 -1
  49. package/dist/sdk.esm.js +1584 -1266
  50. package/dist/sdk.esm.js.map +1 -1
  51. package/package.json +1 -1
  52. package/src/constants.js +6 -1
  53. package/src/examples/deltaV2.ts +21 -25
  54. package/src/examples/helpers/delta.ts +6 -1
  55. package/src/examples/helpers/deltaV2.ts +9 -4
  56. package/src/index.js +167 -83
  57. package/src/index.ts +27 -143
  58. package/src/methods/delta/helpers/orders.ts +5 -1
  59. package/src/methods/delta/helpers/types.ts +19 -18
  60. package/src/methods/deltaV2/{buildDeltaOrderV2.ts → buildDeltaOrder.ts} +16 -16
  61. package/src/methods/deltaV2/{buildExternalDeltaOrderV2.ts → buildExternalDeltaOrder.ts} +16 -16
  62. package/src/methods/deltaV2/{buildTWAPDeltaOrderV2.ts → buildTWAPDeltaOrder.ts} +22 -22
  63. package/src/methods/deltaV2/{cancelDeltaOrderV2.ts → cancelDeltaOrder.ts} +17 -20
  64. package/src/methods/deltaV2/getAgentsList.ts +32 -0
  65. package/src/methods/deltaV2/getBridgeRoutes.ts +7 -7
  66. package/src/methods/deltaV2/getDeltaOrders.ts +149 -0
  67. package/src/methods/deltaV2/{getDeltaPriceV2.ts → getDeltaPrice.ts} +15 -15
  68. package/src/methods/deltaV2/helpers/orders.ts +363 -0
  69. package/src/methods/deltaV2/index.ts +148 -139
  70. package/src/methods/deltaV2/{isTokenSupportedInDeltaV2.ts → isTokenSupportedInDelta.ts} +7 -7
  71. package/src/methods/deltaV2/{postDeltaOrderV2.ts → postDeltaOrder.ts} +13 -16
  72. package/src/methods/deltaV2/postExternalDeltaOrder.ts +45 -0
  73. package/src/methods/deltaV2/{postTWAPDeltaOrderV2.ts → postTWAPDeltaOrder.ts} +14 -19
  74. package/src/methods/deltaV2/types.ts +35 -22
  75. package/src/sdk/full.ts +2 -2
  76. package/src/sdk/simple.ts +17 -43
  77. package/dist/methods/deltaV2/buildDeltaOrderV2.d.ts.map +0 -1
  78. package/dist/methods/deltaV2/buildExternalDeltaOrderV2.d.ts.map +0 -1
  79. package/dist/methods/deltaV2/buildTWAPDeltaOrderV2.d.ts.map +0 -1
  80. package/dist/methods/deltaV2/cancelDeltaOrderV2.d.ts +0 -21
  81. package/dist/methods/deltaV2/cancelDeltaOrderV2.d.ts.map +0 -1
  82. package/dist/methods/deltaV2/getAgentsListV2.d.ts +0 -10
  83. package/dist/methods/deltaV2/getAgentsListV2.d.ts.map +0 -1
  84. package/dist/methods/deltaV2/getDeltaOrdersV2.d.ts +0 -33
  85. package/dist/methods/deltaV2/getDeltaOrdersV2.d.ts.map +0 -1
  86. package/dist/methods/deltaV2/getDeltaPriceV2.d.ts.map +0 -1
  87. package/dist/methods/deltaV2/isTokenSupportedInDeltaV2.d.ts +0 -8
  88. package/dist/methods/deltaV2/isTokenSupportedInDeltaV2.d.ts.map +0 -1
  89. package/dist/methods/deltaV2/postDeltaOrderV2.d.ts.map +0 -1
  90. package/dist/methods/deltaV2/postExternalDeltaOrderV2.d.ts +0 -11
  91. package/dist/methods/deltaV2/postExternalDeltaOrderV2.d.ts.map +0 -1
  92. package/dist/methods/deltaV2/postTWAPDeltaOrderV2.d.ts +0 -16
  93. package/dist/methods/deltaV2/postTWAPDeltaOrderV2.d.ts.map +0 -1
  94. package/src/methods/deltaV2/getAgentsListV2.ts +0 -34
  95. package/src/methods/deltaV2/getDeltaOrdersV2.ts +0 -119
  96. package/src/methods/deltaV2/postExternalDeltaOrderV2.ts +0 -45
@@ -23,19 +23,19 @@ type GetBridgeRoutes = (
23
23
  requestParams?: RequestParameters
24
24
  ) => Promise<BridgeRoute[]>;
25
25
 
26
- type GetBridgeProtocolsV2 = (
26
+ type GetBridgeProtocols = (
27
27
  requestParams?: RequestParameters
28
28
  ) => Promise<BridgeProtocolResponse[]>;
29
29
 
30
- type BridgeProtocolsV2Response = {
30
+ type BridgeProtocolsResponse = {
31
31
  bridgeProtocols: BridgeProtocolResponse[];
32
32
  };
33
33
 
34
34
  export type GetBridgeRoutesFunctions = {
35
35
  /** @description Fetch supported bridge routes as a flat array (v2 replacement for bridge-info). */
36
36
  getBridgeRoutes: GetBridgeRoutes;
37
- /** @description Fetch supported bridge protocols (falls through to v1 controller on the v2 path). */
38
- getBridgeProtocolsV2: GetBridgeProtocolsV2;
37
+ /** @description Fetch supported bridge protocols (unchanged from v1). */
38
+ getBridgeProtocols: GetBridgeProtocols;
39
39
  };
40
40
 
41
41
  export const constructGetBridgeRoutes = ({
@@ -66,10 +66,10 @@ export const constructGetBridgeRoutes = ({
66
66
  return data.routes;
67
67
  };
68
68
 
69
- const getBridgeProtocolsV2: GetBridgeProtocolsV2 = async (requestParams) => {
69
+ const getBridgeProtocols: GetBridgeProtocols = async (requestParams) => {
70
70
  const fetchURL = `${deltaPricesUrl}/bridge-protocols` as const;
71
71
 
72
- const data = await fetcher<BridgeProtocolsV2Response>({
72
+ const data = await fetcher<BridgeProtocolsResponse>({
73
73
  url: fetchURL,
74
74
  method: 'GET',
75
75
  requestParams,
@@ -78,5 +78,5 @@ export const constructGetBridgeRoutes = ({
78
78
  return data.bridgeProtocols;
79
79
  };
80
80
 
81
- return { getBridgeRoutes, getBridgeProtocolsV2 };
81
+ return { getBridgeRoutes, getBridgeProtocols };
82
82
  };
@@ -0,0 +1,149 @@
1
+ import { API_URL } from '../../constants';
2
+ import { constructSearchString } from '../../helpers/misc';
3
+ import type {
4
+ Address,
5
+ ConstructFetchInput,
6
+ PaginatedResponse,
7
+ RequestParameters,
8
+ } from '../../types';
9
+ import type { DeltaOrderType, OnChainOrderType } from '../delta/helpers/types';
10
+ import type { DeltaOrderStatus, DeltaAuction } from './types';
11
+
12
+ type GetDeltaOrderById = (
13
+ orderId: string,
14
+ requestParams?: RequestParameters
15
+ ) => Promise<DeltaAuction>;
16
+
17
+ type GetDeltaOrderByHash = (
18
+ orderHash: string,
19
+ requestParams?: RequestParameters
20
+ ) => Promise<DeltaAuction>;
21
+
22
+ type OrdersFilter = {
23
+ /** @description `order.owner` to fetch Delta Orders for. */
24
+ userAddress: Address;
25
+ /** @description Pagination option. Default 1. */
26
+ page?: number;
27
+ /** @description Pagination option. Default 100, max 1000. */
28
+ limit?: number;
29
+ /** @description Filter by chainId. Omitted = orders across all chains. */
30
+ chainId?: number[];
31
+ /** @description Filter by integrator-facing status. */
32
+ status?: DeltaOrderStatus[];
33
+ /** @description Filter by order type. MARKET or LIMIT. */
34
+ type?: DeltaOrderType;
35
+ /** @description Filter by on-chain order type. */
36
+ onChainOrderType?: OnChainOrderType;
37
+ };
38
+
39
+ type OrderFiltersQuery = Omit<OrdersFilter, 'chainId' | 'status'> & {
40
+ chainId?: string;
41
+ status?: string;
42
+ };
43
+
44
+ type GetDeltaOrders = (
45
+ options: OrdersFilter,
46
+ requestParams?: RequestParameters
47
+ ) => Promise<PaginatedResponse<DeltaAuction>>;
48
+
49
+ type GetRequiredBalanceParams = {
50
+ userAddress: Address;
51
+ tokenAddress?: Address;
52
+ };
53
+
54
+ type GetRequiredBalance = (
55
+ userParams: GetRequiredBalanceParams,
56
+ requestParams?: RequestParameters
57
+ ) => Promise<Record<string, string>>; // token -> required balance across open Delta orders
58
+
59
+ export type GetDeltaOrdersFunctions = {
60
+ /** @description Fetch a single order by its UUID. */
61
+ getDeltaOrderById: GetDeltaOrderById;
62
+ /** @description Fetch a single order by its EIP-712 order hash. */
63
+ getDeltaOrderByHash: GetDeltaOrderByHash;
64
+ /** @description List Delta orders with the v2 pagination envelope. */
65
+ getDeltaOrders: GetDeltaOrders;
66
+ /** @description Required balance per token across the user's open Delta v2 orders. Pass `tokenAddress` to narrow the result to a single token. */
67
+ getRequiredBalanceForDeltaOrders: GetRequiredBalance;
68
+ };
69
+
70
+ export const constructGetDeltaOrders = ({
71
+ apiURL = API_URL,
72
+ fetcher,
73
+ chainId,
74
+ }: ConstructFetchInput): GetDeltaOrdersFunctions => {
75
+ const baseUrl = `${apiURL}/delta/v2/orders` as const;
76
+
77
+ const getDeltaOrderById: GetDeltaOrderById = async (
78
+ orderId,
79
+ requestParams
80
+ ) => {
81
+ const fetchURL = `${baseUrl}/${orderId}` as const;
82
+ return fetcher<DeltaAuction>({
83
+ url: fetchURL,
84
+ method: 'GET',
85
+ requestParams,
86
+ });
87
+ };
88
+
89
+ const getDeltaOrderByHash: GetDeltaOrderByHash = async (
90
+ orderHash,
91
+ requestParams
92
+ ) => {
93
+ const fetchURL = `${baseUrl}/hash/${orderHash}` as const;
94
+ return fetcher<DeltaAuction>({
95
+ url: fetchURL,
96
+ method: 'GET',
97
+ requestParams,
98
+ });
99
+ };
100
+
101
+ const getDeltaOrders: GetDeltaOrders = async (options, requestParams) => {
102
+ const chainIdString = options.chainId?.join(',');
103
+ const statusString = options.status?.join(',');
104
+
105
+ const search = constructSearchString<OrderFiltersQuery>({
106
+ userAddress: options.userAddress,
107
+ page: options.page,
108
+ limit: options.limit,
109
+ type: options.type,
110
+ onChainOrderType: options.onChainOrderType,
111
+ chainId: chainIdString,
112
+ status: statusString,
113
+ });
114
+
115
+ const fetchURL = `${baseUrl}${search}` as const;
116
+
117
+ return fetcher<PaginatedResponse<DeltaAuction>>({
118
+ url: fetchURL,
119
+ method: 'GET',
120
+ requestParams,
121
+ });
122
+ };
123
+
124
+ const getRequiredBalanceForDeltaOrders: GetRequiredBalance = async (
125
+ userParams,
126
+ requestParams
127
+ ) => {
128
+ const userURL =
129
+ `${baseUrl}/fillablebalance/${chainId}/${userParams.userAddress}` as const;
130
+ const fetchURL = userParams.tokenAddress
131
+ ? (`${userURL}/${userParams.tokenAddress}` as const)
132
+ : userURL;
133
+
134
+ const response = await fetcher<Record<string, string>>({
135
+ url: fetchURL,
136
+ method: 'GET',
137
+ requestParams,
138
+ });
139
+
140
+ return response;
141
+ };
142
+
143
+ return {
144
+ getDeltaOrderById,
145
+ getDeltaOrderByHash,
146
+ getDeltaOrders,
147
+ getRequiredBalanceForDeltaOrders,
148
+ };
149
+ };
@@ -5,11 +5,11 @@ import type {
5
5
  EnumerateLiteral,
6
6
  RequestParameters,
7
7
  } from '../../types';
8
- import type { DeltaPriceV2 } from './types';
8
+ import type { DeltaPrice } from './types';
9
9
 
10
10
  type SwapSideUnion = EnumerateLiteral<typeof SwapSide>;
11
11
 
12
- export type DeltaPriceV2Params = {
12
+ export type DeltaPriceParams = {
13
13
  /** @description Source Token Address */
14
14
  srcToken: string;
15
15
  /** @description Destination Token Address. For Crosschain Orders, the destination token on the destination chain */
@@ -46,8 +46,8 @@ export type DeltaPriceV2Params = {
46
46
  degenMode?: boolean;
47
47
  };
48
48
 
49
- type DeltaPriceV2QueryOptions = Omit<
50
- DeltaPriceV2Params,
49
+ type DeltaPriceQueryOptions = Omit<
50
+ DeltaPriceParams,
51
51
  'includeAgents' | 'excludeAgents' | 'includeBridges' | 'excludeBridges'
52
52
  > & {
53
53
  chainId: number;
@@ -57,24 +57,24 @@ type DeltaPriceV2QueryOptions = Omit<
57
57
  excludeBridges?: string;
58
58
  };
59
59
 
60
- type GetDeltaPriceV2 = (
61
- options: DeltaPriceV2Params,
60
+ type GetDeltaPrice = (
61
+ options: DeltaPriceParams,
62
62
  requestParams?: RequestParameters
63
- ) => Promise<DeltaPriceV2>;
63
+ ) => Promise<DeltaPrice>;
64
64
 
65
- export type GetDeltaPriceV2Functions = {
65
+ export type GetDeltaPriceFunctions = {
66
66
  /** @description Fetch a v2 price quote (route-based response). */
67
- getDeltaPriceV2: GetDeltaPriceV2;
67
+ getDeltaPrice: GetDeltaPrice;
68
68
  };
69
69
 
70
- export const constructGetDeltaPriceV2 = ({
70
+ export const constructGetDeltaPrice = ({
71
71
  apiURL = API_URL,
72
72
  chainId,
73
73
  fetcher,
74
- }: ConstructFetchInput): GetDeltaPriceV2Functions => {
74
+ }: ConstructFetchInput): GetDeltaPriceFunctions => {
75
75
  const pricesUrl = `${apiURL}/delta/v2/prices` as const;
76
76
 
77
- const getDeltaPriceV2: GetDeltaPriceV2 = async (options, requestParams) => {
77
+ const getDeltaPrice: GetDeltaPrice = async (options, requestParams) => {
78
78
  const {
79
79
  includeAgents,
80
80
  excludeAgents,
@@ -83,7 +83,7 @@ export const constructGetDeltaPriceV2 = ({
83
83
  ...rest
84
84
  } = options;
85
85
 
86
- const search = constructSearchString<DeltaPriceV2QueryOptions>({
86
+ const search = constructSearchString<DeltaPriceQueryOptions>({
87
87
  ...rest,
88
88
  chainId,
89
89
  side: options.side ?? SwapSide.SELL,
@@ -95,7 +95,7 @@ export const constructGetDeltaPriceV2 = ({
95
95
 
96
96
  const fetchURL = `${pricesUrl}${search}` as const;
97
97
 
98
- const data = await fetcher<DeltaPriceV2>({
98
+ const data = await fetcher<DeltaPrice>({
99
99
  url: fetchURL,
100
100
  method: 'GET',
101
101
  requestParams,
@@ -104,5 +104,5 @@ export const constructGetDeltaPriceV2 = ({
104
104
  return data;
105
105
  };
106
106
 
107
- return { getDeltaPriceV2 };
107
+ return { getDeltaPrice };
108
108
  };
@@ -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
+ };