@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
@@ -0,0 +1,302 @@
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.PathAsset = exports.Path = void 0;
40
+ var ethers_1 = require("ethers");
41
+ var token_1 = require("../core/token");
42
+ var connectorPathAsset_1 = require("./connectorPathAsset");
43
+ var yearnVaultPathAsset_1 = require("./yearnVaultPathAsset");
44
+ var convexLPTokenPathAsset_1 = require("./convexLPTokenPathAsset");
45
+ var curveLPPathAsset_1 = require("./curveLPPathAsset");
46
+ var metaCurveLPPathAsset_1 = require("./metaCurveLPPathAsset");
47
+ var normalTokenPathAsset_1 = require("./normalTokenPathAsset");
48
+ var yearnVaultOfCurveLPPathAsset_1 = require("./yearnVaultOfCurveLPPathAsset");
49
+ var yearnVaultOfMetaCurveLPPathAsset_1 = require("./yearnVaultOfMetaCurveLPPathAsset");
50
+ var types_1 = require("../types");
51
+ var contracts_1 = require("../core/contracts");
52
+ var tradeTypes_1 = require("../core/tradeTypes");
53
+ var Path = /** @class */ (function () {
54
+ // Path is for selling all assets in the credit account, it will return the calls data to sell all the assets, the amount of pool token will got after selling and the total gas limit for all the calls.
55
+ // This is an example to describe how to use Path:
56
+ // const path = new Path({gasUsed:0, balances: creditAccount.balances, })
57
+ // const closurePath = await path.getBestPath();
58
+ // closurePath.calls -> multicalls (!)
59
+ // closurePath.balances[path.pool] = (X)
60
+ // closurePath.balances[!path.pool] = 0 / 1;
61
+ //
62
+ // balances: the balance of each asset in the credit account
63
+ // pool: the pool token
64
+ // creditManager: credit manager data
65
+ // creditAccount: credit account data
66
+ // networkType: network type (Mainnet, Kovan)
67
+ // provider: ehters rpc provider
68
+ // totalGasLimit: the base gas limit, you can pass zero for not adding any base gas limit
69
+ function Path(opts) {
70
+ this.calls = [];
71
+ this.balances = opts.balances;
72
+ this.pool = opts.pool;
73
+ this.creditManager = opts.creditManager;
74
+ this.creditAccount = opts.creditAccount;
75
+ this.networkType = opts.networkType;
76
+ this.provider = opts.provider;
77
+ this.totalGasLimit = opts.totalGasLimit;
78
+ }
79
+ Path.prototype.comparedByPriority = function (_a, _b) {
80
+ var tokenA = _a[0], _balanceA = _a[1];
81
+ var tokenB = _b[0], _balanceB = _b[1];
82
+ var priorityTokenA = token_1.priority[token_1.supportedTokens[tokenA].type];
83
+ var priorityTokenB = token_1.priority[token_1.supportedTokens[tokenB].type];
84
+ if (priorityTokenA > priorityTokenB) {
85
+ return -1;
86
+ }
87
+ else if (priorityTokenA < priorityTokenB) {
88
+ return 1;
89
+ }
90
+ return 0;
91
+ };
92
+ Path.prototype.getBestPath = function () {
93
+ return __awaiter(this, void 0, void 0, function () {
94
+ var existingTokens, nextToken, pathAsset;
95
+ return __generator(this, function (_a) {
96
+ switch (_a.label) {
97
+ case 0:
98
+ existingTokens = Object.entries(this.balances).filter(function (_a) {
99
+ var _token = _a[0], balance = _a[1];
100
+ return balance.gt(1);
101
+ }).sort(this.comparedByPriority);
102
+ if (existingTokens.length == 1) {
103
+ return [2 /*return*/, this];
104
+ }
105
+ nextToken = existingTokens.at(0)[0];
106
+ // Get balances and keep non-zero only
107
+ // Find token with highest priority
108
+ // Get token type of this token
109
+ switch (token_1.supportedTokens[nextToken].type) {
110
+ case token_1.TokenType.CONNECTOR:
111
+ pathAsset = new connectorPathAsset_1.ConnectorPathAsset();
112
+ break;
113
+ case token_1.TokenType.YEARN_VAULT:
114
+ pathAsset = new yearnVaultPathAsset_1.YearnVaultPathAsset();
115
+ break;
116
+ case token_1.TokenType.CONVEX_LP_TOKEN:
117
+ pathAsset = new convexLPTokenPathAsset_1.ConvexLPTokenPathAsset();
118
+ break;
119
+ case token_1.TokenType.CURVE_LP:
120
+ pathAsset = new curveLPPathAsset_1.CurveLPPathAsset();
121
+ break;
122
+ case token_1.TokenType.META_CURVE_LP:
123
+ pathAsset = new metaCurveLPPathAsset_1.MetaCurveLPPathAsset();
124
+ break;
125
+ case token_1.TokenType.NORMAL_TOKEN:
126
+ pathAsset = new normalTokenPathAsset_1.NormalTokenPathAsset();
127
+ break;
128
+ case token_1.TokenType.YEARN_VAULT_OF_CURVE_LP:
129
+ pathAsset = new yearnVaultOfCurveLPPathAsset_1.YearnVaultOfCurveLPPathAsset();
130
+ break;
131
+ case token_1.TokenType.YEARN_VAULT_OF_META_CURVE_LP:
132
+ pathAsset = new yearnVaultOfMetaCurveLPPathAsset_1.YearnVaultOfMetaCurveLPPathAsset();
133
+ break;
134
+ default:
135
+ throw new Error("Token type not supported yet");
136
+ }
137
+ return [4 /*yield*/, pathAsset.getBestPath(nextToken, this)];
138
+ case 1: return [2 /*return*/, _a.sent()];
139
+ }
140
+ });
141
+ });
142
+ };
143
+ return Path;
144
+ }());
145
+ exports.Path = Path;
146
+ ;
147
+ var PathAsset = /** @class */ (function () {
148
+ function PathAsset() {
149
+ }
150
+ PathAsset.prototype.getUniswapV2SwapData = function (adapterAddress, currentTokenAddress, currentBalance, nextTokenAddress, p) {
151
+ return __awaiter(this, void 0, void 0, function () {
152
+ var deadline, uniswapV2Adapter, path, amountsOut, amountOut, gasLimit, call;
153
+ return __generator(this, function (_a) {
154
+ switch (_a.label) {
155
+ case 0:
156
+ deadline = Math.floor(Date.now() / 1000) + 1200;
157
+ uniswapV2Adapter = types_1.UniswapV2Adapter__factory.connect(adapterAddress, p.provider);
158
+ path = [currentTokenAddress, nextTokenAddress];
159
+ return [4 /*yield*/, uniswapV2Adapter.getAmountsOut(currentBalance, path)];
160
+ case 1:
161
+ amountsOut = _a.sent();
162
+ amountOut = amountsOut[amountsOut.length - 1];
163
+ return [4 /*yield*/, uniswapV2Adapter.estimateGas.swapExactTokensForTokens(currentBalance, amountOut, path, p.creditAccount.addr, deadline)];
164
+ case 2:
165
+ gasLimit = _a.sent();
166
+ call = {
167
+ targetContract: adapterAddress,
168
+ callData: types_1.UniswapV2Adapter__factory.createInterface().encodeFunctionData("swapExactTokensForTokens", [currentBalance, amountOut, path, p.creditAccount.addr, deadline])
169
+ };
170
+ return [2 /*return*/, { callData: call, amountOut: amountOut, gasLimit: gasLimit }];
171
+ }
172
+ });
173
+ });
174
+ };
175
+ PathAsset.prototype.getUniswapV3SwapData = function (adapterAddress, currentTokenAddress, currentBalance, nextTokenAddress, p) {
176
+ return __awaiter(this, void 0, void 0, function () {
177
+ var uniswapV3Adapter, iQuoter, amountOut, deadline, exactInputSingleOrder, call, gasLimit;
178
+ return __generator(this, function (_a) {
179
+ switch (_a.label) {
180
+ case 0:
181
+ uniswapV3Adapter = types_1.UniswapV3Adapter__factory.connect(adapterAddress, p.provider);
182
+ iQuoter = types_1.IQuoter__factory.connect(contracts_1.UNISWAP_V3_QUOTER, p.provider);
183
+ return [4 /*yield*/, iQuoter.callStatic.quoteExactInputSingle(currentTokenAddress, nextTokenAddress, 3000, currentBalance, 0)];
184
+ case 1:
185
+ amountOut = _a.sent();
186
+ deadline = Math.floor(Date.now() / 1000) + 1200;
187
+ exactInputSingleOrder = {
188
+ "tokenIn": currentTokenAddress,
189
+ "tokenOut": nextTokenAddress,
190
+ "fee": 3000,
191
+ "recipient": p.creditAccount.addr,
192
+ "amountIn": currentBalance,
193
+ "amountOutMinimum": amountOut,
194
+ "deadline": deadline,
195
+ "sqrtPriceLimitX96": 0
196
+ };
197
+ call = {
198
+ targetContract: adapterAddress,
199
+ callData: types_1.UniswapV3Adapter__factory.createInterface().encodeFunctionData("exactInputSingle", [exactInputSingleOrder])
200
+ };
201
+ return [4 /*yield*/, uniswapV3Adapter.estimateGas.exactInputSingle(exactInputSingleOrder)];
202
+ case 2:
203
+ gasLimit = _a.sent();
204
+ return [2 /*return*/, { callData: call, amountOut: amountOut, gasLimit: gasLimit }];
205
+ }
206
+ });
207
+ });
208
+ };
209
+ PathAsset.prototype.getCurveActionData = function (adapterAddress, currentToken, currentBalance, nextToken, p) {
210
+ return __awaiter(this, void 0, void 0, function () {
211
+ var curve3CrvPool, currentIndex, outputIndex, amountOut, gasLimit, call;
212
+ return __generator(this, function (_a) {
213
+ switch (_a.label) {
214
+ case 0:
215
+ curve3CrvPool = types_1.CurveV1Adapter__factory.connect(adapterAddress, p.provider);
216
+ currentIndex = token_1.Curve3CrvUnderlyingTokenIndex[currentToken];
217
+ outputIndex = token_1.Curve3CrvUnderlyingTokenIndex[nextToken];
218
+ return [4 /*yield*/, curve3CrvPool.get_dy_underlying(currentIndex, outputIndex, currentBalance)];
219
+ case 1:
220
+ amountOut = _a.sent();
221
+ return [4 /*yield*/, curve3CrvPool.estimateGas.exchange_underlying(currentIndex, outputIndex, currentBalance, amountOut)];
222
+ case 2:
223
+ gasLimit = _a.sent();
224
+ call = {
225
+ targetContract: adapterAddress,
226
+ callData: types_1.CurveV1Adapter__factory.createInterface().encodeFunctionData("exchange_underlying", [currentIndex, outputIndex, currentBalance, amountOut])
227
+ };
228
+ return [2 /*return*/, { callData: call, amountOut: amountOut, gasLimit: gasLimit }];
229
+ }
230
+ });
231
+ });
232
+ };
233
+ PathAsset.prototype.getActionData = function (swapAction, currentTokenAddress, currentToken, currentBalance, nextTokenAddress, nextToken, p) {
234
+ return __awaiter(this, void 0, void 0, function () {
235
+ var actionType, actionContract, actionContractAddress, adapterAddress, _a;
236
+ return __generator(this, function (_b) {
237
+ switch (_b.label) {
238
+ case 0:
239
+ actionType = swapAction.type;
240
+ actionContract = swapAction.contract;
241
+ actionContractAddress = contracts_1.contractsByNetwork[p.networkType][actionContract];
242
+ adapterAddress = p.creditManager.adapters[actionContractAddress];
243
+ _a = actionType;
244
+ switch (_a) {
245
+ case tradeTypes_1.TradeType.UniswapV2Swap: return [3 /*break*/, 1];
246
+ case tradeTypes_1.TradeType.UniswapV3Swap: return [3 /*break*/, 3];
247
+ case tradeTypes_1.TradeType.CurveExchange: return [3 /*break*/, 5];
248
+ }
249
+ return [3 /*break*/, 8];
250
+ case 1: return [4 /*yield*/, this.getUniswapV2SwapData(adapterAddress, currentTokenAddress, currentBalance, nextTokenAddress, p)];
251
+ case 2: return [2 /*return*/, _b.sent()];
252
+ case 3: return [4 /*yield*/, this.getUniswapV3SwapData(adapterAddress, currentTokenAddress, currentBalance, nextTokenAddress, p)];
253
+ case 4: return [2 /*return*/, _b.sent()];
254
+ case 5:
255
+ if (!swapAction.tokenOut.includes(p.pool)) return [3 /*break*/, 7];
256
+ return [4 /*yield*/, this.getCurveActionData(adapterAddress, currentToken, currentBalance, nextToken, p)];
257
+ case 6: return [2 /*return*/, _b.sent()];
258
+ case 7: return [2 /*return*/, {
259
+ callData: {
260
+ targetContract: "",
261
+ callData: ""
262
+ },
263
+ amountOut: ethers_1.BigNumber.from(0),
264
+ gasLimit: ethers_1.BigNumber.from(0)
265
+ }];
266
+ case 8: throw Error("TradeType not supported. ".concat(actionType));
267
+ }
268
+ });
269
+ });
270
+ };
271
+ PathAsset.prototype.getYearnActionData = function (lpAction, currentBalance, p) {
272
+ return __awaiter(this, void 0, void 0, function () {
273
+ var actionContract, allowedContract, adapterAddress, call, IYVault, price, gasLimit;
274
+ return __generator(this, function (_a) {
275
+ switch (_a.label) {
276
+ case 0:
277
+ actionContract = lpAction.contract;
278
+ allowedContract = contracts_1.contractsByNetwork[p.networkType][actionContract];
279
+ adapterAddress = p.creditManager.adapters[allowedContract];
280
+ call = {
281
+ targetContract: adapterAddress,
282
+ callData: types_1.YearnAdapter__factory.createInterface().encodeFunctionData('withdraw', [currentBalance, p.creditAccount.addr])
283
+ };
284
+ IYVault = types_1.YearnAdapter__factory.connect(allowedContract, p.provider);
285
+ return [4 /*yield*/, IYVault.pricePerShare()];
286
+ case 1:
287
+ price = _a.sent();
288
+ return [4 /*yield*/, IYVault.estimateGas["withdraw(uint256,address)"](currentBalance, p.creditAccount.addr)];
289
+ case 2:
290
+ gasLimit = _a.sent();
291
+ return [2 /*return*/, {
292
+ callData: call,
293
+ amountOut: currentBalance.mul(price),
294
+ gasLimit: gasLimit
295
+ }];
296
+ }
297
+ });
298
+ });
299
+ };
300
+ return PathAsset;
301
+ }());
302
+ exports.PathAsset = PathAsset;
@@ -0,0 +1,28 @@
1
+ import { BigNumber } from "ethers";
2
+ import { SupportedToken } from "../core/token";
3
+ import { Path, PathAsset } from "./path";
4
+ export declare class ConnectorPathAsset implements PathAsset {
5
+ getBestPath(currentToken: SupportedToken, p: Path): Promise<Path>;
6
+ getMaxPoolAmount(currentToken: SupportedToken, currentBalance: BigNumber, p: Path): Promise<[BigNumber, BigNumber]>;
7
+ }
8
+ export declare class YearnVaultPathAsset implements PathAsset {
9
+ getBestPath(currentToken: SupportedToken, p: Path): Promise<Path>;
10
+ }
11
+ export declare class ConvexLPTokenPathAsset implements PathAsset {
12
+ getBestPath(currentToken: SupportedToken, p: Path): Promise<Path>;
13
+ }
14
+ export declare class CurveLPPathAsset implements PathAsset {
15
+ getBestPath(currentToken: SupportedToken, p: Path): Promise<Path>;
16
+ }
17
+ export declare class MetaCurveLPPathAsset implements PathAsset {
18
+ getBestPath(currentToken: SupportedToken, p: Path): Promise<Path>;
19
+ }
20
+ export declare class NormalTokenPathAsset implements PathAsset {
21
+ getBestPath(currentToken: SupportedToken, p: Path): Promise<Path>;
22
+ }
23
+ export declare class YearnVaultOfCurveLPPathAsset implements PathAsset {
24
+ getBestPath(currentToken: SupportedToken, p: Path): Promise<Path>;
25
+ }
26
+ export declare class YearnVaultOfMetaCurveLPPathAsset implements PathAsset {
27
+ getBestPath(currentToken: SupportedToken, p: Path): Promise<Path>;
28
+ }