@gearbox-protocol/sdk 1.5.7 → 1.5.10

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 (53) hide show
  1. package/lib/contracts/contracts.d.ts +1 -0
  2. package/lib/contracts/contracts.js +5 -1
  3. package/lib/core/adapter.d.ts +4 -9
  4. package/lib/core/adapter.js +2 -19
  5. package/lib/core/creditManager.d.ts +1 -1
  6. package/lib/core/creditManager.js +1 -1
  7. package/lib/core/trade.d.ts +20 -22
  8. package/lib/core/trade.js +108 -36
  9. package/lib/index.d.ts +0 -1
  10. package/lib/index.js +0 -1
  11. package/lib/parsers/txParser.d.ts +9 -1
  12. package/lib/parsers/txParser.js +11 -3
  13. package/lib/pathfinder/core.d.ts +22 -0
  14. package/lib/pathfinder/core.js +9 -0
  15. package/lib/pathfinder/pathOptions.d.ts +11 -0
  16. package/lib/pathfinder/pathOptions.js +74 -0
  17. package/lib/pathfinder/pathOptions.spec.d.ts +1 -0
  18. package/lib/pathfinder/pathOptions.spec.js +90 -0
  19. package/lib/pathfinder/pathfinder.d.ts +24 -0
  20. package/lib/pathfinder/pathfinder.js +177 -0
  21. package/lib/pathfinder/pathfinder.spec.d.ts +1 -0
  22. package/lib/pathfinder/pathfinder.spec.js +21 -0
  23. package/lib/types/contracts/interfaces/IAddressProvider.sol/IAddressProvider.d.ts +9 -1
  24. package/lib/types/contracts/interfaces/adapters/lido/ILidoV1Adapter.sol/ILidoV1Adapter.d.ts +17 -1
  25. package/lib/types/contracts/pathfinder/interfaces/IClosePathResolver.d.ts +4 -1
  26. package/lib/types/contracts/pathfinder/interfaces/IDepositor.sol/IDepositor.d.ts +6 -3
  27. package/lib/types/contracts/pathfinder/interfaces/IDepositor.sol/IDepositorOptions.d.ts +7 -4
  28. package/lib/types/contracts/pathfinder/interfaces/ILPWorker.sol/ILPWorker.d.ts +4 -1
  29. package/lib/types/contracts/pathfinder/interfaces/ILPWorker.sol/ILPWorkerOptions.d.ts +4 -1
  30. package/lib/types/contracts/pathfinder/interfaces/IPathFinder.d.ts +20 -7
  31. package/lib/types/contracts/pathfinder/interfaces/IPathResolver.d.ts +5 -2
  32. package/lib/types/contracts/pathfinder/interfaces/ISwapAggregator.d.ts +8 -5
  33. package/lib/types/contracts/pathfinder/interfaces/IWithdrawer.sol/IWithdrawer.d.ts +7 -4
  34. package/lib/types/contracts/pathfinder/interfaces/IWithdrawer.sol/IWithdrawerOptions.d.ts +7 -4
  35. package/lib/types/factories/contracts/interfaces/IAddressProvider.sol/IAddressProvider__factory.js +13 -0
  36. package/lib/types/factories/contracts/interfaces/adapters/lido/ILidoV1Adapter.sol/ILidoV1Adapter__factory.js +26 -0
  37. package/lib/types/factories/contracts/pathfinder/interfaces/IClosePathResolver__factory.js +10 -0
  38. package/lib/types/factories/contracts/pathfinder/interfaces/IDepositor.sol/IDepositorOptions__factory.js +40 -0
  39. package/lib/types/factories/contracts/pathfinder/interfaces/IDepositor.sol/IDepositor__factory.js +30 -0
  40. package/lib/types/factories/contracts/pathfinder/interfaces/ILPWorker.sol/ILPWorkerOptions__factory.js +10 -0
  41. package/lib/types/factories/contracts/pathfinder/interfaces/ILPWorker.sol/ILPWorker__factory.js +10 -0
  42. package/lib/types/factories/contracts/pathfinder/interfaces/IPathFinder__factory.d.ts +7 -17
  43. package/lib/types/factories/contracts/pathfinder/interfaces/IPathFinder__factory.js +10 -0
  44. package/lib/types/factories/contracts/pathfinder/interfaces/IPathResolver__factory.js +20 -0
  45. package/lib/types/factories/contracts/pathfinder/interfaces/ISwapAggregator__factory.js +50 -0
  46. package/lib/types/factories/contracts/pathfinder/interfaces/IWithdrawer.sol/IWithdrawerOptions__factory.js +40 -0
  47. package/lib/types/factories/contracts/pathfinder/interfaces/IWithdrawer.sol/IWithdrawer__factory.js +40 -0
  48. package/lib/types/factories/contracts/support/MultiCall.sol/Multicall2__factory.d.ts +1 -1
  49. package/lib/types/factories/contracts/support/MultiCall.sol/Multicall2__factory.js +1 -1
  50. package/lib/utils/multicall.d.ts +2 -2
  51. package/package.json +3 -1
  52. package/lib/tokens/connectors.d.ts +0 -2
  53. package/lib/tokens/connectors.js +0 -9
