@morpho-org/blue-sdk 3.0.0-next.8 → 3.0.0

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.
@@ -35,38 +35,41 @@ class Position {
35
35
  }
36
36
  exports.Position = Position;
37
37
  class AccrualPosition extends Position {
38
- /**
39
- * The market on which this position is held.
40
- */
41
- market;
38
+ _market;
42
39
  constructor(position, market) {
43
40
  const _market = new index_js_1.Market(market);
44
41
  super({ ...position, marketId: _market.id });
45
- this.market = _market;
42
+ this._market = _market;
43
+ }
44
+ /**
45
+ * The market on which this position is held.
46
+ */
47
+ get market() {
48
+ return this._market;
46
49
  }
47
50
  get supplyAssets() {
48
- return this.market.toSupplyAssets(this.supplyShares);
51
+ return this._market.toSupplyAssets(this.supplyShares);
49
52
  }
50
53
  get borrowAssets() {
51
- return this.market.toBorrowAssets(this.borrowShares);
54
+ return this._market.toBorrowAssets(this.borrowShares);
52
55
  }
53
56
  /**
54
57
  * The value of this position's collateral quoted in loan assets.
55
- * `undefined` iff the market's oracle is undefined or reverts.
58
+ * `undefined` if the market's oracle is undefined or reverts.
56
59
  */
57
60
  get collateralValue() {
58
- return this.market.getCollateralValue(this.collateral);
61
+ return this._market.getCollateralValue(this.collateral);
59
62
  }
60
63
  /**
61
64
  * The maximum amount of loan assets that can be borrowed against this position's collateral.
62
- * `undefined` iff the market's oracle is undefined or reverts.
65
+ * `undefined` if the market's oracle is undefined or reverts.
63
66
  */
64
67
  get maxBorrowAssets() {
65
- return this.market.getMaxBorrowAssets(this.collateral);
68
+ return this._market.getMaxBorrowAssets(this.collateral);
66
69
  }
67
70
  /**
68
71
  * The maximum additional amount of assets that can be borrowed against this position's collateral.
69
- * `undefined` iff the market's oracle is undefined or reverts.
72
+ * `undefined` if the market's oracle is undefined or reverts.
70
73
  */
71
74
  get maxBorrowableAssets() {
72
75
  const { maxBorrowAssets } = this;
@@ -76,86 +79,97 @@ class AccrualPosition extends Position {
76
79
  }
77
80
  /**
78
81
  * The maximum amount of collateral that can be seized in exchange for the outstanding debt.
79
- * `undefined` iff the market's oracle is undefined or reverts.
82
+ * `undefined` if the market's oracle is undefined or reverts.
80
83
  */
81
84
  get seizableCollateral() {
82
- return this.market.getSeizableCollateral(this);
85
+ return this._market.getSeizableCollateral(this);
83
86
  }
84
87
  /**
85
88
  * The maximum amount of collateral that can be withdrawn.
86
- * `undefined` iff the market's oracle is undefined or reverts.
89
+ * `undefined` if the market's oracle is undefined or reverts.
87
90
  */
88
91
  get withdrawableCollateral() {
89
- return this.market.getWithdrawableCollateral(this);
92
+ return this._market.getWithdrawableCollateral(this);
90
93
  }
91
94
  /**
92
95
  * Whether this position is healthy.
93
- * `undefined` iff the market's oracle is undefined or reverts.
96
+ * `undefined` if the market's oracle is undefined or reverts.
94
97
  */
95
98
  get isHealthy() {
96
- return this.market.isHealthy(this);
99
+ return this._market.isHealthy(this);
100
+ }
101
+ /**
102
+ * Whether this position can be liquidated.
103
+ * `undefined` if the market's oracle is undefined or reverts.
104
+ */
105
+ get isLiquidatable() {
106
+ const isHealthy = this._market.isHealthy(this);
107
+ if (isHealthy == null)
108
+ return;
109
+ return !isHealthy;
97
110
  }
98
111
  /**
99
112
  * The price of the collateral quoted in loan assets that would allow this position to be liquidated.
113
+ * `null` if the position has no borrow.
100
114
  */
101
115
  get liquidationPrice() {
102
- return this.market.getLiquidationPrice(this);
116
+ return this._market.getLiquidationPrice(this);
103
117
  }
104
118
  /**
105
119
  * The price variation required for the position to reach its liquidation threshold (scaled by WAD).
106
120
  * Negative when healthy (the price needs to drop x%), positive when unhealthy (the price needs to soar x%).
107
- * `undefined` iff the market's oracle is undefined or reverts.
108
- * Null if the position is not a borrow.
121
+ * `undefined` if the market's oracle is undefined or reverts.
122
+ * `null` if the position is not a borrow.
109
123
  */
110
124
  get priceVariationToLiquidationPrice() {
111
- return this.market.getPriceVariationToLiquidationPrice(this);
125
+ return this._market.getPriceVariationToLiquidationPrice(this);
112
126
  }
113
127
  /**
114
128
  * This position's Loan-To-Value (debt over collateral power, scaled by WAD).
115
129
  * If the collateral price is 0, LTV is `MaxUint256`.
116
- * `undefined` iff the market's oracle is undefined or reverts.
130
+ * `undefined` if the market's oracle is undefined or reverts.
117
131
  */
118
132
  get ltv() {
119
- return this.market.getLtv(this);
133
+ return this._market.getLtv(this);
120
134
  }
121
135
  /**
122
136
  * This position's health factor (collateral power over debt, scaled by WAD).
123
137
  * If the debt is 0, health factor is `MaxUint256`.
124
- * `undefined` iff the market's oracle is undefined or reverts.
138
+ * `undefined` if the market's oracle is undefined or reverts.
125
139
  */
126
140
  get healthFactor() {
127
- return this.market.getHealthFactor(this);
141
+ return this._market.getHealthFactor(this);
128
142
  }
129
143
  /**
130
144
  * The percentage of this position's borrow power currently used (scaled by WAD).
131
145
  * If the collateral price is 0, usage is `MaxUint256`.
132
146
  */
133
147
  get borrowCapacityUsage() {
134
- return this.market.getBorrowCapacityUsage(this);
148
+ return this._market.getBorrowCapacityUsage(this);
135
149
  }
136
150
  /**
137
151
  * Returns the maximum amount of loan assets that can be withdrawn given a certain supply position
138
152
  * and a balance of loan assets, and the reason for the limit.
139
153
  */
140
154
  get withdrawCapacityLimit() {
141
- return this.market.getWithdrawCapacityLimit(this);
155
+ return this._market.getWithdrawCapacityLimit(this);
142
156
  }
143
157
  /**
144
158
  * Returns a new position derived from this position, whose interest has been accrued up to the given timestamp.
145
159
  * @param timestamp The timestamp at which to accrue interest. Must be greater than or equal to the market's `lastUpdate`.
146
160
  */
147
161
  accrueInterest(timestamp) {
148
- return new AccrualPosition(this, this.market.accrueInterest(timestamp));
162
+ return new AccrualPosition(this, this._market.accrueInterest(timestamp));
149
163
  }
150
164
  supply(assets, shares, timestamp) {
151
- let { market } = this;
165
+ let { _market: market } = this;
152
166
  ({ market, assets, shares } = market.supply(assets, shares, timestamp));
153
167
  const position = new AccrualPosition(this, market);
154
168
  position.supplyShares += shares;
155
169
  return { position, assets, shares };
156
170
  }
157
171
  withdraw(assets, shares, timestamp) {
158
- let { market } = this;
172
+ let { _market: market } = this;
159
173
  ({ market, assets, shares } = market.withdraw(assets, shares, timestamp));
160
174
  const position = new AccrualPosition(this, market);
161
175
  position.supplyShares -= shares;
@@ -165,10 +179,10 @@ class AccrualPosition extends Position {
165
179
  }
166
180
  supplyCollateral(assets) {
167
181
  this.collateral += assets;
168
- return new AccrualPosition(this, new index_js_1.Market(this.market));
182
+ return new AccrualPosition(this, new index_js_1.Market(this._market));
169
183
  }
170
184
  withdrawCollateral(assets, timestamp) {
171
- if (this.market.price == null)
185
+ if (this._market.price == null)
172
186
  throw new errors_js_1.BlueErrors.UnknownOraclePrice(this.marketId);
173
187
  const position = this.accrueInterest(timestamp);
174
188
  position.collateral -= assets;
@@ -179,7 +193,7 @@ class AccrualPosition extends Position {
179
193
  return position;
180
194
  }
181
195
  borrow(assets, shares, timestamp) {
182
- let { market } = this;
196
+ let { _market: market } = this;
183
197
  if (market.price == null)
184
198
  throw new errors_js_1.BlueErrors.UnknownOraclePrice(market.id);
185
199
  ({ market, assets, shares } = market.borrow(assets, shares, timestamp));
@@ -190,7 +204,7 @@ class AccrualPosition extends Position {
190
204
  return { position, assets, shares };
191
205
  }
192
206
  repay(assets, shares, timestamp) {
193
- let { market } = this;
207
+ let { _market: market } = this;
194
208
  ({ market, assets, shares } = market.repay(assets, shares, timestamp));
195
209
  const position = new AccrualPosition(this, market);
196
210
  position.borrowShares -= shares;
@@ -199,13 +213,13 @@ class AccrualPosition extends Position {
199
213
  return { position, assets, shares };
200
214
  }
201
215
  getBorrowCapacityLimit(options) {
202
- return this.market.getBorrowCapacityLimit(this, options);
216
+ return this._market.getBorrowCapacityLimit(this, options);
203
217
  }
204
218
  getWithdrawCollateralCapacityLimit(options) {
205
- return this.market.getWithdrawCollateralCapacityLimit(this, options);
219
+ return this._market.getWithdrawCollateralCapacityLimit(this, options);
206
220
  }
207
221
  getRepayCapacityLimit(loanTokenBalance) {
208
- return this.market.getRepayCapacityLimit(this.borrowShares, loanTokenBalance);
222
+ return this._market.getRepayCapacityLimit(this.borrowShares, loanTokenBalance);
209
223
  }
210
224
  getMaxCapacities(loanTokenBalance, collateralTokenBalance, options) {
211
225
  return {
@@ -0,0 +1,60 @@
1
+ import type { Address } from "viem";
2
+ import { type IMarket, Market } from "../market";
3
+ import type { BigIntish } from "../types";
4
+ import { AccrualPosition, type IAccrualPosition } from "./Position";
5
+ export interface IPreLiquidationParams {
6
+ preLltv: BigIntish;
7
+ preLCF1: BigIntish;
8
+ preLCF2: BigIntish;
9
+ preLIF1: BigIntish;
10
+ preLIF2: BigIntish;
11
+ preLiquidationOracle: Address;
12
+ }
13
+ export declare class PreLiquidationParams implements IPreLiquidationParams {
14
+ readonly preLltv: bigint;
15
+ readonly preLCF1: bigint;
16
+ readonly preLCF2: bigint;
17
+ readonly preLIF1: bigint;
18
+ readonly preLIF2: bigint;
19
+ readonly preLiquidationOracle: `0x${string}`;
20
+ constructor({ preLltv, preLCF1, preLCF2, preLIF1, preLIF2, preLiquidationOracle, }: IPreLiquidationParams);
21
+ getCloseFactor(quotient: BigIntish): bigint;
22
+ getIncentiveFactor(quotient: BigIntish): bigint;
23
+ }
24
+ export interface IPreLiquidationPosition extends IAccrualPosition {
25
+ /**
26
+ * The pre-liquidation parameters of the associated PreLiquidation contract.
27
+ */
28
+ preLiquidationParams: IPreLiquidationParams;
29
+ /**
30
+ * The address of the PreLiquidation contract this position is associated to.
31
+ */
32
+ preLiquidation: Address;
33
+ /**
34
+ * The price of the collateral quoted in loan assets used by the PreLiquidation contract.
35
+ * `undefined` if the oracle reverts.
36
+ */
37
+ preLiquidationOraclePrice?: BigIntish;
38
+ }
39
+ export declare class PreLiquidationPosition extends AccrualPosition implements IPreLiquidationPosition {
40
+ readonly preLiquidationParams: PreLiquidationParams;
41
+ readonly preLiquidation: `0x${string}`;
42
+ readonly preLiquidationOraclePrice?: bigint;
43
+ protected readonly _baseMarket: Market;
44
+ constructor({ preLiquidationParams, preLiquidation, preLiquidationOraclePrice, ...position }: IPreLiquidationPosition, market: IMarket);
45
+ get market(): Market;
46
+ protected get _lltv(): bigint;
47
+ /**
48
+ * @inheritdoc `undefined` if the pre-liquidation's oracle reverts.
49
+ * `undefined` if it may be liquidatable on Morpho.
50
+ */
51
+ get isHealthy(): boolean | undefined;
52
+ /**
53
+ * @inheritdoc `undefined` if the pre-liquidation's oracle reverts.
54
+ */
55
+ get isLiquidatable(): boolean | undefined;
56
+ /**
57
+ * @inheritdoc `undefined` if the pre-liquidation's oracle reverts.
58
+ */
59
+ get seizableCollateral(): bigint | undefined;
60
+ }
@@ -0,0 +1,102 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PreLiquidationPosition = exports.PreLiquidationParams = void 0;
4
+ const constants_1 = require("../constants");
5
+ const market_1 = require("../market");
6
+ const math_1 = require("../math");
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;
31
+ class PreLiquidationPosition extends Position_1.AccrualPosition {
32
+ preLiquidationParams;
33
+ preLiquidation;
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;
58
+ }
59
+ /**
60
+ * @inheritdoc `undefined` if the pre-liquidation's oracle reverts.
61
+ * `undefined` if it may be liquidatable on Morpho.
62
+ */
63
+ get isHealthy() {
64
+ const { collateralValue } = this;
65
+ if (collateralValue == null)
66
+ return;
67
+ const { borrowAssets } = this;
68
+ if (borrowAssets > math_1.MathLib.wMulDown(collateralValue, this._lltv))
69
+ return;
70
+ return (borrowAssets <=
71
+ math_1.MathLib.wMulDown(collateralValue, this.preLiquidationParams.preLltv));
72
+ }
73
+ /**
74
+ * @inheritdoc `undefined` if the pre-liquidation's oracle reverts.
75
+ */
76
+ get isLiquidatable() {
77
+ const { collateralValue } = this;
78
+ if (collateralValue == null)
79
+ return;
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));
84
+ }
85
+ /**
86
+ * @inheritdoc `undefined` if the pre-liquidation's oracle reverts.
87
+ */
88
+ get seizableCollateral() {
89
+ if (this._market.price == null)
90
+ return;
91
+ if (!this.isLiquidatable)
92
+ return 0n;
93
+ const { ltv } = this;
94
+ if (ltv == null)
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);
100
+ }
101
+ }
102
+ exports.PreLiquidationPosition = PreLiquidationPosition;
@@ -1 +1,2 @@
1
1
  export * from "./Position.js";
2
+ export * from "./PreLiquidationPosition.js";
@@ -15,3 +15,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./Position.js"), exports);
18
+ __exportStar(require("./PreLiquidationPosition.js"), exports);
@@ -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;