@tokenlon/v6-sdk 1.0.6-canary.db97cd2 → 1.0.6-canary.df3ee2c
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/constants/abis.js +40 -526
- package/dist/cjs/constants/abis.js.map +1 -1
- package/dist/cjs/constants/addresses.js +5 -58
- package/dist/cjs/constants/addresses.js.map +1 -1
- package/dist/cjs/constants/chains.js +2 -22
- package/dist/cjs/constants/chains.js.map +1 -1
- package/dist/cjs/constants/index.js +0 -1
- package/dist/cjs/constants/index.js.map +1 -1
- package/dist/cjs/constants/unit.js +1 -2
- package/dist/cjs/constants/unit.js.map +1 -1
- package/dist/cjs/price/index.js +2 -2
- package/dist/cjs/price/index.js.map +1 -1
- package/dist/cjs/sdk.js +5 -7
- package/dist/cjs/sdk.js.map +1 -1
- package/dist/cjs/service/contextHost.js +0 -19
- package/dist/cjs/service/contextHost.js.map +1 -1
- package/dist/cjs/serviceHost.js +1 -65
- package/dist/cjs/serviceHost.js.map +1 -1
- package/dist/cjs/swap/index.js +113 -725
- package/dist/cjs/swap/index.js.map +1 -1
- package/dist/cjs/swap/type.js +1 -6
- package/dist/cjs/swap/type.js.map +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/esm/constants/abis.js +39 -525
- package/dist/esm/constants/abis.js.map +1 -1
- package/dist/esm/constants/addresses.js +4 -57
- package/dist/esm/constants/addresses.js.map +1 -1
- package/dist/esm/constants/chains.js +3 -23
- package/dist/esm/constants/chains.js.map +1 -1
- package/dist/esm/constants/index.js +0 -1
- package/dist/esm/constants/index.js.map +1 -1
- package/dist/esm/constants/unit.js +0 -1
- package/dist/esm/constants/unit.js.map +1 -1
- package/dist/esm/price/index.js +3 -3
- package/dist/esm/price/index.js.map +1 -1
- package/dist/esm/sdk.js +5 -7
- package/dist/esm/sdk.js.map +1 -1
- package/dist/esm/service/contextHost.js +0 -19
- package/dist/esm/service/contextHost.js.map +1 -1
- package/dist/esm/serviceHost.js +1 -65
- package/dist/esm/serviceHost.js.map +1 -1
- package/dist/esm/swap/index.js +115 -726
- package/dist/esm/swap/index.js.map +1 -1
- package/dist/esm/swap/type.js +1 -5
- package/dist/esm/swap/type.js.map +1 -1
- package/dist/esm/version.js +1 -1
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/dist/types/constants/abis.d.ts +14 -381
- package/dist/types/constants/abis.d.ts.map +1 -1
- package/dist/types/constants/addresses.d.ts +0 -2
- 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/index.d.ts +0 -1
- package/dist/types/constants/index.d.ts.map +1 -1
- package/dist/types/constants/unit.d.ts +0 -1
- package/dist/types/constants/unit.d.ts.map +1 -1
- package/dist/types/errors/base.d.ts +1 -1
- package/dist/types/sdk.d.ts +0 -1
- package/dist/types/sdk.d.ts.map +1 -1
- package/dist/types/service/contextHost.d.ts +1 -3
- package/dist/types/service/contextHost.d.ts.map +1 -1
- package/dist/types/serviceHost.d.ts +0 -3
- package/dist/types/serviceHost.d.ts.map +1 -1
- package/dist/types/swap/index.d.ts +28 -123
- package/dist/types/swap/index.d.ts.map +1 -1
- package/dist/types/swap/type.d.ts +23 -106
- package/dist/types/swap/type.d.ts.map +1 -1
- package/dist/types/version.d.ts +1 -1
- package/package.json +3 -3
- package/dist/cjs/constants/RFQAbi.js +0 -723
- package/dist/cjs/constants/RFQAbi.js.map +0 -1
- package/dist/cjs/constants/common.js +0 -5
- package/dist/cjs/constants/common.js.map +0 -1
- package/dist/cjs/types/token.js +0 -3
- package/dist/cjs/types/token.js.map +0 -1
- package/dist/esm/constants/RFQAbi.js +0 -721
- package/dist/esm/constants/RFQAbi.js.map +0 -1
- package/dist/esm/constants/common.js +0 -2
- package/dist/esm/constants/common.js.map +0 -1
- package/dist/esm/types/token.js +0 -2
- package/dist/esm/types/token.js.map +0 -1
- package/dist/types/constants/RFQAbi.d.ts +0 -82
- package/dist/types/constants/RFQAbi.d.ts.map +0 -1
- package/dist/types/constants/common.d.ts +0 -2
- package/dist/types/constants/common.d.ts.map +0 -1
- package/dist/types/types/token.d.ts +0 -11
- package/dist/types/types/token.d.ts.map +0 -1
package/dist/cjs/swap/index.js
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Swap = exports.
|
|
3
|
+
exports.Swap = exports.ERC20_TRANSFER_GAS = exports.strategyOperationsABI = void 0;
|
|
4
4
|
const index_js_1 = require("../constants/index.js");
|
|
5
5
|
const contextHost_js_1 = require("../service/contextHost.js");
|
|
6
6
|
const permit2_js_1 = require("../types/permit2.js");
|
|
7
7
|
const index_js_2 = require("../utils/request/index.js");
|
|
8
8
|
const serviceHost_js_1 = require("./../serviceHost.js");
|
|
9
|
-
const type_js_1 = require("./type.js");
|
|
10
9
|
const viem_1 = require("viem");
|
|
11
|
-
exports.strategyOperationsABI = (0, viem_1.parseAbiParameters)('(address dest, address inputToken,
|
|
12
|
-
exports.
|
|
10
|
+
exports.strategyOperationsABI = (0, viem_1.parseAbiParameters)('(address dest, address inputToken, uint128 inputRatio, uint128 dataOffset, uint256 value, bytes data)[]');
|
|
11
|
+
exports.ERC20_TRANSFER_GAS = 65000n;
|
|
13
12
|
class Swap extends contextHost_js_1.ContextHost {
|
|
14
13
|
constructor(config, injected) {
|
|
15
14
|
super({ env: config.env, chainId: config.chain }, {
|
|
@@ -38,7 +37,6 @@ class Swap extends contextHost_js_1.ContextHost {
|
|
|
38
37
|
configurable: true,
|
|
39
38
|
writable: true,
|
|
40
39
|
value: (fee, swapData, operations) => {
|
|
41
|
-
const isMainnetOrSepoliaOrder = this.isMainnetOrSepolia();
|
|
42
40
|
const newOperations = [...operations];
|
|
43
41
|
if (fee > 0n) {
|
|
44
42
|
const feeTransfer = (0, viem_1.encodeFunctionData)({
|
|
@@ -46,75 +44,34 @@ class Swap extends contextHost_js_1.ContextHost {
|
|
|
46
44
|
functionName: 'transfer',
|
|
47
45
|
args: [index_js_1.feeReceiptAddress[this.chainId], fee],
|
|
48
46
|
});
|
|
49
|
-
if (!index_js_1.
|
|
50
|
-
throw new Error('
|
|
51
|
-
const feeToken = this.
|
|
52
|
-
? index_js_1.
|
|
47
|
+
if (!index_js_1.wethAddress[this.chainId])
|
|
48
|
+
throw new Error('weth address not found');
|
|
49
|
+
const feeToken = this.isETH(swapData.makerToken)
|
|
50
|
+
? index_js_1.wethAddress[this.chainId]
|
|
53
51
|
: swapData.makerToken;
|
|
54
|
-
const feeOperation =
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
data: feeTransfer,
|
|
63
|
-
}
|
|
64
|
-
: {
|
|
65
|
-
dest: feeToken,
|
|
66
|
-
inputToken: feeToken,
|
|
67
|
-
inputRatio: 0n,
|
|
68
|
-
dataOffset: 0n,
|
|
69
|
-
value: 0n,
|
|
70
|
-
data: feeTransfer,
|
|
71
|
-
};
|
|
52
|
+
const feeOperation = {
|
|
53
|
+
dest: feeToken,
|
|
54
|
+
inputToken: feeToken,
|
|
55
|
+
inputRatio: 0n,
|
|
56
|
+
dataOffset: 0n,
|
|
57
|
+
value: 0n,
|
|
58
|
+
data: feeTransfer,
|
|
59
|
+
};
|
|
72
60
|
newOperations.push(feeOperation);
|
|
73
61
|
}
|
|
74
|
-
const
|
|
75
|
-
? exports.strategyOperationsABI
|
|
76
|
-
: exports.l2StrategyOperationsABI;
|
|
77
|
-
const newStrategyData = (0, viem_1.encodeAbiParameters)(targetStrategyOperationsABI, [
|
|
62
|
+
const newStrategyData = (0, viem_1.encodeAbiParameters)(exports.strategyOperationsABI, [
|
|
78
63
|
newOperations,
|
|
79
64
|
]);
|
|
80
65
|
return newStrategyData;
|
|
81
66
|
}
|
|
82
67
|
});
|
|
83
|
-
Object.defineProperty(this, "formatRFQOrderParams", {
|
|
84
|
-
enumerable: true,
|
|
85
|
-
configurable: true,
|
|
86
|
-
writable: true,
|
|
87
|
-
value: (order) => {
|
|
88
|
-
const encodeResult = order.encodeResult;
|
|
89
|
-
const rfqTx = encodeResult.rfqTx;
|
|
90
|
-
const rfqOffer = rfqTx.rfqOffer ?? {};
|
|
91
|
-
const { fromAddress } = order;
|
|
92
|
-
const { makerTokenAmount } = rfqOffer;
|
|
93
|
-
const value = BigInt(this.isNativeToken(fromAddress) ? rfqOffer.takerTokenAmount : '0x0');
|
|
94
|
-
const fee = (BigInt(makerTokenAmount) * BigInt(rfqOffer.feeFactor)) / index_js_1.bpBase;
|
|
95
|
-
const minMakerTokenAmount = BigInt(makerTokenAmount) - fee;
|
|
96
|
-
return {
|
|
97
|
-
rfqOffer,
|
|
98
|
-
rfqTx,
|
|
99
|
-
value,
|
|
100
|
-
fee,
|
|
101
|
-
minMakerTokenAmount: minMakerTokenAmount,
|
|
102
|
-
makerTokenAmount: BigInt(rfqOffer.makerTokenAmount),
|
|
103
|
-
takerTokenAmount: BigInt(rfqOffer.takerTokenAmount),
|
|
104
|
-
takerRequestAmount: rfqTx.takerRequestAmount,
|
|
105
|
-
};
|
|
106
|
-
}
|
|
107
|
-
});
|
|
108
68
|
Object.defineProperty(this, "getProtocolFeeAmount", {
|
|
109
69
|
enumerable: true,
|
|
110
70
|
configurable: true,
|
|
111
71
|
writable: true,
|
|
112
72
|
value: (order) => {
|
|
113
|
-
const {
|
|
114
|
-
const
|
|
115
|
-
const makerTokenAmount = isSOROrder
|
|
116
|
-
? order?.encodeResult?.swapData?.makerTokenAmount
|
|
117
|
-
: order?.encodeResult?.rfqTx?.rfqOffer?.makerTokenAmount;
|
|
73
|
+
const { swapData, feeFactor } = order;
|
|
74
|
+
const { makerTokenAmount } = swapData;
|
|
118
75
|
const protocolFeeAmount = (BigInt(makerTokenAmount) * BigInt(feeFactor)) / index_js_1.bpBase;
|
|
119
76
|
return protocolFeeAmount;
|
|
120
77
|
}
|
|
@@ -124,10 +81,8 @@ class Swap extends contextHost_js_1.ContextHost {
|
|
|
124
81
|
configurable: true,
|
|
125
82
|
writable: true,
|
|
126
83
|
value: (order, slippage) => {
|
|
127
|
-
const
|
|
128
|
-
const makerTokenAmount =
|
|
129
|
-
? order?.encodeResult?.swapData?.makerTokenAmount
|
|
130
|
-
: order?.encodeResult?.rfqTx?.rfqOffer?.makerTokenAmount;
|
|
84
|
+
const { swapData } = order;
|
|
85
|
+
const { makerTokenAmount } = swapData;
|
|
131
86
|
const slippageAmount = (BigInt(makerTokenAmount) * BigInt(slippage)) / index_js_1.bpBase;
|
|
132
87
|
return slippageAmount;
|
|
133
88
|
}
|
|
@@ -149,63 +104,8 @@ class Swap extends contextHost_js_1.ContextHost {
|
|
|
149
104
|
enumerable: true,
|
|
150
105
|
configurable: true,
|
|
151
106
|
writable: true,
|
|
152
|
-
value: async ({ protocolFeeAmount,
|
|
153
|
-
return
|
|
154
|
-
}
|
|
155
|
-
});
|
|
156
|
-
Object.defineProperty(this, "getL2RoutesApproveStrategy", {
|
|
157
|
-
enumerable: true,
|
|
158
|
-
configurable: true,
|
|
159
|
-
writable: true,
|
|
160
|
-
value: async (strategyAddress, takerToken, takerTokenAmount, routes = []) => {
|
|
161
|
-
const isNativeToken = (0, viem_1.isAddressEqual)(takerToken, viem_1.zeroAddress) ||
|
|
162
|
-
(0, viem_1.isAddressEqual)(takerToken, index_js_1.nullAddress);
|
|
163
|
-
const approvedToken = isNativeToken
|
|
164
|
-
? index_js_1.wrapperTokenAddress[this.chainId]
|
|
165
|
-
: takerToken;
|
|
166
|
-
if (!strategyAddress) {
|
|
167
|
-
throw new Error(`cannot find strategyAddress on current chain ${this.chainId}`);
|
|
168
|
-
}
|
|
169
|
-
const namedSpenders = routes.flat(2).reduce((acc, route) => {
|
|
170
|
-
route.spender && acc.add(route.spender);
|
|
171
|
-
return acc;
|
|
172
|
-
}, new Set());
|
|
173
|
-
const spenders = [...namedSpenders];
|
|
174
|
-
const allowances = await Promise.all([
|
|
175
|
-
...spenders.map((spender) => this.publicClient.readContract({
|
|
176
|
-
abi: [
|
|
177
|
-
(0, viem_1.parseAbiItem)('function allowance(address owner, address spender) view returns (uint256)'),
|
|
178
|
-
],
|
|
179
|
-
functionName: 'allowance',
|
|
180
|
-
address: approvedToken,
|
|
181
|
-
args: [strategyAddress, spender],
|
|
182
|
-
})),
|
|
183
|
-
]);
|
|
184
|
-
return allowances.reduce((acc, allowance, index) => {
|
|
185
|
-
try {
|
|
186
|
-
if (BigInt(allowance) < BigInt(takerTokenAmount)) {
|
|
187
|
-
const data = (0, viem_1.encodeFunctionData)({
|
|
188
|
-
abi: [
|
|
189
|
-
(0, viem_1.parseAbiItem)('function approve(address spender, uint256 amount) returns (bool)'),
|
|
190
|
-
],
|
|
191
|
-
functionName: 'approve',
|
|
192
|
-
args: [spenders[index], index_js_1.MAX_UINT256],
|
|
193
|
-
});
|
|
194
|
-
acc.push({
|
|
195
|
-
dest: approvedToken,
|
|
196
|
-
inputToken: approvedToken,
|
|
197
|
-
inputRatio: 0n,
|
|
198
|
-
dataOffset: 0n,
|
|
199
|
-
value: 0n,
|
|
200
|
-
data,
|
|
201
|
-
});
|
|
202
|
-
}
|
|
203
|
-
return acc;
|
|
204
|
-
}
|
|
205
|
-
catch {
|
|
206
|
-
return acc;
|
|
207
|
-
}
|
|
208
|
-
}, []);
|
|
107
|
+
value: async ({ protocolFeeAmount, feeTokenAmount, }) => {
|
|
108
|
+
return feeTokenAmount + protocolFeeAmount;
|
|
209
109
|
}
|
|
210
110
|
});
|
|
211
111
|
Object.defineProperty(this, "getRoutesApproveStrategy", {
|
|
@@ -213,10 +113,8 @@ class Swap extends contextHost_js_1.ContextHost {
|
|
|
213
113
|
configurable: true,
|
|
214
114
|
writable: true,
|
|
215
115
|
value: async (takerTokenAmount, needApprovalDestinations) => {
|
|
216
|
-
if (
|
|
217
|
-
needApprovalDestinations.length === 0) {
|
|
116
|
+
if (needApprovalDestinations.length === 0)
|
|
218
117
|
return [];
|
|
219
|
-
}
|
|
220
118
|
const allowances = await Promise.all([
|
|
221
119
|
...needApprovalDestinations.map(({ dest, inputToken }) => this.publicClient.readContract({
|
|
222
120
|
abi: [
|
|
@@ -241,8 +139,7 @@ class Swap extends contextHost_js_1.ContextHost {
|
|
|
241
139
|
const approveOperation = {
|
|
242
140
|
dest: inputToken,
|
|
243
141
|
inputToken: inputToken,
|
|
244
|
-
|
|
245
|
-
ratioDenominator: 0n,
|
|
142
|
+
inputRatio: 0n,
|
|
246
143
|
dataOffset: 0n,
|
|
247
144
|
value: 0n,
|
|
248
145
|
data: approve,
|
|
@@ -261,8 +158,7 @@ class Swap extends contextHost_js_1.ContextHost {
|
|
|
261
158
|
const approveOperation = {
|
|
262
159
|
dest: inputToken,
|
|
263
160
|
inputToken: inputToken,
|
|
264
|
-
|
|
265
|
-
ratioDenominator: 0n,
|
|
161
|
+
inputRatio: 0n,
|
|
266
162
|
dataOffset: 0n,
|
|
267
163
|
value: 0n,
|
|
268
164
|
data: approve2Zero,
|
|
@@ -280,260 +176,63 @@ class Swap extends contextHost_js_1.ContextHost {
|
|
|
280
176
|
return approveStrategies;
|
|
281
177
|
}
|
|
282
178
|
});
|
|
283
|
-
Object.defineProperty(this, "preview", {
|
|
284
|
-
enumerable: true,
|
|
285
|
-
configurable: true,
|
|
286
|
-
writable: true,
|
|
287
|
-
value: async (params) => {
|
|
288
|
-
let preview;
|
|
289
|
-
let { order, gasFee = 0n, sendByRelayer } = params;
|
|
290
|
-
if (order.orderType === type_js_1.OrderType.SOR) {
|
|
291
|
-
order = order;
|
|
292
|
-
const sorPrepare = await this.prepareOrder(params);
|
|
293
|
-
const { swapData, fee, protocolFee, gasFeeTokenAmount } = sorPrepare;
|
|
294
|
-
preview = {
|
|
295
|
-
inputToken: swapData.takerToken,
|
|
296
|
-
outputToken: swapData.makerToken,
|
|
297
|
-
amountIn: swapData.takerTokenAmount,
|
|
298
|
-
amountOut: swapData.makerTokenAmount,
|
|
299
|
-
minAmountOut: swapData.minMakerTokenAmount,
|
|
300
|
-
recipient: swapData.recipient,
|
|
301
|
-
fee,
|
|
302
|
-
protocolFee,
|
|
303
|
-
gasFeeTokenAmount,
|
|
304
|
-
feeFactor: order.protocolFeeFactor,
|
|
305
|
-
};
|
|
306
|
-
}
|
|
307
|
-
else if (order.orderType === type_js_1.OrderType.RFQV6) {
|
|
308
|
-
order = order;
|
|
309
|
-
const { rfqTx } = order.encodeResult;
|
|
310
|
-
const { rfqOffer } = rfqTx;
|
|
311
|
-
const protocolFeeAmount = this.getProtocolFeeAmount(order);
|
|
312
|
-
const { fee, minMakerTokenAmount } = this.formatRFQOrderParams(order);
|
|
313
|
-
let gasFeeTokenAmount = gasFee;
|
|
314
|
-
if (sendByRelayer) {
|
|
315
|
-
gasFeeTokenAmount = await this.calcGasFeeTokenAmount({
|
|
316
|
-
gasFeeAmount: gasFee,
|
|
317
|
-
feeToken: rfqOffer.makerToken,
|
|
318
|
-
});
|
|
319
|
-
}
|
|
320
|
-
preview = {
|
|
321
|
-
inputToken: rfqOffer.takerToken,
|
|
322
|
-
outputToken: rfqOffer.makerToken,
|
|
323
|
-
amountIn: BigInt(rfqOffer.takerTokenAmount),
|
|
324
|
-
amountOut: BigInt(rfqOffer.makerTokenAmount),
|
|
325
|
-
minAmountOut: minMakerTokenAmount,
|
|
326
|
-
recipient: rfqTx.recipient,
|
|
327
|
-
fee,
|
|
328
|
-
protocolFee: protocolFeeAmount,
|
|
329
|
-
gasFeeTokenAmount,
|
|
330
|
-
feeFactor: order.protocolFeeFactor,
|
|
331
|
-
};
|
|
332
|
-
}
|
|
333
|
-
else {
|
|
334
|
-
throw new Error('unsupported order type');
|
|
335
|
-
}
|
|
336
|
-
return preview;
|
|
337
|
-
}
|
|
338
|
-
});
|
|
339
|
-
Object.defineProperty(this, "previewNewOrder", {
|
|
340
|
-
enumerable: true,
|
|
341
|
-
configurable: true,
|
|
342
|
-
writable: true,
|
|
343
|
-
value: async (params) => {
|
|
344
|
-
let preview;
|
|
345
|
-
let { order, slippage, sendByRelayer, gasFee = 0n } = params;
|
|
346
|
-
if (order.orderType === type_js_1.OrderType.SOR) {
|
|
347
|
-
order = order;
|
|
348
|
-
const encodeResult = order.encodeResult ?? {};
|
|
349
|
-
const { swapData } = encodeResult;
|
|
350
|
-
const { 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
|
-
preview = {
|
|
369
|
-
inputToken: swapData.takerToken,
|
|
370
|
-
outputToken: swapData.makerToken,
|
|
371
|
-
amountIn: BigInt(swapData.takerTokenAmount),
|
|
372
|
-
amountOut: BigInt(swapData.makerTokenAmount),
|
|
373
|
-
minAmountOut: BigInt(swapData.minMakerTokenAmount),
|
|
374
|
-
recipient: swapData.recipient,
|
|
375
|
-
fee,
|
|
376
|
-
protocolFee: protocolFeeAmount,
|
|
377
|
-
gasFeeTokenAmount,
|
|
378
|
-
feeFactor: order.protocolFeeFactor,
|
|
379
|
-
};
|
|
380
|
-
}
|
|
381
|
-
else if (order.orderType === type_js_1.OrderType.RFQV6) {
|
|
382
|
-
order = order;
|
|
383
|
-
const { rfqTx } = order.encodeResult;
|
|
384
|
-
const { rfqOffer } = rfqTx;
|
|
385
|
-
const protocolFeeAmount = this.getProtocolFeeAmount(order);
|
|
386
|
-
const { fee, minMakerTokenAmount } = this.formatRFQOrderParams(order);
|
|
387
|
-
let gasFeeTokenAmount = gasFee;
|
|
388
|
-
if (sendByRelayer) {
|
|
389
|
-
gasFeeTokenAmount = await this.calcGasFeeTokenAmount({
|
|
390
|
-
gasFeeAmount: gasFee,
|
|
391
|
-
feeToken: rfqOffer.makerToken,
|
|
392
|
-
});
|
|
393
|
-
}
|
|
394
|
-
preview = {
|
|
395
|
-
inputToken: rfqOffer.takerToken,
|
|
396
|
-
outputToken: rfqOffer.makerToken,
|
|
397
|
-
amountIn: BigInt(rfqOffer.takerTokenAmount),
|
|
398
|
-
amountOut: BigInt(rfqOffer.makerTokenAmount),
|
|
399
|
-
minAmountOut: minMakerTokenAmount,
|
|
400
|
-
recipient: rfqTx.recipient,
|
|
401
|
-
fee,
|
|
402
|
-
protocolFee: protocolFeeAmount,
|
|
403
|
-
gasFeeTokenAmount,
|
|
404
|
-
feeFactor: order.protocolFeeFactor,
|
|
405
|
-
};
|
|
406
|
-
}
|
|
407
|
-
else {
|
|
408
|
-
throw new Error('unsupported order type');
|
|
409
|
-
}
|
|
410
|
-
return preview;
|
|
411
|
-
}
|
|
412
|
-
});
|
|
413
179
|
Object.defineProperty(this, "prepareOrder", {
|
|
414
180
|
enumerable: true,
|
|
415
181
|
configurable: true,
|
|
416
182
|
writable: true,
|
|
417
183
|
value: async ({ order, slippage, gasFee = 0n, sendByRelayer, }) => {
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
if (isMainnetOrSepoliaOrder) {
|
|
435
|
-
if (sendByRelayer) {
|
|
436
|
-
gasFeeTokenAmount = await this.calcGasFeeTokenAmount({
|
|
437
|
-
gasFeeAmount: gasFee,
|
|
438
|
-
feeToken: makerToken,
|
|
439
|
-
});
|
|
440
|
-
fee = await this.calcRelayerFee({
|
|
441
|
-
protocolFeeAmount,
|
|
442
|
-
gasFeeTokenAmount,
|
|
443
|
-
});
|
|
444
|
-
}
|
|
445
|
-
const minMakerTokenAmount = makerTokenAmount - slippageAmount - fee;
|
|
446
|
-
swapData.minMakerTokenAmount = minMakerTokenAmount.toString();
|
|
447
|
-
const [operations] = (0, viem_1.decodeAbiParameters)(exports.strategyOperationsABI, strategyData);
|
|
448
|
-
const approveOperations = await this.getRoutesApproveStrategy(BigInt(swapData.takerTokenAmount), needApprovalDestinations);
|
|
449
|
-
const newStrategyData = this.rebuildStrategyData(fee, swapData, [
|
|
450
|
-
...approveOperations,
|
|
451
|
-
...operations,
|
|
452
|
-
]);
|
|
453
|
-
const newSwapData = {
|
|
454
|
-
...swapData,
|
|
455
|
-
strategyData: newStrategyData,
|
|
456
|
-
takerTokenAmount: BigInt(swapData.takerTokenAmount),
|
|
457
|
-
makerTokenAmount: BigInt(swapData.makerTokenAmount),
|
|
458
|
-
minMakerTokenAmount: BigInt(swapData.minMakerTokenAmount),
|
|
459
|
-
salt: BigInt(swapData.salt),
|
|
460
|
-
expiry: BigInt(swapData.expiry),
|
|
461
|
-
};
|
|
462
|
-
prepare = {
|
|
463
|
-
swapData: newSwapData,
|
|
464
|
-
protocolFee: protocolFeeAmount,
|
|
465
|
-
gasFeeTokenAmount,
|
|
466
|
-
gasFee,
|
|
467
|
-
fee,
|
|
468
|
-
recipient,
|
|
469
|
-
strategyData: newStrategyData,
|
|
470
|
-
value,
|
|
471
|
-
minMakerTokenAmount,
|
|
472
|
-
makerTokenAmount: newSwapData.makerTokenAmount,
|
|
473
|
-
takerTokenAmount: newSwapData.takerTokenAmount,
|
|
474
|
-
};
|
|
475
|
-
}
|
|
476
|
-
else {
|
|
477
|
-
const { address: strategyAddress } = order.strategies[0];
|
|
478
|
-
const minMakerTokenAmount = makerTokenAmount - slippageAmount - fee;
|
|
479
|
-
swapData.minMakerTokenAmount = minMakerTokenAmount.toString();
|
|
480
|
-
const [operations] = (0, viem_1.decodeAbiParameters)(exports.l2StrategyOperationsABI, strategyData);
|
|
481
|
-
const approveOperations = await this.getL2RoutesApproveStrategy(strategyAddress, takerToken, BigInt(swapData.takerTokenAmount), order.routes);
|
|
482
|
-
const newStrategyData = this.rebuildStrategyData(fee, swapData, [
|
|
483
|
-
...approveOperations,
|
|
484
|
-
...operations,
|
|
485
|
-
]);
|
|
486
|
-
const newSwapData = {
|
|
487
|
-
...swapData,
|
|
488
|
-
strategyData: newStrategyData,
|
|
489
|
-
takerTokenAmount: BigInt(swapData.takerTokenAmount),
|
|
490
|
-
makerTokenAmount: BigInt(swapData.makerTokenAmount),
|
|
491
|
-
minMakerTokenAmount: BigInt(swapData.minMakerTokenAmount),
|
|
492
|
-
salt: BigInt(swapData.salt),
|
|
493
|
-
expiry: BigInt(swapData.expiry),
|
|
494
|
-
};
|
|
495
|
-
prepare = {
|
|
496
|
-
swapData: newSwapData,
|
|
497
|
-
protocolFee: protocolFeeAmount,
|
|
498
|
-
gasFeeTokenAmount,
|
|
499
|
-
gasFee,
|
|
500
|
-
fee,
|
|
501
|
-
recipient,
|
|
502
|
-
strategyData: newStrategyData,
|
|
503
|
-
value,
|
|
504
|
-
minMakerTokenAmount,
|
|
505
|
-
makerTokenAmount: newSwapData.makerTokenAmount,
|
|
506
|
-
takerTokenAmount: newSwapData.takerTokenAmount,
|
|
507
|
-
};
|
|
508
|
-
}
|
|
509
|
-
}
|
|
510
|
-
else if (order.orderType === type_js_1.OrderType.RFQV6) {
|
|
511
|
-
order = order;
|
|
512
|
-
const protocolFeeAmount = this.getProtocolFeeAmount(order);
|
|
513
|
-
const rfqOrderParams = this.formatRFQOrderParams(order);
|
|
514
|
-
prepare = {
|
|
515
|
-
...rfqOrderParams,
|
|
516
|
-
protocolFee: protocolFeeAmount,
|
|
517
|
-
};
|
|
518
|
-
}
|
|
519
|
-
else {
|
|
520
|
-
throw new Error('unsupported order type');
|
|
184
|
+
const { swapData, needApprovalDestinations } = order;
|
|
185
|
+
const { recipient, strategyData, makerToken } = swapData;
|
|
186
|
+
const makerTokenAmount = BigInt(swapData.makerTokenAmount);
|
|
187
|
+
const slippageAmount = this.getSlippageAmount(order, slippage);
|
|
188
|
+
const protocolFeeAmount = this.getProtocolFeeAmount(order);
|
|
189
|
+
let fee = protocolFeeAmount;
|
|
190
|
+
let feeTokenAmount = gasFee;
|
|
191
|
+
if (sendByRelayer) {
|
|
192
|
+
feeTokenAmount = await this.calcGasFeeTokenAmount({
|
|
193
|
+
gasFeeAmount: gasFee,
|
|
194
|
+
feeToken: makerToken,
|
|
195
|
+
});
|
|
196
|
+
fee = await this.calcRelayerFee({
|
|
197
|
+
protocolFeeAmount,
|
|
198
|
+
feeTokenAmount,
|
|
199
|
+
});
|
|
521
200
|
}
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
201
|
+
const minMakerTokenAmount = makerTokenAmount - slippageAmount - fee;
|
|
202
|
+
swapData.minMakerTokenAmount = minMakerTokenAmount.toString();
|
|
203
|
+
const [operations] = (0, viem_1.decodeAbiParameters)(exports.strategyOperationsABI, strategyData);
|
|
204
|
+
const approveOperations = await this.getRoutesApproveStrategy(swapData.takerTokenAmount, needApprovalDestinations);
|
|
205
|
+
const newStrategyData = this.rebuildStrategyData(fee, swapData, [
|
|
206
|
+
...approveOperations,
|
|
207
|
+
...operations,
|
|
208
|
+
]);
|
|
209
|
+
const value = (0, viem_1.isAddressEqual)(swapData.takerToken, index_js_1.nullAddress) ||
|
|
210
|
+
(0, viem_1.isAddressEqual)(swapData.takerToken, viem_1.zeroAddress)
|
|
211
|
+
? BigInt(swapData.takerTokenAmount)
|
|
212
|
+
: 0n;
|
|
213
|
+
const newSwapData = {
|
|
214
|
+
...swapData,
|
|
215
|
+
strategyData: newStrategyData,
|
|
216
|
+
takerTokenAmount: BigInt(swapData.takerTokenAmount),
|
|
217
|
+
makerTokenAmount: BigInt(swapData.makerTokenAmount),
|
|
218
|
+
minMakerTokenAmount: BigInt(swapData.minMakerTokenAmount),
|
|
219
|
+
salt: BigInt(swapData.salt),
|
|
220
|
+
expiry: BigInt(swapData.expiry),
|
|
221
|
+
};
|
|
222
|
+
return {
|
|
223
|
+
swapData: newSwapData,
|
|
224
|
+
protocolFee: protocolFeeAmount,
|
|
225
|
+
gasFeeTokenAmount: feeTokenAmount,
|
|
226
|
+
gasFee,
|
|
227
|
+
fee,
|
|
228
|
+
recipient,
|
|
229
|
+
operations,
|
|
230
|
+
strategyData: newStrategyData,
|
|
231
|
+
value,
|
|
232
|
+
};
|
|
534
233
|
}
|
|
535
234
|
});
|
|
536
|
-
Object.defineProperty(this, "
|
|
235
|
+
Object.defineProperty(this, "quote", {
|
|
537
236
|
enumerable: true,
|
|
538
237
|
configurable: true,
|
|
539
238
|
writable: true,
|
|
@@ -546,16 +245,7 @@ class Swap extends contextHost_js_1.ContextHost {
|
|
|
546
245
|
Object.entries(dto).forEach(([key, value]) => {
|
|
547
246
|
p.append(key, value.toString());
|
|
548
247
|
});
|
|
549
|
-
const order = await (0, index_js_2.http)(`${this.
|
|
550
|
-
return order;
|
|
551
|
-
}
|
|
552
|
-
});
|
|
553
|
-
Object.defineProperty(this, "quoteLastOrder", {
|
|
554
|
-
enumerable: true,
|
|
555
|
-
configurable: true,
|
|
556
|
-
writable: true,
|
|
557
|
-
value: async (uniqueId) => {
|
|
558
|
-
const order = await (0, index_js_2.http)(`${this.specialHost(serviceHost_js_1.httpServiceHost)}/${index_js_1.apiVersion}/swap/swap/${this.chainId}?uniqueId=${uniqueId}`);
|
|
248
|
+
const order = await (0, index_js_2.http)(`${this.host(serviceHost_js_1.httpServiceHost)}/v6/swap/quote/${chainId}?${p.toString()}`);
|
|
559
249
|
return order;
|
|
560
250
|
}
|
|
561
251
|
});
|
|
@@ -566,13 +256,13 @@ class Swap extends contextHost_js_1.ContextHost {
|
|
|
566
256
|
value: (token, permitType = permit2_js_1.PermitSource.Permit2AllowanceTransfer) => {
|
|
567
257
|
const permitToAllowanceTarget = (0, viem_1.encodePacked)(['uint8', 'bytes'], [permitType, '0x']);
|
|
568
258
|
let takerTokenPermit = permitToAllowanceTarget;
|
|
569
|
-
if (this.
|
|
259
|
+
if (this.isETH(token)) {
|
|
570
260
|
takerTokenPermit = '0x';
|
|
571
261
|
}
|
|
572
262
|
return takerTokenPermit;
|
|
573
263
|
}
|
|
574
264
|
});
|
|
575
|
-
Object.defineProperty(this, "
|
|
265
|
+
Object.defineProperty(this, "isETH", {
|
|
576
266
|
enumerable: true,
|
|
577
267
|
configurable: true,
|
|
578
268
|
writable: true,
|
|
@@ -581,95 +271,16 @@ class Swap extends contextHost_js_1.ContextHost {
|
|
|
581
271
|
(0, viem_1.isAddressEqual)(tokenAddress, viem_1.zeroAddress));
|
|
582
272
|
}
|
|
583
273
|
});
|
|
584
|
-
Object.defineProperty(this, "
|
|
585
|
-
enumerable: true,
|
|
586
|
-
configurable: true,
|
|
587
|
-
writable: true,
|
|
588
|
-
value: () => {
|
|
589
|
-
const chainId = this.chainId;
|
|
590
|
-
return chainId === 1 || chainId === 11155111;
|
|
591
|
-
}
|
|
592
|
-
});
|
|
593
|
-
Object.defineProperty(this, "estimateByApi", {
|
|
594
|
-
enumerable: true,
|
|
595
|
-
configurable: true,
|
|
596
|
-
writable: true,
|
|
597
|
-
value: async (order, sendByRelayer, useAll = true) => {
|
|
598
|
-
if (!useAll) {
|
|
599
|
-
const { gas_usage } = await (0, index_js_2.http)(`${this.host(serviceHost_js_1.estimateGasServiceHost)}/estimate/v6/gastable`, {
|
|
600
|
-
fetchOptions: {
|
|
601
|
-
method: 'POST',
|
|
602
|
-
},
|
|
603
|
-
body: {
|
|
604
|
-
...order,
|
|
605
|
-
},
|
|
606
|
-
});
|
|
607
|
-
return BigInt(gas_usage || index_js_1.DEFAULT_GAS_LIMIT);
|
|
608
|
-
}
|
|
609
|
-
const { gas_table, simulation } = await (0, index_js_2.http)(`${this.host(serviceHost_js_1.estimateGasServiceHost)}/estimate/v6/all`, {
|
|
610
|
-
fetchOptions: {
|
|
611
|
-
method: 'POST',
|
|
612
|
-
},
|
|
613
|
-
body: {
|
|
614
|
-
...order,
|
|
615
|
-
},
|
|
616
|
-
});
|
|
617
|
-
const simulationResult = simulation?.gas_usage + (sendByRelayer ? 31000 : 0);
|
|
618
|
-
return BigInt(simulationResult || gas_table?.gas_usage || index_js_1.DEFAULT_GAS_LIMIT);
|
|
619
|
-
}
|
|
620
|
-
});
|
|
621
|
-
Object.defineProperty(this, "estimateRFQOrder", {
|
|
622
|
-
enumerable: true,
|
|
623
|
-
configurable: true,
|
|
624
|
-
writable: true,
|
|
625
|
-
value: async ({ account, order, takerTokenPermit, }) => {
|
|
626
|
-
const { encodeResult } = order;
|
|
627
|
-
const { rfqTx } = encodeResult;
|
|
628
|
-
const { rfqOffer } = rfqTx;
|
|
629
|
-
const { value } = this.formatRFQOrderParams(order);
|
|
630
|
-
const gasEstimate = await this.publicClient.estimateContractGas({
|
|
631
|
-
address: index_js_1.rfqAddress[this.chainId],
|
|
632
|
-
abi: index_js_1.RFQ_ABI,
|
|
633
|
-
functionName: 'fillRFQ',
|
|
634
|
-
args: [
|
|
635
|
-
[
|
|
636
|
-
[
|
|
637
|
-
rfqOffer.taker,
|
|
638
|
-
rfqOffer.maker,
|
|
639
|
-
rfqOffer.takerToken,
|
|
640
|
-
rfqOffer.takerTokenAmount,
|
|
641
|
-
rfqOffer.makerToken,
|
|
642
|
-
rfqOffer.makerTokenAmount,
|
|
643
|
-
rfqOffer.feeFactor,
|
|
644
|
-
rfqOffer.flags,
|
|
645
|
-
rfqOffer.expiry,
|
|
646
|
-
rfqOffer.salt,
|
|
647
|
-
],
|
|
648
|
-
rfqTx.recipient,
|
|
649
|
-
rfqTx.takerRequestAmount,
|
|
650
|
-
],
|
|
651
|
-
encodeResult.makerSignature,
|
|
652
|
-
encodeResult.makerTokenPermit,
|
|
653
|
-
takerTokenPermit,
|
|
654
|
-
],
|
|
655
|
-
value,
|
|
656
|
-
account,
|
|
657
|
-
});
|
|
658
|
-
return gasEstimate;
|
|
659
|
-
}
|
|
660
|
-
});
|
|
661
|
-
Object.defineProperty(this, "estimateSOROrder", {
|
|
274
|
+
Object.defineProperty(this, "estimate", {
|
|
662
275
|
enumerable: true,
|
|
663
276
|
configurable: true,
|
|
664
277
|
writable: true,
|
|
665
|
-
value: async ({ account, order, slippage,
|
|
666
|
-
const
|
|
278
|
+
value: async ({ account, order, slippage, permit, }) => {
|
|
279
|
+
const { swapData, value } = await this.prepareOrder({
|
|
667
280
|
order,
|
|
668
281
|
slippage,
|
|
669
282
|
});
|
|
670
|
-
const
|
|
671
|
-
takerTokenPermit =
|
|
672
|
-
takerTokenPermit ?? this.getTokenPermitData(swapData.takerToken);
|
|
283
|
+
const takerTokenPermit = permit ?? this.getTokenPermitData(swapData.takerToken);
|
|
673
284
|
const gasEstimate = await this.publicClient.estimateContractGas({
|
|
674
285
|
address: index_js_1.genericSwapAddress[this.chainId],
|
|
675
286
|
abi: index_js_1.GENERIC_SWAP_ABI,
|
|
@@ -681,102 +292,50 @@ class Swap extends contextHost_js_1.ContextHost {
|
|
|
681
292
|
return gasEstimate;
|
|
682
293
|
}
|
|
683
294
|
});
|
|
684
|
-
Object.defineProperty(this, "
|
|
295
|
+
Object.defineProperty(this, "estimateByApi", {
|
|
685
296
|
enumerable: true,
|
|
686
297
|
configurable: true,
|
|
687
298
|
writable: true,
|
|
688
|
-
value: async (
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
const {
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
takerTokenPermit,
|
|
699
|
-
});
|
|
700
|
-
}
|
|
701
|
-
else if (order.orderType === type_js_1.OrderType.RFQV6) {
|
|
702
|
-
order = order;
|
|
703
|
-
const rfqOffer = order?.encodeResult?.rfqTx?.rfqOffer ?? {};
|
|
704
|
-
takerTokenPermit =
|
|
705
|
-
takerTokenPermit ?? this.getTokenPermitData(rfqOffer.takerToken);
|
|
706
|
-
return await this.estimateRFQOrder({
|
|
707
|
-
account,
|
|
708
|
-
order: order,
|
|
709
|
-
takerTokenPermit,
|
|
299
|
+
value: async (order) => {
|
|
300
|
+
let gasEstimate = index_js_1.DEFAULT_GAS_LIMIT;
|
|
301
|
+
try {
|
|
302
|
+
const { gas_usage } = await (0, index_js_2.http)(`${this.host(serviceHost_js_1.httpServiceHost)}/v1/estimate/gastable`, {
|
|
303
|
+
fetchOptions: {
|
|
304
|
+
method: 'POST',
|
|
305
|
+
},
|
|
306
|
+
body: {
|
|
307
|
+
...order,
|
|
308
|
+
},
|
|
710
309
|
});
|
|
310
|
+
gasEstimate = BigInt(gas_usage);
|
|
711
311
|
}
|
|
712
|
-
|
|
713
|
-
|
|
312
|
+
catch (error) {
|
|
313
|
+
console.log('failed to estimate gas by api', error);
|
|
714
314
|
}
|
|
315
|
+
return gasEstimate;
|
|
715
316
|
}
|
|
716
317
|
});
|
|
717
318
|
Object.defineProperty(this, "estimateWithFallback", {
|
|
718
319
|
enumerable: true,
|
|
719
320
|
configurable: true,
|
|
720
321
|
writable: true,
|
|
721
|
-
value: async ({ account, order, slippage,
|
|
722
|
-
let gasEstimate;
|
|
322
|
+
value: async ({ account, order, slippage, permit, }) => {
|
|
323
|
+
let gasEstimate = index_js_1.DEFAULT_GAS_LIMIT;
|
|
723
324
|
try {
|
|
724
325
|
gasEstimate = await this.estimate({
|
|
725
326
|
account,
|
|
726
327
|
order,
|
|
727
328
|
slippage,
|
|
728
|
-
|
|
329
|
+
permit,
|
|
729
330
|
});
|
|
730
331
|
}
|
|
731
332
|
catch (_error) {
|
|
732
|
-
gasEstimate = await this.estimateByApi(order
|
|
333
|
+
gasEstimate = await this.estimateByApi(order);
|
|
733
334
|
}
|
|
734
335
|
return gasEstimate;
|
|
735
336
|
}
|
|
736
337
|
});
|
|
737
|
-
Object.defineProperty(this, "
|
|
738
|
-
enumerable: true,
|
|
739
|
-
configurable: true,
|
|
740
|
-
writable: true,
|
|
741
|
-
value: async (params) => {
|
|
742
|
-
const { account, value = 0n, txOverride, order, takerTokenPermit } = params;
|
|
743
|
-
const { encodeResult } = order;
|
|
744
|
-
const { rfqTx } = encodeResult;
|
|
745
|
-
const { rfqOffer } = rfqTx;
|
|
746
|
-
const txHash = await this.walletClient.writeContract({
|
|
747
|
-
address: index_js_1.rfqAddress[this.chainId],
|
|
748
|
-
abi: index_js_1.RFQ_ABI,
|
|
749
|
-
account,
|
|
750
|
-
functionName: 'fillRFQ',
|
|
751
|
-
args: [
|
|
752
|
-
[
|
|
753
|
-
[
|
|
754
|
-
rfqOffer.taker,
|
|
755
|
-
rfqOffer.maker,
|
|
756
|
-
rfqOffer.takerToken,
|
|
757
|
-
rfqOffer.takerTokenAmount,
|
|
758
|
-
rfqOffer.makerToken,
|
|
759
|
-
rfqOffer.makerTokenAmount,
|
|
760
|
-
rfqOffer.feeFactor,
|
|
761
|
-
rfqOffer.flags,
|
|
762
|
-
rfqOffer.expiry,
|
|
763
|
-
rfqOffer.salt,
|
|
764
|
-
],
|
|
765
|
-
rfqTx.recipient,
|
|
766
|
-
rfqTx.takerRequestAmount,
|
|
767
|
-
],
|
|
768
|
-
encodeResult.makerSignature,
|
|
769
|
-
encodeResult.makerTokenPermit,
|
|
770
|
-
takerTokenPermit,
|
|
771
|
-
],
|
|
772
|
-
value,
|
|
773
|
-
chain: this.walletClient.chain,
|
|
774
|
-
...txOverride,
|
|
775
|
-
});
|
|
776
|
-
return txHash;
|
|
777
|
-
}
|
|
778
|
-
});
|
|
779
|
-
Object.defineProperty(this, "signSOROrder", {
|
|
338
|
+
Object.defineProperty(this, "sign", {
|
|
780
339
|
enumerable: true,
|
|
781
340
|
configurable: true,
|
|
782
341
|
writable: true,
|
|
@@ -795,59 +354,6 @@ class Swap extends contextHost_js_1.ContextHost {
|
|
|
795
354
|
return txHash;
|
|
796
355
|
}
|
|
797
356
|
});
|
|
798
|
-
Object.defineProperty(this, "sign", {
|
|
799
|
-
enumerable: true,
|
|
800
|
-
configurable: true,
|
|
801
|
-
writable: true,
|
|
802
|
-
value: async (params) => {
|
|
803
|
-
let { account, order, slippage, takerTokenPermit, txOverride, sendByRelayer, } = params;
|
|
804
|
-
const gas = txOverride?.gas ??
|
|
805
|
-
(await this.estimate({
|
|
806
|
-
account,
|
|
807
|
-
order,
|
|
808
|
-
slippage,
|
|
809
|
-
takerTokenPermit,
|
|
810
|
-
}));
|
|
811
|
-
const gasPrice = txOverride?.gasPrice ?? (await this.publicClient.getGasPrice());
|
|
812
|
-
const gasFee = gas * gasPrice;
|
|
813
|
-
txOverride = txOverride;
|
|
814
|
-
if (order.orderType === type_js_1.OrderType.SOR) {
|
|
815
|
-
const prepare = await this.prepareOrder({
|
|
816
|
-
order,
|
|
817
|
-
slippage,
|
|
818
|
-
gasFee,
|
|
819
|
-
sendByRelayer,
|
|
820
|
-
});
|
|
821
|
-
const { swapData, value } = prepare;
|
|
822
|
-
takerTokenPermit =
|
|
823
|
-
takerTokenPermit ?? this.getTokenPermitData(swapData.takerToken);
|
|
824
|
-
return await this.signSOROrder({
|
|
825
|
-
account,
|
|
826
|
-
value,
|
|
827
|
-
takerTokenPermit,
|
|
828
|
-
swapData,
|
|
829
|
-
txOverride,
|
|
830
|
-
});
|
|
831
|
-
}
|
|
832
|
-
else if (order.orderType === type_js_1.OrderType.RFQV6) {
|
|
833
|
-
order = order;
|
|
834
|
-
const rfqOffer = order?.encodeResult?.rfqTx?.rfqOffer ?? {};
|
|
835
|
-
takerTokenPermit =
|
|
836
|
-
takerTokenPermit ?? this.getTokenPermitData(rfqOffer.takerToken);
|
|
837
|
-
const { value } = this.formatRFQOrderParams(order);
|
|
838
|
-
return await this.signRFQOrder({
|
|
839
|
-
account,
|
|
840
|
-
order,
|
|
841
|
-
takerTokenPermit,
|
|
842
|
-
value: value,
|
|
843
|
-
txOverride,
|
|
844
|
-
});
|
|
845
|
-
}
|
|
846
|
-
else {
|
|
847
|
-
throw new Error('unsupported order type');
|
|
848
|
-
}
|
|
849
|
-
}
|
|
850
|
-
});
|
|
851
357
|
Object.defineProperty(this, "signWithReceipt", {
|
|
852
358
|
enumerable: true,
|
|
853
359
|
configurable: true,
|
|
@@ -860,7 +366,7 @@ class Swap extends contextHost_js_1.ContextHost {
|
|
|
860
366
|
return receipt;
|
|
861
367
|
}
|
|
862
368
|
});
|
|
863
|
-
Object.defineProperty(this, "
|
|
369
|
+
Object.defineProperty(this, "signTypedData", {
|
|
864
370
|
enumerable: true,
|
|
865
371
|
configurable: true,
|
|
866
372
|
writable: true,
|
|
@@ -871,138 +377,28 @@ class Swap extends contextHost_js_1.ContextHost {
|
|
|
871
377
|
chainId: this.chainId,
|
|
872
378
|
verifyingContract: index_js_1.genericSwapAddress[this.chainId],
|
|
873
379
|
};
|
|
874
|
-
|
|
875
|
-
primaryType: 'GenericSwapData',
|
|
380
|
+
const EIP712_TYPED = {
|
|
876
381
|
domain: EIP712_DOMAIN,
|
|
877
382
|
types: index_js_1.GS_EIP712_TYPES,
|
|
878
|
-
|
|
879
|
-
account,
|
|
880
|
-
});
|
|
881
|
-
}
|
|
882
|
-
});
|
|
883
|
-
Object.defineProperty(this, "sign712RFQOrder", {
|
|
884
|
-
enumerable: true,
|
|
885
|
-
configurable: true,
|
|
886
|
-
writable: true,
|
|
887
|
-
value: async (rfqTx, account) => {
|
|
888
|
-
const EIP712_DOMAIN = {
|
|
889
|
-
name: 'Tokenlon',
|
|
890
|
-
version: 'v6',
|
|
891
|
-
chainId: this.chainId,
|
|
892
|
-
verifyingContract: index_js_1.rfqAddress[this.chainId],
|
|
893
|
-
};
|
|
894
|
-
const types = {
|
|
895
|
-
RFQOffer: [
|
|
896
|
-
{ name: 'taker', type: 'address' },
|
|
897
|
-
{ name: 'maker', type: 'address' },
|
|
898
|
-
{ name: 'takerToken', type: 'address' },
|
|
899
|
-
{ name: 'takerTokenAmount', type: 'uint256' },
|
|
900
|
-
{ name: 'makerToken', type: 'address' },
|
|
901
|
-
{ name: 'makerTokenAmount', type: 'uint256' },
|
|
902
|
-
{ name: 'feeFactor', type: 'uint256' },
|
|
903
|
-
{ name: 'flags', type: 'uint256' },
|
|
904
|
-
{ name: 'expiry', type: 'uint256' },
|
|
905
|
-
{ name: 'salt', type: 'uint256' },
|
|
906
|
-
],
|
|
907
|
-
RFQTx: [
|
|
908
|
-
{ name: 'rfqOffer', type: 'RFQOffer' },
|
|
909
|
-
{ name: 'recipient', type: 'address' },
|
|
910
|
-
{ name: 'takerRequestAmount', type: 'uint256' },
|
|
911
|
-
],
|
|
383
|
+
primaryType: 'GenericSwapData',
|
|
912
384
|
};
|
|
913
385
|
return this.walletClient.signTypedData({
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
types: types,
|
|
917
|
-
message: rfqTx,
|
|
386
|
+
...EIP712_TYPED,
|
|
387
|
+
message: swapData,
|
|
918
388
|
account,
|
|
919
389
|
});
|
|
920
390
|
}
|
|
921
391
|
});
|
|
922
|
-
Object.defineProperty(this, "signTypedData", {
|
|
923
|
-
enumerable: true,
|
|
924
|
-
configurable: true,
|
|
925
|
-
writable: true,
|
|
926
|
-
value: async (params) => {
|
|
927
|
-
let { account, order, takerTokenPermit, prepare } = params;
|
|
928
|
-
if (order.orderType === type_js_1.OrderType.SOR) {
|
|
929
|
-
if (!prepare) {
|
|
930
|
-
throw new Error('prepare is empty');
|
|
931
|
-
}
|
|
932
|
-
const { swapData } = prepare;
|
|
933
|
-
takerTokenPermit =
|
|
934
|
-
takerTokenPermit ?? this.getTokenPermitData(swapData.takerToken);
|
|
935
|
-
return this.sign712SOROrder(swapData, account);
|
|
936
|
-
}
|
|
937
|
-
else if (order.orderType === type_js_1.OrderType.RFQV6) {
|
|
938
|
-
const rfqOrder = order;
|
|
939
|
-
const { encodeResult } = rfqOrder;
|
|
940
|
-
const { rfqTx } = encodeResult;
|
|
941
|
-
const rfqOffer = rfqTx.rfqOffer;
|
|
942
|
-
takerTokenPermit =
|
|
943
|
-
takerTokenPermit ?? this.getTokenPermitData(rfqOffer.takerToken);
|
|
944
|
-
return this.sign712RFQOrder(rfqTx, account);
|
|
945
|
-
}
|
|
946
|
-
else {
|
|
947
|
-
throw new Error('unsupported order type');
|
|
948
|
-
}
|
|
949
|
-
}
|
|
950
|
-
});
|
|
951
392
|
Object.defineProperty(this, "submit", {
|
|
952
393
|
enumerable: true,
|
|
953
394
|
configurable: true,
|
|
954
395
|
writable: true,
|
|
955
|
-
value: async (
|
|
956
|
-
|
|
957
|
-
const request = {
|
|
958
|
-
uniqueId: order.uniqueId,
|
|
959
|
-
slippage,
|
|
960
|
-
permit: takerTokenPermit,
|
|
961
|
-
fee: {
|
|
962
|
-
protocolFee: 0n,
|
|
963
|
-
gasFee: 0n,
|
|
964
|
-
},
|
|
965
|
-
};
|
|
966
|
-
if (orderSignature)
|
|
967
|
-
request.orderSignature = orderSignature;
|
|
968
|
-
if (txHash)
|
|
969
|
-
request.txHash = txHash;
|
|
970
|
-
if (rawTx)
|
|
971
|
-
request.rawTx = rawTx;
|
|
972
|
-
if (order.orderType === type_js_1.OrderType.SOR) {
|
|
973
|
-
const prepare = await this.prepareOrder({
|
|
974
|
-
order,
|
|
975
|
-
slippage,
|
|
976
|
-
gasFee: gasFee,
|
|
977
|
-
sendByRelayer,
|
|
978
|
-
});
|
|
979
|
-
const { swapData, protocolFee, gasFeeTokenAmount } = prepare;
|
|
980
|
-
request.strategyData = swapData.strategyData;
|
|
981
|
-
request.fee = {
|
|
982
|
-
protocolFee: protocolFee,
|
|
983
|
-
gasFee: gasFeeTokenAmount,
|
|
984
|
-
};
|
|
985
|
-
request.minAmountOut = swapData.minMakerTokenAmount;
|
|
986
|
-
}
|
|
987
|
-
else if (order.orderType === type_js_1.OrderType.RFQV6) {
|
|
988
|
-
order = order;
|
|
989
|
-
const rfqOrderParams = this.formatRFQOrderParams(order);
|
|
990
|
-
const { fee, minMakerTokenAmount } = rfqOrderParams;
|
|
991
|
-
request.slippage = 0;
|
|
992
|
-
request.minAmountOut = minMakerTokenAmount;
|
|
993
|
-
request.fee = {
|
|
994
|
-
gasFee: 0n,
|
|
995
|
-
protocolFee: fee,
|
|
996
|
-
};
|
|
997
|
-
}
|
|
998
|
-
else {
|
|
999
|
-
throw new Error('unsupported order type');
|
|
1000
|
-
}
|
|
1001
|
-
return (0, index_js_2.http)(`${this.specialHost(serviceHost_js_1.httpServiceHost)}/${index_js_1.apiVersion}/swap/submit/${this.chainId}`, {
|
|
396
|
+
value: async (dto) => {
|
|
397
|
+
return (0, index_js_2.http)(`${this.host(serviceHost_js_1.httpServiceHost)}/v6/swap/submit/${this.chainId}`, {
|
|
1002
398
|
fetchOptions: {
|
|
1003
399
|
method: 'POST',
|
|
1004
400
|
},
|
|
1005
|
-
body:
|
|
401
|
+
body: dto,
|
|
1006
402
|
});
|
|
1007
403
|
}
|
|
1008
404
|
});
|
|
@@ -1014,7 +410,7 @@ class Swap extends contextHost_js_1.ContextHost {
|
|
|
1014
410
|
const params = new URLSearchParams({
|
|
1015
411
|
userAddress: dto.account,
|
|
1016
412
|
});
|
|
1017
|
-
return (0, index_js_2.http)(`${this.
|
|
413
|
+
return (0, index_js_2.http)(`${this.host(serviceHost_js_1.httpServiceHost)}?${params}`);
|
|
1018
414
|
}
|
|
1019
415
|
});
|
|
1020
416
|
Object.defineProperty(this, "status", {
|
|
@@ -1025,7 +421,7 @@ class Swap extends contextHost_js_1.ContextHost {
|
|
|
1025
421
|
const params = new URLSearchParams({
|
|
1026
422
|
...dto,
|
|
1027
423
|
});
|
|
1028
|
-
return (0, index_js_2.http)(`${this.
|
|
424
|
+
return (0, index_js_2.http)(`${this.host(serviceHost_js_1.httpServiceHost)}/v6/swap/status/${this.chainId}?${params}`);
|
|
1029
425
|
}
|
|
1030
426
|
});
|
|
1031
427
|
Object.defineProperty(this, "config", {
|
|
@@ -1033,7 +429,7 @@ class Swap extends contextHost_js_1.ContextHost {
|
|
|
1033
429
|
configurable: true,
|
|
1034
430
|
writable: true,
|
|
1035
431
|
value: async (appVersion) => {
|
|
1036
|
-
return (0, index_js_2.http)(`${this.host(serviceHost_js_1.
|
|
432
|
+
return (0, index_js_2.http)(`${this.host(serviceHost_js_1.httpServiceHost)}/v6/swap/config?appVersion=${appVersion}`);
|
|
1037
433
|
}
|
|
1038
434
|
});
|
|
1039
435
|
Object.defineProperty(this, "ordersHistory", {
|
|
@@ -1046,15 +442,7 @@ class Swap extends contextHost_js_1.ContextHost {
|
|
|
1046
442
|
limit: limit.toString(),
|
|
1047
443
|
offset: offset.toString(),
|
|
1048
444
|
});
|
|
1049
|
-
return (0, index_js_2.http)(`${this.host(serviceHost_js_1.
|
|
1050
|
-
}
|
|
1051
|
-
});
|
|
1052
|
-
Object.defineProperty(this, "trendingList", {
|
|
1053
|
-
enumerable: true,
|
|
1054
|
-
configurable: true,
|
|
1055
|
-
writable: true,
|
|
1056
|
-
value: async () => {
|
|
1057
|
-
return (0, index_js_2.http)(`${this.host(serviceHost_js_1.defaultServiceHost)}/${index_js_1.apiVersion}/swap/trendinglist`);
|
|
445
|
+
return (0, index_js_2.http)(`${this.host(serviceHost_js_1.httpServiceHost)}/v6/swap/orders/${chainId}?${params}`);
|
|
1058
446
|
}
|
|
1059
447
|
});
|
|
1060
448
|
this.walletClient = injected.walletClient;
|