@paraswap/dex-lib 3.8.15 → 3.8.16-oswap

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 (240) hide show
  1. package/build/abi/{integral/pool.json → inception/inception-ineth-pool.json} +558 -537
  2. package/build/abi/inception/inception-ineth.json +609 -0
  3. package/build/abi/inception/inception-ratio-feed.json +93 -0
  4. package/build/abi/inception/inception-vault.json +991 -0
  5. package/build/abi/infusion/InfusionFactory.json +147 -0
  6. package/build/abi/infusion/InfusionPair.json +658 -0
  7. package/build/abi/infusion/InfusionRouter.json +442 -0
  8. package/build/dex/aave-v1/aave-v1.d.ts +2 -1
  9. package/build/dex/aave-v1/aave-v1.js +22 -0
  10. package/build/dex/aave-v1/aave-v1.js.map +1 -1
  11. package/build/dex/angle-transmuter/angle-transmuter.js +1 -1
  12. package/build/dex/angle-transmuter/angle-transmuter.js.map +1 -1
  13. package/build/dex/idex.d.ts +11 -7
  14. package/build/dex/idle-dao/idle-dao-pool.d.ts +56 -0
  15. package/build/dex/idle-dao/idle-dao-pool.js +176 -0
  16. package/build/dex/idle-dao/idle-dao-pool.js.map +1 -0
  17. package/build/dex/idle-dao/idle-dao-pooling-pool.d.ts +16 -0
  18. package/build/dex/idle-dao/idle-dao-pooling-pool.js +57 -0
  19. package/build/dex/idle-dao/idle-dao-pooling-pool.js.map +1 -0
  20. package/build/dex/idle-dao/scripts.d.ts +9 -0
  21. package/build/dex/idle-dao/scripts.js +552 -0
  22. package/build/dex/idle-dao/scripts.js.map +1 -0
  23. package/build/dex/inception/config.d.ts +5 -0
  24. package/build/dex/inception/config.js +112 -0
  25. package/build/dex/inception/config.js.map +1 -0
  26. package/build/dex/inception/inception-event-pool.d.ts +23 -0
  27. package/build/dex/inception/inception-event-pool.js +34 -0
  28. package/build/dex/inception/inception-event-pool.js.map +1 -0
  29. package/build/dex/{integral/integral.d.ts → inception/inception-native.d.ts} +21 -19
  30. package/build/dex/inception/inception-native.js +131 -0
  31. package/build/dex/inception/inception-native.js.map +1 -0
  32. package/build/dex/inception/inception-pool.d.ts +18 -0
  33. package/build/dex/inception/inception-pool.js +32 -0
  34. package/build/dex/inception/inception-pool.js.map +1 -0
  35. package/build/dex/inception/inception-price-feed.d.ts +19 -0
  36. package/build/dex/inception/inception-price-feed.js +51 -0
  37. package/build/dex/inception/inception-price-feed.js.map +1 -0
  38. package/build/dex/inception/inception.d.ts +44 -0
  39. package/build/dex/inception/inception.js +162 -0
  40. package/build/dex/inception/inception.js.map +1 -0
  41. package/build/dex/inception/tokens.d.ts +9 -0
  42. package/build/dex/inception/tokens.js +28 -0
  43. package/build/dex/inception/tokens.js.map +1 -0
  44. package/build/dex/inception/types.d.ts +22 -0
  45. package/build/dex/inception/types.js +3 -0
  46. package/build/dex/inception/types.js.map +1 -0
  47. package/build/dex/inception/utils.d.ts +7 -0
  48. package/build/dex/inception/utils.js +58 -0
  49. package/build/dex/inception/utils.js.map +1 -0
  50. package/build/dex/index.js +2 -0
  51. package/build/dex/index.js.map +1 -1
  52. package/build/dex/{integral → infusion}/config.d.ts +1 -1
  53. package/build/dex/infusion/config.js +25 -0
  54. package/build/dex/infusion/config.js.map +1 -0
  55. package/build/dex/infusion/infusion-stable-pool.d.ts +4 -0
  56. package/build/dex/infusion/infusion-stable-pool.js +74 -0
  57. package/build/dex/infusion/infusion-stable-pool.js.map +1 -0
  58. package/build/dex/infusion/infusion.d.ts +54 -0
  59. package/build/dex/infusion/infusion.js +510 -0
  60. package/build/dex/infusion/infusion.js.map +1 -0
  61. package/build/dex/infusion/types.d.ts +47 -0
  62. package/build/dex/infusion/types.js +3 -0
  63. package/build/dex/infusion/types.js.map +1 -0
  64. package/build/dex/infusion/utils/isStablePair.d.ts +2 -0
  65. package/build/dex/infusion/utils/isStablePair.js +18 -0
  66. package/build/dex/infusion/utils/isStablePair.js.map +1 -0
  67. package/build/dex/oswap/oswap.d.ts +2 -2
  68. package/build/dex/oswap/oswap.js +43 -36
  69. package/build/dex/oswap/oswap.js.map +1 -1
  70. package/build/dex/platypus/platypus.d.ts +2 -1
  71. package/build/dex/platypus/platypus.js +31 -0
  72. package/build/dex/platypus/platypus.js.map +1 -1
  73. package/build/dex/swaap-v1/swaap-v1.d.ts +2 -1
  74. package/build/dex/swaap-v1/swaap-v1.js +10 -0
  75. package/build/dex/swaap-v1/swaap-v1.js.map +1 -1
  76. package/build/dex/weth/config.js +3 -0
  77. package/build/dex/weth/config.js.map +1 -1
  78. package/package.json +1 -1
  79. package/src/abi/oswap/oswap.abi.json +452 -0
  80. package/src/dex/angle-transmuter/angle-transmuter.ts +1 -1
  81. package/src/dex/idex.ts +48 -41
  82. package/src/dex/index.ts +2 -0
  83. package/src/dex/oswap/config.ts +32 -0
  84. package/src/dex/oswap/oswap-e2e.test.ts +91 -0
  85. package/src/dex/oswap/oswap-events.test.ts +88 -0
  86. package/src/dex/oswap/oswap-integration.test.ts +269 -0
  87. package/src/dex/oswap/oswap-pool.ts +190 -0
  88. package/src/dex/oswap/oswap.ts +392 -0
  89. package/src/dex/oswap/types.ts +29 -0
  90. package/src/dex/weth/config.ts +3 -0
  91. package/src/dex/weth/weth-e2e.test.ts +156 -0
  92. package/.vscode/launch.json +0 -16
  93. package/.vscode/settings.json +0 -3
  94. package/build/abi/Uncompressor.json +0 -250
  95. package/build/abi/integral/factory.json +0 -333
  96. package/build/abi/integral/oracle.json +0 -501
  97. package/build/abi/integral/relayer.json +0 -1536
  98. package/build/abi/maverick-v2/MaverickV2Router.json +0 -778
  99. package/build/dex/bProtocol.d.ts +0 -18
  100. package/build/dex/bProtocol.js +0 -39
  101. package/build/dex/bProtocol.js.map +0 -1
  102. package/build/dex/bancor.d.ts +0 -26
  103. package/build/dex/bancor.js +0 -58
  104. package/build/dex/bancor.js.map +0 -1
  105. package/build/dex/compound.d.ts +0 -18
  106. package/build/dex/compound.js +0 -46
  107. package/build/dex/compound.js.map +0 -1
  108. package/build/dex/curve-v2.d.ts +0 -58
  109. package/build/dex/curve-v2.js +0 -180
  110. package/build/dex/curve-v2.js.map +0 -1
  111. package/build/dex/dodo-v1.d.ts +0 -33
  112. package/build/dex/dodo-v1.js +0 -63
  113. package/build/dex/dodo-v1.js.map +0 -1
  114. package/build/dex/etherfi/etherfi.d.ts +0 -26
  115. package/build/dex/etherfi/etherfi.js +0 -96
  116. package/build/dex/etherfi/etherfi.js.map +0 -1
  117. package/build/dex/integral/config.js +0 -20
  118. package/build/dex/integral/config.js.map +0 -1
  119. package/build/dex/integral/context.d.ts +0 -40
  120. package/build/dex/integral/context.js +0 -158
  121. package/build/dex/integral/context.js.map +0 -1
  122. package/build/dex/integral/helpers.d.ts +0 -17
  123. package/build/dex/integral/helpers.js +0 -157
  124. package/build/dex/integral/helpers.js.map +0 -1
  125. package/build/dex/integral/integral-factory.d.ts +0 -24
  126. package/build/dex/integral/integral-factory.js +0 -95
  127. package/build/dex/integral/integral-factory.js.map +0 -1
  128. package/build/dex/integral/integral-pool.d.ts +0 -50
  129. package/build/dex/integral/integral-pool.js +0 -149
  130. package/build/dex/integral/integral-pool.js.map +0 -1
  131. package/build/dex/integral/integral-pricing.d.ts +0 -18
  132. package/build/dex/integral/integral-pricing.js +0 -114
  133. package/build/dex/integral/integral-pricing.js.map +0 -1
  134. package/build/dex/integral/integral-relayer.d.ts +0 -42
  135. package/build/dex/integral/integral-relayer.js +0 -192
  136. package/build/dex/integral/integral-relayer.js.map +0 -1
  137. package/build/dex/integral/integral-token.d.ts +0 -27
  138. package/build/dex/integral/integral-token.js +0 -59
  139. package/build/dex/integral/integral-token.js.map +0 -1
  140. package/build/dex/integral/integral.js +0 -377
  141. package/build/dex/integral/integral.js.map +0 -1
  142. package/build/dex/integral/types.d.ts +0 -84
  143. package/build/dex/integral/types.js +0 -9
  144. package/build/dex/integral/types.js.map +0 -1
  145. package/build/dex/integral/utils-e2e.d.ts +0 -9
  146. package/build/dex/integral/utils-e2e.js +0 -131
  147. package/build/dex/integral/utils-e2e.js.map +0 -1
  148. package/build/dex/integral/utils.d.ts +0 -17
  149. package/build/dex/integral/utils.js +0 -94
  150. package/build/dex/integral/utils.js.map +0 -1
  151. package/build/dex/lido.d.ts +0 -19
  152. package/build/dex/lido.js +0 -42
  153. package/build/dex/lido.js.map +0 -1
  154. package/build/dex/onebit.d.ts +0 -25
  155. package/build/dex/onebit.js +0 -42
  156. package/build/dex/onebit.js.map +0 -1
  157. package/build/dex/sdai/config.d.ts +0 -11
  158. package/build/dex/sdai/config.js +0 -21
  159. package/build/dex/sdai/config.js.map +0 -1
  160. package/build/dex/sdai/constants.d.ts +0 -2
  161. package/build/dex/sdai/constants.js +0 -6
  162. package/build/dex/sdai/constants.js.map +0 -1
  163. package/build/dex/sdai/scripts/validate-state.d.ts +0 -1
  164. package/build/dex/sdai/scripts/validate-state.js +0 -102
  165. package/build/dex/sdai/scripts/validate-state.js.map +0 -1
  166. package/build/dex/sdai/sdai-pool.d.ts +0 -16
  167. package/build/dex/sdai/sdai-pool.js +0 -85
  168. package/build/dex/sdai/sdai-pool.js.map +0 -1
  169. package/build/dex/sdai/sdai.d.ts +0 -51
  170. package/build/dex/sdai/sdai.js +0 -172
  171. package/build/dex/sdai/sdai.js.map +0 -1
  172. package/build/dex/sdai/types.d.ts +0 -21
  173. package/build/dex/sdai/types.js +0 -11
  174. package/build/dex/sdai/types.js.map +0 -1
  175. package/build/dex/sdai/utils.d.ts +0 -4
  176. package/build/dex/sdai/utils.js +0 -39
  177. package/build/dex/sdai/utils.js.map +0 -1
  178. package/build/dex/smoothy.d.ts +0 -26
  179. package/build/dex/smoothy.js +0 -48
  180. package/build/dex/smoothy.js.map +0 -1
  181. package/build/dex/stable-pool.d.ts +0 -28
  182. package/build/dex/stable-pool.js +0 -62
  183. package/build/dex/stable-pool.js.map +0 -1
  184. package/build/dex/trader-joe-v2.1.d.ts +0 -43
  185. package/build/dex/trader-joe-v2.1.js +0 -79
  186. package/build/dex/trader-joe-v2.1.js.map +0 -1
  187. package/build/executor/compressor/Compressor.d.ts +0 -23
  188. package/build/executor/compressor/Compressor.js +0 -144
  189. package/build/executor/compressor/Compressor.js.map +0 -1
  190. package/build/executor/compressor/compress.d.ts +0 -17
  191. package/build/executor/compressor/compress.js +0 -187
  192. package/build/executor/compressor/compress.js.map +0 -1
  193. package/build/executor/compressor/compress_functions/addressSubstitution.d.ts +0 -6
  194. package/build/executor/compressor/compress_functions/addressSubstitution.js +0 -29
  195. package/build/executor/compressor/compress_functions/addressSubstitution.js.map +0 -1
  196. package/build/executor/compressor/compress_functions/addressSubstitutionFromObject.d.ts +0 -10
  197. package/build/executor/compressor/compress_functions/addressSubstitutionFromObject.js +0 -40
  198. package/build/executor/compressor/compress_functions/addressSubstitutionFromObject.js.map +0 -1
  199. package/build/executor/compressor/compress_functions/byteSubstitution.d.ts +0 -11
  200. package/build/executor/compressor/compress_functions/byteSubstitution.js +0 -64
  201. package/build/executor/compressor/compress_functions/byteSubstitution.js.map +0 -1
  202. package/build/executor/compressor/compress_functions/ffSubstitution.d.ts +0 -7
  203. package/build/executor/compressor/compress_functions/ffSubstitution.js +0 -54
  204. package/build/executor/compressor/compress_functions/ffSubstitution.js.map +0 -1
  205. package/build/executor/compressor/compress_functions/findLongestAndSubstitute.d.ts +0 -8
  206. package/build/executor/compressor/compress_functions/findLongestAndSubstitute.js +0 -67
  207. package/build/executor/compressor/compress_functions/findLongestAndSubstitute.js.map +0 -1
  208. package/build/executor/compressor/compress_functions/findLongestAndSubstitute_old.d.ts +0 -8
  209. package/build/executor/compressor/compress_functions/findLongestAndSubstitute_old.js +0 -61
  210. package/build/executor/compressor/compress_functions/findLongestAndSubstitute_old.js.map +0 -1
  211. package/build/executor/compressor/compress_functions/reverseAddressSubstitution.d.ts +0 -6
  212. package/build/executor/compressor/compress_functions/reverseAddressSubstitution.js +0 -37
  213. package/build/executor/compressor/compress_functions/reverseAddressSubstitution.js.map +0 -1
  214. package/build/executor/compressor/compress_functions/searchUnusedBytes.d.ts +0 -9
  215. package/build/executor/compressor/compress_functions/searchUnusedBytes.js +0 -33
  216. package/build/executor/compressor/compress_functions/searchUnusedBytes.js.map +0 -1
  217. package/build/executor/compressor/fetchSaved.d.ts +0 -0
  218. package/build/executor/compressor/fetchSaved.js +0 -17
  219. package/build/executor/compressor/fetchSaved.js.map +0 -1
  220. package/build/executor/compressor/utils/computeCostL2.d.ts +0 -2
  221. package/build/executor/compressor/utils/computeCostL2.js +0 -14
  222. package/build/executor/compressor/utils/computeCostL2.js.map +0 -1
  223. package/build/executor/compressor/utils/findLongestDuplicatedString.d.ts +0 -2
  224. package/build/executor/compressor/utils/findLongestDuplicatedString.js +0 -30
  225. package/build/executor/compressor/utils/findLongestDuplicatedString.js.map +0 -1
  226. package/build/executor/compressor/utils/getAllIndexes.d.ts +0 -2
  227. package/build/executor/compressor/utils/getAllIndexes.js +0 -12
  228. package/build/executor/compressor/utils/getAllIndexes.js.map +0 -1
  229. package/build/executor/compressor/utils/getByteForAddress.d.ts +0 -2
  230. package/build/executor/compressor/utils/getByteForAddress.js +0 -14
  231. package/build/executor/compressor/utils/getByteForAddress.js.map +0 -1
  232. package/build/executor/compressor/utils/intTo2Bytes.d.ts +0 -2
  233. package/build/executor/compressor/utils/intTo2Bytes.js +0 -14
  234. package/build/executor/compressor/utils/intTo2Bytes.js.map +0 -1
  235. package/build/executor/compressor/utils/intTo3Bytes.d.ts +0 -2
  236. package/build/executor/compressor/utils/intTo3Bytes.js +0 -18
  237. package/build/executor/compressor/utils/intTo3Bytes.js.map +0 -1
  238. package/build/executor/compressor/utils/intToByte.d.ts +0 -2
  239. package/build/executor/compressor/utils/intToByte.js +0 -10
  240. package/build/executor/compressor/utils/intToByte.js.map +0 -1
