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