@gearbox-protocol/sdk 1.15.17 → 1.15.19
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
|
]);
|
|
@@ -52,9 +52,9 @@ var CreditAccountWatcher = /** @class */ (function () {
|
|
|
52
52
|
*/
|
|
53
53
|
CreditAccountWatcher.getOpenAccounts = function (creditManager, provider, toBlock) {
|
|
54
54
|
return __awaiter(this, void 0, void 0, function () {
|
|
55
|
-
var eventsByDate, addToEvents, accounts, creditConfigurator, cc, cfUpgraded, _i, cfUpgraded_1, creditFacade
|
|
56
|
-
return __generator(this, function (
|
|
57
|
-
switch (
|
|
55
|
+
var eventsByDate, addToEvents, accounts, creditConfigurator, cc, cfUpgraded, _loop_1, _i, cfUpgraded_1, creditFacade;
|
|
56
|
+
return __generator(this, function (_a) {
|
|
57
|
+
switch (_a.label) {
|
|
58
58
|
case 0:
|
|
59
59
|
if (creditManager.version !== 2)
|
|
60
60
|
throw new Error("Works for V2 only");
|
|
@@ -71,59 +71,60 @@ var CreditAccountWatcher = /** @class */ (function () {
|
|
|
71
71
|
cc = types_1.ICreditConfigurator__factory.connect(creditConfigurator, provider);
|
|
72
72
|
return [4 /*yield*/, cc.queryFilter(cc.filters.CreditFacadeUpgraded(), undefined, toBlock)];
|
|
73
73
|
case 1:
|
|
74
|
-
cfUpgraded = (
|
|
74
|
+
cfUpgraded = (_a.sent()).map(function (e) { return e.args.newCreditFacade; });
|
|
75
|
+
_loop_1 = function (creditFacade) {
|
|
76
|
+
var cf, topics, logs;
|
|
77
|
+
return __generator(this, function (_b) {
|
|
78
|
+
switch (_b.label) {
|
|
79
|
+
case 0:
|
|
80
|
+
cf = types_1.ICreditFacade__factory.connect(creditFacade, provider);
|
|
81
|
+
topics = {
|
|
82
|
+
OpenCreditAccount: cf.interface.getEventTopic("OpenCreditAccount"),
|
|
83
|
+
CloseCreditAccount: cf.interface.getEventTopic("CloseCreditAccount"),
|
|
84
|
+
LiquidateCreditAccount: cf.interface.getEventTopic("LiquidateCreditAccount"),
|
|
85
|
+
LiquidateExpiredCreditAccount: cf.interface.getEventTopic("LiquidateExpiredCreditAccount"),
|
|
86
|
+
TransferAccount: cf.interface.getEventTopic("TransferAccount"),
|
|
87
|
+
};
|
|
88
|
+
return [4 /*yield*/, cf.queryFilter({
|
|
89
|
+
address: cf.address,
|
|
90
|
+
topics: [Object.values(topics)],
|
|
91
|
+
}, undefined, toBlock)];
|
|
92
|
+
case 1:
|
|
93
|
+
logs = _b.sent();
|
|
94
|
+
logs.forEach(function (log) {
|
|
95
|
+
var e = cf.interface.parseLog(log);
|
|
96
|
+
switch (log.topics[0]) {
|
|
97
|
+
case topics.OpenCreditAccount:
|
|
98
|
+
addToEvents(log, e.args.onBehalfOf, "add");
|
|
99
|
+
break;
|
|
100
|
+
case topics.CloseCreditAccount:
|
|
101
|
+
case topics.LiquidateCreditAccount:
|
|
102
|
+
case topics.LiquidateExpiredCreditAccount:
|
|
103
|
+
addToEvents(log, e.args.borrower, "delete");
|
|
104
|
+
break;
|
|
105
|
+
case topics.TransferAccount:
|
|
106
|
+
addToEvents(log, e.args.oldOwner, "delete");
|
|
107
|
+
addToEvents(log, log.args.newOwner, "add");
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
return [2 /*return*/];
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
};
|
|
75
115
|
_i = 0, cfUpgraded_1 = cfUpgraded;
|
|
76
|
-
|
|
116
|
+
_a.label = 2;
|
|
77
117
|
case 2:
|
|
78
|
-
if (!(_i < cfUpgraded_1.length)) return [3 /*break*/,
|
|
118
|
+
if (!(_i < cfUpgraded_1.length)) return [3 /*break*/, 5];
|
|
79
119
|
creditFacade = cfUpgraded_1[_i];
|
|
80
|
-
|
|
81
|
-
return [4 /*yield*/, cf.queryFilter(cf.filters.OpenCreditAccount(), undefined, toBlock)];
|
|
120
|
+
return [5 /*yield**/, _loop_1(creditFacade)];
|
|
82
121
|
case 3:
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
];
|
|
86
|
-
return [4 /*yield*/, cf.queryFilter(cf.filters.CloseCreditAccount(), undefined, toBlock)];
|
|
122
|
+
_a.sent();
|
|
123
|
+
_a.label = 4;
|
|
87
124
|
case 4:
|
|
88
|
-
_b = _b.concat([
|
|
89
|
-
_c.sent()
|
|
90
|
-
]);
|
|
91
|
-
return [4 /*yield*/, cf.queryFilter(cf.filters.LiquidateCreditAccount(), undefined, toBlock)];
|
|
92
|
-
case 5:
|
|
93
|
-
_b = _b.concat([
|
|
94
|
-
_c.sent()
|
|
95
|
-
]);
|
|
96
|
-
return [4 /*yield*/, cf.queryFilter(cf.filters.LiquidateExpiredCreditAccount(), undefined, toBlock)];
|
|
97
|
-
case 6:
|
|
98
|
-
_b = _b.concat([
|
|
99
|
-
_c.sent()
|
|
100
|
-
]);
|
|
101
|
-
return [4 /*yield*/, cf.queryFilter(cf.filters.TransferAccount(), undefined, toBlock)];
|
|
102
|
-
case 7:
|
|
103
|
-
_a = _b.concat([
|
|
104
|
-
_c.sent()
|
|
105
|
-
]), openEvents = _a[0], closeEvents = _a[1], liquidateEvents = _a[2], liquidateExpiredEvents = _a[3], transferEvents = _a[4];
|
|
106
|
-
openEvents.forEach(function (e) {
|
|
107
|
-
addToEvents(e, e.args.onBehalfOf, "add");
|
|
108
|
-
});
|
|
109
|
-
closeEvents.forEach(function (e) {
|
|
110
|
-
addToEvents(e, e.args.borrower, "delete");
|
|
111
|
-
});
|
|
112
|
-
liquidateEvents.forEach(function (e) {
|
|
113
|
-
addToEvents(e, e.args.borrower, "delete");
|
|
114
|
-
});
|
|
115
|
-
liquidateExpiredEvents.forEach(function (e) {
|
|
116
|
-
addToEvents(e, e.args.borrower, "delete");
|
|
117
|
-
});
|
|
118
|
-
transferEvents.forEach(function (e) {
|
|
119
|
-
addToEvents(e, e.args.oldOwner, "delete");
|
|
120
|
-
addToEvents(e, e.args.newOwner, "add");
|
|
121
|
-
});
|
|
122
|
-
_c.label = 8;
|
|
123
|
-
case 8:
|
|
124
125
|
_i++;
|
|
125
126
|
return [3 /*break*/, 2];
|
|
126
|
-
case
|
|
127
|
+
case 5:
|
|
127
128
|
eventsByDate
|
|
128
129
|
.sort(function (a, b) {
|
|
129
130
|
return a.time > b.time ? 1 : -1;
|