@@ -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
+ ? state.traderate0
145
+ : 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 <= state.balance1
174
+ : needed <= state.balance0;
175
+ }
176
+ // SwapSide.BUY
177
+ return from.address.toLowerCase() === pool.token0
178
+ ? amount <= state.balance1
179
+ : amount <= 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
+ state.balance0,
363
+ );
364
+ const usd1 = await this.dexHelper.getTokenUSDPrice(
365
+ { address: pool.token1, decimals: 18 },
366
+ 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: bigint;
7
+ traderate1: bigint;
8
+ balance0: bigint;
9
+ balance1: bigint;
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
+ };
@@ -73,4 +73,7 @@ export const Adapters: {
73
73
  [Network.BASE]: {
74
74
  [SwapSide.SELL]: [{ name: 'BaseAdapter02', index: 2 }],
75
75
  },
76
+ [Network.ZKEVM]: {
77
+ [SwapSide.SELL]: [{ name: 'PolygonZkEvmAdapter01', index: 3 }],
78
+ },
76
79
  };
@@ -873,4 +873,160 @@ describe('Weth E2E', () => {
873
873
  });
874
874
  });
875
875
  });
876
+
877
+ describe('Weth ZKEvm', () => {
878
+ const dexKey = 'Weth';
879
+ const network = Network.ZKEVM;
880
+
881
+ const nativeTokenSymbol = 'ETH';
882
+ const wrappedTokenSymbol = 'WETH';
883
+
884
+ const nativeAmount = '1000000000000000000';
885
+ const wrappedAmount = '1000000000000000000';
886
+
887
+ const tokens = Tokens[network];
888
+ const holders = Holders[network];
889
+ const provider = new StaticJsonRpcProvider(
890
+ generateConfig(network).privateHttpProvider,
891
+ network,
892
+ );
893
+
894
+ const nativeToken = tokens[nativeTokenSymbol];
895
+ const wrappedToken = tokens[wrappedTokenSymbol];
896
+
897
+ const nativeHolder = holders[nativeTokenSymbol];
898
+ const wrappedHolder = holders[wrappedTokenSymbol];
899
+
900
+ describe('SimpleSwap SELL', () => {
901
+ const contractMethod = ContractMethod.simpleSwap;
902
+ const side = SwapSide.SELL;
903
+
904
+ it('native -> wrapped', async () => {
905
+ await testE2E(
906
+ nativeToken,
907
+ wrappedToken,
908
+ nativeHolder,
909
+ nativeAmount,
910
+ side,
911
+ dexKey,
912
+ contractMethod,
913
+ network,
914
+ provider,
915
+ undefined,
916
+ undefined,
917
+ undefined,
918
+ undefined,
919
+ undefined,
920
+ true,
921
+ );
922
+ });
923
+ it('wrapped -> native', async () => {
924
+ await testE2E(
925
+ wrappedToken,
926
+ nativeToken,
927
+ wrappedHolder,
928
+ wrappedAmount,
929
+ side,
930
+ dexKey,
931
+ contractMethod,
932
+ network,
933
+ provider,
934
+ undefined,
935
+ undefined,
936
+ undefined,
937
+ undefined,
938
+ undefined,
939
+ true,
940
+ );
941
+ });
942
+ });
943
+
944
+ describe('MultiSwap SELL', () => {
945
+ const contractMethod = ContractMethod.multiSwap;
946
+ const side = SwapSide.SELL;
947
+
948
+ it('native -> wrapped', async () => {
949
+ await testE2E(
950
+ nativeToken,
951
+ wrappedToken,
952
+ nativeHolder,
953
+ nativeAmount,
954
+ side,
955
+ dexKey,
956
+ contractMethod,
957
+ network,
958
+ provider,
959
+ undefined,
960
+ undefined,
961
+ undefined,
962
+ undefined,
963
+ undefined,
964
+ true,
965
+ );
966
+ });
967
+ it('wrapped -> native', async () => {
968
+ await testE2E(
969
+ wrappedToken,
970
+ nativeToken,
971
+ wrappedHolder,
972
+ wrappedAmount,
973
+ side,
974
+ dexKey,
975
+ contractMethod,
976
+ network,
977
+ provider,
978
+ undefined,
979
+ undefined,
980
+ undefined,
981
+ undefined,
982
+ undefined,
983
+ true,
984
+ );
985
+ });
986
+ });
987
+
988
+ describe('MegaSwap SELL', () => {
989
+ const contractMethod = ContractMethod.megaSwap;
990
+ const side = SwapSide.SELL;
991
+
992
+ it('native -> wrapped', async () => {
993
+ await testE2E(
994
+ nativeToken,
995
+ wrappedToken,
996
+ nativeHolder,
997
+ nativeAmount,
998
+ side,
999
+ dexKey,
1000
+ contractMethod,
1001
+ network,
1002
+ provider,
1003
+ undefined,
1004
+ undefined,
1005
+ undefined,
1006
+ undefined,
1007
+ undefined,
1008
+ true,
1009
+ );
1010
+ });
1011
+ it('wrapped -> native', async () => {
1012
+ await testE2E(
1013
+ wrappedToken,
1014
+ nativeToken,
1015
+ wrappedHolder,
1016
+ wrappedAmount,
1017
+ side,
1018
+ dexKey,
1019
+ contractMethod,
1020
+ network,
1021
+ provider,
1022
+ undefined,
1023
+ undefined,
1024
+ undefined,
1025
+ undefined,
1026
+ undefined,
1027
+ true,
1028
+ );
1029
+ });
1030
+ });
1031
+ });
876
1032
  });
@@ -1,16 +0,0 @@
1
- {
2
- "version": "0.2.0",
3
- "configurations": [
4
- {
5
- "args": [
6
- "${workspaceFolder}/src/dex/maverick-v2/maverick-v2-integration.test.ts"
7
- ],
8
- "internalConsoleOptions": "openOnSessionStart",
9
- "name": "Dex-lib",
10
- "program": "${workspaceFolder}/node_modules/jest/bin/jest",
11
- "request": "launch",
12
- "skipFiles": ["<node_internals>/**"],
13
- "type": "node"
14
- }
15
- ]
16
- }
@@ -1,3 +0,0 @@
1
- {
2
- "typescript.tsdk": "node_modules/typescript/lib"
3
- }