@paraswap/dex-lib 3.8.38 → 3.8.39-1701-1507-1721
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/build/abi/USDM.json +807 -0
- package/build/abi/bebop/BebopSettlement.abi.json +2137 -0
- package/build/abi/wUSDM.json +757 -0
- package/build/config.js +16 -9
- package/build/config.js.map +1 -1
- package/build/dex/aave-v2/aave-v2.d.ts +1 -0
- package/build/dex/aave-v2/aave-v2.js +1 -0
- package/build/dex/aave-v2/aave-v2.js.map +1 -1
- package/build/dex/aave-v3/aave-v3.d.ts +1 -0
- package/build/dex/aave-v3/aave-v3.js +1 -0
- package/build/dex/aave-v3/aave-v3.js.map +1 -1
- package/build/dex/aave-v3-stata/aave-v3-stata.d.ts +1 -0
- package/build/dex/aave-v3-stata/aave-v3-stata.js +1 -0
- package/build/dex/aave-v3-stata/aave-v3-stata.js.map +1 -1
- package/build/dex/bancor/bancor.d.ts +1 -0
- package/build/dex/bancor/bancor.js +1 -0
- package/build/dex/bancor/bancor.js.map +1 -1
- package/build/dex/bebop/bebop.d.ts +55 -0
- package/build/dex/bebop/bebop.js +524 -0
- package/build/dex/bebop/bebop.js.map +1 -0
- package/build/dex/bebop/config.d.ts +3 -0
- package/build/dex/bebop/config.js +38 -0
- package/build/dex/bebop/config.js.map +1 -0
- package/build/dex/bebop/constants.d.ts +9 -0
- package/build/dex/bebop/constants.js +13 -0
- package/build/dex/bebop/constants.js.map +1 -0
- package/build/dex/bebop/rate-fetcher.d.ts +22 -0
- package/build/dex/bebop/rate-fetcher.js +80 -0
- package/build/dex/bebop/rate-fetcher.js.map +1 -0
- package/build/dex/bebop/types.d.ts +76 -0
- package/build/dex/bebop/types.js +3 -0
- package/build/dex/bebop/types.js.map +1 -0
- package/build/dex/bebop/validators.d.ts +4 -0
- package/build/dex/bebop/validators.js +42 -0
- package/build/dex/bebop/validators.js.map +1 -0
- package/build/dex/bebop/websocket-fetcher.d.ts +35 -0
- package/build/dex/bebop/websocket-fetcher.js +87 -0
- package/build/dex/bebop/websocket-fetcher.js.map +1 -0
- package/build/dex/compound/compound.d.ts +1 -0
- package/build/dex/compound/compound.js +1 -0
- package/build/dex/compound/compound.js.map +1 -1
- package/build/dex/curve-v1/curve-v1.d.ts +1 -0
- package/build/dex/curve-v1/curve-v1.js +1 -0
- package/build/dex/curve-v1/curve-v1.js.map +1 -1
- package/build/dex/dodo-v1/dodo-v1.d.ts +1 -0
- package/build/dex/dodo-v1/dodo-v1.js +1 -0
- package/build/dex/dodo-v1/dodo-v1.js.map +1 -1
- package/build/dex/hashflow/hashflow.d.ts +6 -4
- package/build/dex/hashflow/hashflow.js +183 -29
- package/build/dex/hashflow/hashflow.js.map +1 -1
- package/build/dex/hashflow/types.d.ts +2 -0
- package/build/dex/hashflow/types.js.map +1 -1
- package/build/dex/idex.d.ts +8 -1
- package/build/dex/index.js +2 -0
- package/build/dex/index.js.map +1 -1
- package/build/dex/jarvis-v6/jarvis-v6.d.ts +1 -0
- package/build/dex/jarvis-v6/jarvis-v6.js +1 -0
- package/build/dex/jarvis-v6/jarvis-v6.js.map +1 -1
- package/build/dex/lido/lido.d.ts +1 -0
- package/build/dex/lido/lido.js +1 -0
- package/build/dex/lido/lido.js.map +1 -1
- package/build/dex/lite-psm/lite-psm.d.ts +1 -0
- package/build/dex/lite-psm/lite-psm.js +1 -0
- package/build/dex/lite-psm/lite-psm.js.map +1 -1
- package/build/dex/maker-psm/maker-psm.d.ts +1 -0
- package/build/dex/maker-psm/maker-psm.js +1 -0
- package/build/dex/maker-psm/maker-psm.js.map +1 -1
- package/build/dex/nerve/nerve.d.ts +1 -0
- package/build/dex/nerve/nerve.js +1 -0
- package/build/dex/nerve/nerve.js.map +1 -1
- package/build/dex/polygon-migrator/polygon-migrator.d.ts +1 -0
- package/build/dex/polygon-migrator/polygon-migrator.js +1 -0
- package/build/dex/polygon-migrator/polygon-migrator.js.map +1 -1
- package/build/dex/simple-exchange.d.ts +3 -1
- package/build/dex/simple-exchange.js +1 -0
- package/build/dex/simple-exchange.js.map +1 -1
- package/build/dex/smoothy/smoothy.d.ts +1 -0
- package/build/dex/smoothy/smoothy.js +1 -0
- package/build/dex/smoothy/smoothy.js.map +1 -1
- package/build/dex/spark/config.js +8 -0
- package/build/dex/spark/config.js.map +1 -1
- package/build/dex/spark/spark.d.ts +4 -1
- package/build/dex/spark/spark.js +20 -15
- package/build/dex/spark/spark.js.map +1 -1
- package/build/dex/spark/types.d.ts +9 -0
- package/build/dex/spark/types.js +8 -1
- package/build/dex/spark/types.js.map +1 -1
- package/build/dex/stable-pool/stable-pool.d.ts +3 -2
- package/build/dex/stable-pool/stable-pool.js +2 -1
- package/build/dex/stable-pool/stable-pool.js.map +1 -1
- package/build/dex/swaap-v2/swaap-v2.d.ts +5 -3
- package/build/dex/swaap-v2/swaap-v2.js +112 -21
- package/build/dex/swaap-v2/swaap-v2.js.map +1 -1
- package/build/dex/swaap-v2/types.d.ts +2 -0
- package/build/dex/swaap-v2/types.js.map +1 -1
- package/build/dex/trader-joe-v2.1/base.d.ts +1 -0
- package/build/dex/trader-joe-v2.1/base.js +1 -0
- package/build/dex/trader-joe-v2.1/base.js.map +1 -1
- package/build/dex/weth/weth.d.ts +1 -0
- package/build/dex/weth/weth.js +1 -0
- package/build/dex/weth/weth.js.map +1 -1
- package/build/dex/woo-fi-v2/woo-fi-v2-pool.d.ts +2 -2
- package/build/dex/woo-fi-v2/woo-fi-v2-pool.js +30 -9
- package/build/dex/woo-fi-v2/woo-fi-v2-pool.js.map +1 -1
- package/build/dex/woo-fi-v2/woo-fi-v2.js +1 -1
- package/build/dex/woo-fi-v2/woo-fi-v2.js.map +1 -1
- package/build/dex/wusdm/config.d.ts +3 -0
- package/build/dex/wusdm/config.js +29 -0
- package/build/dex/wusdm/config.js.map +1 -0
- package/build/dex/wusdm/types.d.ts +10 -0
- package/build/dex/wusdm/types.js +3 -0
- package/build/dex/wusdm/types.js.map +1 -0
- package/build/dex/wusdm/wusdm.d.ts +39 -0
- package/build/dex/wusdm/wusdm.js +212 -0
- package/build/dex/wusdm/wusdm.js.map +1 -0
- package/build/executor/Executor01BytecodeBuilder.d.ts +3 -3
- package/build/executor/Executor01BytecodeBuilder.js +65 -16
- package/build/executor/Executor01BytecodeBuilder.js.map +1 -1
- package/build/executor/Executor02BytecodeBuilder.d.ts +4 -4
- package/build/executor/Executor02BytecodeBuilder.js +65 -18
- package/build/executor/Executor02BytecodeBuilder.js.map +1 -1
- package/build/executor/Executor03BytecodeBuilder.d.ts +2 -2
- package/build/executor/Executor03BytecodeBuilder.js +52 -8
- package/build/executor/Executor03BytecodeBuilder.js.map +1 -1
- package/build/executor/ExecutorBytecodeBuilder.d.ts +5 -4
- package/build/executor/ExecutorBytecodeBuilder.js +4 -4
- package/build/executor/ExecutorBytecodeBuilder.js.map +1 -1
- package/build/executor/ExecutorDetector.js +2 -1
- package/build/executor/ExecutorDetector.js.map +1 -1
- package/build/executor/types.d.ts +1 -0
- package/build/executor/types.js +1 -0
- package/build/executor/types.js.map +1 -1
- package/build/generic-swap-transaction-builder.d.ts +2 -0
- package/build/generic-swap-transaction-builder.js +49 -11
- package/build/generic-swap-transaction-builder.js.map +1 -1
- package/build/implementations/local-paraswap-sdk.js +3 -1
- package/build/implementations/local-paraswap-sdk.js.map +1 -1
- package/build/lib/fetcher/wsFetcher.d.ts +35 -0
- package/build/lib/fetcher/wsFetcher.js +87 -0
- package/build/lib/fetcher/wsFetcher.js.map +1 -0
- package/build/router/simpleswap.js +51 -13
- package/build/router/simpleswap.js.map +1 -1
- package/build/types.d.ts +2 -0
- package/package.json +4 -2
- package/src/abi/bebop/BebopSettlement.abi.json +2137 -0
- package/src/config.ts +17 -9
- package/src/dex/aave-v2/aave-v2.ts +2 -0
- package/src/dex/aave-v3/aave-v3.ts +2 -0
- package/src/dex/aave-v3-stata/aave-v3-stata.ts +2 -0
- package/src/dex/bancor/bancor.ts +2 -0
- package/src/dex/bebop/bebop-e2e.test.ts +195 -0
- package/src/dex/bebop/bebop-integration.test.ts +170 -0
- package/src/dex/bebop/bebop.ts +732 -0
- package/src/dex/bebop/config.ts +37 -0
- package/src/dex/bebop/constants.ts +9 -0
- package/src/dex/bebop/rate-fetcher.ts +138 -0
- package/src/dex/bebop/types.ts +81 -0
- package/src/dex/bebop/validators.ts +43 -0
- package/src/dex/compound/compound.ts +2 -0
- package/src/dex/curve-v1/curve-v1.ts +2 -0
- package/src/dex/dodo-v1/dodo-v1.ts +2 -0
- package/src/dex/hashflow/hashflow-e2e.test.ts +281 -28
- package/src/dex/hashflow/hashflow.ts +285 -54
- package/src/dex/hashflow/types.ts +2 -0
- package/src/dex/idex.ts +18 -1
- package/src/dex/index.ts +2 -0
- package/src/dex/jarvis-v6/jarvis-v6.ts +2 -0
- package/src/dex/lido/lido.ts +2 -0
- package/src/dex/lite-psm/lite-psm.ts +2 -0
- package/src/dex/maker-psm/maker-psm.ts +2 -0
- package/src/dex/nerve/nerve.ts +2 -0
- package/src/dex/polygon-migrator/polygon-migrator.ts +2 -0
- package/src/dex/simple-exchange.ts +4 -2
- package/src/dex/smoothy/smoothy.ts +2 -0
- package/src/dex/spark/config.ts +8 -1
- package/src/dex/spark/spark.ts +27 -18
- package/src/dex/spark/types.ts +10 -0
- package/src/dex/stable-pool/stable-pool.ts +4 -1
- package/src/dex/swaap-v2/swaap-v2-e2e.test.ts +13 -13
- package/src/dex/swaap-v2/swaap-v2.ts +217 -32
- package/src/dex/swaap-v2/types.ts +2 -0
- package/src/dex/trader-joe-v2.1/base.ts +2 -0
- package/src/dex/weth/weth.ts +2 -0
- package/src/dex/woo-fi-v2/woo-fi-v2-pool.ts +58 -27
- package/src/dex/woo-fi-v2/woo-fi-v2.ts +1 -2
- package/src/executor/Executor01BytecodeBuilder.ts +105 -11
- package/src/executor/Executor02BytecodeBuilder.ts +95 -8
- package/src/executor/Executor03BytecodeBuilder.ts +89 -5
- package/src/executor/ExecutorBytecodeBuilder.ts +6 -0
- package/src/executor/ExecutorDetector.ts +2 -1
- package/src/executor/types.ts +1 -0
- package/src/generic-swap-transaction-builder.ts +96 -24
- package/src/implementations/local-paraswap-sdk.ts +21 -10
- package/src/lib/fetcher/wsFetcher.ts +126 -0
- package/src/router/simpleswap.ts +94 -18
- package/src/types.ts +2 -0
- package/tests/constants-e2e.ts +2 -2
- package/tests/utils-e2e.ts +308 -2
|
@@ -15,7 +15,6 @@ import {
|
|
|
15
15
|
ExecutorBytecodeBuilder,
|
|
16
16
|
SingleSwapCallDataParams,
|
|
17
17
|
} from './ExecutorBytecodeBuilder';
|
|
18
|
-
import { assert } from 'ts-essentials';
|
|
19
18
|
|
|
20
19
|
const {
|
|
21
20
|
utils: { hexlify, hexDataLength, hexConcat, hexZeroPad, solidityPack },
|
|
@@ -59,6 +58,7 @@ export class Executor03BytecodeBuilder extends ExecutorBytecodeBuilder<
|
|
|
59
58
|
swapExchangeIndex: number,
|
|
60
59
|
exchangeParamIndex: number,
|
|
61
60
|
maybeWethCallData?: DepositWithdrawReturn,
|
|
61
|
+
maybeWethCallDataForNeedUnwrapWeth?: DepositWithdrawReturn,
|
|
62
62
|
): { dexFlag: Flag; approveFlag: Flag } {
|
|
63
63
|
const { srcToken, destToken } = priceRoute.bestRoute[0].swaps[0];
|
|
64
64
|
const isEthSrc = isETHAddress(srcToken);
|
|
@@ -68,8 +68,8 @@ export class Executor03BytecodeBuilder extends ExecutorBytecodeBuilder<
|
|
|
68
68
|
const swap = priceRoute.bestRoute[0].swaps[0];
|
|
69
69
|
|
|
70
70
|
const {
|
|
71
|
-
dexFuncHasRecipient,
|
|
72
71
|
needWrapNative,
|
|
72
|
+
needUnwrapWeth,
|
|
73
73
|
swappedAmountNotPresentInExchangeData,
|
|
74
74
|
specialDexFlag,
|
|
75
75
|
specialDexSupportsInsertFromAmount,
|
|
@@ -77,6 +77,11 @@ export class Executor03BytecodeBuilder extends ExecutorBytecodeBuilder<
|
|
|
77
77
|
const isSpecialDex =
|
|
78
78
|
specialDexFlag !== undefined && specialDexFlag !== SpecialDex.DEFAULT;
|
|
79
79
|
|
|
80
|
+
const wethAddr =
|
|
81
|
+
this.dexHelper.config.data.wrappedNativeTokenAddress.toLowerCase();
|
|
82
|
+
const isWethSrc = srcToken.toLowerCase() === wethAddr;
|
|
83
|
+
const isWethDest = destToken.toLowerCase() === wethAddr;
|
|
84
|
+
|
|
80
85
|
const forcePreventInsertFromAmount =
|
|
81
86
|
swappedAmountNotPresentInExchangeData ||
|
|
82
87
|
(isSpecialDex && !specialDexSupportsInsertFromAmount);
|
|
@@ -85,6 +90,16 @@ export class Executor03BytecodeBuilder extends ExecutorBytecodeBuilder<
|
|
|
85
90
|
const needUnwrap =
|
|
86
91
|
needWrapNative && isEthDest && maybeWethCallData?.withdraw;
|
|
87
92
|
|
|
93
|
+
const needUnwrapForWethSrc =
|
|
94
|
+
needUnwrapWeth &&
|
|
95
|
+
isWethSrc &&
|
|
96
|
+
maybeWethCallDataForNeedUnwrapWeth?.withdraw;
|
|
97
|
+
|
|
98
|
+
const needWrapForWethDest =
|
|
99
|
+
needUnwrapWeth &&
|
|
100
|
+
isWethDest &&
|
|
101
|
+
maybeWethCallDataForNeedUnwrapWeth?.deposit;
|
|
102
|
+
|
|
88
103
|
let dexFlag = forcePreventInsertFromAmount
|
|
89
104
|
? Flag.DONT_INSERT_FROM_AMOUNT_DONT_CHECK_BALANCE_AFTER_SWAP
|
|
90
105
|
: Flag.INSERT_FROM_AMOUNT_DONT_CHECK_BALANCE_AFTER_SWAP; // 0 or 3
|
|
@@ -95,7 +110,12 @@ export class Executor03BytecodeBuilder extends ExecutorBytecodeBuilder<
|
|
|
95
110
|
if (isEthSrc && !needWrap) {
|
|
96
111
|
dexFlag =
|
|
97
112
|
Flag.SEND_ETH_EQUAL_TO_FROM_AMOUNT_CHECK_SRC_TOKEN_BALANCE_AFTER_SWAP; // 5
|
|
98
|
-
} else if (
|
|
113
|
+
} else if (isWethSrc && needUnwrapForWethSrc) {
|
|
114
|
+
dexFlag = Flag.SEND_ETH_EQUAL_TO_FROM_AMOUNT_CHECK_ETH_BALANCE_AFTER_SWAP; // 13
|
|
115
|
+
} else if (
|
|
116
|
+
(isEthDest && !needUnwrap) ||
|
|
117
|
+
(isWethDest && needWrapForWethDest)
|
|
118
|
+
) {
|
|
99
119
|
dexFlag = forcePreventInsertFromAmount
|
|
100
120
|
? Flag.DONT_INSERT_FROM_AMOUNT_CHECK_ETH_BALANCE_AFTER_SWAP // 4
|
|
101
121
|
: Flag.INSERT_FROM_AMOUNT_CHECK_ETH_BALANCE_AFTER_SWAP; // 7
|
|
@@ -103,7 +123,6 @@ export class Executor03BytecodeBuilder extends ExecutorBytecodeBuilder<
|
|
|
103
123
|
dexFlag = forcePreventInsertFromAmount
|
|
104
124
|
? Flag.DONT_INSERT_FROM_AMOUNT_CHECK_SRC_TOKEN_BALANCE_AFTER_SWAP // 8
|
|
105
125
|
: Flag.INSERT_FROM_AMOUNT_CHECK_SRC_TOKEN_BALANCE_AFTER_SWAP; // 11
|
|
106
|
-
// dexFlag = Flag.ZERO;
|
|
107
126
|
} else if (
|
|
108
127
|
!isETHAddress(swap.destToken) &&
|
|
109
128
|
exchangeParams.some(param => !param.dexFuncHasRecipient)
|
|
@@ -143,6 +162,7 @@ export class Executor03BytecodeBuilder extends ExecutorBytecodeBuilder<
|
|
|
143
162
|
index,
|
|
144
163
|
flags,
|
|
145
164
|
maybeWethCallData,
|
|
165
|
+
maybeWethCallDataForNeedUnwrapWeth,
|
|
146
166
|
swap,
|
|
147
167
|
} = params;
|
|
148
168
|
if (!swap) throw new Error('Swap is not provided for single swap calldata');
|
|
@@ -212,8 +232,32 @@ export class Executor03BytecodeBuilder extends ExecutorBytecodeBuilder<
|
|
|
212
232
|
]);
|
|
213
233
|
}
|
|
214
234
|
|
|
215
|
-
|
|
235
|
+
if (
|
|
236
|
+
maybeWethCallDataForNeedUnwrapWeth?.withdraw &&
|
|
237
|
+
swap.srcToken.toLowerCase() ===
|
|
238
|
+
this.dexHelper.config.data.wrappedNativeTokenAddress.toLowerCase() &&
|
|
239
|
+
curExchangeParam.needUnwrapWeth
|
|
240
|
+
) {
|
|
241
|
+
const withdrawCallData = this.buildUnwrapEthCallData(
|
|
242
|
+
this.getWETHAddress(curExchangeParam),
|
|
243
|
+
maybeWethCallDataForNeedUnwrapWeth.withdraw.calldata,
|
|
244
|
+
);
|
|
245
|
+
|
|
246
|
+
swapCallData = hexConcat([withdrawCallData, swapCallData]);
|
|
247
|
+
|
|
248
|
+
if (maybeWethCallDataForNeedUnwrapWeth.deposit) {
|
|
249
|
+
const depositCallData = this.buildWrapEthCallData(
|
|
250
|
+
this.getWETHAddress(curExchangeParam),
|
|
251
|
+
maybeWethCallDataForNeedUnwrapWeth.deposit?.calldata,
|
|
252
|
+
Flag.SEND_ETH_EQUAL_TO_FROM_AMOUNT_DONT_CHECK_BALANCE_AFTER_SWAP, // 9
|
|
253
|
+
);
|
|
254
|
+
|
|
255
|
+
swapCallData = hexConcat([swapCallData, depositCallData]);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
216
259
|
if (index === exchangeParams.length - 1) {
|
|
260
|
+
// after the last path
|
|
217
261
|
// if some of dexes doesn't have recipient add one transfer in the end
|
|
218
262
|
if (
|
|
219
263
|
exchangeParams.some(param => !param.dexFuncHasRecipient) &&
|
|
@@ -240,6 +284,43 @@ export class Executor03BytecodeBuilder extends ExecutorBytecodeBuilder<
|
|
|
240
284
|
swapCallData = hexConcat([swapCallData, withdrawCallData]);
|
|
241
285
|
}
|
|
242
286
|
|
|
287
|
+
if (
|
|
288
|
+
maybeWethCallDataForNeedUnwrapWeth?.deposit &&
|
|
289
|
+
swap.destToken.toLowerCase() ===
|
|
290
|
+
this.dexHelper.config.data.wrappedNativeTokenAddress.toLowerCase()
|
|
291
|
+
) {
|
|
292
|
+
let approveWethCalldata = '0x';
|
|
293
|
+
if (curExchangeParam.approveData) {
|
|
294
|
+
approveWethCalldata = this.buildApproveCallData(
|
|
295
|
+
curExchangeParam.approveData.target,
|
|
296
|
+
curExchangeParam.approveData.token,
|
|
297
|
+
flags.approves[index],
|
|
298
|
+
);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
const depositCallData = this.buildWrapEthCallData(
|
|
302
|
+
this.getWETHAddress(curExchangeParam),
|
|
303
|
+
maybeWethCallDataForNeedUnwrapWeth.deposit.calldata,
|
|
304
|
+
Flag.SEND_ETH_EQUAL_TO_FROM_AMOUNT_DONT_CHECK_BALANCE_AFTER_SWAP, // 9
|
|
305
|
+
);
|
|
306
|
+
|
|
307
|
+
const transferCallData = this.buildTransferCallData(
|
|
308
|
+
this.erc20Interface.encodeFunctionData('transfer', [
|
|
309
|
+
this.dexHelper.config.data.augustusV6Address,
|
|
310
|
+
// insert 0 because it's still gonna be replaced with balance check result
|
|
311
|
+
'0',
|
|
312
|
+
]),
|
|
313
|
+
swap.destToken,
|
|
314
|
+
);
|
|
315
|
+
|
|
316
|
+
swapCallData = hexConcat([
|
|
317
|
+
swapCallData,
|
|
318
|
+
approveWethCalldata,
|
|
319
|
+
depositCallData,
|
|
320
|
+
transferCallData,
|
|
321
|
+
]);
|
|
322
|
+
}
|
|
323
|
+
|
|
243
324
|
// send ETH to augustus
|
|
244
325
|
if (
|
|
245
326
|
isETHAddress(swap.destToken) &&
|
|
@@ -362,6 +443,7 @@ export class Executor03BytecodeBuilder extends ExecutorBytecodeBuilder<
|
|
|
362
443
|
exchangeParams: DexExchangeBuildParam[],
|
|
363
444
|
sender: string,
|
|
364
445
|
maybeWethCallData?: DepositWithdrawReturn,
|
|
446
|
+
maybeWethCallDataForNeedUnwrapWeth?: DepositWithdrawReturn,
|
|
365
447
|
): string {
|
|
366
448
|
const swap = priceRoute.bestRoute[0].swaps[0];
|
|
367
449
|
|
|
@@ -384,6 +466,7 @@ export class Executor03BytecodeBuilder extends ExecutorBytecodeBuilder<
|
|
|
384
466
|
priceRoute,
|
|
385
467
|
orderedExchangeParams.map(e => e.exchangeParam),
|
|
386
468
|
maybeWethCallData,
|
|
469
|
+
maybeWethCallDataForNeedUnwrapWeth,
|
|
387
470
|
);
|
|
388
471
|
|
|
389
472
|
let swapsCalldata = orderedExchangeParams.reduce<string>(
|
|
@@ -397,6 +480,7 @@ export class Executor03BytecodeBuilder extends ExecutorBytecodeBuilder<
|
|
|
397
480
|
flags,
|
|
398
481
|
sender,
|
|
399
482
|
maybeWethCallData,
|
|
483
|
+
maybeWethCallDataForNeedUnwrapWeth,
|
|
400
484
|
swap: swapWithOrderedExchanges,
|
|
401
485
|
}),
|
|
402
486
|
]),
|
|
@@ -38,6 +38,7 @@ export type SingleSwapCallDataParams<T> = {
|
|
|
38
38
|
flags: { approves: Flag[]; dexes: Flag[]; wrap: Flag };
|
|
39
39
|
sender: string;
|
|
40
40
|
maybeWethCallData?: DepositWithdrawReturn;
|
|
41
|
+
maybeWethCallDataForNeedUnwrapWeth?: DepositWithdrawReturn;
|
|
41
42
|
} & T;
|
|
42
43
|
|
|
43
44
|
export type DexCallDataParams<T> = {
|
|
@@ -67,6 +68,7 @@ export abstract class ExecutorBytecodeBuilder<S = {}, D = {}> {
|
|
|
67
68
|
swapExchangeIndex: number,
|
|
68
69
|
exchangeParamIndex: number,
|
|
69
70
|
maybeWethCallData?: DepositWithdrawReturn,
|
|
71
|
+
maybeWethCallDataForNeedUnwrapWeth?: DepositWithdrawReturn,
|
|
70
72
|
): {
|
|
71
73
|
dexFlag: Flag;
|
|
72
74
|
approveFlag: Flag;
|
|
@@ -86,6 +88,7 @@ export abstract class ExecutorBytecodeBuilder<S = {}, D = {}> {
|
|
|
86
88
|
swapExchangeIndex: number,
|
|
87
89
|
exchangeParamIndex: number,
|
|
88
90
|
maybeWethCallData?: DepositWithdrawReturn,
|
|
91
|
+
maybeWethCallDataForNeedUnwrapWeth?: DepositWithdrawReturn,
|
|
89
92
|
): {
|
|
90
93
|
dexFlag: Flag;
|
|
91
94
|
approveFlag: Flag;
|
|
@@ -102,6 +105,7 @@ export abstract class ExecutorBytecodeBuilder<S = {}, D = {}> {
|
|
|
102
105
|
exchangeParams: DexExchangeBuildParam[],
|
|
103
106
|
sender: string,
|
|
104
107
|
maybeWethCallData?: DepositWithdrawReturn,
|
|
108
|
+
maybeWethCallDataForNeedUnwrapWeth?: DepositWithdrawReturn,
|
|
105
109
|
): string;
|
|
106
110
|
|
|
107
111
|
public abstract getAddress(): string;
|
|
@@ -297,6 +301,7 @@ export abstract class ExecutorBytecodeBuilder<S = {}, D = {}> {
|
|
|
297
301
|
priceRoute: OptimalRate,
|
|
298
302
|
exchangeParams: DexExchangeBuildParam[],
|
|
299
303
|
maybeWethCallData?: DepositWithdrawReturn,
|
|
304
|
+
maybeWethCallDataForNeedUnwrapWeth?: DepositWithdrawReturn,
|
|
300
305
|
): { approves: Flag[]; dexes: Flag[]; wrap: Flag } {
|
|
301
306
|
const isMegaSwap = priceRoute.bestRoute.length > 1;
|
|
302
307
|
const isMultiSwap = !isMegaSwap && priceRoute.bestRoute[0].swaps.length > 1;
|
|
@@ -323,6 +328,7 @@ export abstract class ExecutorBytecodeBuilder<S = {}, D = {}> {
|
|
|
323
328
|
swapExchangeIndex,
|
|
324
329
|
exchangeParamIndex,
|
|
325
330
|
maybeWethCallData,
|
|
331
|
+
maybeWethCallDataForNeedUnwrapWeth,
|
|
326
332
|
);
|
|
327
333
|
|
|
328
334
|
flags.dexes.push(dexFlag);
|
|
@@ -19,7 +19,8 @@ export class ExecutorDetector {
|
|
|
19
19
|
> = {
|
|
20
20
|
[SwapSide.SELL]: {
|
|
21
21
|
[RouteExecutionType.SINGLE_STEP]: Executors.ONE, // simpleSwap via Executor01
|
|
22
|
-
[RouteExecutionType.HORIZONTAL_SEQUENCE]: Executors.ONE, // multiSwap via Executor01
|
|
22
|
+
// [RouteExecutionType.HORIZONTAL_SEQUENCE]: Executors.ONE, // multiSwap via Executor01
|
|
23
|
+
[RouteExecutionType.HORIZONTAL_SEQUENCE]: Executors.TWO, // multiSwap via Executor02
|
|
23
24
|
[RouteExecutionType.VERTICAL_BRANCH]: Executors.TWO, // simpleSwap with percentage on a path via Executor02
|
|
24
25
|
[RouteExecutionType.VERTICAL_BRANCH_HORIZONTAL_SEQUENCE]: Executors.TWO, // multiSwap with percentages on paths via Executor02
|
|
25
26
|
// megaSwap via Executor02
|
package/src/executor/types.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export enum Flag {
|
|
2
2
|
SEND_ETH_EQUAL_TO_FROM_AMOUNT_PLUS_INSERT_FROM_AMOUNT_DONT_CHECK_BALANCE_AFTER_SWAP = 18, // // (flag 18 mod 4) = case 2: sendEth equal to fromAmount + insert fromAmount, (flag 18 mod 3) = case 0: don't check balance after swap
|
|
3
3
|
SEND_ETH_EQUAL_TO_FROM_AMOUNT_PLUS_INSERT_FROM_AMOUNT_CHECK_SRC_TOKEN_BALANCE_AFTER_SWAP = 14, // (flag 14 mod 4) = case 2: sendEth equal to fromAmount + insert fromAmount, (flag 14 mod 3) = case 2: case 2: check "srcToken" balance after swap
|
|
4
|
+
SEND_ETH_EQUAL_TO_FROM_AMOUNT_CHECK_ETH_BALANCE_AFTER_SWAP = 13, // (flag 13 mod 4) = case 1: sendEth equal to fromAmount, (flag 13 mod 3) = case 1: check eth balance after swap
|
|
4
5
|
INSERT_FROM_AMOUNT_CHECK_SRC_TOKEN_BALANCE_AFTER_SWAP = 11, // (flag 11 mod 4) = case 3: insert fromAmount, (flag 11 mod 3) = case 2: check "srcToken" balance after swap
|
|
5
6
|
SEND_ETH_EQUAL_TO_FROM_AMOUNT_DONT_CHECK_BALANCE_AFTER_SWAP = 9, // (flag 9 mod 4) = case 1: sendEth equal to fromAmount, (flag 9 mod 3) = case 0: don't check balance after swap
|
|
6
7
|
DONT_INSERT_FROM_AMOUNT_CHECK_SRC_TOKEN_BALANCE_AFTER_SWAP = 8, // (flag 8 mod 4) = case 0: don't insert fromAmount, (flag 8 mod 3) = case 2: check "srcToken" balance after swap
|
|
@@ -123,6 +123,8 @@ export class GenericSwapTransactionBuilder {
|
|
|
123
123
|
recipient,
|
|
124
124
|
wethDeposit,
|
|
125
125
|
wethWithdraw,
|
|
126
|
+
wethDepositForNeedUnwrapWeth,
|
|
127
|
+
wethWithdrawForNeedUnwrapWeth,
|
|
126
128
|
} = this.getDexCallsParams(
|
|
127
129
|
priceRoute,
|
|
128
130
|
routeIndex,
|
|
@@ -148,6 +150,9 @@ export class GenericSwapTransactionBuilder {
|
|
|
148
150
|
isGlobalDestToken:
|
|
149
151
|
priceRoute.destToken.toLowerCase() ===
|
|
150
152
|
destToken.toLowerCase(),
|
|
153
|
+
priceRoute: priceRoute,
|
|
154
|
+
swap: swap,
|
|
155
|
+
swapExchange: se,
|
|
151
156
|
},
|
|
152
157
|
executorAddress,
|
|
153
158
|
);
|
|
@@ -156,30 +161,47 @@ export class GenericSwapTransactionBuilder {
|
|
|
156
161
|
dexParams,
|
|
157
162
|
wethDeposit,
|
|
158
163
|
wethWithdraw,
|
|
164
|
+
wethDepositForNeedUnwrapWeth,
|
|
165
|
+
wethWithdrawForNeedUnwrapWeth,
|
|
159
166
|
};
|
|
160
167
|
}),
|
|
161
168
|
),
|
|
162
169
|
),
|
|
163
170
|
);
|
|
164
171
|
|
|
165
|
-
const {
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
172
|
+
const {
|
|
173
|
+
exchangeParams,
|
|
174
|
+
srcAmountWethToDeposit,
|
|
175
|
+
destAmountWethToWithdraw,
|
|
176
|
+
srcAmountWethToWithdrawForNeedUnwrapWeth,
|
|
177
|
+
destAmountWethToDepositForNeeUnwrapWeth,
|
|
178
|
+
} = await rawDexParams.reduce<{
|
|
179
|
+
exchangeParams: DexExchangeParam[];
|
|
180
|
+
srcAmountWethToDeposit: bigint;
|
|
181
|
+
destAmountWethToWithdraw: bigint;
|
|
182
|
+
srcAmountWethToWithdrawForNeedUnwrapWeth: bigint;
|
|
183
|
+
destAmountWethToDepositForNeeUnwrapWeth: bigint;
|
|
184
|
+
}>(
|
|
185
|
+
(acc, se) => {
|
|
186
|
+
acc.srcAmountWethToDeposit += BigInt(se.wethDeposit);
|
|
187
|
+
acc.destAmountWethToWithdraw += BigInt(se.wethWithdraw);
|
|
188
|
+
acc.srcAmountWethToWithdrawForNeedUnwrapWeth += BigInt(
|
|
189
|
+
se.wethWithdrawForNeedUnwrapWeth,
|
|
190
|
+
);
|
|
191
|
+
acc.destAmountWethToDepositForNeeUnwrapWeth += BigInt(
|
|
192
|
+
se.wethDepositForNeedUnwrapWeth,
|
|
193
|
+
);
|
|
194
|
+
acc.exchangeParams.push(se.dexParams);
|
|
195
|
+
return acc;
|
|
196
|
+
},
|
|
197
|
+
{
|
|
198
|
+
exchangeParams: [],
|
|
199
|
+
srcAmountWethToDeposit: 0n,
|
|
200
|
+
destAmountWethToWithdraw: 0n,
|
|
201
|
+
srcAmountWethToWithdrawForNeedUnwrapWeth: 0n,
|
|
202
|
+
destAmountWethToDepositForNeeUnwrapWeth: 0n,
|
|
203
|
+
},
|
|
204
|
+
);
|
|
183
205
|
|
|
184
206
|
const maybeWethCallData = this.getDepositWithdrawWethCallData(
|
|
185
207
|
srcAmountWethToDeposit,
|
|
@@ -187,11 +209,19 @@ export class GenericSwapTransactionBuilder {
|
|
|
187
209
|
side,
|
|
188
210
|
);
|
|
189
211
|
|
|
212
|
+
const maybeWethCallDataForNeedUnwrapWeth =
|
|
213
|
+
this.getDepositWithdrawWethCallData(
|
|
214
|
+
destAmountWethToDepositForNeeUnwrapWeth,
|
|
215
|
+
srcAmountWethToWithdrawForNeedUnwrapWeth,
|
|
216
|
+
side,
|
|
217
|
+
);
|
|
218
|
+
|
|
190
219
|
const buildExchangeParams = await this.addDexExchangeApproveParams(
|
|
191
220
|
bytecodeBuilder,
|
|
192
221
|
priceRoute,
|
|
193
222
|
exchangeParams,
|
|
194
223
|
maybeWethCallData,
|
|
224
|
+
maybeWethCallDataForNeedUnwrapWeth,
|
|
195
225
|
);
|
|
196
226
|
|
|
197
227
|
return bytecodeBuilder.buildByteCode(
|
|
@@ -199,6 +229,7 @@ export class GenericSwapTransactionBuilder {
|
|
|
199
229
|
buildExchangeParams,
|
|
200
230
|
userAddress,
|
|
201
231
|
maybeWethCallData,
|
|
232
|
+
maybeWethCallDataForNeedUnwrapWeth,
|
|
202
233
|
);
|
|
203
234
|
}
|
|
204
235
|
|
|
@@ -599,9 +630,11 @@ export class GenericSwapTransactionBuilder {
|
|
|
599
630
|
destAmount: string;
|
|
600
631
|
wethDeposit: bigint;
|
|
601
632
|
wethWithdraw: bigint;
|
|
633
|
+
wethDepositForNeedUnwrapWeth: bigint;
|
|
634
|
+
wethWithdrawForNeedUnwrapWeth: bigint;
|
|
602
635
|
} {
|
|
603
636
|
const wethAddress =
|
|
604
|
-
this.dexAdapterService.dexHelper.config.data.wrappedNativeTokenAddress;
|
|
637
|
+
this.dexAdapterService.dexHelper.config.data.wrappedNativeTokenAddress.toLowerCase();
|
|
605
638
|
|
|
606
639
|
const side = priceRoute.side;
|
|
607
640
|
|
|
@@ -617,6 +650,9 @@ export class GenericSwapTransactionBuilder {
|
|
|
617
650
|
|
|
618
651
|
let wethWithdraw = 0n;
|
|
619
652
|
|
|
653
|
+
let wethWithdrawForNeedUnwrapWeth = 0n;
|
|
654
|
+
let wethDepositForNeedUnwrapWeth = 0n;
|
|
655
|
+
|
|
620
656
|
// For case of buy apply slippage is applied to srcAmount in equal proportion as the complete swap
|
|
621
657
|
// This assumes that the sum of all swaps srcAmount would sum to priceRoute.srcAmount
|
|
622
658
|
// Also that it is a direct swap.
|
|
@@ -633,7 +669,19 @@ export class GenericSwapTransactionBuilder {
|
|
|
633
669
|
// should work if the final slippage check passes.
|
|
634
670
|
const _destAmount = side === SwapSide.SELL ? '1' : se.destAmount;
|
|
635
671
|
|
|
636
|
-
|
|
672
|
+
const dexNeedWrapNative =
|
|
673
|
+
typeof dex.needWrapNative === 'function'
|
|
674
|
+
? dex.needWrapNative(priceRoute, swap, se)
|
|
675
|
+
: dex.needWrapNative;
|
|
676
|
+
|
|
677
|
+
const dexNeedUnwrapWeth =
|
|
678
|
+
dex.needUnwrapWeth !== undefined
|
|
679
|
+
? typeof dex.needUnwrapWeth === 'function'
|
|
680
|
+
? dex.needUnwrapWeth(priceRoute, swap, se)
|
|
681
|
+
: dex.needUnwrapWeth
|
|
682
|
+
: false;
|
|
683
|
+
|
|
684
|
+
if (isETHAddress(swap.srcToken) && dexNeedWrapNative) {
|
|
637
685
|
_src = wethAddress;
|
|
638
686
|
wethDeposit = BigInt(_srcAmount);
|
|
639
687
|
}
|
|
@@ -641,27 +689,50 @@ export class GenericSwapTransactionBuilder {
|
|
|
641
689
|
const forceUnwrap =
|
|
642
690
|
isETHAddress(swap.destToken) &&
|
|
643
691
|
(isMultiSwap || isMegaSwap) &&
|
|
644
|
-
!
|
|
692
|
+
!dexNeedWrapNative &&
|
|
645
693
|
!isLastSwap;
|
|
646
694
|
|
|
647
|
-
if ((isETHAddress(swap.destToken) &&
|
|
648
|
-
_dest = forceUnwrap && !
|
|
695
|
+
if ((isETHAddress(swap.destToken) && dexNeedWrapNative) || forceUnwrap) {
|
|
696
|
+
_dest = forceUnwrap && !dexNeedWrapNative ? _dest : wethAddress;
|
|
649
697
|
wethWithdraw = BigInt(se.destAmount);
|
|
650
698
|
}
|
|
651
699
|
|
|
700
|
+
if (dexNeedUnwrapWeth) {
|
|
701
|
+
const wethAddr =
|
|
702
|
+
this.dexAdapterService.dexHelper.config.data.wrappedNativeTokenAddress.toLowerCase();
|
|
703
|
+
if (swap.srcToken.toLowerCase() === wethAddr) {
|
|
704
|
+
_src = ETHER_ADDRESS;
|
|
705
|
+
wethWithdrawForNeedUnwrapWeth = BigInt(_srcAmount);
|
|
706
|
+
|
|
707
|
+
if (side === SwapSide.BUY) {
|
|
708
|
+
wethDepositForNeedUnwrapWeth = 1n; // to add wrap after dex call for tokens remainder for buy side cases
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
if (swap.destToken.toLowerCase() === wethAddr) {
|
|
713
|
+
_dest = ETHER_ADDRESS;
|
|
714
|
+
wethDepositForNeedUnwrapWeth = BigInt(se.destAmount); // cant use _destAmount here because _destAmount could be 1
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
|
|
652
718
|
const needToWithdrawAfterSwap = _dest === wethAddress && wethWithdraw;
|
|
719
|
+
const needToDepositAfterSwap =
|
|
720
|
+
swap.destToken.toLowerCase() === wethAddress &&
|
|
721
|
+
wethDepositForNeedUnwrapWeth;
|
|
653
722
|
|
|
654
723
|
return {
|
|
655
724
|
srcToken: _src,
|
|
656
725
|
destToken: _dest,
|
|
657
726
|
recipient:
|
|
658
|
-
needToWithdrawAfterSwap || !isLastSwap
|
|
727
|
+
needToWithdrawAfterSwap || !isLastSwap || needToDepositAfterSwap
|
|
659
728
|
? executionContractAddress
|
|
660
729
|
: this.dexAdapterService.dexHelper.config.data.augustusV6Address!,
|
|
661
730
|
srcAmount: _srcAmount,
|
|
662
731
|
destAmount: _destAmount,
|
|
663
732
|
wethDeposit,
|
|
664
733
|
wethWithdraw,
|
|
734
|
+
wethWithdrawForNeedUnwrapWeth,
|
|
735
|
+
wethDepositForNeedUnwrapWeth,
|
|
665
736
|
};
|
|
666
737
|
}
|
|
667
738
|
|
|
@@ -670,6 +741,7 @@ export class GenericSwapTransactionBuilder {
|
|
|
670
741
|
priceRoute: OptimalRate,
|
|
671
742
|
dexExchangeParams: DexExchangeParam[],
|
|
672
743
|
maybeWethCallData?: DepositWithdrawReturn,
|
|
744
|
+
maybeWethCallDataForNeedUnwrapWeth?: DepositWithdrawReturn,
|
|
673
745
|
): Promise<DexExchangeBuildParam[]> {
|
|
674
746
|
const spender = bytecodeBuilder.getAddress();
|
|
675
747
|
const tokenTargetMapping: {
|
|
@@ -262,16 +262,27 @@ export class LocalParaswapSDK implements IParaSwapSDK {
|
|
|
262
262
|
}
|
|
263
263
|
|
|
264
264
|
const { recipient } =
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
265
|
+
priceRoute.version === ParaSwapVersion.V5
|
|
266
|
+
? this.transactionBuilderV5.getDexCallsParams(
|
|
267
|
+
priceRoute,
|
|
268
|
+
routeIndex,
|
|
269
|
+
swap,
|
|
270
|
+
swapIndex,
|
|
271
|
+
se,
|
|
272
|
+
minMaxAmount.toString(),
|
|
273
|
+
dex,
|
|
274
|
+
executionContractAddress,
|
|
275
|
+
)
|
|
276
|
+
: this.transactionBuilder.getDexCallsParams(
|
|
277
|
+
priceRoute,
|
|
278
|
+
routeIndex,
|
|
279
|
+
swap,
|
|
280
|
+
swapIndex,
|
|
281
|
+
se,
|
|
282
|
+
minMaxAmount.toString(),
|
|
283
|
+
dex,
|
|
284
|
+
executionContractAddress,
|
|
285
|
+
);
|
|
275
286
|
|
|
276
287
|
const [preprocessedRoute, txInfo] =
|
|
277
288
|
await dexLibExchange.preProcessTransaction(
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { Logger } from 'log4js';
|
|
2
|
+
import { RequestConfig, Response } from '../../dex-helper/irequest-wrapper';
|
|
3
|
+
import {
|
|
4
|
+
connection as WebSocketConnection,
|
|
5
|
+
client as WebSocketClient,
|
|
6
|
+
} from 'websocket';
|
|
7
|
+
|
|
8
|
+
export class SkippingRequest {
|
|
9
|
+
constructor(public message = '') {}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export type RequestInfo<T> = {
|
|
13
|
+
requestFunc?: (
|
|
14
|
+
options: RequestConfig,
|
|
15
|
+
) => Promise<Response<T> | SkippingRequest>;
|
|
16
|
+
requestOptions: RequestConfig;
|
|
17
|
+
caster: (data: unknown) => T;
|
|
18
|
+
authenticate?: (options: RequestConfig) => RequestConfig;
|
|
19
|
+
excludedFieldsCaching?: string[];
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export type RequestInfoWithHandler<T> = {
|
|
23
|
+
info: RequestInfo<T>;
|
|
24
|
+
handler: (data: T) => void;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export class WebSocketFetcher<T> {
|
|
28
|
+
private requests: RequestInfoWithHandler<T>;
|
|
29
|
+
public lastFetchSucceeded: boolean = false;
|
|
30
|
+
private stop: boolean = true;
|
|
31
|
+
private ws: WebSocketClient = new WebSocketClient();
|
|
32
|
+
private connection: WebSocketConnection | null = null;
|
|
33
|
+
|
|
34
|
+
constructor(requestsInfo: RequestInfoWithHandler<T>, private logger: Logger) {
|
|
35
|
+
this.requests = requestsInfo;
|
|
36
|
+
this.ws.on('connect', this.connected.bind(this));
|
|
37
|
+
this.ws.on('connectFailed', this.connectFailed.bind(this));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
private connected(connection: WebSocketConnection) {
|
|
41
|
+
this.connection = connection;
|
|
42
|
+
this.logger.info(`Connected to ${this.requests.info.requestOptions.url}`);
|
|
43
|
+
this.connection.on('error', this.onError.bind(this));
|
|
44
|
+
this.connection.on('close', this.onClose.bind(this));
|
|
45
|
+
this.connection.on('message', this.onMessage.bind(this));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
private connectFailed(error: any) {
|
|
49
|
+
this.logger.error(`Connect Error: ${error.toString()}`);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
private onClose() {
|
|
53
|
+
this.logger.info(`Connection closed`);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
private onError(error: any) {
|
|
57
|
+
this.logger.error(`Connection Error: ${error.toString()}`);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
private onMessage(message: any) {
|
|
61
|
+
if (message.type === 'utf8') {
|
|
62
|
+
const response = JSON.parse(message.utf8Data) as Response<T>;
|
|
63
|
+
const reqInfo = this.requests;
|
|
64
|
+
const info = reqInfo.info;
|
|
65
|
+
const options = reqInfo.info.requestOptions;
|
|
66
|
+
this.logger.debug(`(${options.url}) received new data`);
|
|
67
|
+
|
|
68
|
+
try {
|
|
69
|
+
const parsedData = info.caster(response);
|
|
70
|
+
reqInfo.handler(parsedData);
|
|
71
|
+
} catch (e) {
|
|
72
|
+
this.logger.info(e);
|
|
73
|
+
this.logger.info(
|
|
74
|
+
`(${options.url}) received incorrect data ${JSON.stringify(
|
|
75
|
+
response,
|
|
76
|
+
).replace(/(?:\r\n|\r|\n)/g, ' ')}`,
|
|
77
|
+
e,
|
|
78
|
+
);
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
private connect() {
|
|
85
|
+
const authorization =
|
|
86
|
+
this.requests.info.requestOptions.headers!.authorization;
|
|
87
|
+
const name = this.requests.info.requestOptions.headers!.name;
|
|
88
|
+
if (typeof authorization !== 'string') {
|
|
89
|
+
throw new Error('Authorization header is not a string');
|
|
90
|
+
}
|
|
91
|
+
if (typeof name !== 'string') {
|
|
92
|
+
throw new Error('Name header is not a string');
|
|
93
|
+
}
|
|
94
|
+
this.ws.connect(
|
|
95
|
+
this.requests.info.requestOptions.url!,
|
|
96
|
+
undefined,
|
|
97
|
+
undefined,
|
|
98
|
+
{
|
|
99
|
+
Authorization: authorization,
|
|
100
|
+
name: name,
|
|
101
|
+
},
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
startPolling(): void {
|
|
106
|
+
this.stop = false;
|
|
107
|
+
this.connect();
|
|
108
|
+
this.logger.info(
|
|
109
|
+
`Connection started for ${this.requests.info.requestOptions.url}`,
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
stopPolling() {
|
|
114
|
+
if (this.connection) {
|
|
115
|
+
this.connection.close();
|
|
116
|
+
}
|
|
117
|
+
this.stop = true;
|
|
118
|
+
this.logger.info(
|
|
119
|
+
`Connection stopped for ${this.requests.info.requestOptions.url}`,
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
isPolling(): boolean {
|
|
124
|
+
return !this.stop;
|
|
125
|
+
}
|
|
126
|
+
}
|