@velora-dex/sdk 9.4.0 → 9.4.1-dev.1

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 (39) hide show
  1. package/dist/index.d.ts +11 -5
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/methods/delta/buildTWAPDeltaOrder.d.ts +65 -0
  4. package/dist/methods/delta/buildTWAPDeltaOrder.d.ts.map +1 -0
  5. package/dist/methods/delta/helpers/buildTWAPOrderData.d.ts +74 -0
  6. package/dist/methods/delta/helpers/buildTWAPOrderData.d.ts.map +1 -0
  7. package/dist/methods/delta/helpers/misc.d.ts +8 -0
  8. package/dist/methods/delta/helpers/misc.d.ts.map +1 -1
  9. package/dist/methods/delta/helpers/types.d.ts +43 -2
  10. package/dist/methods/delta/helpers/types.d.ts.map +1 -1
  11. package/dist/methods/delta/index.d.ts +16 -1
  12. package/dist/methods/delta/index.d.ts.map +1 -1
  13. package/dist/methods/delta/postTWAPDeltaOrder.d.ts +16 -0
  14. package/dist/methods/delta/postTWAPDeltaOrder.d.ts.map +1 -0
  15. package/dist/methods/delta/preSignTWAPDeltaOrder.d.ts +20 -0
  16. package/dist/methods/delta/preSignTWAPDeltaOrder.d.ts.map +1 -0
  17. package/dist/methods/delta/signTWAPDeltaOrder.d.ts +9 -0
  18. package/dist/methods/delta/signTWAPDeltaOrder.d.ts.map +1 -0
  19. package/dist/sdk/partial.d.ts +3 -1
  20. package/dist/sdk/partial.d.ts.map +1 -1
  21. package/dist/sdk.cjs.development.js +610 -25
  22. package/dist/sdk.cjs.development.js.map +1 -1
  23. package/dist/sdk.cjs.production.min.js +1 -1
  24. package/dist/sdk.cjs.production.min.js.map +1 -1
  25. package/dist/sdk.esm.js +606 -26
  26. package/dist/sdk.esm.js.map +1 -1
  27. package/package.json +1 -1
  28. package/src/constants.js +1 -6
  29. package/src/index.js +68 -125
  30. package/src/index.ts +63 -0
  31. package/src/methods/delta/buildTWAPDeltaOrder.ts +181 -0
  32. package/src/methods/delta/helpers/buildTWAPOrderData.ts +227 -0
  33. package/src/methods/delta/helpers/misc.ts +35 -1
  34. package/src/methods/delta/helpers/types.ts +52 -2
  35. package/src/methods/delta/index.ts +78 -1
  36. package/src/methods/delta/postTWAPDeltaOrder.ts +57 -0
  37. package/src/methods/delta/preSignTWAPDeltaOrder.ts +168 -0
  38. package/src/methods/delta/signTWAPDeltaOrder.ts +32 -0
  39. package/src/sdk/partial.ts +3 -1