@@ -71,4 +71,5 @@ export declare type UniversalParams = {
71
71
  export declare type ContractParams = UniswapV2Params | UniswapV3Params | CurveParams | CurveSteCRVPoolParams | YearnParams | ConvexParams | ConvexPoolParams | LidoParams | UniversalParams;
72
72
  export declare const contractParams: Record<SupportedContract, ContractParams>;
73
73
  export declare const contractsByAddress: Record<string, SupportedContract>;
74
+ export declare const isSupportedContract: (t: unknown) => t is SupportedContract;
74
75
  export {};
@@ -11,7 +11,7 @@ var __assign = (this && this.__assign) || function () {
11
11
  return __assign.apply(this, arguments);
12
12
  };
13
13
  Object.defineProperty(exports, "__esModule", { value: true });
14
- exports.contractsByAddress = exports.contractParams = exports.UNISWAP_V3_QUOTER = exports.contractsByNetwork = void 0;
14
+ exports.isSupportedContract = exports.contractsByAddress = exports.contractParams = exports.UNISWAP_V3_QUOTER = exports.contractsByNetwork = void 0;
15
15
  var token_1 = require("../tokens/token");
16
16
  var mappers_1 = require("../utils/mappers");
17
17
  var adapters_1 = require("./adapters");
@@ -299,3 +299,7 @@ exports.contractsByAddress = (0, mappers_1.objectEntries)(exports.contractsByNet
299
299
  var contracts = _a[1];
300
300
  return (__assign(__assign({}, acc), (0, mappers_1.filterEmptyKeys)((0, mappers_1.keyToLowercase)((0, mappers_1.swapKeyValue)(contracts)))));
301
301
  }, {});
302
+ var isSupportedContract = function (t) {
303
+ return typeof t === "string" && !!exports.contractParams[t];
304
+ };
305
+ exports.isSupportedContract = isSupportedContract;
@@ -1,34 +1,29 @@
1
1
  import { Signer } from "ethers";
2
2
  import { AdapterInterface } from "../contracts/adapters";
3
3
  import { SupportedContract } from "../contracts/contracts";
4
- import { PathFinderResultStructOutput } from "../types/contracts/pathfinder/interfaces/IPathFinder";
5
4
  import { EVMTx } from "./eventOrTx";
6
- export declare enum AdapterType {
7
- Swap = 1,
8
- LP = 2
9
- }
5
+ import { TradePath } from "./trade";
10
6
  interface BaseAdapterProps {
11
7
  name: string;
12
- type: AdapterType;
13
8
  adapterInterface: AdapterInterface;
14
9
  contractAddress: string;
15
10
  adapterAddress: string;
16
11
  contractSymbol: SupportedContract;
12
+ creditManager: string;
17
13
  }
18
14
  interface ExecuteProps {
19
- tradePath: PathFinderResultStructOutput;
15
+ tradePath: TradePath;
20
16
  slippage: number;
21
17
  signer: Signer;
22
18
  }
23
19
  export declare class BaseAdapter {
24
20
  readonly name: string;
25
- readonly type: AdapterType;
26
21
  readonly adapterInterface: AdapterInterface;
27
22
  readonly contractAddress: string;
28
23
  readonly adapterAddress: string;
29
24
  readonly contractSymbol: SupportedContract;
25
+ readonly creditManager: string;
30
26
  constructor(props: BaseAdapterProps);
31
27
  execute(props: ExecuteProps): Promise<EVMTx>;
32
- static connect(tradePath: PathFinderResultStructOutput): BaseAdapter;
33
28
  }
34
29
  export {};
@@ -36,21 +36,15 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
36
36
  }
