@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.
Files changed (198) 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/config.js +8 -0
  81. package/build/dex/spark/config.js.map +1 -1
  82. package/build/dex/spark/spark.d.ts +4 -1
  83. package/build/dex/spark/spark.js +20 -15
  84. package/build/dex/spark/spark.js.map +1 -1
  85. package/build/dex/spark/types.d.ts +9 -0
  86. package/build/dex/spark/types.js +8 -1
  87. package/build/dex/spark/types.js.map +1 -1
  88. package/build/dex/stable-pool/stable-pool.d.ts +3 -2
  89. package/build/dex/stable-pool/stable-pool.js +2 -1
  90. package/build/dex/stable-pool/stable-pool.js.map +1 -1
  91. package/build/dex/swaap-v2/swaap-v2.d.ts +5 -3
  92. package/build/dex/swaap-v2/swaap-v2.js +112 -21
  93. package/build/dex/swaap-v2/swaap-v2.js.map +1 -1
  94. package/build/dex/swaap-v2/types.d.ts +2 -0
  95. package/build/dex/swaap-v2/types.js.map +1 -1
  96. package/build/dex/trader-joe-v2.1/base.d.ts +1 -0
  97. package/build/dex/trader-joe-v2.1/base.js +1 -0
  98. package/build/dex/trader-joe-v2.1/base.js.map +1 -1
  99. package/build/dex/weth/weth.d.ts +1 -0
  100. package/build/dex/weth/weth.js +1 -0
  101. package/build/dex/weth/weth.js.map +1 -1
  102. package/build/dex/woo-fi-v2/woo-fi-v2-pool.d.ts +2 -2
  103. package/build/dex/woo-fi-v2/woo-fi-v2-pool.js +30 -9
  104. package/build/dex/woo-fi-v2/woo-fi-v2-pool.js.map +1 -1
  105. package/build/dex/woo-fi-v2/woo-fi-v2.js +1 -1
  106. package/build/dex/woo-fi-v2/woo-fi-v2.js.map +1 -1
  107. package/build/dex/wusdm/config.d.ts +3 -0
  108. package/build/dex/wusdm/config.js +29 -0
  109. package/build/dex/wusdm/config.js.map +1 -0
  110. package/build/dex/wusdm/types.d.ts +10 -0
  111. package/build/dex/wusdm/types.js +3 -0
  112. package/build/dex/wusdm/types.js.map +1 -0
  113. package/build/dex/wusdm/wusdm.d.ts +39 -0
  114. package/build/dex/wusdm/wusdm.js +212 -0
  115. package/build/dex/wusdm/wusdm.js.map +1 -0
  116. package/build/executor/Executor01BytecodeBuilder.d.ts +3 -3
  117. package/build/executor/Executor01BytecodeBuilder.js +65 -16
  118. package/build/executor/Executor01BytecodeBuilder.js.map +1 -1
  119. package/build/executor/Executor02BytecodeBuilder.d.ts +4 -4
  120. package/build/executor/Executor02BytecodeBuilder.js +65 -18
  121. package/build/executor/Executor02BytecodeBuilder.js.map +1 -1
  122. package/build/executor/Executor03BytecodeBuilder.d.ts +2 -2
  123. package/build/executor/Executor03BytecodeBuilder.js +52 -8
  124. package/build/executor/Executor03BytecodeBuilder.js.map +1 -1
  125. package/build/executor/ExecutorBytecodeBuilder.d.ts +5 -4
  126. package/build/executor/ExecutorBytecodeBuilder.js +4 -4
  127. package/build/executor/ExecutorBytecodeBuilder.js.map +1 -1
  128. package/build/executor/ExecutorDetector.js +2 -1
  129. package/build/executor/ExecutorDetector.js.map +1 -1
  130. package/build/executor/types.d.ts +1 -0
  131. package/build/executor/types.js +1 -0
  132. package/build/executor/types.js.map +1 -1
  133. package/build/generic-swap-transaction-builder.d.ts +2 -0
  134. package/build/generic-swap-transaction-builder.js +49 -11
  135. package/build/generic-swap-transaction-builder.js.map +1 -1
  136. package/build/implementations/local-paraswap-sdk.js +3 -1
  137. package/build/implementations/local-paraswap-sdk.js.map +1 -1
  138. package/build/lib/fetcher/wsFetcher.d.ts +35 -0
  139. package/build/lib/fetcher/wsFetcher.js +87 -0
  140. package/build/lib/fetcher/wsFetcher.js.map +1 -0
  141. package/build/router/simpleswap.js +51 -13
  142. package/build/router/simpleswap.js.map +1 -1
  143. package/build/types.d.ts +2 -0
  144. package/package.json +4 -2
  145. package/src/abi/bebop/BebopSettlement.abi.json +2137 -0
  146. package/src/config.ts +17 -9
  147. package/src/dex/aave-v2/aave-v2.ts +2 -0
  148. package/src/dex/aave-v3/aave-v3.ts +2 -0
  149. package/src/dex/aave-v3-stata/aave-v3-stata.ts +2 -0
  150. package/src/dex/bancor/bancor.ts +2 -0
  151. package/src/dex/bebop/bebop-e2e.test.ts +195 -0
  152. package/src/dex/bebop/bebop-integration.test.ts +170 -0
  153. package/src/dex/bebop/bebop.ts +732 -0
  154. package/src/dex/bebop/config.ts +37 -0
  155. package/src/dex/bebop/constants.ts +9 -0
  156. package/src/dex/bebop/rate-fetcher.ts +138 -0
  157. package/src/dex/bebop/types.ts +81 -0
  158. package/src/dex/bebop/validators.ts +43 -0
  159. package/src/dex/compound/compound.ts +2 -0
  160. package/src/dex/curve-v1/curve-v1.ts +2 -0
  161. package/src/dex/dodo-v1/dodo-v1.ts +2 -0
  162. package/src/dex/hashflow/hashflow-e2e.test.ts +281 -28
  163. package/src/dex/hashflow/hashflow.ts +285 -54
  164. package/src/dex/hashflow/types.ts +2 -0
  165. package/src/dex/idex.ts +18 -1
  166. package/src/dex/index.ts +2 -0
  167. package/src/dex/jarvis-v6/jarvis-v6.ts +2 -0
  168. package/src/dex/lido/lido.ts +2 -0
  169. package/src/dex/lite-psm/lite-psm.ts +2 -0
  170. package/src/dex/maker-psm/maker-psm.ts +2 -0
  171. package/src/dex/nerve/nerve.ts +2 -0
  172. package/src/dex/polygon-migrator/polygon-migrator.ts +2 -0
  173. package/src/dex/simple-exchange.ts +4 -2
  174. package/src/dex/smoothy/smoothy.ts +2 -0
  175. package/src/dex/spark/config.ts +8 -1
  176. package/src/dex/spark/spark.ts +27 -18
  177. package/src/dex/spark/types.ts +10 -0
  178. package/src/dex/stable-pool/stable-pool.ts +4 -1
  179. package/src/dex/swaap-v2/swaap-v2-e2e.test.ts +13 -13
  180. package/src/dex/swaap-v2/swaap-v2.ts +217 -32
  181. package/src/dex/swaap-v2/types.ts +2 -0
  182. package/src/dex/trader-joe-v2.1/base.ts +2 -0
  183. package/src/dex/weth/weth.ts +2 -0
  184. package/src/dex/woo-fi-v2/woo-fi-v2-pool.ts +58 -27
  185. package/src/dex/woo-fi-v2/woo-fi-v2.ts +1 -2
  186. package/src/executor/Executor01BytecodeBuilder.ts +105 -11
  187. package/src/executor/Executor02BytecodeBuilder.ts +95 -8
  188. package/src/executor/Executor03BytecodeBuilder.ts +89 -5
  189. package/src/executor/ExecutorBytecodeBuilder.ts +6 -0
  190. package/src/executor/ExecutorDetector.ts +2 -1
  191. package/src/executor/types.ts +1 -0
  192. package/src/generic-swap-transaction-builder.ts +96 -24
  193. package/src/implementations/local-paraswap-sdk.ts +21 -10
  194. package/src/lib/fetcher/wsFetcher.ts +126 -0
  195. package/src/router/simpleswap.ts +94 -18
  196. package/src/types.ts +2 -0
  197. package/tests/constants-e2e.ts +2 -2
  198. package/tests/utils-e2e.ts +308 -2
