@gearbox-protocol/sdk 3.0.0-next.65 → 3.0.0-next.67
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 +4 -3
- package/lib/core/strategy.js +13 -12
- package/lib/core/strategy.spec.js +68 -3
- package/package.json +2 -2
package/lib/core/strategy.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Asset } from "./assets";
|
|
1
2
|
import { CreditManagerData } from "./creditManager";
|
|
2
3
|
export interface StrategyPayload {
|
|
3
4
|
apy?: number;
|
|
@@ -13,8 +14,8 @@ interface LiquidationPriceProps {
|
|
|
13
14
|
liquidationThresholds: Record<string, bigint>;
|
|
14
15
|
borrowed: bigint;
|
|
15
16
|
underlyingToken: string;
|
|
16
|
-
|
|
17
|
-
|
|
17
|
+
targetToken: string;
|
|
18
|
+
assets: Record<string, Asset>;
|
|
18
19
|
}
|
|
19
20
|
export declare class Strategy {
|
|
20
21
|
apy: number | undefined;
|
|
@@ -27,7 +28,7 @@ export declare class Strategy {
|
|
|
27
28
|
constructor(payload: StrategyPayload);
|
|
28
29
|
static maxLeverage(lpToken: string, cms: Array<PartialCM>): number;
|
|
29
30
|
maxAPY(baseAPY: number, maxLeverage: number, borrowAPY: number): number;
|
|
30
|
-
static liquidationPrice({ prices, liquidationThresholds, borrowed, underlyingToken,
|
|
31
|
+
static liquidationPrice({ prices, liquidationThresholds, borrowed, underlyingToken, targetToken, assets, }: LiquidationPriceProps): bigint;
|
|
31
32
|
protected static maxLeverageThreshold(lpToken: string, cms: Array<PartialCM>): readonly [bigint, string];
|
|
32
33
|
}
|
|
33
34
|
type PartialCM = Pick<CreditManagerData, "liquidationThresholds" | "address">;
|
package/lib/core/strategy.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Strategy = void 0;
|
|
4
4
|
const sdk_gov_1 = require("@gearbox-protocol/sdk-gov");
|
|
5
|
+
const math_1 = require("../utils/math");
|
|
5
6
|
const price_1 = require("../utils/price");
|
|
6
7
|
class Strategy {
|
|
7
8
|
apy;
|
|
@@ -31,18 +32,18 @@ class Strategy {
|
|
|
31
32
|
((baseAPY - borrowAPY) * (maxLeverage - Number(sdk_gov_1.LEVERAGE_DECIMALS))) /
|
|
32
33
|
Number(sdk_gov_1.LEVERAGE_DECIMALS));
|
|
33
34
|
}
|
|
34
|
-
static liquidationPrice({ prices, liquidationThresholds, borrowed, underlyingToken,
|
|
35
|
-
const
|
|
36
|
-
const
|
|
37
|
-
const
|
|
38
|
-
const underlyingPrice = prices[
|
|
39
|
-
const borrowedMoney = price_1.PriceUtils.calcTotalPrice(underlyingPrice, borrowed,
|
|
40
|
-
const
|
|
41
|
-
const
|
|
42
|
-
const
|
|
43
|
-
const lpLT = liquidationThresholds[
|
|
44
|
-
const lpPrice = prices[
|
|
45
|
-
const lpMoney = price_1.PriceUtils.calcTotalPrice(lpPrice,
|
|
35
|
+
static liquidationPrice({ prices, liquidationThresholds, borrowed, underlyingToken, targetToken, assets, }) {
|
|
36
|
+
const underlyingTokenLC = underlyingToken.toLowerCase();
|
|
37
|
+
const [, underlyingDecimals = 18] = (0, sdk_gov_1.extractTokenData)(underlyingTokenLC);
|
|
38
|
+
const { balance: underlyingBalance = 0n } = assets[underlyingTokenLC] || {};
|
|
39
|
+
const underlyingPrice = prices[underlyingTokenLC] || sdk_gov_1.PRICE_DECIMALS;
|
|
40
|
+
const borrowedMoney = price_1.PriceUtils.calcTotalPrice(underlyingPrice, math_1.BigIntMath.max(0n, borrowed - underlyingBalance), underlyingDecimals);
|
|
41
|
+
const targetTokenLC = targetToken.toLowerCase();
|
|
42
|
+
const [, targetDecimals = 18] = (0, sdk_gov_1.extractTokenData)(targetTokenLC);
|
|
43
|
+
const { balance: targetBalance = 0n } = assets[targetTokenLC] || {};
|
|
44
|
+
const lpLT = liquidationThresholds[targetTokenLC] || 0n;
|
|
45
|
+
const lpPrice = prices[targetTokenLC] || sdk_gov_1.PRICE_DECIMALS;
|
|
46
|
+
const lpMoney = price_1.PriceUtils.calcTotalPrice(lpPrice, targetBalance, targetDecimals);
|
|
46
47
|
const lpLTMoney = (lpMoney * lpLT) / sdk_gov_1.PERCENTAGE_FACTOR;
|
|
47
48
|
if (lpLTMoney > 0) {
|
|
48
49
|
const lqPrice = (borrowedMoney * sdk_gov_1.WAD) / lpLTMoney;
|
|
@@ -42,14 +42,79 @@ describe("Strategy test", () => {
|
|
|
42
42
|
const result = lidoStrategy.maxAPY(53203, 10 * Number(sdk_gov_1.LEVERAGE_DECIMALS), pools["0x1"].borrowRate);
|
|
43
43
|
(0, chai_1.expect)(result).to.be.eq(284143);
|
|
44
44
|
});
|
|
45
|
-
it("liquidationPrice calculation is correct", () => {
|
|
45
|
+
it("liquidationPrice: calculation is correct", () => {
|
|
46
46
|
const result = strategy_1.Strategy.liquidationPrice({
|
|
47
47
|
liquidationThresholds,
|
|
48
48
|
prices,
|
|
49
49
|
borrowed: (0, formatter_1.toBN)("350", sdk_gov_1.decimals.WETH),
|
|
50
50
|
underlyingToken: sdk_gov_1.tokenDataByNetwork.Mainnet.WETH,
|
|
51
|
-
|
|
52
|
-
|
|
51
|
+
targetToken: sdk_gov_1.tokenDataByNetwork.Mainnet.STETH,
|
|
52
|
+
assets: {
|
|
53
|
+
[sdk_gov_1.tokenDataByNetwork.Mainnet.STETH.toLowerCase()]: {
|
|
54
|
+
balance: (0, formatter_1.toBN)("400", sdk_gov_1.decimals.STETH),
|
|
55
|
+
token: sdk_gov_1.tokenDataByNetwork.Mainnet.STETH,
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
(0, chai_1.expect)(Number((0, formatter_1.toSignificant)(result, sdk_gov_1.WAD_DECIMALS_POW)).toFixed(3)).to.be.eq("0.992");
|
|
60
|
+
});
|
|
61
|
+
it("liquidationPrice: zero result when debt is compensated", () => {
|
|
62
|
+
const result = strategy_1.Strategy.liquidationPrice({
|
|
63
|
+
liquidationThresholds,
|
|
64
|
+
prices,
|
|
65
|
+
borrowed: (0, formatter_1.toBN)("350", sdk_gov_1.decimals.WETH),
|
|
66
|
+
underlyingToken: sdk_gov_1.tokenDataByNetwork.Mainnet.WETH,
|
|
67
|
+
targetToken: sdk_gov_1.tokenDataByNetwork.Mainnet.STETH,
|
|
68
|
+
assets: {
|
|
69
|
+
[sdk_gov_1.tokenDataByNetwork.Mainnet.STETH.toLowerCase()]: {
|
|
70
|
+
balance: (0, formatter_1.toBN)("400", sdk_gov_1.decimals.STETH),
|
|
71
|
+
token: sdk_gov_1.tokenDataByNetwork.Mainnet.STETH,
|
|
72
|
+
},
|
|
73
|
+
[sdk_gov_1.tokenDataByNetwork.Mainnet.WETH.toLowerCase()]: {
|
|
74
|
+
balance: (0, formatter_1.toBN)("350", sdk_gov_1.decimals.WETH),
|
|
75
|
+
token: sdk_gov_1.tokenDataByNetwork.Mainnet.WETH,
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
(0, chai_1.expect)(Number((0, formatter_1.toSignificant)(result, sdk_gov_1.WAD_DECIMALS_POW)).toFixed(3)).to.be.eq("0.000");
|
|
80
|
+
});
|
|
81
|
+
it("liquidationPrice: zero result when debt is overcompensated", () => {
|
|
82
|
+
const result = strategy_1.Strategy.liquidationPrice({
|
|
83
|
+
liquidationThresholds,
|
|
84
|
+
prices,
|
|
85
|
+
borrowed: (0, formatter_1.toBN)("350", sdk_gov_1.decimals.WETH),
|
|
86
|
+
underlyingToken: sdk_gov_1.tokenDataByNetwork.Mainnet.WETH,
|
|
87
|
+
targetToken: sdk_gov_1.tokenDataByNetwork.Mainnet.STETH,
|
|
88
|
+
assets: {
|
|
89
|
+
[sdk_gov_1.tokenDataByNetwork.Mainnet.STETH.toLowerCase()]: {
|
|
90
|
+
balance: (0, formatter_1.toBN)("400", sdk_gov_1.decimals.STETH),
|
|
91
|
+
token: sdk_gov_1.tokenDataByNetwork.Mainnet.STETH,
|
|
92
|
+
},
|
|
93
|
+
[sdk_gov_1.tokenDataByNetwork.Mainnet.WETH.toLowerCase()]: {
|
|
94
|
+
balance: (0, formatter_1.toBN)("450", sdk_gov_1.decimals.WETH),
|
|
95
|
+
token: sdk_gov_1.tokenDataByNetwork.Mainnet.WETH,
|
|
96
|
+
},
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
(0, chai_1.expect)(Number((0, formatter_1.toSignificant)(result, sdk_gov_1.WAD_DECIMALS_POW)).toFixed(3)).to.be.eq("0.000");
|
|
100
|
+
});
|
|
101
|
+
it("liquidationPrice: zero result when debt is not compensated", () => {
|
|
102
|
+
const result = strategy_1.Strategy.liquidationPrice({
|
|
103
|
+
liquidationThresholds,
|
|
104
|
+
prices,
|
|
105
|
+
borrowed: (0, formatter_1.toBN)("450", sdk_gov_1.decimals.WETH),
|
|
106
|
+
underlyingToken: sdk_gov_1.tokenDataByNetwork.Mainnet.WETH,
|
|
107
|
+
targetToken: sdk_gov_1.tokenDataByNetwork.Mainnet.STETH,
|
|
108
|
+
assets: {
|
|
109
|
+
[sdk_gov_1.tokenDataByNetwork.Mainnet.STETH.toLowerCase()]: {
|
|
110
|
+
balance: (0, formatter_1.toBN)("400", sdk_gov_1.decimals.STETH),
|
|
111
|
+
token: sdk_gov_1.tokenDataByNetwork.Mainnet.STETH,
|
|
112
|
+
},
|
|
113
|
+
[sdk_gov_1.tokenDataByNetwork.Mainnet.WETH.toLowerCase()]: {
|
|
114
|
+
balance: (0, formatter_1.toBN)("100", sdk_gov_1.decimals.WETH),
|
|
115
|
+
token: sdk_gov_1.tokenDataByNetwork.Mainnet.WETH,
|
|
116
|
+
},
|
|
117
|
+
},
|
|
53
118
|
});
|
|
54
119
|
(0, chai_1.expect)(Number((0, formatter_1.toSignificant)(result, sdk_gov_1.WAD_DECIMALS_POW)).toFixed(3)).to.be.eq("0.992");
|
|
55
120
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gearbox-protocol/sdk",
|
|
3
|
-
"version": "3.0.0-next.
|
|
3
|
+
"version": "3.0.0-next.67",
|
|
4
4
|
"description": "Gearbox SDK",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"types": "./lib/index.d.ts",
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"test": "npx mocha -r ts-node/register -r dotenv/config src/**/*.spec.ts"
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@gearbox-protocol/sdk-gov": "^1.
|
|
33
|
+
"@gearbox-protocol/sdk-gov": "^1.15.0",
|
|
34
34
|
"axios": "^1.2.6",
|
|
35
35
|
"decimal.js-light": "^2.5.1",
|
|
36
36
|
"deep-eql": "^4.1.0",
|