@tokenlon/v6-sdk 1.0.3 → 1.0.4-canary.25975f2

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 +526 -40
  6. package/dist/cjs/constants/abis.js.map +1 -1
  7. package/dist/cjs/constants/addresses.js +18 -5
  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 +547 -74
  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 +525 -39
  31. package/dist/esm/constants/abis.js.map +1 -1
  32. package/dist/esm/constants/addresses.js +17 -4
  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 +548 -76
  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 +381 -14
  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 +96 -23
  71. package/dist/types/swap/index.d.ts.map +1 -1
  72. package/dist/types/swap/type.d.ts +80 -23
  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,13 +1,15 @@
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
- exports.strategyOperationsABI = (0, viem_1.parseAbiParameters)('(address dest, address inputToken, uint128 inputRatio, uint128 dataOffset, uint256 value, bytes data)[]');
12
+ exports.strategyOperationsABI = (0, viem_1.parseAbiParameters)('(address dest, address inputToken, uint256 ratioNumerator, uint256 ratioDenominator, uint256 dataOffset, uint256 value, bytes data)[]');
11
13
  exports.ERC20_TRANSFER_GAS = 65000n;
12
14
  class Swap extends contextHost_js_1.ContextHost {
13
15
  constructor(config, injected) {
@@ -52,7 +54,8 @@ class Swap extends contextHost_js_1.ContextHost {
52
54
  const feeOperation = {
53
55
  dest: feeToken,
54
56
  inputToken: feeToken,
55
- inputRatio: 0n,
57
+ ratioNumerator: 0n,
58
+ ratioDenominator: 0n,
56
59
  dataOffset: 0n,
57
60
  value: 0n,
58
61
  data: feeTransfer,
@@ -65,13 +68,41 @@ class Swap extends contextHost_js_1.ContextHost {
65
68
  return newStrategyData;
66
69
  }
67
70
  });
71
+ Object.defineProperty(this, "formatRFQOrderParams", {
72
+ enumerable: true,
73
+ configurable: true,
74
+ writable: true,
75
+ value: (order) => {
76
+ const encodeResult = order.encodeResult;
77
+ const rfqTx = encodeResult.rfqTx;
78
+ const rfqOffer = rfqTx.rfqOffer ?? {};
79
+ const { fromAddress } = order;
80
+ const { makerTokenAmount } = rfqOffer;
81
+ const value = BigInt(this.isETH(fromAddress) ? rfqOffer.takerTokenAmount : '0x0');
82
+ const fee = (BigInt(makerTokenAmount) * BigInt(rfqOffer.feeFactor)) / index_js_1.bpBase;
83
+ const minMakerTokenAmount = BigInt(makerTokenAmount) - fee;
84
+ return {
85
+ rfqOffer,
86
+ rfqTx,
87
+ value,
88
+ fee,
89
+ minMakerTokenAmount: minMakerTokenAmount,
90
+ makerTokenAmount: BigInt(rfqOffer.makerTokenAmount),
91
+ takerTokenAmount: BigInt(rfqOffer.takerTokenAmount),
92
+ takerRequestAmount: rfqTx.takerRequestAmount,
93
+ };
94
+ }
95
+ });
68
96
  Object.defineProperty(this, "getProtocolFeeAmount", {
69
97
  enumerable: true,
70
98
  configurable: true,
71
99
  writable: true,
72
100
  value: (order) => {
73
- const { swapData, feeFactor } = order;
74
- const { makerTokenAmount } = swapData;
101
+ const { protocolFeeFactor: feeFactor } = order;
102
+ const isSOROrder = order?.orderType === type_js_1.OrderType.SOR;
103
+ const makerTokenAmount = isSOROrder
104
+ ? order?.encodeResult?.swapData?.makerTokenAmount
105
+ : order?.encodeResult?.rfqTx?.rfqOffer?.makerTokenAmount;
75
106
  const protocolFeeAmount = (BigInt(makerTokenAmount) * BigInt(feeFactor)) / index_js_1.bpBase;
76
107
  return protocolFeeAmount;
77
108
  }
@@ -81,8 +112,10 @@ class Swap extends contextHost_js_1.ContextHost {
81
112
  configurable: true,
82
113
  writable: true,
83
114
  value: (order, slippage) => {
84
- const { swapData } = order;
85
- const { makerTokenAmount } = swapData;
115
+ const isSOROrder = order?.orderType === type_js_1.OrderType.SOR;
116
+ const makerTokenAmount = isSOROrder
117
+ ? order?.encodeResult?.swapData?.makerTokenAmount
118
+ : order?.encodeResult?.rfqTx?.rfqOffer?.makerTokenAmount;
86
119
  const slippageAmount = (BigInt(makerTokenAmount) * BigInt(slippage)) / index_js_1.bpBase;
87
120
  return slippageAmount;
88
121
  }
@@ -104,8 +137,8 @@ class Swap extends contextHost_js_1.ContextHost {
104
137
  enumerable: true,
105
138
  configurable: true,
106
139
  writable: true,
107
- value: async ({ protocolFeeAmount, feeTokenAmount, }) => {
108
- return feeTokenAmount + protocolFeeAmount;
140
+ value: async ({ protocolFeeAmount, gasFeeTokenAmount, }) => {
141
+ return gasFeeTokenAmount + protocolFeeAmount;
109
142
  }
110
143
  });
111
144
  Object.defineProperty(this, "getRoutesApproveStrategy", {
@@ -113,7 +146,8 @@ class Swap extends contextHost_js_1.ContextHost {
113
146
  configurable: true,
114
147
  writable: true,
115
148
  value: async (takerTokenAmount, needApprovalDestinations) => {
116
- if (needApprovalDestinations.length === 0)
149
+ if (!Array.isArray(needApprovalDestinations) ||
150
+ needApprovalDestinations.length === 0)
117
151
  return [];
118
152
  const allowances = await Promise.all([
119
153
  ...needApprovalDestinations.map(({ dest, inputToken }) => this.publicClient.readContract({
@@ -176,63 +210,214 @@ class Swap extends contextHost_js_1.ContextHost {
176
210
  return approveStrategies;
177
211
  }
178
212
  });
213
+ Object.defineProperty(this, "preview", {
214
+ enumerable: true,
215
+ configurable: true,
216
+ writable: true,
217
+ value: async (params) => {
218
+ let preview;
219
+ let { order, gasFee = 0n, sendByRelayer } = params;
220
+ if (order.orderType === type_js_1.OrderType.SOR) {
221
+ order = order;
222
+ const sorPrepare = await this.prepareOrder(params);
223
+ const { swapData, fee, protocolFee, gasFeeTokenAmount } = sorPrepare;
224
+ preview = {
225
+ inputToken: swapData.takerToken,
226
+ outputToken: swapData.makerToken,
227
+ amountIn: swapData.takerTokenAmount,
228
+ amountOut: swapData.makerTokenAmount,
229
+ minAmountOut: swapData.minMakerTokenAmount,
230
+ recipient: swapData.recipient,
231
+ fee,
232
+ protocolFee,
233
+ gasFeeTokenAmount,
234
+ feeFactor: order.protocolFeeFactor,
235
+ };
236
+ }
237
+ else if (order.orderType === type_js_1.OrderType.RFQV6) {
238
+ order = order;
239
+ const { rfqTx } = order.encodeResult;
240
+ const { rfqOffer } = rfqTx;
241
+ const protocolFeeAmount = this.getProtocolFeeAmount(order);
242
+ const { fee, minMakerTokenAmount } = this.formatRFQOrderParams(order);
243
+ let gasFeeTokenAmount = gasFee;
244
+ if (sendByRelayer) {
245
+ gasFeeTokenAmount = await this.calcGasFeeTokenAmount({
246
+ gasFeeAmount: gasFee,
247
+ feeToken: rfqOffer.makerToken,
248
+ });
249
+ }
250
+ preview = {
251
+ inputToken: rfqOffer.takerToken,
252
+ outputToken: rfqOffer.makerToken,
253
+ amountIn: BigInt(rfqOffer.takerTokenAmount),
254
+ amountOut: BigInt(rfqOffer.makerTokenAmount),
255
+ minAmountOut: minMakerTokenAmount,
256
+ recipient: rfqTx.recipient,
257
+ fee,
258
+ protocolFee: protocolFeeAmount,
259
+ gasFeeTokenAmount,
260
+ feeFactor: order.protocolFeeFactor,
261
+ };
262
+ }
263
+ else {
264
+ throw new Error('unsupported order type');
265
+ }
266
+ return preview;
267
+ }
268
+ });
269
+ Object.defineProperty(this, "previewNewOrder", {
270
+ enumerable: true,
271
+ configurable: true,
272
+ writable: true,
273
+ value: async (params) => {
274
+ let preview;
275
+ let { order, slippage, sendByRelayer, gasFee = 0n } = params;
276
+ if (order.orderType === type_js_1.OrderType.SOR) {
277
+ order = order;
278
+ const encodeResult = order.encodeResult ?? {};
279
+ const { swapData } = encodeResult;
280
+ const { makerToken } = swapData;
281
+ const makerTokenAmount = BigInt(swapData.makerTokenAmount);
282
+ const slippageAmount = this.getSlippageAmount(order, slippage);
283
+ const protocolFeeAmount = this.getProtocolFeeAmount(order);
284
+ let fee = protocolFeeAmount;
285
+ let gasFeeTokenAmount = gasFee;
286
+ if (sendByRelayer) {
287
+ gasFeeTokenAmount = await this.calcGasFeeTokenAmount({
288
+ gasFeeAmount: gasFee,
289
+ feeToken: makerToken,
290
+ });
291
+ fee = await this.calcRelayerFee({
292
+ protocolFeeAmount,
293
+ gasFeeTokenAmount,
294
+ });
295
+ }
296
+ const minMakerTokenAmount = makerTokenAmount - slippageAmount - fee;
297
+ swapData.minMakerTokenAmount = minMakerTokenAmount.toString();
298
+ preview = {
299
+ inputToken: swapData.takerToken,
300
+ outputToken: swapData.makerToken,
301
+ amountIn: BigInt(swapData.takerTokenAmount),
302
+ amountOut: BigInt(swapData.makerTokenAmount),
303
+ minAmountOut: BigInt(swapData.minMakerTokenAmount),
304
+ recipient: swapData.recipient,
305
+ fee,
306
+ protocolFee: protocolFeeAmount,
307
+ gasFeeTokenAmount,
308
+ feeFactor: order.protocolFeeFactor,
309
+ };
310
+ }
311
+ else if (order.orderType === type_js_1.OrderType.RFQV6) {
312
+ order = order;
313
+ const { rfqTx } = order.encodeResult;
314
+ const { rfqOffer } = rfqTx;
315
+ const protocolFeeAmount = this.getProtocolFeeAmount(order);
316
+ const { fee, minMakerTokenAmount } = this.formatRFQOrderParams(order);
317
+ let gasFeeTokenAmount = gasFee;
318
+ if (sendByRelayer) {
319
+ gasFeeTokenAmount = await this.calcGasFeeTokenAmount({
320
+ gasFeeAmount: gasFee,
321
+ feeToken: rfqOffer.makerToken,
322
+ });
323
+ }
324
+ preview = {
325
+ inputToken: rfqOffer.takerToken,
326
+ outputToken: rfqOffer.makerToken,
327
+ amountIn: BigInt(rfqOffer.takerTokenAmount),
328
+ amountOut: BigInt(rfqOffer.makerTokenAmount),
329
+ minAmountOut: minMakerTokenAmount,
330
+ recipient: rfqTx.recipient,
331
+ fee,
332
+ protocolFee: protocolFeeAmount,
333
+ gasFeeTokenAmount,
334
+ feeFactor: order.protocolFeeFactor,
335
+ };
336
+ }
337
+ else {
338
+ throw new Error('unsupported order type');
339
+ }
340
+ return preview;
341
+ }
342
+ });
179
343
  Object.defineProperty(this, "prepareOrder", {
180
344
  enumerable: true,
181
345
  configurable: true,
182
346
  writable: true,
183
347
  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
- });
348
+ let prepare;
349
+ if (order.orderType === type_js_1.OrderType.SOR) {
350
+ order = order;
351
+ const encodeResult = order.encodeResult ?? {};
352
+ const { swapData, needApprovalDestinations } = encodeResult;
353
+ const { recipient, strategyData, makerToken } = swapData;
354
+ const makerTokenAmount = BigInt(swapData.makerTokenAmount);
355
+ const slippageAmount = this.getSlippageAmount(order, slippage);
356
+ const protocolFeeAmount = this.getProtocolFeeAmount(order);
357
+ let fee = protocolFeeAmount;
358
+ let gasFeeTokenAmount = gasFee;
359
+ if (sendByRelayer) {
360
+ gasFeeTokenAmount = await this.calcGasFeeTokenAmount({
361
+ gasFeeAmount: gasFee,
362
+ feeToken: makerToken,
363
+ });
364
+ fee = await this.calcRelayerFee({
365
+ protocolFeeAmount,
366
+ gasFeeTokenAmount,
367
+ });
368
+ }
369
+ const minMakerTokenAmount = makerTokenAmount - slippageAmount - fee;
370
+ swapData.minMakerTokenAmount = minMakerTokenAmount.toString();
371
+ const [operations] = (0, viem_1.decodeAbiParameters)(exports.strategyOperationsABI, strategyData);
372
+ const approveOperations = await this.getRoutesApproveStrategy(BigInt(swapData.takerTokenAmount), needApprovalDestinations);
373
+ const newStrategyData = this.rebuildStrategyData(fee, swapData, [
374
+ ...approveOperations,
375
+ ...operations,
376
+ ]);
377
+ const value = (0, viem_1.isAddressEqual)(swapData.takerToken, index_js_1.nullAddress) ||
378
+ (0, viem_1.isAddressEqual)(swapData.takerToken, viem_1.zeroAddress)
379
+ ? BigInt(swapData.takerTokenAmount)
380
+ : 0n;
381
+ const newSwapData = {
382
+ ...swapData,
383
+ strategyData: newStrategyData,
384
+ takerTokenAmount: BigInt(swapData.takerTokenAmount),
385
+ makerTokenAmount: BigInt(swapData.makerTokenAmount),
386
+ minMakerTokenAmount: BigInt(swapData.minMakerTokenAmount),
387
+ salt: BigInt(swapData.salt),
388
+ expiry: BigInt(swapData.expiry),
389
+ };
390
+ prepare = {
391
+ swapData: newSwapData,
392
+ protocolFee: protocolFeeAmount,
393
+ gasFeeTokenAmount,
394
+ gasFee,
395
+ fee,
396
+ recipient,
397
+ operations,
398
+ strategyData: newStrategyData,
399
+ value,
400
+ minMakerTokenAmount,
401
+ makerTokenAmount: newSwapData.makerTokenAmount,
402
+ takerTokenAmount: newSwapData.takerTokenAmount,
403
+ };
200
404
  }
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
- };
405
+ else if (order.orderType === type_js_1.OrderType.RFQV6) {
406
+ order = order;
407
+ const protocolFeeAmount = this.getProtocolFeeAmount(order);
408
+ const rfqOrderParams = this.formatRFQOrderParams(order);
409
+ prepare = {
410
+ ...rfqOrderParams,
411
+ protocolFee: protocolFeeAmount,
412
+ };
413
+ }
414
+ else {
415
+ throw new Error('unsupported order type');
416
+ }
417
+ return prepare;
233
418
  }
234
419
  });
235
- Object.defineProperty(this, "quote", {
420
+ Object.defineProperty(this, "quoteNewOrder", {
236
421
  enumerable: true,
237
422
  configurable: true,
238
423
  writable: true,
@@ -245,7 +430,16 @@ class Swap extends contextHost_js_1.ContextHost {
245
430
  Object.entries(dto).forEach(([key, value]) => {
246
431
  p.append(key, value.toString());
247
432
  });
248
- const order = await (0, index_js_2.http)(`${this.host(serviceHost_js_1.httpServiceHost)}/v6/swap/quote/${chainId}?${p.toString()}`);
433
+ const order = await (0, index_js_2.http)(`${this.host(serviceHost_js_1.httpServiceHost)}/${apiVersion_js_1.apiVersion}/swap/quote/${chainId}?${p.toString()}`);
434
+ return order;
435
+ }
436
+ });
437
+ Object.defineProperty(this, "quoteLastOrder", {
438
+ enumerable: true,
439
+ configurable: true,
440
+ writable: true,
441
+ value: async (uniqueId) => {
442
+ 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
443
  return order;
250
444
  }
251
445
  });
@@ -271,16 +465,58 @@ class Swap extends contextHost_js_1.ContextHost {
271
465
  (0, viem_1.isAddressEqual)(tokenAddress, viem_1.zeroAddress));
272
466
  }
273
467
  });
274
- Object.defineProperty(this, "estimate", {
468
+ Object.defineProperty(this, "estimateRFQOrder", {
275
469
  enumerable: true,
276
470
  configurable: true,
277
471
  writable: true,
278
- value: async ({ account, order, slippage, permit, }) => {
279
- const { swapData, value } = await this.prepareOrder({
472
+ value: async ({ account, order, takerTokenPermit, }) => {
473
+ const { encodeResult } = order;
474
+ const { rfqTx } = encodeResult;
475
+ const { rfqOffer } = rfqTx;
476
+ const { value } = this.formatRFQOrderParams(order);
477
+ const gasEstimate = await this.publicClient.estimateContractGas({
478
+ address: index_js_1.rfqAddress[this.chainId],
479
+ abi: index_js_1.RFQ_ABI,
480
+ functionName: 'fillRFQ',
481
+ args: [
482
+ [
483
+ [
484
+ rfqOffer.taker,
485
+ rfqOffer.maker,
486
+ rfqOffer.takerToken,
487
+ rfqOffer.takerTokenAmount,
488
+ rfqOffer.makerToken,
489
+ rfqOffer.makerTokenAmount,
490
+ rfqOffer.feeFactor,
491
+ rfqOffer.flags,
492
+ rfqOffer.expiry,
493
+ rfqOffer.salt,
494
+ ],
495
+ rfqTx.recipient,
496
+ rfqTx.takerRequestAmount,
497
+ ],
498
+ encodeResult.makerSignature,
499
+ encodeResult.makerTokenPermit,
500
+ takerTokenPermit,
501
+ ],
502
+ value,
503
+ account,
504
+ });
505
+ return gasEstimate;
506
+ }
507
+ });
508
+ Object.defineProperty(this, "estimateSOROrder", {
509
+ enumerable: true,
510
+ configurable: true,
511
+ writable: true,
512
+ value: async ({ account, order, slippage, takerTokenPermit, }) => {
513
+ const prepare = await this.prepareOrder({
280
514
  order,
281
515
  slippage,
282
516
  });
283
- const takerTokenPermit = permit ?? this.getTokenPermitData(swapData.takerToken);
517
+ const { swapData, value } = prepare;
518
+ takerTokenPermit =
519
+ takerTokenPermit ?? this.getTokenPermitData(swapData.takerToken);
284
520
  const gasEstimate = await this.publicClient.estimateContractGas({
285
521
  address: index_js_1.genericSwapAddress[this.chainId],
286
522
  abi: index_js_1.GENERIC_SWAP_ABI,
@@ -292,7 +528,82 @@ class Swap extends contextHost_js_1.ContextHost {
292
528
  return gasEstimate;
293
529
  }
294
530
  });
295
- Object.defineProperty(this, "sign", {
531
+ Object.defineProperty(this, "estimate", {
532
+ enumerable: true,
533
+ configurable: true,
534
+ writable: true,
535
+ value: async ({ account, order, slippage, takerTokenPermit, }) => {
536
+ if (order.orderType === type_js_1.OrderType.SOR) {
537
+ order = order;
538
+ const { swapData } = order.encodeResult;
539
+ takerTokenPermit =
540
+ takerTokenPermit ?? this.getTokenPermitData(swapData.takerToken);
541
+ return await this.estimateSOROrder({
542
+ account,
543
+ order: order,
544
+ slippage,
545
+ takerTokenPermit,
546
+ });
547
+ }
548
+ else if (order.orderType === type_js_1.OrderType.RFQV6) {
549
+ order = order;
550
+ const rfqOffer = order?.encodeResult?.rfqTx?.rfqOffer ?? {};
551
+ takerTokenPermit =
552
+ takerTokenPermit ?? this.getTokenPermitData(rfqOffer.takerToken);
553
+ return await this.estimateRFQOrder({
554
+ account,
555
+ order: order,
556
+ takerTokenPermit,
557
+ });
558
+ }
559
+ else {
560
+ throw new Error('unsupported order type');
561
+ }
562
+ }
563
+ });
564
+ Object.defineProperty(this, "signRFQOrder", {
565
+ enumerable: true,
566
+ configurable: true,
567
+ writable: true,
568
+ value: async (params) => {
569
+ const { account, value = 0n, txOverride, order, takerTokenPermit } = params;
570
+ const { encodeResult } = order;
571
+ const { rfqTx } = encodeResult;
572
+ const { rfqOffer } = rfqTx;
573
+ const txHash = await this.walletClient.writeContract({
574
+ address: index_js_1.rfqAddress[this.chainId],
575
+ abi: index_js_1.RFQ_ABI,
576
+ account,
577
+ functionName: 'fillRFQ',
578
+ args: [
579
+ [
580
+ [
581
+ rfqOffer.taker,
582
+ rfqOffer.maker,
583
+ rfqOffer.takerToken,
584
+ rfqOffer.takerTokenAmount,
585
+ rfqOffer.makerToken,
586
+ rfqOffer.makerTokenAmount,
587
+ rfqOffer.feeFactor,
588
+ rfqOffer.flags,
589
+ rfqOffer.expiry,
590
+ rfqOffer.salt,
591
+ ],
592
+ rfqTx.recipient,
593
+ rfqTx.takerRequestAmount,
594
+ ],
595
+ encodeResult.makerSignature,
596
+ encodeResult.makerTokenPermit,
597
+ takerTokenPermit,
598
+ ],
599
+ value,
600
+ chain: this.walletClient.chain,
601
+ ...txOverride,
602
+ });
603
+ return txHash;
604
+ }
605
+ });
606
+ Object.defineProperty(this, "signSOROrder", {
296
607
  enumerable: true,
297
608
  configurable: true,
298
609
  writable: true,
@@ -311,6 +622,58 @@ class Swap extends contextHost_js_1.ContextHost {
311
622
  return txHash;
312
623
  }
313
624
  });
625
+ Object.defineProperty(this, "sign", {
626
+ enumerable: true,
627
+ configurable: true,
628
+ writable: true,
629
+ value: async (params) => {
630
+ let { account, order, slippage, takerTokenPermit, txOverride, sendByRelayer, } = params;
631
+ const gas = txOverride?.gas ??
632
+ (await this.estimate({
633
+ account,
634
+ order,
635
+ slippage,
636
+ }));
637
+ const gasPrice = txOverride?.gasPrice ?? (await this.publicClient.getGasPrice());
638
+ const gasFee = (gas * gasPrice * 120n) / 100n;
639
+ txOverride = txOverride;
640
+ if (order.orderType === type_js_1.OrderType.SOR) {
641
+ const prepare = await this.prepareOrder({
642
+ order,
643
+ slippage,
644
+ gasFee,
645
+ sendByRelayer,
646
+ });
647
+ const { swapData, value } = prepare;
648
+ takerTokenPermit =
649
+ takerTokenPermit ?? this.getTokenPermitData(swapData.takerToken);
650
+ return await this.signSOROrder({
651
+ account,
652
+ value,
653
+ takerTokenPermit,
654
+ swapData,
655
+ txOverride,
656
+ });
657
+ }
658
+ else if (order.orderType === type_js_1.OrderType.RFQV6) {
659
+ order = order;
660
+ const rfqOffer = order?.encodeResult?.rfqTx?.rfqOffer ?? {};
661
+ takerTokenPermit =
662
+ takerTokenPermit ?? this.getTokenPermitData(rfqOffer.takerToken);
663
+ const { value } = this.formatRFQOrderParams(order);
664
+ return await this.signRFQOrder({
665
+ account,
666
+ order,
667
+ takerTokenPermit,
668
+ value: value,
669
+ txOverride,
670
+ });
671
+ }
672
+ else {
673
+ throw new Error('unsupported order type');
674
+ }
675
+ }
676
+ });
314
677
  Object.defineProperty(this, "signWithReceipt", {
315
678
  enumerable: true,
316
679
  configurable: true,
@@ -323,7 +686,7 @@ class Swap extends contextHost_js_1.ContextHost {
323
686
  return receipt;
324
687
  }
325
688
  });
326
- Object.defineProperty(this, "signTypedData", {
689
+ Object.defineProperty(this, "sign712SOROrder", {
327
690
  enumerable: true,
328
691
  configurable: true,
329
692
  writable: true,
@@ -334,28 +697,138 @@ class Swap extends contextHost_js_1.ContextHost {
334
697
  chainId: this.chainId,
335
698
  verifyingContract: index_js_1.genericSwapAddress[this.chainId],
336
699
  };
337
- const EIP712_TYPED = {
700
+ return this.walletClient.signTypedData({
701
+ primaryType: 'GenericSwapData',
338
702
  domain: EIP712_DOMAIN,
339
703
  types: index_js_1.GS_EIP712_TYPES,
340
- primaryType: 'GenericSwapData',
704
+ message: swapData,
705
+ account,
706
+ });
707
+ }
708
+ });
709
+ Object.defineProperty(this, "sign712RFQOrder", {
710
+ enumerable: true,
711
+ configurable: true,
712
+ writable: true,
713
+ value: async (rfqTx, account) => {
714
+ const EIP712_DOMAIN = {
715
+ name: 'Tokenlon',
716
+ version: 'v6',
717
+ chainId: this.chainId,
718
+ verifyingContract: index_js_1.rfqAddress[this.chainId],
719
+ };
720
+ const types = {
721
+ RFQOffer: [
722
+ { name: 'taker', type: 'address' },
723
+ { name: 'maker', type: 'address' },
724
+ { name: 'takerToken', type: 'address' },
725
+ { name: 'takerTokenAmount', type: 'uint256' },
726
+ { name: 'makerToken', type: 'address' },
727
+ { name: 'makerTokenAmount', type: 'uint256' },
728
+ { name: 'feeFactor', type: 'uint256' },
729
+ { name: 'flags', type: 'uint256' },
730
+ { name: 'expiry', type: 'uint256' },
731
+ { name: 'salt', type: 'uint256' },
732
+ ],
733
+ RFQTx: [
734
+ { name: 'rfqOffer', type: 'RFQOffer' },
735
+ { name: 'recipient', type: 'address' },
736
+ { name: 'takerRequestAmount', type: 'uint256' },
737
+ ],
341
738
  };
342
739
  return this.walletClient.signTypedData({
343
- ...EIP712_TYPED,
344
- message: swapData,
740
+ primaryType: 'RFQTx',
741
+ domain: EIP712_DOMAIN,
742
+ types: types,
743
+ message: rfqTx,
345
744
  account,
346
745
  });
347
746
  }
348
747
  });
748
+ Object.defineProperty(this, "signTypedData", {
749
+ enumerable: true,
750
+ configurable: true,
751
+ writable: true,
752
+ value: async (params) => {
753
+ let { account, order, takerTokenPermit, prepare } = params;
754
+ if (order.orderType === type_js_1.OrderType.SOR) {
755
+ if (!prepare) {
756
+ throw new Error('prepare is empty');
757
+ }
758
+ const { swapData } = prepare;
759
+ takerTokenPermit =
760
+ takerTokenPermit ?? this.getTokenPermitData(swapData.takerToken);
761
+ return this.sign712SOROrder(swapData, account);
762
+ }
763
+ else if (order.orderType === type_js_1.OrderType.RFQV6) {
764
+ const rfqOrder = order;
765
+ const { encodeResult } = rfqOrder;
766
+ const { rfqTx } = encodeResult;
767
+ const rfqOffer = rfqTx.rfqOffer;
768
+ takerTokenPermit =
769
+ takerTokenPermit ?? this.getTokenPermitData(rfqOffer.takerToken);
770
+ return this.sign712RFQOrder(rfqTx, account);
771
+ }
772
+ else {
773
+ throw new Error('unsupported order type');
774
+ }
775
+ }
776
+ });
349
777
  Object.defineProperty(this, "submit", {
350
778
  enumerable: true,
351
779
  configurable: true,
352
780
  writable: true,
353
- value: async (dto) => {
354
- return (0, index_js_2.http)(`${this.host(serviceHost_js_1.httpServiceHost)}/v6/swap/submit/${this.chainId}`, {
781
+ value: async (params) => {
782
+ let { order, slippage, takerTokenPermit, txHash, rawTx, gasFee, sendByRelayer, orderSignature, } = params;
783
+ const request = {
784
+ uniqueId: order.uniqueId,
785
+ slippage,
786
+ permit: takerTokenPermit,
787
+ fee: {
788
+ protocolFee: 0n,
789
+ gasFee: 0n,
790
+ },
791
+ };
792
+ if (orderSignature)
793
+ request.orderSignature = orderSignature;
794
+ if (txHash)
795
+ request.txHash = txHash;
796
+ if (rawTx)
797
+ request.rawTx = rawTx;
798
+ if (order.orderType === type_js_1.OrderType.SOR) {
799
+ const prepare = await this.prepareOrder({
800
+ order,
801
+ slippage,
802
+ gasFee: gasFee,
803
+ sendByRelayer,
804
+ });
805
+ const { swapData, protocolFee, gasFeeTokenAmount } = prepare;
806
+ request.strategyData = swapData.strategyData;
807
+ request.fee = {
808
+ protocolFee: protocolFee,
809
+ gasFee: gasFeeTokenAmount,
810
+ };
811
+ request.minAmountOut = swapData.minMakerTokenAmount;
812
+ }
813
+ else if (order.orderType === type_js_1.OrderType.RFQV6) {
814
+ order = order;
815
+ const rfqOrderParams = this.formatRFQOrderParams(order);
816
+ const { fee, minMakerTokenAmount } = rfqOrderParams;
817
+ request.slippage = 0;
818
+ request.minAmountOut = minMakerTokenAmount;
819
+ request.fee = {
820
+ gasFee: 0n,
821
+ protocolFee: fee,
822
+ };
823
+ }
824
+ else {
825
+ throw new Error('unsupported order type');
826
+ }
827
+ return (0, index_js_2.http)(`${this.host(serviceHost_js_1.httpServiceHost)}/${apiVersion_js_1.apiVersion}/swap/submit/${this.chainId}`, {
355
828
  fetchOptions: {
356
829
  method: 'POST',
357
830
  },
358
- body: dto,
831
+ body: request,
359
832
  });
360
833
  }
361
834
  });
@@ -378,7 +851,7 @@ class Swap extends contextHost_js_1.ContextHost {
378
851
  const params = new URLSearchParams({
379
852
  ...dto,
380
853
  });
381
- return (0, index_js_2.http)(`${this.host(serviceHost_js_1.httpServiceHost)}/v6/swap/status/${this.chainId}?${params}`);
854
+ return (0, index_js_2.http)(`${this.host(serviceHost_js_1.httpServiceHost)}/${apiVersion_js_1.apiVersion}/swap/status/${this.chainId}?${params}`);
382
855
  }
383
856
  });
384
857
  Object.defineProperty(this, "config", {
@@ -386,7 +859,7 @@ class Swap extends contextHost_js_1.ContextHost {
386
859
  configurable: true,
387
860
  writable: true,
388
861
  value: async (appVersion) => {
389
- return (0, index_js_2.http)(`${this.host(serviceHost_js_1.httpServiceHost)}/v6/swap/config?appVersion=${appVersion}`);
862
+ return (0, index_js_2.http)(`${this.host(serviceHost_js_1.httpServiceHost)}/${apiVersion_js_1.apiVersion}/swap/config?appVersion=${appVersion}`);
390
863
  }
391
864
  });
392
865
  this.walletClient = injected.walletClient;