@gearbox-protocol/sdk 0.0.102 → 0.0.103

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 (119) hide show
  1. package/.eslintignore +4 -0
  2. package/.eslintrc.js +52 -0
  3. package/.husky/pre-push +4 -0
  4. package/lib/apy/convexAPY.js +3 -5
  5. package/lib/apy/lidoAPY.js +8 -9
  6. package/lib/contracts/contracts.d.ts +3 -3
  7. package/lib/contracts/contracts.js +2 -2
  8. package/lib/core/constants.d.ts +1 -1
  9. package/lib/core/constants.js +2 -2
  10. package/lib/core/creditAccount.js +7 -5
  11. package/lib/core/creditManager.d.ts +1 -1
  12. package/lib/core/creditManager.js +1 -7
  13. package/lib/core/creditSession.js +14 -3
  14. package/lib/core/eventOrTx.d.ts +1 -1
  15. package/lib/core/events.d.ts +19 -19
  16. package/lib/core/events.js +23 -21
  17. package/lib/core/pool.d.ts +1 -1
  18. package/lib/core/pool.js +1 -7
  19. package/lib/core/price.js +6 -1
  20. package/lib/core/strategy.d.ts +1 -3
  21. package/lib/core/strategy.js +14 -13
  22. package/lib/core/tokenDistributor.js +1 -1
  23. package/lib/core/transactions.d.ts +18 -3
  24. package/lib/core/transactions.js +39 -3
  25. package/lib/index.d.ts +1 -2
  26. package/lib/index.js +1 -1
  27. package/lib/oracles/priceFeeds.js +1 -1
  28. package/lib/pathfinder/convexLP.d.ts +1 -1
  29. package/lib/pathfinder/convexLP.js +26 -29
  30. package/lib/pathfinder/curveLP.d.ts +1 -1
  31. package/lib/pathfinder/curveLP.js +5 -7
  32. package/lib/pathfinder/path.d.ts +1 -1
  33. package/lib/pathfinder/path.js +38 -42
  34. package/lib/pathfinder/trade.d.ts +1 -2
  35. package/lib/pathfinder/tradeTypes.d.ts +5 -5
  36. package/lib/pathfinder/yVault.d.ts +2 -2
  37. package/lib/pathfinder/yVault.js +18 -15
  38. package/lib/strategies/curve.d.ts +6 -60
  39. package/lib/strategies/curve.js +6 -0
  40. package/lib/strategies/uniswapV2.js +3 -19
  41. package/lib/strategies/uniswapV3.js +1 -1
  42. package/lib/strategies/yearn.js +15 -11
  43. package/lib/tokens/convex.d.ts +3 -3
  44. package/lib/tokens/curveLP.d.ts +2 -2
  45. package/lib/tokens/curveLP.js +1 -1
  46. package/lib/tokens/gear.d.ts +2 -2
  47. package/lib/tokens/gear.js +1 -1
  48. package/lib/tokens/normal.d.ts +1 -1
  49. package/lib/tokens/token.js +4 -4
  50. package/lib/tokens/tokenData.d.ts +3 -1
  51. package/lib/tokens/tokenData.js +10 -8
  52. package/lib/tokens/yearn.d.ts +3 -3
  53. package/lib/utils/errors.d.ts +6 -0
  54. package/lib/utils/errors.js +13 -0
  55. package/lib/utils/formatter.d.ts +1 -1
  56. package/lib/utils/formatter.js +17 -14
  57. package/lib/utils/loading.d.ts +2 -1
  58. package/lib/utils/loading.js +9 -13
  59. package/lib/utils/mappers.js +2 -2
  60. package/lib/utils/network.js +2 -2
  61. package/lib/utils/repeater.js +12 -24
  62. package/lib/utils/validate.js +1 -1
  63. package/package.json +26 -8
  64. package/src/apy/convexAPY.ts +6 -6
  65. package/src/apy/lidoAPY.ts +12 -11
  66. package/src/contracts/contracts.ts +8 -7
  67. package/src/core/constants.ts +1 -1
  68. package/src/core/creditAccount.ts +28 -5
  69. package/src/core/creditManager.ts +29 -4
  70. package/src/core/creditOperation.ts +7 -7
  71. package/src/core/creditSession.ts +25 -5
  72. package/src/core/errors.ts +1 -0
  73. package/src/core/eventOrTx.ts +8 -5
  74. package/src/core/events.ts +85 -24
  75. package/src/core/history.ts +46 -46
  76. package/src/core/operations.ts +6 -0
  77. package/src/core/pool.ts +16 -4
  78. package/src/core/price.ts +7 -3
  79. package/src/core/strategy.ts +27 -23
  80. package/src/core/tokenDistributor.ts +2 -2
  81. package/src/core/transactions.ts +427 -350
  82. package/src/index.ts +1 -3
  83. package/src/oracles/priceFeeds.ts +523 -523
  84. package/src/pathfinder/contracts.ts +15 -13
  85. package/src/pathfinder/convexLP.ts +29 -25
  86. package/src/pathfinder/curveLP.ts +57 -53
  87. package/src/pathfinder/path.ts +17 -9
  88. package/src/pathfinder/priority.ts +11 -11
  89. package/src/pathfinder/trade.ts +84 -77
  90. package/src/pathfinder/tradeTypes.ts +98 -94
  91. package/src/pathfinder/yVault.ts +26 -11
  92. package/src/payload/token.ts +3 -3
  93. package/src/strategies/convex.ts +217 -210
  94. package/src/strategies/creditFacade.ts +70 -53
  95. package/src/strategies/curve.ts +263 -194
  96. package/src/strategies/lido.ts +33 -37
  97. package/src/strategies/uniswapV2.ts +90 -112
  98. package/src/strategies/uniswapV3.ts +114 -91
  99. package/src/strategies/yearn.ts +58 -62
  100. package/src/tokens/connectors.ts +6 -6
  101. package/src/tokens/convex.ts +297 -296
  102. package/src/tokens/curveLP.ts +170 -165
  103. package/src/tokens/gear.ts +47 -45
  104. package/src/tokens/normal.ts +801 -802
  105. package/src/tokens/token.ts +9 -5
  106. package/src/tokens/tokenData.ts +19 -9
  107. package/src/tokens/tokenType.ts +11 -11
  108. package/src/tokens/yearn.ts +126 -124
  109. package/src/utils/errors.ts +11 -0
  110. package/src/utils/formatter.ts +22 -18
  111. package/src/utils/loading.ts +14 -6
  112. package/src/utils/mappers.ts +8 -6
  113. package/src/utils/multicall.ts +2 -0
  114. package/src/utils/network.ts +21 -21
  115. package/src/utils/repeater.ts +2 -2
  116. package/src/utils/validate.ts +1 -1
  117. package/lib/utils/events.d.ts +0 -2
  118. package/lib/utils/events.js +0 -13
  119. package/src/utils/events.ts +0 -10
