@paraswap/dex-lib 3.8.15 → 3.8.16-oswap.1

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 (118) hide show
  1. package/build/abi/lite-psm/dai.json +333 -0
  2. package/build/abi/lite-psm/psm.json +468 -0
  3. package/build/abi/lite-psm/usdc.json +814 -0
  4. package/build/config.js +1 -0
  5. package/build/config.js.map +1 -1
  6. package/build/dex/angle-transmuter/angle-transmuter.js +1 -1
  7. package/build/dex/angle-transmuter/angle-transmuter.js.map +1 -1
  8. package/build/dex/dexalot/config.js +3 -0
  9. package/build/dex/dexalot/config.js.map +1 -1
  10. package/build/dex/idex.d.ts +11 -7
  11. package/build/dex/index.js +6 -0
  12. package/build/dex/index.js.map +1 -1
  13. package/build/dex/integral/integral.js +8 -9
  14. package/build/dex/integral/integral.js.map +1 -1
  15. package/build/dex/integral/types.d.ts +1 -0
  16. package/build/dex/integral/types.js.map +1 -1
  17. package/build/dex/lite-psm/config.d.ts +3 -0
  18. package/build/dex/lite-psm/config.js +29 -0
  19. package/build/dex/lite-psm/config.js.map +1 -0
  20. package/build/dex/lite-psm/lite-psm-event-pool.d.ts +29 -0
  21. package/build/dex/lite-psm/lite-psm-event-pool.js +164 -0
  22. package/build/dex/lite-psm/lite-psm-event-pool.js.map +1 -0
  23. package/build/dex/lite-psm/lite-psm.d.ts +52 -0
  24. package/build/dex/lite-psm/lite-psm.js +341 -0
  25. package/build/dex/lite-psm/lite-psm.js.map +1 -0
  26. package/build/dex/lite-psm/types.d.ts +40 -0
  27. package/build/dex/lite-psm/types.js +3 -0
  28. package/build/dex/lite-psm/types.js.map +1 -0
  29. package/build/dex/maker-psm/maker-psm.js +4 -1
  30. package/build/dex/maker-psm/maker-psm.js.map +1 -1
  31. package/build/dex/maverick-v2/config.js +4 -0
  32. package/build/dex/maverick-v2/config.js.map +1 -1
  33. package/build/dex/maverick-v2/maverick-math/maverick-pool-math.js +4 -0
  34. package/build/dex/maverick-v2/maverick-math/maverick-pool-math.js.map +1 -1
  35. package/build/dex/maverick-v2/maverick-v2-pool.d.ts +1 -1
  36. package/build/dex/maverick-v2/maverick-v2-pool.js +8 -8
  37. package/build/dex/maverick-v2/maverick-v2-pool.js.map +1 -1
  38. package/build/dex/maverick-v2/maverick-v2.d.ts +5 -4
  39. package/build/dex/maverick-v2/maverick-v2.js +73 -63
  40. package/build/dex/maverick-v2/maverick-v2.js.map +1 -1
  41. package/build/dex/maverick-v2/types.d.ts +4 -0
  42. package/build/dex/oswap/oswap-pool.js +8 -8
  43. package/build/dex/oswap/oswap-pool.js.map +1 -1
  44. package/build/dex/oswap/oswap.d.ts +2 -2
  45. package/build/dex/oswap/oswap.js +51 -44
  46. package/build/dex/oswap/oswap.js.map +1 -1
  47. package/build/dex/oswap/types.d.ts +4 -4
  48. package/build/dex/solidly-v3/solidly-v3.js +7 -0
  49. package/build/dex/solidly-v3/solidly-v3.js.map +1 -1
  50. package/build/dex/trader-joe-v2.1/base.d.ts +18 -0
  51. package/build/dex/trader-joe-v2.1/base.js +88 -0
  52. package/build/dex/trader-joe-v2.1/base.js.map +1 -0
  53. package/build/dex/trader-joe-v2.1/config.d.ts +3 -0
  54. package/build/dex/trader-joe-v2.1/config.js +7 -3
  55. package/build/dex/trader-joe-v2.1/config.js.map +1 -1
  56. package/build/dex/trader-joe-v2.1/optimizer.d.ts +2 -0
  57. package/build/dex/trader-joe-v2.1/optimizer.js +44 -0
  58. package/build/dex/trader-joe-v2.1/optimizer.js.map +1 -0
  59. package/build/dex/trader-joe-v2.1/trader-joe-v2.1.d.ts +2 -14
  60. package/build/dex/trader-joe-v2.1/trader-joe-v2.1.js +3 -85
  61. package/build/dex/trader-joe-v2.1/trader-joe-v2.1.js.map +1 -1
  62. package/build/dex/trader-joe-v2.1/trader-joe-v2.2.d.ts +7 -0
  63. package/build/dex/trader-joe-v2.1/trader-joe-v2.2.js +20 -0
  64. package/build/dex/trader-joe-v2.1/trader-joe-v2.2.js.map +1 -0
  65. package/build/dex/trader-joe-v2.1/types.d.ts +3 -3
  66. package/build/dex/weth/config.js +3 -0
  67. package/build/dex/weth/config.js.map +1 -1
  68. package/build/dex-helper/dummy-dex-helper.js +1 -1
  69. package/build/dex-helper/dummy-dex-helper.js.map +1 -1
  70. package/build/executor/types.d.ts +2 -1
  71. package/build/executor/types.js +1 -0
  72. package/build/executor/types.js.map +1 -1
  73. package/package.json +1 -1
  74. package/src/abi/lite-psm/dai.json +333 -0
  75. package/src/abi/lite-psm/psm.json +468 -0
  76. package/src/abi/lite-psm/usdc.json +814 -0
  77. package/src/abi/maverick-v2/MaverickV2Router.json +778 -0
  78. package/src/abi/oswap/oswap.abi.json +452 -0
  79. package/src/config.ts +1 -0
  80. package/src/dex/angle-transmuter/angle-transmuter.ts +1 -1
  81. package/src/dex/dexalot/config.ts +3 -0
  82. package/src/dex/dexalot/dexalot-e2e.test.ts +23 -1
  83. package/src/dex/dexalot/dexalot-integration.test.ts +122 -0
  84. package/src/dex/idex.ts +48 -41
  85. package/src/dex/index.ts +6 -0
  86. package/src/dex/lite-psm/config.ts +29 -0
  87. package/src/dex/lite-psm/lite-psm-e2e.test.ts +79 -0
  88. package/src/dex/lite-psm/lite-psm-event-pool.ts +216 -0
  89. package/src/dex/lite-psm/lite-psm-events.test.ts +61 -0
  90. package/src/dex/lite-psm/lite-psm-integration.test.ts +99 -0
  91. package/src/dex/lite-psm/lite-psm.ts +523 -0
  92. package/src/dex/lite-psm/types.ts +46 -0
  93. package/src/dex/maker-psm/maker-psm.ts +4 -1
  94. package/src/dex/maverick-v2/config.ts +4 -0
  95. package/src/dex/maverick-v2/maverick-math/maverick-pool-math.ts +5 -0
  96. package/src/dex/maverick-v2/maverick-v2-e2e.test.ts +21 -43
  97. package/src/dex/maverick-v2/maverick-v2-integration.test.ts +100 -96
  98. package/src/dex/maverick-v2/maverick-v2-pool.ts +8 -8
  99. package/src/dex/maverick-v2/maverick-v2.ts +111 -81
  100. package/src/dex/maverick-v2/types.ts +4 -0
  101. package/src/dex/oswap/config.ts +32 -0
  102. package/src/dex/oswap/oswap-e2e.test.ts +91 -0
  103. package/src/dex/oswap/oswap-events.test.ts +88 -0
  104. package/src/dex/oswap/oswap-integration.test.ts +269 -0
  105. package/src/dex/oswap/oswap-pool.ts +190 -0
  106. package/src/dex/oswap/oswap.ts +392 -0
  107. package/src/dex/oswap/types.ts +29 -0
  108. package/src/dex/solidly-v3/solidly-v3.ts +7 -0
  109. package/src/dex/trader-joe-v2.1/base.ts +167 -0
  110. package/src/dex/trader-joe-v2.1/config.ts +7 -2
  111. package/src/dex/trader-joe-v2.1/trader-joe-v2.1.ts +8 -165
  112. package/src/dex/trader-joe-v2.1/trader-joe-v2.2.ts +23 -0
  113. package/src/dex/trader-joe-v2.1/types.ts +3 -3
  114. package/src/dex/weth/config.ts +3 -0
  115. package/src/dex/weth/weth-e2e.test.ts +156 -0
  116. package/src/dex-helper/dummy-dex-helper.ts +1 -1
  117. package/src/executor/types.ts +1 -0
  118. package/tests/constants-e2e.ts +9 -0
