@paraswap/dex-lib 3.8.38 → 3.8.39-1701-1507

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.
Files changed (191) hide show
  1. package/build/abi/USDM.json +807 -0
  2. package/build/abi/bebop/BebopSettlement.abi.json +2137 -0
  3. package/build/abi/wUSDM.json +757 -0
  4. package/build/config.js +16 -9
  5. package/build/config.js.map +1 -1
  6. package/build/dex/aave-v2/aave-v2.d.ts +1 -0
  7. package/build/dex/aave-v2/aave-v2.js +1 -0
  8. package/build/dex/aave-v2/aave-v2.js.map +1 -1
  9. package/build/dex/aave-v3/aave-v3.d.ts +1 -0
  10. package/build/dex/aave-v3/aave-v3.js +1 -0
  11. package/build/dex/aave-v3/aave-v3.js.map +1 -1
  12. package/build/dex/aave-v3-stata/aave-v3-stata.d.ts +1 -0
  13. package/build/dex/aave-v3-stata/aave-v3-stata.js +1 -0
  14. package/build/dex/aave-v3-stata/aave-v3-stata.js.map +1 -1
  15. package/build/dex/bancor/bancor.d.ts +1 -0
  16. package/build/dex/bancor/bancor.js +1 -0
  17. package/build/dex/bancor/bancor.js.map +1 -1
  18. package/build/dex/bebop/bebop.d.ts +55 -0
  19. package/build/dex/bebop/bebop.js +524 -0
  20. package/build/dex/bebop/bebop.js.map +1 -0
  21. package/build/dex/bebop/config.d.ts +3 -0
  22. package/build/dex/bebop/config.js +38 -0
  23. package/build/dex/bebop/config.js.map +1 -0
  24. package/build/dex/bebop/constants.d.ts +9 -0
  25. package/build/dex/bebop/constants.js +13 -0
  26. package/build/dex/bebop/constants.js.map +1 -0
  27. package/build/dex/bebop/rate-fetcher.d.ts +22 -0
  28. package/build/dex/bebop/rate-fetcher.js +80 -0
  29. package/build/dex/bebop/rate-fetcher.js.map +1 -0
  30. package/build/dex/bebop/types.d.ts +76 -0
  31. package/build/dex/bebop/types.js +3 -0
  32. package/build/dex/bebop/types.js.map +1 -0
  33. package/build/dex/bebop/validators.d.ts +4 -0
  34. package/build/dex/bebop/validators.js +42 -0
  35. package/build/dex/bebop/validators.js.map +1 -0
  36. package/build/dex/bebop/websocket-fetcher.d.ts +35 -0
  37. package/build/dex/bebop/websocket-fetcher.js +87 -0
  38. package/build/dex/bebop/websocket-fetcher.js.map +1 -0
  39. package/build/dex/compound/compound.d.ts +1 -0
  40. package/build/dex/compound/compound.js +1 -0
  41. package/build/dex/compound/compound.js.map +1 -1
  42. package/build/dex/curve-v1/curve-v1.d.ts +1 -0
  43. package/build/dex/curve-v1/curve-v1.js +1 -0
  44. package/build/dex/curve-v1/curve-v1.js.map +1 -1
  45. package/build/dex/dodo-v1/dodo-v1.d.ts +1 -0
  46. package/build/dex/dodo-v1/dodo-v1.js +1 -0
  47. package/build/dex/dodo-v1/dodo-v1.js.map +1 -1
  48. package/build/dex/hashflow/hashflow.d.ts +6 -4
  49. package/build/dex/hashflow/hashflow.js +183 -29
  50. package/build/dex/hashflow/hashflow.js.map +1 -1
  51. package/build/dex/hashflow/types.d.ts +2 -0
  52. package/build/dex/hashflow/types.js.map +1 -1
  53. package/build/dex/idex.d.ts +8 -1
  54. package/build/dex/index.js +2 -0
  55. package/build/dex/index.js.map +1 -1
  56. package/build/dex/jarvis-v6/jarvis-v6.d.ts +1 -0
  57. package/build/dex/jarvis-v6/jarvis-v6.js +1 -0
  58. package/build/dex/jarvis-v6/jarvis-v6.js.map +1 -1
  59. package/build/dex/lido/lido.d.ts +1 -0
  60. package/build/dex/lido/lido.js +1 -0
  61. package/build/dex/lido/lido.js.map +1 -1
  62. package/build/dex/lite-psm/lite-psm.d.ts +1 -0
  63. package/build/dex/lite-psm/lite-psm.js +1 -0
  64. package/build/dex/lite-psm/lite-psm.js.map +1 -1
  65. package/build/dex/maker-psm/maker-psm.d.ts +1 -0
  66. package/build/dex/maker-psm/maker-psm.js +1 -0
  67. package/build/dex/maker-psm/maker-psm.js.map +1 -1
  68. package/build/dex/nerve/nerve.d.ts +1 -0
  69. package/build/dex/nerve/nerve.js +1 -0
  70. package/build/dex/nerve/nerve.js.map +1 -1
  71. package/build/dex/polygon-migrator/polygon-migrator.d.ts +1 -0
  72. package/build/dex/polygon-migrator/polygon-migrator.js +1 -0
  73. package/build/dex/polygon-migrator/polygon-migrator.js.map +1 -1
  74. package/build/dex/simple-exchange.d.ts +3 -1
  75. package/build/dex/simple-exchange.js +1 -0
  76. package/build/dex/simple-exchange.js.map +1 -1
  77. package/build/dex/smoothy/smoothy.d.ts +1 -0
  78. package/build/dex/smoothy/smoothy.js +1 -0
  79. package/build/dex/smoothy/smoothy.js.map +1 -1
  80. package/build/dex/spark/spark.d.ts +1 -0
  81. package/build/dex/spark/spark.js +1 -0
  82. package/build/dex/spark/spark.js.map +1 -1
  83. package/build/dex/stable-pool/stable-pool.d.ts +3 -2
  84. package/build/dex/stable-pool/stable-pool.js +2 -1
  85. package/build/dex/stable-pool/stable-pool.js.map +1 -1
  86. package/build/dex/swaap-v2/swaap-v2.d.ts +5 -3
  87. package/build/dex/swaap-v2/swaap-v2.js +112 -21
  88. package/build/dex/swaap-v2/swaap-v2.js.map +1 -1
  89. package/build/dex/swaap-v2/types.d.ts +2 -0
  90. package/build/dex/swaap-v2/types.js.map +1 -1
  91. package/build/dex/trader-joe-v2.1/base.d.ts +1 -0
  92. package/build/dex/trader-joe-v2.1/base.js +1 -0
  93. package/build/dex/trader-joe-v2.1/base.js.map +1 -1
  94. package/build/dex/weth/weth.d.ts +1 -0
  95. package/build/dex/weth/weth.js +1 -0
  96. package/build/dex/weth/weth.js.map +1 -1
  97. package/build/dex/woo-fi-v2/woo-fi-v2-pool.d.ts +2 -2
  98. package/build/dex/woo-fi-v2/woo-fi-v2-pool.js +30 -9
  99. package/build/dex/woo-fi-v2/woo-fi-v2-pool.js.map +1 -1
  100. package/build/dex/woo-fi-v2/woo-fi-v2.js +1 -1
  101. package/build/dex/woo-fi-v2/woo-fi-v2.js.map +1 -1
  102. package/build/dex/wusdm/config.d.ts +3 -0
  103. package/build/dex/wusdm/config.js +29 -0
  104. package/build/dex/wusdm/config.js.map +1 -0
  105. package/build/dex/wusdm/types.d.ts +10 -0
  106. package/build/dex/wusdm/types.js +3 -0
  107. package/build/dex/wusdm/types.js.map +1 -0
  108. package/build/dex/wusdm/wusdm.d.ts +39 -0
  109. package/build/dex/wusdm/wusdm.js +212 -0
  110. package/build/dex/wusdm/wusdm.js.map +1 -0
  111. package/build/executor/Executor01BytecodeBuilder.d.ts +3 -3
  112. package/build/executor/Executor01BytecodeBuilder.js +65 -16
  113. package/build/executor/Executor01BytecodeBuilder.js.map +1 -1
  114. package/build/executor/Executor02BytecodeBuilder.d.ts +4 -4
  115. package/build/executor/Executor02BytecodeBuilder.js +65 -18
  116. package/build/executor/Executor02BytecodeBuilder.js.map +1 -1
  117. package/build/executor/Executor03BytecodeBuilder.d.ts +2 -2
  118. package/build/executor/Executor03BytecodeBuilder.js +52 -8
  119. package/build/executor/Executor03BytecodeBuilder.js.map +1 -1
  120. package/build/executor/ExecutorBytecodeBuilder.d.ts +5 -4
  121. package/build/executor/ExecutorBytecodeBuilder.js +4 -4
  122. package/build/executor/ExecutorBytecodeBuilder.js.map +1 -1
  123. package/build/executor/ExecutorDetector.js +2 -1
  124. package/build/executor/ExecutorDetector.js.map +1 -1
  125. package/build/executor/types.d.ts +1 -0
  126. package/build/executor/types.js +1 -0
  127. package/build/executor/types.js.map +1 -1
  128. package/build/generic-swap-transaction-builder.d.ts +2 -0
  129. package/build/generic-swap-transaction-builder.js +49 -11
  130. package/build/generic-swap-transaction-builder.js.map +1 -1
  131. package/build/implementations/local-paraswap-sdk.js +3 -1
  132. package/build/implementations/local-paraswap-sdk.js.map +1 -1
  133. package/build/lib/fetcher/wsFetcher.d.ts +35 -0
  134. package/build/lib/fetcher/wsFetcher.js +87 -0
  135. package/build/lib/fetcher/wsFetcher.js.map +1 -0
  136. package/build/router/simpleswap.js +51 -13
  137. package/build/router/simpleswap.js.map +1 -1
  138. package/build/types.d.ts +2 -0
  139. package/package.json +4 -2
  140. package/src/abi/bebop/BebopSettlement.abi.json +2137 -0
  141. package/src/config.ts +17 -9
  142. package/src/dex/aave-v2/aave-v2.ts +2 -0
  143. package/src/dex/aave-v3/aave-v3.ts +2 -0
  144. package/src/dex/aave-v3-stata/aave-v3-stata.ts +2 -0
  145. package/src/dex/bancor/bancor.ts +2 -0
  146. package/src/dex/bebop/bebop-e2e.test.ts +195 -0
  147. package/src/dex/bebop/bebop-integration.test.ts +170 -0
  148. package/src/dex/bebop/bebop.ts +732 -0
  149. package/src/dex/bebop/config.ts +37 -0
  150. package/src/dex/bebop/constants.ts +9 -0
  151. package/src/dex/bebop/rate-fetcher.ts +138 -0
  152. package/src/dex/bebop/types.ts +81 -0
  153. package/src/dex/bebop/validators.ts +43 -0
  154. package/src/dex/compound/compound.ts +2 -0
  155. package/src/dex/curve-v1/curve-v1.ts +2 -0
  156. package/src/dex/dodo-v1/dodo-v1.ts +2 -0
  157. package/src/dex/hashflow/hashflow-e2e.test.ts +281 -28
  158. package/src/dex/hashflow/hashflow.ts +285 -54
  159. package/src/dex/hashflow/types.ts +2 -0
  160. package/src/dex/idex.ts +18 -1
  161. package/src/dex/index.ts +2 -0
  162. package/src/dex/jarvis-v6/jarvis-v6.ts +2 -0
  163. package/src/dex/lido/lido.ts +2 -0
  164. package/src/dex/lite-psm/lite-psm.ts +2 -0
  165. package/src/dex/maker-psm/maker-psm.ts +2 -0
  166. package/src/dex/nerve/nerve.ts +2 -0
  167. package/src/dex/polygon-migrator/polygon-migrator.ts +2 -0
  168. package/src/dex/simple-exchange.ts +4 -2
  169. package/src/dex/smoothy/smoothy.ts +2 -0
  170. package/src/dex/spark/spark.ts +2 -0
  171. package/src/dex/stable-pool/stable-pool.ts +4 -1
  172. package/src/dex/swaap-v2/swaap-v2-e2e.test.ts +13 -13
  173. package/src/dex/swaap-v2/swaap-v2.ts +217 -32
  174. package/src/dex/swaap-v2/types.ts +2 -0
  175. package/src/dex/trader-joe-v2.1/base.ts +2 -0
  176. package/src/dex/weth/weth.ts +2 -0
  177. package/src/dex/woo-fi-v2/woo-fi-v2-pool.ts +58 -27
  178. package/src/dex/woo-fi-v2/woo-fi-v2.ts +1 -2
  179. package/src/executor/Executor01BytecodeBuilder.ts +105 -11
  180. package/src/executor/Executor02BytecodeBuilder.ts +95 -8
  181. package/src/executor/Executor03BytecodeBuilder.ts +89 -5
  182. package/src/executor/ExecutorBytecodeBuilder.ts +6 -0
  183. package/src/executor/ExecutorDetector.ts +2 -1
  184. package/src/executor/types.ts +1 -0
  185. package/src/generic-swap-transaction-builder.ts +96 -24
  186. package/src/implementations/local-paraswap-sdk.ts +21 -10
  187. package/src/lib/fetcher/wsFetcher.ts +126 -0
  188. package/src/router/simpleswap.ts +94 -18
  189. package/src/types.ts +2 -0
  190. package/tests/constants-e2e.ts +2 -2
  191. package/tests/utils-e2e.ts +308 -2
