@morpho-org/blue-sdk 1.0.5 → 1.2.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 (105) hide show
  1. package/README.md +2 -2
  2. package/lib/addresses.d.ts +35 -21
  3. package/lib/addresses.js +51 -14
  4. package/lib/chain.d.ts +30 -0
  5. package/lib/chain.js +288 -0
  6. package/lib/chain.test.js +22 -0
  7. package/lib/constants.d.ts +27 -6
  8. package/lib/constants.js +28 -8
  9. package/lib/errors.d.ts +18 -7
  10. package/lib/errors.js +38 -10
  11. package/lib/helpers/format/format.js +21 -60
  12. package/lib/helpers/index.d.ts +0 -2
  13. package/lib/helpers/index.js +0 -2
  14. package/lib/holding/Holding.d.ts +3 -4
  15. package/lib/holding/Holding.js +32 -4
  16. package/lib/index.d.ts +0 -8
  17. package/lib/index.js +1 -9
  18. package/lib/market/Market.d.ts +206 -55
  19. package/lib/market/Market.js +266 -64
  20. package/lib/market/MarketConfig.d.ts +17 -9
  21. package/lib/market/MarketConfig.js +39 -21
  22. package/lib/market/MarketUtils.d.ts +151 -102
  23. package/lib/market/MarketUtils.js +94 -38
  24. package/lib/market/MarketUtils.test.js +1 -2
  25. package/lib/maths/AdaptiveCurveIrmLib.d.ts +5 -4
  26. package/lib/maths/AdaptiveCurveIrmLib.js +10 -7
  27. package/lib/maths/MathLib.d.ts +35 -15
  28. package/lib/maths/MathLib.js +53 -33
  29. package/lib/maths/SharesMath.d.ts +3 -3
  30. package/lib/maths/SharesMath.js +2 -3
  31. package/lib/maths/index.d.ts +0 -1
  32. package/lib/maths/index.js +0 -1
  33. package/lib/position/Position.d.ts +5 -26
  34. package/lib/position/Position.js +29 -36
  35. package/lib/tests/mocks/markets.d.ts +1 -0
  36. package/lib/tests/mocks/markets.js +35 -29
  37. package/lib/token/Token.d.ts +4 -3
  38. package/lib/token/Token.js +28 -3
  39. package/lib/token/WrappedToken.d.ts +3 -3
  40. package/lib/token/WrappedToken.js +18 -11
  41. package/lib/token/index.d.ts +1 -1
  42. package/lib/token/index.js +1 -1
  43. package/lib/types.d.ts +1 -0
  44. package/lib/types.js +3 -4
  45. package/lib/user/User.js +12 -0
  46. package/lib/vault/Vault.d.ts +12 -12
  47. package/lib/vault/Vault.js +102 -18
  48. package/lib/vault/VaultConfig.js +9 -3
  49. package/lib/vault/VaultMarketAllocation.d.ts +20 -0
  50. package/lib/vault/VaultMarketAllocation.js +30 -0
  51. package/lib/vault/VaultMarketConfig.d.ts +43 -0
  52. package/lib/vault/VaultMarketConfig.js +43 -0
  53. package/lib/vault/VaultMarketPublicAllocatorConfig.d.ts +29 -0
  54. package/lib/vault/VaultMarketPublicAllocatorConfig.js +28 -0
  55. package/lib/vault/VaultUtils.d.ts +8 -9
  56. package/lib/vault/VaultUtils.js +2 -3
  57. package/lib/vault/index.d.ts +3 -1
  58. package/lib/vault/index.js +3 -1
  59. package/package.json +3 -6
  60. package/lib/chain/chain.constants.d.ts +0 -3
  61. package/lib/chain/chain.constants.js +0 -232
  62. package/lib/chain/chain.test.js +0 -37
  63. package/lib/chain/chain.types.d.ts +0 -20
  64. package/lib/chain/chain.types.js +0 -30
  65. package/lib/chain/chain.utils.d.ts +0 -14
  66. package/lib/chain/chain.utils.js +0 -30
  67. package/lib/chain/index.d.ts +0 -2
  68. package/lib/chain/index.js +0 -18
  69. package/lib/ethers/ethers.test.d.ts +0 -1
  70. package/lib/ethers/ethers.test.js +0 -11
  71. package/lib/ethers/index.d.ts +0 -2
  72. package/lib/ethers/index.js +0 -18
  73. package/lib/ethers/safeGetAddress.d.ts +0 -1
  74. package/lib/ethers/safeGetAddress.js +0 -6
  75. package/lib/ethers/safeParseUnits.d.ts +0 -2
  76. package/lib/ethers/safeParseUnits.js +0 -25
  77. package/lib/evm.d.ts +0 -36
  78. package/lib/evm.js +0 -113
  79. package/lib/helpers/getChecksumedAddress.d.ts +0 -7
  80. package/lib/helpers/getChecksumedAddress.js +0 -17
  81. package/lib/helpers/isZeroAddressOrUnset.d.ts +0 -7
  82. package/lib/helpers/isZeroAddressOrUnset.js +0 -14
  83. package/lib/maths/MathUtils.d.ts +0 -15
  84. package/lib/maths/MathUtils.js +0 -33
  85. package/lib/notifications.d.ts +0 -98
  86. package/lib/notifications.js +0 -52
  87. package/lib/signatures/index.d.ts +0 -12
  88. package/lib/signatures/index.js +0 -39
  89. package/lib/signatures/manager.d.ts +0 -10
  90. package/lib/signatures/manager.js +0 -37
  91. package/lib/signatures/permit.d.ts +0 -21
  92. package/lib/signatures/permit.js +0 -101
  93. package/lib/signatures/permit2.d.ts +0 -20
  94. package/lib/signatures/permit2.js +0 -91
  95. package/lib/signatures/types.d.ts +0 -13
  96. package/lib/signatures/types.js +0 -2
  97. package/lib/signatures/utils.d.ts +0 -6
  98. package/lib/signatures/utils.js +0 -44
  99. package/lib/token/ERC20Metadata.d.ts +0 -249
  100. package/lib/token/ERC20Metadata.js +0 -81
  101. package/lib/token/TokenNamespace.d.ts +0 -18
  102. package/lib/token/TokenNamespace.js +0 -55
  103. package/lib/vault/VaultAllocation.d.ts +0 -38
  104. package/lib/vault/VaultAllocation.js +0 -18
  105. /package/lib/{chain/chain.test.d.ts → chain.test.d.ts} +0 -0
