@dolomite-exchange/zap-sdk 0.0.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 (76) hide show
  1. package/.circleci/config.yml +85 -0
  2. package/.eslintrc.js +67 -0
  3. package/LICENSE +176 -0
  4. package/README.md +22 -0
  5. package/__tests__/DolomiteZap.test.ts +552 -0
  6. package/__tests__/clients/DolomiteClient.test.ts +47 -0
  7. package/__tests__/clients/ParaswapAggregator.test.ts +34 -0
  8. package/__tests__/helpers/Setup.ts +4 -0
  9. package/__tests__/helpers/TestConstants.ts +119 -0
  10. package/__tests__/lib/LocalCache.test.ts +40 -0
  11. package/build/__tests__/DolomiteZap.test.d.ts +1 -0
  12. package/build/__tests__/DolomiteZap.test.js +526 -0
  13. package/build/__tests__/clients/DolomiteClient.test.d.ts +1 -0
  14. package/build/__tests__/clients/DolomiteClient.test.js +100 -0
  15. package/build/__tests__/clients/ParaswapAggregator.test.d.ts +1 -0
  16. package/build/__tests__/clients/ParaswapAggregator.test.js +76 -0
  17. package/build/__tests__/helpers/Setup.d.ts +0 -0
  18. package/build/__tests__/helpers/Setup.js +6 -0
  19. package/build/__tests__/helpers/TestConstants.d.ts +9 -0
  20. package/build/__tests__/helpers/TestConstants.js +109 -0
  21. package/build/__tests__/lib/LocalCache.test.d.ts +1 -0
  22. package/build/__tests__/lib/LocalCache.test.js +78 -0
  23. package/build/src/DolomiteZap.d.ts +26 -0
  24. package/build/src/DolomiteZap.js +259 -0
  25. package/build/src/abis/IDolomiteMarginExchangeWrapper.json +80 -0
  26. package/build/src/clients/AggregatorClient.d.ts +7 -0
  27. package/build/src/clients/AggregatorClient.js +10 -0
  28. package/build/src/clients/DolomiteClient.d.ts +11 -0
  29. package/build/src/clients/DolomiteClient.js +225 -0
  30. package/build/src/clients/IsolationModeClient.d.ts +16 -0
  31. package/build/src/clients/IsolationModeClient.js +47 -0
  32. package/build/src/clients/ParaswapAggregator.d.ts +7 -0
  33. package/build/src/clients/ParaswapAggregator.js +150 -0
  34. package/build/src/index.d.ts +5 -0
  35. package/build/src/index.js +51 -0
  36. package/build/src/lib/ApiTypes.d.ts +113 -0
  37. package/build/src/lib/ApiTypes.js +16 -0
  38. package/build/src/lib/Constants.d.ts +17 -0
  39. package/build/src/lib/Constants.js +118 -0
  40. package/build/src/lib/Environment.d.ts +0 -0
  41. package/build/src/lib/Environment.js +3 -0
  42. package/build/src/lib/GraphqlPageable.d.ts +4 -0
  43. package/build/src/lib/GraphqlPageable.js +73 -0
  44. package/build/src/lib/LocalCache.d.ts +9 -0
  45. package/build/src/lib/LocalCache.js +29 -0
  46. package/build/src/lib/Logger.d.ts +3 -0
  47. package/build/src/lib/Logger.js +78 -0
  48. package/build/src/lib/MathUtils.d.ts +4 -0
  49. package/build/src/lib/MathUtils.js +16 -0
  50. package/build/src/lib/Utils.d.ts +5 -0
  51. package/build/src/lib/Utils.js +84 -0
  52. package/build/src/lib/estimators/StandardEstimator.d.ts +9 -0
  53. package/build/src/lib/estimators/StandardEstimator.js +89 -0
  54. package/build/src/lib/graphql-types.d.ts +17 -0
  55. package/build/src/lib/graphql-types.js +3 -0
  56. package/jest.config.js +23 -0
  57. package/package.json +52 -0
  58. package/src/DolomiteZap.ts +235 -0
  59. package/src/abis/IDolomiteMarginExchangeWrapper.json +80 -0
  60. package/src/clients/AggregatorClient.ts +19 -0
  61. package/src/clients/DolomiteClient.ts +234 -0
  62. package/src/clients/IsolationModeClient.ts +55 -0
  63. package/src/clients/ParaswapAggregator.ts +92 -0
  64. package/src/index.ts +31 -0
  65. package/src/lib/ApiTypes.ts +130 -0
  66. package/src/lib/Constants.ts +106 -0
  67. package/src/lib/Environment.ts +1 -0
  68. package/src/lib/GraphqlPageable.ts +23 -0
  69. package/src/lib/LocalCache.ts +34 -0
  70. package/src/lib/Logger.ts +59 -0
  71. package/src/lib/MathUtils.ts +13 -0
  72. package/src/lib/Utils.ts +41 -0
  73. package/src/lib/estimators/StandardEstimator.ts +54 -0
  74. package/src/lib/graphql-types.ts +19 -0
  75. package/tsconfig.eslint.json +6 -0
  76. package/tsconfig.json +36 -0
