@gearbox-protocol/sdk 0.0.55 → 0.0.56

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 (42) hide show
  1. package/lib/core/pathfinder.d.ts +35 -5
  2. package/lib/core/pathfinder.js +547 -67
  3. package/lib/core/token.d.ts +5 -0
  4. package/lib/core/token.js +8 -1
  5. package/lib/core/tradeTypes.d.ts +2 -0
  6. package/lib/pathfinder/connectorPathAsset.d.ts +7 -0
  7. package/lib/pathfinder/connectorPathAsset.js +144 -0
  8. package/lib/pathfinder/convexLPTokenPathAsset.d.ts +5 -0
  9. package/lib/pathfinder/convexLPTokenPathAsset.js +77 -0
  10. package/lib/pathfinder/curveLPPathAsset.d.ts +5 -0
  11. package/lib/pathfinder/curveLPPathAsset.js +72 -0
  12. package/lib/pathfinder/metaCurveLPPathAsset.d.ts +5 -0
  13. package/lib/pathfinder/metaCurveLPPathAsset.js +72 -0
  14. package/lib/pathfinder/normalTokenPathAsset.d.ts +5 -0
  15. package/lib/pathfinder/normalTokenPathAsset.js +136 -0
  16. package/lib/pathfinder/path.d.ts +41 -0
  17. package/lib/pathfinder/path.js +302 -0
  18. package/lib/pathfinder/pathfinder.d.ts +28 -0
  19. package/lib/pathfinder/pathfinder.js +519 -0
  20. package/lib/pathfinder/yearnVaultOfCurveLPPathAsset.d.ts +5 -0
  21. package/lib/pathfinder/yearnVaultOfCurveLPPathAsset.js +87 -0
  22. package/lib/pathfinder/yearnVaultOfMetaCurveLPPathAsset.d.ts +5 -0
  23. package/lib/pathfinder/yearnVaultOfMetaCurveLPPathAsset.js +87 -0
  24. package/lib/pathfinder/yearnVaultPathAsset.d.ts +5 -0
  25. package/lib/pathfinder/yearnVaultPathAsset.js +87 -0
  26. package/lib/utils/types.d.ts +3 -0
  27. package/lib/utils/types.js +2 -0
  28. package/package.json +1 -1
  29. package/src/core/contracts.ts +11 -1
  30. package/src/core/token.ts +12 -0
  31. package/src/core/tradeTypes.ts +2 -0
  32. package/src/pathfinder/connectorPathAsset.ts +69 -0
  33. package/src/pathfinder/convexLPTokenPathAsset.ts +33 -0
  34. package/src/pathfinder/curveLPPathAsset.ts +10 -0
  35. package/src/pathfinder/metaCurveLPPathAsset.ts +12 -0
  36. package/src/pathfinder/normalTokenPathAsset.ts +68 -0
  37. package/src/pathfinder/path.ts +259 -0
  38. package/src/pathfinder/yearnVaultOfCurveLPPathAsset.ts +22 -0
  39. package/src/pathfinder/yearnVaultOfMetaCurveLPPathAsset.ts +22 -0
  40. package/src/pathfinder/yearnVaultPathAsset.ts +27 -0
  41. package/src/utils/types.ts +3 -0
  42. package/src/core/pathfinder.ts +0 -115
@@ -36,39 +36,99 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
36
36
  }
37
37
  };
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
- exports.ConnectorPathAsset = exports.Path = void 0;
39
+ exports.YearnVaultOfMetaCurveLPPathAsset = exports.YearnVaultOfCurveLPPathAsset = exports.NormalTokenPathAsset = exports.MetaCurveLPPathAsset = exports.CurveLPPathAsset = exports.ConvexLPTokenPathAsset = exports.YearnVaultPathAsset = exports.ConnectorPathAsset = exports.Path = void 0;
40
+ var ethers_1 = require("ethers");
41
+ var token_1 = require("./token");
42
+ var constants_1 = require("./constants");
43
+ var types_1 = require("../types");
44
+ var contracts_1 = require("./contracts");
45
+ var tradeTypes_1 = require("./tradeTypes");
40
46
  // const path = new Path({gasUsed:0, balances: creditAccount.balances, })
41
47
  // const closurePath = await path.getBestPath();
42
48
  // closurePath.calls -> multicalls (!)
43
49
  // closurePath.balances[path.pool] = (X)
44
50
  // closurePath.balances[!path.pool] = 0 / 1;
