@morpho-org/blue-sdk 3.0.0-next.9 → 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.
@@ -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;