@gearbox-protocol/sdk 0.0.53 → 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 (63) hide show
  1. package/lib/core/contracts.d.ts +29 -16
  2. package/lib/core/contracts.js +45 -4
  3. package/lib/core/contractsRegister.js +24 -6
  4. package/lib/core/creditAccount.js +26 -20
  5. package/lib/core/events.d.ts +10 -10
  6. package/lib/core/events.js +22 -22
  7. package/lib/core/pathfinder.d.ts +35 -5
  8. package/lib/core/pathfinder.js +547 -67
  9. package/lib/core/priceFeeds.js +61 -0
  10. package/lib/core/token.d.ts +47 -15
  11. package/lib/core/token.js +204 -36
  12. package/lib/core/tradeTypes.d.ts +2 -0
  13. package/lib/core/transactions.d.ts +2 -2
  14. package/lib/core/transactions.js +10 -10
  15. package/lib/index.d.ts +2 -0
  16. package/lib/index.js +6 -1
  17. package/lib/pathfinder/connectorPathAsset.d.ts +7 -0
  18. package/lib/pathfinder/connectorPathAsset.js +144 -0
  19. package/lib/pathfinder/convexLPTokenPathAsset.d.ts +5 -0
  20. package/lib/pathfinder/convexLPTokenPathAsset.js +77 -0
  21. package/lib/pathfinder/curveLPPathAsset.d.ts +5 -0
  22. package/lib/pathfinder/curveLPPathAsset.js +72 -0
  23. package/lib/pathfinder/metaCurveLPPathAsset.d.ts +5 -0
  24. package/lib/pathfinder/metaCurveLPPathAsset.js +72 -0
  25. package/lib/pathfinder/normalTokenPathAsset.d.ts +5 -0
  26. package/lib/pathfinder/normalTokenPathAsset.js +136 -0
  27. package/lib/pathfinder/path.d.ts +41 -0
  28. package/lib/pathfinder/path.js +302 -0
  29. package/lib/pathfinder/pathfinder.d.ts +28 -0
  30. package/lib/pathfinder/pathfinder.js +519 -0
  31. package/lib/pathfinder/yearnVaultOfCurveLPPathAsset.d.ts +5 -0
  32. package/lib/pathfinder/yearnVaultOfCurveLPPathAsset.js +87 -0
  33. package/lib/pathfinder/yearnVaultOfMetaCurveLPPathAsset.d.ts +5 -0
  34. package/lib/pathfinder/yearnVaultOfMetaCurveLPPathAsset.js +87 -0
  35. package/lib/pathfinder/yearnVaultPathAsset.d.ts +5 -0
  36. package/lib/pathfinder/yearnVaultPathAsset.js +87 -0
  37. package/lib/utils/mappers.d.ts +6 -0
  38. package/lib/utils/mappers.js +33 -0
  39. package/lib/utils/types.d.ts +3 -0
  40. package/lib/utils/types.js +2 -0
  41. package/package.json +1 -1
  42. package/src/core/adapters.ts +13 -15
  43. package/src/core/contracts.ts +111 -44
  44. package/src/core/contractsRegister.ts +16 -8
  45. package/src/core/creditAccount.ts +33 -19
  46. package/src/core/events.ts +30 -22
  47. package/src/core/priceFeeds.ts +66 -0
  48. package/src/core/token.ts +645 -370
  49. package/src/core/tradeTypes.ts +2 -0
  50. package/src/core/transactions.ts +10 -10
  51. package/src/index.ts +2 -0
  52. package/src/pathfinder/connectorPathAsset.ts +69 -0
  53. package/src/pathfinder/convexLPTokenPathAsset.ts +33 -0
  54. package/src/pathfinder/curveLPPathAsset.ts +10 -0
  55. package/src/pathfinder/metaCurveLPPathAsset.ts +12 -0
  56. package/src/pathfinder/normalTokenPathAsset.ts +68 -0
  57. package/src/pathfinder/path.ts +259 -0
  58. package/src/pathfinder/yearnVaultOfCurveLPPathAsset.ts +22 -0
  59. package/src/pathfinder/yearnVaultOfMetaCurveLPPathAsset.ts +22 -0
  60. package/src/pathfinder/yearnVaultPathAsset.ts +27 -0
  61. package/src/utils/mappers.ts +23 -0
  62. package/src/utils/types.ts +3 -0
  63. package/src/core/pathfinder.ts +0 -115
