@paraswap/dex-lib 3.8.14 → 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 (219) hide show
  1. package/build/abi/concentrator/arUSD.json +568 -0
  2. package/build/abi/concentrator/arUSD5115.json +1746 -0
  3. package/build/abi/fx-protocol/FxMarket.json +1284 -0
  4. package/build/abi/{wombat/pool.json → fx-protocol/FxUSD.json} +541 -603
  5. package/build/abi/fx-protocol/weETHOralce.json +676 -0
  6. package/build/abi/inception/inception-ineth-pool.json +1062 -0
  7. package/build/abi/inception/inception-ineth.json +609 -0
  8. package/build/abi/inception/inception-ratio-feed.json +93 -0
  9. package/build/abi/inception/inception-vault.json +991 -0
  10. package/build/abi/infusion/InfusionFactory.json +147 -0
  11. package/build/abi/infusion/InfusionPair.json +658 -0
  12. package/build/abi/infusion/InfusionRouter.json +442 -0
  13. package/build/abi/oswap/oswap.abi.json +452 -0
  14. package/build/dex/aave-v1/aave-v1.js +0 -1
  15. package/build/dex/aave-v1/aave-v1.js.map +1 -1
  16. package/build/dex/angle-transmuter/angle-transmuter.js +1 -1
  17. package/build/dex/angle-transmuter/angle-transmuter.js.map +1 -1
  18. package/build/dex/concentrator-arusd/concentrator-arusd-event.d.ts +17 -0
  19. package/build/dex/concentrator-arusd/concentrator-arusd-event.js +66 -0
  20. package/build/dex/concentrator-arusd/concentrator-arusd-event.js.map +1 -0
  21. package/build/dex/concentrator-arusd/concentrator-arusd.d.ts +41 -0
  22. package/build/dex/concentrator-arusd/concentrator-arusd.js +188 -0
  23. package/build/dex/concentrator-arusd/concentrator-arusd.js.map +1 -0
  24. package/build/dex/concentrator-arusd/config.d.ts +3 -0
  25. package/build/dex/concentrator-arusd/config.js +14 -0
  26. package/build/dex/concentrator-arusd/config.js.map +1 -0
  27. package/build/dex/concentrator-arusd/types.d.ts +14 -0
  28. package/build/dex/concentrator-arusd/types.js +3 -0
  29. package/build/dex/concentrator-arusd/types.js.map +1 -0
  30. package/build/dex/concentrator-arusd/utils.d.ts +4 -0
  31. package/build/dex/concentrator-arusd/utils.js +27 -0
  32. package/build/dex/concentrator-arusd/utils.js.map +1 -0
  33. package/build/dex/fx-protocol-rusd/config.d.ts +3 -0
  34. package/build/dex/fx-protocol-rusd/config.js +15 -0
  35. package/build/dex/fx-protocol-rusd/config.js.map +1 -0
  36. package/build/dex/fx-protocol-rusd/fx-protocol-rusd-event.d.ts +18 -0
  37. package/build/dex/fx-protocol-rusd/fx-protocol-rusd-event.js +71 -0
  38. package/build/dex/fx-protocol-rusd/fx-protocol-rusd-event.js.map +1 -0
  39. package/build/dex/fx-protocol-rusd/fx-protocol-rusd.d.ts +39 -0
  40. package/build/dex/fx-protocol-rusd/fx-protocol-rusd.js +201 -0
  41. package/build/dex/fx-protocol-rusd/fx-protocol-rusd.js.map +1 -0
  42. package/build/dex/fx-protocol-rusd/types.d.ts +13 -0
  43. package/build/dex/fx-protocol-rusd/types.js +3 -0
  44. package/build/dex/fx-protocol-rusd/types.js.map +1 -0
  45. package/build/dex/fx-protocol-rusd/utils.d.ts +4 -0
  46. package/build/dex/fx-protocol-rusd/utils.js +26 -0
  47. package/build/dex/fx-protocol-rusd/utils.js.map +1 -0
  48. package/build/dex/idex.d.ts +11 -7
  49. package/build/dex/idle-dao/idle-dao-pool.d.ts +56 -0
  50. package/build/dex/idle-dao/idle-dao-pool.js +176 -0
  51. package/build/dex/idle-dao/idle-dao-pool.js.map +1 -0
  52. package/build/dex/idle-dao/idle-dao-pooling-pool.d.ts +16 -0
  53. package/build/dex/idle-dao/idle-dao-pooling-pool.js +57 -0
  54. package/build/dex/idle-dao/idle-dao-pooling-pool.js.map +1 -0
  55. package/build/dex/idle-dao/scripts.d.ts +9 -0
  56. package/build/dex/idle-dao/scripts.js +552 -0
  57. package/build/dex/idle-dao/scripts.js.map +1 -0
  58. package/build/dex/inception/config.d.ts +5 -0
  59. package/build/dex/inception/config.js +112 -0
  60. package/build/dex/inception/config.js.map +1 -0
  61. package/build/dex/inception/inception-event-pool.d.ts +23 -0
  62. package/build/dex/inception/inception-event-pool.js +34 -0
  63. package/build/dex/inception/inception-event-pool.js.map +1 -0
  64. package/build/dex/inception/inception-native.d.ts +43 -0
  65. package/build/dex/inception/inception-native.js +131 -0
  66. package/build/dex/inception/inception-native.js.map +1 -0
  67. package/build/dex/inception/inception-pool.d.ts +18 -0
  68. package/build/dex/inception/inception-pool.js +32 -0
  69. package/build/dex/inception/inception-pool.js.map +1 -0
  70. package/build/dex/inception/inception-price-feed.d.ts +19 -0
  71. package/build/dex/inception/inception-price-feed.js +51 -0
  72. package/build/dex/inception/inception-price-feed.js.map +1 -0
  73. package/build/dex/inception/inception.d.ts +44 -0
  74. package/build/dex/inception/inception.js +162 -0
  75. package/build/dex/inception/inception.js.map +1 -0
  76. package/build/dex/inception/tokens.d.ts +9 -0
  77. package/build/dex/inception/tokens.js +28 -0
  78. package/build/dex/inception/tokens.js.map +1 -0
  79. package/build/dex/inception/types.d.ts +22 -0
  80. package/build/dex/inception/types.js +3 -0
  81. package/build/dex/inception/types.js.map +1 -0
  82. package/build/dex/inception/utils.d.ts +7 -0
  83. package/build/dex/inception/utils.js +58 -0
  84. package/build/dex/inception/utils.js.map +1 -0
  85. package/build/dex/index.js +6 -0
  86. package/build/dex/index.js.map +1 -1
  87. package/build/dex/infusion/config.d.ts +4 -0
  88. package/build/dex/infusion/config.js +25 -0
  89. package/build/dex/infusion/config.js.map +1 -0
  90. package/build/dex/infusion/infusion-stable-pool.d.ts +4 -0
  91. package/build/dex/infusion/infusion-stable-pool.js +74 -0
  92. package/build/dex/infusion/infusion-stable-pool.js.map +1 -0
  93. package/build/dex/infusion/infusion.d.ts +54 -0
  94. package/build/dex/infusion/infusion.js +510 -0
  95. package/build/dex/infusion/infusion.js.map +1 -0
  96. package/build/dex/infusion/types.d.ts +47 -0
  97. package/build/dex/infusion/types.js +3 -0
  98. package/build/dex/infusion/types.js.map +1 -0
  99. package/build/dex/infusion/utils/isStablePair.d.ts +2 -0
  100. package/build/dex/infusion/utils/isStablePair.js +18 -0
  101. package/build/dex/infusion/utils/isStablePair.js.map +1 -0
  102. package/build/dex/oswap/config.d.ts +5 -0
  103. package/build/dex/oswap/config.js +31 -0
  104. package/build/dex/oswap/config.js.map +1 -0
  105. package/build/dex/oswap/oswap-pool.d.ts +51 -0
  106. package/build/dex/oswap/oswap-pool.js +150 -0
  107. package/build/dex/oswap/oswap-pool.js.map +1 -0
  108. package/build/dex/oswap/oswap.d.ts +46 -0
  109. package/build/dex/oswap/oswap.js +273 -0
  110. package/build/dex/oswap/oswap.js.map +1 -0
  111. package/build/dex/oswap/types.d.ts +20 -0
  112. package/build/dex/oswap/types.js +3 -0
  113. package/build/dex/oswap/types.js.map +1 -0
  114. package/build/dex/platypus/platypus.js +3 -17
  115. package/build/dex/platypus/platypus.js.map +1 -1
  116. package/build/dex/swaap-v1/swaap-v1.js +0 -4
  117. package/build/dex/swaap-v1/swaap-v1.js.map +1 -1
  118. package/build/dex/weth/config.js +3 -0
  119. package/build/dex/weth/config.js.map +1 -1
  120. package/package.json +1 -1
  121. package/src/abi/concentrator/arUSD.json +568 -0
  122. package/src/abi/concentrator/arUSD5115.json +1746 -0
  123. package/src/abi/fx-protocol/FxMarket.json +1284 -0
  124. package/src/abi/fx-protocol/FxUSD.json +1227 -0
  125. package/src/abi/fx-protocol/weETHOralce.json +676 -0
  126. package/src/abi/oswap/oswap.abi.json +452 -0
  127. package/src/dex/angle-transmuter/angle-transmuter.ts +1 -1
  128. package/src/dex/concentrator-arusd/concentrator-arusd-e2e.test.ts +92 -0
  129. package/src/dex/concentrator-arusd/concentrator-arusd-event.ts +100 -0
  130. package/src/dex/concentrator-arusd/concentrator-arusd-integration.test.ts +104 -0
  131. package/src/dex/concentrator-arusd/concentrator-arusd.ts +269 -0
  132. package/src/dex/concentrator-arusd/concetrator-arusd-events.test.ts +97 -0
  133. package/src/dex/concentrator-arusd/config.ts +13 -0
  134. package/src/dex/concentrator-arusd/types.ts +16 -0
  135. package/src/dex/concentrator-arusd/utils.ts +36 -0
  136. package/src/dex/fx-protocol-rusd/config.ts +14 -0
  137. package/src/dex/fx-protocol-rusd/fx-protocol-e2e.test.ts +96 -0
  138. package/src/dex/fx-protocol-rusd/fx-protocol-events.test.ts +101 -0
  139. package/src/dex/fx-protocol-rusd/fx-protocol-integration.test.ts +104 -0
  140. package/src/dex/fx-protocol-rusd/fx-protocol-rusd-event.ts +112 -0
  141. package/src/dex/fx-protocol-rusd/fx-protocol-rusd.ts +282 -0
  142. package/src/dex/fx-protocol-rusd/types.ts +16 -0
  143. package/src/dex/fx-protocol-rusd/utils.ts +47 -0
  144. package/src/dex/idex.ts +48 -41
  145. package/src/dex/index.ts +6 -0
  146. package/src/dex/oswap/config.ts +32 -0
  147. package/src/dex/oswap/oswap-e2e.test.ts +91 -0
  148. package/src/dex/oswap/oswap-events.test.ts +88 -0
  149. package/src/dex/oswap/oswap-integration.test.ts +269 -0
  150. package/src/dex/oswap/oswap-pool.ts +190 -0
  151. package/src/dex/oswap/oswap.ts +392 -0
  152. package/src/dex/oswap/types.ts +29 -0
  153. package/src/dex/weth/config.ts +3 -0
  154. package/src/dex/weth/weth-e2e.test.ts +156 -0
  155. package/src/dex/wsteth/wsteth-integration.test.ts +0 -1
  156. package/tests/constants-e2e.ts +10 -0
  157. package/.vscode/launch.json +0 -53
  158. package/.vscode/settings.json +0 -2
  159. package/.vscode/tasks.json +0 -15
  160. package/build/abi/maverick-v2/MaverickV2Router.json +0 -778
  161. package/build/abi/uniswap-v3/AlienBaseV3Router.abi.json +0 -52
  162. package/build/dex/aave-v2/tokens-avalanche.json +0 -44
  163. package/build/dex/aave-v2/tokens-mainnet.json +0 -188
  164. package/build/dex/aave-v2/tokens-polygon.json +0 -44
  165. package/build/dex/bProtocol.d.ts +0 -18
  166. package/build/dex/bProtocol.js +0 -39
  167. package/build/dex/bProtocol.js.map +0 -1
  168. package/build/dex/bancor.d.ts +0 -26
  169. package/build/dex/bancor.js +0 -58
  170. package/build/dex/bancor.js.map +0 -1
  171. package/build/dex/compound.d.ts +0 -18
  172. package/build/dex/compound.js +0 -46
  173. package/build/dex/compound.js.map +0 -1
  174. package/build/dex/curve-v2.d.ts +0 -58
  175. package/build/dex/curve-v2.js +0 -180
  176. package/build/dex/curve-v2.js.map +0 -1
  177. package/build/dex/dodo-v1.d.ts +0 -33
  178. package/build/dex/dodo-v1.js +0 -63
  179. package/build/dex/dodo-v1.js.map +0 -1
  180. package/build/dex/lido.d.ts +0 -19
  181. package/build/dex/lido.js +0 -42
  182. package/build/dex/lido.js.map +0 -1
  183. package/build/dex/maker-psm/scripts/gem.abi.json +0 -136
  184. package/build/dex/maker-psm/scripts/validate-state.d.ts +0 -0
  185. package/build/dex/maker-psm/scripts/validate-state.js +0 -185
  186. package/build/dex/maker-psm/scripts/validate-state.js.map +0 -1
  187. package/build/dex/maker-psm/scripts/vat.abi.json +0 -243
  188. package/build/dex/onebit.d.ts +0 -25
  189. package/build/dex/onebit.js +0 -42
  190. package/build/dex/onebit.js.map +0 -1
  191. package/build/dex/smoothy.d.ts +0 -26
  192. package/build/dex/smoothy.js +0 -48
  193. package/build/dex/smoothy.js.map +0 -1
  194. package/build/dex/solidly-v3/scripts/check-event-pool-event.d.ts +0 -1
  195. package/build/dex/solidly-v3/scripts/check-event-pool-event.js +0 -53
  196. package/build/dex/solidly-v3/scripts/check-event-pool-event.js.map +0 -1
  197. package/build/dex/stable-pool.d.ts +0 -28
  198. package/build/dex/stable-pool.js +0 -55
  199. package/build/dex/stable-pool.js.map +0 -1
  200. package/build/dex/trader-joe-v2.1/base.d.ts +0 -18
  201. package/build/dex/trader-joe-v2.1/base.js +0 -99
  202. package/build/dex/trader-joe-v2.1/base.js.map +0 -1
  203. package/build/dex/trader-joe-v2.1/trader-joe-v2.1-new.d.ts +0 -7
  204. package/build/dex/trader-joe-v2.1/trader-joe-v2.1-new.js +0 -20
  205. package/build/dex/trader-joe-v2.1/trader-joe-v2.1-new.js.map +0 -1
  206. package/build/dex/trader-joe-v2.1/trader-joe-v2.2.d.ts +0 -7
  207. package/build/dex/trader-joe-v2.1/trader-joe-v2.2.js +0 -20
  208. package/build/dex/trader-joe-v2.1/trader-joe-v2.2.js.map +0 -1
  209. package/build/dex/trader-joe-v2.1.d.ts +0 -43
  210. package/build/dex/trader-joe-v2.1.js +0 -79
  211. package/build/dex/trader-joe-v2.1.js.map +0 -1
  212. package/build/dex/uniswap-v3/forks/alien-base-v3/alien-base-v3.d.ts +0 -17
  213. package/build/dex/uniswap-v3/forks/alien-base-v3/alien-base-v3.js +0 -64
  214. package/build/dex/uniswap-v3/forks/alien-base-v3/alien-base-v3.js.map +0 -1
  215. package/build/dex/uniswap-v3/forks/alien-v3/alien-v3.d.ts +0 -17
  216. package/build/dex/uniswap-v3/forks/alien-v3/alien-v3.js +0 -79
  217. package/build/dex/uniswap-v3/forks/alien-v3/alien-v3.js.map +0 -1
  218. package/config.ts +0 -1946
  219. package/src/dex/dexs.txt +0 -58
