@morpho-org/blue-sdk 5.16.0 → 5.18.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.
- package/lib/{addresses.d.ts → cjs/addresses.d.ts} +12 -0
- package/lib/{addresses.js → cjs/addresses.js} +6 -0
- package/lib/cjs/package.json +1 -0
- package/lib/{position → cjs/position}/PreLiquidationPosition.d.ts +3 -3
- package/lib/{position → cjs/position}/PreLiquidationPosition.js +16 -16
- package/lib/{preLiquidation.d.ts → cjs/preLiquidation.d.ts} +1 -1
- package/lib/{preLiquidation.js → cjs/preLiquidation.js} +2 -2
- package/lib/{vault → cjs/vault}/index.d.ts +1 -1
- package/lib/{vault → cjs/vault}/index.js +1 -1
- package/lib/{vault → cjs/vault}/v2/VaultV2.d.ts +5 -5
- package/lib/{vault → cjs/vault}/v2/VaultV2.js +25 -25
- package/lib/{vault → cjs/vault}/v2/VaultV2Adapter.d.ts +2 -2
- package/lib/{vault → cjs/vault}/v2/VaultV2MorphoMarketV1Adapter.d.ts +7 -7
- package/lib/{vault → cjs/vault}/v2/VaultV2MorphoMarketV1Adapter.js +9 -9
- package/lib/{vault → cjs/vault}/v2/VaultV2MorphoMarketV1AdapterV2.d.ts +6 -6
- package/lib/{vault → cjs/vault}/v2/VaultV2MorphoMarketV1AdapterV2.js +8 -8
- package/lib/{vault → cjs/vault}/v2/VaultV2MorphoVaultV1Adapter.d.ts +6 -6
- package/lib/{vault → cjs/vault}/v2/VaultV2MorphoVaultV1Adapter.js +2 -2
- package/lib/esm/addresses.d.ts +2243 -0
- package/lib/esm/addresses.js +1315 -0
- package/lib/esm/chain.d.ts +405 -0
- package/lib/esm/chain.js +276 -0
- package/lib/esm/constants.d.ts +20 -0
- package/lib/esm/constants.js +21 -0
- package/lib/esm/errors.d.ts +101 -0
- package/lib/esm/errors.js +192 -0
- package/lib/esm/holding/AssetBalances.d.ts +48 -0
- package/lib/esm/holding/AssetBalances.js +38 -0
- package/lib/esm/holding/Holding.d.ts +63 -0
- package/lib/esm/holding/Holding.js +63 -0
- package/lib/esm/holding/index.d.ts +2 -0
- package/lib/esm/holding/index.js +2 -0
- package/lib/esm/index.d.ts +14 -0
- package/lib/esm/index.js +14 -0
- package/lib/esm/market/Market.d.ts +424 -0
- package/lib/esm/market/Market.js +576 -0
- package/lib/esm/market/MarketParams.d.ts +74 -0
- package/lib/esm/market/MarketParams.js +91 -0
- package/lib/esm/market/MarketUtils.d.ts +273 -0
- package/lib/esm/market/MarketUtils.js +323 -0
- package/lib/esm/market/index.d.ts +3 -0
- package/lib/esm/market/index.js +3 -0
- package/lib/esm/math/AdaptiveCurveIrmLib.d.ts +40 -0
- package/lib/esm/math/AdaptiveCurveIrmLib.js +133 -0
- package/lib/esm/math/MathLib.d.ts +102 -0
- package/lib/esm/math/MathLib.js +168 -0
- package/lib/esm/math/SharesMath.d.ts +12 -0
- package/lib/esm/math/SharesMath.js +18 -0
- package/lib/esm/math/index.d.ts +3 -0
- package/lib/esm/math/index.js +3 -0
- package/lib/esm/package.json +1 -0
- package/lib/esm/position/Position.d.ts +147 -0
- package/lib/esm/position/Position.js +237 -0
- package/lib/esm/position/PreLiquidationPosition.d.ts +60 -0
- package/lib/esm/position/PreLiquidationPosition.js +97 -0
- package/lib/esm/position/index.d.ts +2 -0
- package/lib/esm/position/index.js +2 -0
- package/lib/esm/preLiquidation.d.ts +15 -0
- package/lib/esm/preLiquidation.js +91 -0
- package/lib/esm/token/ConstantWrappedToken.d.ts +17 -0
- package/lib/esm/token/ConstantWrappedToken.js +30 -0
- package/lib/esm/token/Eip5267Domain.d.ts +55 -0
- package/lib/esm/token/Eip5267Domain.js +66 -0
- package/lib/esm/token/ExchangeRateWrappedToken.d.ts +11 -0
- package/lib/esm/token/ExchangeRateWrappedToken.js +17 -0
- package/lib/esm/token/Token.d.ts +52 -0
- package/lib/esm/token/Token.js +62 -0
- package/lib/esm/token/VaultToken.d.ts +23 -0
- package/lib/esm/token/VaultToken.js +27 -0
- package/lib/esm/token/WrappedToken.d.ts +17 -0
- package/lib/esm/token/WrappedToken.js +29 -0
- package/lib/esm/token/index.d.ts +6 -0
- package/lib/esm/token/index.js +6 -0
- package/lib/esm/types.d.ts +26 -0
- package/lib/esm/types.js +13 -0
- package/lib/esm/user/User.d.ts +20 -0
- package/lib/esm/user/User.js +19 -0
- package/lib/esm/user/index.d.ts +1 -0
- package/lib/esm/user/index.js +1 -0
- package/lib/esm/utils.d.ts +13 -0
- package/lib/esm/utils.js +10 -0
- package/lib/esm/vault/Vault.d.ts +214 -0
- package/lib/esm/vault/Vault.js +289 -0
- package/lib/esm/vault/VaultConfig.d.ts +11 -0
- package/lib/esm/vault/VaultConfig.js +10 -0
- package/lib/esm/vault/VaultMarketAllocation.d.ts +20 -0
- package/lib/esm/vault/VaultMarketAllocation.js +27 -0
- package/lib/esm/vault/VaultMarketConfig.d.ts +43 -0
- package/lib/esm/vault/VaultMarketConfig.js +39 -0
- package/lib/esm/vault/VaultMarketPublicAllocatorConfig.d.ts +29 -0
- package/lib/esm/vault/VaultMarketPublicAllocatorConfig.js +24 -0
- package/lib/esm/vault/VaultUser.d.ts +26 -0
- package/lib/esm/vault/VaultUser.js +24 -0
- package/lib/esm/vault/VaultUtils.d.ts +16 -0
- package/lib/esm/vault/VaultUtils.js +17 -0
- package/lib/esm/vault/index.d.ts +8 -0
- package/lib/esm/vault/index.js +8 -0
- package/lib/esm/vault/v2/VaultV2.d.ts +87 -0
- package/lib/esm/vault/v2/VaultV2.js +154 -0
- package/lib/esm/vault/v2/VaultV2Adapter.d.ts +29 -0
- package/lib/esm/vault/v2/VaultV2Adapter.js +12 -0
- package/lib/esm/vault/v2/VaultV2MorphoMarketV1Adapter.d.ts +30 -0
- package/lib/esm/vault/v2/VaultV2MorphoMarketV1Adapter.js +54 -0
- package/lib/esm/vault/v2/VaultV2MorphoMarketV1AdapterV2.d.ts +33 -0
- package/lib/esm/vault/v2/VaultV2MorphoMarketV1AdapterV2.js +63 -0
- package/lib/esm/vault/v2/VaultV2MorphoVaultV1Adapter.d.ts +24 -0
- package/lib/esm/vault/v2/VaultV2MorphoVaultV1Adapter.js +36 -0
- package/lib/esm/vault/v2/index.d.ts +5 -0
- package/lib/esm/vault/v2/index.js +5 -0
- package/package.json +16 -7
- package/src/index.ts +14 -0
- /package/lib/{chain.d.ts → cjs/chain.d.ts} +0 -0
- /package/lib/{chain.js → cjs/chain.js} +0 -0
- /package/lib/{constants.d.ts → cjs/constants.d.ts} +0 -0
- /package/lib/{constants.js → cjs/constants.js} +0 -0
- /package/lib/{errors.d.ts → cjs/errors.d.ts} +0 -0
- /package/lib/{errors.js → cjs/errors.js} +0 -0
- /package/lib/{holding → cjs/holding}/AssetBalances.d.ts +0 -0
- /package/lib/{holding → cjs/holding}/AssetBalances.js +0 -0
- /package/lib/{holding → cjs/holding}/Holding.d.ts +0 -0
- /package/lib/{holding → cjs/holding}/Holding.js +0 -0
- /package/lib/{holding → cjs/holding}/index.d.ts +0 -0
- /package/lib/{holding → cjs/holding}/index.js +0 -0
- /package/lib/{index.d.ts → cjs/index.d.ts} +0 -0
- /package/lib/{index.js → cjs/index.js} +0 -0
- /package/lib/{market → cjs/market}/Market.d.ts +0 -0
- /package/lib/{market → cjs/market}/Market.js +0 -0
- /package/lib/{market → cjs/market}/MarketParams.d.ts +0 -0
- /package/lib/{market → cjs/market}/MarketParams.js +0 -0
- /package/lib/{market → cjs/market}/MarketUtils.d.ts +0 -0
- /package/lib/{market → cjs/market}/MarketUtils.js +0 -0
- /package/lib/{market → cjs/market}/index.d.ts +0 -0
- /package/lib/{market → cjs/market}/index.js +0 -0
- /package/lib/{math → cjs/math}/AdaptiveCurveIrmLib.d.ts +0 -0
- /package/lib/{math → cjs/math}/AdaptiveCurveIrmLib.js +0 -0
- /package/lib/{math → cjs/math}/MathLib.d.ts +0 -0
- /package/lib/{math → cjs/math}/MathLib.js +0 -0
- /package/lib/{math → cjs/math}/SharesMath.d.ts +0 -0
- /package/lib/{math → cjs/math}/SharesMath.js +0 -0
- /package/lib/{math → cjs/math}/index.d.ts +0 -0
- /package/lib/{math → cjs/math}/index.js +0 -0
- /package/lib/{position → cjs/position}/Position.d.ts +0 -0
- /package/lib/{position → cjs/position}/Position.js +0 -0
- /package/lib/{position → cjs/position}/index.d.ts +0 -0
- /package/lib/{position → cjs/position}/index.js +0 -0
- /package/lib/{token → cjs/token}/ConstantWrappedToken.d.ts +0 -0
- /package/lib/{token → cjs/token}/ConstantWrappedToken.js +0 -0
- /package/lib/{token → cjs/token}/Eip5267Domain.d.ts +0 -0
- /package/lib/{token → cjs/token}/Eip5267Domain.js +0 -0
- /package/lib/{token → cjs/token}/ExchangeRateWrappedToken.d.ts +0 -0
- /package/lib/{token → cjs/token}/ExchangeRateWrappedToken.js +0 -0
- /package/lib/{token → cjs/token}/Token.d.ts +0 -0
- /package/lib/{token → cjs/token}/Token.js +0 -0
- /package/lib/{token → cjs/token}/VaultToken.d.ts +0 -0
- /package/lib/{token → cjs/token}/VaultToken.js +0 -0
- /package/lib/{token → cjs/token}/WrappedToken.d.ts +0 -0
- /package/lib/{token → cjs/token}/WrappedToken.js +0 -0
- /package/lib/{token → cjs/token}/index.d.ts +0 -0
- /package/lib/{token → cjs/token}/index.js +0 -0
- /package/lib/{types.d.ts → cjs/types.d.ts} +0 -0
- /package/lib/{types.js → cjs/types.js} +0 -0
- /package/lib/{user → cjs/user}/User.d.ts +0 -0
- /package/lib/{user → cjs/user}/User.js +0 -0
- /package/lib/{user → cjs/user}/index.d.ts +0 -0
- /package/lib/{user → cjs/user}/index.js +0 -0
- /package/lib/{utils.d.ts → cjs/utils.d.ts} +0 -0
- /package/lib/{utils.js → cjs/utils.js} +0 -0
- /package/lib/{vault → cjs/vault}/Vault.d.ts +0 -0
- /package/lib/{vault → cjs/vault}/Vault.js +0 -0
- /package/lib/{vault → cjs/vault}/VaultConfig.d.ts +0 -0
- /package/lib/{vault → cjs/vault}/VaultConfig.js +0 -0
- /package/lib/{vault → cjs/vault}/VaultMarketAllocation.d.ts +0 -0
- /package/lib/{vault → cjs/vault}/VaultMarketAllocation.js +0 -0
- /package/lib/{vault → cjs/vault}/VaultMarketConfig.d.ts +0 -0
- /package/lib/{vault → cjs/vault}/VaultMarketConfig.js +0 -0
- /package/lib/{vault → cjs/vault}/VaultMarketPublicAllocatorConfig.d.ts +0 -0
- /package/lib/{vault → cjs/vault}/VaultMarketPublicAllocatorConfig.js +0 -0
- /package/lib/{vault → cjs/vault}/VaultUser.d.ts +0 -0
- /package/lib/{vault → cjs/vault}/VaultUser.js +0 -0
- /package/lib/{vault → cjs/vault}/VaultUtils.d.ts +0 -0
- /package/lib/{vault → cjs/vault}/VaultUtils.js +0 -0
- /package/lib/{vault → cjs/vault}/v2/VaultV2Adapter.js +0 -0
- /package/lib/{vault → cjs/vault}/v2/index.d.ts +0 -0
- /package/lib/{vault → cjs/vault}/v2/index.js +0 -0
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { ZERO_ADDRESS } from "@morpho-org/morpho-ts";
|
|
2
|
+
import { InvalidMarketParamsError, UnknownMarketParamsError, } from "../errors.js";
|
|
3
|
+
import { decodeAbiParameters } from "viem";
|
|
4
|
+
import { MarketUtils } from "./MarketUtils.js";
|
|
5
|
+
export const marketParamsAbi = {
|
|
6
|
+
type: "tuple",
|
|
7
|
+
components: [
|
|
8
|
+
{ type: "address", name: "loanToken" },
|
|
9
|
+
{ type: "address", name: "collateralToken" },
|
|
10
|
+
{ type: "address", name: "oracle" },
|
|
11
|
+
{ type: "address", name: "irm" },
|
|
12
|
+
{ type: "uint256", name: "lltv" },
|
|
13
|
+
],
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Represents a market's configuration (also called market params).
|
|
17
|
+
*/
|
|
18
|
+
export class MarketParams {
|
|
19
|
+
static _CACHE = {};
|
|
20
|
+
/**
|
|
21
|
+
* Returns the previously cached market config for the given id, if any.
|
|
22
|
+
* @throws {UnknownMarketParamsError} If no market config is cached.
|
|
23
|
+
*/
|
|
24
|
+
static get(id) {
|
|
25
|
+
const marketParams = MarketParams._CACHE[id];
|
|
26
|
+
if (!marketParams)
|
|
27
|
+
throw new UnknownMarketParamsError(id);
|
|
28
|
+
return marketParams;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Returns the canonical idle market configuration for the given loan token.
|
|
32
|
+
*/
|
|
33
|
+
static idle(token) {
|
|
34
|
+
return new MarketParams({
|
|
35
|
+
collateralToken: ZERO_ADDRESS,
|
|
36
|
+
loanToken: token,
|
|
37
|
+
oracle: ZERO_ADDRESS,
|
|
38
|
+
irm: ZERO_ADDRESS,
|
|
39
|
+
lltv: 0n,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
static fromHex(data) {
|
|
43
|
+
try {
|
|
44
|
+
const [marketParams] = decodeAbiParameters([marketParamsAbi], data);
|
|
45
|
+
return new MarketParams(marketParams);
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
throw new InvalidMarketParamsError(data);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* The market's collateral token address.
|
|
53
|
+
*/
|
|
54
|
+
collateralToken;
|
|
55
|
+
/**
|
|
56
|
+
* The market's loan token address.
|
|
57
|
+
*/
|
|
58
|
+
loanToken;
|
|
59
|
+
/**
|
|
60
|
+
* The market's oracle address.
|
|
61
|
+
*/
|
|
62
|
+
oracle;
|
|
63
|
+
/**
|
|
64
|
+
* The market's interest rate model address.
|
|
65
|
+
*/
|
|
66
|
+
irm;
|
|
67
|
+
/**
|
|
68
|
+
* The market's liquidation Loan-To-Value (scaled by WAD).
|
|
69
|
+
*/
|
|
70
|
+
lltv;
|
|
71
|
+
/**
|
|
72
|
+
* The market's hex-encoded id, defined as the hash of the market params.
|
|
73
|
+
*/
|
|
74
|
+
id; // Cached because params are readonly.
|
|
75
|
+
/**
|
|
76
|
+
* The market's liquidation incentive factor.
|
|
77
|
+
*/
|
|
78
|
+
liquidationIncentiveFactor; // Cached because lltv is readonly.
|
|
79
|
+
constructor(params) {
|
|
80
|
+
const { collateralToken, loanToken, oracle, irm, lltv } = params;
|
|
81
|
+
this.collateralToken = collateralToken;
|
|
82
|
+
this.loanToken = loanToken;
|
|
83
|
+
this.oracle = oracle;
|
|
84
|
+
this.irm = irm;
|
|
85
|
+
this.lltv = BigInt(lltv);
|
|
86
|
+
this.id = MarketUtils.getMarketId(params);
|
|
87
|
+
this.liquidationIncentiveFactor =
|
|
88
|
+
MarketUtils.getLiquidationIncentiveFactor(params);
|
|
89
|
+
MarketParams._CACHE[this.id] = this;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
import { type RoundingDirection } from "../math/index.js";
|
|
2
|
+
import type { BigIntish, MarketId } from "../types.js";
|
|
3
|
+
import type { IMarketParams } from "./MarketParams.js";
|
|
4
|
+
/**
|
|
5
|
+
* Namespace of utility functions to ease market-related calculations.
|
|
6
|
+
*/
|
|
7
|
+
export declare namespace MarketUtils {
|
|
8
|
+
/**
|
|
9
|
+
* Returns the id of a market based on its params.
|
|
10
|
+
* @param market The market params.
|
|
11
|
+
*/
|
|
12
|
+
function getMarketId(market: IMarketParams): MarketId;
|
|
13
|
+
/**
|
|
14
|
+
* Returns the liquidation incentive factor for a given market params.
|
|
15
|
+
* @param config The market params.
|
|
16
|
+
*/
|
|
17
|
+
function getLiquidationIncentiveFactor({ lltv }: {
|
|
18
|
+
lltv: BigIntish;
|
|
19
|
+
}): bigint;
|
|
20
|
+
/**
|
|
21
|
+
* Returns the market's utilization rate (scaled by WAD).
|
|
22
|
+
* @param market The market state.
|
|
23
|
+
*/
|
|
24
|
+
function getUtilization({ totalSupplyAssets, totalBorrowAssets, }: {
|
|
25
|
+
totalSupplyAssets: BigIntish;
|
|
26
|
+
totalBorrowAssets: BigIntish;
|
|
27
|
+
}): bigint;
|
|
28
|
+
/**
|
|
29
|
+
* Returns the per-second rate continuously compounded over a year,
|
|
30
|
+
* as calculated in Morpho Blue assuming the market is frequently accrued onchain.
|
|
31
|
+
* @param rate The per-second rate to compound annually.
|
|
32
|
+
*/
|
|
33
|
+
function rateToApy(rate: BigIntish): number;
|
|
34
|
+
/**
|
|
35
|
+
* Returns the interest accrued on both sides of the given market
|
|
36
|
+
* as well as the supply shares minted to the fee recipient.
|
|
37
|
+
* @param borrowRate The average borrow rate since the last market update (scaled by WAD).
|
|
38
|
+
* @param market The market state.
|
|
39
|
+
* @param elapsed The time elapsed since the last market update (in seconds).
|
|
40
|
+
*/
|
|
41
|
+
function getAccruedInterest(borrowRate: BigIntish, { totalSupplyAssets, totalBorrowAssets, totalSupplyShares, fee, }: {
|
|
42
|
+
totalSupplyAssets: BigIntish;
|
|
43
|
+
totalBorrowAssets: BigIntish;
|
|
44
|
+
totalSupplyShares: BigIntish;
|
|
45
|
+
fee: BigIntish;
|
|
46
|
+
}, elapsed?: bigint): {
|
|
47
|
+
interest: bigint;
|
|
48
|
+
feeShares: bigint;
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Returns the smallest volume to supply until the market gets the closest to the given utilization rate.
|
|
52
|
+
* @param market The market state.
|
|
53
|
+
* @param utilization The target utilization rate (scaled by WAD).
|
|
54
|
+
*/
|
|
55
|
+
function getSupplyToUtilization(market: {
|
|
56
|
+
totalSupplyAssets: BigIntish;
|
|
57
|
+
totalBorrowAssets: BigIntish;
|
|
58
|
+
}, utilization: BigIntish): bigint;
|
|
59
|
+
/**
|
|
60
|
+
* Returns the liquidity available to withdraw until the market gets the closest to the given utilization rate.
|
|
61
|
+
* @param market The market state.
|
|
62
|
+
* @param utilization The target utilization rate (scaled by WAD).
|
|
63
|
+
*/
|
|
64
|
+
function getWithdrawToUtilization({ totalSupplyAssets, totalBorrowAssets, }: {
|
|
65
|
+
totalSupplyAssets: BigIntish;
|
|
66
|
+
totalBorrowAssets: BigIntish;
|
|
67
|
+
}, utilization: BigIntish): bigint;
|
|
68
|
+
/**
|
|
69
|
+
* Returns the liquidity available to borrow until the market gets the closest to the given utilization rate.
|
|
70
|
+
* @param market The market state.
|
|
71
|
+
* @param utilization The target utilization rate (scaled by WAD).
|
|
72
|
+
*/
|
|
73
|
+
function getBorrowToUtilization({ totalSupplyAssets, totalBorrowAssets, }: {
|
|
74
|
+
totalSupplyAssets: BigIntish;
|
|
75
|
+
totalBorrowAssets: BigIntish;
|
|
76
|
+
}, utilization: BigIntish): bigint;
|
|
77
|
+
/**
|
|
78
|
+
* Returns the smallest volume to repay until the market gets the closest to the given utilization rate.
|
|
79
|
+
* @param market The market state.
|
|
80
|
+
* @param utilization The target utilization rate (scaled by WAD).
|
|
81
|
+
*/
|
|
82
|
+
function getRepayToUtilization({ totalSupplyAssets, totalBorrowAssets, }: {
|
|
83
|
+
totalSupplyAssets: BigIntish;
|
|
84
|
+
totalBorrowAssets: BigIntish;
|
|
85
|
+
}, utilization: BigIntish): bigint;
|
|
86
|
+
function getCollateralPower(collateral: BigIntish, { lltv }: {
|
|
87
|
+
lltv: BigIntish;
|
|
88
|
+
}): bigint;
|
|
89
|
+
/**
|
|
90
|
+
* Returns the value of a given amount of collateral quoted in loan assets.
|
|
91
|
+
* Return `undefined` iff the market's price is undefined.
|
|
92
|
+
*/
|
|
93
|
+
function getCollateralValue(collateral: BigIntish, { price }: {
|
|
94
|
+
price?: BigIntish;
|
|
95
|
+
}): bigint | undefined;
|
|
96
|
+
/**
|
|
97
|
+
* Returns the maximum debt allowed given a certain amount of collateral.
|
|
98
|
+
* Return `undefined` iff the market's price is undefined.
|
|
99
|
+
* To calculate the amount of loan assets that can be borrowed, use `getMaxBorrowableAssets`.
|
|
100
|
+
*/
|
|
101
|
+
function getMaxBorrowAssets(collateral: BigIntish, market: {
|
|
102
|
+
price?: BigIntish;
|
|
103
|
+
}, { lltv }: {
|
|
104
|
+
lltv: BigIntish;
|
|
105
|
+
}): bigint | undefined;
|
|
106
|
+
/**
|
|
107
|
+
* Returns the maximum amount of loan assets that can be borrowed given a certain borrow position.
|
|
108
|
+
* Return `undefined` iff the market's price is undefined.
|
|
109
|
+
*/
|
|
110
|
+
function getMaxBorrowableAssets({ collateral, borrowShares, }: {
|
|
111
|
+
collateral: BigIntish;
|
|
112
|
+
borrowShares: BigIntish;
|
|
113
|
+
}, market: {
|
|
114
|
+
totalBorrowAssets: BigIntish;
|
|
115
|
+
totalBorrowShares: BigIntish;
|
|
116
|
+
price?: BigIntish;
|
|
117
|
+
}, marketParams: {
|
|
118
|
+
lltv: BigIntish;
|
|
119
|
+
}): bigint | undefined;
|
|
120
|
+
/**
|
|
121
|
+
* Returns the amount of collateral that would be seized in a liquidation given a certain amount of repaid shares.
|
|
122
|
+
* Return `undefined` iff the market's price is undefined.
|
|
123
|
+
*/
|
|
124
|
+
function getLiquidationSeizedAssets(repaidShares: BigIntish, market: {
|
|
125
|
+
totalBorrowAssets: BigIntish;
|
|
126
|
+
totalBorrowShares: BigIntish;
|
|
127
|
+
price?: BigIntish;
|
|
128
|
+
}, config: {
|
|
129
|
+
lltv: BigIntish;
|
|
130
|
+
}): bigint | undefined;
|
|
131
|
+
/**
|
|
132
|
+
* Returns the amount of borrow shares that would be repaid in a liquidation given a certain amount of seized collateral.
|
|
133
|
+
* Return `undefined` iff the market's price is undefined.
|
|
134
|
+
*/
|
|
135
|
+
function getLiquidationRepaidShares(seizedAssets: BigIntish, market: {
|
|
136
|
+
totalBorrowAssets: BigIntish;
|
|
137
|
+
totalBorrowShares: BigIntish;
|
|
138
|
+
price?: BigIntish;
|
|
139
|
+
}, config: {
|
|
140
|
+
lltv: BigIntish;
|
|
141
|
+
}): bigint | undefined;
|
|
142
|
+
/**
|
|
143
|
+
* Returns the maximum amount of collateral that is worth being seized in a liquidation given a certain borrow position.
|
|
144
|
+
* Return `undefined` iff the market's price is undefined.
|
|
145
|
+
*/
|
|
146
|
+
function getSeizableCollateral(position: {
|
|
147
|
+
collateral: BigIntish;
|
|
148
|
+
borrowShares: BigIntish;
|
|
149
|
+
}, market: {
|
|
150
|
+
totalBorrowAssets: BigIntish;
|
|
151
|
+
totalBorrowShares: BigIntish;
|
|
152
|
+
price?: BigIntish;
|
|
153
|
+
}, config: {
|
|
154
|
+
lltv: BigIntish;
|
|
155
|
+
}): bigint | undefined;
|
|
156
|
+
/**
|
|
157
|
+
* Returns the amount of collateral that can be withdrawn given a certain borrow position.
|
|
158
|
+
* Return `undefined` iff the market's price is undefined.
|
|
159
|
+
*/
|
|
160
|
+
function getWithdrawableCollateral({ collateral, borrowShares, }: {
|
|
161
|
+
collateral: BigIntish;
|
|
162
|
+
borrowShares: BigIntish;
|
|
163
|
+
}, market: {
|
|
164
|
+
totalBorrowAssets: BigIntish;
|
|
165
|
+
totalBorrowShares: BigIntish;
|
|
166
|
+
price?: BigIntish;
|
|
167
|
+
}, { lltv }: {
|
|
168
|
+
lltv: BigIntish;
|
|
169
|
+
}): bigint | undefined;
|
|
170
|
+
/**
|
|
171
|
+
* Returns whether a given borrow position is healthy.
|
|
172
|
+
* Return `undefined` iff the market's price is undefined.
|
|
173
|
+
* @param position The borrow position to check.
|
|
174
|
+
*/
|
|
175
|
+
function isHealthy({ collateral, borrowShares, }: {
|
|
176
|
+
collateral: BigIntish;
|
|
177
|
+
borrowShares: BigIntish;
|
|
178
|
+
}, market: {
|
|
179
|
+
totalBorrowAssets: BigIntish;
|
|
180
|
+
totalBorrowShares: BigIntish;
|
|
181
|
+
price?: BigIntish;
|
|
182
|
+
}, marketParams: {
|
|
183
|
+
lltv: BigIntish;
|
|
184
|
+
}): boolean | undefined;
|
|
185
|
+
/**
|
|
186
|
+
* Returns the price of the collateral quoted in the loan token (e.g. ETH/DAI)
|
|
187
|
+
* that set the user's position to be liquidatable.
|
|
188
|
+
* Returns null if the position is not a borrow.
|
|
189
|
+
*/
|
|
190
|
+
function getLiquidationPrice({ collateral, borrowShares, }: {
|
|
191
|
+
collateral: BigIntish;
|
|
192
|
+
borrowShares: BigIntish;
|
|
193
|
+
}, market: {
|
|
194
|
+
totalBorrowAssets: BigIntish;
|
|
195
|
+
totalBorrowShares: BigIntish;
|
|
196
|
+
}, marketParams: {
|
|
197
|
+
lltv: BigIntish;
|
|
198
|
+
}): bigint | null;
|
|
199
|
+
/**
|
|
200
|
+
* Returns the price variation required for the given position to reach its liquidation threshold (scaled by WAD).
|
|
201
|
+
* Negative when healthy (the price needs to drop x%), positive when unhealthy (the price needs to soar x%).
|
|
202
|
+
* Returns `undefined` iff the market's price is undefined.
|
|
203
|
+
* Returns null if the position is not a borrow.
|
|
204
|
+
*/
|
|
205
|
+
function getPriceVariationToLiquidationPrice(position: {
|
|
206
|
+
collateral: BigIntish;
|
|
207
|
+
borrowShares: BigIntish;
|
|
208
|
+
}, market: {
|
|
209
|
+
totalBorrowAssets: BigIntish;
|
|
210
|
+
totalBorrowShares: BigIntish;
|
|
211
|
+
price?: BigIntish;
|
|
212
|
+
}, marketParams: {
|
|
213
|
+
lltv: BigIntish;
|
|
214
|
+
}): bigint | null | undefined;
|
|
215
|
+
/**
|
|
216
|
+
* Returns the health factor of a given borrow position (scaled by WAD).
|
|
217
|
+
* If the debt is 0, health factor is `MaxUint256`.
|
|
218
|
+
* Returns `undefined` iff the market's price is undefined.
|
|
219
|
+
*/
|
|
220
|
+
function getHealthFactor({ collateral, borrowShares, }: {
|
|
221
|
+
collateral: BigIntish;
|
|
222
|
+
borrowShares: BigIntish;
|
|
223
|
+
}, market: {
|
|
224
|
+
totalBorrowAssets: BigIntish;
|
|
225
|
+
totalBorrowShares: BigIntish;
|
|
226
|
+
price?: BigIntish;
|
|
227
|
+
}, marketParams: {
|
|
228
|
+
lltv: BigIntish;
|
|
229
|
+
}): bigint | undefined;
|
|
230
|
+
/**
|
|
231
|
+
* Returns the loan-to-value ratio of a given borrow position (scaled by WAD).
|
|
232
|
+
* Returns `undefined` iff the market's price is undefined.
|
|
233
|
+
* Returns null if the position is not a borrow.
|
|
234
|
+
*/
|
|
235
|
+
function getLtv({ collateral, borrowShares, }: {
|
|
236
|
+
collateral: BigIntish;
|
|
237
|
+
borrowShares: BigIntish;
|
|
238
|
+
}, market: {
|
|
239
|
+
totalBorrowAssets: BigIntish;
|
|
240
|
+
totalBorrowShares: BigIntish;
|
|
241
|
+
price?: BigIntish;
|
|
242
|
+
}): bigint | null | undefined;
|
|
243
|
+
/**
|
|
244
|
+
* Returns the usage ratio of the maximum borrow capacity given a certain borrow position (scaled by WAD).
|
|
245
|
+
* Returns `undefined` iff the market's price is undefined.
|
|
246
|
+
*/
|
|
247
|
+
function getBorrowCapacityUsage(position: {
|
|
248
|
+
collateral: BigIntish;
|
|
249
|
+
borrowShares: BigIntish;
|
|
250
|
+
}, market: {
|
|
251
|
+
totalBorrowAssets: BigIntish;
|
|
252
|
+
totalBorrowShares: BigIntish;
|
|
253
|
+
price?: BigIntish;
|
|
254
|
+
}, marketParams: {
|
|
255
|
+
lltv: BigIntish;
|
|
256
|
+
}): bigint | undefined;
|
|
257
|
+
function toSupplyAssets(shares: BigIntish, market: {
|
|
258
|
+
totalSupplyAssets: BigIntish;
|
|
259
|
+
totalSupplyShares: BigIntish;
|
|
260
|
+
}, rounding?: RoundingDirection): bigint;
|
|
261
|
+
function toSupplyShares(assets: BigIntish, market: {
|
|
262
|
+
totalSupplyAssets: BigIntish;
|
|
263
|
+
totalSupplyShares: BigIntish;
|
|
264
|
+
}, rounding?: RoundingDirection): bigint;
|
|
265
|
+
function toBorrowAssets(shares: BigIntish, market: {
|
|
266
|
+
totalBorrowAssets: BigIntish;
|
|
267
|
+
totalBorrowShares: BigIntish;
|
|
268
|
+
}, rounding?: RoundingDirection): bigint;
|
|
269
|
+
function toBorrowShares(assets: BigIntish, market: {
|
|
270
|
+
totalBorrowAssets: BigIntish;
|
|
271
|
+
totalBorrowShares: BigIntish;
|
|
272
|
+
}, rounding?: RoundingDirection): bigint;
|
|
273
|
+
}
|
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
import { keccak_256 } from "@noble/hashes/sha3";
|
|
2
|
+
import { bytesToHex, hexToBytes } from "@noble/hashes/utils";
|
|
3
|
+
import { formatEther } from "viem";
|
|
4
|
+
import { LIQUIDATION_CURSOR, MAX_LIQUIDATION_INCENTIVE_FACTOR, ORACLE_PRICE_SCALE, SECONDS_PER_YEAR, } from "../constants.js";
|
|
5
|
+
import { MathLib, SharesMath } from "../math/index.js";
|
|
6
|
+
/**
|
|
7
|
+
* Namespace of utility functions to ease market-related calculations.
|
|
8
|
+
*/
|
|
9
|
+
export var MarketUtils;
|
|
10
|
+
(function (MarketUtils) {
|
|
11
|
+
/**
|
|
12
|
+
* Returns the id of a market based on its params.
|
|
13
|
+
* @param market The market params.
|
|
14
|
+
*/
|
|
15
|
+
function getMarketId(market) {
|
|
16
|
+
return `0x${bytesToHex(keccak_256(hexToBytes(`${market.loanToken.substring(2).toLowerCase().padStart(64, "0") +
|
|
17
|
+
market.collateralToken
|
|
18
|
+
.substring(2)
|
|
19
|
+
.toLowerCase()
|
|
20
|
+
.padStart(64, "0") +
|
|
21
|
+
market.oracle.substring(2).padStart(64, "0") +
|
|
22
|
+
market.irm.substring(2).toLowerCase().padStart(64, "0") +
|
|
23
|
+
BigInt(market.lltv).toString(16).padStart(64, "0")}`)))}`;
|
|
24
|
+
}
|
|
25
|
+
MarketUtils.getMarketId = getMarketId;
|
|
26
|
+
/**
|
|
27
|
+
* Returns the liquidation incentive factor for a given market params.
|
|
28
|
+
* @param config The market params.
|
|
29
|
+
*/
|
|
30
|
+
function getLiquidationIncentiveFactor({ lltv }) {
|
|
31
|
+
return MathLib.min(MAX_LIQUIDATION_INCENTIVE_FACTOR, MathLib.wDivDown(MathLib.WAD, MathLib.WAD -
|
|
32
|
+
MathLib.wMulDown(LIQUIDATION_CURSOR, MathLib.WAD - BigInt(lltv))));
|
|
33
|
+
}
|
|
34
|
+
MarketUtils.getLiquidationIncentiveFactor = getLiquidationIncentiveFactor;
|
|
35
|
+
/**
|
|
36
|
+
* Returns the market's utilization rate (scaled by WAD).
|
|
37
|
+
* @param market The market state.
|
|
38
|
+
*/
|
|
39
|
+
function getUtilization({ totalSupplyAssets, totalBorrowAssets, }) {
|
|
40
|
+
totalSupplyAssets = BigInt(totalSupplyAssets);
|
|
41
|
+
totalBorrowAssets = BigInt(totalBorrowAssets);
|
|
42
|
+
if (totalSupplyAssets === 0n) {
|
|
43
|
+
if (totalBorrowAssets > 0n)
|
|
44
|
+
return MathLib.MAX_UINT_256;
|
|
45
|
+
return 0n;
|
|
46
|
+
}
|
|
47
|
+
return MathLib.wDivDown(totalBorrowAssets, totalSupplyAssets);
|
|
48
|
+
}
|
|
49
|
+
MarketUtils.getUtilization = getUtilization;
|
|
50
|
+
/**
|
|
51
|
+
* Returns the per-second rate continuously compounded over a year,
|
|
52
|
+
* as calculated in Morpho Blue assuming the market is frequently accrued onchain.
|
|
53
|
+
* @param rate The per-second rate to compound annually.
|
|
54
|
+
*/
|
|
55
|
+
function rateToApy(rate) {
|
|
56
|
+
return Math.expm1(+formatEther(BigInt(rate) * SECONDS_PER_YEAR));
|
|
57
|
+
}
|
|
58
|
+
MarketUtils.rateToApy = rateToApy;
|
|
59
|
+
/**
|
|
60
|
+
* Returns the interest accrued on both sides of the given market
|
|
61
|
+
* as well as the supply shares minted to the fee recipient.
|
|
62
|
+
* @param borrowRate The average borrow rate since the last market update (scaled by WAD).
|
|
63
|
+
* @param market The market state.
|
|
64
|
+
* @param elapsed The time elapsed since the last market update (in seconds).
|
|
65
|
+
*/
|
|
66
|
+
function getAccruedInterest(borrowRate, { totalSupplyAssets, totalBorrowAssets, totalSupplyShares, fee, }, elapsed = 0n) {
|
|
67
|
+
const interest = MathLib.wMulDown(totalBorrowAssets, MathLib.wTaylorCompounded(borrowRate, elapsed));
|
|
68
|
+
const feeAmount = MathLib.wMulDown(interest, fee);
|
|
69
|
+
const feeShares = toSupplyShares(feeAmount, {
|
|
70
|
+
totalSupplyAssets: BigInt(totalSupplyAssets) - feeAmount,
|
|
71
|
+
totalSupplyShares,
|
|
72
|
+
}, "Down");
|
|
73
|
+
return { interest, feeShares };
|
|
74
|
+
}
|
|
75
|
+
MarketUtils.getAccruedInterest = getAccruedInterest;
|
|
76
|
+
/**
|
|
77
|
+
* Returns the smallest volume to supply until the market gets the closest to the given utilization rate.
|
|
78
|
+
* @param market The market state.
|
|
79
|
+
* @param utilization The target utilization rate (scaled by WAD).
|
|
80
|
+
*/
|
|
81
|
+
function getSupplyToUtilization(market, utilization) {
|
|
82
|
+
utilization = BigInt(utilization);
|
|
83
|
+
if (utilization === 0n) {
|
|
84
|
+
if (getUtilization(market) === 0n)
|
|
85
|
+
return 0n;
|
|
86
|
+
return MathLib.MAX_UINT_256;
|
|
87
|
+
}
|
|
88
|
+
return MathLib.zeroFloorSub(MathLib.wDivUp(market.totalBorrowAssets, utilization), market.totalSupplyAssets);
|
|
89
|
+
}
|
|
90
|
+
MarketUtils.getSupplyToUtilization = getSupplyToUtilization;
|
|
91
|
+
/**
|
|
92
|
+
* Returns the liquidity available to withdraw until the market gets the closest to the given utilization rate.
|
|
93
|
+
* @param market The market state.
|
|
94
|
+
* @param utilization The target utilization rate (scaled by WAD).
|
|
95
|
+
*/
|
|
96
|
+
function getWithdrawToUtilization({ totalSupplyAssets, totalBorrowAssets, }, utilization) {
|
|
97
|
+
utilization = BigInt(utilization);
|
|
98
|
+
totalSupplyAssets = BigInt(totalSupplyAssets);
|
|
99
|
+
totalBorrowAssets = BigInt(totalBorrowAssets);
|
|
100
|
+
if (utilization === 0n) {
|
|
101
|
+
if (totalBorrowAssets === 0n)
|
|
102
|
+
return totalSupplyAssets;
|
|
103
|
+
return 0n;
|
|
104
|
+
}
|
|
105
|
+
return MathLib.zeroFloorSub(totalSupplyAssets, MathLib.wDivUp(totalBorrowAssets, utilization));
|
|
106
|
+
}
|
|
107
|
+
MarketUtils.getWithdrawToUtilization = getWithdrawToUtilization;
|
|
108
|
+
/**
|
|
109
|
+
* Returns the liquidity available to borrow until the market gets the closest to the given utilization rate.
|
|
110
|
+
* @param market The market state.
|
|
111
|
+
* @param utilization The target utilization rate (scaled by WAD).
|
|
112
|
+
*/
|
|
113
|
+
function getBorrowToUtilization({ totalSupplyAssets, totalBorrowAssets, }, utilization) {
|
|
114
|
+
return MathLib.zeroFloorSub(MathLib.wMulDown(totalSupplyAssets, utilization), totalBorrowAssets);
|
|
115
|
+
}
|
|
116
|
+
MarketUtils.getBorrowToUtilization = getBorrowToUtilization;
|
|
117
|
+
/**
|
|
118
|
+
* Returns the smallest volume to repay until the market gets the closest to the given utilization rate.
|
|
119
|
+
* @param market The market state.
|
|
120
|
+
* @param utilization The target utilization rate (scaled by WAD).
|
|
121
|
+
*/
|
|
122
|
+
function getRepayToUtilization({ totalSupplyAssets, totalBorrowAssets, }, utilization) {
|
|
123
|
+
return MathLib.zeroFloorSub(totalBorrowAssets, MathLib.wMulDown(totalSupplyAssets, utilization));
|
|
124
|
+
}
|
|
125
|
+
MarketUtils.getRepayToUtilization = getRepayToUtilization;
|
|
126
|
+
function getCollateralPower(collateral, { lltv }) {
|
|
127
|
+
return MathLib.wMulDown(collateral, lltv);
|
|
128
|
+
}
|
|
129
|
+
MarketUtils.getCollateralPower = getCollateralPower;
|
|
130
|
+
/**
|
|
131
|
+
* Returns the value of a given amount of collateral quoted in loan assets.
|
|
132
|
+
* Return `undefined` iff the market's price is undefined.
|
|
133
|
+
*/
|
|
134
|
+
function getCollateralValue(collateral, { price }) {
|
|
135
|
+
if (price == null)
|
|
136
|
+
return;
|
|
137
|
+
return MathLib.mulDivDown(collateral, price, ORACLE_PRICE_SCALE);
|
|
138
|
+
}
|
|
139
|
+
MarketUtils.getCollateralValue = getCollateralValue;
|
|
140
|
+
/**
|
|
141
|
+
* Returns the maximum debt allowed given a certain amount of collateral.
|
|
142
|
+
* Return `undefined` iff the market's price is undefined.
|
|
143
|
+
* To calculate the amount of loan assets that can be borrowed, use `getMaxBorrowableAssets`.
|
|
144
|
+
*/
|
|
145
|
+
function getMaxBorrowAssets(collateral, market, { lltv }) {
|
|
146
|
+
const collateralValue = getCollateralValue(collateral, market);
|
|
147
|
+
if (collateralValue == null)
|
|
148
|
+
return;
|
|
149
|
+
return MathLib.wMulDown(collateralValue, lltv);
|
|
150
|
+
}
|
|
151
|
+
MarketUtils.getMaxBorrowAssets = getMaxBorrowAssets;
|
|
152
|
+
/**
|
|
153
|
+
* Returns the maximum amount of loan assets that can be borrowed given a certain borrow position.
|
|
154
|
+
* Return `undefined` iff the market's price is undefined.
|
|
155
|
+
*/
|
|
156
|
+
function getMaxBorrowableAssets({ collateral, borrowShares, }, market, marketParams) {
|
|
157
|
+
const maxBorrowAssets = getMaxBorrowAssets(collateral, market, marketParams);
|
|
158
|
+
if (maxBorrowAssets == null)
|
|
159
|
+
return;
|
|
160
|
+
return MathLib.zeroFloorSub(maxBorrowAssets, toBorrowAssets(borrowShares, market));
|
|
161
|
+
}
|
|
162
|
+
MarketUtils.getMaxBorrowableAssets = getMaxBorrowableAssets;
|
|
163
|
+
/**
|
|
164
|
+
* Returns the amount of collateral that would be seized in a liquidation given a certain amount of repaid shares.
|
|
165
|
+
* Return `undefined` iff the market's price is undefined.
|
|
166
|
+
*/
|
|
167
|
+
function getLiquidationSeizedAssets(repaidShares, market, config) {
|
|
168
|
+
if (market.price == null)
|
|
169
|
+
return;
|
|
170
|
+
market.price = BigInt(market.price);
|
|
171
|
+
if (market.price === 0n)
|
|
172
|
+
return 0n;
|
|
173
|
+
return MathLib.mulDivDown(MathLib.wMulDown(toBorrowAssets(repaidShares, market, "Down"), getLiquidationIncentiveFactor(config)), ORACLE_PRICE_SCALE, market.price);
|
|
174
|
+
}
|
|
175
|
+
MarketUtils.getLiquidationSeizedAssets = getLiquidationSeizedAssets;
|
|
176
|
+
/**
|
|
177
|
+
* Returns the amount of borrow shares that would be repaid in a liquidation given a certain amount of seized collateral.
|
|
178
|
+
* Return `undefined` iff the market's price is undefined.
|
|
179
|
+
*/
|
|
180
|
+
function getLiquidationRepaidShares(seizedAssets, market, config) {
|
|
181
|
+
if (market.price == null)
|
|
182
|
+
return;
|
|
183
|
+
return toBorrowShares(MathLib.wDivUp(MathLib.mulDivUp(seizedAssets, market.price, ORACLE_PRICE_SCALE), getLiquidationIncentiveFactor(config)), market, "Up");
|
|
184
|
+
}
|
|
185
|
+
MarketUtils.getLiquidationRepaidShares = getLiquidationRepaidShares;
|
|
186
|
+
/**
|
|
187
|
+
* Returns the maximum amount of collateral that is worth being seized in a liquidation given a certain borrow position.
|
|
188
|
+
* Return `undefined` iff the market's price is undefined.
|
|
189
|
+
*/
|
|
190
|
+
function getSeizableCollateral(position, market, config) {
|
|
191
|
+
if (market.price == null)
|
|
192
|
+
return; // Must be checked before calling `isHealthy`.
|
|
193
|
+
market.price = BigInt(market.price);
|
|
194
|
+
if (market.price === 0n || isHealthy(position, market, config))
|
|
195
|
+
return 0n;
|
|
196
|
+
return MathLib.min(position.collateral, getLiquidationSeizedAssets(position.borrowShares, market, config));
|
|
197
|
+
}
|
|
198
|
+
MarketUtils.getSeizableCollateral = getSeizableCollateral;
|
|
199
|
+
/**
|
|
200
|
+
* Returns the amount of collateral that can be withdrawn given a certain borrow position.
|
|
201
|
+
* Return `undefined` iff the market's price is undefined.
|
|
202
|
+
*/
|
|
203
|
+
function getWithdrawableCollateral({ collateral, borrowShares, }, market, { lltv }) {
|
|
204
|
+
if (market.price == null)
|
|
205
|
+
return;
|
|
206
|
+
market.price = BigInt(market.price);
|
|
207
|
+
if (market.price === 0n)
|
|
208
|
+
return 0n;
|
|
209
|
+
return MathLib.zeroFloorSub(collateral, MathLib.wDivUp(MathLib.mulDivUp(toBorrowAssets(borrowShares, market), ORACLE_PRICE_SCALE, market.price), lltv));
|
|
210
|
+
}
|
|
211
|
+
MarketUtils.getWithdrawableCollateral = getWithdrawableCollateral;
|
|
212
|
+
/**
|
|
213
|
+
* Returns whether a given borrow position is healthy.
|
|
214
|
+
* Return `undefined` iff the market's price is undefined.
|
|
215
|
+
* @param position The borrow position to check.
|
|
216
|
+
*/
|
|
217
|
+
function isHealthy({ collateral, borrowShares, }, market, marketParams) {
|
|
218
|
+
const maxBorrowAssets = getMaxBorrowAssets(collateral, market, marketParams);
|
|
219
|
+
if (maxBorrowAssets == null)
|
|
220
|
+
return;
|
|
221
|
+
return maxBorrowAssets >= toBorrowAssets(borrowShares, market);
|
|
222
|
+
}
|
|
223
|
+
MarketUtils.isHealthy = isHealthy;
|
|
224
|
+
/**
|
|
225
|
+
* Returns the price of the collateral quoted in the loan token (e.g. ETH/DAI)
|
|
226
|
+
* that set the user's position to be liquidatable.
|
|
227
|
+
* Returns null if the position is not a borrow.
|
|
228
|
+
*/
|
|
229
|
+
function getLiquidationPrice({ collateral, borrowShares, }, market, marketParams) {
|
|
230
|
+
borrowShares = BigInt(borrowShares);
|
|
231
|
+
market.totalBorrowShares = BigInt(market.totalBorrowShares);
|
|
232
|
+
if (borrowShares === 0n || market.totalBorrowShares === 0n)
|
|
233
|
+
return null;
|
|
234
|
+
const collateralPower = getCollateralPower(collateral, marketParams);
|
|
235
|
+
if (collateralPower === 0n)
|
|
236
|
+
return MathLib.MAX_UINT_256;
|
|
237
|
+
const borrowAssets = toBorrowAssets(borrowShares, market);
|
|
238
|
+
return MathLib.mulDivUp(borrowAssets, ORACLE_PRICE_SCALE, collateralPower);
|
|
239
|
+
}
|
|
240
|
+
MarketUtils.getLiquidationPrice = getLiquidationPrice;
|
|
241
|
+
/**
|
|
242
|
+
* Returns the price variation required for the given position to reach its liquidation threshold (scaled by WAD).
|
|
243
|
+
* Negative when healthy (the price needs to drop x%), positive when unhealthy (the price needs to soar x%).
|
|
244
|
+
* Returns `undefined` iff the market's price is undefined.
|
|
245
|
+
* Returns null if the position is not a borrow.
|
|
246
|
+
*/
|
|
247
|
+
function getPriceVariationToLiquidationPrice(position, market, marketParams) {
|
|
248
|
+
if (market.price == null)
|
|
249
|
+
return;
|
|
250
|
+
market.price = BigInt(market.price);
|
|
251
|
+
if (market.price === 0n)
|
|
252
|
+
return null;
|
|
253
|
+
const liquidationPrice = getLiquidationPrice(position, market, marketParams);
|
|
254
|
+
if (liquidationPrice == null)
|
|
255
|
+
return null;
|
|
256
|
+
return MathLib.wDivUp(liquidationPrice, market.price) - MathLib.WAD;
|
|
257
|
+
}
|
|
258
|
+
MarketUtils.getPriceVariationToLiquidationPrice = getPriceVariationToLiquidationPrice;
|
|
259
|
+
/**
|
|
260
|
+
* Returns the health factor of a given borrow position (scaled by WAD).
|
|
261
|
+
* If the debt is 0, health factor is `MaxUint256`.
|
|
262
|
+
* Returns `undefined` iff the market's price is undefined.
|
|
263
|
+
*/
|
|
264
|
+
function getHealthFactor({ collateral, borrowShares, }, market, marketParams) {
|
|
265
|
+
const borrowAssets = toBorrowAssets(borrowShares, market);
|
|
266
|
+
if (borrowAssets === 0n)
|
|
267
|
+
return MathLib.MAX_UINT_256;
|
|
268
|
+
const maxBorrowAssets = getMaxBorrowAssets(collateral, market, marketParams);
|
|
269
|
+
if (maxBorrowAssets == null)
|
|
270
|
+
return;
|
|
271
|
+
return MathLib.wDivDown(maxBorrowAssets, borrowAssets);
|
|
272
|
+
}
|
|
273
|
+
MarketUtils.getHealthFactor = getHealthFactor;
|
|
274
|
+
/**
|
|
275
|
+
* Returns the loan-to-value ratio of a given borrow position (scaled by WAD).
|
|
276
|
+
* Returns `undefined` iff the market's price is undefined.
|
|
277
|
+
* Returns null if the position is not a borrow.
|
|
278
|
+
*/
|
|
279
|
+
function getLtv({ collateral, borrowShares, }, market) {
|
|
280
|
+
borrowShares = BigInt(borrowShares);
|
|
281
|
+
market.totalBorrowShares = BigInt(market.totalBorrowShares);
|
|
282
|
+
if (borrowShares === 0n || market.totalBorrowShares === 0n)
|
|
283
|
+
return null;
|
|
284
|
+
const collateralValue = getCollateralValue(collateral, market);
|
|
285
|
+
if (collateralValue == null)
|
|
286
|
+
return;
|
|
287
|
+
if (collateralValue === 0n)
|
|
288
|
+
return MathLib.MAX_UINT_256;
|
|
289
|
+
return MathLib.wDivUp(toBorrowAssets(borrowShares, market), collateralValue);
|
|
290
|
+
}
|
|
291
|
+
MarketUtils.getLtv = getLtv;
|
|
292
|
+
/**
|
|
293
|
+
* Returns the usage ratio of the maximum borrow capacity given a certain borrow position (scaled by WAD).
|
|
294
|
+
* Returns `undefined` iff the market's price is undefined.
|
|
295
|
+
*/
|
|
296
|
+
function getBorrowCapacityUsage(position, market, marketParams) {
|
|
297
|
+
const hf = getHealthFactor(position, market, marketParams);
|
|
298
|
+
if (hf === undefined)
|
|
299
|
+
return;
|
|
300
|
+
if (hf === null)
|
|
301
|
+
return 0n;
|
|
302
|
+
if (hf === 0n)
|
|
303
|
+
return MathLib.MAX_UINT_256;
|
|
304
|
+
return MathLib.wDivUp(MathLib.WAD, hf);
|
|
305
|
+
}
|
|
306
|
+
MarketUtils.getBorrowCapacityUsage = getBorrowCapacityUsage;
|
|
307
|
+
function toSupplyAssets(shares, market, rounding = "Down") {
|
|
308
|
+
return SharesMath.toAssets(shares, market.totalSupplyAssets, market.totalSupplyShares, rounding);
|
|
309
|
+
}
|
|
310
|
+
MarketUtils.toSupplyAssets = toSupplyAssets;
|
|
311
|
+
function toSupplyShares(assets, market, rounding = "Up") {
|
|
312
|
+
return SharesMath.toShares(assets, market.totalSupplyAssets, market.totalSupplyShares, rounding);
|
|
313
|
+
}
|
|
314
|
+
MarketUtils.toSupplyShares = toSupplyShares;
|
|
315
|
+
function toBorrowAssets(shares, market, rounding = "Up") {
|
|
316
|
+
return SharesMath.toAssets(shares, market.totalBorrowAssets, market.totalBorrowShares, rounding);
|
|
317
|
+
}
|
|
318
|
+
MarketUtils.toBorrowAssets = toBorrowAssets;
|
|
319
|
+
function toBorrowShares(assets, market, rounding = "Down") {
|
|
320
|
+
return SharesMath.toShares(assets, market.totalBorrowAssets, market.totalBorrowShares, rounding);
|
|
321
|
+
}
|
|
322
|
+
MarketUtils.toBorrowShares = toBorrowShares;
|
|
323
|
+
})(MarketUtils || (MarketUtils = {}));
|