@paraswap/dex-lib 3.9.7-fluid-dex.3 → 3.10.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 (209) hide show
  1. package/.vscode/launch.json +53 -0
  2. package/.vscode/settings.json +2 -0
  3. package/.vscode/tasks.json +15 -0
  4. package/build/abi/{integral/pool.json → inception/inception-ineth-pool.json} +558 -537
  5. package/build/abi/inception/inception-ratio-feed.json +329 -0
  6. package/build/abi/inception/inception-vault.json +991 -0
  7. package/build/abi/uniswap-v3/AlienBaseV3Router.abi.json +52 -0
  8. package/build/abi/{integral/relayer.json → wombat/pool.json} +474 -721
  9. package/build/config.js +28 -0
  10. package/build/config.js.map +1 -1
  11. package/build/constants.d.ts +2 -1
  12. package/build/constants.js +1 -0
  13. package/build/constants.js.map +1 -1
  14. package/build/dex/aave-v1/aave-v1.d.ts +2 -1
  15. package/build/dex/aave-v1/aave-v1.js +23 -0
  16. package/build/dex/aave-v1/aave-v1.js.map +1 -1
  17. package/build/dex/aave-v2/tokens-avalanche.json +44 -0
  18. package/build/dex/aave-v2/tokens-mainnet.json +188 -0
  19. package/build/dex/aave-v2/tokens-polygon.json +44 -0
  20. package/build/dex/aave-v3/aave-v3.js +1 -1
  21. package/build/dex/aave-v3/aave-v3.js.map +1 -1
  22. package/build/dex/aave-v3/config.js +6 -0
  23. package/build/dex/aave-v3/config.js.map +1 -1
  24. package/build/dex/aave-v3-stata/config.js +3 -3
  25. package/build/dex/aave-v3-stata/config.js.map +1 -1
  26. package/build/dex/algebra/algebra-pool-v1_9_bidirectional_fee.js +1 -1
  27. package/build/dex/algebra/algebra-pool-v1_9_bidirectional_fee.js.map +1 -1
  28. package/build/dex/algebra/config.js +20 -0
  29. package/build/dex/algebra/config.js.map +1 -1
  30. package/build/dex/balancer-v2/balancer-v2.js +1 -1
  31. package/build/dex/balancer-v2/balancer-v2.js.map +1 -1
  32. package/build/dex/balancer-v2/config.js +4 -0
  33. package/build/dex/balancer-v2/config.js.map +1 -1
  34. package/build/dex/curve-v1/config.js +36 -0
  35. package/build/dex/curve-v1/config.js.map +1 -1
  36. package/build/dex/curve-v1/curve-v1.js +1 -1
  37. package/build/dex/curve-v1/curve-v1.js.map +1 -1
  38. package/build/dex/curve-v1-factory/constants.js +1 -0
  39. package/build/dex/curve-v1-factory/constants.js.map +1 -1
  40. package/build/dex/curve-v1-factory/curve-v1-factory.js +1 -1
  41. package/build/dex/curve-v1-factory/curve-v1-factory.js.map +1 -1
  42. package/build/dex/curve-v1-stable-ng/config.js +28 -0
  43. package/build/dex/curve-v1-stable-ng/config.js.map +1 -1
  44. package/build/dex/fluid-dex/fluid-dex.d.ts +1 -0
  45. package/build/dex/fluid-dex/fluid-dex.js +19 -17
  46. package/build/dex/fluid-dex/fluid-dex.js.map +1 -1
  47. package/build/dex/fluid-dex/utils.d.ts +2 -0
  48. package/build/dex/fluid-dex/utils.js +18 -0
  49. package/build/dex/fluid-dex/utils.js.map +1 -0
  50. package/build/dex/inception/config.d.ts +4 -0
  51. package/build/dex/inception/config.js +109 -0
  52. package/build/dex/inception/config.js.map +1 -0
  53. package/build/dex/inception/inception-event-pool.d.ts +16 -0
  54. package/build/dex/inception/inception-event-pool.js +49 -0
  55. package/build/dex/inception/inception-event-pool.js.map +1 -0
  56. package/build/dex/inception/inception.d.ts +43 -0
  57. package/build/dex/inception/inception.js +180 -0
  58. package/build/dex/inception/inception.js.map +1 -0
  59. package/build/dex/inception/tokens.d.ts +9 -0
  60. package/build/dex/inception/tokens.js +28 -0
  61. package/build/dex/inception/tokens.js.map +1 -0
  62. package/build/dex/inception/types.d.ts +22 -0
  63. package/build/dex/inception/types.js +3 -0
  64. package/build/dex/inception/types.js.map +1 -0
  65. package/build/dex/inception/utils.d.ts +7 -0
  66. package/build/dex/inception/utils.js +58 -0
  67. package/build/dex/inception/utils.js.map +1 -0
  68. package/build/dex/jarvis.d.ts +2 -1
  69. package/build/dex/jarvis.js +58 -0
  70. package/build/dex/jarvis.js.map +1 -1
  71. package/build/dex/maker-psm/scripts/gem.abi.json +136 -0
  72. package/build/dex/maker-psm/scripts/validate-state.d.ts +0 -0
  73. package/build/dex/maker-psm/scripts/validate-state.js +185 -0
  74. package/build/dex/maker-psm/scripts/validate-state.js.map +1 -0
  75. package/build/dex/maker-psm/scripts/vat.abi.json +243 -0
  76. package/build/dex/platypus/platypus.d.ts +2 -1
  77. package/build/dex/platypus/platypus.js +45 -0
  78. package/build/dex/platypus/platypus.js.map +1 -1
  79. package/build/dex/solidly-v3/scripts/check-event-pool-event.d.ts +1 -0
  80. package/build/dex/solidly-v3/scripts/check-event-pool-event.js +53 -0
  81. package/build/dex/solidly-v3/scripts/check-event-pool-event.js.map +1 -0
  82. package/build/dex/stable-pool.js +1 -8
  83. package/build/dex/stable-pool.js.map +1 -1
  84. package/build/dex/swaap-v1/swaap-v1.d.ts +2 -1
  85. package/build/dex/swaap-v1/swaap-v1.js +14 -0
  86. package/build/dex/swaap-v1/swaap-v1.js.map +1 -1
  87. package/build/dex/uniswap-v2/config.js +14 -0
  88. package/build/dex/uniswap-v2/config.js.map +1 -1
  89. package/build/dex/uniswap-v2/nomiswap-v2.js +10 -0
  90. package/build/dex/uniswap-v2/nomiswap-v2.js.map +1 -1
  91. package/build/dex/uniswap-v2/uniswap-v2.js +1 -1
  92. package/build/dex/uniswap-v2/uniswap-v2.js.map +1 -1
  93. package/build/dex/uniswap-v3/config.js +14 -0
  94. package/build/dex/uniswap-v3/config.js.map +1 -1
  95. package/build/dex/uniswap-v3/forks/alien-base-v3/alien-base-v3.d.ts +17 -0
  96. package/build/dex/uniswap-v3/forks/alien-base-v3/alien-base-v3.js +64 -0
  97. package/build/dex/uniswap-v3/forks/alien-base-v3/alien-base-v3.js.map +1 -0
  98. package/build/dex/uniswap-v3/forks/alien-v3/alien-v3.d.ts +17 -0
  99. package/build/dex/uniswap-v3/forks/alien-v3/alien-v3.js +79 -0
  100. package/build/dex/uniswap-v3/forks/alien-v3/alien-v3.js.map +1 -0
  101. package/build/dex/uniswap-v3/uniswap-v3-pricing.d.ts +4 -0
  102. package/build/dex/uniswap-v3/uniswap-v3-pricing.js +43 -0
  103. package/build/dex/uniswap-v3/uniswap-v3-pricing.js.map +1 -0
  104. package/build/dex/uniswap-v3/uniswap-v3-worker.d.ts +1 -0
  105. package/build/dex/uniswap-v3/uniswap-v3-worker.js +10 -0
  106. package/build/dex/uniswap-v3/uniswap-v3-worker.js.map +1 -0
  107. package/build/dex/uniswap-v3/uniswap-v3.js +1 -0
  108. package/build/dex/uniswap-v3/uniswap-v3.js.map +1 -1
  109. package/build/dex/weth/config.js +5 -0
  110. package/build/dex/weth/config.js.map +1 -1
  111. package/build/dex/wusdm/config.js +8 -0
  112. package/build/dex/wusdm/config.js.map +1 -1
  113. package/build/dex/wusdm/wusdm.d.ts +1 -0
  114. package/build/dex/wusdm/wusdm.js +9 -4
  115. package/build/dex/wusdm/wusdm.js.map +1 -1
  116. package/build/dex/zerox/index.d.ts +4 -26
  117. package/build/dex/zerox/index.js +50 -42
  118. package/build/dex/zerox/index.js.map +1 -1
  119. package/build/dex/zerox/types.d.ts +29 -0
  120. package/build/dex/zerox/types.js +8 -1
  121. package/build/dex/zerox/types.js.map +1 -1
  122. package/build/executor/WETHBytecodeBuilder.js +1 -0
  123. package/build/executor/WETHBytecodeBuilder.js.map +1 -1
  124. package/package.json +1 -1
  125. package/src/config.ts +29 -1
  126. package/src/constants.ts +1 -0
  127. package/src/dex/aave-v3/aave-v3-e2e.test.ts +69 -0
  128. package/src/dex/aave-v3/aave-v3.ts +1 -1
  129. package/src/dex/aave-v3/config.ts +7 -0
  130. package/src/dex/aave-v3-stata/config.ts +4 -4
  131. package/src/dex/algebra/algebra-e2e.test.ts +22 -0
  132. package/src/dex/algebra/algebra-pool-v1_9_bidirectional_fee.ts +1 -1
  133. package/src/dex/algebra/config.ts +21 -0
  134. package/src/dex/balancer-v2/balancer-v2-e2e.test.ts +22 -250
  135. package/src/dex/balancer-v2/balancer-v2.ts +1 -1
  136. package/src/dex/balancer-v2/config.ts +4 -0
  137. package/src/dex/curve-v1/config.ts +36 -0
  138. package/src/dex/curve-v1/curve-v1-e2e.test.ts +69 -0
  139. package/src/dex/curve-v1/curve-v1.ts +1 -1
  140. package/src/dex/curve-v1-factory/constants.ts +1 -0
  141. package/src/dex/curve-v1-factory/curve-v1-factory.ts +2 -1
  142. package/src/dex/curve-v1-stable-ng/config.ts +28 -0
  143. package/src/dex/curve-v1-stable-ng/curve-v1-stable-ng-e2e.test.ts +20 -0
  144. package/src/dex/fluid-dex/fluid-dex-e2e.test.ts +0 -1
  145. package/src/dex/fluid-dex/fluid-dex-integration.test.ts +62 -23
  146. package/src/dex/fluid-dex/fluid-dex.ts +26 -18
  147. package/src/dex/fluid-dex/utils.ts +15 -0
  148. package/src/dex/uniswap-v2/config.ts +16 -0
  149. package/src/dex/uniswap-v2/nomiswap-v2.ts +11 -0
  150. package/src/dex/uniswap-v2/uniswap-v2-e2e-gnosis.test.ts +283 -0
  151. package/src/dex/uniswap-v2/uniswap-v2.ts +1 -1
  152. package/src/dex/uniswap-v3/config.ts +14 -0
  153. package/src/dex/uniswap-v3/uniswap-v3-e2e.test.ts +24 -0
  154. package/src/dex/uniswap-v3/uniswap-v3.ts +1 -0
  155. package/src/dex/weth/config.ts +5 -0
  156. package/src/dex/wusdm/config.ts +8 -0
  157. package/src/dex/wusdm/wusdm-e2e.test.ts +38 -0
  158. package/src/dex/wusdm/wusdm.ts +11 -4
  159. package/src/executor/WETHBytecodeBuilder.ts +3 -2
  160. package/tests/constants-e2e.ts +75 -0
  161. package/.idea/aws.xml +0 -17
  162. package/.idea/codeStyles/Project.xml +0 -19
  163. package/.idea/codeStyles/codeStyleConfig.xml +0 -5
  164. package/.idea/misc.xml +0 -6
  165. package/.idea/modules.xml +0 -8
  166. package/.idea/paraswap-dex-lib.iml +0 -9
  167. package/.idea/prettier.xml +0 -7
  168. package/.idea/vcs.xml +0 -6
  169. package/build/abi/integral/factory.json +0 -333
  170. package/build/abi/integral/oracle.json +0 -501
  171. package/build/dex/fluid-dex/fluid-dex-generate-pool.d.ts +0 -49
  172. package/build/dex/fluid-dex/fluid-dex-generate-pool.js +0 -104
  173. package/build/dex/fluid-dex/fluid-dex-generate-pool.js.map +0 -1
  174. package/build/dex/integral/config.d.ts +0 -4
  175. package/build/dex/integral/config.js +0 -20
  176. package/build/dex/integral/config.js.map +0 -1
  177. package/build/dex/integral/context.d.ts +0 -40
  178. package/build/dex/integral/context.js +0 -158
  179. package/build/dex/integral/context.js.map +0 -1
  180. package/build/dex/integral/integral-factory.d.ts +0 -24
  181. package/build/dex/integral/integral-factory.js +0 -95
  182. package/build/dex/integral/integral-factory.js.map +0 -1
  183. package/build/dex/integral/integral-pool.d.ts +0 -50
  184. package/build/dex/integral/integral-pool.js +0 -149
  185. package/build/dex/integral/integral-pool.js.map +0 -1
  186. package/build/dex/integral/integral-pricing.d.ts +0 -18
  187. package/build/dex/integral/integral-pricing.js +0 -114
  188. package/build/dex/integral/integral-pricing.js.map +0 -1
  189. package/build/dex/integral/integral-relayer.d.ts +0 -42
  190. package/build/dex/integral/integral-relayer.js +0 -192
  191. package/build/dex/integral/integral-relayer.js.map +0 -1
  192. package/build/dex/integral/integral-token.d.ts +0 -27
  193. package/build/dex/integral/integral-token.js +0 -59
  194. package/build/dex/integral/integral-token.js.map +0 -1
  195. package/build/dex/integral/integral.d.ts +0 -41
  196. package/build/dex/integral/integral.js +0 -376
  197. package/build/dex/integral/integral.js.map +0 -1
  198. package/build/dex/integral/types.d.ts +0 -85
  199. package/build/dex/integral/types.js +0 -9
  200. package/build/dex/integral/types.js.map +0 -1
  201. package/build/dex/integral/utils-e2e.d.ts +0 -9
  202. package/build/dex/integral/utils-e2e.js +0 -131
  203. package/build/dex/integral/utils-e2e.js.map +0 -1
  204. package/build/dex/integral/utils.d.ts +0 -17
  205. package/build/dex/integral/utils.js +0 -94
  206. package/build/dex/integral/utils.js.map +0 -1
  207. package/build/dex/trader-joe-v2.1/optimizer.d.ts +0 -2
  208. package/build/dex/trader-joe-v2.1/optimizer.js +0 -47
  209. package/build/dex/trader-joe-v2.1/optimizer.js.map +0 -1