package/src/dex/idex.ts CHANGED
@@ -24,34 +24,7 @@ export type Context = {
24
24
  isGlobalDestToken: boolean;
25
25
  };
26
26
 
27
- export interface IDexTxBuilder<ExchangeData, DirectParam = null> {
28
- needWrapNative: boolean;
29
- needsSequentialPreprocessing?: boolean;
30
-
31
- // Returns the ETH fee required to swap
32
- // It is optional for a DEX to implement this
33
- // This is a legacy function and will be removed soon
34
- getNetworkFee?(
35
- srcToken: Address,
36
- destToken: Address,
37
- srcAmount: NumberAsString,
38
- destAmount: NumberAsString,
39
- data: ExchangeData,
40
- side: SwapSide,
41
- ): NumberAsString;
42
-
43
- // If exists, called before getAdapterParam to use async calls and receive data if needed
44
- preProcessTransaction?(
45
- optimalSwapExchange: OptimalSwapExchange<ExchangeData>,
46
- srcToken: Token,
47
- destToken: Token,
48
- side: SwapSide,
49
- options: PreprocessTransactionOptions,
50
- ): AsyncOrSync<[OptimalSwapExchange<ExchangeData>, ExchangeTxInfo]>;
51
-
52
- // This is helper a function to support testing if preProcessTransaction is implemented
53
- getTokenFromAddress?(address: Address): Token;
54
-
27
+ export interface IDexTxBuilderV5<ExchangeData, DirectParam = null> {
55
28
  // Encode params required by the exchange adapter
56
29
  // V5: Used for multiSwap, buy & megaSwap
57
30
  // V6: NOT used, pass a placeholder
@@ -76,18 +49,6 @@ export interface IDexTxBuilder<ExchangeData, DirectParam = null> {
76
49
  side: SwapSide,
77
50
  ): AsyncOrSync<SimpleExchangeParam>;
78
51
 
79
- getDexParam?(
80
- srcToken: Address,
81
- destToken: Address,
82
- srcAmount: NumberAsString,
83
- destAmount: NumberAsString,
84
- recipient: Address,
85
- data: ExchangeData,
86
- side: SwapSide,
87
- context: Context,
88
- executorAddress: Address,
89
- ): AsyncOrSync<DexExchangeParam>;
90
-
91
52
  // Returns params required by direct swap method.
92
53
  // Only Dexes which have a direct method should implement this
93
54
  // Used if there is a possibility for direct swap (Eg. UniswapV2,
@@ -108,8 +69,23 @@ export interface IDexTxBuilder<ExchangeData, DirectParam = null> {
108
69
  beneficiary: string,
109
70
  contractMethod: string,
110
71
  ): TxInfo<DirectParam>;
72
+ }
111
73
 
112
- // Same as above but for V6
74
+ export interface IDexTxBuilderV6<ExchangeData, DirectParam = null> {
75
+ // Returns params required by generic method
76
+ getDexParam?(
77
+ srcToken: Address,
78
+ destToken: Address,
79
+ srcAmount: NumberAsString,
80
+ destAmount: NumberAsString,
81
+ recipient: Address,
82
+ data: ExchangeData,
83
+ side: SwapSide,
84
+ context: Context,
85
+ executorAddress: Address,
86
+ ): AsyncOrSync<DexExchangeParam>;
87
+
88
+ // Returns params for direct method
113
89
  getDirectParamV6?(
114
90
  srcToken: Address,
115
91
  destToken: Address,
@@ -127,6 +103,37 @@ export interface IDexTxBuilder<ExchangeData, DirectParam = null> {
127
103
  ): TxInfo<DirectParam>;
128
104
  }
129
105
 
106
+ export interface IDexTxBuilder<ExchangeData, DirectParam = null>
107
+ extends IDexTxBuilderV5<ExchangeData, DirectParam>,
108
+ IDexTxBuilderV6<ExchangeData, DirectParam> {
109
+ needWrapNative: boolean;
110
+ needsSequentialPreprocessing?: boolean;
111
+
112
+ // Returns the ETH fee required to swap
113
+ // It is optional for a DEX to implement this
114
+ // This is a legacy function and will be removed soon
115
+ getNetworkFee?(
116
+ srcToken: Address,
117
+ destToken: Address,
118
+ srcAmount: NumberAsString,
119
+ destAmount: NumberAsString,
120
+ data: ExchangeData,
121
+ side: SwapSide,
122
+ ): NumberAsString;
123
+
124
+ // If exists, called before getAdapterParam to use async calls and receive data if needed
125
+ preProcessTransaction?(
126
+ optimalSwapExchange: OptimalSwapExchange<ExchangeData>,
127
+ srcToken: Token,
128
+ destToken: Token,
129
+ side: SwapSide,
130
+ options: PreprocessTransactionOptions,
131
+ ): AsyncOrSync<[OptimalSwapExchange<ExchangeData>, ExchangeTxInfo]>;
132
+
133
+ // This is helper a function to support testing if preProcessTransaction is implemented
134
+ getTokenFromAddress?(address: Address): Token;
135
+ }
136
+
130
137
  export interface IDexPricing<ExchangeData> {
131
138
  readonly dexKey: string;
132
139
  // This is true if the the DEX is simply
package/src/dex/index.ts CHANGED
@@ -81,6 +81,9 @@ import { EtherFi } from './etherfi';
81
81
  import { Spark } from './spark/spark';
82
82
  import { VelodromeSlipstream } from './uniswap-v3/forks/velodrome-slipstream/velodrome-slipstream';
83
83
  import { AaveV3Stata } from './aave-v3-stata/aave-v3-stata';
84
+ import { OSwap } from './oswap/oswap';
85
+ import { ConcentratorArusd } from './concentrator-arusd/concentrator-arusd';
86
+ import { FxProtocolRusd } from './fx-protocol-rusd/fx-protocol-rusd';
84
87
 
85
88
  const LegacyDexes = [
86
89
  CurveV2,
@@ -157,6 +160,9 @@ const Dexes = [
157
160
  PharaohV1,
158
161
  Spark,
159
162
  AaveV3Stata,
163
+ OSwap,
164
+ ConcentratorArusd,
165
+ FxProtocolRusd,
160
166
  ];
161
167
 
162
168
  export type LegacyDexConstructor = new (dexHelper: IDexHelper) => IDexTxBuilder<
@@ -0,0 +1,32 @@
1
+ import { DexParams } from './types';
2
+ import { DexConfigMap, AdapterMappings } from '../../types';
3
+ import { Network, SwapSide } from '../../constants';
4
+
5
+ export const OSWAP_GAS_COST = 80_000;
6
+
7
+ // Important:
8
+ // - All addresses should be lower case.
9
+ // - Only tokens with 18 decimals are supported.
10
+ export const OSwapConfig: DexConfigMap<DexParams> = {
11
+ OSwap: {
12
+ [Network.MAINNET]: {
13
+ pools: [
14
+ {
15
+ id: 'OSwap_0x85b78aca6deae198fbf201c82daf6ca21942acc6', // Pool identifier: `{dex_key}_{pool_address}`
16
+ address: '0x85b78aca6deae198fbf201c82daf6ca21942acc6', // Address of the pool
17
+ token0: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2', // WETH
18
+ token1: '0xae7ab96520de3a18e5e111b5eaab095312d7fe84', // STETH
19
+ },
20
+ ],
21
+ },
22
+ },
23
+ };
24
+
25
+ export const Adapters: Record<number, AdapterMappings> = {
26
+ [Network.MAINNET]: {
27
+ // Note: We re-use the SmarDex adapters since it implements
28
+ // an Uniswap V2 router compatible interface, which OSwap supports.
29
+ [SwapSide.SELL]: [{ name: 'Adapter04', index: 6 }],
30
+ [SwapSide.BUY]: [{ name: 'BuyAdapter02', index: 2 }],
31
+ },
32
+ };
@@ -0,0 +1,91 @@
1
+ /* eslint-disable no-console */
2
+ import dotenv from 'dotenv';
3
+ dotenv.config();
4
+
5
+ import { testE2E } from '../../../tests/utils-e2e';
6
+ import { Tokens, Holders } from '../../../tests/constants-e2e';
7
+ import { Network, ContractMethod, SwapSide } from '../../constants';
8
+ import { StaticJsonRpcProvider } from '@ethersproject/providers';
9
+ import { generateConfig } from '../../config';
10
+
11
+ function testForNetwork(
12
+ network: Network,
13
+ dexKey: string,
14
+ tokenASymbol: string,
15
+ tokenBSymbol: string,
16
+ tokenAAmount: string,
17
+ tokenBAmount: string,
18
+ ) {
19
+ const provider = new StaticJsonRpcProvider(
20
+ generateConfig(network).privateHttpProvider,
21
+ network,
22
+ );
23
+ const tokens = Tokens[network];
24
+ const holders = Holders[network];
25
+
26
+ const sideToContractMethods = new Map([
27
+ [SwapSide.SELL, [ContractMethod.swapExactAmountIn]],
28
+ [SwapSide.BUY, [ContractMethod.swapExactAmountOut]],
29
+ ]);
30
+
31
+ describe(`${network}`, () => {
32
+ sideToContractMethods.forEach((contractMethods, side) =>
33
+ describe(`${side}`, () => {
34
+ contractMethods.forEach((contractMethod: ContractMethod) => {
35
+ describe(`${contractMethod}`, () => {
36
+ it(`${tokenASymbol} -> ${tokenBSymbol}`, async () => {
37
+ await testE2E(
38
+ tokens[tokenASymbol],
39
+ tokens[tokenBSymbol],
40
+ holders[tokenASymbol],
41
+ side === SwapSide.SELL ? tokenAAmount : tokenBAmount,
42
+ side,
43
+ dexKey,
44
+ contractMethod,
45
+ network,
46
+ provider,
47
+ );
48
+ });
49
+
50
+ it(`${tokenBSymbol} -> ${tokenASymbol}`, async () => {
51
+ await testE2E(
52
+ tokens[tokenBSymbol],
53
+ tokens[tokenASymbol],
54
+ holders[tokenBSymbol],
55
+ side === SwapSide.SELL ? tokenBAmount : tokenAAmount,
56
+ side,
57
+ dexKey,
58
+ contractMethod,
59
+ network,
60
+ provider,
61
+ );
62
+ });
63
+ });
64
+ });
65
+ }),
66
+ );
67
+ });
68
+ }
69
+
70
+ describe('Oswap E2E', () => {
71
+ const dexKey = 'OSwap';
72
+
73
+ describe('Mainnet', () => {
74
+ const network = Network.MAINNET;
75
+
76
+ const tokenASymbol: string = 'WETH';
77
+ const tokenBSymbol: string = 'STETH';
78
+
79
+ const tokenAAmount: string = '10000000000000';
80
+ const tokenBAmount: string = '10000000000000';
81
+
82
+ testForNetwork(
83
+ network,
84
+ dexKey,
85
+ tokenASymbol,
86
+ tokenBSymbol,
87
+ tokenAAmount,
88
+ tokenBAmount,
89
+ );
90
+ });
91
+ });
@@ -0,0 +1,88 @@
1
+ /* eslint-disable no-console */
2
+ import dotenv from 'dotenv';
3
+ dotenv.config();
4
+
5
+ import { OSwapConfig } from './config';
6
+ import { OSwapEventPool } from './oswap-pool';
7
+ import { Network } from '../../constants';
8
+ import { Address } from '../../types';
9
+ import { DummyDexHelper } from '../../dex-helper/index';
10
+ import { testEventSubscriber } from '../../../tests/utils-events';
11
+ import { OSwapPool, OSwapPoolState } from './types';
12
+
13
+ jest.setTimeout(50 * 1000);
14
+
15
+ // eventName -> blockNumbers
16
+ type EventMappings = Record<string, number[]>;
17
+
18
+ // Helper function. Returns the absolute value of the difference between 2 bigints.
19
+ function absdelta(a: bigint, b: bigint) {
20
+ const delta = a - b;
21
+ return delta < 0 ? -delta : delta;
22
+ }
23
+
24
+ function stateCompare(state1: OSwapPoolState, state2: OSwapPoolState) {
25
+ const ROUNDING_ERROR = 2;
26
+
27
+ expect(state1.traderate0).toEqual(state2.traderate0);
28
+ expect(state1.traderate1).toEqual(state2.traderate1);
29
+ // Some ERC20 tokens have rounding error which can lead to the balance computed from Transfer event valuee
30
+ // to deviate by a couple gwei vs the on-chain balance.
31
+ // For example stETH: https://docs.lido.fi/guides/lido-tokens-integration-guide/#1-2-wei-corner-case
32
+ expect(absdelta(state1.balance0, state2.balance0)).toBeLessThanOrEqual(
33
+ ROUNDING_ERROR,
34
+ );
35
+ expect(absdelta(state1.balance1, state2.balance1)).toBeLessThanOrEqual(
36
+ ROUNDING_ERROR,
37
+ );
38
+ }
39
+
40
+ describe('Oswap EventPool Mainnet', function () {
41
+ const dexKey = 'OSwap';
42
+ const network = Network.MAINNET;
43
+ const dexHelper = new DummyDexHelper(network);
44
+ const logger = dexHelper.getLogger(dexKey);
45
+ const pool: OSwapPool = OSwapConfig[dexKey][network].pools[0];
46
+
47
+ let eventPool: OSwapEventPool;
48
+
49
+ // poolAddress -> EventMappings
50
+ const eventsToTest: Record<Address, EventMappings> = {
51
+ [pool.address]: {
52
+ TraderateChanged: [18917344],
53
+ Transfer: [18922097, 18924756],
54
+ },
55
+ };
56
+
57
+ beforeEach(async () => {
58
+ eventPool = new OSwapEventPool(dexKey, pool, network, dexHelper, logger);
59
+ });
60
+
61
+ Object.entries(eventsToTest).forEach(
62
+ ([poolAddress, events]: [string, EventMappings]) => {
63
+ describe(`Events for ${poolAddress}`, () => {
64
+ Object.entries(events).forEach(
65
+ ([eventName, blockNumbers]: [string, number[]]) => {
66
+ describe(`${eventName}`, () => {
67
+ blockNumbers.forEach((blockNumber: number) => {
68
+ it(`State after ${blockNumber}`, async function () {
69
+ await testEventSubscriber(
70
+ eventPool,
71
+ eventPool.addressesSubscribed,
72
+ (_blockNumber: number) =>
73
+ eventPool.generateState(_blockNumber),
74
+ blockNumber,
75
+ `${dexKey}_${poolAddress}`,
76
+ dexHelper.provider,
77
+ (state1: OSwapPoolState, state2: OSwapPoolState) =>
78
+ stateCompare(state1, state2),
79
+ );
80
+ });
81
+ });
82
+ });
83
+ },
84
+ );
85
+ });
86
+ },
87
+ );
88
+ });
@@ -0,0 +1,269 @@
1
+ /* eslint-disable no-console */
2
+ import dotenv from 'dotenv';
3
+ dotenv.config();
4
+
5
+ import { DummyDexHelper } from '../../dex-helper/index';
6
+ import { Network, SwapSide } from '../../constants';
7
+ import { BI_POWS } from '../../bigint-constants';
8
+ import { MultiCallParams } from '../../lib/multi-wrapper';
9
+ import { uint256ToBigInt } from '../../lib/decoders';
10
+ import { getBigIntPow } from '../../utils';
11
+ import {
12
+ checkPoolPrices,
13
+ checkPoolsLiquidity,
14
+ checkConstantPoolPrices,
15
+ } from '../../../tests/utils';
16
+ import { Tokens } from '../../../tests/constants-e2e';
17
+ import { Address } from '../../types';
18
+ import { OSwap } from './oswap';
19
+ import { OSwapPool } from './types';
20
+
21
+ async function getOnchainTraderates(
22
+ oswap: OSwap,
23
+ pool: OSwapPool,
24
+ blockNumber: number,
25
+ ): Promise<{ traderate0: bigint; traderate1: bigint }> {
26
+ const callData: MultiCallParams<bigint>[] = [
27
+ {
28
+ target: pool.address,
29
+ callData: oswap.iOSwap.encodeFunctionData('traderate0', []),
30
+ decodeFunction: uint256ToBigInt,
31
+ },
32
+ {
33
+ target: pool.address,
34
+ callData: oswap.iOSwap.encodeFunctionData('traderate1', []),
35
+ decodeFunction: uint256ToBigInt,
36
+ },
37
+ ];
38
+
39
+ const results = await oswap.dexHelper.multiWrapper.aggregate<bigint>(
40
+ callData,
41
+ blockNumber,
42
+ oswap.dexHelper.multiWrapper.defaultBatchSize,
43
+ );
44
+
45
+ return { traderate0: results[0], traderate1: results[1] };
46
+ }
47
+
48
+ // Check prices passed as arguments against prices calculated from on-chain data.
49
+ async function checkOnChainPricing(
50
+ oswap: OSwap,
51
+ pool: OSwapPool,
52
+ blockNumber: number,
53
+ prices: bigint[],
54
+ side: SwapSide,
55
+ srcAddress: Address,
56
+ destAddress: Address,
57
+ amounts: bigint[],
58
+ ) {
59
+ // Get the onchain trade rates from the pool and calculate the prices.
60
+ const data = await getOnchainTraderates(oswap, pool, blockNumber);
61
+ let expectedPrices: bigint[] = [];
62
+ for (const amount of amounts) {
63
+ const rate =
64
+ srcAddress.toLowerCase() === pool.token0
65
+ ? data.traderate0
66
+ : data.traderate1;
67
+ if (side === SwapSide.SELL) {
68
+ expectedPrices.push((amount * rate) / getBigIntPow(36));
69
+ } else {
70
+ // SwapSide.BUY
71
+ expectedPrices.push((amount * getBigIntPow(36)) / rate);
72
+ }
73
+ }
74
+ expect(prices).toEqual(expectedPrices);
75
+ }
76
+
77
+ async function testPricingOnNetwork(
78
+ oswap: OSwap,
79
+ network: Network,
80
+ dexKey: string,
81
+ blockNumber: number,
82
+ srcTokenSymbol: string,
83
+ destTokenSymbol: string,
84
+ side: SwapSide,
85
+ amounts: bigint[],
86
+ ) {
87
+ const networkTokens = Tokens[network];
88
+ const srcToken = networkTokens[srcTokenSymbol];
89
+ const destToken = networkTokens[destTokenSymbol];
90
+
91
+ const poolIds = await oswap.getPoolIdentifiers(
92
+ srcToken,
93
+ destToken,
94
+ side,
95
+ blockNumber,
96
+ );
97
+ console.log(
98
+ `${srcTokenSymbol} <> ${destTokenSymbol} Pool Identifiers: ${poolIds}`,
99
+ );
100
+
101
+ expect(poolIds.length).toBeGreaterThan(0);
102
+
103
+ // Get calculated prices based on the stored state.
104
+ const poolPrices = await oswap.getPricesVolume(
105
+ srcToken,
106
+ destToken,
107
+ amounts,
108
+ side,
109
+ blockNumber,
110
+ poolIds,
111
+ );
112
+ console.log(
113
+ `${side} ${srcTokenSymbol} <> ${destTokenSymbol} Pool Prices: `,
114
+ poolPrices,
115
+ );
116
+
117
+ expect(poolPrices).not.toBeNull();
118
+ if (oswap.hasConstantPriceLargeAmounts) {
119
+ checkConstantPoolPrices(poolPrices!, amounts, dexKey);
120
+ } else {
121
+ checkPoolPrices(poolPrices!, amounts, side, dexKey);
122
+ }
123
+
124
+ // Check that the prices calculated from onchain data match with the ones calculated from the stored state.
125
+ const pool = oswap.getPoolById(poolIds[0]);
126
+ expect(pool).not.toBeNull();
127
+ await checkOnChainPricing(
128
+ oswap,
129
+ pool as OSwapPool,
130
+ blockNumber,
131
+ poolPrices![0].prices,
132
+ side,
133
+ srcToken.address,
134
+ destToken.address,
135
+ amounts,
136
+ );
137
+ }
138
+
139
+ describe('OSwap', function () {
140
+ const dexKey = 'OSwap';
141
+ let blockNumber: number;
142
+ let oswap: OSwap;
143
+
144
+ describe('Mainnet', () => {
145
+ const network = Network.MAINNET;
146
+ const dexHelper = new DummyDexHelper(network);
147
+
148
+ const tokens = Tokens[network];
149
+
150
+ const srcTokenSymbol = 'WETH';
151
+ const destTokenSymbol = 'STETH';
152
+
153
+ const amountsForSell = [
154
+ 0n,
155
+ 1n * BI_POWS[tokens[srcTokenSymbol].decimals],
156
+ 2n * BI_POWS[tokens[srcTokenSymbol].decimals],
157
+ 3n * BI_POWS[tokens[srcTokenSymbol].decimals],
158
+ 4n * BI_POWS[tokens[srcTokenSymbol].decimals],
159
+ 5n * BI_POWS[tokens[srcTokenSymbol].decimals],
160
+ 6n * BI_POWS[tokens[srcTokenSymbol].decimals],
161
+ 7n * BI_POWS[tokens[srcTokenSymbol].decimals],
162
+ 8n * BI_POWS[tokens[srcTokenSymbol].decimals],
163
+ 9n * BI_POWS[tokens[srcTokenSymbol].decimals],
164
+ 10n * BI_POWS[tokens[srcTokenSymbol].decimals],
165
+ ];
166
+
167
+ const amountsForBuy = [
168
+ 0n,
169
+ 1n * BI_POWS[tokens[destTokenSymbol].decimals],
170
+ 2n * BI_POWS[tokens[destTokenSymbol].decimals],
171
+ 3n * BI_POWS[tokens[destTokenSymbol].decimals],
172
+ 4n * BI_POWS[tokens[destTokenSymbol].decimals],
173
+ 5n * BI_POWS[tokens[destTokenSymbol].decimals],
174
+ 6n * BI_POWS[tokens[destTokenSymbol].decimals],
175
+ 7n * BI_POWS[tokens[destTokenSymbol].decimals],
176
+ 8n * BI_POWS[tokens[destTokenSymbol].decimals],
177
+ 9n * BI_POWS[tokens[destTokenSymbol].decimals],
178
+ 10n * BI_POWS[tokens[destTokenSymbol].decimals],
179
+ ];
180
+
181
+ // Return a blockNumber to use for the tests.
182
+ // Check that the pool has enough liquidity to run the tests at the current blockNumber.
183
+ // If not, fallback to a known blockNumber in the past with
184
+ // high enough liquidity - the on-chain queries will just be a bit slower to execute.
185
+ async function getBlockNumberForTesting(oswap: OSwap): Promise<number> {
186
+ const DEFAULT_BLOCK_NUMBER = 18888241;
187
+
188
+ blockNumber = await dexHelper.web3Provider.eth.getBlockNumber();
189
+ const srcToken = Tokens[network][srcTokenSymbol];
190
+ const destToken = Tokens[network][destTokenSymbol];
191
+
192
+ // Get the pool and its state for the given test pair.
193
+ const pool = oswap.getPoolByTokenPair(srcToken, destToken);
194
+ if (!pool)
195
+ throw new Error(
196
+ `No pool found for pair ${srcTokenSymbol}-${destTokenSymbol}`,
197
+ );
198
+
199
+ const eventPool = oswap.eventPools[pool.id];
200
+ const state = await eventPool.getStateOrGenerate(blockNumber, true);
201
+
202
+ const minBalance =
203
+ state.balance0 < state.balance1 ? state.balance0 : state.balance1;
204
+ const maxAmount = [...amountsForSell, ...amountsForBuy].reduce(
205
+ (max, amount) => (amount > max ? amount : max),
206
+ );
207
+ const hasEnoughLiquidity = minBalance > maxAmount;
208
+
209
+ if (!hasEnoughLiquidity) {
210
+ return DEFAULT_BLOCK_NUMBER;
211
+ }
212
+ return blockNumber;
213
+ }
214
+
215
+ beforeAll(async () => {
216
+ oswap = new OSwap(network, dexKey, dexHelper);
217
+
218
+ blockNumber = await getBlockNumberForTesting(oswap);
219
+ });
220
+
221
+ it('getPoolIdentifiers and getPricesVolume SELL', async function () {
222
+ await testPricingOnNetwork(
223
+ oswap,
224
+ network,
225
+ dexKey,
226
+ blockNumber,
227
+ srcTokenSymbol,
228
+ destTokenSymbol,
229
+ SwapSide.SELL,
230
+ amountsForSell,
231
+ );
232
+ });
233
+
234
+ it('getPoolIdentifiers and getPricesVolume BUY', async function () {
235
+ await testPricingOnNetwork(
236
+ oswap,
237
+ network,
238
+ dexKey,
239
+ blockNumber,
240
+ srcTokenSymbol,
241
+ destTokenSymbol,
242
+ SwapSide.BUY,
243
+ amountsForBuy,
244
+ );
245
+ });
246
+
247
+ it('getTopPoolsForToken', async function () {
248
+ // We have to check without calling initializePricing, because
249
+ // pool-tracker is not calling that function
250
+ const newOSwap = new OSwap(network, dexKey, dexHelper);
251
+ if (newOSwap.updatePoolState) {
252
+ await newOSwap.updatePoolState();
253
+ }
254
+
255
+ const poolLiquidity = await newOSwap.getTopPoolsForToken(
256
+ tokens[srcTokenSymbol].address,
257
+ 10,
258
+ );
259
+
260
+ if (!newOSwap.hasConstantPriceLargeAmounts) {
261
+ checkPoolsLiquidity(
262
+ poolLiquidity,
263
+ Tokens[network][srcTokenSymbol].address,
264
+ dexKey,
265
+ );
266
+ }
267
+ });
268
+ });
269
+ });