@gearbox-protocol/sdk 0.0.55 → 0.0.58

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 (47) hide show
  1. package/lib/core/adapters.d.ts +6 -5
  2. package/lib/core/adapters.js +6 -5
  3. package/lib/core/contracts.d.ts +9 -2
  4. package/lib/core/contracts.js +14 -1
  5. package/lib/core/pathfinder.d.ts +35 -5
  6. package/lib/core/pathfinder.js +547 -67
  7. package/lib/core/token.d.ts +5 -0
  8. package/lib/core/token.js +8 -1
  9. package/lib/core/tradeTypes.d.ts +2 -0
  10. package/lib/pathfinder/connectorPathAsset.d.ts +7 -0
  11. package/lib/pathfinder/connectorPathAsset.js +144 -0
  12. package/lib/pathfinder/convexLPTokenPathAsset.d.ts +5 -0
  13. package/lib/pathfinder/convexLPTokenPathAsset.js +77 -0
  14. package/lib/pathfinder/curveLPPathAsset.d.ts +5 -0
  15. package/lib/pathfinder/curveLPPathAsset.js +72 -0
  16. package/lib/pathfinder/metaCurveLPPathAsset.d.ts +5 -0
  17. package/lib/pathfinder/metaCurveLPPathAsset.js +72 -0
  18. package/lib/pathfinder/normalTokenPathAsset.d.ts +5 -0
  19. package/lib/pathfinder/normalTokenPathAsset.js +136 -0
  20. package/lib/pathfinder/path.d.ts +41 -0
  21. package/lib/pathfinder/path.js +302 -0
  22. package/lib/pathfinder/pathfinder.d.ts +28 -0
  23. package/lib/pathfinder/pathfinder.js +519 -0
  24. package/lib/pathfinder/yearnVaultOfCurveLPPathAsset.d.ts +5 -0
  25. package/lib/pathfinder/yearnVaultOfCurveLPPathAsset.js +87 -0
  26. package/lib/pathfinder/yearnVaultOfMetaCurveLPPathAsset.d.ts +5 -0
  27. package/lib/pathfinder/yearnVaultOfMetaCurveLPPathAsset.js +87 -0
  28. package/lib/pathfinder/yearnVaultPathAsset.d.ts +5 -0
  29. package/lib/pathfinder/yearnVaultPathAsset.js +87 -0
  30. package/lib/utils/types.d.ts +3 -0
  31. package/lib/utils/types.js +2 -0
  32. package/package.json +1 -1
  33. package/src/core/adapters.ts +6 -5
  34. package/src/core/contracts.ts +24 -1
  35. package/src/core/token.ts +12 -0
  36. package/src/core/tradeTypes.ts +2 -0
  37. package/src/pathfinder/connectorPathAsset.ts +69 -0
  38. package/src/pathfinder/convexLPTokenPathAsset.ts +33 -0
  39. package/src/pathfinder/curveLPPathAsset.ts +10 -0
  40. package/src/pathfinder/metaCurveLPPathAsset.ts +12 -0
  41. package/src/pathfinder/normalTokenPathAsset.ts +68 -0
  42. package/src/pathfinder/path.ts +259 -0
  43. package/src/pathfinder/yearnVaultOfCurveLPPathAsset.ts +22 -0
  44. package/src/pathfinder/yearnVaultOfMetaCurveLPPathAsset.ts +22 -0
  45. package/src/pathfinder/yearnVaultPathAsset.ts +27 -0
  46. package/src/utils/types.ts +3 -0
  47. package/src/core/pathfinder.ts +0 -115