@@ -0,0 +1,392 @@
1
+ import { Interface } from '@ethersproject/abi';
2
+ import { AsyncOrSync } from 'ts-essentials';
3
+ import {
4
+ Token,
5
+ Address,
6
+ ExchangePrices,
7
+ PoolPrices,
8
+ AdapterExchangeParam,
9
+ PoolLiquidity,
10
+ Logger,
11
+ NumberAsString,
12
+ DexExchangeParam,
13
+ } from '../../types';
14
+ import { SwapSide, Network } from '../../constants';
15
+ import * as CALLDATA_GAS_COST from '../../calldata-gas-cost';
16
+ import { getDexKeysWithNetwork, getBigIntPow } from '../../utils';
17
+ import { Context, IDex } from '../../dex/idex';
18
+ import { IDexHelper } from '../../dex-helper/idex-helper';
19
+ import { OSwapData, OSwapPool, OSwapPoolState } from './types';
20
+ import {
21
+ SimpleExchange,
22
+ getLocalDeadlineAsFriendlyPlaceholder,
23
+ } from '../simple-exchange';
24
+ import { OSwapConfig, Adapters, OSWAP_GAS_COST } from './config';
25
+ import { OSwapEventPool } from './oswap-pool';
26
+ import OSwapABI from '../../abi/oswap/oswap.abi.json';
27
+ import { extractReturnAmountPosition } from '../../executor/utils';
28
+
29
+ export class OSwap extends SimpleExchange implements IDex<OSwapData> {
30
+ readonly eventPools: { [id: string]: OSwapEventPool } = {};
31
+
32
+ readonly hasConstantPriceLargeAmounts = false;
33
+
34
+ // This may change in the future, but currently OSwap does not support native ETH.
35
+ readonly needWrapNative = true;
36
+
37
+ readonly isFeeOnTransferSupported = false;
38
+
39
+ public static dexKeysWithNetwork: { key: string; networks: Network[] }[] =
40
+ getDexKeysWithNetwork(OSwapConfig);
41
+
42
+ logger: Logger;
43
+
44
+ readonly iOSwap: Interface;
45
+
46
+ readonly pools: [OSwapPool];
47
+
48
+ constructor(
49
+ readonly network: Network,
50
+ readonly dexKey: string,
51
+ readonly dexHelper: IDexHelper,
52
+ protected adapters = Adapters[network] || {},
53
+ ) {
54
+ super(dexHelper, dexKey);
55
+ this.logger = dexHelper.getLogger(dexKey);
56
+ this.iOSwap = new Interface(OSwapABI);
57
+
58
+ this.pools = OSwapConfig[dexKey][network].pools;
59
+
60
+ // Create an OSwapEventPool per pool, to track each pool's state by subscribing to on-chain events.
61
+ for (const pool of this.pools) {
62
+ this.eventPools[pool.id] = new OSwapEventPool(
63
+ dexKey,
64
+ pool,
65
+ network,
66
+ dexHelper,
67
+ this.logger,
68
+ );
69
+ }
70
+ }
71
+
72
+ // Returns the list of contract adapters (name and index)
73
+ // for a buy/sell. Return null if there are no adapters.
74
+ getAdapters(side: SwapSide): { name: string; index: number }[] | null {
75
+ return null;
76
+ }
77
+
78
+ // Returns the pool matching the specified token pair or null if none found.
79
+ // Note: OSwap V1 does not support more than 1 pool per pair.
80
+ getPoolByTokenPair(srcToken: Token, destToken: Token): OSwapPool | null {
81
+ const srcAddress = srcToken.address.toLowerCase();
82
+ const destAddress = destToken.address.toLowerCase();
83
+
84
+ // A pair must have 2 different tokens.
85
+ if (srcAddress === destAddress) return null;
86
+
87
+ for (const pool of this.pools) {
88
+ if (
89
+ (srcAddress === pool.token0 && destAddress === pool.token1) ||
90
+ (srcAddress === pool.token1 && destAddress === pool.token0)
91
+ ) {
92
+ return pool;
93
+ }
94
+ }
95
+ return null;
96
+ }
97
+
98
+ getPoolById(id: string): OSwapPool | null {
99
+ for (const pool of this.pools) {
100
+ if (pool.id === id) return pool;
101
+ }
102
+ return null;
103
+ }
104
+
105
+ // Returns a list of pool using the token.
106
+ getPoolsByTokenAddress(tokenAddress: Address): OSwapPool[] {
107
+ const address = tokenAddress.toLowerCase();
108
+ let pools: OSwapPool[] = [];
109
+ for (const pool of this.pools) {
110
+ if (address === pool.token0 || address === pool.token1) {
111
+ pools.push(pool);
112
+ }
113
+ }
114
+ return pools;
115
+ }
116
+
117
+ // Returns the list of pool identifiers that can be used
118
+ // for a given swap. poolIdentifiers must be unique
119
+ // across DEXes. It is recommended to use
120
+ // ${dexKey}_${poolAddress} as a poolIdentifier
121
+ async getPoolIdentifiers(
122
+ srcToken: Token,
123
+ destToken: Token,
124
+ side: SwapSide,
125
+ blockNumber: number,
126
+ ): Promise<string[]> {
127
+ const pool = this.getPoolByTokenPair(srcToken, destToken);
128
+ return pool ? [pool.id] : [];
129
+ }
130
+
131
+ // Sell: Given "amount" of "from" token, how much of "to" token will be received by the trader.
132
+ // Buy: Given "amount" of "dest" token, how much of "to" token is required from the trader.
133
+ // Note: OSwap traderate is at precision 36.
134
+ calcPrice(
135
+ pool: OSwapPool,
136
+ state: OSwapPoolState,
137
+ from: Token,
138
+ amount: bigint,
139
+ side: SwapSide,
140
+ checkLiquidity = true,
141
+ ): bigint {
142
+ const rate =
143
+ from.address.toLowerCase() === pool.token0
144
+ ? BigInt(state.traderate0)
145
+ : BigInt(state.traderate1);
146
+
147
+ const price =
148
+ side === SwapSide.SELL
149
+ ? (amount * rate) / getBigIntPow(36)
150
+ : (amount * getBigIntPow(36)) / rate;
151
+
152
+ if (
153
+ checkLiquidity &&
154
+ !this.hasEnoughLiquidity(pool, state, from, amount, price, side)
155
+ ) {
156
+ throw new Error('Not enough liquidity');
157
+ }
158
+
159
+ return price;
160
+ }
161
+
162
+ // Returns true if the pool has enough liquidity for the swap. False otherwise.
163
+ hasEnoughLiquidity(
164
+ pool: OSwapPool,
165
+ state: OSwapPoolState,
166
+ from: Token,
167
+ amount: bigint,
168
+ needed: bigint,
169
+ side: SwapSide,
170
+ ): boolean {
171
+ if (side === SwapSide.SELL) {
172
+ return from.address.toLowerCase() === pool.token0
173
+ ? needed <= BigInt(state.balance1)
174
+ : needed <= BigInt(state.balance0);
175
+ }
176
+ // SwapSide.BUY
177
+ return from.address.toLowerCase() === pool.token0
178
+ ? amount <= BigInt(state.balance1)
179
+ : amount <= BigInt(state.balance0);
180
+ }
181
+
182
+ // Returns pool prices for amounts.
183
+ // If limitPools is defined only pools in limitPools
184
+ // should be used. If limitPools is undefined then
185
+ // any pool can be used.
186
+ async getPricesVolume(
187
+ srcToken: Token,
188
+ destToken: Token,
189
+ amounts: bigint[],
190
+ side: SwapSide,
191
+ blockNumber: number,
192
+ limitPools?: string[],
193
+ ): Promise<null | ExchangePrices<OSwapData>> {
194
+ try {
195
+ // Get the pool to use.
196
+ const pool = this.getPoolByTokenPair(srcToken, destToken);
197
+ if (!pool) return null;
198
+
199
+ // Make sure the pool meets the optional limitPools filter.
200
+ if (limitPools && !limitPools.includes(pool.id)) return null;
201
+
202
+ const eventPool = this.eventPools[pool.id];
203
+
204
+ if (!eventPool) {
205
+ this.logger.error(`OSwap pool ${pool.id}: No EventPool found.`);
206
+
207
+ return null;
208
+ }
209
+
210
+ const state = await eventPool.getStateOrGenerate(blockNumber);
211
+
212
+ // Calculate the prices
213
+ const unitAmount = getBigIntPow(18);
214
+ const unitPrice = this.calcPrice(
215
+ pool,
216
+ state,
217
+ srcToken,
218
+ unitAmount,
219
+ side,
220
+ false,
221
+ );
222
+ const prices = amounts.map(amount =>
223
+ this.calcPrice(pool, state, srcToken, amount, side),
224
+ );
225
+
226
+ return [
227
+ {
228
+ prices,
229
+ unit: unitPrice,
230
+ data: {
231
+ pool: pool.address,
232
+ path: [srcToken.address, destToken.address],
233
+ },
234
+ exchange: this.dexKey,
235
+ poolIdentifier: pool.id,
236
+ gasCost: OSWAP_GAS_COST,
237
+ poolAddresses: [pool.address],
238
+ },
239
+ ];
240
+ } catch (e) {
241
+ this.logger.error(
242
+ `Error_getPricesVolume ${srcToken.address || srcToken.symbol}, ${
243
+ destToken.address || destToken.symbol
244
+ }, ${side}:`,
245
+ e,
246
+ );
247
+
248
+ return null;
249
+ }
250
+ }
251
+
252
+ // Returns estimated gas cost of calldata for this DEX in multiSwap
253
+ getCalldataGasCost(poolPrices: PoolPrices<OSwapData>): number | number[] {
254
+ return (
255
+ CALLDATA_GAS_COST.DEX_OVERHEAD +
256
+ // ParentStruct header
257
+ CALLDATA_GAS_COST.OFFSET_SMALL +
258
+ // ParentStruct -> path[] header
259
+ CALLDATA_GAS_COST.OFFSET_SMALL +
260
+ // ParentStruct -> path length
261
+ CALLDATA_GAS_COST.LENGTH_SMALL +
262
+ // ParentStruct -> path[0]
263
+ CALLDATA_GAS_COST.ADDRESS +
264
+ // ParentStruct -> path[1]
265
+ CALLDATA_GAS_COST.ADDRESS +
266
+ // ParentStruct -> receiver header
267
+ CALLDATA_GAS_COST.OFFSET_SMALL +
268
+ // ParentStruct -> receiver
269
+ CALLDATA_GAS_COST.ADDRESS
270
+ );
271
+ }
272
+
273
+ // Encode params required by the exchange adapter
274
+ // Used for multiSwap, buy & megaSwap
275
+ getAdapterParam(
276
+ srcToken: string,
277
+ destToken: string,
278
+ srcAmount: string,
279
+ destAmount: string,
280
+ data: OSwapData,
281
+ side: SwapSide,
282
+ ): AdapterExchangeParam {
283
+ return {
284
+ targetExchange: data.pool,
285
+ payload: '',
286
+ networkFee: '0',
287
+ };
288
+ }
289
+
290
+ // Encode call data used by simpleSwap like routers
291
+ // Used for simpleSwap & simpleBuy
292
+ getDexParam(
293
+ srcToken: Address,
294
+ destToken: Address,
295
+ srcAmount: NumberAsString,
296
+ destAmount: NumberAsString,
297
+ recipient: Address,
298
+ data: OSwapData,
299
+ side: SwapSide,
300
+ _: Context,
301
+ executorAddress: Address,
302
+ ): DexExchangeParam {
303
+ let method: string;
304
+ let args: any;
305
+ let returnAmountPos: number | undefined = undefined;
306
+
307
+ const deadline = getLocalDeadlineAsFriendlyPlaceholder();
308
+ if (side === SwapSide.SELL) {
309
+ method = 'swapExactTokensForTokens';
310
+ returnAmountPos = extractReturnAmountPosition(
311
+ this.iOSwap,
312
+ method,
313
+ 'amounts',
314
+ 1,
315
+ );
316
+ args = [srcAmount, destAmount, data.path, recipient, deadline];
317
+ } else {
318
+ method = 'swapTokensForExactTokens';
319
+ args = [destAmount, srcAmount, data.path, recipient, deadline];
320
+ }
321
+
322
+ const swapData = this.iOSwap.encodeFunctionData(method, args);
323
+
324
+ return {
325
+ needWrapNative: this.needWrapNative,
326
+ dexFuncHasRecipient: true,
327
+ exchangeData: swapData,
328
+ targetExchange: data.pool,
329
+ returnAmountPos,
330
+ };
331
+ }
332
+
333
+ // This is called once before getTopPoolsForToken is
334
+ // called for multiple tokens. This can be helpful to
335
+ // update common state required for calculating
336
+ // getTopPoolsForToken. It is optional for a DEX
337
+ // to implement this
338
+ async updatePoolState(): Promise<void> {
339
+ return Promise.resolve();
340
+ }
341
+
342
+ // Returns a list of top pools based on liquidity. Max
343
+ // limit number pools should be returned.
344
+ async getTopPoolsForToken(
345
+ tokenAddress: Address,
346
+ limit: number,
347
+ ): Promise<PoolLiquidity[]> {
348
+ // Get the list of pools using the token.
349
+ const pools = this.getPoolsByTokenAddress(tokenAddress);
350
+ if (!pools.length) return [];
351
+
352
+ const results = await Promise.all<PoolLiquidity>(
353
+ pools.map(async pool => {
354
+ // Get the pool's balance and its USD value.
355
+ const eventPool = this.eventPools[pool.id];
356
+ const blockNumber =
357
+ await this.dexHelper.web3Provider.eth.getBlockNumber();
358
+ const state = await eventPool.getStateOrGenerate(blockNumber);
359
+
360
+ const usd0 = await this.dexHelper.getTokenUSDPrice(
361
+ { address: pool.token0, decimals: 18 },
362
+ BigInt(state.balance0),
363
+ );
364
+ const usd1 = await this.dexHelper.getTokenUSDPrice(
365
+ { address: pool.token1, decimals: 18 },
366
+ BigInt(state.balance1),
367
+ );
368
+
369
+ // Get the other token in the pair.
370
+ const pairedToken =
371
+ pool.token0 === tokenAddress.toLowerCase()
372
+ ? { address: pool.token1, decimals: 18 }
373
+ : { address: pool.token0, decimals: 18 };
374
+
375
+ return {
376
+ exchange: this.dexKey,
377
+ address: pool.address,
378
+ connectorTokens: [pairedToken],
379
+ liquidityUSD: usd0 + usd1,
380
+ };
381
+ }),
382
+ );
383
+ return results
384
+ .filter(r => r)
385
+ .sort((a, b) => a.liquidityUSD - b.liquidityUSD)
386
+ .slice(0, limit);
387
+ }
388
+
389
+ // This is optional function in case if your implementation has acquired any resources
390
+ // you need to release for graceful shutdown. For example, it may be any interval timer
391
+ releaseResources(): AsyncOrSync<void> {}
392
+ }
@@ -0,0 +1,29 @@
1
+ import { Address } from '../../types';
2
+
3
+ // OSwapPoolState is the state of the event subscriber. It is the minimum
4
+ // set of parameters required to compute pool prices.
5
+ export type OSwapPoolState = {
6
+ traderate0: string;
7
+ traderate1: string;
8
+ balance0: string;
9
+ balance1: string;
10
+ };
11
+
12
+ // OSwapPoolState is the state of the event subscriber. It is the minimum
13
+ // set of parameters required to compute pool prices.
14
+ export type OSwapData = {
15
+ pool: Address;
16
+ path: Address[];
17
+ };
18
+
19
+ // Each pool has a contract address and token pairs.
20
+ export type OSwapPool = {
21
+ id: string;
22
+ address: Address;
23
+ token0: Address;
24
+ token1: Address;
25
+ };
26
+
27
+ export type DexParams = {
28
+ pools: [OSwapPool];
29
+ };
@@ -49,6 +49,7 @@ import { Contract } from 'web3-eth-contract';
49
49
  import { AbiItem } from 'web3-utils';