@@ -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
@@ -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 { exchangeParams, srcAmountWethToDeposit, destAmountWethToWithdraw } =
166
- await rawDexParams.reduce<{
167
- exchangeParams: DexExchangeParam[];
168
- srcAmountWethToDeposit: bigint;
169
- destAmountWethToWithdraw: bigint;
170
- }>(
171
- (acc, se) => {
172
- acc.srcAmountWethToDeposit += BigInt(se.wethDeposit);
173
- acc.destAmountWethToWithdraw += BigInt(se.wethWithdraw);
174
- acc.exchangeParams.push(se.dexParams);
175
- return acc;
176
- },
177
- {
178
- exchangeParams: [],
179
- srcAmountWethToDeposit: 0n,
180
- destAmountWethToWithdraw: 0n,
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
- if (isETHAddress(swap.srcToken) && dex.needWrapNative) {
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
- !dex.needWrapNative &&
692
+ !dexNeedWrapNative &&
645
693
  !isLastSwap;
646
694
 
647
- if ((isETHAddress(swap.destToken) && dex.needWrapNative) || forceUnwrap) {
648
- _dest = forceUnwrap && !dex.needWrapNative ? _dest : wethAddress;
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
- this.transactionBuilder.getDexCallsParams(
266
- priceRoute,
267
- routeIndex,
268
- swap,
269
- swapIndex,
270
- se,
271
- minMaxAmount.toString(),
272
- dex,
273
- executionContractAddress,
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
+ }
@@ -6,7 +6,7 @@ import {
6
6
  TxInfo,
7
7
  SimpleExchangeParam,
8
8
  } from '../types';
9
- import { SwapSide } from '../constants';
9
+ import { ETHER_ADDRESS, SwapSide } from '../constants';
10
10
  import IParaswapABI from '../abi/IParaswap.json';
11
11
  import { Interface } from '@ethersproject/abi';
12
12
  import { isETHAddress, uuidToBytes16 } from '../utils';
@@ -95,9 +95,11 @@ export abstract class SimpleRouterBase<RouterParam>
95
95
  swap.swapExchanges.map(async se => {
96
96
  const dex = this.dexAdapterService.getTxBuilderDexByKey(se.exchange);
97
97
  let _src = swap.srcToken;
98
- let wethDeposit = 0n;
98
+ let wethDepositForNeedWrapNative = 0n;
99
+ let wethDepositForNeedUnwrapWeth = 0n;
99
100
  let _dest = swap.destToken;
100
- let wethWithdraw = 0n;
101
+ let wethWithdrawForNeedWrapNative = 0n;
102
+ let wethWithdrawForNeedUnwrapWeth = 0n;
101
103
 
102
104
  // For case of buy apply slippage is applied to srcAmount in equal proportion as the complete swap
103
105
  // This assumes that the sum of all swaps srcAmount would sum to priceRoute.srcAmount
@@ -115,13 +117,23 @@ export abstract class SimpleRouterBase<RouterParam>
115
117
  // should work if the final slippage check passes.
116
118
  const _destAmount = this.side === SwapSide.SELL ? '1' : se.destAmount;
117
119
 
118
- if (dex.needWrapNative) {
120
+ const dexNeedWrapNative =
121
+ typeof dex.needWrapNative === 'function'
122
+ ? dex.needWrapNative(priceRoute, swap, se)
123
+ : dex.needWrapNative;
124
+
125
+ const dexNeedUnwrapWeth =
126
+ typeof dex.needUnwrapWeth === 'function'
127
+ ? dex.needUnwrapWeth(priceRoute, swap, se)
128
+ : dex.needUnwrapWeth;
129
+
130
+ if (dexNeedWrapNative) {
119
131
  if (isETHAddress(swap.srcToken)) {
120
132
  if (swapIndex !== 0) {
121
133
  throw new Error('Wrap native srcToken not in swapIndex 0');
122
134
  }
123
135
  _src = wethAddress;
124
- wethDeposit = BigInt(_srcAmount);
136
+ wethDepositForNeedWrapNative = BigInt(_srcAmount);
125
137
  }
126
138
 
127
139
  if (isETHAddress(swap.destToken)) {
@@ -129,7 +141,21 @@ export abstract class SimpleRouterBase<RouterParam>
129
141
  throw new Error('Wrap native destToken not in swapIndex last');
130
142
  }
131
143
  _dest = wethAddress;
132
- wethWithdraw = BigInt(_destAmount);
144
+ wethWithdrawForNeedWrapNative = BigInt(_destAmount);
145
+ }
146
+ }
147
+
148
+ if (dexNeedUnwrapWeth) {
149
+ const wethAddr =
150
+ this.dexAdapterService.dexHelper.config.data.wrappedNativeTokenAddress.toLowerCase();
151
+ if (swap.srcToken.toLowerCase() === wethAddr) {
152
+ _src = ETHER_ADDRESS;
153
+ wethWithdrawForNeedUnwrapWeth = BigInt(_srcAmount);
154
+ }
155
+
156
+ if (swap.destToken.toLowerCase() === wethAddr) {
157
+ _dest = ETHER_ADDRESS;
158
+ wethDepositForNeedUnwrapWeth = BigInt(se.destAmount); // cant use _destAmount here because _destAmount could be 1
133
159
  }
134
160
  }
135
161
 
@@ -144,8 +170,10 @@ export abstract class SimpleRouterBase<RouterParam>
144
170
 
145
171
  return {
146
172
  simpleParams,
147
- wethDeposit,
148
- wethWithdraw,
173
+ wethDepositForNeedWrapNative,
174
+ wethWithdrawForNeedWrapNative,
175
+ wethWithdrawForNeedUnwrapWeth,
176
+ wethDepositForNeedUnwrapWeth,
149
177
  };
150
178
  }),
151
179
  ),
@@ -153,16 +181,30 @@ export abstract class SimpleRouterBase<RouterParam>
153
181
 
154
182
  const {
155
183
  simpleExchangeDataList,
156
- srcAmountWethToDeposit,
157
- destAmountWethToWithdraw,
184
+ srcAmountWethToDepositForNeedWrapNative,
185
+ destAmountWethToWithdrawForNeedWrapNative,
186
+ srcAmountWethToWithdrawForNeedUnwrapWeth,
187
+ destAmountWethToDepositForNeeUnwrapWeth,
158
188
  } = rawSimpleParams.reduce<{
159
189
  simpleExchangeDataList: SimpleExchangeParam[];
160
- srcAmountWethToDeposit: bigint;
161
- destAmountWethToWithdraw: bigint;
190
+ srcAmountWethToDepositForNeedWrapNative: bigint;
191
+ srcAmountWethToWithdrawForNeedUnwrapWeth: bigint;
192
+ destAmountWethToWithdrawForNeedWrapNative: bigint;
193
+ destAmountWethToDepositForNeeUnwrapWeth: bigint;
162
194
  }>(
163
195
  (acc, se) => {
164
- acc.srcAmountWethToDeposit += BigInt(se.wethDeposit);
165
- acc.destAmountWethToWithdraw += BigInt(se.wethWithdraw);
196
+ acc.srcAmountWethToDepositForNeedWrapNative += BigInt(
197
+ se.wethDepositForNeedWrapNative,
198
+ );
199
+ acc.srcAmountWethToWithdrawForNeedUnwrapWeth += BigInt(
200
+ se.wethWithdrawForNeedUnwrapWeth,
201
+ );
202
+ acc.destAmountWethToWithdrawForNeedWrapNative += BigInt(
203
+ se.wethWithdrawForNeedWrapNative,
204
+ );
205
+ acc.destAmountWethToDepositForNeeUnwrapWeth += BigInt(
206
+ se.wethDepositForNeedUnwrapWeth,
207
+ );
166
208
  // V6 doesn't have simpleParams
167
209
  if (se.simpleParams) {
168
210
  acc.simpleExchangeDataList.push(se.simpleParams);
@@ -172,8 +214,10 @@ export abstract class SimpleRouterBase<RouterParam>
172
214
  },
173
215
  {
174
216
  simpleExchangeDataList: [],
175
- srcAmountWethToDeposit: 0n,
176
- destAmountWethToWithdraw: 0n,
217
+ srcAmountWethToDepositForNeedWrapNative: 0n,
218
+ srcAmountWethToWithdrawForNeedUnwrapWeth: 0n,
219
+ destAmountWethToWithdrawForNeedWrapNative: 0n,
220
+ destAmountWethToDepositForNeeUnwrapWeth: 0n,
177
221
  },
178
222
  );
179
223
 
@@ -188,10 +232,16 @@ export abstract class SimpleRouterBase<RouterParam>
188
232
  );
189
233
 
190
234
  const maybeWethCallData = this.getDepositWithdrawWethCallData(
191
- srcAmountWethToDeposit,
192
- destAmountWethToWithdraw,
235
+ srcAmountWethToDepositForNeedWrapNative,
236
+ destAmountWethToWithdrawForNeedWrapNative,
193
237
  );
194
238
 
239
+ const maybeWethCallDataForNeedUnwrapWeth =
240
+ this.getDepositWithdrawWethCallData(
241
+ destAmountWethToDepositForNeeUnwrapWeth,
242
+ srcAmountWethToWithdrawForNeedUnwrapWeth,
243
+ );
244
+
195
245
  if (maybeWethCallData) {
196
246
  if (maybeWethCallData.deposit) {
197
247
  simpleExchangeDataFlat.callees.unshift(
@@ -211,6 +261,32 @@ export abstract class SimpleRouterBase<RouterParam>
211
261
  }
212
262
  }
213
263
 
264
+ if (maybeWethCallDataForNeedUnwrapWeth) {
265
+ if (maybeWethCallDataForNeedUnwrapWeth.withdraw) {
266
+ simpleExchangeDataFlat.callees.unshift(
267
+ maybeWethCallDataForNeedUnwrapWeth.withdraw.callee,
268
+ );
269
+ simpleExchangeDataFlat.values.unshift(
270
+ maybeWethCallDataForNeedUnwrapWeth.withdraw.value,
271
+ );
272
+ simpleExchangeDataFlat.calldata.unshift(
273
+ maybeWethCallDataForNeedUnwrapWeth.withdraw.calldata,
274
+ );
275
+ }
276
+
277
+ if (maybeWethCallDataForNeedUnwrapWeth.deposit) {
278
+ simpleExchangeDataFlat.callees.push(
279
+ maybeWethCallDataForNeedUnwrapWeth.deposit.callee,
280
+ );
281
+ simpleExchangeDataFlat.values.push(
282
+ maybeWethCallDataForNeedUnwrapWeth.deposit.value,
283
+ );
284
+ simpleExchangeDataFlat.calldata.push(
285
+ maybeWethCallDataForNeedUnwrapWeth.deposit.calldata,
286
+ );
287
+ }
288
+ }
289
+
214
290
  return {
215
291
  partialContractSimpleData: this.buildPartialContractSimpleData(
216
292
  simpleExchangeDataFlat,
package/src/types.ts CHANGED
@@ -159,6 +159,7 @@ export type AdapterExchangeParam = {
159
159
 
160
160
  export type DexExchangeParam = {
161
161
  needWrapNative: boolean;
162
+ needUnwrapWeth?: boolean;
162
163
  skipApproval?: boolean;
163
164
  wethAddress?: string;
164
165
  exchangeData: string;
@@ -304,6 +305,7 @@ export type Config = {
304
305
  uniswapV3EventLoggingSampleRate?: number;
305
306
  swaapV2AuthToken?: string;
306
307
  dexalotAuthToken?: string;
308
+ bebopAuthToken?: string;
307
309
  idleDaoAuthToken?: string;
308
310
  forceRpcFallbackDexs: string[];
309
311
  apiKeyTheGraph: string;
@@ -1532,7 +1532,7 @@ export const Holders: {
1532
1532
  AA_iETHv2: '0xA118aD79E2152b9a3c7Df8B8791887762b0f1D49',
1533
1533
  BB_iETHv2: '0x15079cBAa74C1df2a602fAc88Bd5b98B08FfE6A4',
1534
1534
  ETH: '0x176F3DAb24a159341c0509bB36B833E7fdd0a132',
1535
- USDC: '0x7713974908be4bed47172370115e8b1219f4a5f0',
1535
+ USDC: '0xd8A3c2901b736170b70088a0Df9AA914317d43C6',
1536
1536
  USDE: '0x8707f238936c12c309bfc2B9959C35828AcFc512',
1537
1537
  AMPL: '0x223592a191ECfC7FDC38a9256c3BD96E771539A9',
1538
1538
  WBTC: '0x6daB3bCbFb336b29d06B9C793AEF7eaA57888922',
@@ -1770,7 +1770,7 @@ export const Holders: {
1770
1770
  WETH: '0x3368e17064c9ba5d6f1f93c4c678bea00cc78555',
1771
1771
  BAL: '0x7b7b957c284c2c227c980d6e2f804311947b84d0',
1772
1772
  USDCe: '0x62383739d68dd0f844103db8dfb05a7eded5bbe6',
1773
- USDC: '0xb38e8c17e38363af6ebdcb3dae12e0243582891d',
1773
+ USDC: '0x62383739D68Dd0F844103Db8dFb05a7EdED5BBE6',
1774
1774
  OHM: '0xebce5f29ff5ca9aa330ebdf7ec6b5f474bff271e',
1775
1775
  USDT: '0xf977814e90da44bfa03b6295a0616a897441acec',
1776
1776
  POPS: '0x4b78b52e7de4d8b7d367297cb8a87c1875a9d591',