@@ -1,98 +1,143 @@
1
- import { BigNumberish } from "ethers";
2
- import { MarketParamsStruct } from "ethers-types/dist/protocols/morpho/blue/MorphoBlue";
3
1
  import { RoundingDirection } from "../maths";
4
- import { MarketId } from "../types";
2
+ import { BigIntish, MarketId } from "../types";
3
+ import { MarketParams } from "./MarketConfig";
4
+ /**
5
+ * Namespace of utility functions to ease market-related calculations.
6
+ */
5
7
  export declare namespace MarketUtils {
6
- function getMarketId(market: MarketParamsStruct): MarketId;
7
- function getLiquidationIncentiveFactor({ lltv, }: {
8
- lltv: BigNumberish;
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;
9
19
  }): bigint;
20
+ /**
21
+ * Returns the market's utilization rate (scaled by WAD).
22
+ * @param market The market state.
23
+ */
10
24
  function getUtilization({ totalSupplyAssets, totalBorrowAssets, }: {
11
- totalSupplyAssets: BigNumberish;
12
- totalBorrowAssets: BigNumberish;
25
+ totalSupplyAssets: BigIntish;
26
+ totalBorrowAssets: BigIntish;
13
27
  }): bigint;
14
- function getSupplyRate(borrowRate: BigNumberish, { utilization, fee }: {
15
- utilization: BigNumberish;
16
- fee: BigNumberish;
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;
17
37
  }): bigint;
18
- function getApy(rate: BigNumberish): bigint;
19
- function getAccruedInterest(borrowRate: BigNumberish, { totalSupplyAssets, totalBorrowAssets, totalSupplyShares, fee, }: {
20
- totalSupplyAssets: BigNumberish;
21
- totalBorrowAssets: BigNumberish;
22
- totalSupplyShares: BigNumberish;
23
- fee: BigNumberish;
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;
24
55
  }, elapsed?: bigint): {
25
56
  interest: bigint;
26
57
  feeShares: bigint;
27
58
  };
28
- function getLiquidityToUtilization({ totalSupplyAssets, totalBorrowAssets, }: {
29
- totalSupplyAssets: BigNumberish;
30
- totalBorrowAssets: BigNumberish;
31
- }, utilization: BigNumberish): bigint;
32
- function getCollateralPower(collateral: BigNumberish, { lltv }: {
33
- lltv: BigNumberish;
59
+ /**
60
+ * Returns the liquidity available to withdraw until the market reach the given utilization rate.
61
+ * @param market The market state.
62
+ * @param utilization The target utilization rate (scaled by WAD).
63
+ */
64
+ function getSupplyLiquidityToUtilization({ totalSupplyAssets, totalBorrowAssets, }: {
65
+ totalSupplyAssets: BigIntish;
66
+ totalBorrowAssets: BigIntish;
67
+ }, utilization: BigIntish): BigIntish;
68
+ /**
69
+ * Returns the liquidity available to borrow until the market reach the given utilization rate.
70
+ * @param market The market state.
71
+ * @param utilization The target utilization rate (scaled by WAD).
72
+ */
73
+ function getBorrowLiquidityToUtilization({ totalSupplyAssets, totalBorrowAssets, }: {
74
+ totalSupplyAssets: BigIntish;
75
+ totalBorrowAssets: BigIntish;
76
+ }, utilization: BigIntish): bigint;
77
+ function getCollateralPower(collateral: BigIntish, { lltv }: {
78
+ lltv: BigIntish;
34
79
  }): bigint;
35
- function getCollateralValue(collateral: BigNumberish, { price }: {
36
- price: BigNumberish;
80
+ function getCollateralValue(collateral: BigIntish, { price }: {
81
+ price: BigIntish;
37
82
  }): bigint;
38
- function getMaxBorrowAssets(collateral: BigNumberish, market: {
39
- price: BigNumberish;
83
+ function getMaxBorrowAssets(collateral: BigIntish, market: {
84
+ price: BigIntish;
40
85
  }, { lltv }: {
41
- lltv: BigNumberish;
86
+ lltv: BigIntish;
42
87
  }): bigint;
43
88
  function getMaxBorrowableAssets({ collateral, borrowShares, }: {
44
- collateral: BigNumberish;
45
- borrowShares: BigNumberish;
89
+ collateral: BigIntish;
90
+ borrowShares: BigIntish;
46
91
  }, market: {
47
- totalBorrowAssets: BigNumberish;
48
- totalBorrowShares: BigNumberish;
49
- price: BigNumberish;
92
+ totalBorrowAssets: BigIntish;
93
+ totalBorrowShares: BigIntish;
94
+ price: BigIntish;
50
95
  }, marketConfig: {
51
- lltv: BigNumberish;
96
+ lltv: BigIntish;
52
97
  }): bigint;
53
- function getLiquidationSeizedAssets(repaidShares: BigNumberish, market: {
54
- totalBorrowAssets: BigNumberish;
55
- totalBorrowShares: BigNumberish;
56
- price: BigNumberish;
98
+ function getLiquidationSeizedAssets(repaidShares: BigIntish, market: {
99
+ totalBorrowAssets: BigIntish;
100
+ totalBorrowShares: BigIntish;
101
+ price: BigIntish;
57
102
  }, config: {
58
- lltv: BigNumberish;
103
+ lltv: BigIntish;
59
104
  }): bigint;
60
- function getLiquidationRepaidShares(seizedAssets: BigNumberish, market: {
61
- totalBorrowAssets: BigNumberish;
62
- totalBorrowShares: BigNumberish;
63
- price: BigNumberish;
105
+ function getLiquidationRepaidShares(seizedAssets: BigIntish, market: {
106
+ totalBorrowAssets: BigIntish;
107
+ totalBorrowShares: BigIntish;
108
+ price: BigIntish;
64
109
  }, config: {
65
- lltv: BigNumberish;
110
+ lltv: BigIntish;
66
111
  }): bigint;
67
112
  function getSeizableCollateral(position: {
68
- collateral: BigNumberish;
69
- borrowShares: BigNumberish;
113
+ collateral: BigIntish;
114
+ borrowShares: BigIntish;
70
115
  }, market: {
71
- totalBorrowAssets: BigNumberish;
72
- totalBorrowShares: BigNumberish;
73
- price: BigNumberish;
116
+ totalBorrowAssets: BigIntish;
117
+ totalBorrowShares: BigIntish;
118
+ price: BigIntish;
74
119
  }, config: {
75
- lltv: BigNumberish;
120
+ lltv: BigIntish;
76
121
  }): bigint;
77
122
  function getWithdrawableCollateral({ collateral, borrowShares, }: {
78
- collateral: BigNumberish;
79
- borrowShares: BigNumberish;
123
+ collateral: BigIntish;
124
+ borrowShares: BigIntish;
80
125
  }, market: {
81
- totalBorrowAssets: BigNumberish;
82
- totalBorrowShares: BigNumberish;
83
- price: BigNumberish;
126
+ totalBorrowAssets: BigIntish;
127
+ totalBorrowShares: BigIntish;
128
+ price: BigIntish;
84
129
  }, { lltv }: {
85
- lltv: BigNumberish;
130
+ lltv: BigIntish;
86
131
  }): bigint;
87
132
  function isHealthy({ collateral, borrowShares, }: {
88
- collateral: BigNumberish;
89
- borrowShares: BigNumberish;
133
+ collateral: BigIntish;
134
+ borrowShares: BigIntish;
90
135
  }, market: {
91
- totalBorrowAssets: BigNumberish;
92
- totalBorrowShares: BigNumberish;
93
- price: BigNumberish;
136
+ totalBorrowAssets: BigIntish;
137
+ totalBorrowShares: BigIntish;
138
+ price: BigIntish;
94
139
  }, marketConfig: {
95
- lltv: BigNumberish;
140
+ lltv: BigIntish;
96
141
  }): boolean;
97
142
  /**
98
143
  * Returns the price of the collateral quoted in the loan token (e.g. ETH/DAI)
@@ -100,66 +145,70 @@ export declare namespace MarketUtils {
100
145
  * Returns null if the user is not a borrower
101
146
  */
102
147
  function getLiquidationPrice({ collateral, borrowShares, }: {
103
- collateral: BigNumberish;
104
- borrowShares: BigNumberish;
148
+ collateral: BigIntish;
149
+ borrowShares: BigIntish;
105
150
  }, market: {
106
- totalBorrowAssets: BigNumberish;
107
- totalBorrowShares: BigNumberish;
151
+ totalBorrowAssets: BigIntish;
152
+ totalBorrowShares: BigIntish;
108
153
  }, marketConfig: {
109
- lltv: BigNumberish;
154
+ lltv: BigIntish;
110
155
  }): bigint | null;
156
+ /**
157
+ * Returns the price deviation required for the given borrow position to be unhealthy (scaled by WAD).
158
+ * @param position The borrow position to consider.
159
+ */
111
160
  function getPriceVariationToLiquidation(position: {
112
- collateral: BigNumberish;
113
- borrowShares: BigNumberish;
161
+ collateral: BigIntish;
162
+ borrowShares: BigIntish;
114
163
  }, market: {
115
- totalBorrowAssets: BigNumberish;
116
- totalBorrowShares: BigNumberish;
117
- price: BigNumberish;
164
+ totalBorrowAssets: BigIntish;
165
+ totalBorrowShares: BigIntish;
166
+ price: BigIntish;
118
167
  }, marketConfig: {
119
- lltv: BigNumberish;
168
+ lltv: BigIntish;
120
169
  }): bigint | null;
121
170
  function getHealthFactor({ collateral, borrowShares, }: {
122
- collateral: BigNumberish;
123
- borrowShares: BigNumberish;
171
+ collateral: BigIntish;
172
+ borrowShares: BigIntish;
124
173
  }, market: {
125
- totalBorrowAssets: BigNumberish;
126
- totalBorrowShares: BigNumberish;
127
- price: BigNumberish;
174
+ totalBorrowAssets: BigIntish;
175
+ totalBorrowShares: BigIntish;
176
+ price: BigIntish;
128
177
  }, marketConfig: {
129
- lltv: BigNumberish;
178
+ lltv: BigIntish;
130
179
  }): bigint | null;
131
180
  function getLtv({ collateral, borrowShares, }: {
132
- collateral: BigNumberish;
133
- borrowShares: BigNumberish;
181
+ collateral: BigIntish;
182
+ borrowShares: BigIntish;
134
183
  }, market: {
135
- totalBorrowAssets: BigNumberish;
136
- totalBorrowShares: BigNumberish;
137
- price: BigNumberish;
184
+ totalBorrowAssets: BigIntish;
185
+ totalBorrowShares: BigIntish;
186
+ price: BigIntish;
138
187
  }): bigint | null;
139
188
  function getBorrowCapacityUsage(position: {
140
- collateral: BigNumberish;
141
- borrowShares: BigNumberish;
189
+ collateral: BigIntish;
190
+ borrowShares: BigIntish;
142
191
  }, market: {
143
- totalBorrowAssets: BigNumberish;
144
- totalBorrowShares: BigNumberish;
145
- price: BigNumberish;
192
+ totalBorrowAssets: BigIntish;
193
+ totalBorrowShares: BigIntish;
194
+ price: BigIntish;
146
195
  }, marketConfig: {
147
- lltv: BigNumberish;
196
+ lltv: BigIntish;
148
197
  }): bigint | null;
149
- function toSupplyAssets(shares: BigNumberish, market: {
150
- totalSupplyAssets: BigNumberish;
151
- totalSupplyShares: BigNumberish;
198
+ function toSupplyAssets(shares: BigIntish, market: {
199
+ totalSupplyAssets: BigIntish;
200
+ totalSupplyShares: BigIntish;
152
201
  }, rounding?: RoundingDirection): bigint;
153
- function toSupplyShares(assets: BigNumberish, market: {
154
- totalSupplyAssets: BigNumberish;
155
- totalSupplyShares: BigNumberish;
202
+ function toSupplyShares(assets: BigIntish, market: {
203
+ totalSupplyAssets: BigIntish;
204
+ totalSupplyShares: BigIntish;
156
205
  }, rounding?: RoundingDirection): bigint;
157
- function toBorrowAssets(shares: BigNumberish, market: {
158
- totalBorrowAssets: BigNumberish;
159
- totalBorrowShares: BigNumberish;
206
+ function toBorrowAssets(shares: BigIntish, market: {
207
+ totalBorrowAssets: BigIntish;
208
+ totalBorrowShares: BigIntish;
160
209
  }, rounding?: RoundingDirection): bigint;
161
- function toBorrowShares(assets: BigNumberish, market: {
162
- totalBorrowAssets: BigNumberish;
163
- totalBorrowShares: BigNumberish;
210
+ function toBorrowShares(assets: BigIntish, market: {
211
+ totalBorrowAssets: BigIntish;
212
+ totalBorrowShares: BigIntish;
164
213
  }, rounding?: RoundingDirection): bigint;
165
214
  }
@@ -1,65 +1,115 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.MarketUtils = void 0;
4
- const ethers_1 = require("ethers");
7
+ const keccak256_1 = __importDefault(require("keccak256"));
5
8
  const constants_1 = require("../constants");
6
9
  const maths_1 = require("../maths");
10
+ /**
11
+ * Namespace of utility functions to ease market-related calculations.
12
+ */
7
13
  var MarketUtils;
8
14
  (function (MarketUtils) {
15
+ /**
16
+ * Returns the id of a market based on its params.
17
+ * @param market The market params.
18
+ */
9
19
  function getMarketId(market) {
10
- const encodedMarket = ethers_1.AbiCoder.defaultAbiCoder().encode(["address", "address", "address", "address", "uint256"], [
11
- market.loanToken,
12
- market.collateralToken,
13
- market.oracle,
14
- market.irm,
15
- market.lltv,
16
- ]);
17
- return (0, ethers_1.keccak256)(encodedMarket);
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
+ market.lltv.toString(16).padStart(64, "0")).toString("hex")}`;
18
26
  }
19
27
  MarketUtils.getMarketId = getMarketId;
20
- function getLiquidationIncentiveFactor({ lltv, }) {
28
+ /**
29
+ * Returns the liquidation incentive factor for a given market params.
30
+ * @param config The market params.
31
+ */
32
+ function getLiquidationIncentiveFactor({ lltv }) {
21
33
  return maths_1.MathLib.min(constants_1.MAX_LIQUIDATION_INCENTIVE_FACTOR, maths_1.MathLib.wDivDown(maths_1.MathLib.WAD, maths_1.MathLib.WAD -
22
- maths_1.MathLib.wMulDown(constants_1.LIQUIDATION_CURSOR, maths_1.MathLib.WAD - (0, ethers_1.toBigInt)(lltv))));
34
+ maths_1.MathLib.wMulDown(constants_1.LIQUIDATION_CURSOR, maths_1.MathLib.WAD - BigInt(lltv))));
23
35
  }
24
36
  MarketUtils.getLiquidationIncentiveFactor = getLiquidationIncentiveFactor;
37
+ /**
38
+ * Returns the market's utilization rate (scaled by WAD).
39
+ * @param market The market state.
40
+ */
25
41
  function getUtilization({ totalSupplyAssets, totalBorrowAssets, }) {
26
- totalSupplyAssets = (0, ethers_1.toBigInt)(totalSupplyAssets);
27
- if (totalSupplyAssets === 0n)
42
+ totalSupplyAssets = BigInt(totalSupplyAssets);
43
+ totalBorrowAssets = BigInt(totalBorrowAssets);
44
+ if (totalSupplyAssets === 0n) {
45
+ if (totalBorrowAssets > 0n)
46
+ return maths_1.MathLib.MAX_UINT_256;
28
47
  return 0n;
48
+ }
29
49
  return maths_1.MathLib.wDivDown(totalBorrowAssets, totalSupplyAssets);
30
50
  }
31
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
+ */
32
58
  function getSupplyRate(borrowRate, { utilization, fee }) {
33
59
  const borrowRateWithoutFees = maths_1.MathLib.wMulUp(borrowRate, utilization);
34
- return maths_1.MathLib.wMulUp(borrowRateWithoutFees, maths_1.MathLib.WAD - (0, ethers_1.toBigInt)(fee));
60
+ return maths_1.MathLib.wMulUp(borrowRateWithoutFees, maths_1.MathLib.WAD - BigInt(fee));
35
61
  }
36
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
+ */
37
67
  function getApy(rate) {
38
68
  return maths_1.MathLib.wTaylorCompounded(rate, constants_1.SECONDS_PER_YEAR);
39
69
  }
40
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
+ */
41
78
  function getAccruedInterest(borrowRate, { totalSupplyAssets, totalBorrowAssets, totalSupplyShares, fee, }, elapsed = 0n) {
42
79
  const interest = maths_1.MathLib.wMulDown(totalBorrowAssets, maths_1.MathLib.wTaylorCompounded(borrowRate, elapsed));
43
80
  const feeAmount = maths_1.MathLib.wMulDown(interest, fee);
44
81
  const feeShares = toSupplyShares(feeAmount, {
45
- totalSupplyAssets: (0, ethers_1.toBigInt)(totalSupplyAssets) - feeAmount,
82
+ totalSupplyAssets: BigInt(totalSupplyAssets) - feeAmount,
46
83
  totalSupplyShares,
47
84
  }, "Down");
48
85
  return { interest, feeShares };
49
86
  }
50
87
  MarketUtils.getAccruedInterest = getAccruedInterest;
51
- function getLiquidityToUtilization({ totalSupplyAssets, totalBorrowAssets, }, utilization) {
52
- utilization = (0, ethers_1.toBigInt)(utilization);
53
- totalSupplyAssets = (0, ethers_1.toBigInt)(totalSupplyAssets);
54
- totalBorrowAssets = (0, ethers_1.toBigInt)(totalBorrowAssets);
88
+ /**
89
+ * Returns the liquidity available to withdraw until the market reach the given utilization rate.
90
+ * @param market The market state.
91
+ * @param utilization The target utilization rate (scaled by WAD).
92
+ */
93
+ function getSupplyLiquidityToUtilization({ totalSupplyAssets, totalBorrowAssets, }, utilization) {
94
+ utilization = BigInt(utilization);
95
+ totalBorrowAssets = BigInt(totalBorrowAssets);
55
96
  if (utilization === 0n) {
56
97
  if (totalBorrowAssets === 0n)
57
98
  return totalSupplyAssets;
58
99
  return 0n;
59
100
  }
60
- return maths_1.MathUtils.zeroFloorSub(totalSupplyAssets, maths_1.MathLib.wDivUp(totalBorrowAssets, utilization));
101
+ return maths_1.MathLib.zeroFloorSub(totalSupplyAssets, maths_1.MathLib.wDivUp(totalBorrowAssets, utilization));
61
102
  }
62
- MarketUtils.getLiquidityToUtilization = getLiquidityToUtilization;
103
+ MarketUtils.getSupplyLiquidityToUtilization = getSupplyLiquidityToUtilization;
104
+ /**
105
+ * Returns the liquidity available to borrow until the market reach the given utilization rate.
106
+ * @param market The market state.
107
+ * @param utilization The target utilization rate (scaled by WAD).
108
+ */
109
+ function getBorrowLiquidityToUtilization({ totalSupplyAssets, totalBorrowAssets, }, utilization) {
110
+ return maths_1.MathLib.zeroFloorSub(maths_1.MathLib.wMulDown(totalSupplyAssets, utilization), totalBorrowAssets);
111
+ }
112
+ MarketUtils.getBorrowLiquidityToUtilization = getBorrowLiquidityToUtilization;
63
113
  function getCollateralPower(collateral, { lltv }) {
64
114
  return maths_1.MathLib.wMulDown(collateral, lltv);
65
115
  }
@@ -73,11 +123,11 @@ var MarketUtils;
73
123
  }
74
124
  MarketUtils.getMaxBorrowAssets = getMaxBorrowAssets;
75
125
  function getMaxBorrowableAssets({ collateral, borrowShares, }, market, marketConfig) {
76
- return maths_1.MathUtils.zeroFloorSub(getMaxBorrowAssets(collateral, market, marketConfig), toBorrowAssets(borrowShares, market));
126
+ return maths_1.MathLib.zeroFloorSub(getMaxBorrowAssets(collateral, market, marketConfig), toBorrowAssets(borrowShares, market));
77
127
  }
78
128
  MarketUtils.getMaxBorrowableAssets = getMaxBorrowableAssets;
79
129
  function getLiquidationSeizedAssets(repaidShares, market, config) {
80
- market.price = (0, ethers_1.toBigInt)(market.price);
130
+ market.price = BigInt(market.price);
81
131
  if (market.price === 0n)
82
132
  return 0n;
83
133
  return maths_1.MathLib.mulDivDown(maths_1.MathLib.wMulDown(toBorrowAssets(repaidShares, market, "Down"), getLiquidationIncentiveFactor(config)), constants_1.ORACLE_PRICE_SCALE, market.price);
@@ -88,17 +138,17 @@ var MarketUtils;
88
138
  }
89
139
  MarketUtils.getLiquidationRepaidShares = getLiquidationRepaidShares;
90
140
  function getSeizableCollateral(position, market, config) {
91
- market.price = (0, ethers_1.toBigInt)(market.price);
141
+ market.price = BigInt(market.price);
92
142
  if (market.price === 0n || isHealthy(position, market, config))
93
143
  return 0n;
94
144
  return maths_1.MathLib.min(position.collateral, getLiquidationSeizedAssets(position.borrowShares, market, config));
95
145
  }
96
146
  MarketUtils.getSeizableCollateral = getSeizableCollateral;
97
147
  function getWithdrawableCollateral({ collateral, borrowShares, }, market, { lltv }) {
98
- market.price = (0, ethers_1.toBigInt)(market.price);
148
+ market.price = BigInt(market.price);
99
149
  if (market.price === 0n)
100
150
  return 0n;
101
- return maths_1.MathUtils.zeroFloorSub((0, ethers_1.toBigInt)(collateral), maths_1.MathLib.wDivUp(maths_1.MathLib.mulDivUp(toBorrowAssets(borrowShares, market), constants_1.ORACLE_PRICE_SCALE, market.price), lltv));
151
+ 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));
102
152
  }
103
153
  MarketUtils.getWithdrawableCollateral = getWithdrawableCollateral;
104
154
  function isHealthy({ collateral, borrowShares, }, market, marketConfig) {
@@ -112,45 +162,51 @@ var MarketUtils;
112
162
  * Returns null if the user is not a borrower
113
163
  */
114
164
  function getLiquidationPrice({ collateral, borrowShares, }, market, marketConfig) {
115
- borrowShares = (0, ethers_1.toBigInt)(borrowShares);
116
- market.totalBorrowShares = (0, ethers_1.toBigInt)(market.totalBorrowShares);
165
+ borrowShares = BigInt(borrowShares);
166
+ market.totalBorrowShares = BigInt(market.totalBorrowShares);
117
167
  if (borrowShares === 0n || market.totalBorrowShares === 0n)
118
168
  return null;
119
169
  const collateralPower = getCollateralPower(collateral, marketConfig);
120
170
  if (collateralPower === 0n)
121
- return ethers_1.MaxUint256;
171
+ return maths_1.MathLib.MAX_UINT_256;
122
172
  const borrowAssets = toBorrowAssets(borrowShares, market);
123
173
  return maths_1.MathLib.mulDivUp(borrowAssets, constants_1.ORACLE_PRICE_SCALE, collateralPower);
124
174
  }
125
175
  MarketUtils.getLiquidationPrice = getLiquidationPrice;
176
+ /**
177
+ * Returns the price deviation required for the given borrow position to be unhealthy (scaled by WAD).
178
+ * @param position The borrow position to consider.
179
+ */
126
180
  function getPriceVariationToLiquidation(position, market, marketConfig) {
127
- market.price = (0, ethers_1.toBigInt)(market.price);
181
+ market.price = BigInt(market.price);
182
+ if (market.price === 0n)
183
+ return null;
128
184
  const liquidationPrice = getLiquidationPrice(position, market, marketConfig);
129
- if (market.price === 0n || liquidationPrice == null)
185
+ if (liquidationPrice == null)
130
186
  return null;
131
187
  return maths_1.MathLib.WAD - maths_1.MathLib.wDivUp(liquidationPrice, market.price);
132
188
  }
133
189
  MarketUtils.getPriceVariationToLiquidation = getPriceVariationToLiquidation;
134
190
  function getHealthFactor({ collateral, borrowShares, }, market, marketConfig) {
135
- borrowShares = (0, ethers_1.toBigInt)(borrowShares);
136
- market.totalBorrowShares = (0, ethers_1.toBigInt)(market.totalBorrowShares);
191
+ borrowShares = BigInt(borrowShares);
192
+ market.totalBorrowShares = BigInt(market.totalBorrowShares);
137
193
  if (borrowShares === 0n || market.totalBorrowShares === 0n)
138
194
  return null;
139
195
  const borrowAssets = toBorrowAssets(borrowShares, market);
140
196
  if (borrowAssets === 0n)
141
- return ethers_1.MaxUint256;
197
+ return maths_1.MathLib.MAX_UINT_256;
142
198
  const maxBorrowAssets = getMaxBorrowAssets(collateral, market, marketConfig);
143
199
  return maths_1.MathLib.wDivDown(maxBorrowAssets, borrowAssets);
144
200
  }
145
201
  MarketUtils.getHealthFactor = getHealthFactor;
146
202
  function getLtv({ collateral, borrowShares, }, market) {
147
- borrowShares = (0, ethers_1.toBigInt)(borrowShares);
148
- market.totalBorrowShares = (0, ethers_1.toBigInt)(market.totalBorrowShares);
203
+ borrowShares = BigInt(borrowShares);
204
+ market.totalBorrowShares = BigInt(market.totalBorrowShares);
149
205
  if (borrowShares === 0n || market.totalBorrowShares === 0n)
150
206
  return null;
151
207
  const collateralValue = getCollateralValue(collateral, market);
152
208
  if (collateralValue === 0n)
153
- return ethers_1.MaxUint256;
209
+ return maths_1.MathLib.MAX_UINT_256;
154
210
  return maths_1.MathLib.wDivUp(toBorrowAssets(borrowShares, market), collateralValue);
155
211
  }
156
212
  MarketUtils.getLtv = getLtv;
@@ -159,7 +215,7 @@ var MarketUtils;
159
215
  if (hf === null)
160
216
  return null;
161
217
  if (hf === 0n)
162
- return ethers_1.MaxUint256;
218
+ return maths_1.MathLib.MAX_UINT_256;
163
219
  return maths_1.MathLib.wDivUp(maths_1.MathLib.WAD, hf);
164
220
  }
165
221
  MarketUtils.getBorrowCapacityUsage = getBorrowCapacityUsage;
@@ -1,13 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- const ethers_1 = require("ethers");
4
3
  const MarketUtils_1 = require("./MarketUtils");
5
4
  const market = {
6
5
  loanToken: "0x0000000000000000000000000000000000000001",
7
6
  collateralToken: "0x0000000000000000000000000000000000000002",
8
7
  oracle: "0x0000000000000000000000000000000000000003",
9
8
  irm: "0x0000000000000000000000000000000000000004",
10
- lltv: (0, ethers_1.parseEther)("0.86"),
9
+ lltv: 860000000000000000n,
11
10
  };
12
11
  describe("MarketUtils", () => {
13
12
  it("should calculate the correct market id", () => {
@@ -1,9 +1,10 @@
1
+ import { BigIntish } from "../types";
1
2
  /**
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.
3
4
  */
4
5
  export declare namespace AdaptiveCurveIrmLib {
5
- const CURVE_STEEPNESS: bigint;
6
- const TARGET_UTILIZATION: bigint;
6
+ const CURVE_STEEPNESS = 4000000000000000000n;
7
+ const TARGET_UTILIZATION = 900000000000000000n;
7
8
  const INITIAL_RATE_AT_TARGET: bigint;
8
9
  const ADJUSTMENT_SPEED: bigint;
9
10
  const MIN_RATE_AT_TARGET: bigint;
@@ -29,8 +30,8 @@ export declare namespace AdaptiveCurveIrmLib {
29
30
  * Returns an approximation of exp(x) used by the Adaptive Curve IRM.
30
31
  * @param x
31
32
  */
32
- function wExp(x: bigint): bigint;
33
- function getBorrowRate(startUtilization: bigint, startRateAtTarget: bigint, elapsed: bigint): {
33
+ function wExp(x: BigIntish): bigint;
34
+ function getBorrowRate(startUtilization: BigIntish, startRateAtTarget: BigIntish, elapsed: BigIntish): {
34
35
  avgBorrowRate: bigint;
35
36
  endRateAtTarget: bigint;
36
37
  };
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.AdaptiveCurveIrmLib = void 0;
4
- const ethers_1 = require("ethers");
5
4
  const constants_1 = require("../constants");
6
5
  const MathLib_1 = require("./MathLib");
7
6
  /**
@@ -9,12 +8,12 @@ const MathLib_1 = require("./MathLib");
9
8
  */
10
9
  var AdaptiveCurveIrmLib;
11
10
  (function (AdaptiveCurveIrmLib) {
12
- AdaptiveCurveIrmLib.CURVE_STEEPNESS = (0, ethers_1.parseUnits)("4");
13
- AdaptiveCurveIrmLib.TARGET_UTILIZATION = (0, ethers_1.parseUnits)("0.9");
14
- AdaptiveCurveIrmLib.INITIAL_RATE_AT_TARGET = (0, ethers_1.parseUnits)("0.04") / constants_1.SECONDS_PER_YEAR;
15
- AdaptiveCurveIrmLib.ADJUSTMENT_SPEED = (0, ethers_1.parseUnits)("50") / constants_1.SECONDS_PER_YEAR;
16
- AdaptiveCurveIrmLib.MIN_RATE_AT_TARGET = (0, ethers_1.parseUnits)("0.001") / constants_1.SECONDS_PER_YEAR;
17
- AdaptiveCurveIrmLib.MAX_RATE_AT_TARGET = (0, ethers_1.parseUnits)("2") / constants_1.SECONDS_PER_YEAR;
11
+ AdaptiveCurveIrmLib.CURVE_STEEPNESS = 4000000000000000000n;
12
+ AdaptiveCurveIrmLib.TARGET_UTILIZATION = 900000000000000000n;
13
+ AdaptiveCurveIrmLib.INITIAL_RATE_AT_TARGET = 40000000000000000n / constants_1.SECONDS_PER_YEAR;
14
+ AdaptiveCurveIrmLib.ADJUSTMENT_SPEED = 50000000000000000000n / constants_1.SECONDS_PER_YEAR;
15
+ AdaptiveCurveIrmLib.MIN_RATE_AT_TARGET = 1000000000000000n / constants_1.SECONDS_PER_YEAR;
16
+ AdaptiveCurveIrmLib.MAX_RATE_AT_TARGET = 2000000000000000000n / constants_1.SECONDS_PER_YEAR;
18
17
  /**
19
18
  * ln(2), scaled by WAD.
20
19
  */
@@ -37,6 +36,7 @@ var AdaptiveCurveIrmLib;
37
36
  * @param x
38
37
  */
39
38
  function wExp(x) {
39
+ x = BigInt(x);
40
40
  // If x < ln(1e-18) then exp(x) < 1e-18 so it is rounded to zero.
41
41
  if (x < AdaptiveCurveIrmLib.LN_WEI_INT)
42
42
  return 0n;
@@ -57,6 +57,9 @@ var AdaptiveCurveIrmLib;
57
57
  }
58
58
  AdaptiveCurveIrmLib.wExp = wExp;
59
59
  function getBorrowRate(startUtilization, startRateAtTarget, elapsed) {
60
+ startUtilization = BigInt(startUtilization);
61
+ startRateAtTarget = BigInt(startRateAtTarget);
62
+ elapsed = BigInt(elapsed);
60
63
  const errNormFactor = startUtilization > AdaptiveCurveIrmLib.TARGET_UTILIZATION
61
64
  ? MathLib_1.MathLib.WAD - AdaptiveCurveIrmLib.TARGET_UTILIZATION
62
65
  : AdaptiveCurveIrmLib.TARGET_UTILIZATION;