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