@@ -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.YearnVaultOfMetaCurveLPPathAsset = void 0;
55
+ var token_1 = require("../core/token");
56
+ var path_1 = require("./path");
57
+ var ethers_1 = require("ethers");
58
+ var YearnVaultOfMetaCurveLPPathAsset = /** @class */ (function (_super) {
59
+ __extends(YearnVaultOfMetaCurveLPPathAsset, _super);
60
+ function YearnVaultOfMetaCurveLPPathAsset() {
61
+ return _super !== null && _super.apply(this, arguments) || this;
62
+ }
63
+ YearnVaultOfMetaCurveLPPathAsset.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 YearnVaultOfMetaCurveLPPathAsset;
86
+ }(path_1.PathAsset));
87
+ exports.YearnVaultOfMetaCurveLPPathAsset = YearnVaultOfMetaCurveLPPathAsset;
@@ -0,0 +1,5 @@
1
+ import { SupportedToken } from "../core/token";
2
+ import { Path, PathAsset } from "./path";
3
+ export declare class YearnVaultPathAsset 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.YearnVaultPathAsset = void 0;
55
+ var ethers_1 = require("ethers");
56
+ var token_1 = require("../core/token");
57
+ var path_1 = require("./path");
58
+ var YearnVaultPathAsset = /** @class */ (function (_super) {
59
+ __extends(YearnVaultPathAsset, _super);
60
+ function YearnVaultPathAsset() {
61
+ return _super !== null && _super.apply(this, arguments) || this;
62
+ }
63
+ YearnVaultPathAsset.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 YearnVaultPathAsset;
86
+ }(path_1.PathAsset));
87
+ exports.YearnVaultPathAsset = YearnVaultPathAsset;
@@ -0,0 +1,3 @@
1
+ export declare type PartialRecord<K extends keyof any, T> = {
2
+ [P in K]?: T;
3
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gearbox-protocol/sdk",
3
- "version": "0.0.55",
3
+ "version": "0.0.58",
4
4
  "description": "Gearbox SDK",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./lib/index.d.ts",
@@ -6,9 +6,10 @@ export enum AdapterInterface {
6
6
  CURVE_V1_3ASSETS, // 4 - 16
7
7
  CURVE_V1_4ASSETS, // 5 - 32
8
8
  CURVE_V1_STECRV_POOL, // 6 - 64
9
- YEARN_V2, // 7 - 128
10
- CONVEX_V1_BASE_REWARD_POOL, // 8 - 256
11
- CONVEX_V1_BOOSTER, // 9 - 512
12
- CONVEX_V1_CLAIM_ZAP, // 10 - 1024
13
- LIDO_V1 // 11 - 2048
9
+ CURVE_V1_DEPOSIT, // 7 - 128
10
+ YEARN_V2, // 8 - 256
11
+ CONVEX_V1_BASE_REWARD_POOL, // 9 - 512
12
+ CONVEX_V1_BOOSTER, // 10 - 1024
13
+ CONVEX_V1_CLAIM_ZAP, // 11 - 2048
14
+ LIDO_V1 // 12 - 4096
14
15
  }
@@ -22,6 +22,7 @@ export type CurvePoolContract =
22
22
  | "CURVE_FRAX_POOL"
23
23
  | "CURVE_LUSD_POOL"
24
24
  | "CURVE_GUSD_POOL"
25
+ | "CURVE_SUSD_DEPOSIT"
25
26
  | "CURVE_SUSD_POOL";
26
27
 
27
28
  export type YearnVaultContract =
@@ -64,6 +65,7 @@ export const contractsByNetwork: Record<
64
65
  CURVE_FRAX_POOL: tokenDataByNetwork.Mainnet.FRAX3CRV,
65
66
  CURVE_LUSD_POOL: tokenDataByNetwork.Mainnet.LUSD3CRV,
66
67
  CURVE_SUSD_POOL: "0xA5407eAE9Ba41422680e2e00537571bcC53efBfD", // SEPARATE TOKEN
68
+ CURVE_SUSD_DEPOSIT: "0xFCBa3E75865d2d561BE8D220616520c171F12851",
67
69
  CURVE_GUSD_POOL: "0x4f062658EaAF2C1ccf8C8e36D6824CDf41167956", // SEPARATE TOKEN
68
70
 
69
71
  // YEARN
@@ -97,6 +99,7 @@ export const contractsByNetwork: Record<
97
99
  CURVE_FRAX_POOL: "",
98
100
  CURVE_LUSD_POOL: tokenDataByNetwork.Kovan.LUSD3CRV,
99
101
  CURVE_SUSD_POOL: "0x032f1cE00865F3499C0052ceBA5F2348842416DB", // SEPARATE TOKEN
102
+ CURVE_SUSD_DEPOSIT: "",
100
103
  CURVE_GUSD_POOL: "",
101
104
 
102
105
  // YEARN
@@ -155,6 +158,14 @@ export type CurveSteCRVPoolParams = {
155
158
  lpToken: "steCRV";
156
159
  } & BaseContractParams;
157
160
 
161
+ export type CurveDepositParams = {
162
+ protocol: Protocols.Curve;
163
+ type: AdapterInterface.CURVE_V1_DEPOSIT;
164
+ pool: Record<NetworkType, string>;
165
+ lpToken: CurveLPToken;
166
+ nCoins: number
167
+ } & BaseContractParams;
168
+
158
169
  type YearnParams = {
159
170
  protocol: Protocols.Yearn;
160
171
  type: AdapterInterface.YEARN_V2;
@@ -185,6 +196,7 @@ export type ContractParams =
185
196
  | UniswapV3Params
186
197
  | CurveParams
187
198
  | CurveSteCRVPoolParams
199
+ | CurveDepositParams
188
200
  | YearnParams
189
201
  | ConvexParams
190
202
  | ConvexPoolParams
@@ -247,12 +259,23 @@ export const contractParams: Record<SupportedContract, ContractParams> = {
247
259
  lpToken: "crvPlain3andSUSD",
248
260
  tokens: ["DAI", "USDC", "USDT", "sUSD"]
249
261
  },
262
+ CURVE_SUSD_DEPOSIT: {
263
+ name: "Curve SUSD Deposit",
264
+ protocol: Protocols.Curve,
265
+ type: AdapterInterface.CURVE_V1_DEPOSIT,
266
+ lpToken: "crvPlain3andSUSD",
267
+ pool: {
268
+ Mainnet: "0x22eE18aca7F3Ee920D01F25dA85840D12d98E8Ca",
269
+ Kovan: ""
270
+ },
271
+ nCoins: 4
272
+ },
250
273
  CURVE_GUSD_POOL: {
251
274
  name: "Curve GUSD",
252
275
  protocol: Protocols.Curve,
253
276
  type: AdapterInterface.CURVE_V1_2ASSETS,
254
277
  lpToken: "gusd3CRV",
255
- tokens: ["3Crv", "FRAX"]
278
+ tokens: ["3Crv", "GUSD"]
256
279
  },
257
280
 
258
281
  YEARN_DAI_VAULT: {
package/src/core/token.ts CHANGED
@@ -2,6 +2,8 @@ import { objectEntries, keyToLowercase, swapKeyValue } from "../utils/mappers";
2
2
  import { NetworkType } from "./constants";
3
3
  import { ConvexPoolContract } from "./contracts";
4
4
  import { TradeAction, TradeType } from "./tradeTypes";
5
+ import { PartialRecord } from "../utils/types";
6
+ import { BigNumber } from "ethers";
5
7
 
6
8
  export enum TokenType {
7
9
  CONNECTOR,
@@ -29,6 +31,14 @@ export const priority: Record<TokenType, number> = {
29
31
  [TokenType.DIESEL_LP_TOKEN]: 6
30
32
  };
31
33
 
34
+ export const Curve3CrvUnderlyingTokenIndex: PartialRecord<SupportedToken, BigNumber> = {
35
+ DAI: BigNumber.from(0),
36
+ USDC: BigNumber.from(1),
37
+ USDT: BigNumber.from(2)
38
+ }
39
+
40
+ export const ConnectorTokens: Array<SupportedToken> = ["DAI", "USDC", "WETH", "WBTC"];
41
+
32
42
  export type NormalToken =
33
43
  | "1INCH"
34
44
  | "AAVE"
@@ -182,6 +192,7 @@ type DieselTokenData = {
182
192
 
183
193
  type GearboxTokenData = {
184
194
  symbol: GearboxToken;
195
+ swapActions?: Array<TradeAction>;
185
196
  type: TokenType.NORMAL_TOKEN;
186
197
  } & TokenBase;
187
198
 
@@ -199,6 +210,7 @@ export type TokenDataI =
199
210
  | GearboxTokenData;
200
211
 
201
212
  export const supportedTokens: Record<SupportedToken, TokenDataI> = {
213
+
202
214
  "1INCH": {
203
215
  name: "1INCH",
204
216
  decimals: 18,
@@ -34,10 +34,12 @@ export type TradeAction =
34
34
  | {
35
35
  type: TradeType.UniswapV2Swap;
36
36
  contract: UniswapV2Contract;
37
+ tokenOut?: NormalToken;
37
38
  }
38
39
  | {
39
40
  type: TradeType.UniswapV3Swap;
40
41
  contract: "UNISWAP_V3_ROUTER";
42
+ tokenOut?: NormalToken;
41
43
  }
42
44
  | {
43
45
  type: TradeType.CurveExchange;
@@ -0,0 +1,69 @@
1
+ import {BigNumber} from "ethers";
2
+ import {
3
+ SupportedToken,
4
+ supportedTokens,
5
+ tokenDataByNetwork,
6
+ TokenDataI,
7
+ TokenType
8
+ } from "../core/token";
9
+ import {Path, PathAsset, ActionData} from "./path";
10
+
11
+ export class ConnectorPathAsset extends PathAsset {
12
+ async getBestPath(currentToken: SupportedToken, p: Path): Promise<Path> {
13
+ type ConnectorDataI = Extract<TokenDataI, { type: TokenType.CONNECTOR }>;
14
+
15
+ const currentBalance: BigNumber = p.balances[currentToken].sub(1);
16
+ const currentTokenAddress: string = tokenDataByNetwork[p.networkType][currentToken];
17
+ const currentTokenData: TokenDataI = supportedTokens[currentToken] as ConnectorDataI;
18
+ // connector token will only swap to pool
19
+ const nextToken: SupportedToken = p.pool;
20
+ const nextTokenAddress: string = tokenDataByNetwork[p.networkType][nextToken];
21
+
22
+ let actionData: ActionData = {
23
+ callData: {
24
+ targetContract: "",
25
+ callData: ""
26
+ },
27
+ amountOut: BigNumber.from(0),
28
+ gasLimit: BigNumber.from(0),
29
+ };
30
+ for (let swapAction of currentTokenData.swapActions) {
31
+ const tmpActionData: ActionData = await this.getActionData(swapAction, currentTokenAddress, currentToken, currentBalance, nextTokenAddress, nextToken, p);
32
+
33
+ if (tmpActionData.amountOut > actionData.amountOut) {
34
+ actionData = tmpActionData;
35
+ }
36
+ }
37
+
38
+ if (actionData.callData.targetContract != "") {
39
+ p.balances[nextToken].add(actionData.amountOut);
40
+ p.balances[currentToken] = BigNumber.from(1);
41
+ p.totalGasLimit.add(actionData.gasLimit);
42
+ p.calls.push(actionData.callData);
43
+ }
44
+
45
+ return await p.getBestPath();
46
+ }
47
+
48
+ // Only compute the maximum amount of pool token output, will not modify path.
49
+ async getMaxPoolAmount(currentToken: SupportedToken, currentBalance: BigNumber, p: Path): Promise<BigNumber> {
50
+ type ConnectorDataI = Extract<TokenDataI, { type: TokenType.CONNECTOR }>;
51
+
52
+ const currentTokenAddress: string = tokenDataByNetwork[p.networkType][currentToken];
53
+ const currentTokenData: TokenDataI = supportedTokens[currentToken] as ConnectorDataI;
54
+ // connector token will only swap to pool
55
+ const nextToken: SupportedToken = p.pool;
56
+ const nextTokenAddress: string = tokenDataByNetwork[p.networkType][nextToken];
57
+
58
+ let maxAmountOut: BigNumber = BigNumber.from(0);
59
+ for (let swapAction of currentTokenData.swapActions) {
60
+ const actionData: ActionData = await this.getActionData(swapAction, currentTokenAddress, currentToken, currentBalance, nextTokenAddress, nextToken, p);
61
+
62
+ if (actionData.amountOut > maxAmountOut) {
63
+ maxAmountOut = actionData.amountOut;
64
+ }
65
+ }
66
+
67
+ return maxAmountOut;
68
+ }
69
+ }
@@ -0,0 +1,33 @@
1
+ import {BigNumber} from "ethers";
2
+ import {
3
+ SupportedToken,
4
+ supportedTokens,
5
+ TokenDataI,
6
+ TokenType
7
+ } from "../core/token";
8
+ import {contractsByNetwork} from "../core/contracts";
9
+ import {TradeType} from "../core/tradeTypes";
10
+ import {Path, PathAsset} from "./path";
11
+
12
+ export class ConvexLPTokenPathAsset extends PathAsset {
13
+ async getBestPath(currentToken: SupportedToken, p: Path): Promise<Path> {
14
+ throw Error("Implementation not finished!");
15
+ const currentBalance: BigNumber = p.balances[currentToken].sub(1);
16
+ type ConvexLPTokenDataI = Extract<TokenDataI, { type: TokenType.CONVEX_LP_TOKEN }>;
17
+ const currentTokenData: ConvexLPTokenDataI = supportedTokens[currentToken] as ConvexLPTokenDataI;
18
+
19
+ for (let lpAction of currentTokenData.lpActions) {
20
+ if (lpAction.type == TradeType.ConvexWithdrawLP) {
21
+ const actionContractAddress: string = contractsByNetwork[p.networkType][lpAction.contract];
22
+ const adapterAddress: string = p.creditManager.adapters[actionContractAddress];
23
+ const outputToken: SupportedToken = lpAction.tokenOut as SupportedToken;
24
+ console.log(currentBalance);
25
+ console.log(adapterAddress);
26
+ console.log(outputToken);
27
+ // TODO(byli) wait convex adapter types
28
+ }
29
+ }
30
+
31
+ return await p.getBestPath();
32
+ }
33
+ }
@@ -0,0 +1,10 @@
1
+ import {SupportedToken} from "../core/token";
2
+ import {Path, PathAsset} from "./path";
3
+
4
+ export class CurveLPPathAsset extends PathAsset {
5
+ async getBestPath(currentToken: SupportedToken, p: Path): Promise<Path> {
6
+ console.log(currentToken);
7
+ console.log(p);
8
+ throw Error("Not implemented yet.");
9
+ }
10
+ }
@@ -0,0 +1,12 @@
1
+ import {
2
+ SupportedToken,
3
+ } from "../core/token";
4
+ import {Path, PathAsset} from "./path";
5
+
6
+ export class MetaCurveLPPathAsset extends PathAsset {
7
+ async getBestPath(currentToken: SupportedToken, p: Path): Promise<Path> {
8
+ console.log(currentToken);
9
+ console.log(p);
10
+ throw Error("Not implemented yet.");
11
+ }
12
+ }
@@ -0,0 +1,68 @@
1
+ import {BigNumber} from "ethers";
2
+ import {
3
+ ConnectorTokens,
4
+ SupportedToken,
5
+ supportedTokens,
6
+ tokenDataByNetwork,
7
+ TokenDataI,
8
+ TokenType
9
+ } from "../core/token";
10
+ import {ActionData, Path, PathAsset} from "./path";
11
+ import {ConnectorPathAsset} from "./connectorPathAsset";
12
+
13
+ export class NormalTokenPathAsset extends PathAsset {
14
+ async getBestPath(currentToken: SupportedToken, p: Path): Promise<Path> {
15
+ type ConnectorDataI = Extract<TokenDataI, { type: TokenType.NORMAL_TOKEN }>;
16
+
17
+ const currentBalance: BigNumber = p.balances[currentToken].sub(1);
18
+ const currentTokenAddress: string = tokenDataByNetwork[p.networkType][currentToken];
19
+ const currentTokenData: TokenDataI = supportedTokens[currentToken] as ConnectorDataI;
20
+
21
+ let actionData: ActionData = {
22
+ callData: {
23
+ targetContract: "",
24
+ callData: ""
25
+ },
26
+ amountOut: BigNumber.from(0),
27
+ gasLimit: BigNumber.from(0),
28
+ };
29
+ let nextToken: SupportedToken = p.pool;
30
+ let maxPoolAmountOut: BigNumber = BigNumber.from(0);
31
+
32
+ // enumerate connector token and find a best one
33
+ for (let connToken of ConnectorTokens) {
34
+ const connTokenAddress: string = tokenDataByNetwork[p.networkType][connToken];
35
+
36
+ for (let swapAction of currentTokenData.swapActions!) {
37
+ const tmpActionData: ActionData = await this.getActionData(swapAction, currentTokenAddress, currentToken, currentBalance, connTokenAddress, connToken, p);
38
+
39
+ // compute connToken to pool token
40
+ if (connToken != p.pool) {
41
+ const connectorPathAsset = new ConnectorPathAsset();
42
+ const poolAmountOut: BigNumber = await connectorPathAsset.getMaxPoolAmount(connToken, actionData.amountOut, p);
43
+ if (poolAmountOut > maxPoolAmountOut) {
44
+ maxPoolAmountOut = poolAmountOut;
45
+
46
+ actionData = tmpActionData;
47
+ nextToken = connToken;
48
+ }
49
+ } else {
50
+ if (actionData.amountOut > maxPoolAmountOut) {
51
+ maxPoolAmountOut = actionData.amountOut;
52
+
53
+ actionData = tmpActionData;
54
+ nextToken = p.pool;
55
+ }
56
+ }
57
+ }
58
+ }
59
+
60
+ if (actionData.callData.targetContract != "") {
61
+ p.balances[nextToken].add(actionData.amountOut);
62
+ p.balances[currentToken] = BigNumber.from(1);
63
+ p.totalGasLimit.add(actionData.gasLimit);
64
+ p.calls.push(actionData.callData);
65
+ }
66
+ return await p.getBestPath();
67
+ }
68
+ }