37
37
  };
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
- exports.BaseAdapter = exports.AdapterType = void 0;
40
- var adapters_1 = require("../contracts/adapters");
41
- var AdapterType;
42
- (function (AdapterType) {
43
- AdapterType[AdapterType["Swap"] = 1] = "Swap";
44
- AdapterType[AdapterType["LP"] = 2] = "LP";
45
- })(AdapterType = exports.AdapterType || (exports.AdapterType = {}));
39
+ exports.BaseAdapter = void 0;
46
40
  var BaseAdapter = /** @class */ (function () {
47
41
  function BaseAdapter(props) {
48
42
  this.name = props.name;
49
- this.type = props.type;
50
43
  this.adapterInterface = props.adapterInterface;
51
44
  this.adapterAddress = props.adapterAddress;
52
45
  this.contractAddress = props.contractAddress;
53
46
  this.contractSymbol = props.contractSymbol;
47
+ this.creditManager = props.creditManager;
54
48
  }
55
49
  BaseAdapter.prototype.execute = function (props) {
56
50
  return __awaiter(this, void 0, void 0, function () {
@@ -60,17 +54,6 @@ var BaseAdapter = /** @class */ (function () {
60
54
  });
61
55
  });
62
56
  };
63
- BaseAdapter.connect = function (tradePath) {
64
- console.debug(tradePath);
65
- return new BaseAdapter({
66
- name: "",
67
- type: AdapterType.Swap,
68
- adapterInterface: adapters_1.AdapterInterface.NO_SWAP,
69
- adapterAddress: "",
70
- contractAddress: "",
71
- contractSymbol: "CONVEX_3CRV_POOL",
72
- });
73
- };
74
57
  return BaseAdapter;
75
58
  }());
76
59
  exports.BaseAdapter = BaseAdapter;
