@tokenlon/v6-sdk 1.0.3 → 1.0.4-canary.a50512a

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 (76) hide show
  1. package/dist/cjs/apiVersion.js +5 -0
  2. package/dist/cjs/apiVersion.js.map +1 -0
  3. package/dist/cjs/constants/RFQAbi.js +723 -0
  4. package/dist/cjs/constants/RFQAbi.js.map +1 -0
  5. package/dist/cjs/constants/abis.js +301 -1
  6. package/dist/cjs/constants/abis.js.map +1 -1
  7. package/dist/cjs/constants/addresses.js +14 -1
  8. package/dist/cjs/constants/addresses.js.map +1 -1
  9. package/dist/cjs/constants/chains.js +2 -2
  10. package/dist/cjs/constants/chains.js.map +1 -1
  11. package/dist/cjs/constants/unit.js +2 -1
  12. package/dist/cjs/constants/unit.js.map +1 -1
  13. package/dist/cjs/errors/base.js +1 -1
  14. package/dist/cjs/permit/index.js +20 -4
  15. package/dist/cjs/permit/index.js.map +1 -1
  16. package/dist/cjs/sdk.js +7 -5
  17. package/dist/cjs/sdk.js.map +1 -1
  18. package/dist/cjs/serviceHost.js +6 -0
  19. package/dist/cjs/serviceHost.js.map +1 -1
  20. package/dist/cjs/swap/index.js +481 -69
  21. package/dist/cjs/swap/index.js.map +1 -1
  22. package/dist/cjs/swap/type.js +6 -1
  23. package/dist/cjs/swap/type.js.map +1 -1
  24. package/dist/cjs/version.js +1 -1
  25. package/dist/cjs/version.js.map +1 -1
  26. package/dist/esm/apiVersion.js +2 -0
  27. package/dist/esm/apiVersion.js.map +1 -0
  28. package/dist/esm/constants/RFQAbi.js +721 -0
  29. package/dist/esm/constants/RFQAbi.js.map +1 -0
  30. package/dist/esm/constants/abis.js +300 -0
  31. package/dist/esm/constants/abis.js.map +1 -1
  32. package/dist/esm/constants/addresses.js +13 -0
  33. package/dist/esm/constants/addresses.js.map +1 -1
  34. package/dist/esm/constants/chains.js +3 -3
  35. package/dist/esm/constants/chains.js.map +1 -1
  36. package/dist/esm/constants/unit.js +1 -0
  37. package/dist/esm/constants/unit.js.map +1 -1
  38. package/dist/esm/errors/base.js +1 -1
  39. package/dist/esm/permit/index.js +21 -5
  40. package/dist/esm/permit/index.js.map +1 -1
  41. package/dist/esm/sdk.js +7 -5
  42. package/dist/esm/sdk.js.map +1 -1
  43. package/dist/esm/serviceHost.js +7 -1
  44. package/dist/esm/serviceHost.js.map +1 -1
  45. package/dist/esm/swap/index.js +482 -71
  46. package/dist/esm/swap/index.js.map +1 -1
  47. package/dist/esm/swap/type.js +5 -1
  48. package/dist/esm/swap/type.js.map +1 -1
  49. package/dist/esm/version.js +1 -1
  50. package/dist/esm/version.js.map +1 -1
  51. package/dist/tsconfig.build.tsbuildinfo +1 -1
  52. package/dist/types/apiVersion.d.ts +2 -0
  53. package/dist/types/apiVersion.d.ts.map +1 -0
  54. package/dist/types/constants/RFQAbi.d.ts +82 -0
  55. package/dist/types/constants/RFQAbi.d.ts.map +1 -0
  56. package/dist/types/constants/abis.d.ts +336 -0
  57. package/dist/types/constants/abis.d.ts.map +1 -1
  58. package/dist/types/constants/addresses.d.ts +1 -0
  59. package/dist/types/constants/addresses.d.ts.map +1 -1
  60. package/dist/types/constants/chains.d.ts +1 -1
  61. package/dist/types/constants/chains.d.ts.map +1 -1
  62. package/dist/types/constants/unit.d.ts +1 -0
  63. package/dist/types/constants/unit.d.ts.map +1 -1
  64. package/dist/types/errors/base.d.ts +1 -1
  65. package/dist/types/errors/base.d.ts.map +1 -1
  66. package/dist/types/permit/index.d.ts.map +1 -1
  67. package/dist/types/sdk.d.ts +1 -0
  68. package/dist/types/sdk.d.ts.map +1 -1
  69. package/dist/types/serviceHost.d.ts.map +1 -1
  70. package/dist/types/swap/index.d.ts +76 -19
  71. package/dist/types/swap/index.d.ts.map +1 -1
  72. package/dist/types/swap/type.d.ts +54 -22
  73. package/dist/types/swap/type.d.ts.map +1 -1
  74. package/dist/types/version.d.ts +1 -1
  75. package/dist/types/version.d.ts.map +1 -1
  76. package/package.json +28 -21
