@tokenlon/v6-sdk 1.0.3 → 1.0.4-canary.14e9384
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/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 +52 -5
- package/dist/cjs/constants/addresses.js.map +1 -1
- package/dist/cjs/constants/chains.js +22 -2
- package/dist/cjs/constants/chains.js.map +1 -1
- package/dist/cjs/constants/common.js +5 -0
- package/dist/cjs/constants/common.js.map +1 -0
- package/dist/cjs/constants/index.js +1 -0
- package/dist/cjs/constants/index.js.map +1 -1
- package/dist/cjs/constants/unit.js +3 -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 +67 -1
- package/dist/cjs/serviceHost.js.map +1 -1
- package/dist/cjs/swap/index.js +739 -92
- 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/types/token.js +3 -0
- package/dist/cjs/types/token.js.map +1 -0
- package/dist/cjs/version.js +1 -1
- package/dist/cjs/version.js.map +1 -1
- 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 +51 -4
- package/dist/esm/constants/addresses.js.map +1 -1
- package/dist/esm/constants/chains.js +23 -3
- package/dist/esm/constants/chains.js.map +1 -1
- package/dist/esm/constants/common.js +2 -0
- package/dist/esm/constants/common.js.map +1 -0
- package/dist/esm/constants/index.js +1 -0
- package/dist/esm/constants/index.js.map +1 -1
- package/dist/esm/constants/unit.js +2 -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 +67 -1
- package/dist/esm/serviceHost.js.map +1 -1
- package/dist/esm/swap/index.js +740 -94
- 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/types/token.js +2 -0
- package/dist/esm/types/token.js.map +1 -0
- 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/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 +2 -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/common.d.ts +2 -0
- package/dist/types/constants/common.d.ts.map +1 -0
- package/dist/types/constants/index.d.ts +1 -0
- package/dist/types/constants/index.d.ts.map +1 -1
- package/dist/types/constants/unit.d.ts +2 -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 +1 -0
- package/dist/types/serviceHost.d.ts.map +1 -1
- package/dist/types/swap/index.d.ts +127 -29
- package/dist/types/swap/index.d.ts.map +1 -1
- package/dist/types/swap/type.d.ts +106 -23
- package/dist/types/swap/type.d.ts.map +1 -1
- package/dist/types/types/token.d.ts +11 -0
- package/dist/types/types/token.d.ts.map +1 -0
- package/dist/types/version.d.ts +1 -1
- package/dist/types/version.d.ts.map +1 -1
- package/package.json +27 -21
package/dist/cjs/swap/index.js
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Swap = exports.
|
|
3
|
+
exports.Swap = exports.l2StrategyOperationsABI = 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");
|
|
9
10
|
const viem_1 = require("viem");
|
|
10
|
-
exports.strategyOperationsABI = (0, viem_1.parseAbiParameters)('(address dest, address inputToken,
|
|
11
|
-
exports.
|
|
11
|
+
exports.strategyOperationsABI = (0, viem_1.parseAbiParameters)('(address dest, address inputToken, uint256 ratioNumerator, uint256 ratioDenominator, uint256 dataOffset, uint256 value, bytes data)[]');
|
|
12
|
+
exports.l2StrategyOperationsABI = (0, viem_1.parseAbiParameters)('(address dest, address inputToken, uint128 inputRatio, uint128 dataOffset, uint256 value, bytes data)[]');
|
|
12
13
|
class Swap extends contextHost_js_1.ContextHost {
|
|
13
14
|
constructor(config, injected) {
|
|
14
15
|
super({ env: config.env, chainId: config.chain }, {
|
|
@@ -37,6 +38,7 @@ class Swap extends contextHost_js_1.ContextHost {
|
|
|
37
38
|
configurable: true,
|
|
38
39
|
writable: true,
|
|
39
40
|
value: (fee, swapData, operations) => {
|
|
41
|
+
const isMainnetOrSepoliaOrder = this.isMainnetOrSepolia();
|
|
40
42
|
const newOperations = [...operations];
|
|
41
43
|
if (fee > 0n) {
|
|
42
44
|
const feeTransfer = (0, viem_1.encodeFunctionData)({
|
|
@@ -44,34 +46,75 @@ class Swap extends contextHost_js_1.ContextHost {
|
|
|
44
46
|
functionName: 'transfer',
|
|
45
47
|
args: [index_js_1.feeReceiptAddress[this.chainId], fee],
|
|
46
48
|
});
|
|
47
|
-
if (!index_js_1.
|
|
48
|
-
throw new Error('
|
|
49
|
-
const feeToken = this.
|
|
50
|
-
? index_js_1.
|
|
49
|
+
if (!index_js_1.wrapperTokenAddress[this.chainId])
|
|
50
|
+
throw new Error('wrapper token address not found');
|
|
51
|
+
const feeToken = this.isNativeToken(swapData.makerToken)
|
|
52
|
+
? index_js_1.wrapperTokenAddress[this.chainId]
|
|
51
53
|
: swapData.makerToken;
|
|
52
|
-
const feeOperation =
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
54
|
+
const feeOperation = isMainnetOrSepoliaOrder
|
|
55
|
+
? {
|
|
56
|
+
dest: feeToken,
|
|
57
|
+
inputToken: feeToken,
|
|
58
|
+
ratioNumerator: 0n,
|
|
59
|
+
ratioDenominator: 0n,
|
|
60
|
+
dataOffset: 0n,
|
|
61
|
+
value: 0n,
|
|
62
|
+
data: feeTransfer,
|
|
63
|
+
}
|
|
64
|
+
: {
|
|
65
|
+
dest: feeToken,
|
|
66
|
+
inputToken: feeToken,
|
|
67
|
+
inputRatio: 0n,
|
|
68
|
+
dataOffset: 0n,
|
|
69
|
+
value: 0n,
|
|
70
|
+
data: feeTransfer,
|
|
71
|
+
};
|
|
60
72
|
newOperations.push(feeOperation);
|
|
61
73
|
}
|
|
62
|
-
const
|
|
74
|
+
const targetStrategyOperationsABI = isMainnetOrSepoliaOrder
|
|
75
|
+
? exports.strategyOperationsABI
|
|
76
|
+
: exports.l2StrategyOperationsABI;
|
|
77
|
+
const newStrategyData = (0, viem_1.encodeAbiParameters)(targetStrategyOperationsABI, [
|
|
63
78
|
newOperations,
|
|
64
79
|
]);
|
|
65
80
|
return newStrategyData;
|
|
66
81
|
}
|
|
67
82
|
});
|
|
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
|
+
});
|
|
68
108
|
Object.defineProperty(this, "getProtocolFeeAmount", {
|
|
69
109
|
enumerable: true,
|
|
70
110
|
configurable: true,
|
|
71
111
|
writable: true,
|
|
72
112
|
value: (order) => {
|
|
73
|
-
const {
|
|
74
|
-
const
|
|
113
|
+
const { protocolFeeFactor: feeFactor } = order;
|
|
114
|
+
const isSOROrder = order?.orderType === type_js_1.OrderType.SOR;
|
|
115
|
+
const makerTokenAmount = isSOROrder
|
|
116
|
+
? order?.encodeResult?.swapData?.makerTokenAmount
|
|
117
|
+
: order?.encodeResult?.rfqTx?.rfqOffer?.makerTokenAmount;
|
|
75
118
|
const protocolFeeAmount = (BigInt(makerTokenAmount) * BigInt(feeFactor)) / index_js_1.bpBase;
|
|
76
119
|
return protocolFeeAmount;
|
|
77
120
|
}
|
|
@@ -81,8 +124,10 @@ class Swap extends contextHost_js_1.ContextHost {
|
|
|
81
124
|
configurable: true,
|
|
82
125
|
writable: true,
|
|
83
126
|
value: (order, slippage) => {
|
|
84
|
-
const
|
|
85
|
-
const
|
|
127
|
+
const isSOROrder = order?.orderType === type_js_1.OrderType.SOR;
|
|
128
|
+
const makerTokenAmount = isSOROrder
|
|
129
|
+
? order?.encodeResult?.swapData?.makerTokenAmount
|
|
130
|
+
: order?.encodeResult?.rfqTx?.rfqOffer?.makerTokenAmount;
|
|
86
131
|
const slippageAmount = (BigInt(makerTokenAmount) * BigInt(slippage)) / index_js_1.bpBase;
|
|
87
132
|
return slippageAmount;
|
|
88
133
|
}
|
|
@@ -104,8 +149,63 @@ class Swap extends contextHost_js_1.ContextHost {
|
|
|
104
149
|
enumerable: true,
|
|
105
150
|
configurable: true,
|
|
106
151
|
writable: true,
|
|
107
|
-
value: async ({ protocolFeeAmount,
|
|
108
|
-
return
|
|
152
|
+
value: async ({ protocolFeeAmount, gasFeeTokenAmount, }) => {
|
|
153
|
+
return gasFeeTokenAmount + protocolFeeAmount;
|
|
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
|
+
}, []);
|
|
109
209
|
}
|
|
110
210
|
});
|
|
111
211
|
Object.defineProperty(this, "getRoutesApproveStrategy", {
|
|
@@ -113,8 +213,10 @@ class Swap extends contextHost_js_1.ContextHost {
|
|
|
113
213
|
configurable: true,
|
|
114
214
|
writable: true,
|
|
115
215
|
value: async (takerTokenAmount, needApprovalDestinations) => {
|
|
116
|
-
if (needApprovalDestinations
|
|
216
|
+
if (!Array.isArray(needApprovalDestinations) ||
|
|
217
|
+
needApprovalDestinations.length === 0) {
|
|
117
218
|
return [];
|
|
219
|
+
}
|
|
118
220
|
const allowances = await Promise.all([
|
|
119
221
|
...needApprovalDestinations.map(({ dest, inputToken }) => this.publicClient.readContract({
|
|
120
222
|
abi: [
|
|
@@ -139,7 +241,8 @@ class Swap extends contextHost_js_1.ContextHost {
|
|
|
139
241
|
const approveOperation = {
|
|
140
242
|
dest: inputToken,
|
|
141
243
|
inputToken: inputToken,
|
|
142
|
-
|
|
244
|
+
ratioNumerator: 0n,
|
|
245
|
+
ratioDenominator: 0n,
|
|
143
246
|
dataOffset: 0n,
|
|
144
247
|
value: 0n,
|
|
145
248
|
data: approve,
|
|
@@ -158,7 +261,8 @@ class Swap extends contextHost_js_1.ContextHost {
|
|
|
158
261
|
const approveOperation = {
|
|
159
262
|
dest: inputToken,
|
|
160
263
|
inputToken: inputToken,
|
|
161
|
-
|
|
264
|
+
ratioNumerator: 0n,
|
|
265
|
+
ratioDenominator: 0n,
|
|
162
266
|
dataOffset: 0n,
|
|
163
267
|
value: 0n,
|
|
164
268
|
data: approve2Zero,
|
|
@@ -176,63 +280,260 @@ class Swap extends contextHost_js_1.ContextHost {
|
|
|
176
280
|
return approveStrategies;
|
|
177
281
|
}
|
|
178
282
|
});
|
|
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
|
+
});
|
|
179
413
|
Object.defineProperty(this, "prepareOrder", {
|
|
180
414
|
enumerable: true,
|
|
181
415
|
configurable: true,
|
|
182
416
|
writable: true,
|
|
183
417
|
value: async ({ order, slippage, gasFee = 0n, sendByRelayer, }) => {
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
418
|
+
let prepare;
|
|
419
|
+
if (order.orderType === type_js_1.OrderType.SOR) {
|
|
420
|
+
order = order;
|
|
421
|
+
const isMainnetOrSepoliaOrder = this.isMainnetOrSepolia();
|
|
422
|
+
const encodeResult = order.encodeResult ?? {};
|
|
423
|
+
const { swapData, needApprovalDestinations } = encodeResult;
|
|
424
|
+
const { recipient, strategyData, makerToken, takerToken } = swapData;
|
|
425
|
+
const makerTokenAmount = BigInt(swapData.makerTokenAmount);
|
|
426
|
+
const slippageAmount = this.getSlippageAmount(order, slippage);
|
|
427
|
+
const protocolFeeAmount = this.getProtocolFeeAmount(order);
|
|
428
|
+
const value = (0, viem_1.isAddressEqual)(swapData.takerToken, index_js_1.nullAddress) ||
|
|
429
|
+
(0, viem_1.isAddressEqual)(swapData.takerToken, viem_1.zeroAddress)
|
|
430
|
+
? BigInt(swapData.takerTokenAmount)
|
|
431
|
+
: 0n;
|
|
432
|
+
let fee = protocolFeeAmount;
|
|
433
|
+
let gasFeeTokenAmount = gasFee;
|
|
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
|
+
}
|
|
200
509
|
}
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
gasFeeTokenAmount: feeTokenAmount,
|
|
226
|
-
gasFee,
|
|
227
|
-
fee,
|
|
228
|
-
recipient,
|
|
229
|
-
operations,
|
|
230
|
-
strategyData: newStrategyData,
|
|
231
|
-
value,
|
|
232
|
-
};
|
|
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');
|
|
521
|
+
}
|
|
522
|
+
return prepare;
|
|
523
|
+
}
|
|
524
|
+
});
|
|
525
|
+
Object.defineProperty(this, "getTokenList", {
|
|
526
|
+
enumerable: true,
|
|
527
|
+
configurable: true,
|
|
528
|
+
writable: true,
|
|
529
|
+
value: async (chainId) => {
|
|
530
|
+
if (!chainId)
|
|
531
|
+
chainId = this.chainId;
|
|
532
|
+
const tokenList = await (0, index_js_2.http)(`${this.host(serviceHost_js_1.httpServiceHost)}/${index_js_1.apiVersion}/swap/tokenlist/${chainId}`);
|
|
533
|
+
return tokenList;
|
|
233
534
|
}
|
|
234
535
|
});
|
|
235
|
-
Object.defineProperty(this, "
|
|
536
|
+
Object.defineProperty(this, "quoteNewOrder", {
|
|
236
537
|
enumerable: true,
|
|
237
538
|
configurable: true,
|
|
238
539
|
writable: true,
|
|
@@ -245,7 +546,16 @@ class Swap extends contextHost_js_1.ContextHost {
|
|
|
245
546
|
Object.entries(dto).forEach(([key, value]) => {
|
|
246
547
|
p.append(key, value.toString());
|
|
247
548
|
});
|
|
248
|
-
const order = await (0, index_js_2.http)(`${this.host(serviceHost_js_1.httpServiceHost)}/
|
|
549
|
+
const order = await (0, index_js_2.http)(`${this.host(serviceHost_js_1.httpServiceHost)}/${index_js_1.apiVersion}/swap/quote/${chainId}?${p.toString()}`);
|
|
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.host(serviceHost_js_1.httpServiceHost)}/${index_js_1.apiVersion}/swap/swap/${this.chainId}?uniqueId=${uniqueId}`);
|
|
249
559
|
return order;
|
|
250
560
|
}
|
|
251
561
|
});
|
|
@@ -256,13 +566,13 @@ class Swap extends contextHost_js_1.ContextHost {
|
|
|
256
566
|
value: (token, permitType = permit2_js_1.PermitSource.Permit2AllowanceTransfer) => {
|
|
257
567
|
const permitToAllowanceTarget = (0, viem_1.encodePacked)(['uint8', 'bytes'], [permitType, '0x']);
|
|
258
568
|
let takerTokenPermit = permitToAllowanceTarget;
|
|
259
|
-
if (this.
|
|
569
|
+
if (this.isNativeToken(token)) {
|
|
260
570
|
takerTokenPermit = '0x';
|
|
261
571
|
}
|
|
262
572
|
return takerTokenPermit;
|
|
263
573
|
}
|
|
264
574
|
});
|
|
265
|
-
Object.defineProperty(this, "
|
|
575
|
+
Object.defineProperty(this, "isNativeToken", {
|
|
266
576
|
enumerable: true,
|
|
267
577
|
configurable: true,
|
|
268
578
|
writable: true,
|
|
@@ -271,16 +581,95 @@ class Swap extends contextHost_js_1.ContextHost {
|
|
|
271
581
|
(0, viem_1.isAddressEqual)(tokenAddress, viem_1.zeroAddress));
|
|
272
582
|
}
|
|
273
583
|
});
|
|
274
|
-
Object.defineProperty(this, "
|
|
584
|
+
Object.defineProperty(this, "isMainnetOrSepolia", {
|
|
275
585
|
enumerable: true,
|
|
276
586
|
configurable: true,
|
|
277
587
|
writable: true,
|
|
278
|
-
value:
|
|
279
|
-
const
|
|
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", {
|
|
662
|
+
enumerable: true,
|
|
663
|
+
configurable: true,
|
|
664
|
+
writable: true,
|
|
665
|
+
value: async ({ account, order, slippage, takerTokenPermit, }) => {
|
|
666
|
+
const prepare = await this.prepareOrder({
|
|
280
667
|
order,
|
|
281
668
|
slippage,
|
|
282
669
|
});
|
|
283
|
-
const
|
|
670
|
+
const { swapData, value } = prepare;
|
|
671
|
+
takerTokenPermit =
|
|
672
|
+
takerTokenPermit ?? this.getTokenPermitData(swapData.takerToken);
|
|
284
673
|
const gasEstimate = await this.publicClient.estimateContractGas({
|
|
285
674
|
address: index_js_1.genericSwapAddress[this.chainId],
|
|
286
675
|
abi: index_js_1.GENERIC_SWAP_ABI,
|
|
@@ -292,7 +681,102 @@ class Swap extends contextHost_js_1.ContextHost {
|
|
|
292
681
|
return gasEstimate;
|
|
293
682
|
}
|
|
294
683
|
});
|
|
295
|
-
Object.defineProperty(this, "
|
|
684
|
+
Object.defineProperty(this, "estimate", {
|
|
685
|
+
enumerable: true,
|
|
686
|
+
configurable: true,
|
|
687
|
+
writable: true,
|
|
688
|
+
value: async ({ account, order, slippage, takerTokenPermit, }) => {
|
|
689
|
+
if (order.orderType === type_js_1.OrderType.SOR) {
|
|
690
|
+
order = order;
|
|
691
|
+
const { swapData } = order.encodeResult;
|
|
692
|
+
takerTokenPermit =
|
|
693
|
+
takerTokenPermit ?? this.getTokenPermitData(swapData.takerToken);
|
|
694
|
+
return await this.estimateSOROrder({
|
|
695
|
+
account,
|
|
696
|
+
order: order,
|
|
697
|
+
slippage,
|
|
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,
|
|
710
|
+
});
|
|
711
|
+
}
|
|
712
|
+
else {
|
|
713
|
+
throw new Error('unsupported order type');
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
});
|
|
717
|
+
Object.defineProperty(this, "estimateWithFallback", {
|
|
718
|
+
enumerable: true,
|
|
719
|
+
configurable: true,
|
|
720
|
+
writable: true,
|
|
721
|
+
value: async ({ account, order, slippage, sendByRelayer, takerTokenPermit, }) => {
|
|
722
|
+
let gasEstimate;
|
|
723
|
+
try {
|
|
724
|
+
gasEstimate = await this.estimate({
|
|
725
|
+
account,
|
|
726
|
+
order,
|
|
727
|
+
slippage,
|
|
728
|
+
takerTokenPermit,
|
|
729
|
+
});
|
|
730
|
+
}
|
|
731
|
+
catch (_error) {
|
|
732
|
+
gasEstimate = await this.estimateByApi(order, sendByRelayer);
|
|
733
|
+
}
|
|
734
|
+
return gasEstimate;
|
|
735
|
+
}
|
|
736
|
+
});
|
|
737
|
+
Object.defineProperty(this, "signRFQOrder", {
|
|
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", {
|
|
296
780
|
enumerable: true,
|
|
297
781
|
configurable: true,
|
|
298
782
|
writable: true,
|
|
@@ -311,6 +795,59 @@ class Swap extends contextHost_js_1.ContextHost {
|
|
|
311
795
|
return txHash;
|
|
312
796
|
}
|
|
313
797
|
});
|
|
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 * 120n) / 100n;
|
|
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
|
+
});
|
|
314
851
|
Object.defineProperty(this, "signWithReceipt", {
|
|
315
852
|
enumerable: true,
|
|
316
853
|
configurable: true,
|
|
@@ -323,7 +860,7 @@ class Swap extends contextHost_js_1.ContextHost {
|
|
|
323
860
|
return receipt;
|
|
324
861
|
}
|
|
325
862
|
});
|
|
326
|
-
Object.defineProperty(this, "
|
|
863
|
+
Object.defineProperty(this, "sign712SOROrder", {
|
|
327
864
|
enumerable: true,
|
|
328
865
|
configurable: true,
|
|
329
866
|
writable: true,
|
|
@@ -334,28 +871,138 @@ class Swap extends contextHost_js_1.ContextHost {
|
|
|
334
871
|
chainId: this.chainId,
|
|
335
872
|
verifyingContract: index_js_1.genericSwapAddress[this.chainId],
|
|
336
873
|
};
|
|
337
|
-
|
|
874
|
+
return this.walletClient.signTypedData({
|
|
875
|
+
primaryType: 'GenericSwapData',
|
|
338
876
|
domain: EIP712_DOMAIN,
|
|
339
877
|
types: index_js_1.GS_EIP712_TYPES,
|
|
340
|
-
|
|
878
|
+
message: swapData,
|
|
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
|
+
],
|
|
341
912
|
};
|
|
342
913
|
return this.walletClient.signTypedData({
|
|
343
|
-
|
|
344
|
-
|
|
914
|
+
primaryType: 'RFQTx',
|
|
915
|
+
domain: EIP712_DOMAIN,
|
|
916
|
+
types: types,
|
|
917
|
+
message: rfqTx,
|
|
345
918
|
account,
|
|
346
919
|
});
|
|
347
920
|
}
|
|
348
921
|
});
|
|
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
|
+
});
|
|
349
951
|
Object.defineProperty(this, "submit", {
|
|
350
952
|
enumerable: true,
|
|
351
953
|
configurable: true,
|
|
352
954
|
writable: true,
|
|
353
|
-
value: async (
|
|
354
|
-
|
|
955
|
+
value: async (params) => {
|
|
956
|
+
let { order, slippage, takerTokenPermit, txHash, rawTx, gasFee, sendByRelayer, orderSignature, } = params;
|
|
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.host(serviceHost_js_1.httpServiceHost)}/${index_js_1.apiVersion}/swap/submit/${this.chainId}`, {
|
|
355
1002
|
fetchOptions: {
|
|
356
1003
|
method: 'POST',
|
|
357
1004
|
},
|
|
358
|
-
body:
|
|
1005
|
+
body: request,
|
|
359
1006
|
});
|
|
360
1007
|
}
|
|
361
1008
|
});
|
|
@@ -378,7 +1025,7 @@ class Swap extends contextHost_js_1.ContextHost {
|
|
|
378
1025
|
const params = new URLSearchParams({
|
|
379
1026
|
...dto,
|
|
380
1027
|
});
|
|
381
|
-
return (0, index_js_2.http)(`${this.host(serviceHost_js_1.httpServiceHost)}/
|
|
1028
|
+
return (0, index_js_2.http)(`${this.host(serviceHost_js_1.httpServiceHost)}/${index_js_1.apiVersion}/swap/status/${this.chainId}?${params}`);
|
|
382
1029
|
}
|
|
383
1030
|
});
|
|
384
1031
|
Object.defineProperty(this, "config", {
|
|
@@ -386,7 +1033,7 @@ class Swap extends contextHost_js_1.ContextHost {
|
|
|
386
1033
|
configurable: true,
|
|
387
1034
|
writable: true,
|
|
388
1035
|
value: async (appVersion) => {
|
|
389
|
-
return (0, index_js_2.http)(`${this.host(serviceHost_js_1.httpServiceHost)}/
|
|
1036
|
+
return (0, index_js_2.http)(`${this.host(serviceHost_js_1.httpServiceHost)}/${index_js_1.apiVersion}/swap/config?appVersion=${appVersion}`);
|
|
390
1037
|
}
|
|
391
1038
|
});
|
|
392
1039
|
this.walletClient = injected.walletClient;
|