@@ -15,7 +15,7 @@ export declare class CreditManagerData {
15
15
  readonly maxAmount: BigNumber;
16
16
  readonly maxLeverageFactor: number;
17
17
  readonly availableLiquidity: BigNumber;
18
- readonly allowedTokens: Array<string>;
18
+ readonly collateralTokens: Array<string>;
19
19
  readonly adapters: Record<string, string>;
20
20
  readonly liquidationThresholds: Record<string, BigNumber>;
21
21
  readonly version: number;
@@ -52,7 +52,7 @@ var CreditManagerData = /** @class */ (function () {
52
52
  this.maxAmount = ethers_1.BigNumber.from(payload.maxAmount || 0);
53
53
  this.maxLeverageFactor = ethers_1.BigNumber.from(payload.maxLeverageFactor || 0).toNumber();
54
54
  this.availableLiquidity = ethers_1.BigNumber.from(payload.availableLiquidity || 0);
55
- this.allowedTokens = (payload.collateralTokens || []).map(function (t) {
55
+ this.collateralTokens = (payload.collateralTokens || []).map(function (t) {
56
56
  return t.toLowerCase();
57
57
  });
58
58
  this.adapters = (payload.adapters || []).reduce(function (acc, _a) {
@@ -1,49 +1,47 @@
1
- import { BigNumber, Signer } from "ethers";
1
+ import { BigNumber } from "ethers";
2
2
  import { AdapterInterface } from "../contracts/adapters";
3
3
  import { SwapType } from "../pathfinder/tradeTypes";
4
- import { TokenData } from "../tokens/tokenData";
5
- import { PathFinderResultStructOutput } from "../types/contracts/pathfinder/interfaces/IPathFinder";
4
+ import { ICreditFacade } from "../types";
5
+ import { PathFinderResultStruct, PathFinderResultStructOutput } from "../types/contracts/pathfinder/interfaces/IPathFinder";
6
6
  import { BaseAdapter } from "./adapter";
7
7
  import { EVMTx } from "./eventOrTx";
8
+ import { TXSwap } from "./transactions";
9
+ export declare type TradePath = Pick<PathFinderResultStructOutput, keyof PathFinderResultStruct>;
8
10
  export interface BaseTradeInterface {
9
11
  swapType: SwapType;
12
+ sourceAmount: BigNumber;
10
13
  expectedAmount: BigNumber;
11
14
  rate: BigNumber;
12
15
  tokenFrom: string;
13
16
  tokenTo: string;
14
- operationName: string;
17
+ operationName: TradeOperations;
15
18
  }
16
- export interface TradeProps {
19
+ export interface TradeProps extends BaseTradeInterface {
17
20
  adapter: BaseAdapter;
18
- tradePath: PathFinderResultStructOutput;
19
- swapType: SwapType;
20
- expectedAmount: BigNumber;
21
- rate: BigNumber;
22
- tokenFrom: string;
23
- tokenTo: string;
24
- operationName: string;
25
- }
26
- export interface ConnectTradeProps {
27
- adapter: BaseAdapter;
28
- tradePath: PathFinderResultStructOutput;
21
+ tradePath: TradePath;
22
+ creditFacade: ICreditFacade;
29
23
  }
24
+ export declare type TradeOperations = "farmWithdraw" | "farmDeposit" | "swap" | "unknownOperation";
30
25
  export declare class Trade implements BaseTradeInterface {
31
26
  protected helper: BaseAdapter;
32
- protected tradePath: PathFinderResultStructOutput;
27
+ protected tradePath: TradePath;
28
+ protected creditFacade: ICreditFacade;
33
29
  readonly swapType: SwapType;
30
+ readonly sourceAmount: BigNumber;
34
31
  readonly expectedAmount: BigNumber;
35
32
  readonly rate: BigNumber;
36
33
  readonly tokenFrom: string;
37
34
  readonly tokenTo: string;
38
- readonly operationName: string;
39
- constructor({ tradePath, adapter }: TradeProps);
40
- static connect({ tradePath, adapter }: ConnectTradeProps): Trade;
35
+ readonly operationName: TradeOperations;
36
+ constructor(props: TradeProps);
41
37
  getName(): string;
42
38
  getAdapterInterface(): AdapterInterface;
43
39
  getContractAddress(): string;
44
40
  getAdapterAddress(): string;
45
- execute(slippage: number, signer: Signer): Promise<EVMTx>;
46
- toString(tokenData: Record<string, TokenData>): string;
41
+ execute(): Promise<EVMTx>;
42
+ protected _execute(): Promise<TXSwap>;
43
+ toString(): string;
47
44
  getFromAmount(slippage: number): BigNumber;
48
45
  getToAmount(slippage: number): BigNumber;
46
+ static getOperationName(tokenInAddress: string, tokenOutAddress: string): TradeOperations;
49
47
  }
package/lib/core/trade.js CHANGED
@@ -1,36 +1,66 @@
1
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
+ };
2
38
  Object.defineProperty(exports, "__esModule", { value: true });
3
39
  exports.Trade = void 0;
4
- var ethers_1 = require("ethers");
5
- var tradeTypes_1 = require("../pathfinder/tradeTypes");
40
+ var decimals_1 = require("../tokens/decimals");
41
+ var token_1 = require("../tokens/token");
6
42
  var formatter_1 = require("../utils/formatter");
7
- var adapter_1 = require("./adapter");
8
43
  var constants_1 = require("./constants");
44
+ var transactions_1 = require("./transactions");
45
+ var OPERATION_NAMES = {
46
+ farmWithdraw: "Farm withdraw",
47
+ farmDeposit: "Farm deposit",
48
+ swap: "Swap",
49
+ unknownOperation: "Unknown operation",
50
+ };
9
51
  var Trade = /** @class */ (function () {
10
- function Trade(_a) {
11
- var tradePath = _a.tradePath, adapter = _a.adapter;
12
- this.helper = adapter;
13
- this.tradePath = tradePath;
14
- this.swapType = tradeTypes_1.SwapType.ExactInput;
15
- this.expectedAmount = ethers_1.BigNumber.from(0);
16
- this.rate = ethers_1.BigNumber.from(0);
17
- this.tokenFrom = "";
18
- this.tokenTo = "";
19
- this.operationName = "";
52
+ function Trade(props) {
53
+ this.helper = props.adapter;
54
+ this.tradePath = props.tradePath;
55
+ this.creditFacade = props.creditFacade;
56
+ this.swapType = props.swapType;
57
+ this.sourceAmount = props.sourceAmount;
58
+ this.expectedAmount = props.expectedAmount;
59
+ this.rate = props.rate;
60
+ this.tokenFrom = props.tokenFrom;
61
+ this.tokenTo = props.tokenTo;
62
+ this.operationName = props.operationName;
20
63
  }
21
- Trade.connect = function (_a) {
22
- var tradePath = _a.tradePath, adapter = _a.adapter;
23
- return new Trade({
24
- tradePath: tradePath,
25
- adapter: adapter,
26
- swapType: tradeTypes_1.SwapType.ExactInput,
27
- expectedAmount: ethers_1.BigNumber.from(0),
28
- rate: ethers_1.BigNumber.from(0),
29
- tokenFrom: "",
30
- tokenTo: "",
31
- operationName: "",
32
- });
33
- };
34
64
  Trade.prototype.getName = function () {
35
65
  return this.helper.name;
36
66
  };
@@ -43,17 +73,42 @@ var Trade = /** @class */ (function () {
43
73
  Trade.prototype.getAdapterAddress = function () {
44
74
  return this.helper.adapterAddress;
45
75
  };
46
- Trade.prototype.execute = function (slippage, signer) {
47
- return this.helper.execute({ tradePath: this.tradePath, slippage: slippage, signer: signer });
76
+ Trade.prototype.execute = function () {
77
+ return this._execute();
48
78
  };
49
- Trade.prototype.toString = function (tokenData) {
50
- var result = this.helper.type === adapter_1.AdapterType.Swap
51
- ? "Swap "
52
- : "".concat(this.operationName || "Farm", " ");
53
- var fromToken = tokenData[this.tokenFrom];
54
- var toToken = tokenData[this.tokenTo];
55
- result += "".concat((0, formatter_1.formatBN)(this.tradePath.amount, fromToken.decimals), " ").concat(fromToken.symbol, " \u21D2 ").concat((0, formatter_1.formatBN)(this.expectedAmount, toToken.decimals), " ").concat(toToken.symbol, " on ").concat(this.helper.name);
56
- return result;
79
+ Trade.prototype._execute = function () {
80
+ return __awaiter(this, void 0, void 0, function () {
81
+ var receipt;
82
+ return __generator(this, function (_a) {
83
+ switch (_a.label) {
84
+ case 0: return [4 /*yield*/, this.creditFacade.multicall(this.tradePath.calls)];
85
+ case 1:
86
+ receipt = _a.sent();
87
+ return [2 /*return*/, new transactions_1.TXSwap({
88
+ txHash: receipt.hash,
89
+ protocol: this.helper.contractAddress,
90
+ operation: OPERATION_NAMES[this.operationName],
91
+ amountFrom: this.sourceAmount,
92
+ amountTo: this.expectedAmount,
93
+ tokenFrom: this.tokenFrom,
94
+ tokenTo: this.tokenTo,
95
+ creditManager: this.helper.creditManager,
96
+ timestamp: 0,
97
+ })];
98
+ }
99
+ });
100
+ });
101
+ };
102
+ Trade.prototype.toString = function () {
103
+ var symbolFrom = token_1.tokenSymbolByAddress[this.tokenFrom.toLowerCase()];
104
+ var symbolTo = token_1.tokenSymbolByAddress[this.tokenTo.toLowerCase()];
105
+ if (!symbolFrom)
106
+ throw new Error("Unknown token: ".concat(this.tokenFrom));
107
+ if (!symbolTo)
108
+ throw new Error("Unknown token: ".concat(this.tokenTo));
109
+ var decimalsFrom = decimals_1.decimals[symbolFrom];
110
+ var decimalsTo = decimals_1.decimals[symbolTo];
111
+ return "".concat(this.operationName, " ").concat((0, formatter_1.formatBN)(this.sourceAmount, decimalsFrom), " ").concat(symbolFrom, " \u21D2 ").concat((0, formatter_1.formatBN)(this.expectedAmount, decimalsTo), " ").concat(symbolTo, " on ").concat(this.helper.name);
57
112
  };
58
113
  Trade.prototype.getFromAmount = function (slippage) {
59
114
  return this.expectedAmount
@@ -65,6 +120,23 @@ var Trade = /** @class */ (function () {
65
120
  .mul(constants_1.PERCENTAGE_FACTOR + slippage)
66
121
  .div(constants_1.PERCENTAGE_FACTOR);
67
122
  };
123
+ Trade.getOperationName = function (tokenInAddress, tokenOutAddress) {
124
+ var tokenInSymbol = token_1.tokenSymbolByAddress[tokenInAddress];
125
+ var tokenOutSymbol = token_1.tokenSymbolByAddress[tokenOutAddress];
126
+ var tokenInIsLp = (0, token_1.isLPToken)(tokenInSymbol);
127
+ var tokenOutIsLp = (0, token_1.isLPToken)(tokenOutSymbol);
128
+ if (!tokenInSymbol)
129
+ throw new Error("Unknown token: ".concat(tokenInAddress));
130
+ if (!tokenOutSymbol)
131
+ throw new Error("Unknown token: ".concat(tokenOutAddress));
132
+ if (tokenInIsLp && !tokenOutIsLp)
133
+ return "farmWithdraw";
134
+ if (!tokenInIsLp && tokenOutIsLp)
135
+ return "farmDeposit";
136
+ if (!tokenInIsLp && !tokenOutIsLp)
137
+ return "swap";
138
+ return "unknownOperation";
139
+ };
68
140
  return Trade;
69
141
  }());
70
142
  exports.Trade = Trade;
package/lib/index.d.ts CHANGED
@@ -42,7 +42,6 @@ export * from "./strategies/lido";
42
42
  export * from "./strategies/uniswapV2";
43
43
  export * from "./strategies/uniswapV3";
44
44
  export * from "./strategies/yearn";
45
- export * from "./tokens/connectors";
46
45
  export * from "./tokens/convex";
47
46
  export * from "./tokens/curveLP";
48
47
  export * from "./tokens/decimals";
package/lib/index.js CHANGED
@@ -62,7 +62,6 @@ __exportStar(require("./strategies/lido"), exports);
62
62
  __exportStar(require("./strategies/uniswapV2"), exports);
63
63
  __exportStar(require("./strategies/uniswapV3"), exports);
64
64
  __exportStar(require("./strategies/yearn"), exports);
65
- __exportStar(require("./tokens/connectors"), exports);
66
65
  __exportStar(require("./tokens/convex"), exports);
67
66
  __exportStar(require("./tokens/curveLP"), exports);
68
67
  __exportStar(require("./tokens/decimals"), exports);
@@ -2,17 +2,25 @@ import { SupportedContract } from "../contracts/contracts";
2
2
  import { NetworkType } from "../core/constants";
3
3
  import { SupportedToken } from "../tokens/token";
4
4
  import { MultiCallStruct } from "../types/contracts/interfaces/ICreditFacade.sol/ICreditFacade";
5
+ import { AbstractParser } from "./abstractParser";
5
6
  import { IParser } from "./iParser";
6
7
  export interface AdapterForParser {
7
8
  adapter: string;
8
9
  contract: string;
9
10
  }
11
+ interface ParseData {
12
+ contract: string;
13
+ adapterName: string;
14
+ }
10
15
  export declare class TxParser {
11
- protected static parsers: Record<string, IParser>;
16
+ protected static parsers: Record<string, IParser & AbstractParser>;
12
17
  static parse(address: string, calldata: string): string;
18
+ static getParseData(address: string): ParseData;
13
19
  static parseMultiCall(calls: Array<MultiCallStruct>): Array<string>;
14
20
  static addAdapters(adapters: Array<AdapterForParser>): void;
15
21
  static addContracts(network: NetworkType): void;
16
22
  static addCreditFacade(creditFacade: string, underlying: SupportedToken): void;
23
+ static getParser(address: string): IParser & AbstractParser;
17
24
  protected static addParser(address: string, contract: SupportedContract, adapterType: number, isContract: boolean): void;
18
25
  }
26
+ export {};
@@ -15,11 +15,13 @@ var TxParser = /** @class */ (function () {
15
15
  function TxParser() {
16
16
  }
17
17
  TxParser.parse = function (address, calldata) {
18
- var parser = this.parsers[address.toLowerCase()];
19
- if (!parser)
20
- throw new Error("Can find parser for ".concat(address));
18
+ var parser = this.getParser(address);
21
19
  return parser.parse(calldata);
22
20
  };
21
+ TxParser.getParseData = function (address) {
22
+ var parser = this.getParser(address);
23
+ return { contract: parser.contract, adapterName: parser.adapterName };
24
+ };
23
25
  TxParser.parseMultiCall = function (calls) {
24
26
  return calls.map(function (call) {
25
27
  return TxParser.parse(call.target, call.callData.toString());
@@ -42,6 +44,12 @@ var TxParser = /** @class */ (function () {
42
44
  TxParser.addCreditFacade = function (creditFacade, underlying) {
43
45
  this.parsers[creditFacade.toLowerCase()] = new creditFacadeParser_1.CreditFacadeParser(underlying);
44
46
  };
47
+ TxParser.getParser = function (address) {
48
+ var parser = this.parsers[address.toLowerCase()];
49
+ if (!parser)
50
+ throw new Error("Can find parser for ".concat(address));
51
+ return parser;
52
+ };
45
53
  TxParser.addParser = function (address, contract, adapterType, isContract) {
46
54
  var addressLC = address.toLowerCase();
47
55
  switch (adapters_1.AdapterInterface[adapterType]) {
@@ -0,0 +1,22 @@
1
+ import { BigNumber } from "ethers";
2
+ export declare enum SwapOperation {
3
+ EXACT_INPUT = 0,
4
+ EXACT_INPUT_ALL = 1,
5
+ EXACT_OUTPUT = 2
6
+ }
7
+ export interface MultiCall {
8
+ target: string;
9
+ callData: string;
10
+ }
11
+ export interface PathFinderResult {
12
+ amount: BigNumber;
13
+ calls: Array<MultiCall>;
14
+ }
15
+ export interface PathFinderOpenStrategyResult {
16
+ balances: Record<string, BigNumber>;
17
+ calls: Array<MultiCall>;
18
+ }
19
+ export interface PathFinderCloseResult {
20
+ underlyingBalance: BigNumber;
21
+ calls: Array<MultiCall>;
22
+ }
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SwapOperation = void 0;
4
+ var SwapOperation;
5
+ (function (SwapOperation) {
6
+ SwapOperation[SwapOperation["EXACT_INPUT"] = 0] = "EXACT_INPUT";
7
+ SwapOperation[SwapOperation["EXACT_INPUT_ALL"] = 1] = "EXACT_INPUT_ALL";
8
+ SwapOperation[SwapOperation["EXACT_OUTPUT"] = 2] = "EXACT_OUTPUT";
9
+ })(SwapOperation = exports.SwapOperation || (exports.SwapOperation = {}));
@@ -0,0 +1,11 @@
1
+ import { BigNumberish } from "ethers";
2
+ export interface PathOption {
3
+ target: string;
4
+ option: number;
5
+ totalOptions: number;
6
+ }
7
+ export declare type PathOptionSerie = Array<PathOption>;
8
+ export declare class PathOptionFactory {
9
+ static generatePathOptions(balances: Record<string, BigNumberish>, loopsInTx: number): Array<PathOptionSerie>;
10
+ static next(path: PathOptionSerie): PathOptionSerie;
11
+ }
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
14
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
15
+ if (ar || !(i in from)) {
16
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
17
+ ar[i] = from[i];
18
+ }
19
+ }
20
+ return to.concat(ar || Array.prototype.slice.call(from));
21
+ };
22
+ Object.defineProperty(exports, "__esModule", { value: true });
23
+ exports.PathOptionFactory = void 0;
24
+ var ethers_1 = require("ethers");
25
+ var contracts_1 = require("../contracts/contracts");
26
+ var curveLP_1 = require("../tokens/curveLP");
27
+ var token_1 = require("../tokens/token");
28
+ var PathOptionFactory = /** @class */ (function () {
29
+ function PathOptionFactory() {
30
+ }
31
+ PathOptionFactory.generatePathOptions = function (balances, loopsInTx) {
32
+ var curveSymbols = Object.keys(curveLP_1.curveTokens);
33
+ var initPO = Object.entries(balances)
34
+ .filter(function (_a) {
35
+ var token = _a[0], balance = _a[1];
36
+ return ethers_1.BigNumber.from(balance).gt(1) &&
37
+ curveSymbols.includes(token_1.tokenSymbolByAddress[token.toLowerCase()]);
38
+ })
39
+ .map(function (_a) {
40
+ var target = _a[0];
41
+ return {
42
+ target: target,
43
+ option: 0,
44
+ totalOptions: contracts_1.contractParams[curveLP_1.curveTokens[token_1.tokenSymbolByAddress[target.toLowerCase()]].pool].tokens.length,
45
+ };
46
+ });
47
+ var totalLoops = initPO.reduce(function (acc, item) { return acc * item.totalOptions; }, 1);
48
+ var result = [];
49
+ var currentPo = __spreadArray([], initPO, true);
50
+ for (var i = 0; i < totalLoops; i++) {
51
+ if (i % loopsInTx === 0) {
52
+ result.push(currentPo);
53
+ }
54
+ if (i < totalLoops - 1) {
55
+ currentPo = PathOptionFactory.next(currentPo);
56
+ }
57
+ }
58
+ return result;
59
+ };
60
+ PathOptionFactory.next = function (path) {
61
+ var newPath = __spreadArray([], path, true);
62
+ for (var i = path.length - 1; i >= 0; i--) {
63
+ var po = __assign({}, newPath[i]);
64
+ po.option++;
65
+ newPath[i] = po;
66
+ if (po.option < po.totalOptions)
67
+ return newPath;
68
+ po.option = 0;
69
+ }
70
+ throw new Error("Path options overflow");
71
+ };
72
+ return PathOptionFactory;
73
+ }());
74
+ exports.PathOptionFactory = PathOptionFactory;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,90 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var chai_1 = require("chai");
4
+ var token_1 = require("../tokens/token");
5
+ var pathOptions_1 = require("./pathOptions");
6
+ describe("PathOptionFactory test", function () {
7
+ it("next works correctly", function () {
8
+ var serie = [
9
+ {
10
+ target: token_1.tokenDataByNetwork.Mainnet["3Crv"],
11
+ option: 0,
12
+ totalOptions: 3,
13
+ },
14
+ {
15
+ target: token_1.tokenDataByNetwork.Mainnet.FRAX3CRV,
16
+ option: 0,
17
+ totalOptions: 2,
18
+ },
19
+ {
20
+ target: token_1.tokenDataByNetwork.Mainnet.LUSD3CRV,
21
+ option: 0,
22
+ totalOptions: 2,
23
+ },
24
+ ];
25
+ var expectedNext = JSON.parse(JSON.stringify(serie));
26
+ expectedNext[2].option = 1;
27
+ var next = pathOptions_1.PathOptionFactory.next(serie);
28
+ (0, chai_1.expect)(next).to.be.eql(expectedNext);
29
+ expectedNext[1].option = 1;
30
+ expectedNext[2].option = 0;
31
+ next = pathOptions_1.PathOptionFactory.next(next);
32
+ (0, chai_1.expect)(next).to.be.eql(expectedNext);
33
+ expectedNext[1].option = 1;
34
+ expectedNext[2].option = 1;
35
+ next = pathOptions_1.PathOptionFactory.next(next);
36
+ (0, chai_1.expect)(next).to.be.eql(expectedNext);
37
+ expectedNext[0].option = 1;
38
+ expectedNext[1].option = 0;
39
+ expectedNext[2].option = 0;
40
+ next = pathOptions_1.PathOptionFactory.next(next);
41
+ (0, chai_1.expect)(next).to.be.eql(expectedNext);
42
+ expectedNext[0].option = 1;
43
+ expectedNext[1].option = 0;
44
+ expectedNext[2].option = 1;
45
+ next = pathOptions_1.PathOptionFactory.next(next);
46
+ (0, chai_1.expect)(next).to.be.eql(expectedNext);
47
+ expectedNext[0].option = 1;
48
+ expectedNext[1].option = 1;
49
+ expectedNext[2].option = 0;
50
+ next = pathOptions_1.PathOptionFactory.next(next);
51
+ (0, chai_1.expect)(next).to.be.eql(expectedNext);
52
+ expectedNext[0].option = 1;
53
+ expectedNext[1].option = 1;
54
+ expectedNext[2].option = 1;
55
+ next = pathOptions_1.PathOptionFactory.next(next);
56
+ (0, chai_1.expect)(next).to.be.eql(expectedNext);
57
+ });
58
+ it("generatePathOptions works correctly", function () {
59
+ var _a;
60
+ var balances = (_a = {},
61
+ _a[token_1.tokenDataByNetwork.Mainnet["1INCH"]] = 100,
62
+ _a[token_1.tokenDataByNetwork.Mainnet["3Crv"]] = 200,
63
+ _a[token_1.tokenDataByNetwork.Mainnet.USDC] = 200,
64
+ _a[token_1.tokenDataByNetwork.Mainnet.FRAX3CRV] = 200,
65
+ _a[token_1.tokenDataByNetwork.Mainnet.LUSD3CRV] = 200,
66
+ _a);
67
+ var result = pathOptions_1.PathOptionFactory.generatePathOptions(balances, 4);
68
+ var expected = [
69
+ {
70
+ target: token_1.tokenDataByNetwork.Mainnet["3Crv"],
71
+ option: 0,
72
+ totalOptions: 3,
73
+ },
74
+ {
75
+ target: token_1.tokenDataByNetwork.Mainnet.FRAX3CRV,
76
+ option: 0,
77
+ totalOptions: 2,
78
+ },
79
+ {
80
+ target: token_1.tokenDataByNetwork.Mainnet.LUSD3CRV,
81
+ option: 0,
82
+ totalOptions: 2,
83
+ },
84
+ ];
85
+ for (var i = 0; i < 3; i++) {
86
+ expected[0].option = i;
87
+ (0, chai_1.expect)(expected).to.be.eql(result[i]);
88
+ }
89
+ });
90
+ });