@@ -14,6 +14,8 @@ import {
14
14
  } from '../../../tests/utils';
15
15
  import { Tokens } from '../../../tests/constants-e2e';
16
16
  import ResolverABI from '../../abi/fluid-dex/resolver.abi.json';
17
+ import { Contract } from 'ethers';
18
+ import { Pool } from './types';
17
19
 
18
20
  /*
19
21
  README
@@ -33,14 +35,21 @@ import ResolverABI from '../../abi/fluid-dex/resolver.abi.json';
33
35
  function getReaderCalldata(
34
36
  exchangeAddress: string,
35
37
  readerIface: Interface,
38
+ poolAddress: string,
36
39
  amounts: bigint[],
37
40
  funcName: string,
41
+ pools: { address: string; token0: string; token1: string }[],
42
+ srcToken: string,
38
43
  ) {
44
+ const pool = pools.find(
45
+ item => item.address.toLowerCase() === poolAddress.toLowerCase(),
46
+ );
47
+
39
48
  return amounts.map(amount => ({
40
49
  target: exchangeAddress,
41
50
  callData: readerIface.encodeFunctionData(funcName, [
42
- '0x0b1a513ee24972daef112bc777a5610d4325c9e7',
43
- funcName == 'estimateSwapIn' ? true : false,
51
+ poolAddress,
52
+ pool!.token0.toLowerCase() === srcToken.toLowerCase() ? true : false,
44
53
  amount,
45
54
  funcName == 'estimateSwapIn' ? 0 : 2n * amount,
46
55
  ]),
@@ -60,20 +69,42 @@ function decodeReaderResult(
60
69
  async function checkOnChainPricing(
61
70
  fluidDex: FluidDex,
62
71
  funcName: string,
72
+ poolAddress: string,
63
73
  blockNumber: number,
64
74
  prices: bigint[],
65
75
  amounts: bigint[],
76
+ dexHelper: DummyDexHelper,
77
+ srcToken: string,
66
78
  ) {
67
79
  const resolverAddress = '0xE8a07a32489BD9d5a00f01A55749Cf5cB854Fd13';
68
80
 
69
81
  const readerIface = new Interface(ResolverABI);
70
82
 
83
+ const resolverContract = new Contract(
84
+ resolverAddress,
85
+ ResolverABI,
86
+ dexHelper.provider,
87
+ );
88
+ const rawResult = await resolverContract.callStatic.getAllPools({
89
+ blockTag: blockNumber,
90
+ });
91
+
92
+ const pools: Pool[] = rawResult.map((result: any) => ({
93
+ address: result[0],
94
+ token0: result[1],
95
+ token1: result[2],
96
+ }));
97
+
71
98
  const readerCallData = getReaderCalldata(
72
99
  resolverAddress,
73
100
  readerIface,
101
+ poolAddress,
74
102
  amounts.slice(1),
75
103
  funcName,
104
+ pools,
105
+ srcToken,
76
106
  );
107
+
77
108
  const readerResult = (
78
109
  await fluidDex.dexHelper.multiContract.methods
79
110
  .aggregate(readerCallData)
@@ -83,6 +114,8 @@ async function checkOnChainPricing(
83
114
  const expectedPrices = [0n].concat(
84
115
  decodeReaderResult(readerResult, readerIface, funcName),
85
116
  );
117
+
118
+ expect(prices).toEqual(expectedPrices);
86
119
  }
87
120
 
88
121
  async function testPricingOnNetwork(
@@ -95,6 +128,7 @@ async function testPricingOnNetwork(
95
128
  side: SwapSide,
96
129
  amounts: bigint[],
97
130
  funcNameToCheck: string,
131
+ dexHelper: DummyDexHelper,
98
132
  ) {
99
133
  const networkTokens = Tokens[network];
100
134
 
@@ -138,9 +172,12 @@ async function testPricingOnNetwork(
138
172
  await checkOnChainPricing(
139
173
  fluidDex,
140
174
  funcNameToCheck,
175
+ poolPrices![0].poolAddresses![0],
141
176
  blockNumber,
142
177
  poolPrices![0].prices,
143
178
  amounts,
179
+ dexHelper,
180
+ networkTokens[srcTokenSymbol].address,
144
181
  );
145
182
  }
146
183
 
@@ -153,8 +190,6 @@ describe('FluidDex', function () {
153
190
  const network = Network.MAINNET;
154
191
  const dexHelper = new DummyDexHelper(network);
155
192
 
156
- const tokens = Tokens[network];
157
-
158
193
  beforeAll(async () => {
159
194
  blockNumber = await dexHelper.provider.getBlockNumber();
160
195
  fluidDex = new FluidDex(network, dexKey, dexHelper);
@@ -169,16 +204,16 @@ describe('FluidDex', function () {
169
204
 
170
205
  const amountsForSell = [
171
206
  0n,
172
- 1n * BI_POWS[8],
173
- 2n * BI_POWS[8],
174
- 3n * BI_POWS[8],
175
- 4n * BI_POWS[8],
176
- 5n * BI_POWS[8],
177
- 6n * BI_POWS[8],
178
- 7n * BI_POWS[8],
179
- 8n * BI_POWS[8],
180
- 9n * BI_POWS[8],
181
- 10n * BI_POWS[8],
207
+ 1n * BI_POWS[18],
208
+ 2n * BI_POWS[18],
209
+ 3n * BI_POWS[18],
210
+ 4n * BI_POWS[18],
211
+ 5n * BI_POWS[18],
212
+ 6n * BI_POWS[18],
213
+ 7n * BI_POWS[18],
214
+ 8n * BI_POWS[18],
215
+ 9n * BI_POWS[18],
216
+ 10n * BI_POWS[18],
182
217
  ];
183
218
 
184
219
  it('wstETH -> ETH, getPoolIdentifiers and getPricesVolume SELL', async function () {
@@ -192,6 +227,7 @@ describe('FluidDex', function () {
192
227
  SwapSide.SELL,
193
228
  amountsForSell,
194
229
  'estimateSwapIn',
230
+ dexHelper,
195
231
  );
196
232
  });
197
233
 
@@ -206,6 +242,7 @@ describe('FluidDex', function () {
206
242
  SwapSide.SELL,
207
243
  amountsForSell,
208
244
  'estimateSwapIn',
245
+ dexHelper,
209
246
  );
210
247
  });
211
248
  });
@@ -216,16 +253,16 @@ describe('FluidDex', function () {
216
253
 
217
254
  const amountsForSell = [
218
255
  0n,
219
- 1n * BI_POWS[6],
220
- 2n * BI_POWS[6],
221
- 3n * BI_POWS[6],
222
- 4n * BI_POWS[6],
223
- 5n * BI_POWS[6],
224
- 6n * BI_POWS[6],
225
- 7n * BI_POWS[6],
226
- 8n * BI_POWS[6],
227
- 9n * BI_POWS[6],
228
256
  10n * BI_POWS[6],
257
+ 20n * BI_POWS[6],
258
+ 30n * BI_POWS[6],
259
+ 40n * BI_POWS[6],
260
+ 50n * BI_POWS[6],
261
+ 60n * BI_POWS[6],
262
+ 70n * BI_POWS[6],
263
+ 80n * BI_POWS[6],
264
+ 90n * BI_POWS[6],
265
+ 100n * BI_POWS[6],
229
266
  ];
230
267
 
231
268
  it('USDC -> USDT getPoolIdentifiers and getPricesVolume SELL', async function () {
@@ -239,6 +276,7 @@ describe('FluidDex', function () {
239
276
  SwapSide.SELL,
240
277
  amountsForSell,
241
278
  'estimateSwapIn',
279
+ dexHelper,
242
280
  );
243
281
  });
244
282
 
@@ -253,6 +291,7 @@ describe('FluidDex', function () {
253
291
  SwapSide.SELL,
254
292
  amountsForSell,
255
293
  'estimateSwapIn',
294
+ dexHelper,
256
295
  );
257
296
  });
258
297
  });
@@ -31,6 +31,8 @@ import { getDexKeysWithNetwork, getBigIntPow } from '../../utils';
31
31
  import { extractReturnAmountPosition } from '../../executor/utils';
32
32
  import { MultiResult } from '../../lib/multi-wrapper';
33
33
  import { generalDecoder } from '../../lib/decoders';
34
+ import { BigNumber } from 'ethers';
35
+ import { sqrt } from './utils';
34
36
 
35
37
  export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
36
38
  eventPools: { [id: string]: FluidDexEventPool } = {};
@@ -139,6 +141,8 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
139
141
  side: SwapSide,
140
142
  blockNumber: number,
141
143
  ): Promise<string[]> {
144
+ if (side === SwapSide.BUY) return [];
145
+
142
146
  const pool = await this.getPoolByTokenPair(
143
147
  srcToken.address,
144
148
  destToken.address,
@@ -183,7 +187,7 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
183
187
  if (srcToken.address.toLowerCase() === destToken.address.toLowerCase())
184
188
  return null;
185
189
 
186
- if (side === SwapSide.BUY) throw new Error(`Buy not supported`);
190
+ if (side === SwapSide.BUY) return null;
187
191
  // Get the pool to use.
188
192
  const pool = await this.getPoolByTokenPair(
189
193
  srcToken.address,
@@ -358,15 +362,14 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
358
362
  return 0n;
359
363
  }
360
364
  const amountInAdjusted =
361
- (((amountIn * (this.FEE_100_PERCENT - fee)) / this.FEE_100_PERCENT) *
362
- BigInt(10 ** 12)) /
363
- BigInt(10 ** inDecimals);
365
+ (amountIn * BigInt(10 ** 12)) / BigInt(10 ** inDecimals);
364
366
 
365
367
  const amountOut = this.swapInAdjusted(
366
368
  swap0To1,
367
369
  amountInAdjusted, // Convert back to number for internal calculations
368
370
  colReserves,
369
371
  debtReserves,
372
+ fee,
370
373
  );
371
374
  const result = (amountOut * BigInt(10 ** outDecimals)) / BigInt(10 ** 12);
372
375
  return result;
@@ -385,6 +388,7 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
385
388
  amountToSwap: bigint,
386
389
  colReserves: CollateralReserves,
387
390
  debtReserves: DebtReserves,
391
+ fee: bigint,
388
392
  ): bigint {
389
393
  const {
390
394
  token0RealReserves,
@@ -466,22 +470,26 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
466
470
  if (a <= BigInt(0)) {
467
471
  // Entire trade routes through debt pool
468
472
  amountOutDebt = this.getAmountOut(
469
- amountToSwap,
473
+ this.applyFee(amountToSwap, fee),
470
474
  debtIReserveIn,
471
475
  debtIReserveOut,
472
476
  );
473
477
  } else if (a >= amountToSwap) {
474
478
  // Entire trade routes through collateral pool
475
479
  amountOutCollateral = this.getAmountOut(
476
- amountToSwap,
480
+ this.applyFee(amountToSwap, fee),
477
481
  colIReserveIn,
478
482
  colIReserveOut,
479
483
  );
480
484
  } else {
481
485
  // Trade routes through both pools
482
- amountOutCollateral = this.getAmountOut(a, colIReserveIn, colIReserveOut);
486
+ amountOutCollateral = this.getAmountOut(
487
+ this.applyFee(a, fee),
488
+ colIReserveIn,
489
+ colIReserveOut,
490
+ );
483
491
  amountOutDebt = this.getAmountOut(
484
- amountToSwap - a,
492
+ this.applyFee(amountToSwap - a, fee),
485
493
  debtIReserveIn,
486
494
  debtIReserveOut,
487
495
  );
@@ -499,6 +507,10 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
499
507
  return totalAmountOut;
500
508
  }
501
509
 
510
+ applyFee(amount: bigint, fee: bigint): bigint {
511
+ return (amount * (this.FEE_100_PERCENT - fee)) / this.FEE_100_PERCENT;
512
+ }
513
+
502
514
  /**
503
515
  * Given an input amount of asset and pair reserves, returns the maximum output amount of the other asset.
504
516
  * @param amountIn - The amount of input asset.
@@ -599,18 +611,14 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
599
611
  ): bigint {
600
612
  // Adding 1e18 precision
601
613
 
602
- const xyRoot = BigInt(Math.floor(Math.sqrt(Number(x * y * BigInt(1e18)))));
603
- const x2y2Root = BigInt(
604
- Math.floor(Math.sqrt(Number(x2 * y2 * BigInt(1e18)))),
605
- );
614
+ const xyRoot = sqrt(BigNumber.from(x).mul(y).mul(BigInt(1e18))).toBigInt();
615
+ const x2y2Root = sqrt(
616
+ BigNumber.from(x2).mul(y2).mul(BigInt(1e18)),
617
+ ).toBigInt();
606
618
 
607
619
  // Calculating 'a' using the given formula
608
- const a =
609
- (Number(y2) * Number(xyRoot) +
610
- Number(t) * Number(xyRoot) -
611
- Number(y) * Number(x2y2Root)) /
612
- (Number(xyRoot) + Number(x2y2Root));
613
- return BigInt(Math.floor(a));
620
+ const a = (y2 * xyRoot + t * xyRoot - y * x2y2Root) / (xyRoot + x2y2Root);
621
+ return a;
614
622
  }
615
623
 
616
624
  /**
@@ -0,0 +1,15 @@
1
+ import { BigNumber } from 'ethers';
2
+
3
+ const ONE = BigNumber.from(1);
4
+ const TWO = BigNumber.from(2);
5
+
6
+ export function sqrt(value: BigNumber) {
7
+ let x = value;
8
+ let z = x.add(ONE).div(TWO);
9
+ let y = x;
10
+ while (z.sub(y).isNegative()) {
11
+ y = z;
12
+ z = x.div(z).add(z).div(TWO);
13
+ }
14
+ return y;
15
+ }
@@ -427,6 +427,22 @@ export const UniswapV2Config: DexConfigMap<DexParams> = {
427
427
  '0xe18a34eb0e04b04f7a0ac29a6e80748dca96319b42c54d679cb821dca90c6303',
428
428
  feeCode: 30,
429
429
  },
430
+ [Network.GNOSIS]: {
431
+ subgraphURL: '7czeiia7ZXvsW45szX2w8EK1ZNgZWZET83zYCwE6JT9x',
432
+ factoryAddress: '0xc35dadb65012ec5796536bd9864ed8773abc74c4',
433
+ initCode:
434
+ '0xe18a34eb0e04b04f7a0ac29a6e80748dca96319b42c54d679cb821dca90c6303',
435
+ feeCode: 30,
436
+ },
437
+ },
438
+ HoneySwap: {
439
+ [Network.GNOSIS]: {
440
+ subgraphURL: '7vjh6q6gj6M1bhRNgkKZmWqd5jot79kV84BiQtUDQSdV',
441
+ factoryAddress: '0xa818b4f111ccac7aa31d0bcc0806d64f2e0737d7',
442
+ initCode:
443
+ '0xdbd265d4828759db2a0351a7b97180bcc41449a038bcd08a45afe937147f6267',
444
+ feeCode: 30,
445
+ },
430
446
  },
431
447
  Zyberswap: {
432
448
  [Network.ARBITRUM]: {
@@ -18,6 +18,17 @@ export const NomiswapV2Config: DexConfigMap<DexParams> = {
18
18
  subgraphURL: '5CBKsDihF7KeBrNX4bgtb4tVFqy41PguVm88zBGpd4Hi',
19
19
  },
20
20
  },
21
+ // not exactly the same, but swapFee logic works in the same way
22
+ SwaprV2: {
23
+ [Network.GNOSIS]: {
24
+ factoryAddress: '0x5D48C95AdfFD4B40c1AAADc4e08fc44117E02179',
25
+ initCode:
26
+ '0xfcee8e246628cf9708fcb9d6f5d4aa0d96b62bb50f1af012a75a3292849e2dca',
27
+ poolGasCost: 120 * 1000,
28
+ feeCode: 25, // this is ignored as SwaprV2 uses dynamic fees,
29
+ subgraphURL: 'EWoa3JwNntAWtaLsLixTU25smp4R5tzGvs9rFXx9NHKZ',
30
+ },
31
+ },
21
32
  };
22
33
 
23
34
  export class NomiswapV2 extends UniswapV2 {
@@ -0,0 +1,283 @@
1
+ import dotenv from 'dotenv';
2
+ dotenv.config();
3
+
4
+ import { testE2E } from '../../../tests/utils-e2e';
5
+ import { Tokens, Holders } from '../../../tests/constants-e2e';
6
+ import { Network, ContractMethod, SwapSide } from '../../constants';
7
+ import { StaticJsonRpcProvider } from '@ethersproject/providers';
8
+ import { generateConfig } from '../../config';
9
+
10
+ describe('UniswapV2 E2E Gnosis', () => {
11
+ const network = Network.GNOSIS;
12
+ const tokens = Tokens[network];
13
+ const holders = Holders[network];
14
+ const provider = new StaticJsonRpcProvider(
15
+ generateConfig(network).privateHttpProvider,
16
+ network,
17
+ );
18
+
19
+ describe('SushiSwap', () => {
20
+ const dexKey = 'SushiSwap';
21
+
22
+ describe('swapExactAmountIn', () => {
23
+ it('SushiSwap ETH -> TOKEN', async () => {
24
+ await testE2E(
25
+ tokens.XDAI,
26
+ tokens.USDC,
27
+ holders.XDAI,
28
+ '700000000000000000',
29
+ SwapSide.SELL,
30
+ dexKey,
31
+ ContractMethod.swapExactAmountIn,
32
+ network,
33
+ provider,
34
+ );
35
+ });
36
+ it('SushiSwap TOKEN -> ETH', async () => {
37
+ await testE2E(
38
+ tokens.WETH,
39
+ tokens.XDAI,
40
+ holders.WETH,
41
+ '700000000000000000',
42
+ SwapSide.SELL,
43
+ dexKey,
44
+ ContractMethod.swapExactAmountIn,
45
+ network,
46
+ provider,
47
+ );
48
+ });
49
+ it('SushiSwap TOKEN -> TOKEN', async () => {
50
+ await testE2E(
51
+ tokens.WETH,
52
+ tokens.USDC,
53
+ holders.WETH,
54
+ '7000000000000000',
55
+ SwapSide.SELL,
56
+ dexKey,
57
+ ContractMethod.swapExactAmountIn,
58
+ network,
59
+ provider,
60
+ );
61
+ });
62
+ });
63
+
64
+ describe('swapExactAmountOut', () => {
65
+ it('SushiSwap ETH -> TOKEN', async () => {
66
+ await testE2E(
67
+ tokens.XDAI,
68
+ tokens.USDC,
69
+ holders.XDAI,
70
+ '700000000',
71
+ SwapSide.BUY,
72
+ dexKey,
73
+ ContractMethod.swapExactAmountOut,
74
+ network,
75
+ provider,
76
+ );
77
+ });
78
+ it('SushiSwap TOKEN -> ETH', async () => {
79
+ await testE2E(
80
+ tokens.USDC,
81
+ tokens.XDAI,
82
+ holders.USDC,
83
+ '7000000000000000000',
84
+ SwapSide.BUY,
85
+ dexKey,
86
+ ContractMethod.swapExactAmountOut,
87
+ network,
88
+ provider,
89
+ );
90
+ });
91
+ it('SushiSwap TOKEN -> TOKEN', async () => {
92
+ await testE2E(
93
+ tokens.USDC,
94
+ tokens.WETH,
95
+ holders.USDC,
96
+ '7000000000000000',
97
+ SwapSide.BUY,
98
+ dexKey,
99
+ ContractMethod.swapExactAmountOut,
100
+ network,
101
+ provider,
102
+ );
103
+ });
104
+ });
105
+ });
106
+
107
+ describe('HoneySwap', () => {
108
+ const dexKey = 'HoneySwap';
109
+
110
+ describe('swapExactAmountIn', () => {
111
+ it('SushiSwap ETH -> TOKEN', async () => {
112
+ await testE2E(
113
+ tokens.XDAI,
114
+ tokens.USDC,
115
+ holders.XDAI,
116
+ '700000000000000000',
117
+ SwapSide.SELL,
118
+ dexKey,
119
+ ContractMethod.swapExactAmountIn,
120
+ network,
121
+ provider,
122
+ );
123
+ });
124
+ it('SushiSwap TOKEN -> ETH', async () => {
125
+ await testE2E(
126
+ tokens.WETH,
127
+ tokens.XDAI,
128
+ holders.WETH,
129
+ '700000000000000000',
130
+ SwapSide.SELL,
131
+ dexKey,
132
+ ContractMethod.swapExactAmountIn,
133
+ network,
134
+ provider,
135
+ );
136
+ });
137
+ it('SushiSwap TOKEN -> TOKEN', async () => {
138
+ await testE2E(
139
+ tokens.WETH,
140
+ tokens.USDC,
141
+ holders.WETH,
142
+ '7000000000000000',
143
+ SwapSide.SELL,
144
+ dexKey,
145
+ ContractMethod.swapExactAmountIn,
146
+ network,
147
+ provider,
148
+ );
149
+ });
150
+ });
151
+
152
+ describe('swapExactAmountOut', () => {
153
+ it('SushiSwap ETH -> TOKEN', async () => {
154
+ await testE2E(
155
+ tokens.XDAI,
156
+ tokens.USDC,
157
+ holders.XDAI,
158
+ '700000000',
159
+ SwapSide.BUY,
160
+ dexKey,
161
+ ContractMethod.swapExactAmountOut,
162
+ network,
163
+ provider,
164
+ );
165
+ });
166
+ it('SushiSwap TOKEN -> ETH', async () => {
167
+ await testE2E(
168
+ tokens.USDC,
169
+ tokens.XDAI,
170
+ holders.USDC,
171
+ '7000000000000000000',
172
+ SwapSide.BUY,
173
+ dexKey,
174
+ ContractMethod.swapExactAmountOut,
175
+ network,
176
+ provider,
177
+ );
178
+ });
179
+ it('SushiSwap TOKEN -> TOKEN', async () => {
180
+ await testE2E(
181
+ tokens.USDC,
182
+ tokens.WETH,
183
+ holders.USDC,
184
+ '700000000000000',
185
+ SwapSide.BUY,
186
+ dexKey,
187
+ ContractMethod.swapExactAmountOut,
188
+ network,
189
+ provider,
190
+ );
191
+ });
192
+ });
193
+ });
194
+
195
+ describe('SwaprV2', () => {
196
+ const dexKey = 'SwaprV2';
197
+
198
+ describe('swapExactAmountIn', () => {
199
+ it('SushiSwap ETH -> TOKEN', async () => {
200
+ await testE2E(
201
+ tokens.XDAI,
202
+ tokens.USDC,
203
+ holders.XDAI,
204
+ '700000000000000000',
205
+ SwapSide.SELL,
206
+ dexKey,
207
+ ContractMethod.swapExactAmountIn,
208
+ network,
209
+ provider,
210
+ );
211
+ });
212
+ it('SwaprV2 TOKEN -> ETH', async () => {
213
+ await testE2E(
214
+ tokens.WETH,
215
+ tokens.XDAI,
216
+ holders.WETH,
217
+ '700000000000000000',
218
+ SwapSide.SELL,
219
+ dexKey,
220
+ ContractMethod.swapExactAmountIn,
221
+ network,
222
+ provider,
223
+ );
224
+ });
225
+ it('SwaprV2 TOKEN -> TOKEN', async () => {
226
+ await testE2E(
227
+ tokens.WETH,
228
+ tokens.USDC,
229
+ holders.WETH,
230
+ '7000000000000000',
231
+ SwapSide.SELL,
232
+ dexKey,
233
+ ContractMethod.swapExactAmountIn,
234
+ network,
235
+ provider,
236
+ );
237
+ });
238
+ });
239
+
240
+ describe('swapExactAmountOut', () => {
241
+ it('SwaprV2 ETH -> TOKEN', async () => {
242
+ await testE2E(
243
+ tokens.XDAI,
244
+ // SWPR/WXDAI pool has non-standard feeCode - 100
245
+ tokens.SWPR,
246
+ holders.XDAI,
247
+ '7000000000000000000',
248
+ SwapSide.BUY,
249
+ dexKey,
250
+ ContractMethod.swapExactAmountOut,
251
+ network,
252
+ provider,
253
+ );
254
+ });
255
+ it('SwaprV2 TOKEN -> ETH', async () => {
256
+ await testE2E(
257
+ tokens.USDC,
258
+ tokens.XDAI,
259
+ holders.USDC,
260
+ '7000000000000000000',
261
+ SwapSide.BUY,
262
+ dexKey,
263
+ ContractMethod.swapExactAmountOut,
264
+ network,
265
+ provider,
266
+ );
267
+ });
268
+ it('SwaprV2 TOKEN -> TOKEN', async () => {
269
+ await testE2E(
270
+ tokens.USDC,
271
+ tokens.WETH,
272
+ holders.USDC,
273
+ '700000000000000',
274
+ SwapSide.BUY,
275
+ dexKey,
276
+ ContractMethod.swapExactAmountOut,
277
+ network,
278
+ provider,
279
+ );
280
+ });
281
+ });
282
+ });
283
+ });
@@ -681,7 +681,7 @@ export class UniswapV2
681
681
  }
682
682
 
683
683
  getAdapters(side: SwapSide): { name: string; index: number }[] | null {
684
- return this.adapters[side];
684
+ return this.adapters?.[side] ?? null;
685
685
  }
686
686
 
687
687
  async getTopPoolsForToken(
@@ -346,6 +346,20 @@ export const UniswapV3Config: DexConfigMap<DexParams> = {
346
346
  subgraphURL: 'https://api.studio.thegraph.com/query/59130/v3alb/0.3',
347
347
  },
348
348
  },
349
+ OkuTradeV3: {
350
+ [Network.GNOSIS]: {
351
+ factory: '0xe32f7dd7e3f098d518ff19a22d5f028e076489b1',
352
+ quoter: '0x7E9cB3499A6cee3baBe5c8a3D328EA7FD36578f4',
353
+ router: '0xB5253c895361678FF5D0fFDdA81Dd02f1F7a81D6',
354
+ supportedFees: SUPPORTED_FEES,
355
+ stateMulticall: '0x35Db9Ac2ff3C5A86fde165Bd26D43d303417942E',
356
+ uniswapMulticall: '0x4dfa9a980efE4802E969AC33968E3d6E59B8a19e',
357
+ chunksCount: 10,
358
+ initRetryFrequency: 10,
359
+ initHash: `0xe34f199b19b2b4f47f68442619d555527d244f78a3297ea89325f843f87b8b54`,
360
+ subgraphURL: 'Dimv1udMsJu1DqirVM4G2vNRvH8CWzWTn7GffQQCGAaq',
361
+ },
362
+ },
349
363
  VelodromeSlipstream: {
350
364
  [Network.OPTIMISM]: {
351
365
  factory: '0x548118C7E0B865C2CfA94D15EC86B666468ac758',