@morpho-org/blue-sdk 1.0.0-integ-650.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.
Files changed (86) hide show
  1. package/README.md +106 -0
  2. package/lib/addresses.d.ts +113 -0
  3. package/lib/addresses.js +162 -0
  4. package/lib/chain.d.ts +29 -0
  5. package/lib/chain.js +289 -0
  6. package/lib/chain.test.d.ts +1 -0
  7. package/lib/chain.test.js +21 -0
  8. package/lib/constants.d.ts +29 -0
  9. package/lib/constants.js +33 -0
  10. package/lib/errors.d.ts +58 -0
  11. package/lib/errors.js +119 -0
  12. package/lib/helpers/format/format.d.ts +122 -0
  13. package/lib/helpers/format/format.js +286 -0
  14. package/lib/helpers/format/format.test.d.ts +1 -0
  15. package/lib/helpers/format/format.test.js +255 -0
  16. package/lib/helpers/format/index.d.ts +1 -0
  17. package/lib/helpers/format/index.js +17 -0
  18. package/lib/helpers/index.d.ts +2 -0
  19. package/lib/helpers/index.js +18 -0
  20. package/lib/helpers/locale.d.ts +46 -0
  21. package/lib/helpers/locale.js +96 -0
  22. package/lib/holding/AssetBalances.d.ts +48 -0
  23. package/lib/holding/AssetBalances.js +42 -0
  24. package/lib/holding/Holding.d.ts +59 -0
  25. package/lib/holding/Holding.js +59 -0
  26. package/lib/holding/index.d.ts +2 -0
  27. package/lib/holding/index.js +18 -0
  28. package/lib/index.d.ts +25 -0
  29. package/lib/index.js +54 -0
  30. package/lib/market/Market.d.ts +329 -0
  31. package/lib/market/Market.js +460 -0
  32. package/lib/market/MarketConfig.d.ts +52 -0
  33. package/lib/market/MarketConfig.js +76 -0
  34. package/lib/market/MarketUtils.d.ts +232 -0
  35. package/lib/market/MarketUtils.js +263 -0
  36. package/lib/market/MarketUtils.test.d.ts +1 -0
  37. package/lib/market/MarketUtils.test.js +38 -0
  38. package/lib/market/index.d.ts +3 -0
  39. package/lib/market/index.js +19 -0
  40. package/lib/maths/AdaptiveCurveIrmLib.d.ts +39 -0
  41. package/lib/maths/AdaptiveCurveIrmLib.js +134 -0
  42. package/lib/maths/MathLib.d.ts +114 -0
  43. package/lib/maths/MathLib.js +175 -0
  44. package/lib/maths/SharesMath.d.ts +12 -0
  45. package/lib/maths/SharesMath.js +21 -0
  46. package/lib/maths/index.d.ts +3 -0
  47. package/lib/maths/index.js +19 -0
  48. package/lib/position/Position.d.ts +126 -0
  49. package/lib/position/Position.js +203 -0
  50. package/lib/position/index.d.ts +1 -0
  51. package/lib/position/index.js +17 -0
  52. package/lib/tests/mocks/markets.d.ts +19 -0
  53. package/lib/tests/mocks/markets.js +121 -0
  54. package/lib/token/ConstantWrappedToken.d.ts +18 -0
  55. package/lib/token/ConstantWrappedToken.js +36 -0
  56. package/lib/token/ExchangeRateWrappedToken.d.ts +11 -0
  57. package/lib/token/ExchangeRateWrappedToken.js +21 -0
  58. package/lib/token/Token.d.ts +46 -0
  59. package/lib/token/Token.js +64 -0
  60. package/lib/token/VaultToken.d.ts +14 -0
  61. package/lib/token/VaultToken.js +23 -0
  62. package/lib/token/WrappedToken.d.ts +17 -0
  63. package/lib/token/WrappedToken.js +33 -0
  64. package/lib/token/index.d.ts +5 -0
  65. package/lib/token/index.js +21 -0
  66. package/lib/types.d.ts +30 -0
  67. package/lib/types.js +22 -0
  68. package/lib/user/User.d.ts +20 -0
  69. package/lib/user/User.js +23 -0
  70. package/lib/user/index.d.ts +1 -0
  71. package/lib/user/index.js +17 -0
  72. package/lib/vault/Vault.d.ts +166 -0
  73. package/lib/vault/Vault.js +234 -0
  74. package/lib/vault/VaultConfig.d.ts +22 -0
  75. package/lib/vault/VaultConfig.js +32 -0
  76. package/lib/vault/VaultMarketAllocation.d.ts +20 -0
  77. package/lib/vault/VaultMarketAllocation.js +30 -0
  78. package/lib/vault/VaultMarketConfig.d.ts +43 -0
  79. package/lib/vault/VaultMarketConfig.js +43 -0
  80. package/lib/vault/VaultMarketPublicAllocatorConfig.d.ts +29 -0
  81. package/lib/vault/VaultMarketPublicAllocatorConfig.js +28 -0
  82. package/lib/vault/VaultUtils.d.ts +18 -0
  83. package/lib/vault/VaultUtils.js +20 -0
  84. package/lib/vault/index.d.ts +6 -0
  85. package/lib/vault/index.js +22 -0
  86. package/package.json +53 -0
