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