@gearbox-protocol/sdk 1.15.18 → 1.15.20
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/strategy.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { BigNumber } from "ethers";
|
|
2
|
-
import { Asset } from "./assets";
|
|
3
2
|
import { CreditManagerData } from "./creditManager";
|
|
4
3
|
export interface StrategyPayload {
|
|
5
4
|
apy?: number;
|
|
@@ -15,7 +14,6 @@ interface PoolStats {
|
|
|
15
14
|
}
|
|
16
15
|
declare type PoolList = Record<string, PoolStats>;
|
|
17
16
|
interface LiquidationPriceProps {
|
|
18
|
-
assets: Array<Asset>;
|
|
19
17
|
prices: Record<string, BigNumber>;
|
|
20
18
|
liquidationThresholds: Record<string, BigNumber>;
|
|
21
19
|
borrowed: BigNumber;
|
|
@@ -35,7 +33,7 @@ export declare class Strategy {
|
|
|
35
33
|
static maxLeverage(lpToken: string, cms: Array<PartialCM>): number;
|
|
36
34
|
maxAPY(maxLeverage: number, poolApy: PoolList): number;
|
|
37
35
|
overallAPY(apy: number, leverage: number, depositCollateral: string, borrowAPY: number): number;
|
|
38
|
-
liquidationPrice({
|
|
36
|
+
liquidationPrice({ prices, liquidationThresholds, borrowed, underlyingToken, lpAmount, lpToken, }: LiquidationPriceProps): BigNumber;
|
|
39
37
|
protected farmLev(leverage: number, depositCollateral: string): number;
|
|
40
38
|
protected inBaseAssets(depositCollateral: string): boolean;
|
|
41
39
|
protected inLeveragableAssets(depositCollateral: string): boolean;
|
package/lib/core/strategy.js
CHANGED
|
@@ -36,18 +36,7 @@ var Strategy = /** @class */ (function () {
|
|
|
36
36
|
};
|
|
37
37
|
// eslint-disable-next-line class-methods-use-this
|
|
38
38
|
Strategy.prototype.liquidationPrice = function (_a) {
|
|
39
|
-
var
|
|
40
|
-
var collateralLTMoney = assets.reduce(function (acc, _a) {
|
|
41
|
-
var tokenAddress = _a.token, amount = _a.balance;
|
|
42
|
-
var tokenAddressLC = tokenAddress.toLowerCase();
|
|
43
|
-
var tokenSymbol = token_1.tokenSymbolByAddress[tokenAddressLC];
|
|
44
|
-
var lt = liquidationThresholds[tokenAddressLC] || ethers_1.BigNumber.from(0);
|
|
45
|
-
var price = prices[tokenAddressLC] || ethers_1.BigNumber.from(0);
|
|
46
|
-
var tokenDecimals = decimals_1.decimals[tokenSymbol];
|
|
47
|
-
var money = (0, price_1.calcTotalPrice)(price, amount, tokenDecimals);
|
|
48
|
-
var ltMoney = money.mul(lt).div(constants_1.PERCENTAGE_FACTOR);
|
|
49
|
-
return acc.add(ltMoney);
|
|
50
|
-
}, ethers_1.BigNumber.from(0));
|
|
39
|
+
var prices = _a.prices, liquidationThresholds = _a.liquidationThresholds, borrowed = _a.borrowed, underlyingToken = _a.underlyingToken, lpAmount = _a.lpAmount, lpToken = _a.lpToken;
|
|
51
40
|
var underlyingTokenAddressLC = underlyingToken.toLowerCase();
|
|
52
41
|
var underlyingTokenSymbol = token_1.tokenSymbolByAddress[underlyingTokenAddressLC];
|
|
53
42
|
var underlyingTokenDecimals = decimals_1.decimals[underlyingTokenSymbol];
|
|
@@ -56,13 +45,12 @@ var Strategy = /** @class */ (function () {
|
|
|
56
45
|
var lpTokenAddressLC = lpToken.toLowerCase();
|
|
57
46
|
var lpTokenSymbol = token_1.tokenSymbolByAddress[lpTokenAddressLC];
|
|
58
47
|
var lpTokenDecimals = decimals_1.decimals[lpTokenSymbol];
|
|
48
|
+
var lpLT = liquidationThresholds[lpTokenAddressLC] || ethers_1.BigNumber.from(0);
|
|
59
49
|
var lpPrice = prices[lpTokenAddressLC] || constants_1.PRICE_DECIMALS;
|
|
60
50
|
var lpMoney = (0, price_1.calcTotalPrice)(lpPrice, lpAmount, lpTokenDecimals);
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
.mul(constants_1.WAD)
|
|
65
|
-
.div(lpMoney);
|
|
51
|
+
var lpLTMoney = lpMoney.mul(lpLT).div(constants_1.PERCENTAGE_FACTOR);
|
|
52
|
+
if (lpLTMoney.gt(0)) {
|
|
53
|
+
var lqPrice = borrowedMoney.mul(constants_1.WAD).div(lpLTMoney);
|
|
66
54
|
return lqPrice.gte(0) ? lqPrice : ethers_1.BigNumber.from(0);
|
|
67
55
|
}
|
|
68
56
|
return ethers_1.BigNumber.from(0);
|
|
@@ -51,22 +51,16 @@ describe("Strategy test", function () {
|
|
|
51
51
|
});
|
|
52
52
|
it("liquidationPrice calculation is correct", function () {
|
|
53
53
|
var result = lidoStrategy.liquidationPrice({
|
|
54
|
-
assets: [
|
|
55
|
-
{
|
|
56
|
-
balance: (0, formatter_1.toBN)("30", decimals_1.decimals.WETH),
|
|
57
|
-
token: token_1.tokenDataByNetwork.Mainnet.WETH,
|
|
58
|
-
},
|
|
59
|
-
],
|
|
60
54
|
liquidationThresholds: liquidationThresholds,
|
|
61
55
|
prices: prices,
|
|
62
|
-
borrowed: (0, formatter_1.toBN)("
|
|
56
|
+
borrowed: (0, formatter_1.toBN)("350", decimals_1.decimals.WETH),
|
|
63
57
|
underlyingToken: token_1.tokenDataByNetwork.Mainnet.WETH,
|
|
64
|
-
lpAmount: (0, formatter_1.toBN)("
|
|
58
|
+
lpAmount: (0, formatter_1.toBN)("400", decimals_1.decimals.STETH),
|
|
65
59
|
lpToken: token_1.tokenDataByNetwork.Mainnet.STETH,
|
|
66
60
|
});
|
|
67
|
-
(0, chai_1.expect)(Number((0, formatter_1.toSignificant)(result, constants_1.WAD_DECIMALS_POW)).toFixed(3)).to.be.eq("0.
|
|
61
|
+
(0, chai_1.expect)(Number((0, formatter_1.toSignificant)(result, constants_1.WAD_DECIMALS_POW)).toFixed(3)).to.be.eq("0.992");
|
|
68
62
|
});
|
|
69
|
-
it("
|
|
63
|
+
it("maxLeverage is correct", function () {
|
|
70
64
|
var result = strategy_1.Strategy.maxLeverage(token_1.tokenDataByNetwork.Mainnet.STETH, [
|
|
71
65
|
{ address: "0x1", liquidationThresholds: liquidationThresholds },
|
|
72
66
|
]);
|
|
@@ -40,6 +40,6 @@ export declare class PathFinder {
|
|
|
40
40
|
* - underlyingBalance - total balance of underlying token
|
|
41
41
|
* - calls - list of calls which should be done to swap & unwrap everything to underlying token
|
|
42
42
|
*/
|
|
43
|
-
findBestClosePath(creditAccount: CreditAccountData, slippage: number): Promise<PathFinderCloseResult>;
|
|
43
|
+
findBestClosePath(creditAccount: CreditAccountData, slippage: number, noConcurency?: boolean): Promise<PathFinderCloseResult>;
|
|
44
44
|
static compare(r1: CloseResult, r2: CloseResult, gasPriceRAY: BigNumberish): CloseResult;
|
|
45
45
|
}
|
|
@@ -164,23 +164,45 @@ var PathFinder = /** @class */ (function () {
|
|
|
164
164
|
* - underlyingBalance - total balance of underlying token
|
|
165
165
|
* - calls - list of calls which should be done to swap & unwrap everything to underlying token
|
|
166
166
|
*/
|
|
167
|
-
PathFinder.prototype.findBestClosePath = function (creditAccount, slippage) {
|
|
167
|
+
PathFinder.prototype.findBestClosePath = function (creditAccount, slippage, noConcurency) {
|
|
168
|
+
if (noConcurency === void 0) { noConcurency = false; }
|
|
168
169
|
return __awaiter(this, void 0, void 0, function () {
|
|
169
|
-
var loopsPerTx, pathOptions,
|
|
170
|
+
var loopsPerTx, pathOptions, results, _i, pathOptions_2, po, _a, _b, requests, bestResult;
|
|
170
171
|
var _this = this;
|
|
171
|
-
return __generator(this, function (
|
|
172
|
-
switch (
|
|
172
|
+
return __generator(this, function (_c) {
|
|
173
|
+
switch (_c.label) {
|
|
173
174
|
case 0:
|
|
174
175
|
loopsPerTx = Math.floor(GAS_PER_BLOCK / MAX_GAS_PER_ROUTE);
|
|
175
176
|
pathOptions = pathOptions_1.PathOptionFactory.generatePathOptions(creditAccount.allBalances, loopsPerTx);
|
|
177
|
+
results = [];
|
|
178
|
+
if (!noConcurency) return [3 /*break*/, 5];
|
|
179
|
+
_i = 0, pathOptions_2 = pathOptions;
|
|
180
|
+
_c.label = 1;
|
|
181
|
+
case 1:
|
|
182
|
+
if (!(_i < pathOptions_2.length)) return [3 /*break*/, 4];
|
|
183
|
+
po = pathOptions_2[_i];
|
|
184
|
+
_b = (_a = results).push;
|
|
185
|
+
return [4 /*yield*/, this.pathFinder.callStatic.findBestClosePath(creditAccount.addr, this._connectors, slippage, po, loopsPerTx, false, {
|
|
186
|
+
gasLimit: GAS_PER_BLOCK,
|
|
187
|
+
})];
|
|
188
|
+
case 2:
|
|
189
|
+
_b.apply(_a, [_c.sent()]);
|
|
190
|
+
_c.label = 3;
|
|
191
|
+
case 3:
|
|
192
|
+
_i++;
|
|
193
|
+
return [3 /*break*/, 1];
|
|
194
|
+
case 4: return [3 /*break*/, 7];
|
|
195
|
+
case 5:
|
|
176
196
|
requests = pathOptions.map(function (po) {
|
|
177
197
|
return _this.pathFinder.callStatic.findBestClosePath(creditAccount.addr, _this._connectors, slippage, po, loopsPerTx, false, {
|
|
178
198
|
gasLimit: GAS_PER_BLOCK,
|
|
179
199
|
});
|
|
180
200
|
});
|
|
181
201
|
return [4 /*yield*/, Promise.all(requests)];
|
|
182
|
-
case
|
|
183
|
-
results =
|
|
202
|
+
case 6:
|
|
203
|
+
results = _c.sent();
|
|
204
|
+
_c.label = 7;
|
|
205
|
+
case 7:
|
|
184
206
|
bestResult = results.reduce(function (best, _a) {
|
|
185
207
|
var pathFinderResult = _a[0], gasPriceRAY = _a[1];
|
|
186
208
|
return PathFinder.compare(best, pathFinderResult, gasPriceRAY);
|