@@ -0,0 +1,227 @@
1
+ import { Domain } from '../../common/orders/buildOrderData';
2
+ import {
3
+ Bridge,
4
+ TWAPDeltaOrder,
5
+ TWAPBuyDeltaOrder,
6
+ TWAPOnChainOrderType,
7
+ OnChainOrderMap,
8
+ } from './types';
9
+ import { DELTA_DEFAULT_EXPIRY, producePartnerAndFee } from './misc';
10
+
11
+ const BRIDGE_EIP_712_TYPE = [
12
+ { name: 'protocolSelector', type: 'bytes4' },
13
+ { name: 'destinationChainId', type: 'uint256' },
14
+ { name: 'outputToken', type: 'address' },
15
+ { name: 'scalingFactor', type: 'int8' },
16
+ { name: 'protocolData', type: 'bytes' },
17
+ ];
18
+
19
+ const TWAP_ORDER_EIP_712_TYPES = {
20
+ TWAPOrder: [
21
+ { name: 'owner', type: 'address' },
22
+ { name: 'beneficiary', type: 'address' },
23
+ { name: 'srcToken', type: 'address' },
24
+ { name: 'destToken', type: 'address' },
25
+ { name: 'nonce', type: 'uint256' },
26
+ { name: 'partnerAndFee', type: 'uint256' },
27
+ { name: 'deadline', type: 'uint64' },
28
+ { name: 'interval', type: 'uint64' },
29
+ { name: 'numSlices', type: 'uint32' },
30
+ { name: 'destAmountPerSlice', type: 'uint256' },
31
+ { name: 'totalSrcAmount', type: 'uint256' },
32
+ { name: 'permit', type: 'bytes' },
33
+ { name: 'metadata', type: 'bytes' },
34
+ { name: 'bridge', type: 'Bridge' },
35
+ ],
36
+ Bridge: BRIDGE_EIP_712_TYPE,
37
+ };
38
+
39
+ const TWAP_BUY_ORDER_EIP_712_TYPES = {
40
+ TWAPBuyOrder: [
41
+ { name: 'owner', type: 'address' },
42
+ { name: 'beneficiary', type: 'address' },
43
+ { name: 'srcToken', type: 'address' },
44
+ { name: 'destToken', type: 'address' },
45
+ { name: 'nonce', type: 'uint256' },
46
+ { name: 'partnerAndFee', type: 'uint256' },
47
+ { name: 'deadline', type: 'uint64' },
48
+ { name: 'interval', type: 'uint64' },
49
+ { name: 'numSlices', type: 'uint32' },
50
+ { name: 'totalDestAmount', type: 'uint256' },
51
+ { name: 'maxSrcAmount', type: 'uint256' },
52
+ { name: 'permit', type: 'bytes' },
53
+ { name: 'metadata', type: 'bytes' },
54
+ { name: 'bridge', type: 'Bridge' },
55
+ ],
56
+ Bridge: BRIDGE_EIP_712_TYPE,
57
+ };
58
+
59
+ export type SignableTWAPSellOrderData = {
60
+ types: typeof TWAP_ORDER_EIP_712_TYPES;
61
+ domain: Domain;
62
+ data: TWAPDeltaOrder;
63
+ };
64
+
65
+ export type SignableTWAPBuyOrderData = {
66
+ types: typeof TWAP_BUY_ORDER_EIP_712_TYPES;
67
+ domain: Domain;
68
+ data: TWAPBuyDeltaOrder;
69
+ };
70
+
71
+ export type SignableTWAPOrderData =
72
+ | SignableTWAPSellOrderData
73
+ | SignableTWAPBuyOrderData;
74
+
75
+ type ProduceTWAPOrderTypedDataInput<T extends TWAPOnChainOrderType> = {
76
+ orderInput: OnChainOrderMap[T];
77
+ paraswapDeltaAddress: string;
78
+ chainId: number;
79
+ onChainOrderType: T;
80
+ };
81
+
82
+ export function produceTWAPOrderTypedData(
83
+ params: ProduceTWAPOrderTypedDataInput<'TWAPOrder'>
84
+ ): SignableTWAPSellOrderData;
85
+ export function produceTWAPOrderTypedData(
86
+ params: ProduceTWAPOrderTypedDataInput<'TWAPBuyOrder'>
87
+ ): SignableTWAPBuyOrderData;
88
+ export function produceTWAPOrderTypedData({
89
+ orderInput,
90
+ chainId,
91
+ paraswapDeltaAddress,
92
+ onChainOrderType,
93
+ }: ProduceTWAPOrderTypedDataInput<TWAPOnChainOrderType>) {
94
+ const domain: Domain = {
95
+ name: 'Portikus',
96
+ version: '2.0.0',
97
+ chainId,
98
+ verifyingContract: paraswapDeltaAddress,
99
+ };
100
+
101
+ if (onChainOrderType === 'TWAPOrder') {
102
+ return {
103
+ types: TWAP_ORDER_EIP_712_TYPES,
104
+ domain,
105
+ data: orderInput,
106
+ };
107
+ }
108
+
109
+ return {
110
+ types: TWAP_BUY_ORDER_EIP_712_TYPES,
111
+ domain,
112
+ data: orderInput,
113
+ };
114
+ }
115
+
116
+ type TWAPOrderCommonInput = {
117
+ owner: string;
118
+ beneficiary?: string;
119
+ srcToken: string;
120
+ destToken: string;
121
+ nonce?: string;
122
+ deadline?: number;
123
+ permit?: string;
124
+ metadata?: string;
125
+ interval: number;
126
+ numSlices: number;
127
+ bridge: Bridge;
128
+
129
+ partnerAddress: string;
130
+ paraswapDeltaAddress: string;
131
+ partnerFeeBps: number;
132
+ partnerTakesSurplus?: boolean;
133
+ capSurplus?: boolean;
134
+ chainId: number;
135
+ };
136
+
137
+ export type BuildTWAPSellOrderDataInput = TWAPOrderCommonInput & {
138
+ onChainOrderType: 'TWAPOrder';
139
+ destAmountPerSlice: string;
140
+ totalSrcAmount: string;
141
+ };
142
+
143
+ export type BuildTWAPBuyOrderDataInput = TWAPOrderCommonInput & {
144
+ onChainOrderType: 'TWAPBuyOrder';
145
+ totalDestAmount: string;
146
+ maxSrcAmount: string;
147
+ };
148
+
149
+ export type BuildTWAPOrderDataInput =
150
+ | BuildTWAPSellOrderDataInput
151
+ | BuildTWAPBuyOrderDataInput;
152
+
153
+ export function buildTWAPSignableOrderData(
154
+ input: BuildTWAPOrderDataInput
155
+ ): SignableTWAPOrderData {
156
+ const {
157
+ owner,
158
+ beneficiary = owner,
159
+ srcToken,
160
+ destToken,
161
+ nonce = Date.now().toString(10),
162
+ deadline = Math.floor(Date.now() / 1000 + DELTA_DEFAULT_EXPIRY),
163
+ permit = '0x',
164
+ metadata = '0x',
165
+ interval,
166
+ numSlices,
167
+ bridge,
168
+
169
+ partnerAddress,
170
+ partnerFeeBps,
171
+ partnerTakesSurplus = false,
172
+ capSurplus = true,
173
+ chainId,
174
+ paraswapDeltaAddress,
175
+ onChainOrderType,
176
+ } = input;
177
+
178
+ const partnerAndFee = producePartnerAndFee({
179
+ partnerFeeBps,
180
+ partnerAddress,
181
+ partnerTakesSurplus,
182
+ capSurplus,
183
+ });
184
+
185
+ const commonFields = {
186
+ owner,
187
+ beneficiary,
188
+ srcToken,
189
+ destToken,
190
+ nonce,
191
+ partnerAndFee,
192
+ deadline,
193
+ interval,
194
+ numSlices,
195
+ permit,
196
+ metadata,
197
+ bridge,
198
+ };
199
+
200
+ if (onChainOrderType === 'TWAPOrder') {
201
+ const orderInput: TWAPDeltaOrder = {
202
+ ...commonFields,
203
+ destAmountPerSlice: input.destAmountPerSlice,
204
+ totalSrcAmount: input.totalSrcAmount,
205
+ };
206
+
207
+ return produceTWAPOrderTypedData({
208
+ orderInput,
209
+ chainId,
210
+ paraswapDeltaAddress,
211
+ onChainOrderType: 'TWAPOrder',
212
+ });
213
+ }
214
+
215
+ const orderInput: TWAPBuyDeltaOrder = {
216
+ ...commonFields,
217
+ totalDestAmount: input.totalDestAmount,
218
+ maxSrcAmount: input.maxSrcAmount,
219
+ };
220
+
221
+ return produceTWAPOrderTypedData({
222
+ orderInput,
223
+ chainId,
224
+ paraswapDeltaAddress,
225
+ onChainOrderType: 'TWAPBuyOrder',
226
+ });
227
+ }
@@ -1,6 +1,7 @@
1
1
  import { ZERO_ADDRESS } from '../../common/orders/buildOrderData';
