@gearbox-protocol/sdk 0.0.53 → 0.0.56
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/core/contracts.d.ts +29 -16
- package/lib/core/contracts.js +45 -4
- package/lib/core/contractsRegister.js +24 -6
- package/lib/core/creditAccount.js +26 -20
- package/lib/core/events.d.ts +10 -10
- package/lib/core/events.js +22 -22
- package/lib/core/pathfinder.d.ts +35 -5
- package/lib/core/pathfinder.js +547 -67
- package/lib/core/priceFeeds.js +61 -0
- package/lib/core/token.d.ts +47 -15
- package/lib/core/token.js +204 -36
- package/lib/core/tradeTypes.d.ts +2 -0
- package/lib/core/transactions.d.ts +2 -2
- package/lib/core/transactions.js +10 -10
- package/lib/index.d.ts +2 -0
- package/lib/index.js +6 -1
- package/lib/pathfinder/connectorPathAsset.d.ts +7 -0
- package/lib/pathfinder/connectorPathAsset.js +144 -0
- package/lib/pathfinder/convexLPTokenPathAsset.d.ts +5 -0
- package/lib/pathfinder/convexLPTokenPathAsset.js +77 -0
- package/lib/pathfinder/curveLPPathAsset.d.ts +5 -0
- package/lib/pathfinder/curveLPPathAsset.js +72 -0
- package/lib/pathfinder/metaCurveLPPathAsset.d.ts +5 -0
- package/lib/pathfinder/metaCurveLPPathAsset.js +72 -0
- package/lib/pathfinder/normalTokenPathAsset.d.ts +5 -0
- package/lib/pathfinder/normalTokenPathAsset.js +136 -0
- package/lib/pathfinder/path.d.ts +41 -0
- package/lib/pathfinder/path.js +302 -0
- package/lib/pathfinder/pathfinder.d.ts +28 -0
- package/lib/pathfinder/pathfinder.js +519 -0
- package/lib/pathfinder/yearnVaultOfCurveLPPathAsset.d.ts +5 -0
- package/lib/pathfinder/yearnVaultOfCurveLPPathAsset.js +87 -0
- package/lib/pathfinder/yearnVaultOfMetaCurveLPPathAsset.d.ts +5 -0
- package/lib/pathfinder/yearnVaultOfMetaCurveLPPathAsset.js +87 -0
- package/lib/pathfinder/yearnVaultPathAsset.d.ts +5 -0
- package/lib/pathfinder/yearnVaultPathAsset.js +87 -0
- package/lib/utils/mappers.d.ts +6 -0
- package/lib/utils/mappers.js +33 -0
- package/lib/utils/types.d.ts +3 -0
- package/lib/utils/types.js +2 -0
- package/package.json +1 -1
- package/src/core/adapters.ts +13 -15
- package/src/core/contracts.ts +111 -44
- package/src/core/contractsRegister.ts +16 -8
- package/src/core/creditAccount.ts +33 -19
- package/src/core/events.ts +30 -22
- package/src/core/priceFeeds.ts +66 -0
- package/src/core/token.ts +645 -370
- package/src/core/tradeTypes.ts +2 -0
- package/src/core/transactions.ts +10 -10
- package/src/index.ts +2 -0
- package/src/pathfinder/connectorPathAsset.ts +69 -0
- package/src/pathfinder/convexLPTokenPathAsset.ts +33 -0
- package/src/pathfinder/curveLPPathAsset.ts +10 -0
- package/src/pathfinder/metaCurveLPPathAsset.ts +12 -0
- package/src/pathfinder/normalTokenPathAsset.ts +68 -0
- package/src/pathfinder/path.ts +259 -0
- package/src/pathfinder/yearnVaultOfCurveLPPathAsset.ts +22 -0
- package/src/pathfinder/yearnVaultOfMetaCurveLPPathAsset.ts +22 -0
- package/src/pathfinder/yearnVaultPathAsset.ts +27 -0
- package/src/utils/mappers.ts +23 -0
- package/src/utils/types.ts +3 -0
- package/src/core/pathfinder.ts +0 -115
|
@@ -0,0 +1,136 @@
|
|
|
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.NormalTokenPathAsset = void 0;
|
|
55
|
+
var ethers_1 = require("ethers");
|
|
56
|
+
var token_1 = require("../core/token");
|
|
57
|
+
var path_1 = require("./path");
|
|
58
|
+
var connectorPathAsset_1 = require("./connectorPathAsset");
|
|
59
|
+
var NormalTokenPathAsset = /** @class */ (function (_super) {
|
|
60
|
+
__extends(NormalTokenPathAsset, _super);
|
|
61
|
+
function NormalTokenPathAsset() {
|
|
62
|
+
return _super !== null && _super.apply(this, arguments) || this;
|
|
63
|
+
}
|
|
64
|
+
NormalTokenPathAsset.prototype.getBestPath = function (currentToken, p) {
|
|
65
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
66
|
+
var currentBalance, currentTokenAddress, currentTokenData, actionData, nextToken, maxPoolAmountOut, _i, ConnectorTokens_1, connToken, connTokenAddress, _a, _b, swapAction, tmpActionData, connectorPathAsset, poolAmountOut;
|
|
67
|
+
return __generator(this, function (_c) {
|
|
68
|
+
switch (_c.label) {
|
|
69
|
+
case 0:
|
|
70
|
+
currentBalance = p.balances[currentToken].sub(1);
|
|
71
|
+
currentTokenAddress = token_1.tokenDataByNetwork[p.networkType][currentToken];
|
|
72
|
+
currentTokenData = token_1.supportedTokens[currentToken];
|
|
73
|
+
actionData = {
|
|
74
|
+
callData: {
|
|
75
|
+
targetContract: "",
|
|
76
|
+
callData: ""
|
|
77
|
+
},
|
|
78
|
+
amountOut: ethers_1.BigNumber.from(0),
|
|
79
|
+
gasLimit: ethers_1.BigNumber.from(0),
|
|
80
|
+
};
|
|
81
|
+
nextToken = p.pool;
|
|
82
|
+
maxPoolAmountOut = ethers_1.BigNumber.from(0);
|
|
83
|
+
_i = 0, ConnectorTokens_1 = token_1.ConnectorTokens;
|
|
84
|
+
_c.label = 1;
|
|
85
|
+
case 1:
|
|
86
|
+
if (!(_i < ConnectorTokens_1.length)) return [3 /*break*/, 8];
|
|
87
|
+
connToken = ConnectorTokens_1[_i];
|
|
88
|
+
connTokenAddress = token_1.tokenDataByNetwork[p.networkType][connToken];
|
|
89
|
+
_a = 0, _b = currentTokenData.swapActions;
|
|
90
|
+
_c.label = 2;
|
|
91
|
+
case 2:
|
|
92
|
+
if (!(_a < _b.length)) return [3 /*break*/, 7];
|
|
93
|
+
swapAction = _b[_a];
|
|
94
|
+
return [4 /*yield*/, this.getActionData(swapAction, currentTokenAddress, currentToken, currentBalance, connTokenAddress, connToken, p)];
|
|
95
|
+
case 3:
|
|
96
|
+
tmpActionData = _c.sent();
|
|
97
|
+
if (!(connToken != p.pool)) return [3 /*break*/, 5];
|
|
98
|
+
connectorPathAsset = new connectorPathAsset_1.ConnectorPathAsset();
|
|
99
|
+
return [4 /*yield*/, connectorPathAsset.getMaxPoolAmount(connToken, actionData.amountOut, p)];
|
|
100
|
+
case 4:
|
|
101
|
+
poolAmountOut = _c.sent();
|
|
102
|
+
if (poolAmountOut > maxPoolAmountOut) {
|
|
103
|
+
maxPoolAmountOut = poolAmountOut;
|
|
104
|
+
actionData = tmpActionData;
|
|
105
|
+
nextToken = connToken;
|
|
106
|
+
}
|
|
107
|
+
return [3 /*break*/, 6];
|
|
108
|
+
case 5:
|
|
109
|
+
if (actionData.amountOut > maxPoolAmountOut) {
|
|
110
|
+
maxPoolAmountOut = actionData.amountOut;
|
|
111
|
+
actionData = tmpActionData;
|
|
112
|
+
nextToken = p.pool;
|
|
113
|
+
}
|
|
114
|
+
_c.label = 6;
|
|
115
|
+
case 6:
|
|
116
|
+
_a++;
|
|
117
|
+
return [3 /*break*/, 2];
|
|
118
|
+
case 7:
|
|
119
|
+
_i++;
|
|
120
|
+
return [3 /*break*/, 1];
|
|
121
|
+
case 8:
|
|
122
|
+
if (actionData.callData.targetContract != "") {
|
|
123
|
+
p.balances[nextToken].add(actionData.amountOut);
|
|
124
|
+
p.balances[currentToken] = ethers_1.BigNumber.from(1);
|
|
125
|
+
p.totalGasLimit.add(actionData.gasLimit);
|
|
126
|
+
p.calls.push(actionData.callData);
|
|
127
|
+
}
|
|
128
|
+
return [4 /*yield*/, p.getBestPath()];
|
|
129
|
+
case 9: return [2 /*return*/, _c.sent()];
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
};
|
|
134
|
+
return NormalTokenPathAsset;
|
|
135
|
+
}(path_1.PathAsset));
|
|
136
|
+
exports.NormalTokenPathAsset = NormalTokenPathAsset;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { BigNumber, ethers } from "ethers";
|
|
2
|
+
import { CreditManagerData } from "../core/creditManager";
|
|
3
|
+
import { MultiCall } from "../core/multicall";
|
|
4
|
+
import { SupportedToken } from "../core/token";
|
|
5
|
+
import { NetworkType } from "../core/constants";
|
|
6
|
+
import { CreditAccountData } from "../core/creditAccount";
|
|
7
|
+
import { TradeAction } from "../core/tradeTypes";
|
|
8
|
+
export declare class Path {
|
|
9
|
+
readonly calls: Array<MultiCall>;
|
|
10
|
+
readonly balances: Record<SupportedToken, BigNumber>;
|
|
11
|
+
readonly pool: SupportedToken;
|
|
12
|
+
readonly creditManager: CreditManagerData;
|
|
13
|
+
readonly creditAccount: CreditAccountData;
|
|
14
|
+
readonly networkType: NetworkType;
|
|
15
|
+
readonly provider: ethers.providers.Provider;
|
|
16
|
+
readonly totalGasLimit: BigNumber;
|
|
17
|
+
constructor(opts: {
|
|
18
|
+
balances: Record<SupportedToken, BigNumber>;
|
|
19
|
+
pool: SupportedToken;
|
|
20
|
+
creditManager: CreditManagerData;
|
|
21
|
+
creditAccount: CreditAccountData;
|
|
22
|
+
networkType: NetworkType;
|
|
23
|
+
provider: ethers.providers.Provider;
|
|
24
|
+
totalGasLimit: BigNumber;
|
|
25
|
+
});
|
|
26
|
+
private comparedByPriority;
|
|
27
|
+
getBestPath(): Promise<Path>;
|
|
28
|
+
}
|
|
29
|
+
export interface ActionData {
|
|
30
|
+
callData: MultiCall;
|
|
31
|
+
amountOut: BigNumber;
|
|
32
|
+
gasLimit: BigNumber;
|
|
33
|
+
}
|
|
34
|
+
export declare abstract class PathAsset {
|
|
35
|
+
abstract getBestPath(currentToken: SupportedToken, p: Path): Promise<Path>;
|
|
36
|
+
getUniswapV2SwapData(adapterAddress: string, currentTokenAddress: string, currentBalance: BigNumber, nextTokenAddress: string, p: Path): Promise<ActionData>;
|
|
37
|
+
getUniswapV3SwapData(adapterAddress: string, currentTokenAddress: string, currentBalance: BigNumber, nextTokenAddress: string, p: Path): Promise<ActionData>;
|
|
38
|
+
getCurveActionData(adapterAddress: string, currentToken: SupportedToken, currentBalance: BigNumber, nextToken: SupportedToken, p: Path): Promise<ActionData>;
|
|
39
|
+
getActionData(swapAction: TradeAction, currentTokenAddress: string, currentToken: SupportedToken, currentBalance: BigNumber, nextTokenAddress: string, nextToken: SupportedToken, p: Path): Promise<ActionData>;
|
|
40
|
+
getYearnActionData(lpAction: TradeAction, currentBalance: BigNumber, p: Path): Promise<ActionData>;
|
|
41
|
+
}
|
|
@@ -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
|
+
}
|