@@ -1,11 +1,6 @@
1
1
  import { SimpleExchange } from '../simple-exchange';
2
2
  import { Context, IDex } from '../idex';
3
- import {
4
- SparkParams,
5
- SparkData,
6
- SparkSDaiFunctions,
7
- SparkSDaiPoolState,
8
- } from './types';
3
+ import { SparkParams, SparkData, SparkSDaiPoolState } from './types';
9
4
  import { Network, SwapSide } from '../../constants';
10
5
  import { getDexKeysWithNetwork } from '../../utils';
11
6
  import { Adapters, SDaiConfig } from './config';
@@ -23,12 +18,10 @@ import {
23
18
  } from '../../types';
24
19
  import { IDexHelper } from '../../dex-helper';
25
20
  import * as CALLDATA_GAS_COST from '../../calldata-gas-cost';
26
- import PotAbi from '../../abi/maker-psm/pot.json';
27
- import SavingsDaiAbi from '../../abi/sdai/SavingsDai.abi.json';
28
21
  import { Interface } from 'ethers/lib/utils';
29
22
  import { calcChi, RAY, SparkSDaiEventPool } from './spark-sdai-pool';
30
23
  import { BI_POWS } from '../../bigint-constants';
31
- import { SDAI_DEPOSIT_GAS_COST, SDAI_REDEEM_GAS_COST } from './constants';
24
+ import { SDAI_DEPOSIT_GAS_COST } from './constants';
32
25
  import { extractReturnAmountPosition } from '../../executor/utils';