50
50
  import { TickMath } from './contract-math/TickMath';
51
51
  import { OnPoolCreatedCallback, SolidlyV3Factory } from './solidly-v3-factory';
52
+ import { SpecialDex } from '../../executor/types';
52
53
 
53
54
  type PoolPairsInfo = {
54
55
  token0: Address;
@@ -727,6 +728,12 @@ export class SolidlyV3
727
728
  exchangeData: swapData,
728
729
  targetExchange: data.poolAddress,
729
730
  returnAmountPos: undefined,
731
+ ...(side === SwapSide.BUY
732
+ ? {
733
+ specialDexSupportsInsertFromAmount: true,
734
+ specialDexFlag: SpecialDex.BUY_ON_SOLIDLY_V3,
735
+ }
736
+ : {}),
730
737
  };
731
738
  }
732
739
 
@@ -0,0 +1,167 @@
1
+ import { SwapSide } from '../../constants';
2
+ import {
3
+ AdapterExchangeParam,
4
+ Address,
5
+ DexExchangeParam,
6
+ SimpleExchangeParam,
7
+ } from '../../types';
8
+ import { IDexTxBuilder } from '../idex';
9
+ import { IDexHelper } from '../../dex-helper';
10
+ import {
11
+ getLocalDeadlineAsFriendlyPlaceholder,
12
+ SimpleExchange,
13
+ } from '../simple-exchange';
14
+ import { NumberAsString } from '@paraswap/core';
15
+ import { AsyncOrSync } from 'ts-essentials';
16
+ import { Interface, JsonFragment } from '@ethersproject/abi';
17
+ import TraderJoeV21RouterABI from '../../abi/TraderJoeV21Router.json';
18
+ import {
19
+ TraderJoeV2Data,
20
+ TraderJoeV2RouterFunctions,
21
+ TraderJoeV2RouterParam,
22
+ } from './types';
23
+ import { extractReturnAmountPosition } from '../../executor/utils';
24
+
25
+ export class BaseTraderJoeV2
26
+ extends SimpleExchange
27
+ implements IDexTxBuilder<TraderJoeV2Data, TraderJoeV2RouterParam>
28
+ {
29
+ exchangeRouterInterface: Interface;
30
+
31
+ constructor(
32
+ dexHelper: IDexHelper,
33
+ dexKey: string,
34
+ protected routerAddress: string,
35
+ protected versionIndex: string,
36
+ ) {
37
+ super(dexHelper, dexKey);
38
+
39
+ this.exchangeRouterInterface = new Interface(
40
+ TraderJoeV21RouterABI as JsonFragment[],
41
+ );
42
+ }
43
+
44
+ getAdapterParam(
45
+ srcToken: Address,
46
+ destToken: Address,
47
+ srcAmount: NumberAsString,
48
+ destAmount: NumberAsString,
49
+ data: TraderJoeV2Data,
50
+ side: SwapSide,
51
+ ): AdapterExchangeParam {
52
+ let payload = this.abiCoder.encodeParameters(
53
+ ['tuple(tuple(uint256[],uint8[],address[]),uint256)'],
54
+ [
55
+ [
56
+ [data.binSteps, data.versions, data.tokenPath],
57
+ getLocalDeadlineAsFriendlyPlaceholder(), // _deadline: uint256
58
+ ],
59
+ ],
60
+ );
61
+
62
+ return {
63
+ targetExchange: this.routerAddress,
64
+ payload,
65
+ networkFee: '0',
66
+ };
67
+ }
68
+
69
+ getSimpleParam(
70
+ srcToken: Address,
71
+ destToken: Address,
72
+ srcAmount: NumberAsString,
73
+ destAmount: NumberAsString,
74
+ data: TraderJoeV2Data,
75
+ side: SwapSide,
76
+ ): AsyncOrSync<SimpleExchangeParam> {
77
+ const swapFunction =
78
+ side === SwapSide.SELL
79
+ ? TraderJoeV2RouterFunctions.swapExactTokensForTokens
80
+ : TraderJoeV2RouterFunctions.swapTokensForExactTokens;
81
+
82
+ const swapFunctionParams: TraderJoeV2RouterParam =
83
+ side === SwapSide.SELL
84
+ ? [
85
+ srcAmount,
86
+ destAmount,
87
+ [data.binSteps, data.versions, data.tokenPath],
88
+ this.augustusAddress,
89
+ getLocalDeadlineAsFriendlyPlaceholder(),
90
+ ]
91
+ : [
92
+ destAmount,
93
+ srcAmount,
94
+ [data.binSteps, data.versions, data.tokenPath],
95
+ this.augustusAddress,
96
+ getLocalDeadlineAsFriendlyPlaceholder(),
97
+ ];
98
+
99
+ const swapData = this.exchangeRouterInterface.encodeFunctionData(
100
+ swapFunction,
101
+ swapFunctionParams,
102
+ );
103
+
104
+ return this.buildSimpleParamWithoutWETHConversion(
105
+ srcToken,
106
+ srcAmount,
107
+ destToken,
108
+ destAmount,
109
+ swapData,
110
+ this.routerAddress,
111
+ );
112
+ }
113
+
114
+ getDexParam(
115
+ srcToken: Address,
116
+ destToken: Address,
117
+ srcAmount: NumberAsString,
118
+ destAmount: NumberAsString,
119
+ recipient: Address,
120
+ data: TraderJoeV2Data,
121
+ side: SwapSide,
122
+ ): DexExchangeParam {
123
+ const swapFunction =
124
+ side === SwapSide.SELL
125
+ ? TraderJoeV2RouterFunctions.swapExactTokensForTokens
126
+ : TraderJoeV2RouterFunctions.swapTokensForExactTokens;
127
+
128
+ const placeholder = getLocalDeadlineAsFriendlyPlaceholder();
129
+
130
+ const swapFunctionParams: TraderJoeV2RouterParam =
131
+ side === SwapSide.SELL
132
+ ? [
133
+ srcAmount,
134
+ destAmount,
135
+ [data.binSteps, data.versions, data.tokenPath],
136
+ recipient,
137
+ placeholder,
138
+ ]
139
+ : [
140
+ destAmount,
141
+ srcAmount,
142
+ [data.binSteps, data.versions, data.tokenPath],
143
+ recipient,
144
+ placeholder,
145
+ ];
146
+
147
+ const swapData = this.exchangeRouterInterface.encodeFunctionData(
148
+ swapFunction,
149
+ swapFunctionParams,
150
+ );
151
+
152
+ return {
153
+ needWrapNative: this.needWrapNative,
154
+ dexFuncHasRecipient: true,
155
+ exchangeData: swapData,
156
+ targetExchange: this.routerAddress,
157
+ returnAmountPos:
158
+ side === SwapSide.SELL
159
+ ? extractReturnAmountPosition(
160
+ this.exchangeRouterInterface,
161
+ swapFunction,
162
+ 'amountOut',
163
+ )
164
+ : undefined,
165
+ };
166
+ }
167
+ }
@@ -2,8 +2,13 @@ import { Address } from '@paraswap/core';
2
2
  import { Network } from '../../constants';
3
3
 
4
4
  export const TRADERJOE_V2_1_ROUTER_ADDRESS: { [network: number]: Address } = {
5
- [Network.AVALANCHE]: '0xb4315e873dBcf96Ffd0acd8EA43f689D8c20fB30',
6
- [Network.ARBITRUM]: '0xb4315e873dBcf96Ffd0acd8EA43f689D8c20fB30',
5
+ [Network.AVALANCHE]: '0x18556da13313f3532c54711497a8fedac273220e',
6
+ [Network.ARBITRUM]: '0x18556da13313f3532c54711497a8fedac273220e',
7
7
  [Network.BSC]: '0xb4315e873dBcf96Ffd0acd8EA43f689D8c20fB30',
8
8
  [Network.MAINNET]: '0x9A93a421b74F1c5755b83dD2C211614dC419C44b',
9
9
  };
10
+
11
+ export const TRADERJOE_V2_2_ROUTER_ADDRESS: { [network: number]: Address } = {
12
+ [Network.AVALANCHE]: '0x18556da13313f3532c54711497a8fedac273220e',
13
+ [Network.ARBITRUM]: '0x18556da13313f3532c54711497a8fedac273220e',
14
+ };