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