33
26
 
34
27
  export class Spark
@@ -38,6 +31,8 @@ export class Spark
38
31
  readonly hasConstantPriceLargeAmounts = true;
39
32
  readonly isFeeOnTransferSupported = false;
40
33
 
34
+ needWrapNative = false;
35
+
41
36
  public static dexKeysWithNetwork: { key: string; networks: Network[] }[] =
42
37
  getDexKeysWithNetwork(SDaiConfig);
43
38
 
@@ -56,7 +51,9 @@ export class Spark
56
51
  .poolInterface,
57
52
 
58
53
  protected adapters = Adapters[network] || {},
59
- protected sdaiInterface = new Interface(SavingsDaiAbi),
54
+ protected sdaiInterface = SDaiConfig[dexKey][network].exchangeInterface,
55
+ protected swapFunctions = SDaiConfig[dexKey][network].swapFunctions,
56
+ protected referralCode = SDaiConfig[dexKey][network].referralCode,
60
57
  ) {
61
58
  super(dexHelper, dexKey);
62
59
  this.logger = dexHelper.getLogger(dexKey);
@@ -191,13 +188,19 @@ export class Spark
191
188
 
192
189
  let swapData: string;
193
190
  if (this.isDai(srcToken)) {
191
+ const calldata = [isSell ? srcAmount : destAmount, this.augustusAddress];
192
+
193
+ if (this.referralCode) {
194
+ calldata.push(this.referralCode);
195
+ }
196
+
194
197
  swapData = this.sdaiInterface.encodeFunctionData(
195
- isSell ? SparkSDaiFunctions.deposit : SparkSDaiFunctions.mint,
196
- [isSell ? srcAmount : destAmount, this.augustusAddress],
198
+ isSell ? this.swapFunctions.deposit : this.swapFunctions.mint,
199
+ calldata,
197
200
  );
198
201
  } else {
199
202
  swapData = this.sdaiInterface.encodeFunctionData(
200
- isSell ? SparkSDaiFunctions.redeem : SparkSDaiFunctions.withdraw,
203
+ isSell ? this.swapFunctions.redeem : this.swapFunctions.withdraw,
201
204
  [
202
205
  isSell ? srcAmount : destAmount,
203
206
  this.augustusAddress,
@@ -236,13 +239,19 @@ export class Spark
236
239
 
237
240
  let swapData: string;
238
241
  if (this.isDai(srcToken)) {
242
+ const calldata = [isSell ? srcAmount : destAmount, recipient];
243
+
244
+ if (this.referralCode) {
245
+ calldata.push(this.referralCode);
246
+ }
247
+
239
248
  swapData = this.sdaiInterface.encodeFunctionData(
240
- isSell ? SparkSDaiFunctions.deposit : SparkSDaiFunctions.mint,
241
- [isSell ? srcAmount : destAmount, recipient],
249
+ isSell ? this.swapFunctions.deposit : this.swapFunctions.mint,
250
+ calldata,
242
251
  );
243
252
  } else {
244
253
  swapData = this.sdaiInterface.encodeFunctionData(
245
- isSell ? SparkSDaiFunctions.redeem : SparkSDaiFunctions.withdraw,
254
+ isSell ? this.swapFunctions.redeem : this.swapFunctions.withdraw,
246
255
  [isSell ? srcAmount : destAmount, recipient, executorAddress],
247
256
  );
248
257
  }
@@ -256,8 +265,8 @@ export class Spark
256
265
  ? extractReturnAmountPosition(
257
266
  this.sdaiInterface,
258
267
  this.isDai(srcToken)
259
- ? SparkSDaiFunctions.deposit
260
- : SparkSDaiFunctions.redeem,
268
+ ? this.swapFunctions.deposit
269
+ : this.swapFunctions.redeem,
261
270
  this.isDai(srcToken) ? 'shares' : 'assets',
262
271
  )
263
272
  : undefined,
@@ -12,6 +12,9 @@ export type SparkParams = {
12
12
  topic: string;
13
13
  };
14
14
  poolInterface: Interface;
15
+ exchangeInterface: Interface;
16
+ swapFunctions: typeof SparkSDaiFunctions | typeof SparkSUSDSFunctions;
17
+ referralCode: null | string;
15
18
  };
16
19
 
17
20
  export enum SparkSDaiFunctions {
@@ -21,6 +24,13 @@ export enum SparkSDaiFunctions {
21
24
  mint = 'mint',
22
25
  }
23
26
 
27
+ export enum SparkSUSDSFunctions {
28
+ deposit = 'deposit(uint256,address,uint16)',
29
+ redeem = 'redeem',
30
+ withdraw = 'withdraw',
31
+ mint = 'mint(uint256,address,uint16)',
32
+ }
33
+
24
34
  export type SparkSDaiPoolState = {
25
35
  rho: string;
26
36
  chi: string;
@@ -7,7 +7,7 @@ import {
7
7
  NumberAsString,
8
8
  SimpleExchangeParam,
9
9
  } from '../../types';
10
- import { IDexTxBuilder } from '../idex';
10
+ import { Context, IDexTxBuilder } from '../idex';
11
11
  import { SimpleExchange } from '../simple-exchange';
12
12
  import StablePoolABI from '../../abi/StablePool.json';
13
13
  import { IDexHelper } from '../../dex-helper';
@@ -22,6 +22,8 @@ export class StablePool
22
22
  exchangeRouterInterface: Interface;
23
23
  minConversionRate = '1';
24
24
 
25
+ needWrapNative = false;
26
+
25
27
  constructor(dexHelper: IDexHelper) {
26
28
  super(dexHelper, 'stablePool');
27
29
  this.exchangeRouterInterface = new Interface(StablePoolABI);
@@ -96,6 +98,7 @@ export class StablePool
96
98
  _recipient: Address,
97
99
  data: StablePoolData,
98
100
  side: SwapSide,
101
+ context: Context,
99
102
  ): DexExchangeParam {
100
103
  if (side === SwapSide.BUY) throw new Error(`Buy not supported`);
101
104
 
@@ -23,7 +23,7 @@ describe('SwaapV2 E2E', () => {
23
23
  ],
24
24
  ],
25
25
  // [SwapSide.BUY, [ContractMethod.simpleBuy, ContractMethod.buy]],
26
- [SwapSide.BUY, [ContractMethod.swapExactAmountOut]],
26
+ // [SwapSide.BUY, [ContractMethod.swapExactAmountOut]],
27
27
  ]);
28
28
 
29
29
  describe('Mainnet', () => {
@@ -48,18 +48,18 @@ describe('SwaapV2 E2E', () => {
48
48
  buyAmount: '100000000',
49
49
  },
50
50
  ],
51
- [
52
- {
53
- name: 'USDC',
54
- sellAmount: '45410357',
55
- buyAmount: '1000000000000000000',
56
- },
57
- {
58
- name: 'ETH',
59
- sellAmount: '1000000000000000000',
60
- buyAmount: '100000000',
61
- },
62
- ],
51
+ // [
52
+ // {
53
+ // name: 'USDC',
54
+ // sellAmount: '45410357',
55
+ // buyAmount: '1000000000000000000',
56
+ // },
57
+ // {
58
+ // name: 'ETH',
59
+ // sellAmount: '1000000000000000000',
60
+ // buyAmount: '100000000',
61
+ // },
62
+ // ],
63
63
  ];
64
64
 
65
65
  sideToContractMethods.forEach((contractMethods, side) =>
@@ -18,10 +18,13 @@ import {
18
18
  MAX_INT,
19
19
  MAX_UINT,
20
20
  CACHE_PREFIX,
21
+ NULL_ADDRESS,
22
+ ETHER_ADDRESS,
21
23
  } from '../../constants';
22
24
  import * as CALLDATA_GAS_COST from '../../calldata-gas-cost';
25
+ import { uniq } from 'lodash';
23
26
  import { getDexKeysWithNetwork, isAxiosError } from '../../utils';
24
- import { IDex } from '../idex';
27
+ import { Context, IDex } from '../idex';
25
28
  import { IDexHelper } from '../../dex-helper/idex-helper';
26
29
  import {
27
30
  SwaapV2Data,
@@ -78,19 +81,68 @@ import {
78
81
  import { BI_MAX_UINT256 } from '../../bigint-constants';
79
82
  import { SpecialDex } from '../../executor/types';
80
83
  import { extractReturnAmountPosition } from '../../executor/utils';
84
+ import { OptimalRate, OptimalSwap } from '@paraswap/core';
85
+ import { ZERO_ADDRESS } from '@hashflow/sdk';
81
86
 
82
87
  const BLACKLISTED = 'blacklisted';
83
88
 
84
89
  export class SwaapV2 extends SimpleExchange implements IDex<SwaapV2Data> {
85
90
  readonly isStatePollingDex = true;
86
91
  readonly hasConstantPriceLargeAmounts = false;
87
- readonly needWrapNative = false;
88
92
  readonly isFeeOnTransferSupported = false;
89
93
  private rateFetcher: RateFetcher;
90
94
  private swaapV2AuthToken: string;
91
95
  private tokensMap: TokensMap = {};
92
96
  private runtimeMMsRestrictHashMapKey: string;
93
97
 
98
+ needUnwrapWeth = (
99
+ priceRoute: OptimalRate,
100
+ swap: OptimalSwap,
101
+ se: OptimalSwapExchange<any>,
102
+ ): boolean => {
103
+ const swapSrc = swap.srcToken.toLowerCase();
104
+ const swapDest = swap.destToken.toLowerCase();
105
+
106
+ const wethAddr =
107
+ this.dexHelper.config.data.wrappedNativeTokenAddress.toLowerCase();
108
+ const tokenIn = se.data.tokenIn.toLowerCase();
109
+ const tokenOut = se.data.tokenOut.toLowerCase();
110
+
111
+ if (swapSrc.toLowerCase() === wethAddr && tokenIn === NULL_ADDRESS) {
112
+ return true; // WETH is src but native ETH pool is used, we need unwrap weth in this case
113
+ }
114
+
115
+ if (swapDest.toLowerCase() === wethAddr && tokenOut === NULL_ADDRESS) {
116
+ return true; // WETH is dest but native ETH pool is used, we need unwrap weth in this case
117
+ }
118
+
119
+ return false;
120
+ };
121
+
122
+ needWrapNative = (
123
+ priceRoute: OptimalRate,
124
+ swap: OptimalSwap,
125
+ se: OptimalSwapExchange<any>,
126
+ ): boolean => {
127
+ const swapSrc = swap.srcToken.toLowerCase();
128
+ const swapDest = swap.destToken.toLowerCase();
129
+
130
+ const wethAddr =
131
+ this.dexHelper.config.data.wrappedNativeTokenAddress.toLowerCase();
132
+ const tokenIn = se.data.tokenIn.toLowerCase();
133
+ const tokenOut = se.data.tokenOut.toLowerCase();
134
+
135
+ if (swapSrc === ETHER_ADDRESS.toLowerCase() && tokenIn === wethAddr) {
136
+ return true; // ETH is src but WETH pool is used, we need wrap native in this case
137
+ }
138
+
139
+ if (swapDest === ETHER_ADDRESS.toLowerCase() && tokenOut === wethAddr) {
140
+ return true; // ETH is dest but WETH pool is used, we need wrap native in this case
141
+ }
142
+
143
+ return false;
144
+ };
145
+
94
146
  public static dexKeysWithNetwork: { key: string; networks: Network[] }[] =
95
147
  getDexKeysWithNetwork(SwaapV2Config);
96
148
 
@@ -162,26 +214,68 @@ export class SwaapV2 extends SimpleExchange implements IDex<SwaapV2Data> {
162
214
  return [];
163
215
  }
164
216
 
165
- const poolIdentifier = getPoolIdentifier(
166
- this.dexKey,
167
- normalizedSrcToken.address,
168
- normalizedDestToken.address,
169
- );
217
+ const wethAddr =
218
+ this.dexHelper.config.data.wrappedNativeTokenAddress.toLowerCase();
219
+ const isSrcEth = normalizedSrcToken.address === NULL_ADDRESS; // SwaapV2 uses zero address for native eth
220
+ const isSrcWeth = this.isWETH(normalizedSrcToken.address);
221
+
222
+ const isDestEth = normalizedDestToken.address === NULL_ADDRESS; // SwaapV2 uses zero address for native eth
223
+ const isDestWeth = this.isWETH(normalizedDestToken.address);
224
+
225
+ const poolIdentifiers = [
226
+ getPoolIdentifier(
227
+ this.dexKey,
228
+ normalizedSrcToken.address,
229
+ normalizedDestToken.address,
230
+ ),
231
+ ];
232
+
233
+ if (isSrcWeth) {
234
+ poolIdentifiers.push(
235
+ getPoolIdentifier(
236
+ this.dexKey,
237
+ NULL_ADDRESS,
238
+ normalizedDestToken.address,
239
+ ),
240
+ ); // native eth pool
241
+ }
242
+
243
+ if (isSrcEth) {
244
+ poolIdentifiers.push(this.dexKey, wethAddr, normalizedDestToken.address); // weth pool
245
+ }
246
+
247
+ if (isDestWeth) {
248
+ poolIdentifiers.push(
249
+ getPoolIdentifier(
250
+ this.dexKey,
251
+ normalizedSrcToken.address,
252
+ NULL_ADDRESS,
253
+ ),
254
+ ); // native eth pool
255
+ }
256
+
257
+ if (isDestEth) {
258
+ poolIdentifiers.push(
259
+ getPoolIdentifier(this.dexKey, normalizedSrcToken.address, wethAddr),
260
+ ); // weth pool
261
+ }
170
262
 
171
263
  const levels = await this.getCachedLevels();
172
264
  if (levels === null) {
173
265
  return [];
174
266
  }
175
267
 
176
- return Object.keys(levels)
177
- .map((pair: string) => {
178
- return getPoolIdentifier(
179
- this.dexKey,
180
- levels[pair].base!,
181
- levels[pair].quote!,
182
- );
183
- })
184
- .filter((pi: string) => pi === poolIdentifier);
268
+ const allPoolIds = Object.keys(levels).map((pair: string) => {
269
+ return getPoolIdentifier(
270
+ this.dexKey,
271
+ levels[pair].base!,
272
+ levels[pair].quote!,
273
+ );
274
+ });
275
+
276
+ return uniq(
277
+ allPoolIds.filter((pi: string) => poolIdentifiers.includes(pi)),
278
+ );
185
279
  }
186
280
 
187
281
  computePricesFromLevelsBids(
@@ -340,9 +434,17 @@ export class SwaapV2 extends SimpleExchange implements IDex<SwaapV2Data> {
340
434
  return null;
341
435
  }
342
436
 
437
+ const wethAddr =
438
+ this.dexHelper.config.data.wrappedNativeTokenAddress.toLowerCase();
439
+ const isSrcEth = normalizedSrcToken.address === NULL_ADDRESS; // SwaapV2 uses zero address for native eth
440
+ const isSrcWeth = this.isWETH(normalizedSrcToken.address);
441
+
442
+ const isDestEth = normalizedDestToken.address === NULL_ADDRESS; // SwaapV2 uses zero address for native eth
443
+ const isDestWeth = this.isWETH(normalizedDestToken.address);
444
+
343
445
  const pools = limitPools
344
- ? limitPools.filter(
345
- p =>
446
+ ? limitPools.filter(p => {
447
+ const isAppropriatePool =
346
448
  p ===
347
449
  getPoolIdentifier(
348
450
  this.dexKey,
@@ -354,8 +456,58 @@ export class SwaapV2 extends SimpleExchange implements IDex<SwaapV2Data> {
354
456
  this.dexKey,
355
457
  normalizedDestToken.address,
356
458
  normalizedSrcToken.address,
357
- ),
358
- )
459
+ );
460
+
461
+ if (isSrcEth) {
462
+ return (
463
+ isAppropriatePool ||
464
+ p ===
465
+ getPoolIdentifier(
466
+ this.dexKey,
467
+ wethAddr,
468
+ normalizedDestToken.address,
469
+ )
470
+ );
471
+ }
472
+
473
+ if (isSrcWeth) {
474
+ return (
475
+ isAppropriatePool ||
476
+ p ===
477
+ getPoolIdentifier(
478
+ this.dexKey,
479
+ NULL_ADDRESS,
480
+ normalizedDestToken.address,
481
+ )
482
+ );
483
+ }
484
+
485
+ if (isDestEth) {
486
+ return (
487
+ isAppropriatePool ||
488
+ p ===
489
+ getPoolIdentifier(
490
+ this.dexKey,
491
+ normalizedSrcToken.address,
492
+ wethAddr,
493
+ )
494
+ );
495
+ }
496
+
497
+ if (isDestWeth) {
498
+ return (
499
+ isAppropriatePool ||
500
+ p ===
501
+ getPoolIdentifier(
502
+ this.dexKey,
503
+ normalizedSrcToken.address,
504
+ NULL_ADDRESS,
505
+ )
506
+ );
507
+ }
508
+
509
+ return isAppropriatePool;
510
+ })
359
511
  : await this.getPoolIdentifiers(srcToken, destToken, side, blockNumber);
360
512
 
361
513
  if (pools.length === 0) {
@@ -370,14 +522,10 @@ export class SwaapV2 extends SimpleExchange implements IDex<SwaapV2Data> {
370
522
  const levelEntries: SwaapV2PriceLevels[] = Object.keys(levels)
371
523
  .filter(pair => {
372
524
  const { base, quote } = levels[pair];
373
- if (
525
+ return (
374
526
  pools.includes(getPoolIdentifier(this.dexKey, quote, base)) ||
375
527
  pools.includes(getPoolIdentifier(this.dexKey, base, quote))
376
- ) {
377
- return true;
378
- }
379
-
380
- return false;
528
+ );
381
529
  })
382
530
  .map(pair => {
383
531
  return levels[pair];
@@ -421,13 +569,38 @@ export class SwaapV2 extends SimpleExchange implements IDex<SwaapV2Data> {
421
569
  ? STABLE_SWAP_GAS_COST_ESTIMATION
422
570
  : VOLATILE_SWAP_GAS_COST_ESTIMATION;
423
571
 
572
+ const tokenIn =
573
+ normalizedSrcToken.address === NULL_ADDRESS &&
574
+ [askAndBids.base, askAndBids.quote].includes(wethAddr)
575
+ ? wethAddr
576
+ : normalizedSrcToken.address === wethAddr &&
577
+ [askAndBids.base, askAndBids.quote].includes(NULL_ADDRESS)
578
+ ? NULL_ADDRESS
579
+ : normalizedSrcToken.address;
580
+
581
+ const tokenOut =
582
+ normalizedDestToken.address === NULL_ADDRESS &&
583
+ [askAndBids.base, askAndBids.quote].includes(wethAddr)
584
+ ? wethAddr
585
+ : normalizedDestToken.address === wethAddr &&
586
+ [askAndBids.base, askAndBids.quote].includes(NULL_ADDRESS)
587
+ ? NULL_ADDRESS
588
+ : normalizedDestToken.address;
589
+
424
590
  return {
425
591
  gasCost: gasCost,
426
592
  exchange: this.dexKey,
427
- data: {},
593
+ data: {
594
+ tokenIn,
595
+ tokenOut,
596
+ },
428
597
  prices,
429
598
  unit: unitPrice === undefined ? 0n : unitPrice,
430
- poolIdentifier: requestedPoolIdentifier,
599
+ poolIdentifier: getPoolIdentifier(
600
+ this.dexKey,
601
+ askAndBids.base,
602
+ askAndBids.quote,
603
+ ),
431
604
  } as PoolPrices<SwaapV2Data>;
432
605
  });
433
606
 
@@ -453,6 +626,7 @@ export class SwaapV2 extends SimpleExchange implements IDex<SwaapV2Data> {
453
626
  recipient: string,
454
627
  data: SwaapV2Data,
455
628
  side: SwapSide,
629
+ context: Context,
456
630
  ): AsyncOrSync<DexExchangeParam> {
457
631
  const { router, callData } = data;
458
632
  const isBatchSwap = callData.slice(0, 10) === BATCH_SWAP_SELECTOR;
@@ -471,7 +645,16 @@ export class SwaapV2 extends SimpleExchange implements IDex<SwaapV2Data> {
471
645
  );
472
646
 
473
647
  return {
474
- needWrapNative: this.needWrapNative,
648
+ needWrapNative: this.needWrapNative(
649
+ context.priceRoute,
650
+ context.swap,
651
+ context.swapExchange,
652
+ ),
653
+ needUnwrapWeth: this.needUnwrapWeth(
654
+ context.priceRoute,
655
+ context.swap,
656
+ context.swapExchange,
657
+ ),
475
658
  specialDexSupportsInsertFromAmount: true,
476
659
  dexFuncHasRecipient: true,
477
660
  exchangeData: callData,
@@ -520,8 +703,8 @@ export class SwaapV2 extends SimpleExchange implements IDex<SwaapV2Data> {
520
703
  try {
521
704
  const quote = await this.rateFetcher.getQuote(
522
705
  this.network,
523
- normalizedSrcToken,
524
- normalizedDestToken,
706
+ { address: optimalSwapExchange.data!.tokenIn, decimals: 18 },
707
+ { address: optimalSwapExchange.data!.tokenOut, decimals: 18 },
525
708
  isSell ? optimalSwapExchange.srcAmount : optimalSwapExchange.destAmount,
526
709
  isSell ? SWAAP_ORDER_TYPE_SELL : SWAAP_ORDER_TYPE_BUY,
527
710
  options.userAddress,
@@ -628,6 +811,8 @@ export class SwaapV2 extends SimpleExchange implements IDex<SwaapV2Data> {
628
811
  {
629
812
  ...optimalSwapExchange,
630
813
  data: {
814
+ tokenIn: optimalSwapExchange.data!.tokenIn,
815
+ tokenOut: optimalSwapExchange.data!.tokenOut,
631
816
  router: quote.router,
632
817
  callData: quote.calldata,
633
818
  },
@@ -659,7 +844,7 @@ export class SwaapV2 extends SimpleExchange implements IDex<SwaapV2Data> {
659
844
  normalizedSrcToken.address,
660
845
  normalizedDestToken.address,
661
846
  );
662
- var message = 'Unknown error';
847
+ let message = 'Unknown error';
663
848
  if (e instanceof Error) {
664
849
  message = `${e.name}: ${e.message}`;
665
850
  }
@@ -5,6 +5,8 @@ import { Token } from '../../types';
5
5
  export type SwaapV2Data = {
6
6
  router: string;
7
7
  callData: string;
8
+ tokenIn: string;
9
+ tokenOut: string;
8
10
  };
9
11
 
10
12
  export type DexParams = {};
@@ -28,6 +28,8 @@ export class BaseTraderJoeV2
28
28
  {
29
29
  exchangeRouterInterface: Interface;
30
30
 
31
+ needWrapNative = false;
32
+
31
33
  constructor(
32
34
  dexHelper: IDexHelper,
33
35
  dexKey: string,
@@ -34,6 +34,8 @@ export class Weth
34
34
  readonly hasConstantPriceLargeAmounts = true;
35
35
  readonly isFeeOnTransferSupported = false;
36
36
 
37
+ needWrapNative = false;
38
+
37
39
  public static dexKeysWithNetwork: { key: string; networks: Network[] }[] =
38
40
  getDexKeysWithNetwork(WethConfig);
39
41