45
- //
46
- // [0,0,0, 100]
47
- // [ 12, 12, 12, 0]
48
51
  var Path = /** @class */ (function () {
49
52
  function Path(opts) {
50
53
  this.calls = [];
51
- this._gasUsed = opts.gasUsed;
52
54
  this.balances = opts.balances;
53
55
  this.pool = opts.pool;
54
56
  this.creditManager = opts.creditManager;
57
+ this.creditAccount = opts.creditAccount;
58
+ this.networkType = opts.networkType;
59
+ this.provider = opts.provider;
60
+ this.totalGasLimit = opts.totalGasLimit;
55
61
  }
56
62
  Path.prototype.getBestPath = function () {
57
63
  return __awaiter(this, void 0, void 0, function () {
58
- return __generator(this, function (_a) {
59
- // const nextToken = Object.entries(this.balances).filter(([token, balance]) => balance.gt(1) ) //.sort((a,b) => {})
60
- // if (nextToken.length ===1 ) {
61
- // }
62
- // Get balances and keep non-zero only
63
- // Find token with highest priority
64
- // Get token type of this token
65
- // switch (type) {
66
- // case TokenType.YearnValut:
67
- // const assetPathClass = new YearnPathAsset();
68
- // return assetPathClass.getBestPath(this);
69
- // ...
70
- //}
71
- throw new Error("Not implmented");
64
+ var existingTokens, nextToken, _a, connectorPathAsset, yearnVaultPathAsset, convexLPTokenPathAsset, curveLPPathAsset, metaCurveLPPathAsset, normalTokenPathAsset, yearnVaultOfCurveLPPathAsset, yearnVaultOfMetaCurveLPPathAsset;
65
+ return __generator(this, function (_b) {
66
+ switch (_b.label) {
67
+ case 0:
68
+ existingTokens = Object.entries(this.balances).filter(function (_a) {
69
+ var balance = _a[1];
70
+ return balance.gt(1);
71
+ }).sort(function (_a, _b) {
72
+ var tokenA = _a[0];
73
+ var tokenB = _b[0];
74
+ if (token_1.priority[token_1.supportedTokens[tokenA].type] > token_1.priority[token_1.supportedTokens[tokenB].type]) {
75
+ return -1;
76
+ }
77
+ else if (token_1.priority[token_1.supportedTokens[tokenA].type] < token_1.priority[token_1.supportedTokens[tokenB].type]) {
78
+ return 1;
79
+ }
80
+ return 0;
81
+ });
82
+ if (existingTokens.length == 1) {
83
+ return [2 /*return*/, Promise.resolve(this)];
84
+ }
85
+ nextToken = existingTokens.at(0)[0];
86
+ _a = token_1.supportedTokens[nextToken].type;
87
+ switch (_a) {
88
+ case token_1.TokenType.CONNECTOR: return [3 /*break*/, 1];
89
+ case token_1.TokenType.YEARN_VAULT: return [3 /*break*/, 3];
90
+ case token_1.TokenType.CONVEX_LP_TOKEN: return [3 /*break*/, 5];
91
+ case token_1.TokenType.CURVE_LP: return [3 /*break*/, 7];
92
+ case token_1.TokenType.META_CURVE_LP: return [3 /*break*/, 9];
93
+ case token_1.TokenType.NORMAL_TOKEN: return [3 /*break*/, 11];
94
+ case token_1.TokenType.YEARN_VAULT_OF_CURVE_LP: return [3 /*break*/, 13];
95
+ case token_1.TokenType.YEARN_VAULT_OF_META_CURVE_LP: return [3 /*break*/, 15];
96
+ }
97
+ return [3 /*break*/, 17];
98
+ case 1:
99
+ connectorPathAsset = new ConnectorPathAsset();
100
+ return [4 /*yield*/, connectorPathAsset.getBestPath(nextToken, this)];
101
+ case 2: return [2 /*return*/, _b.sent()];
102
+ case 3:
103
+ yearnVaultPathAsset = new YearnVaultPathAsset();
104
+ return [4 /*yield*/, yearnVaultPathAsset.getBestPath(nextToken, this)];
105
+ case 4: return [2 /*return*/, _b.sent()];
106
+ case 5:
107
+ convexLPTokenPathAsset = new ConvexLPTokenPathAsset();
108
+ return [4 /*yield*/, convexLPTokenPathAsset.getBestPath(nextToken, this)];
109
+ case 6: return [2 /*return*/, _b.sent()];
110
+ case 7:
111
+ curveLPPathAsset = new CurveLPPathAsset();
112
+ return [4 /*yield*/, curveLPPathAsset.getBestPath(nextToken, this)];
113
+ case 8: return [2 /*return*/, _b.sent()];
114
+ case 9:
115
+ metaCurveLPPathAsset = new MetaCurveLPPathAsset();
116
+ return [4 /*yield*/, metaCurveLPPathAsset.getBestPath(nextToken, this)];
117
+ case 10: return [2 /*return*/, _b.sent()];
118
+ case 11:
119
+ normalTokenPathAsset = new NormalTokenPathAsset();
120
+ return [4 /*yield*/, normalTokenPathAsset.getBestPath(nextToken, this)];
121
+ case 12: return [2 /*return*/, _b.sent()];
122
+ case 13:
123
+ yearnVaultOfCurveLPPathAsset = new YearnVaultOfCurveLPPathAsset();
124
+ return [4 /*yield*/, yearnVaultOfCurveLPPathAsset.getBestPath(nextToken, this)];
125
+ case 14: return [2 /*return*/, _b.sent()];
126
+ case 15:
127
+ yearnVaultOfMetaCurveLPPathAsset = new YearnVaultOfMetaCurveLPPathAsset();
128
+ return [4 /*yield*/, yearnVaultOfMetaCurveLPPathAsset.getBestPath(nextToken, this)];
129
+ case 16: return [2 /*return*/, _b.sent()];
130
+ case 17: throw new Error("Token type not supported yet");
131
+ }
72
132
  });
73
133
  });
74
134
  };
@@ -78,55 +138,475 @@ exports.Path = Path;
78
138
  var ConnectorPathAsset = /** @class */ (function () {
79
139
  function ConnectorPathAsset() {
80
140
  }
81
- ConnectorPathAsset.prototype.getBestPath = function (p) {
82
- console.log(p);
83
- // How to unwrap
84
- // set balance of current token to 0
85
- // add balnace what we get
86
- // add call
87
- // create new Paths
88
- // 3CRV:
89
- // coinst balance3crv = p.balance["3rcv"];
90
- // const p.balance["3rcv"] = 0;
91
- // DAI
92
- // const daiPath = {...p};
93
- // const daiPath["Dai"] = await curve.getAmount(t balance3crv);
94
- // const pathDai = new Path(daiPath)
95
- // ...
96
- // const bestPath = pathDai.balance[0] > pathUSDC.balance(0) ? pathDai : pathUsdc;
97
- // --- DAI
98
- // --|
99
- // --- USDC
100
- //
101
- // NORMAL TOKEN
102
- // ============
103
- //
104
- // - NORMAL -> POOL (1 pair) => #1
105
- // for(let conn of connectors) {
106
- // const result = getAmount([NORMAL, conn, pool])
107
- // }
108
- // => best Path to get max Pool tokens
109
- // const resultInConnector = getAmount([NORMAL, conn]);
110
- // calls.push("Uniswap.swap(NORMAL, conn)");
111
- // CRV => CRV -> USDC -> DAI
112
- // LINK => LINK -> USDC -> DAI
113
- //
114
- // [UniV2.swap("CRV", "USDC"),
115
- // UniV3.swap("LINK", "USDC"),
116
- // Curve.exchange("USDC", "DAI")]
117
- //
118
- // Below is not optimal:
119
- // [ UniV2.swap("CRV -> USDC -> DAI"),
120
- // UniV2.swap("LINK -> USDC -> DAI")]
121
- //
122
- //
123
- // [ UniV2.swap("CRV -> USDC")
124
- // UniV2.swap("USDC -> DAI"),
125
- // UniV2.swap("LINK -> USDC"),
126
- // UniV2.swap("USDC -> DAI") ]
127
- // return maxValuable(new Paths.getBestPath())
128
- throw new Error("Method not implemented.");
141
+ ConnectorPathAsset.prototype.getBestPath = function (currentToken, p) {
142
+ return __awaiter(this, void 0, void 0, function () {
143
+ var currentBalance, currentTokenAddress, currentTokenData, nextToken, nextTokenAddress, deadline, callData, gasLimit, maxAmountOut, _i, _a, swapAction, actionType, actionContract, actionContractAddress, adapterAddress, call, amountOut, gasLimitTmp, _b, uniswapV2Adapter, path, amountsOut, uniswapV3Adapter, iQuoter, exactInputSingleOrder, curve3CrvPool, currentIndex, outputIndex;
144
+ return __generator(this, function (_c) {
145
+ switch (_c.label) {
146
+ case 0:
147
+ currentBalance = p.balances[currentToken].sub(1);
148
+ currentTokenAddress = token_1.tokenDataByNetwork[p.networkType][currentToken];
149
+ currentTokenData = token_1.supportedTokens[currentToken];
150
+ nextToken = p.pool;
151
+ nextTokenAddress = token_1.tokenDataByNetwork[p.networkType][nextToken];
152
+ deadline = Math.floor(Date.now() / 1000) + 1200;
153
+ callData = {
154
+ targetContract: constants_1.ADDRESS_0x0,
155
+ callData: ""
156
+ };
157
+ gasLimit = ethers_1.BigNumber.from(0);
158
+ maxAmountOut = ethers_1.BigNumber.from(0);
159
+ _i = 0, _a = currentTokenData.swapActions;
160
+ _c.label = 1;
161
+ case 1:
162
+ if (!(_i < _a.length)) return [3 /*break*/, 15];
163
+ swapAction = _a[_i];
164
+ actionType = swapAction.type;
165
+ actionContract = swapAction.contract;
166
+ actionContractAddress = contracts_1.contractsByNetwork[p.networkType][actionContract];
167
+ adapterAddress = p.creditManager.adapters[actionContractAddress];
168
+ call = {
169
+ targetContract: constants_1.ADDRESS_0x0,
170
+ callData: ""
171
+ };
172
+ amountOut = ethers_1.BigNumber.from(0);
173
+ gasLimitTmp = ethers_1.BigNumber.from(0);
174
+ _b = actionType;
175
+ switch (_b) {
176
+ case tradeTypes_1.TradeType.UniswapV2Swap: return [3 /*break*/, 2];
177
+ case tradeTypes_1.TradeType.UniswapV3Swap: return [3 /*break*/, 5];
178
+ case tradeTypes_1.TradeType.CurveExchange: return [3 /*break*/, 8];
179
+ }
180
+ return [3 /*break*/, 12];
181
+ case 2:
182
+ uniswapV2Adapter = types_1.UniswapV2Adapter__factory.connect(adapterAddress, p.provider);
183
+ path = [currentTokenAddress, nextTokenAddress];
184
+ return [4 /*yield*/, uniswapV2Adapter.getAmountsOut(currentBalance, path)];
185
+ case 3:
186
+ amountsOut = _c.sent();
187
+ amountOut = amountsOut[amountsOut.length - 1];
188
+ call = {
189
+ targetContract: adapterAddress,
190
+ callData: types_1.UniswapV2Adapter__factory.createInterface().encodeFunctionData("swapExactTokensForTokens", [currentBalance, amountOut, path, p.creditAccount.addr, deadline])
191
+ };
192
+ return [4 /*yield*/, uniswapV2Adapter.estimateGas.swapExactTokensForTokens(currentBalance, amountOut, path, p.creditAccount.addr, deadline)];
193
+ case 4:
194
+ gasLimitTmp = _c.sent();
195
+ return [3 /*break*/, 13];
196
+ case 5:
197
+ uniswapV3Adapter = types_1.UniswapV3Adapter__factory.connect(adapterAddress, p.provider);
198
+ iQuoter = types_1.IQuoter__factory.connect(contracts_1.UNISWAP_V3_QUOTER, p.provider);
199
+ return [4 /*yield*/, iQuoter.callStatic.quoteExactInputSingle(currentTokenAddress, nextTokenAddress, 3000, currentBalance, 0)];
200
+ case 6:
201
+ amountOut = _c.sent();
202
+ exactInputSingleOrder = {
203
+ "tokenIn": currentTokenAddress,
204
+ "tokenOut": nextTokenAddress,
205
+ "fee": 3000,
206
+ "recipient": p.creditAccount.addr,
207
+ "amountIn": currentBalance,
208
+ "amountOutMinimum": amountOut,
209
+ "deadline": deadline,
210
+ "sqrtPriceLimitX96": 0
211
+ };
212
+ call = {
213
+ targetContract: adapterAddress,
214
+ callData: types_1.UniswapV3Adapter__factory.createInterface().encodeFunctionData("exactInputSingle", [exactInputSingleOrder])
215
+ };
216
+ return [4 /*yield*/, uniswapV3Adapter.estimateGas.exactInputSingle(exactInputSingleOrder)];
217
+ case 7:
218
+ gasLimitTmp = _c.sent();
219
+ return [3 /*break*/, 13];
220
+ case 8:
221
+ if (!swapAction.tokenOut.includes(p.pool)) return [3 /*break*/, 11];
222
+ curve3CrvPool = types_1.CurveV1Adapter__factory.connect(adapterAddress, p.provider);
223
+ currentIndex = token_1.Curve3CrvUnderlyingTokenIndex[currentToken];
224
+ outputIndex = token_1.Curve3CrvUnderlyingTokenIndex[p.pool];
225
+ return [4 /*yield*/, curve3CrvPool.get_dy_underlying(currentIndex, outputIndex, currentBalance)];
226
+ case 9:
227
+ amountOut = _c.sent();
228
+ call = {
229
+ targetContract: adapterAddress,
230
+ callData: types_1.CurveV1Adapter__factory.createInterface().encodeFunctionData("exchange_underlying", [currentIndex, outputIndex, currentBalance, amountOut])
231
+ };
232
+ return [4 /*yield*/, curve3CrvPool.estimateGas.exchange_underlying(currentIndex, outputIndex, currentBalance, amountOut)];
233
+ case 10:
234
+ gasLimitTmp = _c.sent();
235
+ _c.label = 11;
236
+ case 11: return [3 /*break*/, 13];
237
+ case 12: throw Error("TradeType not supported. ".concat(actionType));
238
+ case 13:
239
+ if (amountOut > maxAmountOut) {
240
+ callData = call;
241
+ maxAmountOut = amountOut;
242
+ gasLimit = gasLimitTmp;
243
+ }
244
+ _c.label = 14;
245
+ case 14:
246
+ _i++;
247
+ return [3 /*break*/, 1];
248
+ case 15:
249
+ if (callData.targetContract != constants_1.ADDRESS_0x0) {
250
+ p.balances[nextToken].add(maxAmountOut);
251
+ p.balances[currentToken] = ethers_1.BigNumber.from(1);
252
+ p.totalGasLimit.add(gasLimit);
253
+ p.calls.push(callData);
254
+ }
255
+ return [4 /*yield*/, p.getBestPath()];
256
+ case 16: return [2 /*return*/, _c.sent()];
257
+ }
258
+ });
259
+ });
260
+ };
261
+ // Only compute the maximum amount of pool token output, will not modify p.
262
+ ConnectorPathAsset.prototype.getMaxPoolAmount = function (currentToken, currentBalance, p) {
263
+ return __awaiter(this, void 0, void 0, function () {
264
+ var currentTokenAddress, currentTokenData, nextToken, nextTokenAddress, deadline, gasLimit, maxAmountOut, _i, _a, swapAction, actionType, actionContract, actionContractAddress, adapterAddress, amountOut, gasLimitTmp, _b, uniswapV2Adapter, path, amountsOut, uniswapV3Adapter, iQuoter, exactInputSingleOrder, curve3CrvPool, currentIndex, outputIndex;
265
+ return __generator(this, function (_c) {
266
+ switch (_c.label) {
267
+ case 0:
268
+ currentTokenAddress = token_1.tokenDataByNetwork[p.networkType][currentToken];
269
+ currentTokenData = token_1.supportedTokens[currentToken];
270
+ nextToken = p.pool;
271
+ nextTokenAddress = token_1.tokenDataByNetwork[p.networkType][nextToken];
272
+ deadline = Math.floor(Date.now() / 1000) + 1200;
273
+ gasLimit = ethers_1.BigNumber.from(0);
274
+ maxAmountOut = ethers_1.BigNumber.from(0);
275
+ _i = 0, _a = currentTokenData.swapActions;
276
+ _c.label = 1;
277
+ case 1:
278
+ if (!(_i < _a.length)) return [3 /*break*/, 15];
279
+ swapAction = _a[_i];
280
+ actionType = swapAction.type;
281
+ actionContract = swapAction.contract;
282
+ actionContractAddress = contracts_1.contractsByNetwork[p.networkType][actionContract];
283
+ adapterAddress = p.creditManager.adapters[actionContractAddress];
284
+ amountOut = ethers_1.BigNumber.from(0);
285
+ gasLimitTmp = ethers_1.BigNumber.from(0);
286
+ _b = actionType;
287
+ switch (_b) {
288
+ case tradeTypes_1.TradeType.UniswapV2Swap: return [3 /*break*/, 2];
289
+ case tradeTypes_1.TradeType.UniswapV3Swap: return [3 /*break*/, 5];
290
+ case tradeTypes_1.TradeType.CurveExchange: return [3 /*break*/, 8];
291
+ }
292
+ return [3 /*break*/, 12];
293
+ case 2:
294
+ uniswapV2Adapter = types_1.UniswapV2Adapter__factory.connect(adapterAddress, p.provider);
295
+ path = [currentTokenAddress, nextTokenAddress];
296
+ return [4 /*yield*/, uniswapV2Adapter.getAmountsOut(currentBalance, path)];
297
+ case 3:
298
+ amountsOut = _c.sent();
299
+ amountOut = amountsOut[amountsOut.length - 1];
300
+ return [4 /*yield*/, uniswapV2Adapter.estimateGas.swapExactTokensForTokens(currentBalance, amountOut, path, p.creditAccount.addr, deadline)];
301
+ case 4:
302
+ gasLimitTmp = _c.sent();
303
+ return [3 /*break*/, 13];
304
+ case 5:
305
+ uniswapV3Adapter = types_1.UniswapV3Adapter__factory.connect(adapterAddress, p.provider);
306
+ iQuoter = types_1.IQuoter__factory.connect(contracts_1.UNISWAP_V3_QUOTER, p.provider);
307
+ return [4 /*yield*/, iQuoter.callStatic.quoteExactInputSingle(currentTokenAddress, nextTokenAddress, 3000, currentBalance, 0)];
308
+ case 6:
309
+ amountOut = _c.sent();
310
+ exactInputSingleOrder = {
311
+ "tokenIn": currentTokenAddress,
312
+ "tokenOut": nextTokenAddress,
313
+ "fee": 3000,
314
+ "recipient": p.creditAccount.addr,
315
+ "amountIn": currentBalance,
316
+ "amountOutMinimum": amountOut,
317
+ "deadline": deadline,
318
+ "sqrtPriceLimitX96": 0
319
+ };
320
+ return [4 /*yield*/, uniswapV3Adapter.estimateGas.exactInputSingle(exactInputSingleOrder)];
321
+ case 7:
322
+ gasLimitTmp = _c.sent();
323
+ return [3 /*break*/, 13];
324
+ case 8:
325
+ if (!swapAction.tokenOut.includes(nextToken)) return [3 /*break*/, 11];
326
+ curve3CrvPool = types_1.CurveV1Adapter__factory.connect(adapterAddress, p.provider);
327
+ currentIndex = token_1.Curve3CrvUnderlyingTokenIndex[currentToken];
328
+ outputIndex = token_1.Curve3CrvUnderlyingTokenIndex[nextToken];
329
+ return [4 /*yield*/, curve3CrvPool.get_dy_underlying(currentIndex, outputIndex, currentBalance)];
330
+ case 9:
331
+ amountOut = _c.sent();
332
+ return [4 /*yield*/, curve3CrvPool.estimateGas.exchange_underlying(currentIndex, outputIndex, currentBalance, amountOut)];
333
+ case 10:
334
+ gasLimitTmp = _c.sent();
335
+ _c.label = 11;
336
+ case 11: return [3 /*break*/, 13];
337
+ case 12: throw Error("TradeType not supported. ".concat(actionType));
338
+ case 13:
339
+ if (amountOut > maxAmountOut) {
340
+ maxAmountOut = amountOut;
341
+ gasLimit = gasLimitTmp;
342
+ }
343
+ _c.label = 14;
344
+ case 14:
345
+ _i++;
346
+ return [3 /*break*/, 1];
347
+ case 15: return [2 /*return*/, [maxAmountOut, gasLimit]];
348
+ }
349
+ });
350
+ });
129
351
  };
130
352
  return ConnectorPathAsset;
131
353
  }());
132
354
  exports.ConnectorPathAsset = ConnectorPathAsset;
355
+ var YearnVaultPathAsset = /** @class */ (function () {
356
+ function YearnVaultPathAsset() {
357
+ }
358
+ YearnVaultPathAsset.prototype.getBestPath = function (currentToken, p) {
359
+ return __awaiter(this, void 0, void 0, function () {
360
+ var currentBalance, currentTokenData, allowedContract, outputToken, adapterAddress, call, IYVault, price, gasLimit;
361
+ return __generator(this, function (_a) {
362
+ switch (_a.label) {
363
+ case 0:
364
+ currentBalance = p.balances[currentToken].sub(1);
365
+ currentTokenData = token_1.supportedTokens[currentToken];
366
+ allowedContract = contracts_1.contractsByNetwork[p.networkType][currentTokenData.lpActions[0].contract];
367
+ outputToken = currentTokenData.lpActions[0].tokenOut;
368
+ adapterAddress = p.creditManager.adapters[allowedContract];
369
+ call = {
370
+ targetContract: adapterAddress,
371
+ callData: types_1.YearnAdapter__factory.createInterface().encodeFunctionData('withdraw', [currentBalance, p.creditAccount.addr])
372
+ };
373
+ IYVault = types_1.YearnAdapter__factory.connect(allowedContract, p.provider);
374
+ return [4 /*yield*/, IYVault.pricePerShare()];
375
+ case 1:
376
+ price = _a.sent();
377
+ return [4 /*yield*/, IYVault.estimateGas["withdraw(uint256,address)"](currentBalance, p.creditAccount.addr)];
378
+ case 2:
379
+ gasLimit = _a.sent();
380
+ p.balances[outputToken].add(price.mul(currentBalance));
381
+ p.balances[currentToken] = ethers_1.BigNumber.from(1);
382
+ p.totalGasLimit.add(gasLimit);
383
+ p.calls.push(call);
384
+ return [4 /*yield*/, p.getBestPath()];
385
+ case 3: return [2 /*return*/, _a.sent()];
386
+ }
387
+ });
388
+ });
389
+ };
390
+ return YearnVaultPathAsset;
391
+ }());
392
+ exports.YearnVaultPathAsset = YearnVaultPathAsset;
393
+ var ConvexLPTokenPathAsset = /** @class */ (function () {
394
+ function ConvexLPTokenPathAsset() {
395
+ }
396
+ ConvexLPTokenPathAsset.prototype.getBestPath = function (currentToken, p) {
397
+ return __awaiter(this, void 0, void 0, function () {
398
+ var currentBalance, currentTokenData, _i, _a, lpAction, actionContractAddress, adapterAddress, outputToken;
399
+ return __generator(this, function (_b) {
400
+ switch (_b.label) {
401
+ case 0: throw Error("Implementation not finished!");
402
+ case 1: return [2 /*return*/, _b.sent()];
403
+ }
404
+ });
405
+ });
406
+ };
407
+ return ConvexLPTokenPathAsset;
408
+ }());
409
+ exports.ConvexLPTokenPathAsset = ConvexLPTokenPathAsset;
410
+ var CurveLPPathAsset = /** @class */ (function () {
411
+ function CurveLPPathAsset() {
412
+ }
413
+ CurveLPPathAsset.prototype.getBestPath = function (currentToken, p) {
414
+ return __awaiter(this, void 0, void 0, function () {
415
+ return __generator(this, function (_a) {
416
+ console.log(currentToken);
417
+ console.log(p);
418
+ throw Error("Not implemented yet.");
419
+ });
420
+ });
421
+ };
422
+ return CurveLPPathAsset;
423
+ }());
424
+ exports.CurveLPPathAsset = CurveLPPathAsset;
425
+ var MetaCurveLPPathAsset = /** @class */ (function () {
426
+ function MetaCurveLPPathAsset() {
427
+ }
428
+ MetaCurveLPPathAsset.prototype.getBestPath = function (currentToken, p) {
429
+ return __awaiter(this, void 0, void 0, function () {
430
+ return __generator(this, function (_a) {
431
+ console.log(currentToken);
432
+ console.log(p);
433
+ throw Error("Not implemented yet.");
434
+ });
435
+ });
436
+ };
437
+ return MetaCurveLPPathAsset;
438
+ }());
439
+ exports.MetaCurveLPPathAsset = MetaCurveLPPathAsset;
440
+ var NormalTokenPathAsset = /** @class */ (function () {
441
+ function NormalTokenPathAsset() {
442
+ }
443
+ NormalTokenPathAsset.prototype.getBestPath = function (currentToken, p) {
444
+ return __awaiter(this, void 0, void 0, function () {
445
+ var currentBalance, currentTokenAddress, currentTokenData, deadline, callData, nextToken, gasLimit, maxAmountOut, _i, ConnectorTokens_1, connToken, connTokenAddress, _a, _b, swapAction, actionType, actionContract, actionContractAddress, adapterAddress, call, amountOut, gasLimitTmp, _c, uniswapV2Adapter, path, amountsOut, uniswapV3Adapter, iQuoter, exactInputSingleOrder, curve3CrvPool, currentIndex, outputIndex, connectorPathAsset, _d, maxPoolAmount, gasLimitConn;
446
+ return __generator(this, function (_e) {
447
+ switch (_e.label) {
448
+ case 0:
449
+ currentBalance = p.balances[currentToken].sub(1);
450
+ currentTokenAddress = token_1.tokenDataByNetwork[p.networkType][currentToken];
451
+ currentTokenData = token_1.supportedTokens[currentToken];
452
+ deadline = Math.floor(Date.now() / 1000) + 1200;
453
+ callData = {
454
+ targetContract: constants_1.ADDRESS_0x0,
455
+ callData: ""
456
+ };
457
+ nextToken = p.pool;
458
+ gasLimit = ethers_1.BigNumber.from(0);
459
+ maxAmountOut = ethers_1.BigNumber.from(0);
460
+ _i = 0, ConnectorTokens_1 = token_1.ConnectorTokens;
461
+ _e.label = 1;
462
+ case 1:
463
+ if (!(_i < ConnectorTokens_1.length)) return [3 /*break*/, 19];
464
+ connToken = ConnectorTokens_1[_i];
465
+ connTokenAddress = token_1.tokenDataByNetwork[p.networkType][connToken];
466
+ _a = 0, _b = currentTokenData.swapActions;
467
+ _e.label = 2;
468
+ case 2:
469
+ if (!(_a < _b.length)) return [3 /*break*/, 18];
470
+ swapAction = _b[_a];
471
+ actionType = swapAction.type;
472
+ actionContract = swapAction.contract;
473
+ actionContractAddress = contracts_1.contractsByNetwork[p.networkType][actionContract];
474
+ adapterAddress = p.creditManager.adapters[actionContractAddress];
475
+ call = {
476
+ targetContract: constants_1.ADDRESS_0x0,
477
+ callData: ""
478
+ };
479
+ amountOut = ethers_1.BigNumber.from(0);
480
+ gasLimitTmp = ethers_1.BigNumber.from(0);
481
+ _c = actionType;
482
+ switch (_c) {
483
+ case tradeTypes_1.TradeType.UniswapV2Swap: return [3 /*break*/, 3];
484
+ case tradeTypes_1.TradeType.UniswapV3Swap: return [3 /*break*/, 6];
485
+ case tradeTypes_1.TradeType.CurveExchange: return [3 /*break*/, 9];
486
+ }
487
+ return [3 /*break*/, 13];
488
+ case 3:
489
+ uniswapV2Adapter = types_1.UniswapV2Adapter__factory.connect(adapterAddress, p.provider);
490
+ path = [currentTokenAddress, connTokenAddress];
491
+ return [4 /*yield*/, uniswapV2Adapter.getAmountsOut(currentBalance, path)];
492
+ case 4:
493
+ amountsOut = _e.sent();
494
+ amountOut = amountsOut[amountsOut.length - 1];
495
+ call = {
496
+ targetContract: adapterAddress,
497
+ callData: types_1.UniswapV2Adapter__factory.createInterface().encodeFunctionData("swapExactTokensForTokens", [currentBalance, amountOut, path, p.creditAccount.addr, deadline])
498
+ };
499
+ return [4 /*yield*/, uniswapV2Adapter.estimateGas.swapExactTokensForTokens(currentBalance, amountOut, path, p.creditAccount.addr, deadline)];
500
+ case 5:
501
+ gasLimitTmp = _e.sent();
502
+ return [3 /*break*/, 14];
503
+ case 6:
504
+ uniswapV3Adapter = types_1.UniswapV3Adapter__factory.connect(adapterAddress, p.provider);
505
+ iQuoter = types_1.IQuoter__factory.connect(contracts_1.UNISWAP_V3_QUOTER, p.provider);
506
+ return [4 /*yield*/, iQuoter.callStatic.quoteExactInputSingle(currentTokenAddress, connTokenAddress, 3000, currentBalance, 0)];
507
+ case 7:
508
+ amountOut = _e.sent();
509
+ exactInputSingleOrder = {
510
+ "tokenIn": currentTokenAddress,
511
+ "tokenOut": connTokenAddress,
512
+ "fee": 3000,
513
+ "recipient": p.creditAccount.addr,
514
+ "amountIn": currentBalance,
515
+ "amountOutMinimum": amountOut,
516
+ "deadline": deadline,
517
+ "sqrtPriceLimitX96": 0
518
+ };
519
+ call = {
520
+ targetContract: adapterAddress,
521
+ callData: types_1.UniswapV3Adapter__factory.createInterface().encodeFunctionData("exactInputSingle", [exactInputSingleOrder])
522
+ };
523
+ return [4 /*yield*/, uniswapV3Adapter.estimateGas.exactInputSingle(exactInputSingleOrder)];
524
+ case 8:
525
+ gasLimitTmp = _e.sent();
526
+ return [3 /*break*/, 14];
527
+ case 9:
528
+ if (!swapAction.tokenOut.includes(connToken)) return [3 /*break*/, 12];
529
+ curve3CrvPool = types_1.CurveV1Adapter__factory.connect(adapterAddress, p.provider);
530
+ currentIndex = token_1.Curve3CrvUnderlyingTokenIndex[currentToken];
531
+ outputIndex = token_1.Curve3CrvUnderlyingTokenIndex[connToken];
532
+ return [4 /*yield*/, curve3CrvPool.get_dy_underlying(currentIndex, outputIndex, currentBalance)];
533
+ case 10:
534
+ amountOut = _e.sent();
535
+ call = {
536
+ targetContract: adapterAddress,
537
+ callData: types_1.CurveV1Adapter__factory.createInterface().encodeFunctionData("exchange_underlying", [currentIndex, outputIndex, currentBalance, amountOut])
538
+ };
539
+ return [4 /*yield*/, curve3CrvPool.estimateGas.exchange_underlying(currentIndex, outputIndex, currentBalance, amountOut)];
540
+ case 11:
541
+ gasLimitTmp = _e.sent();
542
+ _e.label = 12;
543
+ case 12: return [3 /*break*/, 14];
544
+ case 13: throw Error("TradeType not supported. ".concat(actionType));
545
+ case 14:
546
+ if (!(connToken != p.pool)) return [3 /*break*/, 16];
547
+ connectorPathAsset = new ConnectorPathAsset();
548
+ return [4 /*yield*/, connectorPathAsset.getMaxPoolAmount(connToken, amountOut, p)];
549
+ case 15:
550
+ _d = _e.sent(), maxPoolAmount = _d[0], gasLimitConn = _d[1];
551
+ amountOut = maxPoolAmount;
552
+ gasLimitTmp.add(gasLimitConn);
553
+ _e.label = 16;
554
+ case 16:
555
+ if (amountOut > maxAmountOut) {
556
+ callData = call;
557
+ maxAmountOut = amountOut;
558
+ gasLimit = gasLimitTmp;
559
+ }
560
+ _e.label = 17;
561
+ case 17:
562
+ _a++;
563
+ return [3 /*break*/, 2];
564
+ case 18:
565
+ _i++;
566
+ return [3 /*break*/, 1];
567
+ case 19:
568
+ if (callData.targetContract != constants_1.ADDRESS_0x0) {
569
+ p.balances[nextToken].add(maxAmountOut);
570
+ p.balances[currentToken] = ethers_1.BigNumber.from(1);
571
+ p.totalGasLimit.add(gasLimit);
572
+ p.calls.push(callData);
573
+ }
574
+ return [4 /*yield*/, p.getBestPath()];
575
+ case 20: return [2 /*return*/, _e.sent()];
576
+ }
577
+ });
578
+ });
579
+ };
580
+ return NormalTokenPathAsset;
581
+ }());
582
+ exports.NormalTokenPathAsset = NormalTokenPathAsset;
583
+ var YearnVaultOfCurveLPPathAsset = /** @class */ (function () {
584
+ function YearnVaultOfCurveLPPathAsset() {
585
+ }
586
+ YearnVaultOfCurveLPPathAsset.prototype.getBestPath = function (currentToken, p) {
587
+ return __awaiter(this, void 0, void 0, function () {
588
+ return __generator(this, function (_a) {
589
+ console.log(currentToken);
590
+ console.log(p);
591
+ throw Error("Not implemented yet.");
592
+ });
593
+ });
594
+ };
595
+ return YearnVaultOfCurveLPPathAsset;
596
+ }());
597
+ exports.YearnVaultOfCurveLPPathAsset = YearnVaultOfCurveLPPathAsset;
598
+ var YearnVaultOfMetaCurveLPPathAsset = /** @class */ (function () {
599
+ function YearnVaultOfMetaCurveLPPathAsset() {
600
+ }
601
+ YearnVaultOfMetaCurveLPPathAsset.prototype.getBestPath = function (currentToken, p) {
602
+ return __awaiter(this, void 0, void 0, function () {
603
+ return __generator(this, function (_a) {
604
+ console.log(currentToken);
605
+ console.log(p);
606
+ throw Error("Not implemented yet.");
607
+ });
608
+ });
609
+ };
610
+ return YearnVaultOfMetaCurveLPPathAsset;
611
+ }());
612
+ exports.YearnVaultOfMetaCurveLPPathAsset = YearnVaultOfMetaCurveLPPathAsset;