package/lib/core/price.js CHANGED
@@ -2,8 +2,13 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.calcTotalPrice = void 0;
4
4
  var ethers_1 = require("ethers");
5
+ var constants_1 = require("./constants");
5
6
  var calcTotalPrice = function (price, amount, decimals) {
6
7
  if (decimals === void 0) { decimals = 18; }
7
- return amount.mul(price).div(ethers_1.BigNumber.from(10).pow(decimals));
8
+ return amount
9
+ .mul(constants_1.WAD)
10
+ .mul(price)
11
+ .div(ethers_1.BigNumber.from(10).pow(decimals))
12
+ .div(constants_1.PRICE_DECIMALS);
8
13
  };
9
14
  exports.calcTotalPrice = calcTotalPrice;
@@ -26,11 +26,9 @@ export declare class Strategy {
26
26
  leveragableCollateral: Array<string>;
27
27
  baseAssets: Array<string>;
28
28
  constructor(payload: StrategyPayload);
29
- maxAPY(apy: number, maxLeverage: number, poolApy: PoolList): number;
29
+ maxAPY(maxLeverage: number, poolApy: PoolList): number;
30
30
  overallAPY(apy: number, leverage: number, depositCollateral: string, borrowAPY: number): number;
31
31
  liquidationPrice(borrowed: TokenDescription, collateral: TokenDescription, lp: TokenDescription, ltCollateral: BigNumber): BigNumber;
32
- private roi;
33
- private minBorrowApy;
34
32
  private farmLev;
35
33
  private inBaseAssets;
36
34
  private inLeveragableAssets;
@@ -2,8 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Strategy = void 0;
4
4
  var ethers_1 = require("ethers");
5
- var constants_1 = require("../core/constants");
6
- var price_1 = require("../core/price");
5
+ var constants_1 = require("./constants");
6
+ var price_1 = require("./price");
7
7
  var Strategy = /** @class */ (function () {
8
8
  function Strategy(payload) {
9
9
  this.apy = payload.apy;
@@ -14,14 +14,15 @@ var Strategy = /** @class */ (function () {
14
14
  this.leveragableCollateral = payload.leveragableCollateral;
15
15
  this.baseAssets = payload.baseAssets;
16
16
  }
17
- Strategy.prototype.maxAPY = function (apy, maxLeverage, poolApy) {
18
- var minApy = this.minBorrowApy(poolApy);
19
- return this.roi(apy, maxLeverage, maxLeverage - constants_1.LEVERAGE_DECIMALS, minApy);
17
+ Strategy.prototype.maxAPY = function (maxLeverage, poolApy) {
18
+ var minApy = minBorrowApy(poolApy);
19
+ return roi(this.apy || 0, maxLeverage, maxLeverage - constants_1.LEVERAGE_DECIMALS, minApy);
20
20
  };
21
21
  Strategy.prototype.overallAPY = function (apy, leverage, depositCollateral, borrowAPY) {
22
22
  var farmLev = this.farmLev(leverage, depositCollateral);
23
- return this.roi(apy, farmLev, leverage - constants_1.LEVERAGE_DECIMALS, borrowAPY);
23
+ return roi(apy, farmLev, leverage - constants_1.LEVERAGE_DECIMALS, borrowAPY);
24
24
  };
25
+ // eslint-disable-next-line class-methods-use-this
25
26
  Strategy.prototype.liquidationPrice = function (borrowed, collateral, lp, ltCollateral) {
26
27
  var borrowedMoney = (0, price_1.calcTotalPrice)(borrowed.price, borrowed.amount, borrowed.decimals);
27
28
  var collateralMoney = (0, price_1.calcTotalPrice)(collateral.price, collateral.amount, collateral.decimals)
@@ -32,13 +33,6 @@ var Strategy = /** @class */ (function () {
32
33
  ? borrowedMoney.sub(collateralMoney).mul(constants_1.WAD).div(lpMoney)
33
34
  : ethers_1.BigNumber.from(0);
34
35
  };
35
- Strategy.prototype.roi = function (apy, farmLev, debtLev, borrowAPY) {
36
- return (apy * farmLev - borrowAPY * debtLev) / constants_1.LEVERAGE_DECIMALS;
37
- };
38
- Strategy.prototype.minBorrowApy = function (poolApy) {
39
- var apys = Object.values(poolApy).sort(function (a, b) { return a.borrowRate - b.borrowRate; });
40
- return apys.length > 0 ? apys[0].borrowRate : 0;
41
- };
42
36
  Strategy.prototype.farmLev = function (leverage, depositCollateral) {
43
37
  return this.inBaseAssets(depositCollateral) ||
44
38
  this.inLeveragableAssets(depositCollateral)
@@ -54,3 +48,10 @@ var Strategy = /** @class */ (function () {
54
48
  return Strategy;
55
49
  }());
56
50
  exports.Strategy = Strategy;
51
+ function roi(apy, farmLev, debtLev, borrowAPY) {
52
+ return (apy * farmLev - borrowAPY * debtLev) / constants_1.LEVERAGE_DECIMALS;
53
+ }
54
+ function minBorrowApy(poolApy) {
55
+ var apys = Object.values(poolApy).sort(function (a, b) { return a.borrowRate - b.borrowRate; });
56
+ return apys.length > 0 ? apys[0].borrowRate : 0;
57
+ }
@@ -5,5 +5,5 @@ var VotingPower;
5
5
  (function (VotingPower) {
6
6
  VotingPower[VotingPower["A"] = 0] = "A";
7
7
  VotingPower[VotingPower["B"] = 1] = "B";
8
- VotingPower[VotingPower["ZERO_VOTING_POWER"] = 2] = "ZERO_VOTING_POWER";
8
+ VotingPower[VotingPower["ZERO_VOTING_POWER"] = 2] = "ZERO_VOTING_POWER"; // zero voting power & B-type vesting parameters
9
9
  })(VotingPower = exports.VotingPower || (exports.VotingPower = {}));
@@ -2,7 +2,7 @@ import { BigNumber } from "ethers";
2
2
  import { TokenData } from "../tokens/tokenData";
3
3
  import { EVMTx, EVMTxProps } from "./eventOrTx";
4
4
  export interface TxSerialized {
5
- type: "TxAddLiquidity" | "TxRemoveLiquidity" | "TxSwap" | "TxAddCollateral" | "TxIncreaseBorrowAmount" | "TxOpenAccount" | "TxRepayAccount" | "TxCloseAccount" | "TxApprove";
5
+ type: "TxAddLiquidity" | "TxRemoveLiquidity" | "TxSwap" | "TxAddCollateral" | "TxIncreaseBorrowAmount" | "TxOpenAccount" | "TxRepayAccount" | "TxCloseAccount" | "TxApprove" | "TxOpenMultitokenAccount";
6
6
  content: string;
7
7
  }
8
8
  export declare class TxSerializer {
@@ -97,13 +97,28 @@ export declare class TxOpenAccount extends EVMTx {
97
97
  toString(tokenData: Record<string, TokenData>): string;
98
98
  serialize(): TxSerialized;
99
99
  }
100
+ interface TxOpenMultitokenAccountProps extends EVMTxProps {
101
+ borrowedAmount: BigNumber;
102
+ creditManager: string;
103
+ underlyingToken: string;
104
+ assets: Array<string>;
105
+ }
106
+ export declare class TxOpenMultitokenAccount extends EVMTx {
107
+ readonly borrowedAmount: BigNumber;
108
+ readonly creditManager: string;
109
+ readonly underlyingToken: string;
110
+ readonly assets: Array<string>;
111
+ constructor(opts: TxOpenMultitokenAccountProps);
112
+ toString(tokenData: Record<string, TokenData>): string;
113
+ serialize(): TxSerialized;
114
+ }
100
115
  interface RepayAccountProps extends EVMTxProps {
101
116
  creditManager: string;
102
117
  }
103
118
  export declare class TxRepayAccount extends EVMTx {
104
119
  readonly creditManager: string;
105
120
  constructor(opts: RepayAccountProps);
106
- toString(_: Record<string, TokenData>): string;
121
+ toString(): string;
107
122
  serialize(): TxSerialized;
108
123
  }
109
124
  interface CloseAccountProps extends EVMTxProps {
@@ -112,7 +127,7 @@ interface CloseAccountProps extends EVMTxProps {
112
127
  export declare class TxCloseAccount extends EVMTx {
113
128
  readonly creditManager: string;
114
129
  constructor(opts: CloseAccountProps);
115
- toString(_: Record<string, TokenData>): string;
130
+ toString(): string;
116
131
  serialize(): TxSerialized;
117
132
  }
118
133
  interface ApproveProps extends EVMTxProps {
@@ -15,7 +15,7 @@ var __extends = (this && this.__extends) || (function () {
15
15
  };
16
16
  })();
17
17
  Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.TxApprove = exports.TxCloseAccount = exports.TxRepayAccount = exports.TxOpenAccount = exports.TxIncreaseBorrowAmount = exports.TxAddCollateral = exports.TXSwap = exports.TxRemoveLiquidity = exports.TxAddLiquidity = exports.TxSerializer = void 0;
18
+ exports.TxApprove = exports.TxCloseAccount = exports.TxRepayAccount = exports.TxOpenMultitokenAccount = exports.TxOpenAccount = exports.TxIncreaseBorrowAmount = exports.TxAddCollateral = exports.TXSwap = exports.TxRemoveLiquidity = exports.TxAddLiquidity = exports.TxSerializer = void 0;
19
19
  var ethers_1 = require("ethers");
20
20
  var formatter_1 = require("../utils/formatter");
21
21
  var eventOrTx_1 = require("./eventOrTx");
@@ -49,6 +49,8 @@ var TxSerializer = /** @class */ (function () {
49
49
  return new TxCloseAccount(params);
50
50
  case "TxApprove":
51
51
  return new TxApprove(params);
52
+ case "TxOpenMultitokenAccount":
53
+ return new TxOpenMultitokenAccount(params);
52
54
  default:
53
55
  throw new Error("Unknown transaction for parsing");
54
56
  }
@@ -231,6 +233,40 @@ var TxOpenAccount = /** @class */ (function (_super) {
231
233
  return TxOpenAccount;
232
234
  }(eventOrTx_1.EVMTx));
233
235
  exports.TxOpenAccount = TxOpenAccount;
236
+ var TxOpenMultitokenAccount = /** @class */ (function (_super) {
237
+ __extends(TxOpenMultitokenAccount, _super);
238
+ function TxOpenMultitokenAccount(opts) {
239
+ var _this = _super.call(this, {
240
+ block: opts.block,
241
+ txHash: opts.txHash,
242
+ txStatus: opts.txStatus,
243
+ timestamp: opts.timestamp
244
+ }) || this;
245
+ _this.borrowedAmount = ethers_1.BigNumber.from(opts.borrowedAmount);
246
+ _this.underlyingToken = opts.underlyingToken;
247
+ _this.creditManager = opts.creditManager;
248
+ _this.assets = opts.assets;
249
+ return _this;
250
+ }
251
+ TxOpenMultitokenAccount.prototype.toString = function (tokenData) {
252
+ var underlyingToken = tokenData[this.underlyingToken.toLowerCase()];
253
+ var assetSymbols = this.assets.reduce(function (acc, assetAddress) {
254
+ var token = tokenData[assetAddress.toLowerCase()];
255
+ if (token)
256
+ acc.push(token.symbol);
257
+ return acc;
258
+ }, []);
259
+ return "Credit account ".concat((0, contractsRegister_1.getContractName)(this.creditManager), ": opening. Borrowed amount: ").concat((0, formatter_1.formatBN)(this.borrowedAmount, (underlyingToken === null || underlyingToken === void 0 ? void 0 : underlyingToken.decimals) || 18), " ").concat(underlyingToken === null || underlyingToken === void 0 ? void 0 : underlyingToken.symbol, "; assets: ").concat(assetSymbols.join(", "));
260
+ };
261
+ TxOpenMultitokenAccount.prototype.serialize = function () {
262
+ return {
263
+ type: "TxOpenMultitokenAccount",
264
+ content: JSON.stringify(this)
265
+ };
266
+ };
267
+ return TxOpenMultitokenAccount;
268
+ }(eventOrTx_1.EVMTx));
269
+ exports.TxOpenMultitokenAccount = TxOpenMultitokenAccount;
234
270
  var TxRepayAccount = /** @class */ (function (_super) {
235
271
  __extends(TxRepayAccount, _super);
236
272
  function TxRepayAccount(opts) {
@@ -243,7 +279,7 @@ var TxRepayAccount = /** @class */ (function (_super) {
243
279
  _this.creditManager = opts.creditManager;
244
280
  return _this;
245
281
  }
246
- TxRepayAccount.prototype.toString = function (_) {
282
+ TxRepayAccount.prototype.toString = function () {
247
283
  return "Credit account ".concat((0, contractsRegister_1.getContractName)(this.creditManager), ": Repaying account");
248
284
  };
249
285
  TxRepayAccount.prototype.serialize = function () {
@@ -267,7 +303,7 @@ var TxCloseAccount = /** @class */ (function (_super) {
267
303
  _this.creditManager = opts.creditManager;
268
304
  return _this;
269
305
  }
270
- TxCloseAccount.prototype.toString = function (_) {
306
+ TxCloseAccount.prototype.toString = function () {
271
307
  return "Credit account ".concat((0, contractsRegister_1.getContractName)(this.creditManager), ": Closing account");
272
308
  };
273
309
  TxCloseAccount.prototype.serialize = function () {
package/lib/index.d.ts CHANGED
@@ -23,12 +23,10 @@ export * from "./payload/token";
23
23
  export * from "./utils/formatter";
24
24
  export * from "./utils/loading";
25
25
  export * from "./utils/validate";
26
- export * from "./utils/events";
27
26
  export * from "./utils/math";
28
27
  export * from "./payload/graphPayload";
29
28
  export * from "./types/index";
30
29
  export * from "./core/strategy";
31
- export type { IDataCompressor, IWETHGateway } from "./types";
32
30
  export { TokenType } from "./tokens/tokenType";
33
31
  export * from "./tokens/token";
34
32
  export * from "./tokens/tokenData";
@@ -43,6 +41,7 @@ export * from "./apy/convexAPY";
43
41
  export * from "./core/history";
44
42
  export * from "./utils/multicall";
45
43
  export * from "./utils/types";
44
+ export * from "./utils/errors";
46
45
  export { callRepeater } from "./utils/repeater";
47
46
  export { getContractName } from "./contracts/contractsRegister";
48
47
  export { AdapterInterface } from "./contracts/adapters";
package/lib/index.js CHANGED
@@ -40,7 +40,6 @@ __exportStar(require("./payload/token"), exports);
40
40
  __exportStar(require("./utils/formatter"), exports);
41
41
  __exportStar(require("./utils/loading"), exports);
42
42
  __exportStar(require("./utils/validate"), exports);
43
- __exportStar(require("./utils/events"), exports);
44
43
  __exportStar(require("./utils/math"), exports);
45
44
  __exportStar(require("./payload/graphPayload"), exports);
46
45
  __exportStar(require("./types/index"), exports);
@@ -61,6 +60,7 @@ __exportStar(require("./apy/convexAPY"), exports);
61
60
  __exportStar(require("./core/history"), exports);
62
61
  __exportStar(require("./utils/multicall"), exports);
63
62
  __exportStar(require("./utils/types"), exports);
63
+ __exportStar(require("./utils/errors"), exports);
64
64
  var repeater_1 = require("./utils/repeater");
65
65
  Object.defineProperty(exports, "callRepeater", { enumerable: true, get: function () { return repeater_1.callRepeater; } });
66
66
  var contractsRegister_1 = require("./contracts/contractsRegister");
@@ -446,7 +446,7 @@ exports.priceFeedsByNetwork = {
446
446
  curveSymbol: "gusd3CRV"
447
447
  }
448
448
  },
449
- //GEARBOX
449
+ // GEARBOX
450
450
  dDAI: {
451
451
  priceFeedETH: {
452
452
  type: oracles_1.OracleType.CHAINLINK_ORACLE,
@@ -1,4 +1,4 @@
1
- import { LPWithdrawPathFinder, Path } from "./path";
1
+ import type { LPWithdrawPathFinder, Path } from "./path";
2
2
  export declare class ConvexLPPathFinder implements LPWithdrawPathFinder {
3
3
  findWithdrawPaths(p: Path): Promise<Array<Path>>;
4
4
  }
@@ -38,40 +38,37 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
39
  exports.ConvexLPPathFinder = void 0;
40
40
  var convex_1 = require("../tokens/convex");
41
+ var mappers_1 = require("../utils/mappers");
41
42
  var ConvexLPPathFinder = /** @class */ (function () {
42
43
  function ConvexLPPathFinder() {
43
44
  }
45
+ // eslint-disable-next-line class-methods-use-this
44
46
  ConvexLPPathFinder.prototype.findWithdrawPaths = function (p) {
45
47
  return __awaiter(this, void 0, void 0, function () {
46
- var pids, _i, _a, _b, cvxToken, tokenData, currentBalance;
47
- return __generator(this, function (_c) {
48
- switch (_c.label) {
49
- case 0:
50
- pids = new Set();
51
- for (_i = 0, _a = Object.entries(convex_1.convexTokens); _i < _a.length; _i++) {
52
- _b = _a[_i], cvxToken = _b[0], tokenData = _b[1];
53
- currentBalance = p.popBalance(cvxToken);
54
- if (currentBalance.gt(1)) {
55
- pids.add(tokenData.pid);
56
- }
57
- }
58
- return [4 /*yield*/, p.withdrawTokens()];
59
- case 1:
60
- // const convexPathFinder = ConvexPathFinder__factory.connect(
61
- // pathFindersByNetwork[p.networkType].CONVEX_PATH_FINDER,
62
- // p.provider
63
- // );
64
- // const tokenBalances = await convexPathFinder.calcRewards(
65
- // p.creditAccount.addr,
66
- // Array.from(pids)
67
- // );
68
- // for (let balance of tokenBalances) {
69
- // p.balances[balance.token] = p.balances[balance.token].add(
70
- // balance.balance
71
- // );
72
- // }
73
- return [2 /*return*/, _c.sent()];
74
- }
48
+ var pids;
49
+ return __generator(this, function (_a) {
50
+ pids = (0, mappers_1.objectEntries)(convex_1.convexTokens).reduce(function (acc, _a) {
51
+ var cvxToken = _a[0], tokenData = _a[1];
52
+ var currentBalance = p.popBalance(cvxToken);
53
+ if (currentBalance.gt(1)) {
54
+ pids.add(tokenData.pid);
55
+ }
56
+ return acc;
57
+ }, new Set());
58
+ // const convexPathFinder = ConvexPathFinder__factory.connect(
59
+ // pathFindersByNetwork[p.networkType].CONVEX_PATH_FINDER,
60
+ // p.provider
61
+ // );
62
+ // const tokenBalances = await convexPathFinder.calcRewards(
63
+ // p.creditAccount.addr,
64
+ // Array.from(pids)
65
+ // );
66
+ // for (let balance of tokenBalances) {
67
+ // p.balances[balance.token] = p.balances[balance.token].add(
68
+ // balance.balance
69
+ // );
70
+ // }
71
+ return [2 /*return*/, p.withdrawTokens()];
75
72
  });
76
73
  });
77
74
  };
@@ -1,4 +1,4 @@
1
- import { LPWithdrawPathFinder, Path } from "./path";
1
+ import type { LPWithdrawPathFinder, Path } from "./path";
2
2
  import { CurveLPToken } from "../tokens/curveLP";
3
3
  import { CurvePoolContract } from "../contracts/contracts";
4
4
  export declare class CurvePathFinder implements LPWithdrawPathFinder {
@@ -45,12 +45,11 @@ var types_1 = require("../types");
45
45
  var CurvePathFinder = /** @class */ (function () {
46
46
  function CurvePathFinder(token) {
47
47
  this.lpToken = token;
48
- var curvePools = curveLP_1.curveTokens[this.lpToken].lpActions.filter(function (a) { return a.type == tradeTypes_1.TradeType.CurveWithdrawLP; });
48
+ var curvePools = curveLP_1.curveTokens[this.lpToken].lpActions.filter(function (a) { return a.type === tradeTypes_1.TradeType.CurveWithdrawLP; });
49
49
  if (curvePools.length !== 1)
50
50
  throw new Error("Cant find Withdrawer");
51
51
  this.contract = curvePools[0].contract;
52
- var wrapper = contracts_1.contractParams[curvePools[0].contract]
53
- .wrapper;
52
+ var wrapper = contracts_1.contractParams[curvePools[0].contract].wrapper;
54
53
  if (wrapper) {
55
54
  this.contract = wrapper;
56
55
  }
@@ -66,9 +65,8 @@ var CurvePathFinder = /** @class */ (function () {
66
65
  lpBalance = p.popBalance(this.lpToken);
67
66
  multiCallContract = new multicall_1.MultiCallContract(contracts_1.contractsByAddress[this.contract], types_1.ICurvePool__factory.createInterface(), p.provider);
68
67
  data = [];
69
- for (i = 0; i < nCoins; i++) {
68
+ for (i = 0; i < nCoins; i += 1) {
70
69
  data.push({
71
- // @ts-ignore
72
70
  method: "calc_withdraw_one_coin(uint256,int128)",
73
71
  params: [lpBalance, i]
74
72
  });
@@ -76,9 +74,9 @@ var CurvePathFinder = /** @class */ (function () {
76
74
  return [4 /*yield*/, multiCallContract.call(data)];
77
75
  case 1:
78
76
  balances = _a.sent();
79
- console.log(balances);
77
+ console.debug(balances);
80
78
  paths = [];
81
- for (i = 0; i < nCoins; i++) {
79
+ for (i = 0; i < nCoins; i += 1) {
82
80
  newPath = p.clone();
83
81
  // newPath.balances[coins[i]] = newPath.balances[coins[i]].add(balances[i]);
84
82
  // newPath.calls.push({
@@ -24,7 +24,7 @@ export declare class Path {
24
24
  totalGasLimit: number;
25
25
  });
26
26
  popBalance(token: SupportedToken): BigNumber;
27
- private comparedByPriority;
27
+ private static comparedByPriority;
28
28
  static findBestPath(creditAccount: CreditAccountData, creditManager: CreditManagerData, provider: ethers.providers.Provider): Promise<void>;
29
29
  withdrawTokens(): Promise<Array<Path>>;
30
30
  clone(): Path;
@@ -76,15 +76,15 @@ var Path = /** @class */ (function () {
76
76
  this.balances[token] = ethers_1.BigNumber.from(1);
77
77
  return currentBalance.sub(1);
78
78
  };
79
- Path.prototype.comparedByPriority = function (_a, _b) {
80
- var tokenA = _a[0], _balanceA = _a[1];
81
- var tokenB = _b[0], _balanceB = _b[1];
79
+ Path.comparedByPriority = function (_a, _b) {
80
+ var tokenA = _a[0];
81
+ var tokenB = _b[0];
82
82
  var priorityTokenA = priority_1.priority[token_1.supportedTokens[tokenA].type];
83
83
  var priorityTokenB = priority_1.priority[token_1.supportedTokens[tokenB].type];
84
84
  if (priorityTokenA > priorityTokenB) {
85
85
  return -1;
86
86
  }
87
- else if (priorityTokenA < priorityTokenB) {
87
+ if (priorityTokenA < priorityTokenB) {
88
88
  return 1;
89
89
  }
90
90
  return 0;
@@ -123,8 +123,8 @@ var Path = /** @class */ (function () {
123
123
  case 2:
124
124
  lpPaths = _a.sent();
125
125
  pathFinder = types_1.PathFinder__factory.connect(contracts_1.pathFindersByNetwork[networkType].PATH_FINDER, provider);
126
- console.log(lpPaths);
127
- console.log(pathFinder);
126
+ console.debug(lpPaths);
127
+ console.debug(pathFinder);
128
128
  return [2 /*return*/];
129
129
  }
130
130
  });
@@ -134,43 +134,39 @@ var Path = /** @class */ (function () {
134
134
  return __awaiter(this, void 0, void 0, function () {
135
135
  var existingTokens, nextToken, lpPathFinder;
136
136
  return __generator(this, function (_a) {
137
- switch (_a.label) {
138
- case 0:
139
- existingTokens = Object.entries(this.balances)
140
- .filter(function (_a) {
141
- var _token = _a[0], balance = _a[1];
142
- return balance.gt(1);
143
- })
144
- .sort(this.comparedByPriority);
145
- // TODO: Add checks for lenght
146
- if (existingTokens.length === 0)
147
- throw new Error("No tokens with balance >1");
148
- nextToken = existingTokens[0][0];
149
- // Get balances and keep non-zero only
150
- // Find token with highest priority
151
- // Get token type of this token
152
- switch (token_1.supportedTokens[nextToken].type) {
153
- case tokenType_1.TokenType.NORMAL_TOKEN:
154
- case tokenType_1.TokenType.CONNECTOR:
155
- return [2 /*return*/, [this]];
156
- case tokenType_1.TokenType.YEARN_VAULT_OF_CURVE_LP:
157
- case tokenType_1.TokenType.YEARN_VAULT_OF_META_CURVE_LP:
158
- case tokenType_1.TokenType.YEARN_VAULT:
159
- lpPathFinder = new yVault_1.YearnVaultPathFinder(nextToken);
160
- break;
161
- case tokenType_1.TokenType.CONVEX_LP_TOKEN:
162
- lpPathFinder = new convexLP_1.ConvexLPPathFinder();
163
- break;
164
- case tokenType_1.TokenType.META_CURVE_LP:
165
- case tokenType_1.TokenType.CURVE_LP:
166
- lpPathFinder = new curveLP_1.CurvePathFinder(nextToken);
167
- break;
168
- default:
169
- throw new Error("Token type not supported yet");
170
- }
171
- return [4 /*yield*/, lpPathFinder.findWithdrawPaths(this)];
172
- case 1: return [2 /*return*/, _a.sent()];
137
+ existingTokens = Object.entries(this.balances)
138
+ .filter(function (_a) {
139
+ var balance = _a[1];
140
+ return balance.gt(1);
141
+ })
142
+ .sort(Path.comparedByPriority);
143
+ // TODO: Add checks for lenght
144
+ if (existingTokens.length === 0)
145
+ throw new Error("No tokens with balance >1");
146
+ nextToken = existingTokens[0][0];
147
+ // Get balances and keep non-zero only
148
+ // Find token with highest priority
149
+ // Get token type of this token
150
+ switch (token_1.supportedTokens[nextToken].type) {
151
+ case tokenType_1.TokenType.NORMAL_TOKEN:
152
+ case tokenType_1.TokenType.CONNECTOR:
153
+ return [2 /*return*/, [this]];
154
+ case tokenType_1.TokenType.YEARN_VAULT_OF_CURVE_LP:
155
+ case tokenType_1.TokenType.YEARN_VAULT_OF_META_CURVE_LP:
156
+ case tokenType_1.TokenType.YEARN_VAULT:
157
+ lpPathFinder = new yVault_1.YearnVaultPathFinder(nextToken);
158
+ break;
159
+ case tokenType_1.TokenType.CONVEX_LP_TOKEN:
160
+ lpPathFinder = new convexLP_1.ConvexLPPathFinder();
161
+ break;
162
+ case tokenType_1.TokenType.META_CURVE_LP:
163
+ case tokenType_1.TokenType.CURVE_LP:
164
+ lpPathFinder = new curveLP_1.CurvePathFinder(nextToken);
165
+ break;
166
+ default:
167
+ throw new Error("Token type not supported yet");
173
168
  }
169
+ return [2 /*return*/, lpPathFinder.findWithdrawPaths(this)];
174
170
  });
175
171
  });
176
172
  };
@@ -1,5 +1,4 @@
1
- import { BigNumber } from "ethers";
2
- import { BytesLike } from "@ethersproject/bytes";
1
+ import { BigNumber, BytesLike } from "ethers";
3
2
  import { SwapType } from "./tradeTypes";
4
3
  export interface CloseTradePath {
5
4
  path: Array<string>;
@@ -1,8 +1,8 @@
1
- import { ConvexPoolContract, CurvePoolContract, UniswapV2Contract, YearnVaultContract } from "../contracts/contracts";
2
- import { NormalToken } from "../tokens/normal";
3
- import { CurveLPToken } from "../tokens/curveLP";
4
- import { YearnLPToken } from "../tokens/yearn";
5
- import { ConvexLPToken, ConvexStakedPhantomToken } from "../tokens/convex";
1
+ import type { ConvexPoolContract, CurvePoolContract, UniswapV2Contract, YearnVaultContract } from "../contracts/contracts";
2
+ import type { NormalToken } from "../tokens/normal";
3
+ import type { CurveLPToken } from "../tokens/curveLP";
4
+ import type { YearnLPToken } from "../tokens/yearn";
5
+ import type { ConvexLPToken, ConvexStakedPhantomToken } from "../tokens/convex";
6
6
  export declare enum TradeType {
7
7
  UniswapV2Swap = 0,
8
8
  UniswapV3Swap = 1,
@@ -1,10 +1,10 @@
1
- import { Path, LPWithdrawPathFinder } from "./path";
2
1
  import { YearnLPToken } from "../tokens/yearn";
3
2
  import { NormalToken } from "../tokens/normal";
4
3
  import { CurveLPToken } from "../tokens/curveLP";
4
+ import type { Path, LPWithdrawPathFinder } from "./path";
5
5
  export declare class YearnVaultPathFinder implements LPWithdrawPathFinder {
6
6
  _vault: YearnLPToken;
7
7
  token: NormalToken | CurveLPToken;
8
8
  constructor(vault: YearnLPToken);
9
- findWithdrawPaths(p: Path): Promise<Array<Path>>;
9
+ findWithdrawPaths(path: Path): Promise<Array<Path>>;
10
10
  }
@@ -38,10 +38,11 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
39
  exports.YearnVaultPathFinder = void 0;
40
40
  var ethers_1 = require("ethers");
41
- var types_1 = require("../types");
42
41
  var yearn_1 = require("../tokens/yearn");
43
42
  var token_1 = require("../tokens/token");
44
43
  var multicall_1 = require("../utils/multicall");
44
+ var mappers_1 = require("../utils/mappers");
45
+ var types_1 = require("../types");
45
46
  var YearnVaultPathFinder = /** @class */ (function () {
46
47
  function YearnVaultPathFinder(vault) {
47
48
  this._vault = vault;
@@ -49,23 +50,26 @@ var YearnVaultPathFinder = /** @class */ (function () {
49
50
  // Yearn Vault only has one lp action
50
51
  this.token = currentTokenData.underlying;
51
52
  }
52
- YearnVaultPathFinder.prototype.findWithdrawPaths = function (p) {
53
+ // eslint-disable-next-line class-methods-use-this
54
+ YearnVaultPathFinder.prototype.findWithdrawPaths = function (path) {
53
55
  return __awaiter(this, void 0, void 0, function () {
54
- var vaultBalances, _i, _a, _b, yVault, tokenData, currentBalance, vaultList, multicallData, prices, i, vault, vb, tokenAddress, adapterAddress, callData;
55
- return __generator(this, function (_c) {
56
- switch (_c.label) {
56
+ var p, vaultBalances, vaultList, multicallData, prices, i, vault, vb, tokenAddress, adapterAddress, callData;
57
+ return __generator(this, function (_a) {
58
+ switch (_a.label) {
57
59
  case 0:
58
- vaultBalances = {};
59
- for (_i = 0, _a = Object.entries(yearn_1.yearnTokens); _i < _a.length; _i++) {
60
- _b = _a[_i], yVault = _b[0], tokenData = _b[1];
61
- currentBalance = p.popBalance(yVault);
60
+ p = Object.assign(Object.create(Object.getPrototypeOf(path)), path);
61
+ vaultBalances = (0, mappers_1.objectEntries)(yearn_1.yearnTokens).reduce(function (acc, _a) {
62
+ var yVault = _a[0], tokenData = _a[1];
63
+ var typedVault = yVault;
64
+ var currentBalance = p.popBalance(typedVault);
62
65
  if (currentBalance.gt(1)) {
63
- vaultBalances[yVault] = {
66
+ acc[typedVault] = {
64
67
  token: tokenData.underlying,
65
68
  balance: currentBalance
66
69
  };
67
70
  }
68
- }
71
+ return acc;
72
+ }, {});
69
73
  vaultList = Object.keys(vaultBalances);
70
74
  multicallData = vaultList
71
75
  .map(function (t) { return token_1.tokenDataByNetwork[p.networkType][t]; })
@@ -76,8 +80,8 @@ var YearnVaultPathFinder = /** @class */ (function () {
76
80
  }); });
77
81
  return [4 /*yield*/, (0, multicall_1.multicall)(multicallData, p.provider)];
78
82
  case 1:
79
- prices = _c.sent();
80
- for (i = 0; i < vaultList.length; i++) {
83
+ prices = _a.sent();
84
+ for (i = 0; i < vaultList.length; i += 1) {
81
85
  vault = vaultList[i];
82
86
  vb = vaultBalances[vault];
83
87
  p.balances[vb.token] = (p.balances[vb.token] || ethers_1.BigNumber.from(0)).add(ethers_1.BigNumber.from((vb === null || vb === void 0 ? void 0 : vb.balance) || 0).mul(prices[i]));
@@ -86,8 +90,7 @@ var YearnVaultPathFinder = /** @class */ (function () {
86
90
  callData = types_1.IYVault__factory.createInterface().encodeFunctionData("withdraw()");
87
91
  p.calls.push({ target: adapterAddress, callData: callData });
88
92
  }
89
- return [4 /*yield*/, p.withdrawTokens()];
90
- case 2: return [2 /*return*/, _c.sent()];
93
+ return [2 /*return*/, p.withdrawTokens()];
91
94
  }
92
95
  });
93
96
  });