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