@@ -0,0 +1,232 @@
1
+ import { RoundingDirection } from "../maths";
2
+ import { BigIntish, MarketId } from "../types";
3
+ import { MarketParams } from "./MarketConfig";
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: MarketParams): 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 rate at which interest accrued on average for suppliers on the corresponding market,
30
+ * since the last time the market was updated (scaled by WAD).
31
+ * @param borrowRate The average borrow rate since the last market update (scaled by WAD).
32
+ * @param market The market state.
33
+ */
34
+ function getSupplyRate(borrowRate: BigIntish, { utilization, fee }: {
35
+ utilization: BigIntish;
36
+ fee: BigIntish;
37
+ }): bigint;
38
+ /**
39
+ * Returns the Annual Percentage Yield (APY) from an average rate, as calculated in Morpho Blue.
40
+ * @param rate The average rate to convert to APY (scaled by WAD).
41
+ */
42
+ function getApy(rate: BigIntish): bigint;
43
+ /**
44
+ * Returns the interest accrued on both sides of the given market
45
+ * as well as the supply shares minted to the fee recipient.
46
+ * @param borrowRate The average borrow rate since the last market update (scaled by WAD).
47
+ * @param market The market state.
48
+ * @param elapsed The time elapsed since the last market update (in seconds).
49
+ */
50
+ function getAccruedInterest(borrowRate: BigIntish, { totalSupplyAssets, totalBorrowAssets, totalSupplyShares, fee, }: {
51
+ totalSupplyAssets: BigIntish;
52
+ totalBorrowAssets: BigIntish;
53
+ totalSupplyShares: BigIntish;
54
+ fee: BigIntish;
55
+ }, elapsed?: bigint): {
56
+ interest: bigint;
57
+ feeShares: bigint;
58
+ };
59
+ /**
60
+ * Returns the smallest volume to supply 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 getSupplyToUtilization(market: {
65
+ totalSupplyAssets: BigIntish;
66
+ totalBorrowAssets: BigIntish;
67
+ }, utilization: BigIntish): bigint;
68
+ /**
69
+ * Returns the liquidity available to withdraw 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 getWithdrawToUtilization({ totalSupplyAssets, totalBorrowAssets, }: {
74
+ totalSupplyAssets: BigIntish;
75
+ totalBorrowAssets: BigIntish;
76
+ }, utilization: BigIntish): bigint;
77
+ /**
78
+ * Returns the liquidity available to borrow 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 getBorrowToUtilization({ totalSupplyAssets, totalBorrowAssets, }: {
83
+ totalSupplyAssets: BigIntish;
84
+ totalBorrowAssets: BigIntish;
85
+ }, utilization: BigIntish): bigint;
86
+ /**
87
+ * Returns the smallest volume to repay until the market gets the closest to the given utilization rate.
88
+ * @param market The market state.
89
+ * @param utilization The target utilization rate (scaled by WAD).
90
+ */
91
+ function getRepayToUtilization({ totalSupplyAssets, totalBorrowAssets, }: {
92
+ totalSupplyAssets: BigIntish;
93
+ totalBorrowAssets: BigIntish;
94
+ }, utilization: BigIntish): bigint;
95
+ function getCollateralPower(collateral: BigIntish, { lltv }: {
96
+ lltv: BigIntish;
97
+ }): bigint;
98
+ function getCollateralValue(collateral: BigIntish, { price }: {
99
+ price: BigIntish;
100
+ }): bigint;
101
+ function getMaxBorrowAssets(collateral: BigIntish, market: {
102
+ price: BigIntish;
103
+ }, { lltv }: {
104
+ lltv: BigIntish;
105
+ }): bigint;
106
+ function getMaxBorrowableAssets({ collateral, borrowShares, }: {
107
+ collateral: BigIntish;
108
+ borrowShares: BigIntish;
109
+ }, market: {
110
+ totalBorrowAssets: BigIntish;
111
+ totalBorrowShares: BigIntish;
112
+ price: BigIntish;
113
+ }, marketConfig: {
114
+ lltv: BigIntish;
115
+ }): bigint;
116
+ function getLiquidationSeizedAssets(repaidShares: BigIntish, market: {
117
+ totalBorrowAssets: BigIntish;
118
+ totalBorrowShares: BigIntish;
119
+ price: BigIntish;
120
+ }, config: {
121
+ lltv: BigIntish;
122
+ }): bigint;
123
+ function getLiquidationRepaidShares(seizedAssets: BigIntish, market: {
124
+ totalBorrowAssets: BigIntish;
125
+ totalBorrowShares: BigIntish;
126
+ price: BigIntish;
127
+ }, config: {
128
+ lltv: BigIntish;
129
+ }): bigint;
130
+ function getSeizableCollateral(position: {
131
+ collateral: BigIntish;
132
+ borrowShares: BigIntish;
133
+ }, market: {
134
+ totalBorrowAssets: BigIntish;
135
+ totalBorrowShares: BigIntish;
136
+ price: BigIntish;
137
+ }, config: {
138
+ lltv: BigIntish;
139
+ }): bigint;
140
+ function getWithdrawableCollateral({ collateral, borrowShares, }: {
141
+ collateral: BigIntish;
142
+ borrowShares: BigIntish;
143
+ }, market: {
144
+ totalBorrowAssets: BigIntish;
145
+ totalBorrowShares: BigIntish;
146
+ price: BigIntish;
147
+ }, { lltv }: {
148
+ lltv: BigIntish;
149
+ }): bigint;
150
+ function isHealthy({ collateral, borrowShares, }: {
151
+ collateral: BigIntish;
152
+ borrowShares: BigIntish;
153
+ }, market: {
154
+ totalBorrowAssets: BigIntish;
155
+ totalBorrowShares: BigIntish;
156
+ price: BigIntish;
157
+ }, marketConfig: {
158
+ lltv: BigIntish;
159
+ }): boolean;
160
+ /**
161
+ * Returns the price of the collateral quoted in the loan token (e.g. ETH/DAI)
162
+ * that set the user's position to be liquidatable.
163
+ * Returns null if the user is not a borrower
164
+ */
165
+ function getLiquidationPrice({ collateral, borrowShares, }: {
166
+ collateral: BigIntish;
167
+ borrowShares: BigIntish;
168
+ }, market: {
169
+ totalBorrowAssets: BigIntish;
170
+ totalBorrowShares: BigIntish;
171
+ }, marketConfig: {
172
+ lltv: BigIntish;
173
+ }): bigint | null;
174
+ /**
175
+ * Returns the price deviation required for the given borrow position to be unhealthy (scaled by WAD).
176
+ * @param position The borrow position to consider.
177
+ */
178
+ function getPriceVariationToLiquidation(position: {
179
+ collateral: BigIntish;
180
+ borrowShares: BigIntish;
181
+ }, market: {
182
+ totalBorrowAssets: BigIntish;
183
+ totalBorrowShares: BigIntish;
184
+ price: BigIntish;
185
+ }, marketConfig: {
186
+ lltv: BigIntish;
187
+ }): bigint | null;
188
+ function getHealthFactor({ collateral, borrowShares, }: {
189
+ collateral: BigIntish;
190
+ borrowShares: BigIntish;
191
+ }, market: {
192
+ totalBorrowAssets: BigIntish;
193
+ totalBorrowShares: BigIntish;
194
+ price: BigIntish;
195
+ }, marketConfig: {
196
+ lltv: BigIntish;
197
+ }): bigint | null;
198
+ function getLtv({ collateral, borrowShares, }: {
199
+ collateral: BigIntish;
200
+ borrowShares: BigIntish;
201
+ }, market: {
202
+ totalBorrowAssets: BigIntish;
203
+ totalBorrowShares: BigIntish;
204
+ price: BigIntish;
205
+ }): bigint | null;
206
+ function getBorrowCapacityUsage(position: {
207
+ collateral: BigIntish;
208
+ borrowShares: BigIntish;
209
+ }, market: {
210
+ totalBorrowAssets: BigIntish;
211
+ totalBorrowShares: BigIntish;
212
+ price: BigIntish;
213
+ }, marketConfig: {
214
+ lltv: BigIntish;
215
+ }): bigint | null;
216
+ function toSupplyAssets(shares: BigIntish, market: {
217
+ totalSupplyAssets: BigIntish;
218
+ totalSupplyShares: BigIntish;
219
+ }, rounding?: RoundingDirection): bigint;
220
+ function toSupplyShares(assets: BigIntish, market: {
221
+ totalSupplyAssets: BigIntish;
222
+ totalSupplyShares: BigIntish;
223
+ }, rounding?: RoundingDirection): bigint;
224
+ function toBorrowAssets(shares: BigIntish, market: {
225
+ totalBorrowAssets: BigIntish;
226
+ totalBorrowShares: BigIntish;
227
+ }, rounding?: RoundingDirection): bigint;
228
+ function toBorrowShares(assets: BigIntish, market: {
229
+ totalBorrowAssets: BigIntish;
230
+ totalBorrowShares: BigIntish;
231
+ }, rounding?: RoundingDirection): bigint;
232
+ }
@@ -0,0 +1,263 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.MarketUtils = void 0;
7
+ const keccak256_1 = __importDefault(require("keccak256"));
8
+ const constants_1 = require("../constants");
9
+ const maths_1 = require("../maths");
10
+ /**
11
+ * Namespace of utility functions to ease market-related calculations.
12
+ */
13
+ var MarketUtils;
14
+ (function (MarketUtils) {
15
+ /**
16
+ * Returns the id of a market based on its params.
17
+ * @param market The market params.
18
+ */
19
+ function getMarketId(market) {
20
+ return `0x${(0, keccak256_1.default)("0x" +
21
+ market.loanToken.substring(2).toLowerCase().padStart(64, "0") +
22
+ market.collateralToken.substring(2).toLowerCase().padStart(64, "0") +
23
+ market.oracle.substring(2).padStart(64, "0") +
24
+ market.irm.substring(2).toLowerCase().padStart(64, "0") +
25
+ BigInt(market.lltv).toString(16).padStart(64, "0")).toString("hex")}`;
26
+ }
27
+ MarketUtils.getMarketId = getMarketId;
28
+ /**
29
+ * Returns the liquidation incentive factor for a given market params.
30
+ * @param config The market params.
31
+ */
32
+ function getLiquidationIncentiveFactor({ lltv }) {
33
+ return maths_1.MathLib.min(constants_1.MAX_LIQUIDATION_INCENTIVE_FACTOR, maths_1.MathLib.wDivDown(maths_1.MathLib.WAD, maths_1.MathLib.WAD -
34
+ maths_1.MathLib.wMulDown(constants_1.LIQUIDATION_CURSOR, maths_1.MathLib.WAD - BigInt(lltv))));
35
+ }
36
+ MarketUtils.getLiquidationIncentiveFactor = getLiquidationIncentiveFactor;
37
+ /**
38
+ * Returns the market's utilization rate (scaled by WAD).
39
+ * @param market The market state.
40
+ */
41
+ function getUtilization({ totalSupplyAssets, totalBorrowAssets, }) {
42
+ totalSupplyAssets = BigInt(totalSupplyAssets);
43
+ totalBorrowAssets = BigInt(totalBorrowAssets);
44
+ if (totalSupplyAssets === 0n) {
45
+ if (totalBorrowAssets > 0n)
46
+ return maths_1.MathLib.MAX_UINT_256;
47
+ return 0n;
48
+ }
49
+ return maths_1.MathLib.wDivDown(totalBorrowAssets, totalSupplyAssets);
50
+ }
51
+ MarketUtils.getUtilization = getUtilization;
52
+ /**
53
+ * Returns the rate at which interest accrued on average for suppliers on the corresponding market,
54
+ * since the last time the market was updated (scaled by WAD).
55
+ * @param borrowRate The average borrow rate since the last market update (scaled by WAD).
56
+ * @param market The market state.
57
+ */
58
+ function getSupplyRate(borrowRate, { utilization, fee }) {
59
+ const borrowRateWithoutFees = maths_1.MathLib.wMulUp(borrowRate, utilization);
60
+ return maths_1.MathLib.wMulUp(borrowRateWithoutFees, maths_1.MathLib.WAD - BigInt(fee));
61
+ }
62
+ MarketUtils.getSupplyRate = getSupplyRate;
63
+ /**
64
+ * Returns the Annual Percentage Yield (APY) from an average rate, as calculated in Morpho Blue.
65
+ * @param rate The average rate to convert to APY (scaled by WAD).
66
+ */
67
+ function getApy(rate) {
68
+ return maths_1.MathLib.wTaylorCompounded(rate, constants_1.SECONDS_PER_YEAR);
69
+ }
70
+ MarketUtils.getApy = getApy;
71
+ /**
72
+ * Returns the interest accrued on both sides of the given market
73
+ * as well as the supply shares minted to the fee recipient.
74
+ * @param borrowRate The average borrow rate since the last market update (scaled by WAD).
75
+ * @param market The market state.
76
+ * @param elapsed The time elapsed since the last market update (in seconds).
77
+ */
78
+ function getAccruedInterest(borrowRate, { totalSupplyAssets, totalBorrowAssets, totalSupplyShares, fee, }, elapsed = 0n) {
79
+ const interest = maths_1.MathLib.wMulDown(totalBorrowAssets, maths_1.MathLib.wTaylorCompounded(borrowRate, elapsed));
80
+ const feeAmount = maths_1.MathLib.wMulDown(interest, fee);
81
+ const feeShares = toSupplyShares(feeAmount, {
82
+ totalSupplyAssets: BigInt(totalSupplyAssets) - feeAmount,
83
+ totalSupplyShares,
84
+ }, "Down");
85
+ return { interest, feeShares };
86
+ }
87
+ MarketUtils.getAccruedInterest = getAccruedInterest;
88
+ /**
89
+ * Returns the smallest volume to supply until the market gets the closest to the given utilization rate.
90
+ * @param market The market state.
91
+ * @param utilization The target utilization rate (scaled by WAD).
92
+ */
93
+ function getSupplyToUtilization(market, utilization) {
94
+ utilization = BigInt(utilization);
95
+ if (utilization === 0n) {
96
+ if (getUtilization(market) === 0n)
97
+ return 0n;
98
+ return maths_1.MathLib.MAX_UINT_256;
99
+ }
100
+ return maths_1.MathLib.zeroFloorSub(maths_1.MathLib.wDivUp(market.totalBorrowAssets, utilization), market.totalSupplyAssets);
101
+ }
102
+ MarketUtils.getSupplyToUtilization = getSupplyToUtilization;
103
+ /**
104
+ * Returns the liquidity available to withdraw until the market gets the closest to the given utilization rate.
105
+ * @param market The market state.
106
+ * @param utilization The target utilization rate (scaled by WAD).
107
+ */
108
+ function getWithdrawToUtilization({ totalSupplyAssets, totalBorrowAssets, }, utilization) {
109
+ utilization = BigInt(utilization);
110
+ totalSupplyAssets = BigInt(totalSupplyAssets);
111
+ totalBorrowAssets = BigInt(totalBorrowAssets);
112
+ if (utilization === 0n) {
113
+ if (totalBorrowAssets === 0n)
114
+ return totalSupplyAssets;
115
+ return 0n;
116
+ }
117
+ return maths_1.MathLib.zeroFloorSub(totalSupplyAssets, maths_1.MathLib.wDivUp(totalBorrowAssets, utilization));
118
+ }
119
+ MarketUtils.getWithdrawToUtilization = getWithdrawToUtilization;
120
+ /**
121
+ * Returns the liquidity available to borrow until the market gets the closest to the given utilization rate.
122
+ * @param market The market state.
123
+ * @param utilization The target utilization rate (scaled by WAD).
124
+ */
125
+ function getBorrowToUtilization({ totalSupplyAssets, totalBorrowAssets, }, utilization) {
126
+ return maths_1.MathLib.zeroFloorSub(maths_1.MathLib.wMulDown(totalSupplyAssets, utilization), totalBorrowAssets);
127
+ }
128
+ MarketUtils.getBorrowToUtilization = getBorrowToUtilization;
129
+ /**
130
+ * Returns the smallest volume to repay until the market gets the closest to the given utilization rate.
131
+ * @param market The market state.
132
+ * @param utilization The target utilization rate (scaled by WAD).
133
+ */
134
+ function getRepayToUtilization({ totalSupplyAssets, totalBorrowAssets, }, utilization) {
135
+ return maths_1.MathLib.zeroFloorSub(totalBorrowAssets, maths_1.MathLib.wMulDown(totalSupplyAssets, utilization));
136
+ }
137
+ MarketUtils.getRepayToUtilization = getRepayToUtilization;
138
+ function getCollateralPower(collateral, { lltv }) {
139
+ return maths_1.MathLib.wMulDown(collateral, lltv);
140
+ }
141
+ MarketUtils.getCollateralPower = getCollateralPower;
142
+ function getCollateralValue(collateral, { price }) {
143
+ return maths_1.MathLib.mulDivDown(collateral, price, constants_1.ORACLE_PRICE_SCALE);
144
+ }
145
+ MarketUtils.getCollateralValue = getCollateralValue;
146
+ function getMaxBorrowAssets(collateral, market, { lltv }) {
147
+ return maths_1.MathLib.wMulDown(getCollateralValue(collateral, market), lltv);
148
+ }
149
+ MarketUtils.getMaxBorrowAssets = getMaxBorrowAssets;
150
+ function getMaxBorrowableAssets({ collateral, borrowShares, }, market, marketConfig) {
151
+ return maths_1.MathLib.zeroFloorSub(getMaxBorrowAssets(collateral, market, marketConfig), toBorrowAssets(borrowShares, market));
152
+ }
153
+ MarketUtils.getMaxBorrowableAssets = getMaxBorrowableAssets;
154
+ function getLiquidationSeizedAssets(repaidShares, market, config) {
155
+ market.price = BigInt(market.price);
156
+ if (market.price === 0n)
157
+ return 0n;
158
+ return maths_1.MathLib.mulDivDown(maths_1.MathLib.wMulDown(toBorrowAssets(repaidShares, market, "Down"), getLiquidationIncentiveFactor(config)), constants_1.ORACLE_PRICE_SCALE, market.price);
159
+ }
160
+ MarketUtils.getLiquidationSeizedAssets = getLiquidationSeizedAssets;
161
+ function getLiquidationRepaidShares(seizedAssets, market, config) {
162
+ return toBorrowShares(maths_1.MathLib.wDivUp(maths_1.MathLib.mulDivUp(seizedAssets, market.price, constants_1.ORACLE_PRICE_SCALE), getLiquidationIncentiveFactor(config)), market, "Up");
163
+ }
164
+ MarketUtils.getLiquidationRepaidShares = getLiquidationRepaidShares;
165
+ function getSeizableCollateral(position, market, config) {
166
+ market.price = BigInt(market.price);
167
+ if (market.price === 0n || isHealthy(position, market, config))
168
+ return 0n;
169
+ return maths_1.MathLib.min(position.collateral, getLiquidationSeizedAssets(position.borrowShares, market, config));
170
+ }
171
+ MarketUtils.getSeizableCollateral = getSeizableCollateral;
172
+ function getWithdrawableCollateral({ collateral, borrowShares, }, market, { lltv }) {
173
+ market.price = BigInt(market.price);
174
+ if (market.price === 0n)
175
+ return 0n;
176
+ return maths_1.MathLib.zeroFloorSub(collateral, maths_1.MathLib.wDivUp(maths_1.MathLib.mulDivUp(toBorrowAssets(borrowShares, market), constants_1.ORACLE_PRICE_SCALE, market.price), lltv));
177
+ }
178
+ MarketUtils.getWithdrawableCollateral = getWithdrawableCollateral;
179
+ function isHealthy({ collateral, borrowShares, }, market, marketConfig) {
180
+ return (getMaxBorrowAssets(collateral, market, marketConfig) >=
181
+ toBorrowAssets(borrowShares, market));
182
+ }
183
+ MarketUtils.isHealthy = isHealthy;
184
+ /**
185
+ * Returns the price of the collateral quoted in the loan token (e.g. ETH/DAI)
186
+ * that set the user's position to be liquidatable.
187
+ * Returns null if the user is not a borrower
188
+ */
189
+ function getLiquidationPrice({ collateral, borrowShares, }, market, marketConfig) {
190
+ borrowShares = BigInt(borrowShares);
191
+ market.totalBorrowShares = BigInt(market.totalBorrowShares);
192
+ if (borrowShares === 0n || market.totalBorrowShares === 0n)
193
+ return null;
194
+ const collateralPower = getCollateralPower(collateral, marketConfig);
195
+ if (collateralPower === 0n)
196
+ return maths_1.MathLib.MAX_UINT_256;
197
+ const borrowAssets = toBorrowAssets(borrowShares, market);
198
+ return maths_1.MathLib.mulDivUp(borrowAssets, constants_1.ORACLE_PRICE_SCALE, collateralPower);
199
+ }
200
+ MarketUtils.getLiquidationPrice = getLiquidationPrice;
201
+ /**
202
+ * Returns the price deviation required for the given borrow position to be unhealthy (scaled by WAD).
203
+ * @param position The borrow position to consider.
204
+ */
205
+ function getPriceVariationToLiquidation(position, market, marketConfig) {
206
+ market.price = BigInt(market.price);
207
+ if (market.price === 0n)
208
+ return null;
209
+ const liquidationPrice = getLiquidationPrice(position, market, marketConfig);
210
+ if (liquidationPrice == null)
211
+ return null;
212
+ return maths_1.MathLib.WAD - maths_1.MathLib.wDivUp(liquidationPrice, market.price);
213
+ }
214
+ MarketUtils.getPriceVariationToLiquidation = getPriceVariationToLiquidation;
215
+ function getHealthFactor({ collateral, borrowShares, }, market, marketConfig) {
216
+ borrowShares = BigInt(borrowShares);
217
+ market.totalBorrowShares = BigInt(market.totalBorrowShares);
218
+ if (borrowShares === 0n || market.totalBorrowShares === 0n)
219
+ return null;
220
+ const borrowAssets = toBorrowAssets(borrowShares, market);
221
+ if (borrowAssets === 0n)
222
+ return maths_1.MathLib.MAX_UINT_256;
223
+ const maxBorrowAssets = getMaxBorrowAssets(collateral, market, marketConfig);
224
+ return maths_1.MathLib.wDivDown(maxBorrowAssets, borrowAssets);
225
+ }
226
+ MarketUtils.getHealthFactor = getHealthFactor;
227
+ function getLtv({ collateral, borrowShares, }, market) {
228
+ borrowShares = BigInt(borrowShares);
229
+ market.totalBorrowShares = BigInt(market.totalBorrowShares);
230
+ if (borrowShares === 0n || market.totalBorrowShares === 0n)
231
+ return null;
232
+ const collateralValue = getCollateralValue(collateral, market);
233
+ if (collateralValue === 0n)
234
+ return maths_1.MathLib.MAX_UINT_256;
235
+ return maths_1.MathLib.wDivUp(toBorrowAssets(borrowShares, market), collateralValue);
236
+ }
237
+ MarketUtils.getLtv = getLtv;
238
+ function getBorrowCapacityUsage(position, market, marketConfig) {
239
+ const hf = getHealthFactor(position, market, marketConfig);
240
+ if (hf === null)
241
+ return null;
242
+ if (hf === 0n)
243
+ return maths_1.MathLib.MAX_UINT_256;
244
+ return maths_1.MathLib.wDivUp(maths_1.MathLib.WAD, hf);
245
+ }
246
+ MarketUtils.getBorrowCapacityUsage = getBorrowCapacityUsage;
247
+ function toSupplyAssets(shares, market, rounding = "Down") {
248
+ return maths_1.SharesMath.toAssets(shares, market.totalSupplyAssets, market.totalSupplyShares, rounding);
249
+ }
250
+ MarketUtils.toSupplyAssets = toSupplyAssets;
251
+ function toSupplyShares(assets, market, rounding = "Up") {
252
+ return maths_1.SharesMath.toShares(assets, market.totalSupplyAssets, market.totalSupplyShares, rounding);
253
+ }
254
+ MarketUtils.toSupplyShares = toSupplyShares;
255
+ function toBorrowAssets(shares, market, rounding = "Up") {
256
+ return maths_1.SharesMath.toAssets(shares, market.totalBorrowAssets, market.totalBorrowShares, rounding);
257
+ }
258
+ MarketUtils.toBorrowAssets = toBorrowAssets;
259
+ function toBorrowShares(assets, market, rounding = "Down") {
260
+ return maths_1.SharesMath.toShares(assets, market.totalBorrowAssets, market.totalBorrowShares, rounding);
261
+ }
262
+ MarketUtils.toBorrowShares = toBorrowShares;
263
+ })(MarketUtils || (exports.MarketUtils = MarketUtils = {}));
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const maths_1 = require("../maths");
4
+ const MarketUtils_1 = require("./MarketUtils");
5
+ const market = {
6
+ loanToken: "0x0000000000000000000000000000000000000001",
7
+ collateralToken: "0x0000000000000000000000000000000000000002",
8
+ oracle: "0x0000000000000000000000000000000000000003",
9
+ irm: "0x0000000000000000000000000000000000000004",
10
+ lltv: 860000000000000000n,
11
+ };
12
+ describe("MarketUtils", () => {
13
+ it("should calculate the correct market id", () => {
14
+ expect(MarketUtils_1.MarketUtils.getMarketId(market)).toEqual("0x625e29dff74826b71c1f4c74b208a896109cc8ac9910192ce2927a982b0809e6");
15
+ });
16
+ it("should calculate the correct liquidation incentive factor", () => {
17
+ expect(MarketUtils_1.MarketUtils.getLiquidationIncentiveFactor(market)).toEqual(1043841336116910229n);
18
+ });
19
+ it("should calculate the supply volume to reach utilization", () => {
20
+ expect(MarketUtils_1.MarketUtils.getSupplyToUtilization({ totalSupplyAssets: maths_1.MathLib.WAD, totalBorrowAssets: maths_1.MathLib.WAD }, 900000000000000000n)).toEqual(111111111111111112n);
21
+ expect(MarketUtils_1.MarketUtils.getSupplyToUtilization({ totalSupplyAssets: maths_1.MathLib.WAD, totalBorrowAssets: 0n }, 900000000000000000n)).toEqual(0n);
22
+ expect(MarketUtils_1.MarketUtils.getSupplyToUtilization({ totalSupplyAssets: maths_1.MathLib.WAD, totalBorrowAssets: 0n }, 0n)).toEqual(0n);
23
+ expect(MarketUtils_1.MarketUtils.getSupplyToUtilization({ totalSupplyAssets: maths_1.MathLib.WAD, totalBorrowAssets: 1n }, 0n)).toEqual(maths_1.MathLib.MAX_UINT_256);
24
+ });
25
+ it("should calculate the withdraw volume to reach utilization", () => {
26
+ expect(MarketUtils_1.MarketUtils.getWithdrawToUtilization({ totalSupplyAssets: maths_1.MathLib.WAD, totalBorrowAssets: maths_1.MathLib.WAD }, 900000000000000000n)).toEqual(0n);
27
+ expect(MarketUtils_1.MarketUtils.getWithdrawToUtilization({ totalSupplyAssets: 2n * maths_1.MathLib.WAD, totalBorrowAssets: maths_1.MathLib.WAD }, 900000000000000000n)).toEqual(888888888888888888n);
28
+ expect(MarketUtils_1.MarketUtils.getWithdrawToUtilization({ totalSupplyAssets: maths_1.MathLib.WAD, totalBorrowAssets: 0n }, 900000000000000000n)).toEqual(maths_1.MathLib.WAD);
29
+ });
30
+ it("should calculate the borrow volume to reach utilization", () => {
31
+ expect(MarketUtils_1.MarketUtils.getBorrowToUtilization({ totalSupplyAssets: maths_1.MathLib.WAD, totalBorrowAssets: maths_1.MathLib.WAD }, 900000000000000000n)).toEqual(0n);
32
+ expect(MarketUtils_1.MarketUtils.getBorrowToUtilization({ totalSupplyAssets: maths_1.MathLib.WAD, totalBorrowAssets: 0n }, 900000000000000000n)).toEqual(900000000000000000n);
33
+ });
34
+ it("should calculate the repay volume to reach utilization", () => {
35
+ expect(MarketUtils_1.MarketUtils.getRepayToUtilization({ totalSupplyAssets: maths_1.MathLib.WAD, totalBorrowAssets: maths_1.MathLib.WAD }, 900000000000000000n)).toEqual(100000000000000000n);
36
+ expect(MarketUtils_1.MarketUtils.getRepayToUtilization({ totalSupplyAssets: maths_1.MathLib.WAD, totalBorrowAssets: 0n }, 900000000000000000n)).toEqual(0n);
37
+ });
38
+ });
@@ -0,0 +1,3 @@
1
+ export * from "./MarketUtils";
2
+ export * from "./MarketConfig";
3
+ export * from "./Market";
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./MarketUtils"), exports);
18
+ __exportStar(require("./MarketConfig"), exports);
19
+ __exportStar(require("./Market"), exports);
@@ -0,0 +1,39 @@
1
+ import { BigIntish } from "../types";
2
+ /**
3
+ * JS implementation of {@link https://github.com/morpho-org/morpho-blue-irm/blob/main/src/libraries/adaptive-curve/ExpLib.sol ExpLib} used by the Adaptive Curve IRM.
4
+ */
5
+ export declare namespace AdaptiveCurveIrmLib {
6
+ const CURVE_STEEPNESS = 4000000000000000000n;
7
+ const TARGET_UTILIZATION = 900000000000000000n;
8
+ const INITIAL_RATE_AT_TARGET: bigint;
9
+ const ADJUSTMENT_SPEED: bigint;
10
+ const MIN_RATE_AT_TARGET: bigint;
11
+ const MAX_RATE_AT_TARGET: bigint;
12
+ /**
13
+ * ln(2), scaled by WAD.
14
+ */
15
+ const LN_2_INT = 693147180559945309n;
16
+ /**
17
+ * ln(1e-18), scaled by WAD.
18
+ */
19
+ const LN_WEI_INT = -41446531673892822312n;
20
+ /**
21
+ * Above this bound, `wExp` is clipped to avoid overflowing when multiplied with 1 ether.
22
+ * This upper bound corresponds to: ln(type(int256).max / 1e36) (scaled by WAD, floored).
23
+ */
24
+ const WEXP_UPPER_BOUND = 93859467695000404319n;
25
+ /**
26
+ * The value of wExp(`WEXP_UPPER_BOUND`).
27
+ */
28
+ const WEXP_UPPER_VALUE = 57716089161558943949701069502944508345128422502756744429568n;
29
+ /**
30
+ * Returns an approximation of exp(x) used by the Adaptive Curve IRM.
31
+ * @param x
32
+ */
33
+ function wExp(x: BigIntish): bigint;
34
+ function getBorrowRate(startUtilization: BigIntish, startRateAtTarget: BigIntish, elapsed: BigIntish): {
35
+ avgBorrowRate: bigint;
36
+ endRateAtTarget: bigint;
37
+ };
38
+ function getUtilizationAtBorrowRate(borrowRate: BigIntish, rateAtTarget: BigIntish): bigint;
39
+ }