@tokenlon/v6-sdk 1.0.2 → 1.0.3-canary.3c1bacd

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 (69) 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 +18 -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/sdk.js +7 -5
  14. package/dist/cjs/sdk.js.map +1 -1
  15. package/dist/cjs/serviceHost.js +6 -0
  16. package/dist/cjs/serviceHost.js.map +1 -1
  17. package/dist/cjs/swap/index.js +491 -80
  18. package/dist/cjs/swap/index.js.map +1 -1
  19. package/dist/cjs/swap/type.js +6 -1
  20. package/dist/cjs/swap/type.js.map +1 -1
  21. package/dist/cjs/version.js +1 -1
  22. package/dist/cjs/version.js.map +1 -1
  23. package/dist/esm/apiVersion.js +2 -0
  24. package/dist/esm/apiVersion.js.map +1 -0
  25. package/dist/esm/constants/RFQAbi.js +721 -0
  26. package/dist/esm/constants/RFQAbi.js.map +1 -0
  27. package/dist/esm/constants/abis.js +300 -0
  28. package/dist/esm/constants/abis.js.map +1 -1
  29. package/dist/esm/constants/addresses.js +17 -0
  30. package/dist/esm/constants/addresses.js.map +1 -1
  31. package/dist/esm/constants/chains.js +3 -3
  32. package/dist/esm/constants/chains.js.map +1 -1
  33. package/dist/esm/constants/unit.js +1 -0
  34. package/dist/esm/constants/unit.js.map +1 -1
  35. package/dist/esm/sdk.js +7 -5
  36. package/dist/esm/sdk.js.map +1 -1
  37. package/dist/esm/serviceHost.js +7 -1
  38. package/dist/esm/serviceHost.js.map +1 -1
  39. package/dist/esm/swap/index.js +490 -80
  40. package/dist/esm/swap/index.js.map +1 -1
  41. package/dist/esm/swap/type.js +5 -0
  42. package/dist/esm/swap/type.js.map +1 -1
  43. package/dist/esm/version.js +1 -1
  44. package/dist/esm/version.js.map +1 -1
  45. package/dist/tsconfig.build.tsbuildinfo +1 -1
  46. package/dist/types/apiVersion.d.ts +2 -0
  47. package/dist/types/apiVersion.d.ts.map +1 -0
  48. package/dist/types/constants/RFQAbi.d.ts +82 -0
  49. package/dist/types/constants/RFQAbi.d.ts.map +1 -0
  50. package/dist/types/constants/abis.d.ts +336 -0
  51. package/dist/types/constants/abis.d.ts.map +1 -1
  52. package/dist/types/constants/addresses.d.ts +2 -0
  53. package/dist/types/constants/addresses.d.ts.map +1 -1
  54. package/dist/types/constants/chains.d.ts +1 -1
  55. package/dist/types/constants/chains.d.ts.map +1 -1
  56. package/dist/types/constants/unit.d.ts +1 -0
  57. package/dist/types/constants/unit.d.ts.map +1 -1
  58. package/dist/types/errors/base.d.ts +1 -1
  59. package/dist/types/errors/base.d.ts.map +1 -1
  60. package/dist/types/sdk.d.ts +1 -0
  61. package/dist/types/sdk.d.ts.map +1 -1
  62. package/dist/types/serviceHost.d.ts.map +1 -1
  63. package/dist/types/swap/index.d.ts +103 -19
  64. package/dist/types/swap/index.d.ts.map +1 -1
  65. package/dist/types/swap/type.d.ts +54 -18
  66. package/dist/types/swap/type.d.ts.map +1 -1
  67. package/dist/types/version.d.ts +1 -1
  68. package/dist/types/version.d.ts.map +1 -1
  69. package/package.json +28 -21
@@ -1,13 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Swap = exports.ERC20_TRANSFER_GAS = void 0;
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
- const 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, uint128 inputRatio, uint128 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) {
@@ -59,18 +61,45 @@ class Swap extends contextHost_js_1.ContextHost {
59
61
  };
60
62
  newOperations.push(feeOperation);
61
63
  }