2
2
  import type { SignableDeltaOrderData } from './buildDeltaOrderData';
3
3
  import type { SignableExternalOrderData } from './buildExternalOrderData';
4
+ import type { SignableTWAPOrderData } from './buildTWAPOrderData';
4
5
  import type { GetPartnerFeeFunctions } from '../getPartnerFee';
5
6
  import type { RequestParameters } from '../../../types';
6
7
  import type { AmountsWithSlippage, SwapSideUnion } from './types';
@@ -44,7 +45,7 @@ type ApplySlippageInput = {
44
45
  increase: boolean;
45
46
  };
46
47
 
47
- function applySlippage({
48
+ export function applySlippage({
48
49
  amount,
49
50
  slippageBps,
50
51
  increase,
@@ -244,3 +245,36 @@ export function sanitizeExternalOrderData({
244
245
  data,
245
246
  };
246
247
  }
248
+
249
+ export function sanitizeTWAPOrderData(
250
+ orderData: SignableTWAPOrderData['data']
251
+ ): SignableTWAPOrderData['data'] {
252
+ const common = {
253
+ owner: orderData.owner,
254
+ beneficiary: orderData.beneficiary,
255
+ srcToken: orderData.srcToken,
256
+ destToken: orderData.destToken,
257
+ nonce: orderData.nonce,
258
+ partnerAndFee: orderData.partnerAndFee,
259
+ deadline: orderData.deadline,
260
+ interval: orderData.interval,
261
+ numSlices: orderData.numSlices,
262
+ permit: orderData.permit,
263
+ metadata: orderData.metadata,
264
+ bridge: orderData.bridge,
265
+ };
266
+
267
+ if ('destAmountPerSlice' in orderData) {
268
+ return {
269
+ ...common,
270
+ destAmountPerSlice: orderData.destAmountPerSlice,
271
+ totalSrcAmount: orderData.totalSrcAmount,
272
+ };
273
+ }
274
+
275
+ return {
276
+ ...common,
277
+ totalDestAmount: orderData.totalDestAmount,
278
+ maxSrcAmount: orderData.maxSrcAmount,
279
+ };
280
+ }
@@ -122,6 +122,48 @@ export type ExternalDeltaOrder = {
122
122
  data: string;
123
123
  };
124
124
 
125
+ type TWAPDeltaOrderBase = {
126
+ /** @description The address of the order owner */
127
+ owner: string;
128
+ /** @description The address of the order beneficiary */
129
+ beneficiary: string; // beneficiary==owner if no transferTo
130
+ /** @description The address of the src token */
131
+ srcToken: string; // lowercase
132
+ /** @description The address of the dest token */
133
+ destToken: string; // lowercase
134
+ /** @description The nonce of the order */
135
+ nonce: string; // can be random, can even be Date.now()
136
+ /** @description Encoded partner address, fee bps, and flags for the order. partnerAndFee = (partner << 96) | (partnerTakesSurplus << 8) | fee in bps (max fee is 2%) */
137
+ partnerAndFee: string;
138
+ /** @description The deadline for the order */
139
+ deadline: number; // seconds
140
+ /** @description The interval between each slice execution */
141
+ interval: number; // seconds
142
+ /** @description The number of slices to execute */
143
+ numSlices: number;
144
+ /** @description Optional permit signature for the src token */
145
+ permit: string; //can be "0x"
146
+ /** @description Metadata for the order, hex string */
147
+ metadata: string;
148
+ /** @description The bridge input */
149
+ bridge: Bridge;
150
+ };
151
+
152
+ export type TWAPDeltaOrder = TWAPDeltaOrderBase & {
153
+ /** @description The amount of dest token to receive per slice */
154
+ destAmountPerSlice: string; // wei
155
+ /** @description The total amount of src token to swap */
156
+ totalSrcAmount: string; // wei
157
+ /** @description Optional permit signature for the src token */
158
+ };
159
+
160
+ export type TWAPBuyDeltaOrder = TWAPDeltaOrderBase & {
161
+ /** @description The total amount of dest token to buy across all slices */
162
+ totalDestAmount: string; // wei
163
+ /** @description The maximum amount of src token willing to spend */
164
+ maxSrcAmount: string; // wei
165
+ };
166
+
125
167
  export type DeltaAuctionStatus =
126
168
  | 'NOT_STARTED'
127
169
  | 'AWAITING_PRE_SIGNATURE'
@@ -135,7 +177,7 @@ export type DeltaAuctionStatus =
135
177
  | 'SUSPENDED'
136
178
  | 'REFUNDED';
137
179
 
138
- type DeltaAuctionTransaction = {
180
+ export type DeltaAuctionTransaction = {
139
181
  id: string;
140
182
  hash: string;
141
183
  orderId: string;
@@ -165,6 +207,8 @@ type DeltaAuctionTransaction = {
165
207
  export type OnChainOrderMap = {
166
208
  Order: DeltaAuctionOrder;
167
209
  ExternalOrder: ExternalDeltaOrder;
210
+ TWAPOrder: TWAPDeltaOrder;
211
+ TWAPBuyOrder: TWAPBuyDeltaOrder;
168
212
  };
169
213
 
170
214
  type DeltaAuctionBase = {
@@ -215,7 +259,13 @@ export type BridgeMetadata = {
215
259
  // refunded is basically failed
216
260
  export type BridgeStatus = 'pending' | 'filled' | 'expired' | 'refunded';
217
261
 
218
- export type OnChainOrderType = 'Order' | 'ExternalOrder';
262
+ export type OnChainOrderType =
263
+ | 'Order'
264
+ | 'ExternalOrder'
265
+ | 'TWAPOrder'
266
+ | 'TWAPBuyOrder';
267
+
268
+ export type TWAPOnChainOrderType = 'TWAPOrder' | 'TWAPBuyOrder';
219
269
 
220
270
  //// available on BridgePrice ////
221
271
 
@@ -71,6 +71,23 @@ import {
71
71
  constructPreSignExternalDeltaOrder,
72
72
  PreSignExternalDeltaOrderFunctions,
73
73
  } from './preSignExternalDeltaOrder';
74
+ import {
75
+ BuildTWAPDeltaOrderParams,
76
+ BuildTWAPDeltaOrderFunctions,
77
+ constructBuildTWAPDeltaOrder,
78
+ } from './buildTWAPDeltaOrder';
79
+ import {
80
+ constructSignTWAPDeltaOrder,
81
+ SignTWAPDeltaOrderFunctions,
82
+ } from './signTWAPDeltaOrder';
83
+ import {
84
+ constructPostTWAPDeltaOrder,
85
+ PostTWAPDeltaOrderFunctions,
86
+ } from './postTWAPDeltaOrder';
87
+ import {
88
+ constructPreSignTWAPDeltaOrder,
89
+ PreSignTWAPDeltaOrderFunctions,
90
+ } from './preSignTWAPDeltaOrder';
74
91
 
75
92
  export type SubmitDeltaOrderParams = BuildDeltaOrderDataParams & {
76
93
  /** @description designates the Order as being able to be partially filled, as opposed to fill-or-kill */
@@ -163,6 +180,50 @@ export const constructSubmitExternalDeltaOrder = (
163
180
  return { submitExternalDeltaOrder };
164
181
  };
165
182
 
183
+ export type SubmitTWAPDeltaOrderParams = BuildTWAPDeltaOrderParams & {
184
+ /** @description designates the Order as being able to be partially filled, as opposed to fill-or-kill */
185
+ partiallyFillable?: boolean;
186
+ /** @description Referrer address */
187
+ referrerAddress?: string;
188
+ } & Pick<DeltaOrderToPost, 'type' | 'includeAgents' | 'excludeAgents'>;
189
+
190
+ type SubmitTWAPDeltaOrder = (
191
+ orderParams: SubmitTWAPDeltaOrderParams
192
+ ) => Promise<DeltaAuction<'TWAPOrder'> | DeltaAuction<'TWAPBuyOrder'>>;
193
+
194
+ export type SubmitTWAPDeltaOrderFuncs = {
195
+ submitTWAPDeltaOrder: SubmitTWAPDeltaOrder;
196
+ };
197
+
198
+ export const constructSubmitTWAPDeltaOrder = (
199
+ options: ConstructProviderFetchInput<any, 'signTypedDataCall'>
200
+ ): SubmitTWAPDeltaOrderFuncs => {
201
+ const { buildTWAPDeltaOrder } = constructBuildTWAPDeltaOrder(options);
202
+ const { signTWAPDeltaOrder } = constructSignTWAPDeltaOrder(options);
203
+ const { postTWAPDeltaOrder } = constructPostTWAPDeltaOrder(options);
204
+
205
+ const submitTWAPDeltaOrder: SubmitTWAPDeltaOrder = async (orderParams) => {
206
+ const orderData = await buildTWAPDeltaOrder(orderParams);
207
+ const signature = await signTWAPDeltaOrder(orderData);
208
+
209
+ const response = await postTWAPDeltaOrder({
210
+ signature,
211
+ partner: orderParams.partner,
212
+ order: orderData.data,
213
+ onChainOrderType: orderParams.onChainOrderType,
214
+ partiallyFillable: orderParams.partiallyFillable,
215
+ referrerAddress: orderParams.referrerAddress,
216
+ type: orderParams.type,
217
+ includeAgents: orderParams.includeAgents,
218
+ excludeAgents: orderParams.excludeAgents,
219
+ });
220
+
221
+ return response;
222
+ };
223
+
224
+ return { submitTWAPDeltaOrder };
225
+ };
226
+
166
227
  export type DeltaOrderHandlers<T> = SubmitDeltaOrderFuncs &
167
228
  ApproveTokenForDeltaFunctions<T> &
168
229
  BuildDeltaOrderFunctions &
@@ -181,7 +242,12 @@ export type DeltaOrderHandlers<T> = SubmitDeltaOrderFuncs &
181
242
  BuildExternalDeltaOrderFunctions &
182
243
  SignExternalDeltaOrderFunctions &
183
244
  PostExternalDeltaOrderFunctions &
184
- PreSignExternalDeltaOrderFunctions<T>;
245
+ PreSignExternalDeltaOrderFunctions<T> &
246
+ SubmitTWAPDeltaOrderFuncs &
247
+ BuildTWAPDeltaOrderFunctions &
248
+ SignTWAPDeltaOrderFunctions &
249
+ PostTWAPDeltaOrderFunctions &
250
+ PreSignTWAPDeltaOrderFunctions<T>;
185
251
 
186
252
  /** @description construct SDK with every Delta Order-related method, fetching from API and Order signing */
187
253
  export const constructAllDeltaOrdersHandlers = <TxResponse>(
@@ -218,6 +284,12 @@ export const constructAllDeltaOrdersHandlers = <TxResponse>(
218
284
  const externalDeltaOrdersPreSign =
219
285
  constructPreSignExternalDeltaOrder(options);
220
286
 
287
+ const twapDeltaOrdersSubmit = constructSubmitTWAPDeltaOrder(options);
288
+ const twapDeltaOrdersBuild = constructBuildTWAPDeltaOrder(options);
289
+ const twapDeltaOrdersSign = constructSignTWAPDeltaOrder(options);
290
+ const twapDeltaOrdersPost = constructPostTWAPDeltaOrder(options);
291
+ const twapDeltaOrdersPreSign = constructPreSignTWAPDeltaOrder(options);
292
+
221
293
  return {
222
294
  ...deltaOrdersGetters,
223
295
  ...deltaOrdersContractGetter,
@@ -238,5 +310,10 @@ export const constructAllDeltaOrdersHandlers = <TxResponse>(
238
310
  ...externalDeltaOrdersSign,
239
311
  ...externalDeltaOrdersPost,
240
312
  ...externalDeltaOrdersPreSign,
313
+ ...twapDeltaOrdersSubmit,
314
+ ...twapDeltaOrdersBuild,
315
+ ...twapDeltaOrdersSign,
316
+ ...twapDeltaOrdersPost,
317
+ ...twapDeltaOrdersPreSign,
241
318
  };
242
319
  };
@@ -0,0 +1,57 @@
1
+ import { Prettify } from 'ts-essentials';
2
+ import { API_URL } from '../../constants';
3
+ import type { ConstructFetchInput, RequestParameters } from '../../types';
4
+ import type { DeltaAuction, TWAPOnChainOrderType } from './helpers/types';
5
+ import type { DeltaOrderToPost } from './postDeltaOrder';
6
+ import { constructSearchString } from '../../helpers/misc';
7
+
8
+ export type PostTWAPDeltaOrderParams = Prettify<
9
+ Omit<
10
+ DeltaOrderToPost<'TWAPOrder'> | DeltaOrderToPost<'TWAPBuyOrder'>,
11
+ 'chainId'
12
+ > & {
13
+ /** @description Must be "TWAPOrder" or "TWAPBuyOrder" */
14
+ onChainOrderType: TWAPOnChainOrderType;
15
+ degenMode?: boolean;
16
+ }
17
+ >;
18
+
19
+ type PostTWAPDeltaOrder = (
20
+ postData: PostTWAPDeltaOrderParams,
21
+ requestParams?: RequestParameters
22
+ ) => Promise<DeltaAuction<'TWAPOrder'> | DeltaAuction<'TWAPBuyOrder'>>;
23
+
24
+ export type PostTWAPDeltaOrderFunctions = {
25
+ postTWAPDeltaOrder: PostTWAPDeltaOrder;
26
+ };
27
+
28
+ export const constructPostTWAPDeltaOrder = ({
29
+ apiURL = API_URL,
30
+ chainId,
31
+ fetcher,
32
+ }: ConstructFetchInput): PostTWAPDeltaOrderFunctions => {
33
+ const postOrderUrl = `${apiURL}/delta/orders` as const;
34
+
35
+ const postTWAPDeltaOrder: PostTWAPDeltaOrder = (_postData, requestParams) => {
36
+ const { degenMode, ...postData } = _postData;
37
+ const deltaOrderToPost: DeltaOrderToPost<'TWAPOrder' | 'TWAPBuyOrder'> = {
38
+ ...postData,
39
+ chainId,
40
+ };
41
+
42
+ const search = constructSearchString<{ degenMode?: boolean }>({
43
+ degenMode,
44
+ });
45
+
46
+ const fetchURL = `${postOrderUrl}/${search}` as const;
47
+
48
+ return fetcher<DeltaAuction<'TWAPOrder'> | DeltaAuction<'TWAPBuyOrder'>>({
49
+ url: fetchURL,
50
+ method: 'POST',
51
+ data: deltaOrderToPost,
52
+ requestParams,
53
+ });
54
+ };
55
+
56
+ return { postTWAPDeltaOrder };
57
+ };
@@ -0,0 +1,168 @@
1
+ import { hashTypedData } from 'viem/utils';
2
+ import type {
3
+ ConstructProviderFetchInput,
4
+ RequestParameters,
5
+ TxSendOverrides,
6
+ } from '../../types';
7
+ import {
8
+ produceTWAPOrderTypedData,
9
+ SignableTWAPOrderData,
10
+ } from './helpers/buildTWAPOrderData';
11
+ import { sanitizeTWAPOrderData } from './helpers/misc';
12
+ import { PreSignatureModuleAbi } from './helpers/abi';
13
+ import type { ExtractAbiMethodNames } from '../../helpers/misc';
14
+ import { findPrimaryType } from '../../helpers/providers/helpers';
15
+ import { constructGetDeltaContract } from './getDeltaContract';
16
+ import type { TWAPDeltaOrder, TWAPBuyDeltaOrder } from './helpers/types';
17
+ import { TypedDataField } from 'ethersV5';
18
+
19
+ type HashTWAPDeltaOrderTypedData = (
20
+ signableOrderData: SignableTWAPOrderData
21
+ ) => string;
22
+
23
+ type HashTWAPDeltaOrder = {
24
+ (
25
+ orderData: TWAPDeltaOrder,
26
+ onChainOrderType: 'TWAPOrder',
27
+ requestParams?: RequestParameters
28
+ ): Promise<string>;
29
+
30
+ (
31
+ orderData: TWAPBuyDeltaOrder,
32
+ onChainOrderType: 'TWAPBuyOrder',
33
+ requestParams?: RequestParameters
34
+ ): Promise<string>;
35
+ };
36
+
37
+ export type SetTWAPDeltaOrderPreSignature<T> = (
38
+ orderHash: string,
39
+ overrides?: TxSendOverrides,
40
+ requestParams?: RequestParameters
41
+ ) => Promise<T>;
42
+
43
+ export type PreSignTWAPDeltaOrder<T> = (
44
+ signableOrderData: SignableTWAPOrderData,
45
+ overrides?: TxSendOverrides,
46
+ requestParams?: RequestParameters
47
+ ) => Promise<T>;
48
+
49
+ export type PreSignTWAPDeltaOrderFunctions<T> = {
50
+ hashTWAPDeltaOrderTypedData: HashTWAPDeltaOrderTypedData;
51
+ hashTWAPDeltaOrder: HashTWAPDeltaOrder;
52
+ setTWAPDeltaOrderPreSignature: SetTWAPDeltaOrderPreSignature<T>;
53
+ preSignTWAPDeltaOrder: PreSignTWAPDeltaOrder<T>;
54
+ };
55
+
56
+ type AvailableMethods = ExtractAbiMethodNames<typeof PreSignatureModuleAbi>;
57
+
58
+ export const constructPreSignTWAPDeltaOrder = <T>(
59
+ options: ConstructProviderFetchInput<T, 'transactCall'>
60
+ ): PreSignTWAPDeltaOrderFunctions<T> => {
61
+ const hashTWAPDeltaOrderTypedData: HashTWAPDeltaOrderTypedData = (
62
+ typedData
63
+ ) => {
64
+ const typedDataOnly = {
65
+ ...typedData,
66
+ data: sanitizeTWAPOrderData(typedData.data),
67
+ } as SignableTWAPOrderData;
68
+
69
+ const orderHash = produceTWAPOrderHash(typedDataOnly);
70
+
71
+ return orderHash;
72
+ };
73
+
74
+ const { getDeltaContract } = constructGetDeltaContract(options);
75
+
76
+ const hashTWAPDeltaOrder: HashTWAPDeltaOrder = async (
77
+ orderData,
78
+ onChainOrderType,
79
+ requestParams
80
+ ) => {
81
+ const ParaswapDelta = await getDeltaContract(requestParams);
82
+ if (!ParaswapDelta) {
83
+ throw new Error(`Delta is not available on chain ${options.chainId}`);
84
+ }
85
+
86
+ if (onChainOrderType === 'TWAPOrder' && 'totalSrcAmount' in orderData) {
87
+ const typedData = produceTWAPOrderTypedData({
88
+ orderInput: orderData,
89
+ chainId: options.chainId,
90
+ paraswapDeltaAddress: ParaswapDelta,
91
+ onChainOrderType,
92
+ });
93
+ return hashTWAPDeltaOrderTypedData(typedData);
94
+ }
95
+
96
+ if (onChainOrderType === 'TWAPBuyOrder' && 'maxSrcAmount' in orderData) {
97
+ const typedData = produceTWAPOrderTypedData({
98
+ orderInput: orderData,
99
+ chainId: options.chainId,
100
+ paraswapDeltaAddress: ParaswapDelta,
101
+ onChainOrderType,
102
+ });
103
+ return hashTWAPDeltaOrderTypedData(typedData);
104
+ }
105
+
106
+ throw new Error(
107
+ `Invalid order data for onChainOrderType ${onChainOrderType}`
108
+ );
109
+ };
110
+
111
+ const setTWAPDeltaOrderPreSignature: SetTWAPDeltaOrderPreSignature<
112
+ T
113
+ > = async (orderHash, overrides = {}, requestParams) => {
114
+ const ParaswapDelta = await getDeltaContract(requestParams);
115
+ if (!ParaswapDelta) {
116
+ throw new Error(`Delta is not available on chain ${options.chainId}`);
117
+ }
118
+
119
+ const res = await options.contractCaller.transactCall<AvailableMethods>({
120
+ address: ParaswapDelta,
121
+ abi: PreSignatureModuleAbi,
122
+ contractMethod: 'setPreSignature',
123
+ args: [orderHash, true],
124
+ overrides,
125
+ });
126
+
127
+ return res;
128
+ };
129
+
130
+ const preSignTWAPDeltaOrder: PreSignTWAPDeltaOrder<T> = async (
131
+ signableOrderData,
132
+ overrides = {},
133
+ requestParams
134
+ ) => {
135
+ const orderHash = hashTWAPDeltaOrderTypedData(signableOrderData);
136
+ const res = await setTWAPDeltaOrderPreSignature(
137
+ orderHash,
138
+ overrides,
139
+ requestParams
140
+ );
141
+ return res;
142
+ };
143
+
144
+ return {
145
+ hashTWAPDeltaOrderTypedData,
146
+ hashTWAPDeltaOrder,
147
+ setTWAPDeltaOrderPreSignature,
148
+ preSignTWAPDeltaOrder,
149
+ };
150
+ };
151
+
152
+ export function produceTWAPOrderHash(typedData: SignableTWAPOrderData): string {
153
+ const types = typedData.types as Record<
154
+ 'TWAPOrder' | 'TWAPBuyOrder' | 'Bridge',
155
+ TypedDataField[]
156
+ >;
157
+ return hashTypedData({
158
+ domain: {
159
+ name: typedData.domain.name,
160
+ version: typedData.domain.version,
161
+ chainId: typedData.domain.chainId,
162
+ verifyingContract: typedData.domain.verifyingContract as `0x${string}`,
163
+ },
164
+ types,
165
+ primaryType: findPrimaryType(types),
166
+ message: typedData.data,
167
+ });
168
+ }