@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.
- package/README.md +2 -2
- package/lib/addresses.d.ts +35 -21
- package/lib/addresses.js +51 -14
- package/lib/chain.d.ts +30 -0
- package/lib/chain.js +288 -0
- package/lib/chain.test.js +22 -0
- package/lib/constants.d.ts +27 -6
- package/lib/constants.js +28 -8
- package/lib/errors.d.ts +18 -7
- package/lib/errors.js +38 -10
- package/lib/helpers/format/format.js +21 -60
- package/lib/helpers/index.d.ts +0 -2
- package/lib/helpers/index.js +0 -2
- package/lib/holding/Holding.d.ts +3 -4
- package/lib/holding/Holding.js +32 -4
- package/lib/index.d.ts +0 -8
- package/lib/index.js +1 -9
- package/lib/market/Market.d.ts +206 -55
- package/lib/market/Market.js +266 -64
- package/lib/market/MarketConfig.d.ts +17 -9
- package/lib/market/MarketConfig.js +39 -21
- package/lib/market/MarketUtils.d.ts +151 -102
- package/lib/market/MarketUtils.js +94 -38
- package/lib/market/MarketUtils.test.js +1 -2
- package/lib/maths/AdaptiveCurveIrmLib.d.ts +5 -4
- package/lib/maths/AdaptiveCurveIrmLib.js +10 -7
- package/lib/maths/MathLib.d.ts +35 -15
- package/lib/maths/MathLib.js +53 -33
- package/lib/maths/SharesMath.d.ts +3 -3
- package/lib/maths/SharesMath.js +2 -3
- package/lib/maths/index.d.ts +0 -1
- package/lib/maths/index.js +0 -1
- package/lib/position/Position.d.ts +5 -26
- package/lib/position/Position.js +29 -36
- package/lib/tests/mocks/markets.d.ts +1 -0
- package/lib/tests/mocks/markets.js +35 -29
- package/lib/token/Token.d.ts +4 -3
- package/lib/token/Token.js +28 -3
- package/lib/token/WrappedToken.d.ts +3 -3
- package/lib/token/WrappedToken.js +18 -11
- package/lib/token/index.d.ts +1 -1
- package/lib/token/index.js +1 -1
- package/lib/types.d.ts +1 -0
- package/lib/types.js +3 -4
- package/lib/user/User.js +12 -0
- package/lib/vault/Vault.d.ts +12 -12
- package/lib/vault/Vault.js +102 -18
- package/lib/vault/VaultConfig.js +9 -3
- package/lib/vault/VaultMarketAllocation.d.ts +20 -0
- package/lib/vault/VaultMarketAllocation.js +30 -0
- package/lib/vault/VaultMarketConfig.d.ts +43 -0
- package/lib/vault/VaultMarketConfig.js +43 -0
- package/lib/vault/VaultMarketPublicAllocatorConfig.d.ts +29 -0
- package/lib/vault/VaultMarketPublicAllocatorConfig.js +28 -0
- package/lib/vault/VaultUtils.d.ts +8 -9
- package/lib/vault/VaultUtils.js +2 -3
- package/lib/vault/index.d.ts +3 -1
- package/lib/vault/index.js +3 -1
- package/package.json +3 -6
- package/lib/chain/chain.constants.d.ts +0 -3
- package/lib/chain/chain.constants.js +0 -232
- package/lib/chain/chain.test.js +0 -37
- package/lib/chain/chain.types.d.ts +0 -20
- package/lib/chain/chain.types.js +0 -30
- package/lib/chain/chain.utils.d.ts +0 -14
- package/lib/chain/chain.utils.js +0 -30
- package/lib/chain/index.d.ts +0 -2
- package/lib/chain/index.js +0 -18
- package/lib/ethers/ethers.test.d.ts +0 -1
- package/lib/ethers/ethers.test.js +0 -11
- package/lib/ethers/index.d.ts +0 -2
- package/lib/ethers/index.js +0 -18
- package/lib/ethers/safeGetAddress.d.ts +0 -1
- package/lib/ethers/safeGetAddress.js +0 -6
- package/lib/ethers/safeParseUnits.d.ts +0 -2
- package/lib/ethers/safeParseUnits.js +0 -25
- package/lib/evm.d.ts +0 -36
- package/lib/evm.js +0 -113
- package/lib/helpers/getChecksumedAddress.d.ts +0 -7
- package/lib/helpers/getChecksumedAddress.js +0 -17
- package/lib/helpers/isZeroAddressOrUnset.d.ts +0 -7
- package/lib/helpers/isZeroAddressOrUnset.js +0 -14
- package/lib/maths/MathUtils.d.ts +0 -15
- package/lib/maths/MathUtils.js +0 -33
- package/lib/notifications.d.ts +0 -98
- package/lib/notifications.js +0 -52
- package/lib/signatures/index.d.ts +0 -12
- package/lib/signatures/index.js +0 -39
- package/lib/signatures/manager.d.ts +0 -10
- package/lib/signatures/manager.js +0 -37
- package/lib/signatures/permit.d.ts +0 -21
- package/lib/signatures/permit.js +0 -101
- package/lib/signatures/permit2.d.ts +0 -20
- package/lib/signatures/permit2.js +0 -91
- package/lib/signatures/types.d.ts +0 -13
- package/lib/signatures/types.js +0 -2
- package/lib/signatures/utils.d.ts +0 -6
- package/lib/signatures/utils.js +0 -44
- package/lib/token/ERC20Metadata.d.ts +0 -249
- package/lib/token/ERC20Metadata.js +0 -81
- package/lib/token/TokenNamespace.d.ts +0 -18
- package/lib/token/TokenNamespace.js +0 -55
- package/lib/vault/VaultAllocation.d.ts +0 -38
- package/lib/vault/VaultAllocation.js +0 -18
- /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
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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:
|
|
12
|
-
totalBorrowAssets:
|
|
25
|
+
totalSupplyAssets: BigIntish;
|
|
26
|
+
totalBorrowAssets: BigIntish;
|
|
13
27
|
}): bigint;
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
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:
|
|
36
|
-
price:
|
|
80
|
+
function getCollateralValue(collateral: BigIntish, { price }: {
|
|
81
|
+
price: BigIntish;
|
|
37
82
|
}): bigint;
|
|
38
|
-
function getMaxBorrowAssets(collateral:
|
|
39
|
-
price:
|
|
83
|
+
function getMaxBorrowAssets(collateral: BigIntish, market: {
|
|
84
|
+
price: BigIntish;
|
|
40
85
|
}, { lltv }: {
|
|
41
|
-
lltv:
|
|
86
|
+
lltv: BigIntish;
|
|
42
87
|
}): bigint;
|
|
43
88
|
function getMaxBorrowableAssets({ collateral, borrowShares, }: {
|
|
44
|
-
collateral:
|
|
45
|
-
borrowShares:
|
|
89
|
+
collateral: BigIntish;
|
|
90
|
+
borrowShares: BigIntish;
|
|
46
91
|
}, market: {
|
|
47
|
-
totalBorrowAssets:
|
|
48
|
-
totalBorrowShares:
|
|
49
|
-
price:
|
|
92
|
+
totalBorrowAssets: BigIntish;
|
|
93
|
+
totalBorrowShares: BigIntish;
|
|
94
|
+
price: BigIntish;
|
|
50
95
|
}, marketConfig: {
|
|
51
|
-
lltv:
|
|
96
|
+
lltv: BigIntish;
|
|
52
97
|
}): bigint;
|
|
53
|
-
function getLiquidationSeizedAssets(repaidShares:
|
|
54
|
-
totalBorrowAssets:
|
|
55
|
-
totalBorrowShares:
|
|
56
|
-
price:
|
|
98
|
+
function getLiquidationSeizedAssets(repaidShares: BigIntish, market: {
|
|
99
|
+
totalBorrowAssets: BigIntish;
|
|
100
|
+
totalBorrowShares: BigIntish;
|
|
101
|
+
price: BigIntish;
|
|
57
102
|
}, config: {
|
|
58
|
-
lltv:
|
|
103
|
+
lltv: BigIntish;
|
|
59
104
|
}): bigint;
|
|
60
|
-
function getLiquidationRepaidShares(seizedAssets:
|
|
61
|
-
totalBorrowAssets:
|
|
62
|
-
totalBorrowShares:
|
|
63
|
-
price:
|
|
105
|
+
function getLiquidationRepaidShares(seizedAssets: BigIntish, market: {
|
|
106
|
+
totalBorrowAssets: BigIntish;
|
|
107
|
+
totalBorrowShares: BigIntish;
|
|
108
|
+
price: BigIntish;
|
|
64
109
|
}, config: {
|
|
65
|
-
lltv:
|
|
110
|
+
lltv: BigIntish;
|
|
66
111
|
}): bigint;
|
|
67
112
|
function getSeizableCollateral(position: {
|
|
68
|
-
collateral:
|
|
69
|
-
borrowShares:
|
|
113
|
+
collateral: BigIntish;
|
|
114
|
+
borrowShares: BigIntish;
|
|
70
115
|
}, market: {
|
|
71
|
-
totalBorrowAssets:
|
|
72
|
-
totalBorrowShares:
|
|
73
|
-
price:
|
|
116
|
+
totalBorrowAssets: BigIntish;
|
|
117
|
+
totalBorrowShares: BigIntish;
|
|
118
|
+
price: BigIntish;
|
|
74
119
|
}, config: {
|
|
75
|
-
lltv:
|
|
120
|
+
lltv: BigIntish;
|
|
76
121
|
}): bigint;
|
|
77
122
|
function getWithdrawableCollateral({ collateral, borrowShares, }: {
|
|
78
|
-
collateral:
|
|
79
|
-
borrowShares:
|
|
123
|
+
collateral: BigIntish;
|
|
124
|
+
borrowShares: BigIntish;
|
|
80
125
|
}, market: {
|
|
81
|
-
totalBorrowAssets:
|
|
82
|
-
totalBorrowShares:
|
|
83
|
-
price:
|
|
126
|
+
totalBorrowAssets: BigIntish;
|
|
127
|
+
totalBorrowShares: BigIntish;
|
|
128
|
+
price: BigIntish;
|
|
84
129
|
}, { lltv }: {
|
|
85
|
-
lltv:
|
|
130
|
+
lltv: BigIntish;
|
|
86
131
|
}): bigint;
|
|
87
132
|
function isHealthy({ collateral, borrowShares, }: {
|
|
88
|
-
collateral:
|
|
89
|
-
borrowShares:
|
|
133
|
+
collateral: BigIntish;
|
|
134
|
+
borrowShares: BigIntish;
|
|
90
135
|
}, market: {
|
|
91
|
-
totalBorrowAssets:
|
|
92
|
-
totalBorrowShares:
|
|
93
|
-
price:
|
|
136
|
+
totalBorrowAssets: BigIntish;
|
|
137
|
+
totalBorrowShares: BigIntish;
|
|
138
|
+
price: BigIntish;
|
|
94
139
|
}, marketConfig: {
|
|
95
|
-
lltv:
|
|
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:
|
|
104
|
-
borrowShares:
|
|
148
|
+
collateral: BigIntish;
|
|
149
|
+
borrowShares: BigIntish;
|
|
105
150
|
}, market: {
|
|
106
|
-
totalBorrowAssets:
|
|
107
|
-
totalBorrowShares:
|
|
151
|
+
totalBorrowAssets: BigIntish;
|
|
152
|
+
totalBorrowShares: BigIntish;
|
|
108
153
|
}, marketConfig: {
|
|
109
|
-
lltv:
|
|
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:
|
|
113
|
-
borrowShares:
|
|
161
|
+
collateral: BigIntish;
|
|
162
|
+
borrowShares: BigIntish;
|
|
114
163
|
}, market: {
|
|
115
|
-
totalBorrowAssets:
|
|
116
|
-
totalBorrowShares:
|
|
117
|
-
price:
|
|
164
|
+
totalBorrowAssets: BigIntish;
|
|
165
|
+
totalBorrowShares: BigIntish;
|
|
166
|
+
price: BigIntish;
|
|
118
167
|
}, marketConfig: {
|
|
119
|
-
lltv:
|
|
168
|
+
lltv: BigIntish;
|
|
120
169
|
}): bigint | null;
|
|
121
170
|
function getHealthFactor({ collateral, borrowShares, }: {
|
|
122
|
-
collateral:
|
|
123
|
-
borrowShares:
|
|
171
|
+
collateral: BigIntish;
|
|
172
|
+
borrowShares: BigIntish;
|
|
124
173
|
}, market: {
|
|
125
|
-
totalBorrowAssets:
|
|
126
|
-
totalBorrowShares:
|
|
127
|
-
price:
|
|
174
|
+
totalBorrowAssets: BigIntish;
|
|
175
|
+
totalBorrowShares: BigIntish;
|
|
176
|
+
price: BigIntish;
|
|
128
177
|
}, marketConfig: {
|
|
129
|
-
lltv:
|
|
178
|
+
lltv: BigIntish;
|
|
130
179
|
}): bigint | null;
|
|
131
180
|
function getLtv({ collateral, borrowShares, }: {
|
|
132
|
-
collateral:
|
|
133
|
-
borrowShares:
|
|
181
|
+
collateral: BigIntish;
|
|
182
|
+
borrowShares: BigIntish;
|
|
134
183
|
}, market: {
|
|
135
|
-
totalBorrowAssets:
|
|
136
|
-
totalBorrowShares:
|
|
137
|
-
price:
|
|
184
|
+
totalBorrowAssets: BigIntish;
|
|
185
|
+
totalBorrowShares: BigIntish;
|
|
186
|
+
price: BigIntish;
|
|
138
187
|
}): bigint | null;
|
|
139
188
|
function getBorrowCapacityUsage(position: {
|
|
140
|
-
collateral:
|
|
141
|
-
borrowShares:
|
|
189
|
+
collateral: BigIntish;
|
|
190
|
+
borrowShares: BigIntish;
|
|
142
191
|
}, market: {
|
|
143
|
-
totalBorrowAssets:
|
|
144
|
-
totalBorrowShares:
|
|
145
|
-
price:
|
|
192
|
+
totalBorrowAssets: BigIntish;
|
|
193
|
+
totalBorrowShares: BigIntish;
|
|
194
|
+
price: BigIntish;
|
|
146
195
|
}, marketConfig: {
|
|
147
|
-
lltv:
|
|
196
|
+
lltv: BigIntish;
|
|
148
197
|
}): bigint | null;
|
|
149
|
-
function toSupplyAssets(shares:
|
|
150
|
-
totalSupplyAssets:
|
|
151
|
-
totalSupplyShares:
|
|
198
|
+
function toSupplyAssets(shares: BigIntish, market: {
|
|
199
|
+
totalSupplyAssets: BigIntish;
|
|
200
|
+
totalSupplyShares: BigIntish;
|
|
152
201
|
}, rounding?: RoundingDirection): bigint;
|
|
153
|
-
function toSupplyShares(assets:
|
|
154
|
-
totalSupplyAssets:
|
|
155
|
-
totalSupplyShares:
|
|
202
|
+
function toSupplyShares(assets: BigIntish, market: {
|
|
203
|
+
totalSupplyAssets: BigIntish;
|
|
204
|
+
totalSupplyShares: BigIntish;
|
|
156
205
|
}, rounding?: RoundingDirection): bigint;
|
|
157
|
-
function toBorrowAssets(shares:
|
|
158
|
-
totalBorrowAssets:
|
|
159
|
-
totalBorrowShares:
|
|
206
|
+
function toBorrowAssets(shares: BigIntish, market: {
|
|
207
|
+
totalBorrowAssets: BigIntish;
|
|
208
|
+
totalBorrowShares: BigIntish;
|
|
160
209
|
}, rounding?: RoundingDirection): bigint;
|
|
161
|
-
function toBorrowShares(assets:
|
|
162
|
-
totalBorrowAssets:
|
|
163
|
-
totalBorrowShares:
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
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 - (
|
|
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 = (
|
|
27
|
-
|
|
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 - (
|
|
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: (
|
|
82
|
+
totalSupplyAssets: BigInt(totalSupplyAssets) - feeAmount,
|
|
46
83
|
totalSupplyShares,
|
|
47
84
|
}, "Down");
|
|
48
85
|
return { interest, feeShares };
|
|
49
86
|
}
|
|
50
87
|
MarketUtils.getAccruedInterest = getAccruedInterest;
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
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.
|
|
101
|
+
return maths_1.MathLib.zeroFloorSub(totalSupplyAssets, maths_1.MathLib.wDivUp(totalBorrowAssets, utilization));
|
|
61
102
|
}
|
|
62
|
-
MarketUtils.
|
|
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.
|
|
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 = (
|
|
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 = (
|
|
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 = (
|
|
148
|
+
market.price = BigInt(market.price);
|
|
99
149
|
if (market.price === 0n)
|
|
100
150
|
return 0n;
|
|
101
|
-
return maths_1.
|
|
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 = (
|
|
116
|
-
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
|
|
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 = (
|
|
181
|
+
market.price = BigInt(market.price);
|
|
182
|
+
if (market.price === 0n)
|
|
183
|
+
return null;
|
|
128
184
|
const liquidationPrice = getLiquidationPrice(position, market, marketConfig);
|
|
129
|
-
if (
|
|
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 = (
|
|
136
|
-
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
|
|
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 = (
|
|
148
|
-
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
|
|
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
|
|
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:
|
|
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
|
|
6
|
-
const TARGET_UTILIZATION
|
|
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:
|
|
33
|
-
function getBorrowRate(startUtilization:
|
|
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 =
|
|
13
|
-
AdaptiveCurveIrmLib.TARGET_UTILIZATION =
|
|
14
|
-
AdaptiveCurveIrmLib.INITIAL_RATE_AT_TARGET =
|
|
15
|
-
AdaptiveCurveIrmLib.ADJUSTMENT_SPEED =
|
|
16
|
-
AdaptiveCurveIrmLib.MIN_RATE_AT_TARGET =
|
|
17
|
-
AdaptiveCurveIrmLib.MAX_RATE_AT_TARGET =
|
|
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;
|