@tokenlon/v6-sdk 1.0.2 → 1.0.3-canary.3c1bacd
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/apiVersion.js +5 -0
- package/dist/cjs/apiVersion.js.map +1 -0
- package/dist/cjs/constants/RFQAbi.js +723 -0
- package/dist/cjs/constants/RFQAbi.js.map +1 -0
- package/dist/cjs/constants/abis.js +301 -1
- package/dist/cjs/constants/abis.js.map +1 -1
- package/dist/cjs/constants/addresses.js +18 -1
- package/dist/cjs/constants/addresses.js.map +1 -1
- package/dist/cjs/constants/chains.js +2 -2
- package/dist/cjs/constants/chains.js.map +1 -1
- package/dist/cjs/constants/unit.js +2 -1
- package/dist/cjs/constants/unit.js.map +1 -1
- package/dist/cjs/sdk.js +7 -5
- package/dist/cjs/sdk.js.map +1 -1
- package/dist/cjs/serviceHost.js +6 -0
- package/dist/cjs/serviceHost.js.map +1 -1
- package/dist/cjs/swap/index.js +491 -80
- package/dist/cjs/swap/index.js.map +1 -1
- package/dist/cjs/swap/type.js +6 -1
- package/dist/cjs/swap/type.js.map +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/cjs/version.js.map +1 -1
- package/dist/esm/apiVersion.js +2 -0
- package/dist/esm/apiVersion.js.map +1 -0
- package/dist/esm/constants/RFQAbi.js +721 -0
- package/dist/esm/constants/RFQAbi.js.map +1 -0
- package/dist/esm/constants/abis.js +300 -0
- package/dist/esm/constants/abis.js.map +1 -1
- package/dist/esm/constants/addresses.js +17 -0
- package/dist/esm/constants/addresses.js.map +1 -1
- package/dist/esm/constants/chains.js +3 -3
- package/dist/esm/constants/chains.js.map +1 -1
- package/dist/esm/constants/unit.js +1 -0
- package/dist/esm/constants/unit.js.map +1 -1
- package/dist/esm/sdk.js +7 -5
- package/dist/esm/sdk.js.map +1 -1
- package/dist/esm/serviceHost.js +7 -1
- package/dist/esm/serviceHost.js.map +1 -1
- package/dist/esm/swap/index.js +490 -80
- package/dist/esm/swap/index.js.map +1 -1
- package/dist/esm/swap/type.js +5 -0
- package/dist/esm/swap/type.js.map +1 -1
- package/dist/esm/version.js +1 -1
- package/dist/esm/version.js.map +1 -1
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/dist/types/apiVersion.d.ts +2 -0
- package/dist/types/apiVersion.d.ts.map +1 -0
- package/dist/types/constants/RFQAbi.d.ts +82 -0
- package/dist/types/constants/RFQAbi.d.ts.map +1 -0
- package/dist/types/constants/abis.d.ts +336 -0
- package/dist/types/constants/abis.d.ts.map +1 -1
- package/dist/types/constants/addresses.d.ts +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/unit.d.ts +1 -0
- package/dist/types/constants/unit.d.ts.map +1 -1
- package/dist/types/errors/base.d.ts +1 -1
- package/dist/types/errors/base.d.ts.map +1 -1
- package/dist/types/sdk.d.ts +1 -0
- package/dist/types/sdk.d.ts.map +1 -1
- package/dist/types/serviceHost.d.ts.map +1 -1
- package/dist/types/swap/index.d.ts +103 -19
- package/dist/types/swap/index.d.ts.map +1 -1
- package/dist/types/swap/type.d.ts +54 -18
- package/dist/types/swap/type.d.ts.map +1 -1
- package/dist/types/version.d.ts +1 -1
- package/dist/types/version.d.ts.map +1 -1
- package/package.json +28 -21
package/dist/esm/swap/index.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { apiVersion } from '../apiVersion.js';
|
|
2
|
+
import { DEFAULT_GAS_LIMIT, GENERIC_SWAP_ABI, GS_EIP712_TYPES, MAX_UINT256, RFQ_ABI, bpBase, feeReceiptAddress, genericSwapAddress, nullAddress, rfqAddress, strategyAddress, usdtAddress, wethAddress, } from '../constants/index.js';
|
|
2
3
|
import { ContextHost } from '../service/contextHost.js';
|
|
3
4
|
import { PermitSource } from '../types/permit2.js';
|
|
4
5
|
import { http } from '../utils/request/index.js';
|
|
5
6
|
import { httpServiceHost } from './../serviceHost.js';
|
|
6
|
-
import {} from './type.js';
|
|
7
|
+
import { OrderType, } from './type.js';
|
|
7
8
|
import { JsonRpcProvider } from 'ethers';
|
|
8
9
|
import { decodeAbiParameters, encodeAbiParameters, encodeFunctionData, encodePacked, isAddressEqual, parseAbiItem, parseAbiParameters, zeroAddress, } from 'viem';
|
|
9
|
-
const strategyOperationsABI = parseAbiParameters('(address dest, address inputToken, uint128 inputRatio, uint128 dataOffset, uint256 value, bytes data)[]');
|
|
10
|
+
export const strategyOperationsABI = parseAbiParameters('(address dest, address inputToken, uint128 inputRatio, uint128 dataOffset, uint256 value, bytes data)[]');
|
|
10
11
|
export const ERC20_TRANSFER_GAS = 65000n;
|
|
11
12
|
export class Swap extends ContextHost {
|
|
12
13
|
constructor(config, injected) {
|
|
@@ -64,12 +65,39 @@ export class Swap extends ContextHost {
|
|
|
64
65
|
return newStrategyData;
|
|
65
66
|
}
|
|
66
67
|
});
|
|
68
|
+
Object.defineProperty(this, "formatRFQOrderParams", {
|
|
69
|
+
enumerable: true,
|
|
70
|
+
configurable: true,
|
|
71
|
+
writable: true,
|
|
72
|
+
value: (order) => {
|
|
73
|
+
const encodeResult = order.encodeResult;
|
|
74
|
+
const rfqTx = encodeResult.rfqTx;
|
|
75
|
+
const rfqOffer = rfqTx.rfqOffer ?? {};
|
|
76
|
+
const { fromAddress } = order;
|
|
77
|
+
const { makerTokenAmount } = rfqOffer;
|
|
78
|
+
const value = BigInt(this.isETH(fromAddress) ? rfqOffer.takerTokenAmount : '0x0');
|
|
79
|
+
const fee = (BigInt(makerTokenAmount) * BigInt(rfqOffer.feeFactor)) / bpBase;
|
|
80
|
+
const minMakerTokenAmount = BigInt(makerTokenAmount) - fee;
|
|
81
|
+
return {
|
|
82
|
+
rfqOffer,
|
|
83
|
+
rfqTx,
|
|
84
|
+
value,
|
|
85
|
+
fee,
|
|
86
|
+
minMakerTokenAmount: minMakerTokenAmount,
|
|
87
|
+
makerTokenAmount: rfqOffer.makerTokenAmount,
|
|
88
|
+
takerTokenAmount: rfqOffer.takerTokenAmount,
|
|
89
|
+
takerRequestAmount: rfqTx.takerRequestAmount,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
});
|
|
67
93
|
Object.defineProperty(this, "getProtocolFeeAmount", {
|
|
68
94
|
enumerable: true,
|
|
69
95
|
configurable: true,
|
|
70
96
|
writable: true,
|
|
71
97
|
value: (order) => {
|
|
72
|
-
const
|
|
98
|
+
const encodeResult = order?.encodeResult ?? {};
|
|
99
|
+
const { feeFactor } = order;
|
|
100
|
+
const { swapData } = encodeResult;
|
|
73
101
|
const { makerTokenAmount } = swapData;
|
|
74
102
|
const protocolFeeAmount = (BigInt(makerTokenAmount) * BigInt(feeFactor)) / bpBase;
|
|
75
103
|
return protocolFeeAmount;
|
|
@@ -80,8 +108,10 @@ export class Swap extends ContextHost {
|
|
|
80
108
|
configurable: true,
|
|
81
109
|
writable: true,
|
|
82
110
|
value: (order, slippage) => {
|
|
83
|
-
const
|
|
84
|
-
const
|
|
111
|
+
const isSOROrder = order?.orderType === OrderType.SOR;
|
|
112
|
+
const makerTokenAmount = isSOROrder
|
|
113
|
+
? order?.encodeResult?.swapData?.makerTokenAmount
|
|
114
|
+
: order?.encodeResult?.rfqTx?.rfqOffer?.makerTokenAmount;
|
|
85
115
|
const slippageAmount = (BigInt(makerTokenAmount) * BigInt(slippage)) / bpBase;
|
|
86
116
|
return slippageAmount;
|
|
87
117
|
}
|
|
@@ -111,39 +141,60 @@ export class Swap extends ContextHost {
|
|
|
111
141
|
enumerable: true,
|
|
112
142
|
configurable: true,
|
|
113
143
|
writable: true,
|
|
114
|
-
value: async (
|
|
115
|
-
if (
|
|
116
|
-
|
|
144
|
+
value: async (takerTokenAmount, needApprovalDestinations) => {
|
|
145
|
+
if (!Array.isArray(needApprovalDestinations) ||
|
|
146
|
+
needApprovalDestinations.length === 0)
|
|
117
147
|
return [];
|
|
118
148
|
const allowances = await Promise.all([
|
|
119
|
-
...
|
|
149
|
+
...needApprovalDestinations.map(({ dest, inputToken }) => this.publicClient.readContract({
|
|
120
150
|
abi: [
|
|
121
151
|
parseAbiItem('function allowance(address owner, address spender) view returns (uint256)'),
|
|
122
152
|
],
|
|
123
153
|
functionName: 'allowance',
|
|
124
|
-
address:
|
|
125
|
-
args: [strategyAddress[this.chainId],
|
|
154
|
+
address: inputToken,
|
|
155
|
+
args: [strategyAddress[this.chainId], dest],
|
|
126
156
|
})),
|
|
127
157
|
]);
|
|
128
158
|
const approveStrategies = allowances.reduce((acc, allowance, index) => {
|
|
129
159
|
try {
|
|
130
|
-
if (BigInt(allowance)
|
|
160
|
+
if (BigInt(allowance) < BigInt(takerTokenAmount)) {
|
|
161
|
+
const { dest, inputToken } = needApprovalDestinations[index];
|
|
131
162
|
const approve = encodeFunctionData({
|
|
132
163
|
abi: [
|
|
133
164
|
parseAbiItem('function approve(address spender, uint256 amount) returns (bool)'),
|
|
134
165
|
],
|
|
135
166
|
functionName: 'approve',
|
|
136
|
-
args: [
|
|
167
|
+
args: [dest, MAX_UINT256],
|
|
137
168
|
});
|
|
138
169
|
const approveOperation = {
|
|
139
|
-
dest:
|
|
140
|
-
inputToken:
|
|
170
|
+
dest: inputToken,
|
|
171
|
+
inputToken: inputToken,
|
|
141
172
|
inputRatio: 0n,
|
|
142
173
|
dataOffset: 0n,
|
|
143
174
|
value: 0n,
|
|
144
175
|
data: approve,
|
|
145
176
|
};
|
|
146
177
|
acc.push(approveOperation);
|
|
178
|
+
if (inputToken.toLowerCase() ===
|
|
179
|
+
usdtAddress[this.chainId]?.toLowerCase() &&
|
|
180
|
+
BigInt(allowance) > 0n) {
|
|
181
|
+
const approve2Zero = encodeFunctionData({
|
|
182
|
+
abi: [
|
|
183
|
+
parseAbiItem('function approve(address spender, uint256 amount) returns (bool)'),
|
|
184
|
+
],
|
|
185
|
+
functionName: 'approve',
|
|
186
|
+
args: [dest, 0n],
|
|
187
|
+
});
|
|
188
|
+
const approveOperation = {
|
|
189
|
+
dest: inputToken,
|
|
190
|
+
inputToken: inputToken,
|
|
191
|
+
inputRatio: 0n,
|
|
192
|
+
dataOffset: 0n,
|
|
193
|
+
value: 0n,
|
|
194
|
+
data: approve2Zero,
|
|
195
|
+
};
|
|
196
|
+
acc.unshift(approveOperation);
|
|
197
|
+
}
|
|
147
198
|
return acc;
|
|
148
199
|
}
|
|
149
200
|
return acc;
|
|
@@ -155,60 +206,142 @@ export class Swap extends ContextHost {
|
|
|
155
206
|
return approveStrategies;
|
|
156
207
|
}
|
|
157
208
|
});
|
|
209
|
+
Object.defineProperty(this, "getRFQFeeAmount", {
|
|
210
|
+
enumerable: true,
|
|
211
|
+
configurable: true,
|
|
212
|
+
writable: true,
|
|
213
|
+
value: (order) => {
|
|
214
|
+
const encodeResult = order?.encodeResult ?? {};
|
|
215
|
+
const { rfqTx } = encodeResult;
|
|
216
|
+
const { rfqOffer } = rfqTx;
|
|
217
|
+
const protocolFeeAmount = (BigInt(rfqOffer.makerTokenAmount) * BigInt(rfqOffer.feeFactor)) / bpBase;
|
|
218
|
+
return protocolFeeAmount;
|
|
219
|
+
}
|
|
220
|
+
});
|
|
221
|
+
Object.defineProperty(this, "preview", {
|
|
222
|
+
enumerable: true,
|
|
223
|
+
configurable: true,
|
|
224
|
+
writable: true,
|
|
225
|
+
value: async (params) => {
|
|
226
|
+
let preview;
|
|
227
|
+
let { order, account, slippage } = params;
|
|
228
|
+
const gas = await this.estimate({
|
|
229
|
+
account,
|
|
230
|
+
order,
|
|
231
|
+
slippage,
|
|
232
|
+
}).catch(() => DEFAULT_GAS_LIMIT);
|
|
233
|
+
if (order.orderType === OrderType.SOR) {
|
|
234
|
+
order = order;
|
|
235
|
+
const sorPrepare = await this.prepareOrder(params);
|
|
236
|
+
const { swapData, fee } = sorPrepare;
|
|
237
|
+
preview = {
|
|
238
|
+
inputToken: swapData.takerToken,
|
|
239
|
+
outputToken: swapData.makerToken,
|
|
240
|
+
amountIn: swapData.takerTokenAmount,
|
|
241
|
+
amountOut: swapData.makerTokenAmount,
|
|
242
|
+
minAmountOut: swapData.minMakerTokenAmount,
|
|
243
|
+
recipient: swapData.recipient,
|
|
244
|
+
fee,
|
|
245
|
+
gas,
|
|
246
|
+
feeFactor: order.feeFactor,
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
else if (order.orderType === OrderType.RFQV6) {
|
|
250
|
+
order = order;
|
|
251
|
+
const { rfqTx } = order.encodeResult;
|
|
252
|
+
const { rfqOffer } = rfqTx;
|
|
253
|
+
const { fee, minMakerTokenAmount } = this.formatRFQOrderParams(order);
|
|
254
|
+
preview = {
|
|
255
|
+
inputToken: rfqOffer.takerToken,
|
|
256
|
+
outputToken: rfqOffer.makerToken,
|
|
257
|
+
amountIn: rfqOffer.takerTokenAmount,
|
|
258
|
+
amountOut: rfqOffer.makerTokenAmount,
|
|
259
|
+
minAmountOut: minMakerTokenAmount,
|
|
260
|
+
recipient: rfqTx.recipient,
|
|
261
|
+
fee,
|
|
262
|
+
gas,
|
|
263
|
+
feeFactor: order.feeFactor,
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
else {
|
|
267
|
+
throw new Error('unsupported order type');
|
|
268
|
+
}
|
|
269
|
+
return preview;
|
|
270
|
+
}
|
|
271
|
+
});
|
|
158
272
|
Object.defineProperty(this, "prepareOrder", {
|
|
159
273
|
enumerable: true,
|
|
160
274
|
configurable: true,
|
|
161
275
|
writable: true,
|
|
162
276
|
value: async ({ order, slippage, gasFee = 0n, sendByRelayer, }) => {
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
277
|
+
let prepare;
|
|
278
|
+
if (order.orderType === OrderType.SOR) {
|
|
279
|
+
order = order;
|
|
280
|
+
const encodeResult = order.encodeResult ?? {};
|
|
281
|
+
const { swapData, needApprovalDestinations } = encodeResult;
|
|
282
|
+
const { recipient, strategyData, makerToken } = swapData;
|
|
283
|
+
const makerTokenAmount = BigInt(swapData.makerTokenAmount);
|
|
284
|
+
const slippageAmount = this.getSlippageAmount(order, slippage);
|
|
285
|
+
const protocolFeeAmount = this.getProtocolFeeAmount(order);
|
|
286
|
+
let fee = protocolFeeAmount;
|
|
287
|
+
let feeTokenAmount = gasFee;
|
|
288
|
+
if (sendByRelayer) {
|
|
289
|
+
feeTokenAmount = await this.calcGasFeeTokenAmount({
|
|
290
|
+
gasFeeAmount: gasFee,
|
|
291
|
+
feeToken: makerToken,
|
|
292
|
+
});
|
|
293
|
+
fee = await this.calcRelayerFee({
|
|
294
|
+
protocolFeeAmount,
|
|
295
|
+
feeTokenAmount,
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
const minMakerTokenAmount = makerTokenAmount - slippageAmount - fee;
|
|
299
|
+
swapData.minMakerTokenAmount = minMakerTokenAmount.toString();
|
|
300
|
+
const [operations] = decodeAbiParameters(strategyOperationsABI, strategyData);
|
|
301
|
+
const approveOperations = await this.getRoutesApproveStrategy(swapData.takerTokenAmount, needApprovalDestinations);
|
|
302
|
+
const newStrategyData = this.rebuildStrategyData(fee, swapData, [
|
|
303
|
+
...approveOperations,
|
|
304
|
+
...operations,
|
|
305
|
+
]);
|
|
306
|
+
const value = isAddressEqual(swapData.takerToken, nullAddress) ||
|
|
307
|
+
isAddressEqual(swapData.takerToken, zeroAddress)
|
|
308
|
+
? BigInt(swapData.takerTokenAmount)
|
|
309
|
+
: 0n;
|
|
310
|
+
const newSwapData = {
|
|
311
|
+
...swapData,
|
|
312
|
+
strategyData: newStrategyData,
|
|
313
|
+
takerTokenAmount: BigInt(swapData.takerTokenAmount),
|
|
314
|
+
makerTokenAmount: BigInt(swapData.makerTokenAmount),
|
|
315
|
+
minMakerTokenAmount: BigInt(swapData.minMakerTokenAmount),
|
|
316
|
+
salt: BigInt(swapData.salt),
|
|
317
|
+
expiry: BigInt(swapData.expiry),
|
|
318
|
+
};
|
|
319
|
+
prepare = {
|
|
320
|
+
swapData: newSwapData,
|
|
321
|
+
protocolFee: protocolFeeAmount,
|
|
322
|
+
gasFeeTokenAmount: feeTokenAmount,
|
|
323
|
+
gasFee,
|
|
324
|
+
fee,
|
|
325
|
+
recipient,
|
|
326
|
+
operations,
|
|
327
|
+
strategyData: newStrategyData,
|
|
328
|
+
value,
|
|
329
|
+
minMakerTokenAmount,
|
|
330
|
+
makerTokenAmount: newSwapData.makerTokenAmount,
|
|
331
|
+
takerTokenAmount: newSwapData.takerTokenAmount,
|
|
332
|
+
};
|
|
179
333
|
}
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
: 0n;
|
|
192
|
-
const newSwapData = {
|
|
193
|
-
...swapData,
|
|
194
|
-
strategyData: newStrategyData,
|
|
195
|
-
takerTokenAmount: BigInt(swapData.takerTokenAmount),
|
|
196
|
-
makerTokenAmount: BigInt(swapData.makerTokenAmount),
|
|
197
|
-
minMakerTokenAmount: BigInt(swapData.minMakerTokenAmount),
|
|
198
|
-
salt: BigInt(swapData.salt),
|
|
199
|
-
expiry: BigInt(swapData.expiry),
|
|
200
|
-
};
|
|
201
|
-
return {
|
|
202
|
-
swapData: newSwapData,
|
|
203
|
-
protocolFee: protocolFeeAmount,
|
|
204
|
-
gasFeeTokenAmount: feeTokenAmount,
|
|
205
|
-
gasFee,
|
|
206
|
-
fee,
|
|
207
|
-
recipient,
|
|
208
|
-
operations,
|
|
209
|
-
strategyData: newStrategyData,
|
|
210
|
-
value,
|
|
211
|
-
};
|
|
334
|
+
else if (order.orderType === OrderType.RFQV6) {
|
|
335
|
+
order = order;
|
|
336
|
+
const rfqOrderParams = this.formatRFQOrderParams(order);
|
|
337
|
+
prepare = {
|
|
338
|
+
...rfqOrderParams,
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
else {
|
|
342
|
+
throw new Error('unsupported order type');
|
|
343
|
+
}
|
|
344
|
+
return prepare;
|
|
212
345
|
}
|
|
213
346
|
});
|
|
214
347
|
Object.defineProperty(this, "quote", {
|
|
@@ -224,7 +357,7 @@ export class Swap extends ContextHost {
|
|
|
224
357
|
Object.entries(dto).forEach(([key, value]) => {
|
|
225
358
|
p.append(key, value.toString());
|
|
226
359
|
});
|
|
227
|
-
const order = await http(`${this.host(httpServiceHost)}/
|
|
360
|
+
const order = await http(`${this.host(httpServiceHost)}/${apiVersion}/swap/quote/${chainId}?${p.toString()}`);
|
|
228
361
|
return order;
|
|
229
362
|
}
|
|
230
363
|
});
|
|
@@ -250,16 +383,58 @@ export class Swap extends ContextHost {
|
|
|
250
383
|
isAddressEqual(tokenAddress, zeroAddress));
|
|
251
384
|
}
|
|
252
385
|
});
|
|
253
|
-
Object.defineProperty(this, "
|
|
386
|
+
Object.defineProperty(this, "estimateRFQOrder", {
|
|
387
|
+
enumerable: true,
|
|
388
|
+
configurable: true,
|
|
389
|
+
writable: true,
|
|
390
|
+
value: async ({ account, order, takerTokenPermit, }) => {
|
|
391
|
+
const { encodeResult } = order;
|
|
392
|
+
const { rfqTx } = encodeResult;
|
|
393
|
+
const { rfqOffer } = rfqTx;
|
|
394
|
+
const { value } = this.formatRFQOrderParams(order);
|
|
395
|
+
const gasEstimate = await this.publicClient.estimateContractGas({
|
|
396
|
+
address: rfqAddress[this.chainId],
|
|
397
|
+
abi: RFQ_ABI,
|
|
398
|
+
functionName: 'fillRFQ',
|
|
399
|
+
args: [
|
|
400
|
+
[
|
|
401
|
+
[
|
|
402
|
+
rfqOffer.taker,
|
|
403
|
+
rfqOffer.maker,
|
|
404
|
+
rfqOffer.takerToken,
|
|
405
|
+
rfqOffer.takerTokenAmount,
|
|
406
|
+
rfqOffer.makerToken,
|
|
407
|
+
rfqOffer.makerTokenAmount,
|
|
408
|
+
rfqOffer.feeFactor,
|
|
409
|
+
rfqOffer.flags,
|
|
410
|
+
rfqOffer.expiry,
|
|
411
|
+
rfqOffer.salt,
|
|
412
|
+
],
|
|
413
|
+
rfqTx.recipient,
|
|
414
|
+
rfqTx.takerRequestAmount,
|
|
415
|
+
],
|
|
416
|
+
encodeResult.makerSignature,
|
|
417
|
+
encodeResult.makerTokenPermit,
|
|
418
|
+
takerTokenPermit,
|
|
419
|
+
],
|
|
420
|
+
value,
|
|
421
|
+
account,
|
|
422
|
+
});
|
|
423
|
+
return gasEstimate;
|
|
424
|
+
}
|
|
425
|
+
});
|
|
426
|
+
Object.defineProperty(this, "estimateSOROrder", {
|
|
254
427
|
enumerable: true,
|
|
255
428
|
configurable: true,
|
|
256
429
|
writable: true,
|
|
257
|
-
value: async ({ account, order, slippage,
|
|
258
|
-
const
|
|
430
|
+
value: async ({ account, order, slippage, takerTokenPermit, }) => {
|
|
431
|
+
const prepare = await this.prepareOrder({
|
|
259
432
|
order,
|
|
260
433
|
slippage,
|
|
261
434
|
});
|
|
262
|
-
const
|
|
435
|
+
const { swapData, value } = prepare;
|
|
436
|
+
takerTokenPermit =
|
|
437
|
+
takerTokenPermit ?? this.getTokenPermitData(swapData.takerToken);
|
|
263
438
|
const gasEstimate = await this.publicClient.estimateContractGas({
|
|
264
439
|
address: genericSwapAddress[this.chainId],
|
|
265
440
|
abi: GENERIC_SWAP_ABI,
|
|
@@ -271,7 +446,82 @@ export class Swap extends ContextHost {
|
|
|
271
446
|
return gasEstimate;
|
|
272
447
|
}
|
|
273
448
|
});
|
|
274
|
-
Object.defineProperty(this, "
|
|
449
|
+
Object.defineProperty(this, "estimate", {
|
|
450
|
+
enumerable: true,
|
|
451
|
+
configurable: true,
|
|
452
|
+
writable: true,
|
|
453
|
+
value: async ({ account, order, slippage, takerTokenPermit, }) => {
|
|
454
|
+
if (order.orderType === OrderType.SOR) {
|
|
455
|
+
order = order;
|
|
456
|
+
const { swapData } = order.encodeResult;
|
|
457
|
+
takerTokenPermit =
|
|
458
|
+
takerTokenPermit ?? this.getTokenPermitData(swapData.takerToken);
|
|
459
|
+
return await this.estimateSOROrder({
|
|
460
|
+
account,
|
|
461
|
+
order: order,
|
|
462
|
+
slippage,
|
|
463
|
+
takerTokenPermit,
|
|
464
|
+
});
|
|
465
|
+
}
|
|
466
|
+
else if (order.orderType === OrderType.RFQV6) {
|
|
467
|
+
order = order;
|
|
468
|
+
const rfqOffer = order?.encodeResult?.rfqTx?.rfqOffer ?? {};
|
|
469
|
+
takerTokenPermit =
|
|
470
|
+
takerTokenPermit ?? this.getTokenPermitData(rfqOffer.takerToken);
|
|
471
|
+
return await this.estimateRFQOrder({
|
|
472
|
+
account,
|
|
473
|
+
order: order,
|
|
474
|
+
takerTokenPermit,
|
|
475
|
+
});
|
|
476
|
+
}
|
|
477
|
+
else {
|
|
478
|
+
throw new Error('unsupported order type');
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
});
|
|
482
|
+
Object.defineProperty(this, "signRFQOrder", {
|
|
483
|
+
enumerable: true,
|
|
484
|
+
configurable: true,
|
|
485
|
+
writable: true,
|
|
486
|
+
value: async (params) => {
|
|
487
|
+
const { account, value = 0n, txOverride, order, takerTokenPermit } = params;
|
|
488
|
+
const { encodeResult } = order;
|
|
489
|
+
const { rfqTx } = encodeResult;
|
|
490
|
+
const { rfqOffer } = rfqTx;
|
|
491
|
+
const txHash = await this.walletClient.writeContract({
|
|
492
|
+
address: rfqAddress[this.chainId],
|
|
493
|
+
abi: RFQ_ABI,
|
|
494
|
+
account,
|
|
495
|
+
functionName: 'fillRFQ',
|
|
496
|
+
args: [
|
|
497
|
+
[
|
|
498
|
+
[
|
|
499
|
+
rfqOffer.taker,
|
|
500
|
+
rfqOffer.maker,
|
|
501
|
+
rfqOffer.takerToken,
|
|
502
|
+
rfqOffer.takerTokenAmount,
|
|
503
|
+
rfqOffer.makerToken,
|
|
504
|
+
rfqOffer.makerTokenAmount,
|
|
505
|
+
rfqOffer.feeFactor,
|
|
506
|
+
rfqOffer.flags,
|
|
507
|
+
rfqOffer.expiry,
|
|
508
|
+
rfqOffer.salt,
|
|
509
|
+
],
|
|
510
|
+
rfqTx.recipient,
|
|
511
|
+
rfqTx.takerRequestAmount,
|
|
512
|
+
],
|
|
513
|
+
encodeResult.makerSignature,
|
|
514
|
+
encodeResult.makerTokenPermit,
|
|
515
|
+
takerTokenPermit,
|
|
516
|
+
],
|
|
517
|
+
value,
|
|
518
|
+
chain: this.walletClient.chain,
|
|
519
|
+
...txOverride,
|
|
520
|
+
});
|
|
521
|
+
return txHash;
|
|
522
|
+
}
|
|
523
|
+
});
|
|
524
|
+
Object.defineProperty(this, "signSOROrder", {
|
|
275
525
|
enumerable: true,
|
|
276
526
|
configurable: true,
|
|
277
527
|
writable: true,
|
|
@@ -290,6 +540,58 @@ export class Swap extends ContextHost {
|
|
|
290
540
|
return txHash;
|
|
291
541
|
}
|
|
292
542
|
});
|
|
543
|
+
Object.defineProperty(this, "sign", {
|
|
544
|
+
enumerable: true,
|
|
545
|
+
configurable: true,
|
|
546
|
+
writable: true,
|
|
547
|
+
value: async (params) => {
|
|
548
|
+
let { account, order, slippage, takerTokenPermit, txOverride, sendByRelayer, } = params;
|
|
549
|
+
const gas = txOverride?.gas ??
|
|
550
|
+
(await this.estimate({
|
|
551
|
+
account,
|
|
552
|
+
order,
|
|
553
|
+
slippage,
|
|
554
|
+
}));
|
|
555
|
+
const gasPrice = txOverride?.gasPrice ?? (await this.publicClient.getGasPrice());
|
|
556
|
+
const gasFee = (gas * gasPrice * 120n) / 100n;
|
|
557
|
+
txOverride = txOverride;
|
|
558
|
+
if (order.orderType === OrderType.SOR) {
|
|
559
|
+
const prepare = await this.prepareOrder({
|
|
560
|
+
order,
|
|
561
|
+
slippage,
|
|
562
|
+
gasFee,
|
|
563
|
+
sendByRelayer,
|
|
564
|
+
});
|
|
565
|
+
const { swapData, value } = prepare;
|
|
566
|
+
takerTokenPermit =
|
|
567
|
+
takerTokenPermit ?? this.getTokenPermitData(swapData.takerToken);
|
|
568
|
+
return await this.signSOROrder({
|
|
569
|
+
account,
|
|
570
|
+
value,
|
|
571
|
+
takerTokenPermit,
|
|
572
|
+
swapData,
|
|
573
|
+
txOverride,
|
|
574
|
+
});
|
|
575
|
+
}
|
|
576
|
+
else if (order.orderType === OrderType.RFQV6) {
|
|
577
|
+
order = order;
|
|
578
|
+
const rfqOffer = order?.encodeResult?.rfqTx?.rfqOffer ?? {};
|
|
579
|
+
takerTokenPermit =
|
|
580
|
+
takerTokenPermit ?? this.getTokenPermitData(rfqOffer.takerToken);
|
|
581
|
+
const { value } = this.formatRFQOrderParams(order);
|
|
582
|
+
return await this.signRFQOrder({
|
|
583
|
+
account,
|
|
584
|
+
order,
|
|
585
|
+
takerTokenPermit,
|
|
586
|
+
value: value,
|
|
587
|
+
txOverride,
|
|
588
|
+
});
|
|
589
|
+
}
|
|
590
|
+
else {
|
|
591
|
+
throw new Error('unsupported order type');
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
});
|
|
293
595
|
Object.defineProperty(this, "signWithReceipt", {
|
|
294
596
|
enumerable: true,
|
|
295
597
|
configurable: true,
|
|
@@ -302,7 +604,7 @@ export class Swap extends ContextHost {
|
|
|
302
604
|
return receipt;
|
|
303
605
|
}
|
|
304
606
|
});
|
|
305
|
-
Object.defineProperty(this, "
|
|
607
|
+
Object.defineProperty(this, "sign712SOROrder", {
|
|
306
608
|
enumerable: true,
|
|
307
609
|
configurable: true,
|
|
308
610
|
writable: true,
|
|
@@ -313,28 +615,136 @@ export class Swap extends ContextHost {
|
|
|
313
615
|
chainId: this.chainId,
|
|
314
616
|
verifyingContract: genericSwapAddress[this.chainId],
|
|
315
617
|
};
|
|
316
|
-
|
|
618
|
+
return this.walletClient.signTypedData({
|
|
619
|
+
primaryType: 'GenericSwapData',
|
|
317
620
|
domain: EIP712_DOMAIN,
|
|
318
621
|
types: GS_EIP712_TYPES,
|
|
319
|
-
|
|
622
|
+
message: swapData,
|
|
623
|
+
account,
|
|
624
|
+
});
|
|
625
|
+
}
|
|
626
|
+
});
|
|
627
|
+
Object.defineProperty(this, "sign712RFQOrder", {
|
|
628
|
+
enumerable: true,
|
|
629
|
+
configurable: true,
|
|
630
|
+
writable: true,
|
|
631
|
+
value: async (rfqTx, account) => {
|
|
632
|
+
const EIP712_DOMAIN = {
|
|
633
|
+
name: 'Tokenlon',
|
|
634
|
+
version: 'v6',
|
|
635
|
+
chainId: this.chainId,
|
|
636
|
+
verifyingContract: rfqAddress[this.chainId],
|
|
637
|
+
};
|
|
638
|
+
const types = {
|
|
639
|
+
RFQOffer: [
|
|
640
|
+
{ name: 'taker', type: 'address' },
|
|
641
|
+
{ name: 'maker', type: 'address' },
|
|
642
|
+
{ name: 'takerToken', type: 'address' },
|
|
643
|
+
{ name: 'takerTokenAmount', type: 'uint256' },
|
|
644
|
+
{ name: 'makerToken', type: 'address' },
|
|
645
|
+
{ name: 'makerTokenAmount', type: 'uint256' },
|
|
646
|
+
{ name: 'feeFactor', type: 'uint256' },
|
|
647
|
+
{ name: 'flags', type: 'uint256' },
|
|
648
|
+
{ name: 'expiry', type: 'uint256' },
|
|
649
|
+
{ name: 'salt', type: 'uint256' },
|
|
650
|
+
],
|
|
651
|
+
RFQTx: [
|
|
652
|
+
{ name: 'rfqOffer', type: 'RFQOffer' },
|
|
653
|
+
{ name: 'recipient', type: 'address' },
|
|
654
|
+
{ name: 'takerRequestAmount', type: 'uint256' },
|
|
655
|
+
],
|
|
320
656
|
};
|
|
321
657
|
return this.walletClient.signTypedData({
|
|
322
|
-
|
|
323
|
-
|
|
658
|
+
primaryType: 'RFQTx',
|
|
659
|
+
domain: EIP712_DOMAIN,
|
|
660
|
+
types: types,
|
|
661
|
+
message: rfqTx,
|
|
324
662
|
account,
|
|
325
663
|
});
|
|
326
664
|
}
|
|
327
665
|
});
|
|
666
|
+
Object.defineProperty(this, "signTypedData", {
|
|
667
|
+
enumerable: true,
|
|
668
|
+
configurable: true,
|
|
669
|
+
writable: true,
|
|
670
|
+
value: async (params) => {
|
|
671
|
+
let { account, order, takerTokenPermit, prepare } = params;
|
|
672
|
+
if (order.orderType === OrderType.SOR) {
|
|
673
|
+
if (!prepare) {
|
|
674
|
+
throw new Error('prepare is empty');
|
|
675
|
+
}
|
|
676
|
+
const { swapData } = prepare;
|
|
677
|
+
takerTokenPermit =
|
|
678
|
+
takerTokenPermit ?? this.getTokenPermitData(swapData.takerToken);
|
|
679
|
+
return this.sign712SOROrder(swapData, account);
|
|
680
|
+
}
|
|
681
|
+
else if (order.orderType === OrderType.RFQV6) {
|
|
682
|
+
const rfqOrder = order;
|
|
683
|
+
const { encodeResult } = rfqOrder;
|
|
684
|
+
const { rfqTx } = encodeResult;
|
|
685
|
+
const rfqOffer = rfqTx.rfqOffer;
|
|
686
|
+
takerTokenPermit =
|
|
687
|
+
takerTokenPermit ?? this.getTokenPermitData(rfqOffer.takerToken);
|
|
688
|
+
return this.sign712RFQOrder(rfqTx, account);
|
|
689
|
+
}
|
|
690
|
+
else {
|
|
691
|
+
throw new Error('unsupported order type');
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
});
|
|
328
695
|
Object.defineProperty(this, "submit", {
|
|
329
696
|
enumerable: true,
|
|
330
697
|
configurable: true,
|
|
331
698
|
writable: true,
|
|
332
|
-
value: async (
|
|
333
|
-
|
|
699
|
+
value: async (params) => {
|
|
700
|
+
let { order, slippage, takerTokenPermit, gasFee, sendByRelayer, orderSignature, } = params;
|
|
701
|
+
const request = {
|
|
702
|
+
uniqueId: order.uniqueId,
|
|
703
|
+
slippage: slippage,
|
|
704
|
+
permit: takerTokenPermit,
|
|
705
|
+
fee: {
|
|
706
|
+
protocolFee: 0n,
|
|
707
|
+
gasFee: 0n,
|
|
708
|
+
},
|
|
709
|
+
};
|
|
710
|
+
if (orderSignature) {
|
|
711
|
+
request.orderSignature = orderSignature;
|
|
712
|
+
}
|
|
713
|
+
if (order.orderType === OrderType.SOR) {
|
|
714
|
+
const prepare = await this.prepareOrder({
|
|
715
|
+
order,
|
|
716
|
+
slippage,
|
|
717
|
+
gasFee: gasFee,
|
|
718
|
+
sendByRelayer,
|
|
719
|
+
});
|
|
720
|
+
const { swapData, protocolFee, gasFeeTokenAmount } = prepare;
|
|
721
|
+
request.strategyData = swapData.strategyData;
|
|
722
|
+
request.fee = {
|
|
723
|
+
protocolFee: protocolFee,
|
|
724
|
+
gasFee: gasFeeTokenAmount,
|
|
725
|
+
};
|
|
726
|
+
request.strategyData = swapData.strategyData;
|
|
727
|
+
request.minAmountOut = swapData.minMakerTokenAmount;
|
|
728
|
+
}
|
|
729
|
+
else if (order.orderType === OrderType.RFQV6) {
|
|
730
|
+
order = order;
|
|
731
|
+
const rfqOrderParams = this.formatRFQOrderParams(order);
|
|
732
|
+
const { fee, minMakerTokenAmount } = rfqOrderParams;
|
|
733
|
+
request.slippage = 0;
|
|
734
|
+
request.minAmountOut = minMakerTokenAmount;
|
|
735
|
+
request.fee = {
|
|
736
|
+
gasFee: 0n,
|
|
737
|
+
protocolFee: fee,
|
|
738
|
+
};
|
|
739
|
+
}
|
|
740
|
+
else {
|
|
741
|
+
throw new Error('unsupported order type');
|
|
742
|
+
}
|
|
743
|
+
return http(`${this.host(httpServiceHost)}/${apiVersion}/swap/submit/${this.chainId}`, {
|
|
334
744
|
fetchOptions: {
|
|
335
745
|
method: 'POST',
|
|
336
746
|
},
|
|
337
|
-
body:
|
|
747
|
+
body: request,
|
|
338
748
|
});
|
|
339
749
|
}
|
|
340
750
|
});
|
|
@@ -357,7 +767,7 @@ export class Swap extends ContextHost {
|
|
|
357
767
|
const params = new URLSearchParams({
|
|
358
768
|
...dto,
|
|
359
769
|
});
|
|
360
|
-
return http(`${this.host(httpServiceHost)}/
|
|
770
|
+
return http(`${this.host(httpServiceHost)}/${apiVersion}/swap/status/${this.chainId}?${params}`);
|
|
361
771
|
}
|
|
362
772
|
});
|
|
363
773
|
Object.defineProperty(this, "config", {
|
|
@@ -365,7 +775,7 @@ export class Swap extends ContextHost {
|
|
|
365
775
|
configurable: true,
|
|
366
776
|
writable: true,
|
|
367
777
|
value: async (appVersion) => {
|
|
368
|
-
return http(`${this.host(httpServiceHost)}/
|
|
778
|
+
return http(`${this.host(httpServiceHost)}/${apiVersion}/swap/config?appVersion=${appVersion}`);
|
|
369
779
|
}
|
|
370
780
|
});
|
|
371
781
|
this.walletClient = injected.walletClient;
|