62
- const newStrategyData = (0, viem_1.encodeAbiParameters)(strategyOperationsABI, [
64
+ const newStrategyData = (0, viem_1.encodeAbiParameters)(exports.strategyOperationsABI, [
63
65
  newOperations,
64
66
  ]);
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: rfqOffer.makerTokenAmount,
90
+ takerTokenAmount: 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;
100
+ const encodeResult = order?.encodeResult ?? {};
101
+ const { feeFactor } = order;
102
+ const { swapData } = encodeResult;
74
103
  const { makerTokenAmount } = swapData;
75
104
  const protocolFeeAmount = (BigInt(makerTokenAmount) * BigInt(feeFactor)) / index_js_1.bpBase;
76
105
  return protocolFeeAmount;
@@ -81,8 +110,10 @@ class Swap extends contextHost_js_1.ContextHost {
81
110
  configurable: true,
82
111
  writable: true,
83
112
  value: (order, slippage) => {
84
- const { swapData } = order;
85
- const { makerTokenAmount } = swapData;
113
+ const isSOROrder = order?.orderType === type_js_1.OrderType.SOR;
114
+ const makerTokenAmount = isSOROrder
115
+ ? order?.encodeResult?.swapData?.makerTokenAmount
116
+ : order?.encodeResult?.rfqTx?.rfqOffer?.makerTokenAmount;
86
117
  const slippageAmount = (BigInt(makerTokenAmount) * BigInt(slippage)) / index_js_1.bpBase;
87
118
  return slippageAmount;
88
119
  }
@@ -112,39 +143,60 @@ class Swap extends contextHost_js_1.ContextHost {
112
143
  enumerable: true,
113
144
  configurable: true,
114
145
  writable: true,
115
- value: async (fromToken, permitAddresses = []) => {
116
- if ((0, viem_1.isAddressEqual)(fromToken, viem_1.zeroAddress) ||
117
- (0, viem_1.isAddressEqual)(fromToken, index_js_1.nullAddress))
146
+ value: async (takerTokenAmount, needApprovalDestinations) => {
147
+ if (!Array.isArray(needApprovalDestinations) ||
148
+ needApprovalDestinations.length === 0)
118
149
  return [];
119
150
  const allowances = await Promise.all([
120
- ...permitAddresses.map((permitAddress) => this.publicClient.readContract({
151
+ ...needApprovalDestinations.map(({ dest, inputToken }) => this.publicClient.readContract({
121
152
  abi: [
122
153
  (0, viem_1.parseAbiItem)('function allowance(address owner, address spender) view returns (uint256)'),
123
154
  ],
124
155
  functionName: 'allowance',
125
- address: fromToken,
126
- args: [index_js_1.strategyAddress[this.chainId], permitAddress],
156
+ address: inputToken,
157
+ args: [index_js_1.strategyAddress[this.chainId], dest],
127
158
  })),
128
159
  ]);
129
160
  const approveStrategies = allowances.reduce((acc, allowance, index) => {
130
161
  try {
131
- if (BigInt(allowance) !== index_js_1.MAX_UINT256) {
162
+ if (BigInt(allowance) < BigInt(takerTokenAmount)) {
163
+ const { dest, inputToken } = needApprovalDestinations[index];
132
164
  const approve = (0, viem_1.encodeFunctionData)({
133
165
  abi: [
134
166
  (0, viem_1.parseAbiItem)('function approve(address spender, uint256 amount) returns (bool)'),
135
167
  ],
136
168
  functionName: 'approve',
137
- args: [permitAddresses[index], index_js_1.MAX_UINT256],
169
+ args: [dest, index_js_1.MAX_UINT256],
138
170
  });
139
171
  const approveOperation = {
140
- dest: fromToken,
141
- inputToken: fromToken,
172
+ dest: inputToken,
173
+ inputToken: inputToken,
142
174
  inputRatio: 0n,
143
175
  dataOffset: 0n,
144
176
  value: 0n,
145
177
  data: approve,
146
178
  };
147
179
  acc.push(approveOperation);
180
+ if (inputToken.toLowerCase() ===
181
+ index_js_1.usdtAddress[this.chainId]?.toLowerCase() &&
182
+ BigInt(allowance) > 0n) {
183
+ const approve2Zero = (0, viem_1.encodeFunctionData)({
184
+ abi: [
185
+ (0, viem_1.parseAbiItem)('function approve(address spender, uint256 amount) returns (bool)'),
186
+ ],
187
+ functionName: 'approve',
188
+ args: [dest, 0n],
189
+ });
190
+ const approveOperation = {
191
+ dest: inputToken,
192
+ inputToken: inputToken,
193
+ inputRatio: 0n,
194
+ dataOffset: 0n,
195
+ value: 0n,
196
+ data: approve2Zero,
197
+ };
198
+ acc.unshift(approveOperation);
199
+ }
148
200
  return acc;
149
201
  }
150
202
  return acc;
@@ -156,60 +208,142 @@ class Swap extends contextHost_js_1.ContextHost {
156
208
  return approveStrategies;
157
209
  }
158
210
  });
211
+ Object.defineProperty(this, "getRFQFeeAmount", {
212
+ enumerable: true,
213
+ configurable: true,
214
+ writable: true,
215
+ value: (order) => {
216
+ const encodeResult = order?.encodeResult ?? {};
217
+ const { rfqTx } = encodeResult;
218
+ const { rfqOffer } = rfqTx;
219
+ const protocolFeeAmount = (BigInt(rfqOffer.makerTokenAmount) * BigInt(rfqOffer.feeFactor)) / index_js_1.bpBase;
220
+ return protocolFeeAmount;
221
+ }
222
+ });
223
+ Object.defineProperty(this, "preview", {
224
+ enumerable: true,
225
+ configurable: true,
226
+ writable: true,
227
+ value: async (params) => {
228
+ let preview;
229
+ let { order, account, slippage } = params;
230
+ const gas = await this.estimate({
231
+ account,
232
+ order,
233
+ slippage,
234
+ }).catch(() => index_js_1.DEFAULT_GAS_LIMIT);
235
+ if (order.orderType === type_js_1.OrderType.SOR) {
236
+ order = order;
237
+ const sorPrepare = await this.prepareOrder(params);
238
+ const { swapData, fee } = sorPrepare;
239
+ preview = {
240
+ inputToken: swapData.takerToken,
241
+ outputToken: swapData.makerToken,
242
+ amountIn: swapData.takerTokenAmount,
243
+ amountOut: swapData.makerTokenAmount,
244
+ minAmountOut: swapData.minMakerTokenAmount,
245
+ recipient: swapData.recipient,
246
+ fee,
247
+ gas,
248
+ feeFactor: order.feeFactor,
249
+ };
250
+ }
251
+ else if (order.orderType === type_js_1.OrderType.RFQV6) {
252
+ order = order;
253
+ const { rfqTx } = order.encodeResult;
254
+ const { rfqOffer } = rfqTx;
255
+ const { fee, minMakerTokenAmount } = this.formatRFQOrderParams(order);
256
+ preview = {
257
+ inputToken: rfqOffer.takerToken,
258
+ outputToken: rfqOffer.makerToken,
259
+ amountIn: rfqOffer.takerTokenAmount,
260
+ amountOut: rfqOffer.makerTokenAmount,
261
+ minAmountOut: minMakerTokenAmount,
262
+ recipient: rfqTx.recipient,
263
+ fee,
264
+ gas,
265
+ feeFactor: order.feeFactor,
266
+ };
267
+ }
268
+ else {
269
+ throw new Error('unsupported order type');
270
+ }
271
+ return preview;
272
+ }
273
+ });
159
274
  Object.defineProperty(this, "prepareOrder", {
160
275
  enumerable: true,
161
276
  configurable: true,
162
277
  writable: true,
163
278
  value: async ({ order, slippage, gasFee = 0n, sendByRelayer, }) => {
164
- const { swapData } = order;
165
- const { recipient, strategyData, makerToken } = swapData;
166
- const makerTokenAmount = BigInt(swapData.makerTokenAmount);
167
- const slippageAmount = this.getSlippageAmount(order, slippage);
168
- const protocolFeeAmount = this.getProtocolFeeAmount(order);
169
- let fee = protocolFeeAmount;
170
- let feeTokenAmount = gasFee;
171
- if (sendByRelayer) {
172
- feeTokenAmount = await this.calcGasFeeTokenAmount({
173
- gasFeeAmount: gasFee,
174
- feeToken: makerToken,
175
- });
176
- fee = await this.calcRelayerFee({
177
- protocolFeeAmount,
178
- feeTokenAmount,
179
- });
279
+ let prepare;
280
+ if (order.orderType === type_js_1.OrderType.SOR) {
281
+ order = order;
282
+ const encodeResult = order.encodeResult ?? {};
283
+ const { swapData, needApprovalDestinations } = encodeResult;
284
+ const { recipient, strategyData, makerToken } = swapData;
285
+ const makerTokenAmount = BigInt(swapData.makerTokenAmount);
286
+ const slippageAmount = this.getSlippageAmount(order, slippage);
287
+ const protocolFeeAmount = this.getProtocolFeeAmount(order);
288
+ let fee = protocolFeeAmount;
289
+ let feeTokenAmount = gasFee;
290
+ if (sendByRelayer) {
291
+ feeTokenAmount = await this.calcGasFeeTokenAmount({
292
+ gasFeeAmount: gasFee,
293
+ feeToken: makerToken,
294
+ });
295
+ fee = await this.calcRelayerFee({
296
+ protocolFeeAmount,
297
+ feeTokenAmount,
298
+ });
299
+ }
300
+ const minMakerTokenAmount = makerTokenAmount - slippageAmount - fee;
301
+ swapData.minMakerTokenAmount = minMakerTokenAmount.toString();
302
+ const [operations] = (0, viem_1.decodeAbiParameters)(exports.strategyOperationsABI, strategyData);
303
+ const approveOperations = await this.getRoutesApproveStrategy(swapData.takerTokenAmount, needApprovalDestinations);
304
+ const newStrategyData = this.rebuildStrategyData(fee, swapData, [
305
+ ...approveOperations,
306
+ ...operations,
307
+ ]);
308
+ const value = (0, viem_1.isAddressEqual)(swapData.takerToken, index_js_1.nullAddress) ||
309
+ (0, viem_1.isAddressEqual)(swapData.takerToken, viem_1.zeroAddress)
310
+ ? BigInt(swapData.takerTokenAmount)
311
+ : 0n;
312
+ const newSwapData = {
313
+ ...swapData,
314
+ strategyData: newStrategyData,
315
+ takerTokenAmount: BigInt(swapData.takerTokenAmount),
316
+ makerTokenAmount: BigInt(swapData.makerTokenAmount),
317
+ minMakerTokenAmount: BigInt(swapData.minMakerTokenAmount),
318
+ salt: BigInt(swapData.salt),
319
+ expiry: BigInt(swapData.expiry),
320
+ };
321
+ prepare = {
322
+ swapData: newSwapData,
323
+ protocolFee: protocolFeeAmount,
324
+ gasFeeTokenAmount: feeTokenAmount,
325
+ gasFee,
326
+ fee,
327
+ recipient,
328
+ operations,
329
+ strategyData: newStrategyData,
330
+ value,
331
+ minMakerTokenAmount,
332
+ makerTokenAmount: newSwapData.makerTokenAmount,
333
+ takerTokenAmount: newSwapData.takerTokenAmount,
334
+ };
180
335
  }
181
- const minMakerTokenAmount = makerTokenAmount - slippageAmount - fee;
182
- swapData.minMakerTokenAmount = minMakerTokenAmount.toString();
183
- const [operations] = (0, viem_1.decodeAbiParameters)(strategyOperationsABI, strategyData);
184
- const approveOperations = await this.getRoutesApproveStrategy(swapData.takerToken, order.permitAddresses);
185
- const newStrategyData = this.rebuildStrategyData(fee, swapData, [
186
- ...approveOperations,
187
- ...operations,
188
- ]);
189
- const value = (0, viem_1.isAddressEqual)(swapData.takerToken, index_js_1.nullAddress) ||
190
- (0, viem_1.isAddressEqual)(swapData.takerToken, viem_1.zeroAddress)
191
- ? BigInt(swapData.takerTokenAmount)
192
- : 0n;
193
- const newSwapData = {
194
- ...swapData,
195
- strategyData: newStrategyData,
196
- takerTokenAmount: BigInt(swapData.takerTokenAmount),
197
- makerTokenAmount: BigInt(swapData.makerTokenAmount),
198
- minMakerTokenAmount: BigInt(swapData.minMakerTokenAmount),
199
- salt: BigInt(swapData.salt),
200
- expiry: BigInt(swapData.expiry),
201
- };
202
- return {
203
- swapData: newSwapData,
204
- protocolFee: protocolFeeAmount,
205
- gasFeeTokenAmount: feeTokenAmount,
206
- gasFee,
207
- fee,
208
- recipient,
209
- operations,
210
- strategyData: newStrategyData,
211
- value,
212
- };
336
+ else if (order.orderType === type_js_1.OrderType.RFQV6) {
337
+ order = order;
338
+ const rfqOrderParams = this.formatRFQOrderParams(order);
339
+ prepare = {
340
+ ...rfqOrderParams,
341
+ };
342
+ }
343
+ else {
344
+ throw new Error('unsupported order type');
345
+ }
346
+ return prepare;
213
347
  }
214
348
  });
215
349
  Object.defineProperty(this, "quote", {
@@ -225,7 +359,7 @@ class Swap extends contextHost_js_1.ContextHost {
225
359
  Object.entries(dto).forEach(([key, value]) => {
226
360
  p.append(key, value.toString());
227
361
  });
228
- const order = await (0, index_js_2.http)(`${this.host(serviceHost_js_1.httpServiceHost)}/v6/swap/quote/${chainId}?${p.toString()}`);
362
+ const order = await (0, index_js_2.http)(`${this.host(serviceHost_js_1.httpServiceHost)}/${apiVersion_js_1.apiVersion}/swap/quote/${chainId}?${p.toString()}`);
229
363
  return order;
230
364
  }
231
365
  });
@@ -251,16 +385,58 @@ class Swap extends contextHost_js_1.ContextHost {
251
385
  (0, viem_1.isAddressEqual)(tokenAddress, viem_1.zeroAddress));
252
386
  }
253
387
  });
254
- Object.defineProperty(this, "estimate", {
388
+ Object.defineProperty(this, "estimateRFQOrder", {
389
+ enumerable: true,
390
+ configurable: true,
391
+ writable: true,
392
+ value: async ({ account, order, takerTokenPermit, }) => {
393
+ const { encodeResult } = order;
394
+ const { rfqTx } = encodeResult;
395
+ const { rfqOffer } = rfqTx;
396
+ const { value } = this.formatRFQOrderParams(order);
397
+ const gasEstimate = await this.publicClient.estimateContractGas({
398
+ address: index_js_1.rfqAddress[this.chainId],
399
+ abi: index_js_1.RFQ_ABI,
400
+ functionName: 'fillRFQ',
401
+ args: [
402
+ [
403
+ [
404
+ rfqOffer.taker,
405
+ rfqOffer.maker,
406
+ rfqOffer.takerToken,
407
+ rfqOffer.takerTokenAmount,
408
+ rfqOffer.makerToken,
409
+ rfqOffer.makerTokenAmount,
410
+ rfqOffer.feeFactor,
411
+ rfqOffer.flags,
412
+ rfqOffer.expiry,
413
+ rfqOffer.salt,
414
+ ],
415
+ rfqTx.recipient,
416
+ rfqTx.takerRequestAmount,
417
+ ],
418
+ encodeResult.makerSignature,
419
+ encodeResult.makerTokenPermit,
420
+ takerTokenPermit,
421
+ ],
422
+ value,
423
+ account,
424
+ });
425
+ return gasEstimate;
426
+ }
427
+ });
428
+ Object.defineProperty(this, "estimateSOROrder", {
255
429
  enumerable: true,
256
430
  configurable: true,
257
431
  writable: true,
258
- value: async ({ account, order, slippage, permit, }) => {
259
- const { swapData, value } = await this.prepareOrder({
432
+ value: async ({ account, order, slippage, takerTokenPermit, }) => {
433
+ const prepare = await this.prepareOrder({
260
434
  order,
261
435
  slippage,
262
436
  });
263
- const takerTokenPermit = permit ?? this.getTokenPermitData(swapData.takerToken);
437
+ const { swapData, value } = prepare;
438
+ takerTokenPermit =
439
+ takerTokenPermit ?? this.getTokenPermitData(swapData.takerToken);
264
440
  const gasEstimate = await this.publicClient.estimateContractGas({
265
441
  address: index_js_1.genericSwapAddress[this.chainId],
266
442
  abi: index_js_1.GENERIC_SWAP_ABI,
@@ -272,7 +448,82 @@ class Swap extends contextHost_js_1.ContextHost {
272
448
  return gasEstimate;
273
449
  }
274
450
  });
275
- Object.defineProperty(this, "sign", {
451
+ Object.defineProperty(this, "estimate", {
452
+ enumerable: true,
453
+ configurable: true,
454
+ writable: true,
455
+ value: async ({ account, order, slippage, takerTokenPermit, }) => {
456
+ if (order.orderType === type_js_1.OrderType.SOR) {
457
+ order = order;
458
+ const { swapData } = order.encodeResult;
459
+ takerTokenPermit =
460
+ takerTokenPermit ?? this.getTokenPermitData(swapData.takerToken);
461
+ return await this.estimateSOROrder({
462
+ account,
463
+ order: order,
464
+ slippage,
465
+ takerTokenPermit,
466
+ });
467
+ }
468
+ else if (order.orderType === type_js_1.OrderType.RFQV6) {
469
+ order = order;
470
+ const rfqOffer = order?.encodeResult?.rfqTx?.rfqOffer ?? {};
471
+ takerTokenPermit =
472
+ takerTokenPermit ?? this.getTokenPermitData(rfqOffer.takerToken);
473
+ return await this.estimateRFQOrder({
474
+ account,
475
+ order: order,
476
+ takerTokenPermit,
477
+ });
478
+ }
479
+ else {
480
+ throw new Error('unsupported order type');
481
+ }
482
+ }
483
+ });
484
+ Object.defineProperty(this, "signRFQOrder", {
485
+ enumerable: true,
486
+ configurable: true,
487
+ writable: true,
488
+ value: async (params) => {
489
+ const { account, value = 0n, txOverride, order, takerTokenPermit } = params;
490
+ const { encodeResult } = order;
491
+ const { rfqTx } = encodeResult;
492
+ const { rfqOffer } = rfqTx;
493
+ const txHash = await this.walletClient.writeContract({
494
+ address: index_js_1.rfqAddress[this.chainId],
495
+ abi: index_js_1.RFQ_ABI,
496
+ account,
497
+ functionName: 'fillRFQ',
498
+ args: [
499
+ [
500
+ [
501
+ rfqOffer.taker,
502
+ rfqOffer.maker,
503
+ rfqOffer.takerToken,
504
+ rfqOffer.takerTokenAmount,
505
+ rfqOffer.makerToken,
506
+ rfqOffer.makerTokenAmount,
507
+ rfqOffer.feeFactor,
508
+ rfqOffer.flags,
509
+ rfqOffer.expiry,
510
+ rfqOffer.salt,
511
+ ],
512
+ rfqTx.recipient,
513
+ rfqTx.takerRequestAmount,
514
+ ],
515
+ encodeResult.makerSignature,
516
+ encodeResult.makerTokenPermit,
517
+ takerTokenPermit,
518
+ ],
519
+ value,
520
+ chain: this.walletClient.chain,
521
+ ...txOverride,
522
+ });
523
+ return txHash;
524
+ }
525
+ });
526
+ Object.defineProperty(this, "signSOROrder", {
276
527
  enumerable: true,
277
528
  configurable: true,
278
529
  writable: true,
@@ -291,6 +542,58 @@ class Swap extends contextHost_js_1.ContextHost {
291
542
  return txHash;
292
543
  }
293
544
  });
545
+ Object.defineProperty(this, "sign", {
546
+ enumerable: true,
547
+ configurable: true,
548
+ writable: true,
549
+ value: async (params) => {
550
+ let { account, order, slippage, takerTokenPermit, txOverride, sendByRelayer, } = params;
551
+ const gas = txOverride?.gas ??
552
+ (await this.estimate({
553
+ account,
554
+ order,
555
+ slippage,
556
+ }));
557
+ const gasPrice = txOverride?.gasPrice ?? (await this.publicClient.getGasPrice());
558
+ const gasFee = (gas * gasPrice * 120n) / 100n;
559
+ txOverride = txOverride;
560
+ if (order.orderType === type_js_1.OrderType.SOR) {
561
+ const prepare = await this.prepareOrder({
562
+ order,
563
+ slippage,
564
+ gasFee,
565
+ sendByRelayer,
566
+ });
567
+ const { swapData, value } = prepare;
568
+ takerTokenPermit =
569
+ takerTokenPermit ?? this.getTokenPermitData(swapData.takerToken);
570
+ return await this.signSOROrder({
571
+ account,
572
+ value,
573
+ takerTokenPermit,
574
+ swapData,
575
+ txOverride,
576
+ });
577
+ }
578
+ else if (order.orderType === type_js_1.OrderType.RFQV6) {
579
+ order = order;
580
+ const rfqOffer = order?.encodeResult?.rfqTx?.rfqOffer ?? {};
581
+ takerTokenPermit =
582
+ takerTokenPermit ?? this.getTokenPermitData(rfqOffer.takerToken);
583
+ const { value } = this.formatRFQOrderParams(order);
584
+ return await this.signRFQOrder({
585
+ account,
586
+ order,
587
+ takerTokenPermit,
588
+ value: value,
589
+ txOverride,
590
+ });
591
+ }
592
+ else {
593
+ throw new Error('unsupported order type');
594
+ }
595
+ }
596
+ });
294
597
  Object.defineProperty(this, "signWithReceipt", {
295
598
  enumerable: true,
296
599
  configurable: true,
@@ -303,7 +606,7 @@ class Swap extends contextHost_js_1.ContextHost {
303
606
  return receipt;
304
607
  }
305
608
  });
306
- Object.defineProperty(this, "signTypedData", {
609
+ Object.defineProperty(this, "sign712SOROrder", {
307
610
  enumerable: true,
308
611
  configurable: true,
309
612
  writable: true,
@@ -314,28 +617,136 @@ class Swap extends contextHost_js_1.ContextHost {
314
617
  chainId: this.chainId,
315
618
  verifyingContract: index_js_1.genericSwapAddress[this.chainId],
316
619
  };
317
- const EIP712_TYPED = {
620
+ return this.walletClient.signTypedData({
621
+ primaryType: 'GenericSwapData',
318
622
  domain: EIP712_DOMAIN,
319
623
  types: index_js_1.GS_EIP712_TYPES,
320
- primaryType: 'GenericSwapData',
624
+ message: swapData,
625
+ account,
626
+ });
627
+ }
628
+ });
629
+ Object.defineProperty(this, "sign712RFQOrder", {
630
+ enumerable: true,
631
+ configurable: true,
632
+ writable: true,
633
+ value: async (rfqTx, account) => {
634
+ const EIP712_DOMAIN = {
635
+ name: 'Tokenlon',
636
+ version: 'v6',
637
+ chainId: this.chainId,
638
+ verifyingContract: index_js_1.rfqAddress[this.chainId],
639
+ };
640
+ const types = {
641
+ RFQOffer: [
642
+ { name: 'taker', type: 'address' },
643
+ { name: 'maker', type: 'address' },
644
+ { name: 'takerToken', type: 'address' },
645
+ { name: 'takerTokenAmount', type: 'uint256' },
646
+ { name: 'makerToken', type: 'address' },
647
+ { name: 'makerTokenAmount', type: 'uint256' },
648
+ { name: 'feeFactor', type: 'uint256' },
649
+ { name: 'flags', type: 'uint256' },
650
+ { name: 'expiry', type: 'uint256' },
651
+ { name: 'salt', type: 'uint256' },
652
+ ],
653
+ RFQTx: [
654
+ { name: 'rfqOffer', type: 'RFQOffer' },
655
+ { name: 'recipient', type: 'address' },
656
+ { name: 'takerRequestAmount', type: 'uint256' },
657
+ ],
321
658
  };
322
659
  return this.walletClient.signTypedData({
323
- ...EIP712_TYPED,
324
- message: swapData,
660
+ primaryType: 'RFQTx',
661
+ domain: EIP712_DOMAIN,
662
+ types: types,
663
+ message: rfqTx,
325
664
  account,
326
665
  });
327
666
  }
328
667
  });
668
+ Object.defineProperty(this, "signTypedData", {
669
+ enumerable: true,
670
+ configurable: true,
671
+ writable: true,
672
+ value: async (params) => {
673
+ let { account, order, takerTokenPermit, prepare } = params;
674
+ if (order.orderType === type_js_1.OrderType.SOR) {
675
+ if (!prepare) {
676
+ throw new Error('prepare is empty');
677
+ }
678
+ const { swapData } = prepare;
679
+ takerTokenPermit =
680
+ takerTokenPermit ?? this.getTokenPermitData(swapData.takerToken);
681
+ return this.sign712SOROrder(swapData, account);
682
+ }
683
+ else if (order.orderType === type_js_1.OrderType.RFQV6) {
684
+ const rfqOrder = order;
685
+ const { encodeResult } = rfqOrder;
686
+ const { rfqTx } = encodeResult;
687
+ const rfqOffer = rfqTx.rfqOffer;
688
+ takerTokenPermit =
689
+ takerTokenPermit ?? this.getTokenPermitData(rfqOffer.takerToken);
690
+ return this.sign712RFQOrder(rfqTx, account);
691
+ }
692
+ else {
693
+ throw new Error('unsupported order type');
694
+ }
695
+ }
696
+ });
329
697
  Object.defineProperty(this, "submit", {
330
698
  enumerable: true,
331
699
  configurable: true,
332
700
  writable: true,
333
- value: async (dto) => {
334
- return (0, index_js_2.http)(`${this.host(serviceHost_js_1.httpServiceHost)}/v6/swap/submit/${this.chainId}`, {
701
+ value: async (params) => {
702
+ let { order, slippage, takerTokenPermit, gasFee, sendByRelayer, orderSignature, } = params;
703
+ const request = {
704
+ uniqueId: order.uniqueId,
705
+ slippage: slippage,
706
+ permit: takerTokenPermit,
707
+ fee: {
708
+ protocolFee: 0n,
709
+ gasFee: 0n,
710
+ },
711
+ };
712
+ if (orderSignature) {
713
+ request.orderSignature = orderSignature;
714
+ }
715
+ if (order.orderType === type_js_1.OrderType.SOR) {
716
+ const prepare = await this.prepareOrder({
717
+ order,
718
+ slippage,
719
+ gasFee: gasFee,
720
+ sendByRelayer,
721
+ });
722
+ const { swapData, protocolFee, gasFeeTokenAmount } = prepare;
723
+ request.strategyData = swapData.strategyData;
724
+ request.fee = {
725
+ protocolFee: protocolFee,
726
+ gasFee: gasFeeTokenAmount,
727
+ };
728
+ request.strategyData = swapData.strategyData;
729
+ request.minAmountOut = swapData.minMakerTokenAmount;
730
+ }
731
+ else if (order.orderType === type_js_1.OrderType.RFQV6) {
732
+ order = order;
733
+ const rfqOrderParams = this.formatRFQOrderParams(order);
734
+ const { fee, minMakerTokenAmount } = rfqOrderParams;
735
+ request.slippage = 0;
736
+ request.minAmountOut = minMakerTokenAmount;
737
+ request.fee = {
738
+ gasFee: 0n,
739
+ protocolFee: fee,
740
+ };
741
+ }
742
+ else {
743
+ throw new Error('unsupported order type');
744
+ }
745
+ return (0, index_js_2.http)(`${this.host(serviceHost_js_1.httpServiceHost)}/${apiVersion_js_1.apiVersion}/swap/submit/${this.chainId}`, {
335
746
  fetchOptions: {
336
747
  method: 'POST',
337
748
  },
338
- body: dto,
749
+ body: request,
339
750
  });
340
751
  }
341
752
  });
@@ -358,7 +769,7 @@ class Swap extends contextHost_js_1.ContextHost {
358
769
  const params = new URLSearchParams({
359
770
  ...dto,
360
771
  });
361
- return (0, index_js_2.http)(`${this.host(serviceHost_js_1.httpServiceHost)}/v6/swap/status/${this.chainId}?${params}`);
772
+ return (0, index_js_2.http)(`${this.host(serviceHost_js_1.httpServiceHost)}/${apiVersion_js_1.apiVersion}/swap/status/${this.chainId}?${params}`);
362
773
  }
363
774
  });
364
775
  Object.defineProperty(this, "config", {
@@ -366,7 +777,7 @@ class Swap extends contextHost_js_1.ContextHost {
366
777
  configurable: true,
367
778
  writable: true,
368
779
  value: async (appVersion) => {
369
- return (0, index_js_2.http)(`${this.host(serviceHost_js_1.httpServiceHost)}/v6/swap/config?appVersion=${appVersion}`);
780
+ return (0, index_js_2.http)(`${this.host(serviceHost_js_1.httpServiceHost)}/${apiVersion_js_1.apiVersion}/swap/config?appVersion=${appVersion}`);
370
781
  }
371
782
  });
372
783
  this.walletClient = injected.walletClient;