@@ -1,11 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Swap = exports.ERC20_TRANSFER_GAS = exports.strategyOperationsABI = void 0;
4
+ const apiVersion_js_1 = require("../apiVersion.js");
4
5
  const index_js_1 = require("../constants/index.js");
5
6
  const contextHost_js_1 = require("../service/contextHost.js");
6
7
  const permit2_js_1 = require("../types/permit2.js");
7
8
  const index_js_2 = require("../utils/request/index.js");
8
9
  const serviceHost_js_1 = require("./../serviceHost.js");
10
+ const type_js_1 = require("./type.js");
9
11
  const viem_1 = require("viem");
10
12
  exports.strategyOperationsABI = (0, viem_1.parseAbiParameters)('(address dest, address inputToken, uint128 inputRatio, uint128 dataOffset, uint256 value, bytes data)[]');
11
13
  exports.ERC20_TRANSFER_GAS = 65000n;
@@ -65,13 +67,48 @@ class Swap extends contextHost_js_1.ContextHost {
65
67
  return newStrategyData;
66
68
  }
67
69
  });
70
+ Object.defineProperty(this, "formatRFQOrderParams", {
71
+ enumerable: true,
72
+ configurable: true,
73
+ writable: true,
74
+ value: (order) => {
75
+ const encodeResult = order.encodeResult;
76
+ const rfqTx = encodeResult.rfqTx;
77
+ const rfqOffer = rfqTx.rfqOffer ?? {};
78
+ const { fromAddress } = order;
79
+ const { makerTokenAmount } = rfqOffer;
80
+ const value = BigInt(this.isETH(fromAddress) ? rfqOffer.takerTokenAmount : '0x0');
81
+ const fee = (BigInt(makerTokenAmount) * BigInt(rfqOffer.feeFactor)) / index_js_1.bpBase;
82
+ const minMakerTokenAmount = BigInt(makerTokenAmount) - fee;
83
+ return {
84
+ rfqOffer,
85
+ rfqTx,
86
+ value,
87
+ fee,
88
+ minMakerTokenAmount: minMakerTokenAmount,
89
+ makerTokenAmount: BigInt(rfqOffer.makerTokenAmount),
90
+ takerTokenAmount: BigInt(rfqOffer.takerTokenAmount),
91
+ takerRequestAmount: rfqTx.takerRequestAmount,
92
+ };
93
+ }
94
+ });
68
95
  Object.defineProperty(this, "getProtocolFeeAmount", {
69
96
  enumerable: true,
70
97
  configurable: true,
71
98
  writable: true,
72
99
  value: (order) => {
73
- const { swapData, feeFactor } = order;
74
- const { makerTokenAmount } = swapData;
100
+ const encodeResult = order?.encodeResult ?? {};
101
+ const { protocolFeeFactor: feeFactor } = order;
102
+ let makerTokenAmount = 0n;
103
+ if (order.orderType === type_js_1.OrderType.SOR) {
104
+ const { swapData } = encodeResult;
105
+ makerTokenAmount = BigInt(swapData?.makerTokenAmount);
106
+ }
107
+ else if (order.orderType === type_js_1.OrderType.RFQV6) {
108
+ const { rfqTx } = encodeResult;
109
+ const { rfqOffer } = rfqTx;
110
+ makerTokenAmount = BigInt(rfqOffer?.makerTokenAmount);
111
+ }
75
112
  const protocolFeeAmount = (BigInt(makerTokenAmount) * BigInt(feeFactor)) / index_js_1.bpBase;
76
113
  return protocolFeeAmount;
77
114
  }
@@ -81,8 +118,10 @@ class Swap extends contextHost_js_1.ContextHost {
81
118
  configurable: true,
82
119
  writable: true,
83
120
  value: (order, slippage) => {
84
- const { swapData } = order;
85
- const { makerTokenAmount } = swapData;
121
+ const isSOROrder = order?.orderType === type_js_1.OrderType.SOR;
122
+ const makerTokenAmount = isSOROrder
123
+ ? order?.encodeResult?.swapData?.makerTokenAmount
124
+ : order?.encodeResult?.rfqTx?.rfqOffer?.makerTokenAmount;
86
125
  const slippageAmount = (BigInt(makerTokenAmount) * BigInt(slippage)) / index_js_1.bpBase;
87
126
  return slippageAmount;
88
127
  }
@@ -113,7 +152,8 @@ class Swap extends contextHost_js_1.ContextHost {
113
152
  configurable: true,
114
153
  writable: true,
115
154
  value: async (takerTokenAmount, needApprovalDestinations) => {
116
- if (needApprovalDestinations.length === 0)
155
+ if (!Array.isArray(needApprovalDestinations) ||
156
+ needApprovalDestinations.length === 0)
117
157
  return [];
118
158
  const allowances = await Promise.all([
119
159
  ...needApprovalDestinations.map(({ dest, inputToken }) => this.publicClient.readContract({
@@ -176,60 +216,144 @@ class Swap extends contextHost_js_1.ContextHost {
176
216
  return approveStrategies;
177
217
  }
178
218
  });
219
+ Object.defineProperty(this, "preview", {
220
+ enumerable: true,
221
+ configurable: true,
222
+ writable: true,
223
+ value: async (params) => {
224
+ let preview;
225
+ let { order, account, slippage, gasFee = 0n, sendByRelayer } = params;
226
+ const gas = await this.estimate({
227
+ account,
228
+ order,
229
+ slippage,
230
+ }).catch(() => index_js_1.DEFAULT_GAS_LIMIT);
231
+ if (order.orderType === type_js_1.OrderType.SOR) {
232
+ order = order;
233
+ const sorPrepare = await this.prepareOrder(params);
234
+ const { swapData, fee, protocolFee, gasFeeTokenAmount } = sorPrepare;
235
+ preview = {
236
+ inputToken: swapData.takerToken,
237
+ outputToken: swapData.makerToken,
238
+ amountIn: swapData.takerTokenAmount,
239
+ amountOut: swapData.makerTokenAmount,
240
+ minAmountOut: swapData.minMakerTokenAmount,
241
+ recipient: swapData.recipient,
242
+ fee,
243
+ protocolFee,
244
+ gas,
245
+ gasFeeTokenAmount,
246
+ feeFactor: order.protocolFeeFactor,
247
+ };
248
+ }
249
+ else if (order.orderType === type_js_1.OrderType.RFQV6) {
250
+ order = order;
251
+ const { rfqTx } = order.encodeResult;
252
+ const { rfqOffer } = rfqTx;
253
+ const { fee, minMakerTokenAmount } = this.formatRFQOrderParams(order);
254
+ const protocolFeeAmount = this.getProtocolFeeAmount(order);
255
+ let gasFeeTokenAmount = gasFee;
256
+ if (sendByRelayer) {
257
+ gasFeeTokenAmount = await this.calcGasFeeTokenAmount({
258
+ gasFeeAmount: gasFee,
259
+ feeToken: rfqOffer.makerToken,
260
+ });
261
+ }
262
+ preview = {
263
+ inputToken: rfqOffer.takerToken,
264
+ outputToken: rfqOffer.makerToken,
265
+ amountIn: BigInt(rfqOffer.takerTokenAmount),
266
+ amountOut: BigInt(rfqOffer.makerTokenAmount),
267
+ minAmountOut: minMakerTokenAmount,
268
+ recipient: rfqTx.recipient,
269
+ fee,
270
+ protocolFee: protocolFeeAmount,
271
+ gas,
272
+ gasFeeTokenAmount,
273
+ feeFactor: order.protocolFeeFactor,
274
+ };
275
+ }
276
+ else {
277
+ throw new Error('unsupported order type');
278
+ }
279
+ return preview;
280
+ }
281
+ });
179
282
  Object.defineProperty(this, "prepareOrder", {
180
283
  enumerable: true,
181
284
  configurable: true,
182
285
  writable: true,
183
286
  value: async ({ order, slippage, gasFee = 0n, sendByRelayer, }) => {
184
- const { swapData, needApprovalDestinations } = order;
185
- const { recipient, strategyData, makerToken } = swapData;
186
- const makerTokenAmount = BigInt(swapData.makerTokenAmount);
187
- const slippageAmount = this.getSlippageAmount(order, slippage);
188
- const protocolFeeAmount = this.getProtocolFeeAmount(order);
189
- let fee = protocolFeeAmount;
190
- let feeTokenAmount = gasFee;
191
- if (sendByRelayer) {
192
- feeTokenAmount = await this.calcGasFeeTokenAmount({
193
- gasFeeAmount: gasFee,
194
- feeToken: makerToken,
195
- });
196
- fee = await this.calcRelayerFee({
197
- protocolFeeAmount,
198
- feeTokenAmount,
199
- });
287
+ let prepare;
288
+ if (order.orderType === type_js_1.OrderType.SOR) {
289
+ order = order;
290
+ const encodeResult = order.encodeResult ?? {};
291
+ const { swapData, needApprovalDestinations } = encodeResult;
292
+ const { recipient, strategyData, makerToken } = swapData;
293
+ const makerTokenAmount = BigInt(swapData.makerTokenAmount);
294
+ const slippageAmount = this.getSlippageAmount(order, slippage);
295
+ const protocolFeeAmount = this.getProtocolFeeAmount(order);
296
+ let fee = protocolFeeAmount;
297
+ let feeTokenAmount = gasFee;
298
+ if (sendByRelayer) {
299
+ feeTokenAmount = await this.calcGasFeeTokenAmount({
300
+ gasFeeAmount: gasFee,
301
+ feeToken: makerToken,
302
+ });
303
+ fee = await this.calcRelayerFee({
304
+ protocolFeeAmount,
305
+ feeTokenAmount,
306
+ });
307
+ }
308
+ const minMakerTokenAmount = makerTokenAmount - slippageAmount - fee;
309
+ swapData.minMakerTokenAmount = minMakerTokenAmount.toString();
310
+ const [operations] = (0, viem_1.decodeAbiParameters)(exports.strategyOperationsABI, strategyData);
311
+ const approveOperations = await this.getRoutesApproveStrategy(BigInt(swapData.takerTokenAmount), needApprovalDestinations);
312
+ const newStrategyData = this.rebuildStrategyData(fee, swapData, [
313
+ ...approveOperations,
314
+ ...operations,
315
+ ]);
316
+ const value = (0, viem_1.isAddressEqual)(swapData.takerToken, index_js_1.nullAddress) ||
317
+ (0, viem_1.isAddressEqual)(swapData.takerToken, viem_1.zeroAddress)
318
+ ? BigInt(swapData.takerTokenAmount)
319
+ : 0n;
320
+ const newSwapData = {
321
+ ...swapData,
322
+ strategyData: newStrategyData,
323
+ takerTokenAmount: BigInt(swapData.takerTokenAmount),
324
+ makerTokenAmount: BigInt(swapData.makerTokenAmount),
325
+ minMakerTokenAmount: BigInt(swapData.minMakerTokenAmount),
326
+ salt: BigInt(swapData.salt),
327
+ expiry: BigInt(swapData.expiry),
328
+ };
329
+ prepare = {
330
+ swapData: newSwapData,
331
+ protocolFee: protocolFeeAmount,
332
+ gasFeeTokenAmount: feeTokenAmount,
333
+ gasFee,
334
+ fee,
335
+ recipient,
336
+ operations,
337
+ strategyData: newStrategyData,
338
+ value,
339
+ minMakerTokenAmount,
340
+ makerTokenAmount: newSwapData.makerTokenAmount,
341
+ takerTokenAmount: newSwapData.takerTokenAmount,
342
+ };
200
343
  }
201
- const minMakerTokenAmount = makerTokenAmount - slippageAmount - fee;
202
- swapData.minMakerTokenAmount = minMakerTokenAmount.toString();
203
- const [operations] = (0, viem_1.decodeAbiParameters)(exports.strategyOperationsABI, strategyData);
204
- const approveOperations = await this.getRoutesApproveStrategy(swapData.takerTokenAmount, needApprovalDestinations);
205
- const newStrategyData = this.rebuildStrategyData(fee, swapData, [
206
- ...approveOperations,
207
- ...operations,
208
- ]);
209
- const value = (0, viem_1.isAddressEqual)(swapData.takerToken, index_js_1.nullAddress) ||
210
- (0, viem_1.isAddressEqual)(swapData.takerToken, viem_1.zeroAddress)
211
- ? BigInt(swapData.takerTokenAmount)
212
- : 0n;
213
- const newSwapData = {
214
- ...swapData,
215
- strategyData: newStrategyData,
216
- takerTokenAmount: BigInt(swapData.takerTokenAmount),
217
- makerTokenAmount: BigInt(swapData.makerTokenAmount),
218
- minMakerTokenAmount: BigInt(swapData.minMakerTokenAmount),
219
- salt: BigInt(swapData.salt),
220
- expiry: BigInt(swapData.expiry),
221
- };
222
- return {
223
- swapData: newSwapData,
224
- protocolFee: protocolFeeAmount,
225
- gasFeeTokenAmount: feeTokenAmount,
226
- gasFee,
227
- fee,
228
- recipient,
229
- operations,
230
- strategyData: newStrategyData,
231
- value,
232
- };
344
+ else if (order.orderType === type_js_1.OrderType.RFQV6) {
345
+ order = order;
346
+ const protocolFeeAmount = this.getProtocolFeeAmount(order);
347
+ const rfqOrderParams = this.formatRFQOrderParams(order);
348
+ prepare = {
349
+ ...rfqOrderParams,
350
+ protocolFee: protocolFeeAmount,
351
+ };
352
+ }
353
+ else {
354
+ throw new Error('unsupported order type');
355
+ }
356
+ return prepare;
233
357
  }
234
358
  });
235
359
  Object.defineProperty(this, "quote", {
@@ -245,7 +369,16 @@ class Swap extends contextHost_js_1.ContextHost {
245
369
  Object.entries(dto).forEach(([key, value]) => {
246
370
  p.append(key, value.toString());
247
371
  });
248
- const order = await (0, index_js_2.http)(`${this.host(serviceHost_js_1.httpServiceHost)}/v6/swap/quote/${chainId}?${p.toString()}`);
372
+ const order = await (0, index_js_2.http)(`${this.host(serviceHost_js_1.httpServiceHost)}/${apiVersion_js_1.apiVersion}/swap/quote/${chainId}?${p.toString()}`);
373
+ return order;
374
+ }
375
+ });
376
+ Object.defineProperty(this, "quoteLastOrder", {
377
+ enumerable: true,
378
+ configurable: true,
379
+ writable: true,
380
+ value: async (uniqueId) => {
381
+ const order = await (0, index_js_2.http)(`${this.host(serviceHost_js_1.httpServiceHost)}/${apiVersion_js_1.apiVersion}/swap/swap/${this.chainId}?uniqueId=${uniqueId}`);
249
382
  return order;
250
383
  }
251
384
  });
@@ -271,16 +404,58 @@ class Swap extends contextHost_js_1.ContextHost {
271
404
  (0, viem_1.isAddressEqual)(tokenAddress, viem_1.zeroAddress));
272
405
  }
273
406
  });
274
- Object.defineProperty(this, "estimate", {
407
+ Object.defineProperty(this, "estimateRFQOrder", {
275
408
  enumerable: true,
276
409
  configurable: true,
277
410
  writable: true,
278
- value: async ({ account, order, slippage, permit, }) => {
279
- const { swapData, value } = await this.prepareOrder({
411
+ value: async ({ account, order, takerTokenPermit, }) => {
412
+ const { encodeResult } = order;
413
+ const { rfqTx } = encodeResult;
414
+ const { rfqOffer } = rfqTx;
415
+ const { value } = this.formatRFQOrderParams(order);
416
+ const gasEstimate = await this.publicClient.estimateContractGas({
417
+ address: index_js_1.rfqAddress[this.chainId],
418
+ abi: index_js_1.RFQ_ABI,
419
+ functionName: 'fillRFQ',
420
+ args: [
421
+ [
422
+ [
423
+ rfqOffer.taker,
424
+ rfqOffer.maker,
425
+ rfqOffer.takerToken,
426
+ rfqOffer.takerTokenAmount,
427
+ rfqOffer.makerToken,
428
+ rfqOffer.makerTokenAmount,
429
+ rfqOffer.feeFactor,
430
+ rfqOffer.flags,
431
+ rfqOffer.expiry,
432
+ rfqOffer.salt,
433
+ ],
434
+ rfqTx.recipient,
435
+ rfqTx.takerRequestAmount,
436
+ ],
437
+ encodeResult.makerSignature,
438
+ encodeResult.makerTokenPermit,
439
+ takerTokenPermit,
440
+ ],
441
+ value,
442
+ account,
443
+ });
444
+ return gasEstimate;
445
+ }
446
+ });
447
+ Object.defineProperty(this, "estimateSOROrder", {
448
+ enumerable: true,
449
+ configurable: true,
450
+ writable: true,
451
+ value: async ({ account, order, slippage, takerTokenPermit, }) => {
452
+ const prepare = await this.prepareOrder({
280
453
  order,
281
454
  slippage,
282
455
  });
283
- const takerTokenPermit = permit ?? this.getTokenPermitData(swapData.takerToken);
456
+ const { swapData, value } = prepare;
457
+ takerTokenPermit =
458
+ takerTokenPermit ?? this.getTokenPermitData(swapData.takerToken);
284
459
  const gasEstimate = await this.publicClient.estimateContractGas({
285
460
  address: index_js_1.genericSwapAddress[this.chainId],
286
461
  abi: index_js_1.GENERIC_SWAP_ABI,
@@ -292,7 +467,82 @@ class Swap extends contextHost_js_1.ContextHost {
292
467
  return gasEstimate;
293
468
  }
294
469
  });
295
- Object.defineProperty(this, "sign", {
470
+ Object.defineProperty(this, "estimate", {
471
+ enumerable: true,
472
+ configurable: true,
473
+ writable: true,
474
+ value: async ({ account, order, slippage, takerTokenPermit, }) => {
475
+ if (order.orderType === type_js_1.OrderType.SOR) {
476
+ order = order;
477
+ const { swapData } = order.encodeResult;
478
+ takerTokenPermit =
479
+ takerTokenPermit ?? this.getTokenPermitData(swapData.takerToken);
480
+ return await this.estimateSOROrder({
481
+ account,
482
+ order: order,
483
+ slippage,
484
+ takerTokenPermit,
485
+ });
486
+ }
487
+ else if (order.orderType === type_js_1.OrderType.RFQV6) {
488
+ order = order;
489
+ const rfqOffer = order?.encodeResult?.rfqTx?.rfqOffer ?? {};
490
+ takerTokenPermit =
491
+ takerTokenPermit ?? this.getTokenPermitData(rfqOffer.takerToken);
492
+ return await this.estimateRFQOrder({
493
+ account,
494
+ order: order,
495
+ takerTokenPermit,
496
+ });
497
+ }
498
+ else {
499
+ throw new Error('unsupported order type');
500
+ }
501
+ }
502
+ });
503
+ Object.defineProperty(this, "signRFQOrder", {
504
+ enumerable: true,
505
+ configurable: true,
506
+ writable: true,
507
+ value: async (params) => {
508
+ const { account, value = 0n, txOverride, order, takerTokenPermit } = params;
509
+ const { encodeResult } = order;
510
+ const { rfqTx } = encodeResult;
511
+ const { rfqOffer } = rfqTx;
512
+ const txHash = await this.walletClient.writeContract({
513
+ address: index_js_1.rfqAddress[this.chainId],
514
+ abi: index_js_1.RFQ_ABI,
515
+ account,
516
+ functionName: 'fillRFQ',
517
+ args: [
518
+ [
519
+ [
520
+ rfqOffer.taker,
521
+ rfqOffer.maker,
522
+ rfqOffer.takerToken,
523
+ rfqOffer.takerTokenAmount,
524
+ rfqOffer.makerToken,
525
+ rfqOffer.makerTokenAmount,
526
+ rfqOffer.feeFactor,
527
+ rfqOffer.flags,
528
+ rfqOffer.expiry,
529
+ rfqOffer.salt,
530
+ ],
531
+ rfqTx.recipient,
532
+ rfqTx.takerRequestAmount,
533
+ ],
534
+ encodeResult.makerSignature,
535
+ encodeResult.makerTokenPermit,
536
+ takerTokenPermit,
537
+ ],
538
+ value,
539
+ chain: this.walletClient.chain,
540
+ ...txOverride,
541
+ });
542
+ return txHash;
543
+ }
544
+ });
545
+ Object.defineProperty(this, "signSOROrder", {
296
546
  enumerable: true,
297
547
  configurable: true,
298
548
  writable: true,
@@ -311,6 +561,58 @@ class Swap extends contextHost_js_1.ContextHost {
311
561
  return txHash;
312
562
  }
313
563
  });
564
+ Object.defineProperty(this, "sign", {
565
+ enumerable: true,
566
+ configurable: true,
567
+ writable: true,
568
+ value: async (params) => {
569
+ let { account, order, slippage, takerTokenPermit, txOverride, sendByRelayer, } = params;
570
+ const gas = txOverride?.gas ??
571
+ (await this.estimate({
572
+ account,
573
+ order,
574
+ slippage,
575
+ }));
576
+ const gasPrice = txOverride?.gasPrice ?? (await this.publicClient.getGasPrice());
577
+ const gasFee = (gas * gasPrice * 120n) / 100n;
578
+ txOverride = txOverride;
579
+ if (order.orderType === type_js_1.OrderType.SOR) {
580
+ const prepare = await this.prepareOrder({
581
+ order,
582
+ slippage,
583
+ gasFee,
584
+ sendByRelayer,
585
+ });
586
+ const { swapData, value } = prepare;
587
+ takerTokenPermit =
588
+ takerTokenPermit ?? this.getTokenPermitData(swapData.takerToken);
589
+ return await this.signSOROrder({
590
+ account,
591
+ value,
592
+ takerTokenPermit,
593
+ swapData,
594
+ txOverride,
595
+ });
596
+ }
597
+ else if (order.orderType === type_js_1.OrderType.RFQV6) {
598
+ order = order;
599
+ const rfqOffer = order?.encodeResult?.rfqTx?.rfqOffer ?? {};
600
+ takerTokenPermit =
601
+ takerTokenPermit ?? this.getTokenPermitData(rfqOffer.takerToken);
602
+ const { value } = this.formatRFQOrderParams(order);
603
+ return await this.signRFQOrder({
604
+ account,
605
+ order,
606
+ takerTokenPermit,
607
+ value: value,
608
+ txOverride,
609
+ });
610
+ }
611
+ else {
612
+ throw new Error('unsupported order type');
613
+ }
614
+ }
615
+ });
314
616
  Object.defineProperty(this, "signWithReceipt", {
315
617
  enumerable: true,
316
618
  configurable: true,
@@ -323,7 +625,7 @@ class Swap extends contextHost_js_1.ContextHost {
323
625
  return receipt;
324
626
  }
325
627
  });
326
- Object.defineProperty(this, "signTypedData", {
628
+ Object.defineProperty(this, "sign712SOROrder", {
327
629
  enumerable: true,
328
630
  configurable: true,
329
631
  writable: true,
@@ -334,28 +636,138 @@ class Swap extends contextHost_js_1.ContextHost {
334
636
  chainId: this.chainId,
335
637
  verifyingContract: index_js_1.genericSwapAddress[this.chainId],
336
638
  };
337
- const EIP712_TYPED = {
639
+ return this.walletClient.signTypedData({
640
+ primaryType: 'GenericSwapData',
338
641
  domain: EIP712_DOMAIN,
339
642
  types: index_js_1.GS_EIP712_TYPES,
340
- primaryType: 'GenericSwapData',
643
+ message: swapData,
644
+ account,
645
+ });
646
+ }
647
+ });
648
+ Object.defineProperty(this, "sign712RFQOrder", {
649
+ enumerable: true,
650
+ configurable: true,
651
+ writable: true,
652
+ value: async (rfqTx, account) => {
653
+ const EIP712_DOMAIN = {
654
+ name: 'Tokenlon',
655
+ version: 'v6',
656
+ chainId: this.chainId,
657
+ verifyingContract: index_js_1.rfqAddress[this.chainId],
658
+ };
659
+ const types = {
660
+ RFQOffer: [
661
+ { name: 'taker', type: 'address' },
662
+ { name: 'maker', type: 'address' },
663
+ { name: 'takerToken', type: 'address' },
664
+ { name: 'takerTokenAmount', type: 'uint256' },
665
+ { name: 'makerToken', type: 'address' },
666
+ { name: 'makerTokenAmount', type: 'uint256' },
667
+ { name: 'feeFactor', type: 'uint256' },
668
+ { name: 'flags', type: 'uint256' },
669
+ { name: 'expiry', type: 'uint256' },
670
+ { name: 'salt', type: 'uint256' },
671
+ ],
672
+ RFQTx: [
673
+ { name: 'rfqOffer', type: 'RFQOffer' },
674
+ { name: 'recipient', type: 'address' },
675
+ { name: 'takerRequestAmount', type: 'uint256' },
676
+ ],
341
677
  };
342
678
  return this.walletClient.signTypedData({
343
- ...EIP712_TYPED,
344
- message: swapData,
679
+ primaryType: 'RFQTx',
680
+ domain: EIP712_DOMAIN,
681
+ types: types,
682
+ message: rfqTx,
345
683
  account,
346
684
  });
347
685
  }
348
686
  });
687
+ Object.defineProperty(this, "signTypedData", {
688
+ enumerable: true,
689
+ configurable: true,
690
+ writable: true,
691
+ value: async (params) => {
692
+ let { account, order, takerTokenPermit, prepare } = params;
693
+ if (order.orderType === type_js_1.OrderType.SOR) {
694
+ if (!prepare) {
695
+ throw new Error('prepare is empty');
696
+ }
697
+ const { swapData } = prepare;
698
+ takerTokenPermit =
699
+ takerTokenPermit ?? this.getTokenPermitData(swapData.takerToken);
700
+ return this.sign712SOROrder(swapData, account);
701
+ }
702
+ else if (order.orderType === type_js_1.OrderType.RFQV6) {
703
+ const rfqOrder = order;
704
+ const { encodeResult } = rfqOrder;
705
+ const { rfqTx } = encodeResult;
706
+ const rfqOffer = rfqTx.rfqOffer;
707
+ takerTokenPermit =
708
+ takerTokenPermit ?? this.getTokenPermitData(rfqOffer.takerToken);
709
+ return this.sign712RFQOrder(rfqTx, account);
710
+ }
711
+ else {
712
+ throw new Error('unsupported order type');
713
+ }
714
+ }
715
+ });
349
716
  Object.defineProperty(this, "submit", {
350
717
  enumerable: true,
351
718
  configurable: true,
352
719
  writable: true,
353
- value: async (dto) => {
354
- return (0, index_js_2.http)(`${this.host(serviceHost_js_1.httpServiceHost)}/v6/swap/submit/${this.chainId}`, {
720
+ value: async (params) => {
721
+ let { order, slippage, takerTokenPermit, txHash, rawTx, gasFee, sendByRelayer, orderSignature, } = params;
722
+ const request = {
723
+ uniqueId: order.uniqueId,
724
+ slippage,
725
+ permit: takerTokenPermit,
726
+ fee: {
727
+ protocolFee: 0n,
728
+ gasFee: 0n,
729
+ },
730
+ };
731
+ if (orderSignature)
732
+ request.orderSignature = orderSignature;
733
+ if (txHash)
734
+ request.txHash = txHash;
735
+ if (rawTx)
736
+ request.rawTx = rawTx;
737
+ if (order.orderType === type_js_1.OrderType.SOR) {
738
+ const prepare = await this.prepareOrder({
739
+ order,
740
+ slippage,
741
+ gasFee: gasFee,
742
+ sendByRelayer,
743
+ });
744
+ const { swapData, protocolFee, gasFeeTokenAmount } = prepare;
745
+ request.strategyData = swapData.strategyData;
746
+ request.fee = {
747
+ protocolFee: protocolFee,
748
+ gasFee: gasFeeTokenAmount,
749
+ };
750
+ request.minAmountOut = swapData.minMakerTokenAmount;
751
+ }
752
+ else if (order.orderType === type_js_1.OrderType.RFQV6) {
753
+ order = order;
754
+ const rfqOrderParams = this.formatRFQOrderParams(order);
755
+ const { fee, minMakerTokenAmount } = rfqOrderParams;
756
+ request.slippage = 0;
757
+ request.minAmountOut = minMakerTokenAmount;
758
+ request.fee = {
759
+ gasFee: 0n,
760
+ protocolFee: fee,
761
+ };
762
+ }
763
+ else {
764
+ throw new Error('unsupported order type');
765
+ }
766
+ return (0, index_js_2.http)(`${this.host(serviceHost_js_1.httpServiceHost)}/${apiVersion_js_1.apiVersion}/swap/submit/${this.chainId}`, {
355
767
  fetchOptions: {
356
768
  method: 'POST',
357
769
  },
358
- body: dto,
770
+ body: request,
359
771
  });
360
772
  }
361
773
  });
@@ -378,7 +790,7 @@ class Swap extends contextHost_js_1.ContextHost {
378
790
  const params = new URLSearchParams({
379
791
  ...dto,
380
792
  });
381
- return (0, index_js_2.http)(`${this.host(serviceHost_js_1.httpServiceHost)}/v6/swap/status/${this.chainId}?${params}`);
793
+ return (0, index_js_2.http)(`${this.host(serviceHost_js_1.httpServiceHost)}/${apiVersion_js_1.apiVersion}/swap/status/${this.chainId}?${params}`);
382
794
  }
383
795
  });
384
796
  Object.defineProperty(this, "config", {
@@ -386,7 +798,7 @@ class Swap extends contextHost_js_1.ContextHost {
386
798
  configurable: true,
387
799
  writable: true,
388
800
  value: async (appVersion) => {
389
- return (0, index_js_2.http)(`${this.host(serviceHost_js_1.httpServiceHost)}/v6/swap/config?appVersion=${appVersion}`);
801
+ return (0, index_js_2.http)(`${this.host(serviceHost_js_1.httpServiceHost)}/${apiVersion_js_1.apiVersion}/swap/config?appVersion=${appVersion}`);
390
802
  }
391
803
  });
392
804
  this.walletClient = injected.walletClient;