@@ -0,0 +1,552 @@
1
+ import Deployments from '@dolomite-exchange/dolomite-margin-modules/scripts/deployments.json';
2
+ import BigNumber from 'bignumber.js';
3
+ import { ethers } from 'ethers';
4
+ import { DolomiteZap, GenericTraderType, Network } from '../src';
5
+ import { BYTES_EMPTY } from '../src/lib/Constants';
6
+ import {
7
+ ARB_MARKET,
8
+ GLP_MARKET,
9
+ MAGIC_GLP_MARKET,
10
+ PLV_GLP_MARKET,
11
+ setUnwrapperMarketIdByMarketId,
12
+ USDC_MARKET,
13
+ WETH_MARKET,
14
+ } from './helpers/TestConstants';
15
+
16
+ const txOrigin = '0x52256ef863a713Ef349ae6E97A7E8f35785145dE';
17
+
18
+ describe('DolomiteZap', () => {
19
+ const network = Network.ARBITRUM_ONE;
20
+ const subgraphUrl = process.env.SUBGRAPH_URL;
21
+ if (!subgraphUrl) {
22
+ throw new Error('SUBGRAPH_URL env var not set')
23
+ }
24
+ const web3Provider = new ethers.providers.JsonRpcProvider(process.env.WEB3_PROVIDER_URL);
25
+ const NO_CACHE = -1;
26
+ const zap = new DolomiteZap(network, subgraphUrl, web3Provider, NO_CACHE);
27
+
28
+ describe('#getSwapExactTokensForTokensData', () => {
29
+ it('should work when there is no Isolation Mode tokens nor Liquidity tokens involved', async () => {
30
+ const amountIn = new BigNumber('1000000000000000000'); // 1 ETH
31
+ const minAmountOut = new BigNumber('100000000'); // 100 USDC
32
+ const outputParams = await zap.getSwapExactTokensForTokensParams(
33
+ WETH_MARKET,
34
+ amountIn,
35
+ USDC_MARKET,
36
+ minAmountOut,
37
+ txOrigin,
38
+ );
39
+
40
+ expect(outputParams.length).toBe(1);
41
+
42
+ const outputParam = outputParams[0];
43
+ expect(outputParam.marketIdsPath.length).toEqual(2);
44
+ expect(outputParam.marketIdsPath[0]).toEqual(WETH_MARKET.marketId);
45
+ expect(outputParam.marketIdsPath[1]).toEqual(USDC_MARKET.marketId);
46
+
47
+ expect(outputParam.amountWeisPath.length).toEqual(2);
48
+ expect(outputParam.amountWeisPath[0]).toEqual(amountIn);
49
+ expect(outputParam.amountWeisPath[1].isGreaterThan(minAmountOut)).toBeTruthy();
50
+
51
+ expect(outputParam.traderParams.length).toEqual(1);
52
+ expect(outputParam.traderParams[0].traderType).toEqual(GenericTraderType.ExternalLiquidity);
53
+ expect(outputParam.traderParams[0].makerAccountIndex).toEqual(0);
54
+ expect(outputParam.traderParams[0].trader).toEqual(Deployments.ParaswapAggregatorTrader[network].address);
55
+ expect(outputParam.traderParams[0].tradeData.length).toBeGreaterThan(100);
56
+
57
+ expect(outputParam.makerAccounts.length).toEqual(0);
58
+
59
+ expect(outputParam.amountOutMin).toEqual(minAmountOut);
60
+ });
61
+
62
+ it('should work when there is an Isolation Mode token to be unwrapped and no aggregator', async () => {
63
+ const amountIn = new BigNumber('1000000000000000000'); // 100 GLP
64
+ const minAmountOut = new BigNumber('1000000'); // 1 USDC
65
+ const outputParams = await zap.getSwapExactTokensForTokensParams(
66
+ GLP_MARKET,
67
+ amountIn,
68
+ USDC_MARKET,
69
+ minAmountOut,
70
+ txOrigin,
71
+ );
72
+
73
+ expect(outputParams.length).toBe(1);
74
+
75
+ const outputParam = outputParams[0];
76
+ expect(outputParam.marketIdsPath.length).toEqual(2);
77
+ expect(outputParam.marketIdsPath[0]).toEqual(GLP_MARKET.marketId);
78
+ expect(outputParam.marketIdsPath[1]).toEqual(USDC_MARKET.marketId);
79
+
80
+ expect(outputParam.amountWeisPath.length).toEqual(2);
81
+ expect(outputParam.amountWeisPath[0]).toEqual(amountIn);
82
+ expect(outputParam.amountWeisPath[1].isGreaterThan(minAmountOut)).toBeTruthy();
83
+
84
+ expect(outputParam.traderParams.length).toEqual(1);
85
+ expect(outputParam.traderParams[0].traderType).toEqual(GenericTraderType.IsolationModeUnwrapper);
86
+ expect(outputParam.traderParams[0].makerAccountIndex).toEqual(0);
87
+ expect(outputParam.traderParams[0].trader)
88
+ .toEqual(Deployments.GLPIsolationModeUnwrapperTraderV2[network].address);
89
+ expect(outputParam.traderParams[0].tradeData).toEqual(BYTES_EMPTY);
90
+
91
+ expect(outputParam.makerAccounts.length).toEqual(0);
92
+ expect(outputParam.amountOutMin).toEqual(minAmountOut);
93
+ });
94
+
95
+ it('should work when there is an Isolation Mode token to be wrapped and no aggregator', async () => {
96
+ const amountIn = new BigNumber('100000000'); // 100 USDC
97
+ const minAmountOut = new BigNumber('50000000000000000000'); // 50 GLP
98
+ const outputParams = await zap.getSwapExactTokensForTokensParams(
99
+ USDC_MARKET,
100
+ amountIn,
101
+ GLP_MARKET,
102
+ minAmountOut,
103
+ txOrigin,
104
+ );
105
+
106
+ expect(outputParams.length).toBe(1);
107
+
108
+ const outputParam = outputParams[0];
109
+ expect(outputParam.marketIdsPath.length).toEqual(2);
110
+ expect(outputParam.marketIdsPath[0]).toEqual(USDC_MARKET.marketId);
111
+ expect(outputParam.marketIdsPath[1]).toEqual(GLP_MARKET.marketId);
112
+
113
+ expect(outputParam.amountWeisPath.length).toEqual(2);
114
+ expect(outputParam.amountWeisPath[0]).toEqual(amountIn);
115
+ expect(outputParam.amountWeisPath[1].isGreaterThan(minAmountOut)).toBeTruthy();
116
+
117
+ expect(outputParam.traderParams.length).toEqual(1);
118
+ expect(outputParam.traderParams[0].traderType).toEqual(GenericTraderType.IsolationModeWrapper);
119
+ expect(outputParam.traderParams[0].makerAccountIndex).toEqual(0);
120
+ expect(outputParam.traderParams[0].trader)
121
+ .toEqual(Deployments.GLPIsolationModeWrapperTraderV2[network].address);
122
+ expect(outputParam.traderParams[0].tradeData).toEqual(BYTES_EMPTY);
123
+
124
+ expect(outputParam.makerAccounts.length).toEqual(0);
125
+ expect(outputParam.amountOutMin).toEqual(minAmountOut);
126
+ });
127
+
128
+ it('should work when there is an Liquidity Token to be unwrapped and no aggregator', async () => {
129
+ const amountIn = new BigNumber('1000000000000000000'); // 100 mGLP
130
+ const minAmountOut = new BigNumber('1000000'); // 1 USDC
131
+ const outputParams = await zap.getSwapExactTokensForTokensParams(
132
+ MAGIC_GLP_MARKET,
133
+ amountIn,
134
+ USDC_MARKET,
135
+ minAmountOut,
136
+ txOrigin,
137
+ );
138
+
139
+ expect(outputParams.length).toBe(1);
140
+
141
+ const outputParam = outputParams[0];
142
+ expect(outputParam.marketIdsPath.length).toEqual(2);
143
+ expect(outputParam.marketIdsPath[0]).toEqual(MAGIC_GLP_MARKET.marketId);
144
+ expect(outputParam.marketIdsPath[1]).toEqual(USDC_MARKET.marketId);
145
+
146
+ expect(outputParam.amountWeisPath.length).toEqual(2);
147
+ expect(outputParam.amountWeisPath[0]).toEqual(amountIn);
148
+ expect(outputParam.amountWeisPath[1].isGreaterThan(minAmountOut)).toBeTruthy();
149
+
150
+ expect(outputParam.traderParams.length).toEqual(1);
151
+ expect(outputParam.traderParams[0].traderType).toEqual(GenericTraderType.ExternalLiquidity);
152
+ expect(outputParam.traderParams[0].makerAccountIndex).toEqual(0);
153
+ expect(outputParam.traderParams[0].trader)
154
+ .toEqual(Deployments.MagicGLPUnwrapperTraderV2[network].address);
155
+ expect(outputParam.traderParams[0].tradeData).toEqual(BYTES_EMPTY);
156
+
157
+ expect(outputParam.makerAccounts.length).toEqual(0);
158
+ expect(outputParam.amountOutMin).toEqual(minAmountOut);
159
+ });
160
+
161
+ it('should work when there is an Liquidity Token to be wrapped and no aggregator', async () => {
162
+ const amountIn = new BigNumber('100000000'); // 100 USDC
163
+ const minAmountOut = new BigNumber('50000000000000000000'); // 50 mGLP
164
+ const outputParams = await zap.getSwapExactTokensForTokensParams(
165
+ USDC_MARKET,
166
+ amountIn,
167
+ MAGIC_GLP_MARKET,
168
+ minAmountOut,
169
+ txOrigin,
170
+ );
171
+
172
+ expect(outputParams.length).toBe(1);
173
+
174
+ const outputParam = outputParams[0];
175
+ expect(outputParam.marketIdsPath.length).toEqual(2);
176
+ expect(outputParam.marketIdsPath[0]).toEqual(USDC_MARKET.marketId);
177
+ expect(outputParam.marketIdsPath[1]).toEqual(MAGIC_GLP_MARKET.marketId);
178
+
179
+ expect(outputParam.amountWeisPath.length).toEqual(2);
180
+ expect(outputParam.amountWeisPath[0]).toEqual(amountIn);
181
+ expect(outputParam.amountWeisPath[1].isGreaterThan(minAmountOut)).toBeTruthy();
182
+
183
+ expect(outputParam.traderParams.length).toEqual(1);
184
+ expect(outputParam.traderParams[0].traderType).toEqual(GenericTraderType.ExternalLiquidity);
185
+ expect(outputParam.traderParams[0].makerAccountIndex).toEqual(0);
186
+ expect(outputParam.traderParams[0].trader)
187
+ .toEqual(Deployments.MagicGLPWrapperTraderV2[network].address);
188
+ expect(outputParam.traderParams[0].tradeData).toEqual(BYTES_EMPTY);
189
+
190
+ expect(outputParam.makerAccounts.length).toEqual(0);
191
+ expect(outputParam.amountOutMin).toEqual(minAmountOut);
192
+ });
193
+
194
+ it('should work when there is an Isolation Mode token to be unwrapped and uses an aggregator', async () => {
195
+ const amountIn = new BigNumber('100000000000000000000'); // 100 GLP
196
+ const minAmountOut = new BigNumber('1000000000000000000'); // 1 ARB
197
+ const outputParams = await zap.getSwapExactTokensForTokensParams(
198
+ GLP_MARKET,
199
+ amountIn,
200
+ ARB_MARKET,
201
+ minAmountOut,
202
+ txOrigin,
203
+ );
204
+
205
+ expect(outputParams.length).toBe(1);
206
+
207
+ const outputParam = outputParams[0];
208
+ expect(outputParam.marketIdsPath.length).toEqual(3);
209
+ expect(outputParam.marketIdsPath[0]).toEqual(GLP_MARKET.marketId);
210
+ expect(outputParam.marketIdsPath[1]).toEqual(USDC_MARKET.marketId);
211
+ expect(outputParam.marketIdsPath[2]).toEqual(ARB_MARKET.marketId);
212
+
213
+ const _50_USDC = new BigNumber('50000000');
214
+ expect(outputParam.amountWeisPath.length).toEqual(3);
215
+ expect(outputParam.amountWeisPath[0]).toEqual(amountIn);
216
+ expect(outputParam.amountWeisPath[1].isGreaterThan(_50_USDC)).toBeTruthy();
217
+ expect(outputParam.amountWeisPath[2].isGreaterThan(minAmountOut)).toBeTruthy();
218
+
219
+ expect(outputParam.traderParams.length).toEqual(2);
220
+ expect(outputParam.traderParams[0].traderType).toEqual(GenericTraderType.IsolationModeUnwrapper);
221
+ expect(outputParam.traderParams[0].makerAccountIndex).toEqual(0);
222
+ expect(outputParam.traderParams[0].trader)
223
+ .toEqual(Deployments.GLPIsolationModeUnwrapperTraderV2[network].address);
224
+ expect(outputParam.traderParams[0].tradeData).toEqual(BYTES_EMPTY);
225
+
226
+ expect(outputParam.traderParams[1].traderType).toEqual(GenericTraderType.ExternalLiquidity);
227
+ expect(outputParam.traderParams[1].makerAccountIndex).toEqual(0);
228
+ expect(outputParam.traderParams[1].trader).toEqual(Deployments.ParaswapAggregatorTrader[network].address);
229
+ expect(outputParam.traderParams[1].tradeData.length).toBeGreaterThan(100);
230
+
231
+ expect(outputParam.makerAccounts.length).toEqual(0);
232
+ expect(outputParam.amountOutMin).toEqual(minAmountOut);
233
+ });
234
+
235
+ it('should work when there is an Isolation Mode token to be wrapped and uses an aggregator', async () => {
236
+ const amountIn = new BigNumber('100000000000000000000'); // 100 ARB
237
+ const minAmountOut = new BigNumber('50000000000000000000'); // 50 GLP
238
+ const outputParams = await zap.getSwapExactTokensForTokensParams(
239
+ ARB_MARKET,
240
+ amountIn,
241
+ GLP_MARKET,
242
+ minAmountOut,
243
+ txOrigin,
244
+ );
245
+
246
+ expect(outputParams.length).toBe(1);
247
+
248
+ const outputParam = outputParams[0];
249
+ expect(outputParam.marketIdsPath.length).toEqual(3);
250
+ expect(outputParam.marketIdsPath[0]).toEqual(ARB_MARKET.marketId);
251
+ expect(outputParam.marketIdsPath[1]).toEqual(USDC_MARKET.marketId);
252
+ expect(outputParam.marketIdsPath[2]).toEqual(GLP_MARKET.marketId);
253
+
254
+ const _50_USDC = new BigNumber('50000000');
255
+ expect(outputParam.amountWeisPath.length).toEqual(3);
256
+ expect(outputParam.amountWeisPath[0]).toEqual(amountIn);
257
+ expect(outputParam.amountWeisPath[1].isGreaterThan(_50_USDC)).toBeTruthy();
258
+ expect(outputParam.amountWeisPath[2].isGreaterThan(minAmountOut)).toBeTruthy();
259
+
260
+ expect(outputParam.traderParams.length).toEqual(2);
261
+ expect(outputParam.traderParams[0].traderType).toEqual(GenericTraderType.ExternalLiquidity);
262
+ expect(outputParam.traderParams[0].makerAccountIndex).toEqual(0);
263
+ expect(outputParam.traderParams[0].trader).toEqual(Deployments.ParaswapAggregatorTrader[network].address);
264
+ expect(outputParam.traderParams[0].tradeData.length).toBeGreaterThan(100);
265
+
266
+ expect(outputParam.traderParams[1].traderType).toEqual(GenericTraderType.IsolationModeWrapper);
267
+ expect(outputParam.traderParams[1].makerAccountIndex).toEqual(0);
268
+ expect(outputParam.traderParams[1].trader)
269
+ .toEqual(Deployments.GLPIsolationModeWrapperTraderV2[network].address);
270
+ expect(outputParam.traderParams[1].tradeData).toEqual(BYTES_EMPTY);
271
+
272
+ expect(outputParam.makerAccounts.length).toEqual(0);
273
+ expect(outputParam.amountOutMin).toEqual(minAmountOut);
274
+ });
275
+
276
+ it('should work when there is an Liquidity Token to be unwrapped and uses an aggregator', async () => {
277
+ const amountIn = new BigNumber('100000000000000000000'); // 100 mGLP
278
+ const minAmountOut = new BigNumber('1000000000000000000'); // 1 ARB
279
+ const outputParams = await zap.getSwapExactTokensForTokensParams(
280
+ MAGIC_GLP_MARKET,
281
+ amountIn,
282
+ ARB_MARKET,
283
+ minAmountOut,
284
+ txOrigin,
285
+ );
286
+
287
+ expect(outputParams.length).toBe(1);
288
+
289
+ const outputParam = outputParams[0];
290
+ expect(outputParam.marketIdsPath.length).toEqual(3);
291
+ expect(outputParam.marketIdsPath[0]).toEqual(MAGIC_GLP_MARKET.marketId);
292
+ expect(outputParam.marketIdsPath[1]).toEqual(USDC_MARKET.marketId);
293
+ expect(outputParam.marketIdsPath[2]).toEqual(ARB_MARKET.marketId);
294
+
295
+ const _50_USDC = new BigNumber('50000000');
296
+ expect(outputParam.amountWeisPath.length).toEqual(3);
297
+ expect(outputParam.amountWeisPath[0]).toEqual(amountIn);
298
+ expect(outputParam.amountWeisPath[1].isGreaterThan(_50_USDC)).toBeTruthy();
299
+ expect(outputParam.amountWeisPath[2].isGreaterThan(minAmountOut)).toBeTruthy();
300
+
301
+ expect(outputParam.traderParams.length).toEqual(2);
302
+ expect(outputParam.traderParams[0].traderType).toEqual(GenericTraderType.ExternalLiquidity);
303
+ expect(outputParam.traderParams[0].makerAccountIndex).toEqual(0);
304
+ expect(outputParam.traderParams[0].trader)
305
+ .toEqual(Deployments.MagicGLPUnwrapperTraderV2[network].address);
306
+ expect(outputParam.traderParams[0].tradeData).toEqual(BYTES_EMPTY);
307
+
308
+ expect(outputParam.traderParams[1].traderType).toEqual(GenericTraderType.ExternalLiquidity);
309
+ expect(outputParam.traderParams[1].makerAccountIndex).toEqual(0);
310
+ expect(outputParam.traderParams[1].trader).toEqual(Deployments.ParaswapAggregatorTrader[network].address);
311
+ expect(outputParam.traderParams[1].tradeData.length).toBeGreaterThan(100);
312
+
313
+ expect(outputParam.makerAccounts.length).toEqual(0);
314
+ expect(outputParam.amountOutMin).toEqual(minAmountOut);
315
+ });
316
+
317
+ it('should work when there is an Liquidity Token to be wrapped and uses an aggregator', async () => {
318
+ const amountIn = new BigNumber('100000000000000000000'); // 100 ARB
319
+ const minAmountOut = new BigNumber('50000000000000000000'); // 50 GLP
320
+ const outputParams = await zap.getSwapExactTokensForTokensParams(
321
+ ARB_MARKET,
322
+ amountIn,
323
+ MAGIC_GLP_MARKET,
324
+ minAmountOut,
325
+ txOrigin,
326
+ );
327
+
328
+ expect(outputParams.length).toBe(1);
329
+
330
+ const outputParam = outputParams[0];
331
+ expect(outputParam.marketIdsPath.length).toEqual(3);
332
+ expect(outputParam.marketIdsPath[0]).toEqual(ARB_MARKET.marketId);
333
+ expect(outputParam.marketIdsPath[1]).toEqual(USDC_MARKET.marketId);
334
+ expect(outputParam.marketIdsPath[2]).toEqual(MAGIC_GLP_MARKET.marketId);
335
+
336
+ const _50_USDC = new BigNumber('50000000');
337
+ expect(outputParam.amountWeisPath.length).toEqual(3);
338
+ expect(outputParam.amountWeisPath[0]).toEqual(amountIn);
339
+ expect(outputParam.amountWeisPath[1].isGreaterThan(_50_USDC)).toBeTruthy();
340
+ expect(outputParam.amountWeisPath[2].isGreaterThan(minAmountOut)).toBeTruthy();
341
+
342
+ expect(outputParam.traderParams.length).toEqual(2);
343
+ expect(outputParam.traderParams[0].traderType).toEqual(GenericTraderType.ExternalLiquidity);
344
+ expect(outputParam.traderParams[0].makerAccountIndex).toEqual(0);
345
+ expect(outputParam.traderParams[0].trader).toEqual(Deployments.ParaswapAggregatorTrader[network].address);
346
+ expect(outputParam.traderParams[0].tradeData.length).toBeGreaterThan(100);
347
+
348
+ expect(outputParam.traderParams[1].traderType).toEqual(GenericTraderType.ExternalLiquidity);
349
+ expect(outputParam.traderParams[1].makerAccountIndex).toEqual(0);
350
+ expect(outputParam.traderParams[1].trader)
351
+ .toEqual(Deployments.MagicGLPWrapperTraderV2[network].address);
352
+ expect(outputParam.traderParams[1].tradeData).toEqual(BYTES_EMPTY);
353
+
354
+ expect(outputParam.makerAccounts.length).toEqual(0);
355
+ expect(outputParam.amountOutMin).toEqual(minAmountOut);
356
+ });
357
+
358
+ it('should work when there is an Isolation Mode token to be unwrapped into a wrapper', async () => {
359
+ const amountIn = new BigNumber('100000000000000000000'); // 100 GLP
360
+ const minAmountOut = new BigNumber('50000000000000000000'); // 50 plvGLP
361
+ const outputParams = await zap.getSwapExactTokensForTokensParams(
362
+ GLP_MARKET,
363
+ amountIn,
364
+ PLV_GLP_MARKET,
365
+ minAmountOut,
366
+ txOrigin,
367
+ );
368
+
369
+ expect(outputParams.length).toBe(1);
370
+
371
+ const outputParam = outputParams[0];
372
+ expect(outputParam.marketIdsPath.length).toEqual(3);
373
+ expect(outputParam.marketIdsPath[0]).toEqual(GLP_MARKET.marketId);
374
+ expect(outputParam.marketIdsPath[1]).toEqual(USDC_MARKET.marketId);
375
+ expect(outputParam.marketIdsPath[2]).toEqual(PLV_GLP_MARKET.marketId);
376
+
377
+ const _50_USDC = new BigNumber('50000000');
378
+ expect(outputParam.amountWeisPath.length).toEqual(3);
379
+ expect(outputParam.amountWeisPath[0]).toEqual(amountIn);
380
+ expect(outputParam.amountWeisPath[1].isGreaterThan(_50_USDC)).toBeTruthy();
381
+ expect(outputParam.amountWeisPath[2].isGreaterThan(minAmountOut)).toBeTruthy();
382
+
383
+ expect(outputParam.traderParams.length).toEqual(2);
384
+ expect(outputParam.traderParams[0].traderType).toEqual(GenericTraderType.IsolationModeUnwrapper);
385
+ expect(outputParam.traderParams[0].makerAccountIndex).toEqual(0);
386
+ expect(outputParam.traderParams[0].trader)
387
+ .toEqual(Deployments.GLPIsolationModeUnwrapperTraderV2[network].address);
388
+ expect(outputParam.traderParams[0].tradeData).toEqual(BYTES_EMPTY);
389
+
390
+ expect(outputParam.traderParams[1].traderType).toEqual(GenericTraderType.IsolationModeWrapper);
391
+ expect(outputParam.traderParams[1].makerAccountIndex).toEqual(0);
392
+ expect(outputParam.traderParams[1].trader)
393
+ .toEqual(Deployments.PlutusVaultGLPIsolationModeWrapperTraderV2[network].address);
394
+ expect(outputParam.traderParams[1].tradeData).toEqual(BYTES_EMPTY);
395
+
396
+ expect(outputParam.makerAccounts.length).toEqual(0);
397
+ expect(outputParam.amountOutMin).toEqual(minAmountOut);
398
+ });
399
+
400
+ it('should work when there is an Isolation Mode token to be unwrapped into a Liquidity Token', async () => {
401
+ const amountIn = new BigNumber('100000000000000000000'); // 100 GLP
402
+ const minAmountOut = new BigNumber('50000000000000000000'); // 50 mGLP
403
+ const outputParams = await zap.getSwapExactTokensForTokensParams(
404
+ GLP_MARKET,
405
+ amountIn,
406
+ MAGIC_GLP_MARKET,
407
+ minAmountOut,
408
+ txOrigin,
409
+ );
410
+
411
+ expect(outputParams.length).toBe(1);
412
+
413
+ const outputParam = outputParams[0];
414
+ expect(outputParam.marketIdsPath.length).toEqual(3);
415
+ expect(outputParam.marketIdsPath[0]).toEqual(GLP_MARKET.marketId);
416
+ expect(outputParam.marketIdsPath[1]).toEqual(USDC_MARKET.marketId);
417
+ expect(outputParam.marketIdsPath[2]).toEqual(MAGIC_GLP_MARKET.marketId);
418
+
419
+ const _50_USDC = new BigNumber('50000000');
420
+ expect(outputParam.amountWeisPath.length).toEqual(3);
421
+ expect(outputParam.amountWeisPath[0]).toEqual(amountIn);
422
+ expect(outputParam.amountWeisPath[1].isGreaterThan(_50_USDC)).toBeTruthy();
423
+ expect(outputParam.amountWeisPath[2].isGreaterThan(minAmountOut)).toBeTruthy();
424
+
425
+ expect(outputParam.traderParams.length).toEqual(2);
426
+ expect(outputParam.traderParams[0].traderType).toEqual(GenericTraderType.IsolationModeUnwrapper);
427
+ expect(outputParam.traderParams[0].makerAccountIndex).toEqual(0);
428
+ expect(outputParam.traderParams[0].trader)
429
+ .toEqual(Deployments.GLPIsolationModeUnwrapperTraderV2[network].address);
430
+ expect(outputParam.traderParams[0].tradeData).toEqual(BYTES_EMPTY);
431
+
432
+ expect(outputParam.traderParams[1].traderType).toEqual(GenericTraderType.ExternalLiquidity);
433
+ expect(outputParam.traderParams[1].makerAccountIndex).toEqual(0);
434
+ expect(outputParam.traderParams[1].trader)
435
+ .toEqual(Deployments.MagicGLPWrapperTraderV2[network].address);
436
+ expect(outputParam.traderParams[1].tradeData).toEqual(BYTES_EMPTY);
437
+
438
+ expect(outputParam.makerAccounts.length).toEqual(0);
439
+ expect(outputParam.amountOutMin).toEqual(minAmountOut);
440
+ });
441
+
442
+ it(
443
+ 'should work when there is an Isolation Mode token to be unwrapped into a wrapper and uses an aggregator',
444
+ async () => {
445
+ setUnwrapperMarketIdByMarketId(GLP_MARKET.marketId, WETH_MARKET.marketId, network);
446
+
447
+ const amountIn = new BigNumber('100000000000000000000'); // 100 GLP
448
+ const minAmountOut = new BigNumber('50000000000000000000'); // 50 mGLP
449
+ const outputParams = await zap.getSwapExactTokensForTokensParams(
450
+ GLP_MARKET,
451
+ amountIn,
452
+ MAGIC_GLP_MARKET,
453
+ minAmountOut,
454
+ txOrigin,
455
+ );
456
+
457
+ expect(outputParams.length).toBe(1);
458
+
459
+ const outputParam = outputParams[0];
460
+ expect(outputParam.marketIdsPath.length).toEqual(4);
461
+ expect(outputParam.marketIdsPath[0]).toEqual(GLP_MARKET.marketId);
462
+ expect(outputParam.marketIdsPath[1]).toEqual(WETH_MARKET.marketId);
463
+ expect(outputParam.marketIdsPath[2]).toEqual(USDC_MARKET.marketId);
464
+ expect(outputParam.marketIdsPath[3]).toEqual(MAGIC_GLP_MARKET.marketId);
465
+
466
+ const _0_025_WETH = new BigNumber('25000000000000000');
467
+ const _50_USDC = new BigNumber('50000000');
468
+ expect(outputParam.amountWeisPath.length).toEqual(4);
469
+ expect(outputParam.amountWeisPath[0]).toEqual(amountIn);
470
+ expect(outputParam.amountWeisPath[1].isGreaterThan(_0_025_WETH)).toBeTruthy();
471
+ expect(outputParam.amountWeisPath[2].isGreaterThan(_50_USDC)).toBeTruthy();
472
+ expect(outputParam.amountWeisPath[3].isGreaterThan(minAmountOut)).toBeTruthy();
473
+
474
+ expect(outputParam.traderParams.length).toEqual(3);
475
+ expect(outputParam.traderParams[0].traderType).toEqual(GenericTraderType.IsolationModeUnwrapper);
476
+ expect(outputParam.traderParams[0].makerAccountIndex).toEqual(0);
477
+ expect(outputParam.traderParams[0].trader)
478
+ .toEqual(Deployments.GLPIsolationModeUnwrapperTraderV2[network].address);
479
+ expect(outputParam.traderParams[0].tradeData).toEqual(BYTES_EMPTY);
480
+
481
+ expect(outputParam.traderParams[1].traderType).toEqual(GenericTraderType.ExternalLiquidity);
482
+ expect(outputParam.traderParams[1].makerAccountIndex).toEqual(0);
483
+ expect(outputParam.traderParams[1].trader).toEqual(Deployments.ParaswapAggregatorTrader[network].address);
484
+ expect(outputParam.traderParams[1].tradeData.length).toBeGreaterThan(100);
485
+
486
+ expect(outputParam.traderParams[2].traderType).toEqual(GenericTraderType.ExternalLiquidity);
487
+ expect(outputParam.traderParams[2].makerAccountIndex).toEqual(0);
488
+ expect(outputParam.traderParams[2].trader)
489
+ .toEqual(Deployments.MagicGLPWrapperTraderV2[network].address);
490
+ expect(outputParam.traderParams[2].tradeData).toEqual(BYTES_EMPTY);
491
+
492
+ expect(outputParam.makerAccounts.length).toEqual(0);
493
+ expect(outputParam.amountOutMin).toEqual(minAmountOut);
494
+ },
495
+ );
496
+
497
+ it(
498
+ 'should work when there is an Isolation Mode token to be unwrapped into a Liquidity Token and uses an aggregator',
499
+ async () => {
500
+ setUnwrapperMarketIdByMarketId(GLP_MARKET.marketId, WETH_MARKET.marketId, network);
501
+
502
+ const amountIn = new BigNumber('100000000000000000000'); // 100 GLP
503
+ const minAmountOut = new BigNumber('50000000000000000000'); // 50 mGLP
504
+ const outputParams = await zap.getSwapExactTokensForTokensParams(
505
+ GLP_MARKET,
506
+ amountIn,
507
+ MAGIC_GLP_MARKET,
508
+ minAmountOut,
509
+ txOrigin,
510
+ );
511
+
512
+ expect(outputParams.length).toBe(1);
513
+
514
+ const outputParam = outputParams[0];
515
+ expect(outputParam.marketIdsPath.length).toEqual(4);
516
+ expect(outputParam.marketIdsPath[0]).toEqual(GLP_MARKET.marketId);
517
+ expect(outputParam.marketIdsPath[1]).toEqual(WETH_MARKET.marketId);
518
+ expect(outputParam.marketIdsPath[2]).toEqual(USDC_MARKET.marketId);
519
+ expect(outputParam.marketIdsPath[3]).toEqual(MAGIC_GLP_MARKET.marketId);
520
+
521
+ const _0_025_WETH = new BigNumber('25000000000000000');
522
+ const _50_USDC = new BigNumber('50000000');
523
+ expect(outputParam.amountWeisPath.length).toEqual(4);
524
+ expect(outputParam.amountWeisPath[0]).toEqual(amountIn);
525
+ expect(outputParam.amountWeisPath[1].isGreaterThan(_0_025_WETH)).toBeTruthy();
526
+ expect(outputParam.amountWeisPath[2].isGreaterThan(_50_USDC)).toBeTruthy();
527
+ expect(outputParam.amountWeisPath[3].isGreaterThan(minAmountOut)).toBeTruthy();
528
+
529
+ expect(outputParam.traderParams.length).toEqual(3);
530
+ expect(outputParam.traderParams[0].traderType).toEqual(GenericTraderType.IsolationModeUnwrapper);
531
+ expect(outputParam.traderParams[0].makerAccountIndex).toEqual(0);
532
+ expect(outputParam.traderParams[0].trader)
533
+ .toEqual(Deployments.GLPIsolationModeUnwrapperTraderV2[network].address);
534
+ expect(outputParam.traderParams[0].tradeData).toEqual(BYTES_EMPTY);
535
+
536
+ expect(outputParam.traderParams[1].traderType).toEqual(GenericTraderType.ExternalLiquidity);
537
+ expect(outputParam.traderParams[1].makerAccountIndex).toEqual(0);
538
+ expect(outputParam.traderParams[1].trader).toEqual(Deployments.ParaswapAggregatorTrader[network].address);
539
+ expect(outputParam.traderParams[1].tradeData.length).toBeGreaterThan(100);
540
+
541
+ expect(outputParam.traderParams[2].traderType).toEqual(GenericTraderType.ExternalLiquidity);
542
+ expect(outputParam.traderParams[2].makerAccountIndex).toEqual(0);
543
+ expect(outputParam.traderParams[2].trader)
544
+ .toEqual(Deployments.MagicGLPWrapperTraderV2[network].address);
545
+ expect(outputParam.traderParams[2].tradeData).toEqual(BYTES_EMPTY);
546
+
547
+ expect(outputParam.makerAccounts.length).toEqual(0);
548
+ expect(outputParam.amountOutMin).toEqual(minAmountOut);
549
+ },
550
+ );
551
+ });
552
+ });
@@ -0,0 +1,47 @@
1
+ import { ethers } from 'ethers';
2
+ import { Network } from '../../src';
3
+ import DolomiteClient from '../../src/clients/DolomiteClient';
4
+
5
+ describe('DolomiteClient', () => {
6
+ const networkId = Network.ARBITRUM_ONE;
7
+ const subgraphUrl = process.env.SUBGRAPH_URL;
8
+ if (!subgraphUrl) {
9
+ throw new Error('SUBGRAPH_URL env var not set')
10
+ }
11
+ const web3Provider = new ethers.providers.JsonRpcProvider(process.env.WEB3_PROVIDER_URL);
12
+ const dolomite = new DolomiteClient(subgraphUrl, networkId, web3Provider);
13
+
14
+ describe('#getDolomiteMarkets', () => {
15
+ it('should work for specific block number', async () => {
16
+ const blockNumber = 100_000_000;
17
+ const marketsMap = await dolomite.getDolomiteMarketsMap(blockNumber);
18
+ expect(Object.keys(marketsMap).length).toEqual(10);
19
+ });
20
+
21
+ it('should work for latest block tag', async () => {
22
+ const marketsMap = await dolomite.getDolomiteMarketsMap('latest');
23
+ expect(Object.keys(marketsMap).length).toBeGreaterThanOrEqual(10);
24
+
25
+ const dfsGlp = marketsMap['6'];
26
+ expect(dfsGlp).toBeDefined();
27
+ expect(dfsGlp!.isolationModeUnwrapperInfo).toBeDefined();
28
+ expect(dfsGlp!.isolationModeWrapperInfo).toBeDefined();
29
+ expect(dfsGlp!.liquidityTokenWrapperInfo).toBeUndefined();
30
+ expect(dfsGlp!.liquidityTokenWrapperInfo).toBeUndefined();
31
+
32
+ const dplvGlp = marketsMap['9'];
33
+ expect(dplvGlp).toBeDefined();
34
+ expect(dplvGlp!.isolationModeUnwrapperInfo).toBeDefined();
35
+ expect(dplvGlp!.isolationModeWrapperInfo).toBeDefined();
36
+ expect(dplvGlp!.liquidityTokenWrapperInfo).toBeUndefined();
37
+ expect(dplvGlp!.liquidityTokenWrapperInfo).toBeUndefined();
38
+
39
+ const mGlp = marketsMap['8'];
40
+ expect(mGlp).toBeDefined();
41
+ expect(mGlp!.isolationModeUnwrapperInfo).toBeUndefined();
42
+ expect(mGlp!.isolationModeWrapperInfo).toBeUndefined();
43
+ expect(mGlp!.liquidityTokenWrapperInfo).toBeDefined();
44
+ expect(mGlp!.liquidityTokenWrapperInfo).toBeDefined();
45
+ });
46
+ });
47
+ });
@@ -0,0 +1,34 @@
1
+ import Deployments from '@dolomite-exchange/dolomite-margin-modules/scripts/deployments.json';
2
+ import BigNumber from 'bignumber.js';
3
+ import { ApiMarket, Network } from '../../src';
4
+ import ParaswapAggregator from '../../src/clients/ParaswapAggregator';
5
+ import { USDC_MARKET, WETH_MARKET } from '../helpers/TestConstants';
6
+
7
+ describe('ParaswapAggregator', () => {
8
+ const networkIdOverride = Network.ARBITRUM_ONE;
9
+ const paraswap = new ParaswapAggregator(networkIdOverride);
10
+
11
+ describe('#getSwapExactTokensForTokensData', () => {
12
+ it('should work under normal conditions', async () => {
13
+ const inputMarket: ApiMarket = WETH_MARKET;
14
+ const outputMarket: ApiMarket = USDC_MARKET;
15
+ const inputAmount = new BigNumber('1000000000000000000'); // 1 ETH
16
+ const minOutputAmount = new BigNumber('100000000'); // 100 USDC
17
+ const solidAccount = '0x70997970C51812dc3A010C7d01b50e0d17dc79C8';
18
+ const aggregatorOutput = await paraswap.getSwapExactTokensForTokensData(
19
+ inputMarket,
20
+ inputAmount,
21
+ outputMarket,
22
+ minOutputAmount,
23
+ solidAccount,
24
+ );
25
+ expect(aggregatorOutput).toBeDefined();
26
+
27
+ const { tradeData, traderAddress, expectedAmountOut } = aggregatorOutput!;
28
+ expect(tradeData).toBeDefined();
29
+ expect(tradeData.length).toBeGreaterThanOrEqual(100);
30
+ expect(traderAddress).toEqual(Deployments.ParaswapAggregatorTrader[Network.ARBITRUM_ONE].address);
31
+ expect(expectedAmountOut.gt(minOutputAmount)).toBe(true);
32
+ });
33
+ });
34
+ });
@@ -0,0 +1,4 @@
1
+ module.exports = () => {
2
+ // eslint-disable-next-line global-require
3
+ require('dotenv-flow').config();
4
+ };