@morpho-org/blue-sdk 3.0.0-next.10 → 3.0.0-next.12

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.
@@ -1,158 +1,102 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PreLiquidationPosition = void 0;
3
+ exports.PreLiquidationPosition = exports.PreLiquidationParams = void 0;
4
4
  const constants_1 = require("../constants");
5
5
  const market_1 = require("../market");
6
6
  const math_1 = require("../math");
7
7
  const Position_1 = require("./Position");
8
+ class PreLiquidationParams {
9
+ preLltv;
10
+ preLCF1;
11
+ preLCF2;
12
+ preLIF1;
13
+ preLIF2;
14
+ preLiquidationOracle;
15
+ constructor({ preLltv, preLCF1, preLCF2, preLIF1, preLIF2, preLiquidationOracle, }) {
16
+ this.preLltv = BigInt(preLltv);
17
+ this.preLCF1 = BigInt(preLCF1);
18
+ this.preLCF2 = BigInt(preLCF2);
19
+ this.preLIF1 = BigInt(preLIF1);
20
+ this.preLIF2 = BigInt(preLIF2);
21
+ this.preLiquidationOracle = preLiquidationOracle;
22
+ }
23
+ getCloseFactor(quotient) {
24
+ return (this.preLCF1 + math_1.MathLib.wMulDown(quotient, this.preLCF2 - this.preLCF1));
25
+ }
26
+ getIncentiveFactor(quotient) {
27
+ return (this.preLIF1 + math_1.MathLib.wMulDown(quotient, this.preLIF2 - this.preLIF1));
28
+ }
29
+ }
30
+ exports.PreLiquidationParams = PreLiquidationParams;
8
31
  class PreLiquidationPosition extends Position_1.AccrualPosition {
9
- /**
10
- * The pre-liquidation parameters of the associated PreLiquidation contract.
11
- */
12
32
  preLiquidationParams;
13
- /**
14
- * The address of the PreLiquidation contract this position is associated to.
15
- */
16
33
  preLiquidation;
17
- /**
18
- * Whether the PreLiquidation contract is authorized to manage this position.
19
- */
20
- isPreLiquidationAuthorized;
21
- constructor(position, market) {
22
- super(position, market);
23
- this.preLiquidationParams = position.preLiquidationParams;
24
- this.preLiquidation = position.preLiquidation;
25
- this.isPreLiquidationAuthorized = position.isPreLiquidationAuthorized;
34
+ preLiquidationOraclePrice;
35
+ _baseMarket;
36
+ constructor({ preLiquidationParams, preLiquidation, preLiquidationOraclePrice, ...position }, market) {
37
+ super(position, {
38
+ ...market,
39
+ params: {
40
+ ...market.params,
41
+ lltv: BigInt(preLiquidationParams.preLltv),
42
+ },
43
+ price: preLiquidationOraclePrice != null
44
+ ? BigInt(preLiquidationOraclePrice)
45
+ : undefined,
46
+ });
47
+ this.preLiquidationParams = new PreLiquidationParams(preLiquidationParams);
48
+ this.preLiquidation = preLiquidation;
49
+ if (preLiquidationOraclePrice != null)
50
+ this.preLiquidationOraclePrice = BigInt(preLiquidationOraclePrice);
51
+ this._baseMarket = new market_1.Market(market);
52
+ }
53
+ get market() {
54
+ return this._baseMarket;
55
+ }
56
+ get _lltv() {
57
+ return this._baseMarket.params.lltv;
26
58
  }
27
59
  /**
28
- * Whether this position is liquidatable via the PreLiquidation contract.
29
- * `undefined` iff the market's oracle is undefined or reverts.
60
+ * @inheritdoc `undefined` if the pre-liquidation's oracle reverts.
61
+ * `undefined` if it may be liquidatable on Morpho.
30
62
  */
31
- get isPreLiquidatable() {
63
+ get isHealthy() {
32
64
  const { collateralValue } = this;
33
65
  if (collateralValue == null)
34
66
  return;
35
67
  const { borrowAssets } = this;
36
- return (this.isPreLiquidationAuthorized &&
37
- borrowAssets <=
38
- math_1.MathLib.wMulDown(collateralValue, this.market.params.lltv) &&
39
- borrowAssets >
40
- math_1.MathLib.wMulDown(collateralValue, this.preLiquidationParams.preLltv));
41
- }
42
- /**
43
- * Whether this position is healthy.
44
- * `undefined` iff the market's oracle is undefined or reverts.
45
- */
46
- get isHealthy() {
47
- const { isPreLiquidatable } = this;
48
- if (isPreLiquidatable == null)
49
- return isPreLiquidatable;
50
- const { isLiquidatable } = this;
51
- if (isLiquidatable == null)
52
- return isLiquidatable;
53
- return !isPreLiquidatable && !isLiquidatable;
54
- }
55
- /**
56
- * The price of the collateral quoted in loan assets that would allow this position to be pre-liquidated.
57
- * `null` if the position has no borrow.
58
- */
59
- get preLiquidationPrice() {
60
- if (this.borrowShares === 0n || this.market.totalBorrowShares === 0n)
61
- return null;
62
- const collateralPower = market_1.MarketUtils.getCollateralPower(this.collateral, {
63
- lltv: this.preLiquidationParams.preLltv,
64
- });
65
- if (collateralPower === 0n)
66
- return math_1.MathLib.MAX_UINT_256;
67
- const { borrowAssets } = this;
68
- return math_1.MathLib.mulDivUp(borrowAssets, constants_1.ORACLE_PRICE_SCALE, collateralPower);
69
- }
70
- /**
71
- * The price variation required for the position to reach its pre-liquidation threshold (scaled by WAD).
72
- * Negative when healthy (the price needs to drop x%), positive when unhealthy (the price needs to soar x%).
73
- * `undefined` iff the market's oracle is undefined or reverts.
74
- * `null` if the position is not a borrow.
75
- */
76
- get priceVariationToLiquidationPrice() {
77
- if (this.market.price == null)
68
+ if (borrowAssets > math_1.MathLib.wMulDown(collateralValue, this._lltv))
78
69
  return;
79
- const { preLiquidationPrice } = this;
80
- if (this.market.price === 0n || preLiquidationPrice == null)
81
- return null;
82
- return math_1.MathLib.wDivUp(preLiquidationPrice, this.market.price) - math_1.MathLib.WAD;
70
+ return (borrowAssets <=
71
+ math_1.MathLib.wMulDown(collateralValue, this.preLiquidationParams.preLltv));
83
72
  }
84
73
  /**
85
- * The maximum amount of loan assets that can be borrowed against this position's collateral.
86
- * `undefined` iff the market's oracle is undefined or reverts.
74
+ * @inheritdoc `undefined` if the pre-liquidation's oracle reverts.
87
75
  */
88
- get maxBorrowAssets() {
76
+ get isLiquidatable() {
89
77
  const { collateralValue } = this;
90
78
  if (collateralValue == null)
91
79
  return;
92
- return math_1.MathLib.wMulDown(collateralValue, this.preLiquidationParams.preLltv);
93
- }
94
- /**
95
- * The maximum amount of collateral that can be withdrawn.
96
- * `undefined` iff the market's oracle is undefined or reverts.
97
- */
98
- get withdrawableCollateral() {
99
- return market_1.MarketUtils.getWithdrawableCollateral(this, this.market, {
100
- lltv: this.preLiquidationParams.preLltv,
101
- });
80
+ const { borrowAssets } = this;
81
+ return (borrowAssets >
82
+ math_1.MathLib.wMulDown(collateralValue, this.preLiquidationParams.preLltv) &&
83
+ borrowAssets <= math_1.MathLib.wMulDown(collateralValue, this._lltv));
102
84
  }
103
85
  /**
104
- * The maximum amount of collateral that can be seized in exchange for the outstanding debt in the context of pre-liquidation.
105
- * `undefined` iff the market's oracle is undefined or reverts.
86
+ * @inheritdoc `undefined` if the pre-liquidation's oracle reverts.
106
87
  */
107
- get preSeizableCollateral() {
108
- if (this.market.price == null)
88
+ get seizableCollateral() {
89
+ if (this._market.price == null)
109
90
  return;
91
+ if (!this.isLiquidatable)
92
+ return 0n;
110
93
  const { ltv } = this;
111
94
  if (ltv == null)
112
- return ltv;
113
- if (!this.isPreLiquidatable)
114
- return 0n;
115
- const quotient = math_1.MathLib.wDivDown(ltv - this.preLiquidationParams.preLltv, this.market.params.lltv - this.preLiquidationParams.preLltv);
116
- const preLIF = this.preLiquidationParams.preLIF1 +
117
- math_1.MathLib.wMulDown(quotient, this.preLiquidationParams.preLIF2 - this.preLiquidationParams.preLIF1);
118
- const preLCF = this.preLiquidationParams.preLCF1 +
119
- math_1.MathLib.wMulDown(quotient, this.preLiquidationParams.preLCF2 - this.preLiquidationParams.preLCF1);
120
- const repayableShares = math_1.MathLib.wMulDown(this.borrowShares, preLCF);
121
- const repayableAssets = math_1.MathLib.wMulDown(math_1.SharesMath.toAssets(repayableShares, this.market.totalBorrowAssets, this.market.totalBorrowShares, "Down"), preLIF);
122
- return math_1.MathLib.mulDivDown(repayableAssets, constants_1.ORACLE_PRICE_SCALE, this.market.price);
123
- }
124
- /**
125
- * This position's pre health factor (collateral power over debt, scaled by WAD).
126
- * If the debt is 0, health factor is `MaxUint256`.
127
- * `undefined` iff the market's oracle is undefined or reverts.
128
- */
129
- get preHealthFactor() {
130
- return market_1.MarketUtils.getHealthFactor(this, this.market, {
131
- lltv: this.preLiquidationParams.preLltv,
132
- });
133
- }
134
- /**
135
- * The percentage of this position's borrow power currently used (scaled by WAD).
136
- * If the collateral price is 0, usage is `MaxUint256`.
137
- */
138
- get borrowCapacityUsage() {
139
- return market_1.MarketUtils.getBorrowCapacityUsage(this, this.market, {
140
- lltv: this.preLiquidationParams.preLltv,
141
- });
142
- }
143
- getBorrowCapacityLimit(options = {}) {
144
- options.maxLtv =
145
- options.maxLtv != null
146
- ? math_1.MathLib.min(options.maxLtv, this.preLiquidationParams.preLltv)
147
- : this.preLiquidationParams.preLltv;
148
- return this.market.getBorrowCapacityLimit(this, options);
149
- }
150
- getWithdrawCollateralCapacityLimit(options = {}) {
151
- options.maxLtv =
152
- options.maxLtv != null
153
- ? math_1.MathLib.min(options.maxLtv, this.preLiquidationParams.preLltv)
154
- : this.preLiquidationParams.preLltv;
155
- return this.market.getWithdrawCollateralCapacityLimit(this, options);
95
+ return;
96
+ const quotient = math_1.MathLib.wDivDown(ltv - this.preLiquidationParams.preLltv, this._lltv - this.preLiquidationParams.preLltv);
97
+ const repayableShares = math_1.MathLib.wMulDown(this.borrowShares, this.preLiquidationParams.getCloseFactor(quotient));
98
+ const repayableAssets = math_1.MathLib.wMulDown(math_1.SharesMath.toAssets(repayableShares, this._market.totalBorrowAssets, this._market.totalBorrowShares, "Down"), this.preLiquidationParams.getIncentiveFactor(quotient));
99
+ return math_1.MathLib.mulDivDown(repayableAssets, constants_1.ORACLE_PRICE_SCALE, this._market.price);
156
100
  }
157
101
  }
158
102
  exports.PreLiquidationPosition = PreLiquidationPosition;
@@ -0,0 +1,15 @@
1
+ import type { BigIntish } from "./types";
2
+ export declare const defaultPreLiquidationParamsRegistry: Map<bigint, {
3
+ preLltv: bigint;
4
+ preLCF1: bigint;
5
+ preLCF2: bigint;
6
+ preLIF1: bigint;
7
+ preLIF2: bigint;
8
+ }>;
9
+ export declare const getDefaultPreLiquidationParams: (lltv: BigIntish) => {
10
+ preLltv: bigint;
11
+ preLCF1: bigint;
12
+ preLCF2: bigint;
13
+ preLIF1: bigint;
14
+ preLIF2: bigint;
15
+ };
@@ -0,0 +1,85 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getDefaultPreLiquidationParams = exports.defaultPreLiquidationParamsRegistry = void 0;
4
+ const viem_1 = require("viem");
5
+ const errors_1 = require("./errors");
6
+ exports.defaultPreLiquidationParamsRegistry = new Map([
7
+ [
8
+ (0, viem_1.parseEther)("0.385"),
9
+ {
10
+ preLltv: 301514568055515563n,
11
+ preLCF1: 22637943984157107n,
12
+ preLCF2: 349673199983645648n,
13
+ preLIF1: (0, viem_1.parseEther)("1.15"),
14
+ preLIF2: (0, viem_1.parseEther)("1.15"),
15
+ },
16
+ ],
17
+ [
18
+ (0, viem_1.parseEther)("0.625"),
19
+ {
20
+ preLltv: 562591950487445723n,
21
+ preLCF1: 7543182567291709n,
22
+ preLCF2: 279542312587328718n,
23
+ preLIF1: 1126760563380281690n,
24
+ preLIF2: 1126760563380281690n,
25
+ },
26
+ ],
27
+ [
28
+ (0, viem_1.parseEther)("0.77"),
29
+ {
30
+ preLltv: 727366070175296029n,
31
+ preLCF1: 3706417131700377n,
32
+ preLCF2: 256643181309902852n,
33
+ preLIF1: 1074113856068743286n,
34
+ preLIF2: 1074113856068743286n,
35
+ },
36
+ ],
37
+ [
38
+ (0, viem_1.parseEther)("0.86"),
39
+ {
40
+ preLltv: 832603694978499652n,
41
+ preLCF1: 2001493508968667n,
42
+ preLCF2: 245311807032632372n,
43
+ preLIF1: 1043841336116910229n,
44
+ preLIF2: 1043841336116910229n,
45
+ },
46
+ ],
47
+ [
48
+ (0, viem_1.parseEther)("0.915"),
49
+ {
50
+ preLltv: 897868776651447149n,
51
+ preLCF1: 1135586186384195n,
52
+ preLCF2: 239205538157954887n,
53
+ preLIF1: 1026167265264238070n,
54
+ preLIF2: 1026167265264238070n,
55
+ },
56
+ ],
57
+ [
58
+ (0, viem_1.parseEther)("0.965"),
59
+ {
60
+ preLltv: 957768981497388846n,
61
+ preLCF1: 441038514876104n,
62
+ preLCF2: 234108264807531861n,
63
+ preLIF1: 1010611419909044972n,
64
+ preLIF2: 1010611419909044972n,
65
+ },
66
+ ],
67
+ [
68
+ (0, viem_1.parseEther)("0.985"),
69
+ {
70
+ preLltv: 975838577830248552n,
71
+ preLCF1: 247773050273784n,
72
+ preLCF2: 232655340599010079n,
73
+ preLIF1: 1006036217303822937n,
74
+ preLIF2: 1006036217303822937n,
75
+ },
76
+ ],
77
+ ]);
78
+ const getDefaultPreLiquidationParams = (lltv) => {
79
+ lltv = BigInt(lltv);
80
+ const defaultParams = exports.defaultPreLiquidationParamsRegistry.get(lltv);
81
+ if (defaultParams == null)
82
+ throw new errors_1.UnsupportedPreLiquidationParamsError(lltv);
83
+ return defaultParams;
84
+ };
85
+ exports.getDefaultPreLiquidationParams = getDefaultPreLiquidationParams;