@@ -0,0 +1,519 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (_) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.YearnVaultOfMetaCurveLPPathAsset = exports.YearnVaultOfCurveLPPathAsset = exports.NormalTokenPathAsset = exports.MetaCurveLPPathAsset = exports.CurveLPPathAsset = exports.ConvexLPTokenPathAsset = exports.YearnVaultPathAsset = exports.ConnectorPathAsset = void 0;
40
+ var ethers_1 = require("ethers");
41
+ var token_1 = require("../core/token");
42
+ var types_1 = require("../types");
43
+ var contracts_1 = require("../core/contracts");
44
+ var tradeTypes_1 = require("../core/tradeTypes");
45
+ var ConnectorPathAsset = /** @class */ (function () {
46
+ function ConnectorPathAsset() {
47
+ }
48
+ ConnectorPathAsset.prototype.getBestPath = function (currentToken, p) {
49
+ return __awaiter(this, void 0, void 0, function () {
50
+ 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;
51
+ return __generator(this, function (_c) {
52
+ switch (_c.label) {
53
+ case 0:
54
+ currentBalance = p.balances[currentToken].sub(1);
55
+ currentTokenAddress = token_1.tokenDataByNetwork[p.networkType][currentToken];
56
+ currentTokenData = token_1.supportedTokens[currentToken];
57
+ nextToken = p.pool;
58
+ nextTokenAddress = token_1.tokenDataByNetwork[p.networkType][nextToken];
59
+ deadline = Math.floor(Date.now() / 1000) + 1200;
60
+ callData = {
61
+ targetContract: "",
62
+ callData: ""
63
+ };
64
+ gasLimit = ethers_1.BigNumber.from(0);
65
+ maxAmountOut = ethers_1.BigNumber.from(0);
66
+ _i = 0, _a = currentTokenData.swapActions;
67
+ _c.label = 1;
68
+ case 1:
69
+ if (!(_i < _a.length)) return [3 /*break*/, 15];
70
+ swapAction = _a[_i];
71
+ actionType = swapAction.type;
72
+ actionContract = swapAction.contract;
73
+ actionContractAddress = contracts_1.contractsByNetwork[p.networkType][actionContract];
74
+ adapterAddress = p.creditManager.adapters[actionContractAddress];
75
+ call = {
76
+ targetContract: "",
77
+ callData: ""
78
+ };
79
+ amountOut = ethers_1.BigNumber.from(0);
80
+ gasLimitTmp = ethers_1.BigNumber.from(0);
81
+ _b = actionType;
82
+ switch (_b) {
83
+ case tradeTypes_1.TradeType.UniswapV2Swap: return [3 /*break*/, 2];
84
+ case tradeTypes_1.TradeType.UniswapV3Swap: return [3 /*break*/, 5];
85
+ case tradeTypes_1.TradeType.CurveExchange: return [3 /*break*/, 8];
86
+ }
87
+ return [3 /*break*/, 12];
88
+ case 2:
89
+ uniswapV2Adapter = types_1.UniswapV2Adapter__factory.connect(adapterAddress, p.provider);
90
+ path = [currentTokenAddress, nextTokenAddress];
91
+ return [4 /*yield*/, uniswapV2Adapter.getAmountsOut(currentBalance, path)];
92
+ case 3:
93
+ amountsOut = _c.sent();
94
+ amountOut = amountsOut[amountsOut.length - 1];
95
+ call = {
96
+ targetContract: adapterAddress,
97
+ callData: types_1.UniswapV2Adapter__factory.createInterface().encodeFunctionData("swapExactTokensForTokens", [currentBalance, amountOut, path, p.creditAccount.addr, deadline])
98
+ };
99
+ return [4 /*yield*/, uniswapV2Adapter.estimateGas.swapExactTokensForTokens(currentBalance, amountOut, path, p.creditAccount.addr, deadline)];
100
+ case 4:
101
+ gasLimitTmp = _c.sent();
102
+ return [3 /*break*/, 13];
103
+ case 5:
104
+ uniswapV3Adapter = types_1.UniswapV3Adapter__factory.connect(adapterAddress, p.provider);
105
+ iQuoter = types_1.IQuoter__factory.connect(contracts_1.UNISWAP_V3_QUOTER, p.provider);
106
+ return [4 /*yield*/, iQuoter.callStatic.quoteExactInputSingle(currentTokenAddress, nextTokenAddress, 3000, currentBalance, 0)];
107
+ case 6:
108
+ amountOut = _c.sent();
109
+ exactInputSingleOrder = {
110
+ "tokenIn": currentTokenAddress,
111
+ "tokenOut": nextTokenAddress,
112
+ "fee": 3000,
113
+ "recipient": p.creditAccount.addr,
114
+ "amountIn": currentBalance,
115
+ "amountOutMinimum": amountOut,
116
+ "deadline": deadline,
117
+ "sqrtPriceLimitX96": 0
118
+ };
119
+ call = {
120
+ targetContract: adapterAddress,
121
+ callData: types_1.UniswapV3Adapter__factory.createInterface().encodeFunctionData("exactInputSingle", [exactInputSingleOrder])
122
+ };
123
+ return [4 /*yield*/, uniswapV3Adapter.estimateGas.exactInputSingle(exactInputSingleOrder)];
124
+ case 7:
125
+ gasLimitTmp = _c.sent();
126
+ return [3 /*break*/, 13];
127
+ case 8:
128
+ if (!swapAction.tokenOut.includes(p.pool)) return [3 /*break*/, 11];
129
+ curve3CrvPool = types_1.CurveV1Adapter__factory.connect(adapterAddress, p.provider);
130
+ currentIndex = token_1.Curve3CrvUnderlyingTokenIndex[currentToken];
131
+ outputIndex = token_1.Curve3CrvUnderlyingTokenIndex[p.pool];
132
+ return [4 /*yield*/, curve3CrvPool.get_dy_underlying(currentIndex, outputIndex, currentBalance)];
133
+ case 9:
134
+ amountOut = _c.sent();
135
+ call = {
136
+ targetContract: adapterAddress,
137
+ callData: types_1.CurveV1Adapter__factory.createInterface().encodeFunctionData("exchange_underlying", [currentIndex, outputIndex, currentBalance, amountOut])
138
+ };
139
+ return [4 /*yield*/, curve3CrvPool.estimateGas.exchange_underlying(currentIndex, outputIndex, currentBalance, amountOut)];
140
+ case 10:
141
+ gasLimitTmp = _c.sent();
142
+ _c.label = 11;
143
+ case 11: return [3 /*break*/, 13];
144
+ case 12: throw Error("TradeType not supported. ".concat(actionType));
145
+ case 13:
146
+ if (amountOut > maxAmountOut) {
147
+ callData = call;
148
+ maxAmountOut = amountOut;
149
+ gasLimit = gasLimitTmp;
150
+ }
151
+ _c.label = 14;
152
+ case 14:
153
+ _i++;
154
+ return [3 /*break*/, 1];
155
+ case 15:
156
+ if (callData.targetContract != "") {
157
+ p.balances[nextToken].add(maxAmountOut);
158
+ p.balances[currentToken] = ethers_1.BigNumber.from(1);
159
+ p.totalGasLimit.add(gasLimit);
160
+ p.calls.push(callData);
161
+ }
162
+ return [4 /*yield*/, p.getBestPath()];
163
+ case 16: return [2 /*return*/, _c.sent()];
164
+ }
165
+ });
166
+ });
167
+ };
168
+ // Only compute the maximum amount of pool token output, will not modify p.
169
+ ConnectorPathAsset.prototype.getMaxPoolAmount = function (currentToken, currentBalance, p) {
170
+ return __awaiter(this, void 0, void 0, function () {
171
+ 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;
172
+ return __generator(this, function (_c) {
173
+ switch (_c.label) {
174
+ case 0:
175
+ currentTokenAddress = token_1.tokenDataByNetwork[p.networkType][currentToken];
176
+ currentTokenData = token_1.supportedTokens[currentToken];
177
+ nextToken = p.pool;
178
+ nextTokenAddress = token_1.tokenDataByNetwork[p.networkType][nextToken];
179
+ deadline = Math.floor(Date.now() / 1000) + 1200;
180
+ gasLimit = ethers_1.BigNumber.from(0);
181
+ maxAmountOut = ethers_1.BigNumber.from(0);
182
+ _i = 0, _a = currentTokenData.swapActions;
183
+ _c.label = 1;
184
+ case 1:
185
+ if (!(_i < _a.length)) return [3 /*break*/, 15];
186
+ swapAction = _a[_i];
187
+ actionType = swapAction.type;
188
+ actionContract = swapAction.contract;
189
+ actionContractAddress = contracts_1.contractsByNetwork[p.networkType][actionContract];
190
+ adapterAddress = p.creditManager.adapters[actionContractAddress];
191
+ amountOut = ethers_1.BigNumber.from(0);
192
+ gasLimitTmp = ethers_1.BigNumber.from(0);
193
+ _b = actionType;
194
+ switch (_b) {
195
+ case tradeTypes_1.TradeType.UniswapV2Swap: return [3 /*break*/, 2];
196
+ case tradeTypes_1.TradeType.UniswapV3Swap: return [3 /*break*/, 5];
197
+ case tradeTypes_1.TradeType.CurveExchange: return [3 /*break*/, 8];
198
+ }
199
+ return [3 /*break*/, 12];
200
+ case 2:
201
+ uniswapV2Adapter = types_1.UniswapV2Adapter__factory.connect(adapterAddress, p.provider);
202
+ path = [currentTokenAddress, nextTokenAddress];
203
+ return [4 /*yield*/, uniswapV2Adapter.getAmountsOut(currentBalance, path)];
204
+ case 3:
205
+ amountsOut = _c.sent();
206
+ amountOut = amountsOut[amountsOut.length - 1];
207
+ return [4 /*yield*/, uniswapV2Adapter.estimateGas.swapExactTokensForTokens(currentBalance, amountOut, path, p.creditAccount.addr, deadline)];
208
+ case 4:
209
+ gasLimitTmp = _c.sent();
210
+ return [3 /*break*/, 13];
211
+ case 5:
212
+ uniswapV3Adapter = types_1.UniswapV3Adapter__factory.connect(adapterAddress, p.provider);
213
+ iQuoter = types_1.IQuoter__factory.connect(contracts_1.UNISWAP_V3_QUOTER, p.provider);
214
+ return [4 /*yield*/, iQuoter.callStatic.quoteExactInputSingle(currentTokenAddress, nextTokenAddress, 3000, currentBalance, 0)];
215
+ case 6:
216
+ amountOut = _c.sent();
217
+ exactInputSingleOrder = {
218
+ "tokenIn": currentTokenAddress,
219
+ "tokenOut": nextTokenAddress,
220
+ "fee": 3000,
221
+ "recipient": p.creditAccount.addr,
222
+ "amountIn": currentBalance,
223
+ "amountOutMinimum": amountOut,
224
+ "deadline": deadline,
225
+ "sqrtPriceLimitX96": 0
226
+ };
227
+ return [4 /*yield*/, uniswapV3Adapter.estimateGas.exactInputSingle(exactInputSingleOrder)];
228
+ case 7:
229
+ gasLimitTmp = _c.sent();
230
+ return [3 /*break*/, 13];
231
+ case 8:
232
+ if (!swapAction.tokenOut.includes(nextToken)) return [3 /*break*/, 11];
233
+ curve3CrvPool = types_1.CurveV1Adapter__factory.connect(adapterAddress, p.provider);
234
+ currentIndex = token_1.Curve3CrvUnderlyingTokenIndex[currentToken];
235
+ outputIndex = token_1.Curve3CrvUnderlyingTokenIndex[nextToken];
236
+ return [4 /*yield*/, curve3CrvPool.get_dy_underlying(currentIndex, outputIndex, currentBalance)];
237
+ case 9:
238
+ amountOut = _c.sent();
239
+ return [4 /*yield*/, curve3CrvPool.estimateGas.exchange_underlying(currentIndex, outputIndex, currentBalance, amountOut)];
240
+ case 10:
241
+ gasLimitTmp = _c.sent();
242
+ _c.label = 11;
243
+ case 11: return [3 /*break*/, 13];
244
+ case 12: throw Error("TradeType not supported. ".concat(actionType));
245
+ case 13:
246
+ if (amountOut > maxAmountOut) {
247
+ maxAmountOut = amountOut;
248
+ gasLimit = gasLimitTmp;
249
+ }
250
+ _c.label = 14;
251
+ case 14:
252
+ _i++;
253
+ return [3 /*break*/, 1];
254
+ case 15: return [2 /*return*/, [maxAmountOut, gasLimit]];
255
+ }
256
+ });
257
+ });
258
+ };
259
+ return ConnectorPathAsset;
260
+ }());
261
+ exports.ConnectorPathAsset = ConnectorPathAsset;
262
+ var YearnVaultPathAsset = /** @class */ (function () {
263
+ function YearnVaultPathAsset() {
264
+ }
265
+ YearnVaultPathAsset.prototype.getBestPath = function (currentToken, p) {
266
+ return __awaiter(this, void 0, void 0, function () {
267
+ var currentBalance, currentTokenData, allowedContract, outputToken, adapterAddress, call, IYVault, price, gasLimit;
268
+ return __generator(this, function (_a) {
269
+ switch (_a.label) {
270
+ case 0:
271
+ currentBalance = p.balances[currentToken].sub(1);
272
+ currentTokenData = token_1.supportedTokens[currentToken];
273
+ allowedContract = contracts_1.contractsByNetwork[p.networkType][currentTokenData.lpActions[0].contract];
274
+ outputToken = currentTokenData.lpActions[0].tokenOut;
275
+ adapterAddress = p.creditManager.adapters[allowedContract];
276
+ call = {
277
+ targetContract: adapterAddress,
278
+ callData: types_1.YearnAdapter__factory.createInterface().encodeFunctionData('withdraw', [currentBalance, p.creditAccount.addr])
279
+ };
280
+ IYVault = types_1.YearnAdapter__factory.connect(allowedContract, p.provider);
281
+ return [4 /*yield*/, IYVault.pricePerShare()];
282
+ case 1:
283
+ price = _a.sent();
284
+ return [4 /*yield*/, IYVault.estimateGas["withdraw(uint256,address)"](currentBalance, p.creditAccount.addr)];
285
+ case 2:
286
+ gasLimit = _a.sent();
287
+ p.balances[outputToken].add(price.mul(currentBalance));
288
+ p.balances[currentToken] = ethers_1.BigNumber.from(1);
289
+ p.totalGasLimit.add(gasLimit);
290
+ p.calls.push(call);
291
+ return [4 /*yield*/, p.getBestPath()];
292
+ case 3: return [2 /*return*/, _a.sent()];
293
+ }
294
+ });
295
+ });
296
+ };
297
+ return YearnVaultPathAsset;
298
+ }());
299
+ exports.YearnVaultPathAsset = YearnVaultPathAsset;
300
+ var ConvexLPTokenPathAsset = /** @class */ (function () {
301
+ function ConvexLPTokenPathAsset() {
302
+ }
303
+ ConvexLPTokenPathAsset.prototype.getBestPath = function (currentToken, p) {
304
+ return __awaiter(this, void 0, void 0, function () {
305
+ var currentBalance, currentTokenData, _i, _a, lpAction, actionContractAddress, adapterAddress, outputToken;
306
+ return __generator(this, function (_b) {
307
+ switch (_b.label) {
308
+ case 0: throw Error("Implementation not finished!");
309
+ case 1: return [2 /*return*/, _b.sent()];
310
+ }
311
+ });
312
+ });
313
+ };
314
+ return ConvexLPTokenPathAsset;
315
+ }());
316
+ exports.ConvexLPTokenPathAsset = ConvexLPTokenPathAsset;
317
+ var CurveLPPathAsset = /** @class */ (function () {
318
+ function CurveLPPathAsset() {
319
+ }
320
+ CurveLPPathAsset.prototype.getBestPath = function (currentToken, p) {
321
+ return __awaiter(this, void 0, void 0, function () {
322
+ return __generator(this, function (_a) {
323
+ console.log(currentToken);
324
+ console.log(p);
325
+ throw Error("Not implemented yet.");
326
+ });
327
+ });
328
+ };
329
+ return CurveLPPathAsset;
330
+ }());
331
+ exports.CurveLPPathAsset = CurveLPPathAsset;
332
+ var MetaCurveLPPathAsset = /** @class */ (function () {
333
+ function MetaCurveLPPathAsset() {
334
+ }
335
+ MetaCurveLPPathAsset.prototype.getBestPath = function (currentToken, p) {
336
+ return __awaiter(this, void 0, void 0, function () {
337
+ return __generator(this, function (_a) {
338
+ console.log(currentToken);
339
+ console.log(p);
340
+ throw Error("Not implemented yet.");
341
+ });
342
+ });
343
+ };
344
+ return MetaCurveLPPathAsset;
345
+ }());
346
+ exports.MetaCurveLPPathAsset = MetaCurveLPPathAsset;
347
+ var NormalTokenPathAsset = /** @class */ (function () {
348
+ function NormalTokenPathAsset() {
349
+ }
350
+ NormalTokenPathAsset.prototype.getBestPath = function (currentToken, p) {
351
+ return __awaiter(this, void 0, void 0, function () {
352
+ 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;
353
+ return __generator(this, function (_e) {
354
+ switch (_e.label) {
355
+ case 0:
356
+ currentBalance = p.balances[currentToken].sub(1);
357
+ currentTokenAddress = token_1.tokenDataByNetwork[p.networkType][currentToken];
358
+ currentTokenData = token_1.supportedTokens[currentToken];
359
+ deadline = Math.floor(Date.now() / 1000) + 1200;
360
+ callData = {
361
+ targetContract: "",
362
+ callData: ""
363
+ };
364
+ nextToken = p.pool;
365
+ gasLimit = ethers_1.BigNumber.from(0);
366
+ maxAmountOut = ethers_1.BigNumber.from(0);
367
+ _i = 0, ConnectorTokens_1 = token_1.ConnectorTokens;
368
+ _e.label = 1;
369
+ case 1:
370
+ if (!(_i < ConnectorTokens_1.length)) return [3 /*break*/, 19];
371
+ connToken = ConnectorTokens_1[_i];
372
+ connTokenAddress = token_1.tokenDataByNetwork[p.networkType][connToken];
373
+ _a = 0, _b = currentTokenData.swapActions;
374
+ _e.label = 2;
375
+ case 2:
376
+ if (!(_a < _b.length)) return [3 /*break*/, 18];
377
+ swapAction = _b[_a];
378
+ actionType = swapAction.type;
379
+ actionContract = swapAction.contract;
380
+ actionContractAddress = contracts_1.contractsByNetwork[p.networkType][actionContract];
381
+ adapterAddress = p.creditManager.adapters[actionContractAddress];
382
+ call = {
383
+ targetContract: "",
384
+ callData: ""
385
+ };
386
+ amountOut = ethers_1.BigNumber.from(0);
387
+ gasLimitTmp = ethers_1.BigNumber.from(0);
388
+ _c = actionType;
389
+ switch (_c) {
390
+ case tradeTypes_1.TradeType.UniswapV2Swap: return [3 /*break*/, 3];
391
+ case tradeTypes_1.TradeType.UniswapV3Swap: return [3 /*break*/, 6];
392
+ case tradeTypes_1.TradeType.CurveExchange: return [3 /*break*/, 9];
393
+ }
394
+ return [3 /*break*/, 13];
395
+ case 3:
396
+ uniswapV2Adapter = types_1.UniswapV2Adapter__factory.connect(adapterAddress, p.provider);
397
+ path = [currentTokenAddress, connTokenAddress];
398
+ return [4 /*yield*/, uniswapV2Adapter.getAmountsOut(currentBalance, path)];
399
+ case 4:
400
+ amountsOut = _e.sent();
401
+ amountOut = amountsOut[amountsOut.length - 1];
402
+ call = {
403
+ targetContract: adapterAddress,
404
+ callData: types_1.UniswapV2Adapter__factory.createInterface().encodeFunctionData("swapExactTokensForTokens", [currentBalance, amountOut, path, p.creditAccount.addr, deadline])
405
+ };
406
+ return [4 /*yield*/, uniswapV2Adapter.estimateGas.swapExactTokensForTokens(currentBalance, amountOut, path, p.creditAccount.addr, deadline)];
407
+ case 5:
408
+ gasLimitTmp = _e.sent();
409
+ return [3 /*break*/, 14];
410
+ case 6:
411
+ uniswapV3Adapter = types_1.UniswapV3Adapter__factory.connect(adapterAddress, p.provider);
412
+ iQuoter = types_1.IQuoter__factory.connect(contracts_1.UNISWAP_V3_QUOTER, p.provider);
413
+ return [4 /*yield*/, iQuoter.callStatic.quoteExactInputSingle(currentTokenAddress, connTokenAddress, 3000, currentBalance, 0)];
414
+ case 7:
415
+ amountOut = _e.sent();
416
+ exactInputSingleOrder = {
417
+ "tokenIn": currentTokenAddress,
418
+ "tokenOut": connTokenAddress,
419
+ "fee": 3000,
420
+ "recipient": p.creditAccount.addr,
421
+ "amountIn": currentBalance,
422
+ "amountOutMinimum": amountOut,
423
+ "deadline": deadline,
424
+ "sqrtPriceLimitX96": 0
425
+ };
426
+ call = {
427
+ targetContract: adapterAddress,
428
+ callData: types_1.UniswapV3Adapter__factory.createInterface().encodeFunctionData("exactInputSingle", [exactInputSingleOrder])
429
+ };
430
+ return [4 /*yield*/, uniswapV3Adapter.estimateGas.exactInputSingle(exactInputSingleOrder)];
431
+ case 8:
432
+ gasLimitTmp = _e.sent();
433
+ return [3 /*break*/, 14];
434
+ case 9:
435
+ if (!swapAction.tokenOut.includes(connToken)) return [3 /*break*/, 12];
436
+ curve3CrvPool = types_1.CurveV1Adapter__factory.connect(adapterAddress, p.provider);
437
+ currentIndex = token_1.Curve3CrvUnderlyingTokenIndex[currentToken];
438
+ outputIndex = token_1.Curve3CrvUnderlyingTokenIndex[connToken];
439
+ return [4 /*yield*/, curve3CrvPool.get_dy_underlying(currentIndex, outputIndex, currentBalance)];
440
+ case 10:
441
+ amountOut = _e.sent();
442
+ call = {
443
+ targetContract: adapterAddress,
444
+ callData: types_1.CurveV1Adapter__factory.createInterface().encodeFunctionData("exchange_underlying", [currentIndex, outputIndex, currentBalance, amountOut])
445
+ };
446
+ return [4 /*yield*/, curve3CrvPool.estimateGas.exchange_underlying(currentIndex, outputIndex, currentBalance, amountOut)];
447
+ case 11:
448
+ gasLimitTmp = _e.sent();
449
+ _e.label = 12;
450
+ case 12: return [3 /*break*/, 14];
451
+ case 13: throw Error("TradeType not supported. ".concat(actionType));
452
+ case 14:
453
+ if (!(connToken != p.pool)) return [3 /*break*/, 16];
454
+ connectorPathAsset = new ConnectorPathAsset();
455
+ return [4 /*yield*/, connectorPathAsset.getMaxPoolAmount(connToken, amountOut, p)];
456
+ case 15:
457
+ _d = _e.sent(), maxPoolAmount = _d[0], gasLimitConn = _d[1];
458
+ amountOut = maxPoolAmount;
459
+ gasLimitTmp.add(gasLimitConn);
460
+ _e.label = 16;
461
+ case 16:
462
+ if (amountOut > maxAmountOut) {
463
+ callData = call;
464
+ maxAmountOut = amountOut;
465
+ gasLimit = gasLimitTmp;
466
+ }
467
+ _e.label = 17;
468
+ case 17:
469
+ _a++;
470
+ return [3 /*break*/, 2];
471
+ case 18:
472
+ _i++;
473
+ return [3 /*break*/, 1];
474
+ case 19:
475
+ if (callData.targetContract != "") {
476
+ p.balances[nextToken].add(maxAmountOut);
477
+ p.balances[currentToken] = ethers_1.BigNumber.from(1);
478
+ p.totalGasLimit.add(gasLimit);
479
+ p.calls.push(callData);
480
+ }
481
+ return [4 /*yield*/, p.getBestPath()];
482
+ case 20: return [2 /*return*/, _e.sent()];
483
+ }
484
+ });
485
+ });
486
+ };
487
+ return NormalTokenPathAsset;
488
+ }());
489
+ exports.NormalTokenPathAsset = NormalTokenPathAsset;
490
+ var YearnVaultOfCurveLPPathAsset = /** @class */ (function () {
491
+ function YearnVaultOfCurveLPPathAsset() {
492
+ }
493
+ YearnVaultOfCurveLPPathAsset.prototype.getBestPath = function (currentToken, p) {
494
+ return __awaiter(this, void 0, void 0, function () {
495
+ return __generator(this, function (_a) {
496
+ console.log(currentToken);
497
+ console.log(p);
498
+ throw Error("Not implemented yet.");
499
+ });
500
+ });
501
+ };
502
+ return YearnVaultOfCurveLPPathAsset;
503
+ }());
504
+ exports.YearnVaultOfCurveLPPathAsset = YearnVaultOfCurveLPPathAsset;
505
+ var YearnVaultOfMetaCurveLPPathAsset = /** @class */ (function () {
506
+ function YearnVaultOfMetaCurveLPPathAsset() {
507
+ }
508
+ YearnVaultOfMetaCurveLPPathAsset.prototype.getBestPath = function (currentToken, p) {
509
+ return __awaiter(this, void 0, void 0, function () {
510
+ return __generator(this, function (_a) {
511
+ console.log(currentToken);
512
+ console.log(p);
513
+ throw Error("Not implemented yet.");
514
+ });
515
+ });
516
+ };
517
+ return YearnVaultOfMetaCurveLPPathAsset;
518
+ }());
519
+ exports.YearnVaultOfMetaCurveLPPathAsset = YearnVaultOfMetaCurveLPPathAsset;
@@ -0,0 +1,5 @@
1
+ import { SupportedToken } from "../core/token";
2
+ import { Path, PathAsset } from "./path";
3
+ export declare class YearnVaultOfCurveLPPathAsset extends PathAsset {
4
+ getBestPath(currentToken: SupportedToken, p: Path): Promise<Path>;
5
+ }
@@ -0,0 +1,87 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
18
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
19
+ return new (P || (P = Promise))(function (resolve, reject) {
20
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
21
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
22
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
23
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
24
+ });
25
+ };
26
+ var __generator = (this && this.__generator) || function (thisArg, body) {
27
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
28
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
29
+ function verb(n) { return function (v) { return step([n, v]); }; }
30
+ function step(op) {
31
+ if (f) throw new TypeError("Generator is already executing.");
32
+ while (_) try {
33
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
34
+ if (y = 0, t) op = [op[0] & 2, t.value];
35
+ switch (op[0]) {
36
+ case 0: case 1: t = op; break;
37
+ case 4: _.label++; return { value: op[1], done: false };
38
+ case 5: _.label++; y = op[1]; op = [0]; continue;
39
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
40
+ default:
41
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
42
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
43
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
44
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
45
+ if (t[2]) _.ops.pop();
46
+ _.trys.pop(); continue;
47
+ }
48
+ op = body.call(thisArg, _);
49
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
50
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
51
+ }
52
+ };
53
+ Object.defineProperty(exports, "__esModule", { value: true });
54
+ exports.YearnVaultOfCurveLPPathAsset = void 0;
55
+ var token_1 = require("../core/token");
56
+ var path_1 = require("./path");
57
+ var ethers_1 = require("ethers");
58
+ var YearnVaultOfCurveLPPathAsset = /** @class */ (function (_super) {
59
+ __extends(YearnVaultOfCurveLPPathAsset, _super);
60
+ function YearnVaultOfCurveLPPathAsset() {
61
+ return _super !== null && _super.apply(this, arguments) || this;
62
+ }
63
+ YearnVaultOfCurveLPPathAsset.prototype.getBestPath = function (currentToken, p) {
64
+ return __awaiter(this, void 0, void 0, function () {
65
+ var currentBalance, currentTokenData, outputToken, yearnActionData;
66
+ return __generator(this, function (_a) {
67
+ switch (_a.label) {
68
+ case 0:
69
+ currentBalance = p.balances[currentToken].sub(1);
70
+ currentTokenData = token_1.supportedTokens[currentToken];
71
+ outputToken = currentTokenData.lpActions[0].tokenOut;
72
+ return [4 /*yield*/, this.getYearnActionData(currentTokenData.lpActions[0], currentBalance, p)];
73
+ case 1:
74
+ yearnActionData = _a.sent();
75
+ p.balances[outputToken].add(yearnActionData.amountOut);
76
+ p.balances[currentToken] = ethers_1.BigNumber.from(1);
77
+ p.totalGasLimit.add(yearnActionData.gasLimit);
78
+ p.calls.push(yearnActionData.callData);
79
+ return [4 /*yield*/, p.getBestPath()];
80
+ case 2: return [2 /*return*/, _a.sent()];
81
+ }
82
+ });
83
+ });
84
+ };
85
+ return YearnVaultOfCurveLPPathAsset;
86
+ }(path_1.PathAsset));
87
+ exports.YearnVaultOfCurveLPPathAsset = YearnVaultOfCurveLPPathAsset;
@@ -0,0 +1,5 @@
1
+ import { SupportedToken } from "../core/token";
2
+ import { Path, PathAsset } from "./path";
3
+ export declare class YearnVaultOfMetaCurveLPPathAsset extends PathAsset {
4
+ getBestPath(currentToken: SupportedToken, p: Path): Promise<Path>;
5
+ }