@morpho-org/blue-sdk 1.0.1 → 1.0.2
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/package.json +10 -6
- package/src/addresses.ts +261 -0
- package/src/chain/chain.constants.ts +235 -0
- package/src/chain/chain.test.ts +51 -0
- package/src/chain/chain.types.ts +42 -0
- package/src/chain/chain.utils.ts +44 -0
- package/src/constants.ts +18 -0
- package/src/errors.ts +75 -0
- package/src/ethers/ethers.test.ts +17 -0
- package/src/ethers/safeGetAddress.ts +4 -0
- package/src/ethers/safeParseUnits.ts +29 -0
- package/src/evm.ts +172 -0
- package/src/helpers/format/format.test.ts +340 -0
- package/src/helpers/format/format.ts +416 -0
- package/src/helpers/getChecksumedAddress.ts +15 -0
- package/{lib/helpers/isZeroAddressOrUnset.d.ts → src/helpers/isZeroAddressOrUnset.ts} +7 -1
- package/src/helpers/locale.ts +108 -0
- package/src/holding/Holding.ts +109 -0
- package/src/market/Market.ts +479 -0
- package/src/market/MarketConfig.ts +108 -0
- package/src/market/MarketUtils.test.ts +25 -0
- package/src/market/MarketUtils.ts +467 -0
- package/src/maths/AdaptiveCurveIrmLib.ts +143 -0
- package/src/maths/MathLib.ts +208 -0
- package/src/maths/MathUtils.ts +31 -0
- package/src/maths/SharesMath.ts +40 -0
- package/src/notifications.ts +167 -0
- package/src/position/Position.ts +251 -0
- package/src/signatures/index.ts +18 -0
- package/src/signatures/manager.ts +50 -0
- package/src/signatures/permit.ts +126 -0
- package/src/signatures/permit2.ts +120 -0
- package/src/signatures/types.ts +18 -0
- package/src/signatures/utils.ts +83 -0
- package/src/tests/mocks/markets.ts +110 -0
- package/src/token/ERC20Metadata.ts +124 -0
- package/src/token/Token.ts +83 -0
- package/src/token/TokenNamespace.ts +76 -0
- package/src/token/WrappedToken.ts +142 -0
- package/src/types.ts +37 -0
- package/src/user/User.ts +32 -0
- package/src/user/user.types.ts +23 -0
- package/src/vault/Vault.ts +370 -0
- package/src/vault/VaultAllocation.ts +58 -0
- package/src/vault/VaultConfig.ts +55 -0
- package/src/vault/VaultUtils.ts +47 -0
- package/lib/addresses.d.ts +0 -168
- package/lib/addresses.js +0 -169
- package/lib/chain/chain.constants.d.ts +0 -3
- package/lib/chain/chain.constants.js +0 -232
- 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.js +0 -18
- package/lib/constants.d.ts +0 -8
- package/lib/constants.js +0 -13
- package/lib/errors.d.ts +0 -37
- package/lib/errors.js +0 -71
- 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/format/format.d.ts +0 -98
- package/lib/helpers/format/format.js +0 -301
- package/lib/helpers/format/index.js +0 -17
- package/lib/helpers/getChecksumedAddress.d.ts +0 -7
- package/lib/helpers/getChecksumedAddress.js +0 -17
- package/lib/helpers/index.js +0 -20
- package/lib/helpers/isZeroAddressOrUnset.js +0 -14
- package/lib/helpers/locale.d.ts +0 -36
- package/lib/helpers/locale.js +0 -86
- package/lib/holding/Holding.d.ts +0 -60
- package/lib/holding/Holding.js +0 -31
- package/lib/holding/index.js +0 -17
- package/lib/index.d.ts +0 -33
- package/lib/index.js +0 -62
- package/lib/market/Market.d.ts +0 -159
- package/lib/market/Market.js +0 -240
- package/lib/market/MarketConfig.d.ts +0 -44
- package/lib/market/MarketConfig.js +0 -56
- package/lib/market/MarketUtils.d.ts +0 -165
- package/lib/market/MarketUtils.js +0 -182
- package/lib/market/index.js +0 -19
- package/lib/maths/AdaptiveCurveIrmLib.d.ts +0 -37
- package/lib/maths/AdaptiveCurveIrmLib.js +0 -116
- package/lib/maths/MathLib.d.ts +0 -94
- package/lib/maths/MathLib.js +0 -153
- package/lib/maths/MathUtils.d.ts +0 -15
- package/lib/maths/MathUtils.js +0 -33
- package/lib/maths/SharesMath.d.ts +0 -12
- package/lib/maths/SharesMath.js +0 -22
- package/lib/maths/index.js +0 -20
- package/lib/notifications.d.ts +0 -98
- package/lib/notifications.js +0 -52
- package/lib/position/Position.d.ts +0 -118
- package/lib/position/Position.js +0 -145
- package/lib/position/index.js +0 -17
- 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/tests/mocks/markets.d.ts +0 -17
- package/lib/tests/mocks/markets.js +0 -108
- package/lib/token/ERC20Metadata.d.ts +0 -249
- package/lib/token/ERC20Metadata.js +0 -81
- package/lib/token/Token.d.ts +0 -45
- package/lib/token/Token.js +0 -39
- package/lib/token/TokenNamespace.d.ts +0 -18
- package/lib/token/TokenNamespace.js +0 -55
- package/lib/token/WrappedToken.d.ts +0 -42
- package/lib/token/WrappedToken.js +0 -87
- package/lib/token/index.js +0 -18
- package/lib/types.d.ts +0 -29
- package/lib/types.js +0 -23
- package/lib/user/User.d.ts +0 -20
- package/lib/user/User.js +0 -11
- package/lib/user/index.js +0 -18
- package/lib/user/user.types.d.ts +0 -18
- package/lib/user/user.types.js +0 -2
- package/lib/vault/Vault.d.ts +0 -167
- package/lib/vault/Vault.js +0 -156
- package/lib/vault/VaultAllocation.d.ts +0 -38
- package/lib/vault/VaultAllocation.js +0 -18
- package/lib/vault/VaultConfig.d.ts +0 -23
- package/lib/vault/VaultConfig.js +0 -26
- package/lib/vault/VaultUtils.d.ts +0 -17
- package/lib/vault/VaultUtils.js +0 -17
- package/lib/vault/index.js +0 -20
- /package/{lib/chain/index.d.ts → src/chain/index.ts} +0 -0
- /package/{lib/ethers/index.d.ts → src/ethers/index.ts} +0 -0
- /package/{lib/helpers/format/index.d.ts → src/helpers/format/index.ts} +0 -0
- /package/{lib/helpers/index.d.ts → src/helpers/index.ts} +0 -0
- /package/{lib/holding/index.d.ts → src/holding/index.ts} +0 -0
- /package/{lib/market/index.d.ts → src/market/index.ts} +0 -0
- /package/{lib/maths/index.d.ts → src/maths/index.ts} +0 -0
- /package/{lib/position/index.d.ts → src/position/index.ts} +0 -0
- /package/{lib/token/index.d.ts → src/token/index.ts} +0 -0
- /package/{lib/user/index.d.ts → src/user/index.ts} +0 -0
- /package/{lib/vault/index.d.ts → src/vault/index.ts} +0 -0
package/lib/market/Market.js
DELETED
|
@@ -1,240 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Market = exports.CapacityLimitReason = void 0;
|
|
4
|
-
const ethers_1 = require("ethers");
|
|
5
|
-
const ethers_types_1 = require("ethers-types");
|
|
6
|
-
const addresses_1 = require("../addresses");
|
|
7
|
-
const chain_1 = require("../chain");
|
|
8
|
-
const errors_1 = require("../errors");
|
|
9
|
-
const maths_1 = require("../maths");
|
|
10
|
-
const MarketConfig_1 = require("./MarketConfig");
|
|
11
|
-
const MarketUtils_1 = require("./MarketUtils");
|
|
12
|
-
var CapacityLimitReason;
|
|
13
|
-
(function (CapacityLimitReason) {
|
|
14
|
-
CapacityLimitReason["liquidity"] = "Liquidity";
|
|
15
|
-
CapacityLimitReason["balance"] = "Balance";
|
|
16
|
-
CapacityLimitReason["position"] = "Position";
|
|
17
|
-
CapacityLimitReason["collateral"] = "Collateral";
|
|
18
|
-
CapacityLimitReason["cap"] = "Cap";
|
|
19
|
-
})(CapacityLimitReason || (exports.CapacityLimitReason = CapacityLimitReason = {}));
|
|
20
|
-
class Market {
|
|
21
|
-
static async fetchFromId(id, runner, { chainId, overrides = {}, } = {}) {
|
|
22
|
-
chainId ?? (chainId = chain_1.ChainUtils.parseSupportedChainId((await runner.provider.getNetwork()).chainId));
|
|
23
|
-
const config = await MarketConfig_1.MarketConfig.fetch(id, runner, chainId);
|
|
24
|
-
return Market.fetchFromConfig(config, runner, { chainId, overrides });
|
|
25
|
-
}
|
|
26
|
-
static async fetchFromConfig(config, runner, { chainId, overrides = {}, } = {}) {
|
|
27
|
-
chainId ?? (chainId = chain_1.ChainUtils.parseSupportedChainId((await runner.provider.getNetwork()).chainId));
|
|
28
|
-
const { morpho, adaptiveCurveIrm } = (0, addresses_1.getChainAddresses)(chainId);
|
|
29
|
-
const [{ totalSupplyAssets, totalSupplyShares, totalBorrowShares, totalBorrowAssets, lastUpdate, fee, }, price, rateAtTarget,] = await Promise.all([
|
|
30
|
-
ethers_types_1.MorphoBlue__factory.connect(morpho, runner).market(config.id, overrides),
|
|
31
|
-
config.oracle !== ethers_1.ZeroAddress
|
|
32
|
-
? ethers_types_1.BlueOracle__factory.connect(config.oracle, runner).price(overrides)
|
|
33
|
-
: 0n,
|
|
34
|
-
config.irm === adaptiveCurveIrm
|
|
35
|
-
? await ethers_types_1.AdaptiveCurveIrm__factory.connect(config.irm, runner).rateAtTarget(config.id, overrides)
|
|
36
|
-
: undefined,
|
|
37
|
-
]);
|
|
38
|
-
return new Market({
|
|
39
|
-
config,
|
|
40
|
-
totalSupplyAssets,
|
|
41
|
-
totalBorrowAssets,
|
|
42
|
-
totalSupplyShares,
|
|
43
|
-
totalBorrowShares,
|
|
44
|
-
lastUpdate,
|
|
45
|
-
fee,
|
|
46
|
-
price,
|
|
47
|
-
rateAtTarget,
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
constructor({ config, totalSupplyAssets, totalBorrowAssets, totalSupplyShares, totalBorrowShares, lastUpdate, fee, price, rateAtTarget, }) {
|
|
51
|
-
this.config = config;
|
|
52
|
-
this.totalSupplyAssets = (0, ethers_1.toBigInt)(totalSupplyAssets);
|
|
53
|
-
this.totalBorrowAssets = (0, ethers_1.toBigInt)(totalBorrowAssets);
|
|
54
|
-
this.totalSupplyShares = (0, ethers_1.toBigInt)(totalSupplyShares);
|
|
55
|
-
this.totalBorrowShares = (0, ethers_1.toBigInt)(totalBorrowShares);
|
|
56
|
-
this.lastUpdate = (0, ethers_1.toBigInt)(lastUpdate);
|
|
57
|
-
this.fee = (0, ethers_1.toBigInt)(fee);
|
|
58
|
-
this.price = (0, ethers_1.toBigInt)(price);
|
|
59
|
-
if (rateAtTarget != null)
|
|
60
|
-
this.rateAtTarget = (0, ethers_1.toBigInt)(rateAtTarget);
|
|
61
|
-
}
|
|
62
|
-
get id() {
|
|
63
|
-
return this.config.id;
|
|
64
|
-
}
|
|
65
|
-
get isIdle() {
|
|
66
|
-
return this.config.collateralToken === ethers_1.ZeroAddress;
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* @warning Cannot be used to calculate the liquidity available inside a callback,
|
|
70
|
-
* because the balance of Blue may be lower than the market's liquidity due to assets being transferred out prior to the callback.
|
|
71
|
-
*/
|
|
72
|
-
get liquidity() {
|
|
73
|
-
return this.totalSupplyAssets - this.totalBorrowAssets;
|
|
74
|
-
}
|
|
75
|
-
get utilization() {
|
|
76
|
-
return MarketUtils_1.MarketUtils.getUtilization(this);
|
|
77
|
-
}
|
|
78
|
-
get apyAtTarget() {
|
|
79
|
-
if (this.rateAtTarget == null)
|
|
80
|
-
return;
|
|
81
|
-
return MarketUtils_1.MarketUtils.getApy(this.rateAtTarget);
|
|
82
|
-
}
|
|
83
|
-
get supplyRate() {
|
|
84
|
-
return MarketUtils_1.MarketUtils.getSupplyRate(this.borrowRate, this);
|
|
85
|
-
}
|
|
86
|
-
get borrowRate() {
|
|
87
|
-
if (this.rateAtTarget == null)
|
|
88
|
-
return 0n;
|
|
89
|
-
return maths_1.AdaptiveCurveIrmLib.getBorrowRate(this.utilization, this.rateAtTarget, 0n).avgBorrowRate;
|
|
90
|
-
}
|
|
91
|
-
get supplyApy() {
|
|
92
|
-
return MarketUtils_1.MarketUtils.getApy(this.supplyRate);
|
|
93
|
-
}
|
|
94
|
-
get borrowApy() {
|
|
95
|
-
return MarketUtils_1.MarketUtils.getApy(this.borrowRate);
|
|
96
|
-
}
|
|
97
|
-
accrueInterest(timestamp) {
|
|
98
|
-
timestamp = (0, ethers_1.toBigInt)(timestamp);
|
|
99
|
-
const elapsed = timestamp - this.lastUpdate;
|
|
100
|
-
if (elapsed < 0n)
|
|
101
|
-
throw new errors_1.InvalidInterestAccrualError(this.id, timestamp, this.lastUpdate);
|
|
102
|
-
let borrowRate = 0n;
|
|
103
|
-
let { rateAtTarget } = this;
|
|
104
|
-
if (this.rateAtTarget != null) {
|
|
105
|
-
const { avgBorrowRate, endRateAtTarget } = maths_1.AdaptiveCurveIrmLib.getBorrowRate(this.utilization, this.rateAtTarget, elapsed);
|
|
106
|
-
borrowRate = avgBorrowRate;
|
|
107
|
-
rateAtTarget = endRateAtTarget;
|
|
108
|
-
}
|
|
109
|
-
const { interest, feeShares } = MarketUtils_1.MarketUtils.getAccruedInterest(borrowRate, this, elapsed);
|
|
110
|
-
return new Market({
|
|
111
|
-
...this,
|
|
112
|
-
totalSupplyAssets: this.totalSupplyAssets + interest,
|
|
113
|
-
totalBorrowAssets: this.totalBorrowAssets + interest,
|
|
114
|
-
totalSupplyShares: this.totalSupplyShares + feeShares,
|
|
115
|
-
lastUpdate: timestamp,
|
|
116
|
-
rateAtTarget,
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
getLiquidityToUtilization(utilization) {
|
|
120
|
-
return MarketUtils_1.MarketUtils.getLiquidityToUtilization(this, utilization);
|
|
121
|
-
}
|
|
122
|
-
getCollateralValue(collateral) {
|
|
123
|
-
return MarketUtils_1.MarketUtils.getCollateralValue(collateral, this);
|
|
124
|
-
}
|
|
125
|
-
getMaxBorrowAssets(collateral) {
|
|
126
|
-
return MarketUtils_1.MarketUtils.getMaxBorrowAssets(collateral, this, this.config);
|
|
127
|
-
}
|
|
128
|
-
getMaxBorrowableAssets(position) {
|
|
129
|
-
return MarketUtils_1.MarketUtils.getMaxBorrowableAssets(position, this, this.config);
|
|
130
|
-
}
|
|
131
|
-
getLiquidationSeizedAssets(repaidShares) {
|
|
132
|
-
return MarketUtils_1.MarketUtils.getLiquidationSeizedAssets(repaidShares, this, this.config);
|
|
133
|
-
}
|
|
134
|
-
getLiquidationRepaidShares(seizedAssets) {
|
|
135
|
-
return MarketUtils_1.MarketUtils.getLiquidationRepaidShares(seizedAssets, this, this.config);
|
|
136
|
-
}
|
|
137
|
-
getSeizableCollateral(position) {
|
|
138
|
-
return MarketUtils_1.MarketUtils.getSeizableCollateral(position, this, this.config);
|
|
139
|
-
}
|
|
140
|
-
getWithdrawableCollateral(position) {
|
|
141
|
-
return MarketUtils_1.MarketUtils.getWithdrawableCollateral(position, this, this.config);
|
|
142
|
-
}
|
|
143
|
-
isHealthy(position) {
|
|
144
|
-
return MarketUtils_1.MarketUtils.isHealthy(position, this, this.config);
|
|
145
|
-
}
|
|
146
|
-
getLiquidationPrice(position) {
|
|
147
|
-
return MarketUtils_1.MarketUtils.getLiquidationPrice(position, this, this.config);
|
|
148
|
-
}
|
|
149
|
-
getPriceVariationToLiquidation(position) {
|
|
150
|
-
return MarketUtils_1.MarketUtils.getPriceVariationToLiquidation(position, this, this.config);
|
|
151
|
-
}
|
|
152
|
-
getHealthFactor(position) {
|
|
153
|
-
return MarketUtils_1.MarketUtils.getHealthFactor(position, this, this.config);
|
|
154
|
-
}
|
|
155
|
-
getLtv(position) {
|
|
156
|
-
return MarketUtils_1.MarketUtils.getLtv(position, this);
|
|
157
|
-
}
|
|
158
|
-
getBorrowCapacityUsage(position) {
|
|
159
|
-
return MarketUtils_1.MarketUtils.getBorrowCapacityUsage(position, this, this.config);
|
|
160
|
-
}
|
|
161
|
-
toSupplyAssets(shares, rounding) {
|
|
162
|
-
return MarketUtils_1.MarketUtils.toSupplyAssets(shares, this, rounding);
|
|
163
|
-
}
|
|
164
|
-
toSupplyShares(assets, rounding) {
|
|
165
|
-
return MarketUtils_1.MarketUtils.toSupplyShares(assets, this, rounding);
|
|
166
|
-
}
|
|
167
|
-
toBorrowAssets(shares, rounding) {
|
|
168
|
-
return MarketUtils_1.MarketUtils.toBorrowAssets(shares, this, rounding);
|
|
169
|
-
}
|
|
170
|
-
toBorrowShares(assets, rounding) {
|
|
171
|
-
return MarketUtils_1.MarketUtils.toBorrowShares(assets, this, rounding);
|
|
172
|
-
}
|
|
173
|
-
getBorrowCapacityLimit(collateral) {
|
|
174
|
-
const maxBorrowableAssets = this.getMaxBorrowAssets(collateral);
|
|
175
|
-
const { liquidity } = this;
|
|
176
|
-
if (maxBorrowableAssets > liquidity)
|
|
177
|
-
return {
|
|
178
|
-
value: liquidity,
|
|
179
|
-
limiter: CapacityLimitReason.liquidity,
|
|
180
|
-
};
|
|
181
|
-
return {
|
|
182
|
-
value: maxBorrowableAssets,
|
|
183
|
-
limiter: CapacityLimitReason.collateral,
|
|
184
|
-
};
|
|
185
|
-
}
|
|
186
|
-
getRepayCapacityLimit(borrowShares, loanTokenBalance) {
|
|
187
|
-
const borrowAssets = this.toBorrowAssets(borrowShares);
|
|
188
|
-
if (borrowAssets > loanTokenBalance)
|
|
189
|
-
return {
|
|
190
|
-
value: loanTokenBalance,
|
|
191
|
-
limiter: CapacityLimitReason.balance,
|
|
192
|
-
};
|
|
193
|
-
return {
|
|
194
|
-
value: borrowAssets,
|
|
195
|
-
limiter: CapacityLimitReason.position,
|
|
196
|
-
};
|
|
197
|
-
}
|
|
198
|
-
getWithdrawCapacityLimit(supplyShares) {
|
|
199
|
-
const supplyAssets = this.toSupplyAssets(supplyShares);
|
|
200
|
-
const { liquidity } = this;
|
|
201
|
-
if (supplyAssets > liquidity)
|
|
202
|
-
return {
|
|
203
|
-
value: liquidity,
|
|
204
|
-
limiter: CapacityLimitReason.liquidity,
|
|
205
|
-
};
|
|
206
|
-
return {
|
|
207
|
-
value: supplyAssets,
|
|
208
|
-
limiter: CapacityLimitReason.position,
|
|
209
|
-
};
|
|
210
|
-
}
|
|
211
|
-
getWithdrawCollateralCapacityLimit(position) {
|
|
212
|
-
const withdrawableCollateral = this.getWithdrawableCollateral(position);
|
|
213
|
-
if (position.collateral > withdrawableCollateral)
|
|
214
|
-
return {
|
|
215
|
-
value: withdrawableCollateral,
|
|
216
|
-
limiter: CapacityLimitReason.collateral,
|
|
217
|
-
};
|
|
218
|
-
return {
|
|
219
|
-
value: position.collateral,
|
|
220
|
-
limiter: CapacityLimitReason.position,
|
|
221
|
-
};
|
|
222
|
-
}
|
|
223
|
-
getMaxCapacities(position, loanTokenBalance, collateralTokenBalance) {
|
|
224
|
-
return {
|
|
225
|
-
supply: {
|
|
226
|
-
value: loanTokenBalance,
|
|
227
|
-
limiter: CapacityLimitReason.balance,
|
|
228
|
-
},
|
|
229
|
-
withdraw: this.getWithdrawCapacityLimit(position.supplyShares),
|
|
230
|
-
borrow: this.getBorrowCapacityLimit(position.collateral),
|
|
231
|
-
repay: this.getRepayCapacityLimit(position.borrowShares, loanTokenBalance),
|
|
232
|
-
supplyCollateral: {
|
|
233
|
-
value: collateralTokenBalance,
|
|
234
|
-
limiter: CapacityLimitReason.balance,
|
|
235
|
-
},
|
|
236
|
-
withdrawCollateral: this.getWithdrawCollateralCapacityLimit(position),
|
|
237
|
-
};
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
exports.Market = Market;
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { Provider } from "ethers";
|
|
2
|
-
import { MarketParamsStruct } from "ethers-types/dist/protocols/morpho/blue/MorphoBlue";
|
|
3
|
-
import { ChainId } from "../chain";
|
|
4
|
-
import { Address, MarketId } from "../types";
|
|
5
|
-
export interface MarketParams extends MarketParamsStruct {
|
|
6
|
-
loanToken: Address;
|
|
7
|
-
collateralToken: Address;
|
|
8
|
-
oracle: Address;
|
|
9
|
-
irm: Address;
|
|
10
|
-
}
|
|
11
|
-
export declare class MarketConfig implements MarketParams {
|
|
12
|
-
private static readonly _CACHE;
|
|
13
|
-
static get(id: MarketId): MarketConfig;
|
|
14
|
-
static fetch(id: MarketId, runner: {
|
|
15
|
-
provider: Provider;
|
|
16
|
-
}, chainId?: ChainId): Promise<MarketConfig>;
|
|
17
|
-
static idle(token: Address): MarketConfig;
|
|
18
|
-
/**
|
|
19
|
-
* The market's collateral token address.
|
|
20
|
-
*/
|
|
21
|
-
readonly collateralToken: Address;
|
|
22
|
-
/**
|
|
23
|
-
* The market's loan token address.
|
|
24
|
-
*/
|
|
25
|
-
readonly loanToken: Address;
|
|
26
|
-
/**
|
|
27
|
-
* The market's oracle address.
|
|
28
|
-
*/
|
|
29
|
-
readonly oracle: Address;
|
|
30
|
-
/**
|
|
31
|
-
* The market's interest rate model address.
|
|
32
|
-
*/
|
|
33
|
-
readonly irm: Address;
|
|
34
|
-
/**
|
|
35
|
-
* The market's liquidation Loan-To-Value (scaled by WAD).
|
|
36
|
-
*/
|
|
37
|
-
readonly lltv: bigint;
|
|
38
|
-
constructor({ collateralToken, loanToken, oracle, irm, lltv }: MarketParams);
|
|
39
|
-
/**
|
|
40
|
-
* The market's hex-encoded id, defined as the hash of the market's params.
|
|
41
|
-
*/
|
|
42
|
-
get id(): MarketId;
|
|
43
|
-
get liquidationIncentiveFactor(): bigint;
|
|
44
|
-
}
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.MarketConfig = void 0;
|
|
4
|
-
const ethers_1 = require("ethers");
|
|
5
|
-
const ethers_types_1 = require("ethers-types");
|
|
6
|
-
const addresses_1 = require("../addresses");
|
|
7
|
-
const chain_1 = require("../chain");
|
|
8
|
-
const errors_1 = require("../errors");
|
|
9
|
-
const MarketUtils_1 = require("./MarketUtils");
|
|
10
|
-
class MarketConfig {
|
|
11
|
-
static get(id) {
|
|
12
|
-
const marketConfig = MarketConfig._CACHE[id];
|
|
13
|
-
if (!marketConfig)
|
|
14
|
-
throw new errors_1.UnknownMarketConfigError(id);
|
|
15
|
-
return marketConfig;
|
|
16
|
-
}
|
|
17
|
-
static async fetch(id, runner, chainId) {
|
|
18
|
-
let config = (0, errors_1._try)(() => MarketConfig.get(id), errors_1.UnknownMarketConfigError);
|
|
19
|
-
if (!config) {
|
|
20
|
-
chainId ?? (chainId = chain_1.ChainUtils.parseSupportedChainId((await runner.provider.getNetwork()).chainId));
|
|
21
|
-
const { morpho } = (0, addresses_1.getChainAddresses)(chainId);
|
|
22
|
-
config = new MarketConfig(
|
|
23
|
-
// Always fetch at latest block because config is immutable.
|
|
24
|
-
await ethers_types_1.MorphoBlue__factory.connect(morpho, runner).idToMarketParams(id));
|
|
25
|
-
}
|
|
26
|
-
return config;
|
|
27
|
-
}
|
|
28
|
-
static idle(token) {
|
|
29
|
-
return new MarketConfig({
|
|
30
|
-
collateralToken: ethers_1.ZeroAddress,
|
|
31
|
-
loanToken: token,
|
|
32
|
-
oracle: ethers_1.ZeroAddress,
|
|
33
|
-
irm: ethers_1.ZeroAddress,
|
|
34
|
-
lltv: 0n,
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
constructor({ collateralToken, loanToken, oracle, irm, lltv }) {
|
|
38
|
-
this.collateralToken = collateralToken;
|
|
39
|
-
this.loanToken = loanToken;
|
|
40
|
-
this.oracle = oracle;
|
|
41
|
-
this.irm = irm;
|
|
42
|
-
this.lltv = (0, ethers_1.toBigInt)(lltv);
|
|
43
|
-
MarketConfig._CACHE[this.id] = this;
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* The market's hex-encoded id, defined as the hash of the market's params.
|
|
47
|
-
*/
|
|
48
|
-
get id() {
|
|
49
|
-
return MarketUtils_1.MarketUtils.getMarketId(this);
|
|
50
|
-
}
|
|
51
|
-
get liquidationIncentiveFactor() {
|
|
52
|
-
return MarketUtils_1.MarketUtils.getLiquidationIncentiveFactor(this);
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
exports.MarketConfig = MarketConfig;
|
|
56
|
-
MarketConfig._CACHE = {};
|
|
@@ -1,165 +0,0 @@
|
|
|
1
|
-
import { BigNumberish } from "ethers";
|
|
2
|
-
import { MarketParamsStruct } from "ethers-types/dist/protocols/morpho/blue/MorphoBlue";
|
|
3
|
-
import { RoundingDirection } from "../maths";
|
|
4
|
-
import { MarketId } from "../types";
|
|
5
|
-
export declare namespace MarketUtils {
|
|
6
|
-
function getMarketId(market: MarketParamsStruct): MarketId;
|
|
7
|
-
function getLiquidationIncentiveFactor({ lltv, }: {
|
|
8
|
-
lltv: BigNumberish;
|
|
9
|
-
}): bigint;
|
|
10
|
-
function getUtilization({ totalSupplyAssets, totalBorrowAssets, }: {
|
|
11
|
-
totalSupplyAssets: BigNumberish;
|
|
12
|
-
totalBorrowAssets: BigNumberish;
|
|
13
|
-
}): bigint;
|
|
14
|
-
function getSupplyRate(borrowRate: BigNumberish, { utilization, fee }: {
|
|
15
|
-
utilization: BigNumberish;
|
|
16
|
-
fee: BigNumberish;
|
|
17
|
-
}): 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;
|
|
24
|
-
}, elapsed?: bigint): {
|
|
25
|
-
interest: bigint;
|
|
26
|
-
feeShares: bigint;
|
|
27
|
-
};
|
|
28
|
-
function getLiquidityToUtilization({ totalSupplyAssets, totalBorrowAssets, }: {
|
|
29
|
-
totalSupplyAssets: BigNumberish;
|
|
30
|
-
totalBorrowAssets: BigNumberish;
|
|
31
|
-
}, utilization: BigNumberish): bigint;
|
|
32
|
-
function getCollateralPower(collateral: BigNumberish, { lltv }: {
|
|
33
|
-
lltv: BigNumberish;
|
|
34
|
-
}): bigint;
|
|
35
|
-
function getCollateralValue(collateral: BigNumberish, { price }: {
|
|
36
|
-
price: BigNumberish;
|
|
37
|
-
}): bigint;
|
|
38
|
-
function getMaxBorrowAssets(collateral: BigNumberish, market: {
|
|
39
|
-
price: BigNumberish;
|
|
40
|
-
}, { lltv }: {
|
|
41
|
-
lltv: BigNumberish;
|
|
42
|
-
}): bigint;
|
|
43
|
-
function getMaxBorrowableAssets({ collateral, borrowShares, }: {
|
|
44
|
-
collateral: BigNumberish;
|
|
45
|
-
borrowShares: BigNumberish;
|
|
46
|
-
}, market: {
|
|
47
|
-
totalBorrowAssets: BigNumberish;
|
|
48
|
-
totalBorrowShares: BigNumberish;
|
|
49
|
-
price: BigNumberish;
|
|
50
|
-
}, marketConfig: {
|
|
51
|
-
lltv: BigNumberish;
|
|
52
|
-
}): bigint;
|
|
53
|
-
function getLiquidationSeizedAssets(repaidShares: BigNumberish, market: {
|
|
54
|
-
totalBorrowAssets: BigNumberish;
|
|
55
|
-
totalBorrowShares: BigNumberish;
|
|
56
|
-
price: BigNumberish;
|
|
57
|
-
}, config: {
|
|
58
|
-
lltv: BigNumberish;
|
|
59
|
-
}): bigint;
|
|
60
|
-
function getLiquidationRepaidShares(seizedAssets: BigNumberish, market: {
|
|
61
|
-
totalBorrowAssets: BigNumberish;
|
|
62
|
-
totalBorrowShares: BigNumberish;
|
|
63
|
-
price: BigNumberish;
|
|
64
|
-
}, config: {
|
|
65
|
-
lltv: BigNumberish;
|
|
66
|
-
}): bigint;
|
|
67
|
-
function getSeizableCollateral(position: {
|
|
68
|
-
collateral: BigNumberish;
|
|
69
|
-
borrowShares: BigNumberish;
|
|
70
|
-
}, market: {
|
|
71
|
-
totalBorrowAssets: BigNumberish;
|
|
72
|
-
totalBorrowShares: BigNumberish;
|
|
73
|
-
price: BigNumberish;
|
|
74
|
-
}, config: {
|
|
75
|
-
lltv: BigNumberish;
|
|
76
|
-
}): bigint;
|
|
77
|
-
function getWithdrawableCollateral({ collateral, borrowShares, }: {
|
|
78
|
-
collateral: BigNumberish;
|
|
79
|
-
borrowShares: BigNumberish;
|
|
80
|
-
}, market: {
|
|
81
|
-
totalBorrowAssets: BigNumberish;
|
|
82
|
-
totalBorrowShares: BigNumberish;
|
|
83
|
-
price: BigNumberish;
|
|
84
|
-
}, { lltv }: {
|
|
85
|
-
lltv: BigNumberish;
|
|
86
|
-
}): bigint;
|
|
87
|
-
function isHealthy({ collateral, borrowShares, }: {
|
|
88
|
-
collateral: BigNumberish;
|
|
89
|
-
borrowShares: BigNumberish;
|
|
90
|
-
}, market: {
|
|
91
|
-
totalBorrowAssets: BigNumberish;
|
|
92
|
-
totalBorrowShares: BigNumberish;
|
|
93
|
-
price: BigNumberish;
|
|
94
|
-
}, marketConfig: {
|
|
95
|
-
lltv: BigNumberish;
|
|
96
|
-
}): boolean;
|
|
97
|
-
/**
|
|
98
|
-
* Returns the price of the collateral quoted in the loan token (e.g. ETH/DAI)
|
|
99
|
-
* that set the user's position to be liquidatable.
|
|
100
|
-
* Returns null if the user is not a borrower
|
|
101
|
-
*/
|
|
102
|
-
function getLiquidationPrice({ collateral, borrowShares, }: {
|
|
103
|
-
collateral: BigNumberish;
|
|
104
|
-
borrowShares: BigNumberish;
|
|
105
|
-
}, market: {
|
|
106
|
-
totalBorrowAssets: BigNumberish;
|
|
107
|
-
totalBorrowShares: BigNumberish;
|
|
108
|
-
}, marketConfig: {
|
|
109
|
-
lltv: BigNumberish;
|
|
110
|
-
}): bigint | null;
|
|
111
|
-
function getPriceVariationToLiquidation(position: {
|
|
112
|
-
collateral: BigNumberish;
|
|
113
|
-
borrowShares: BigNumberish;
|
|
114
|
-
}, market: {
|
|
115
|
-
totalBorrowAssets: BigNumberish;
|
|
116
|
-
totalBorrowShares: BigNumberish;
|
|
117
|
-
price: BigNumberish;
|
|
118
|
-
}, marketConfig: {
|
|
119
|
-
lltv: BigNumberish;
|
|
120
|
-
}): bigint | null;
|
|
121
|
-
function getHealthFactor({ collateral, borrowShares, }: {
|
|
122
|
-
collateral: BigNumberish;
|
|
123
|
-
borrowShares: BigNumberish;
|
|
124
|
-
}, market: {
|
|
125
|
-
totalBorrowAssets: BigNumberish;
|
|
126
|
-
totalBorrowShares: BigNumberish;
|
|
127
|
-
price: BigNumberish;
|
|
128
|
-
}, marketConfig: {
|
|
129
|
-
lltv: BigNumberish;
|
|
130
|
-
}): bigint | null;
|
|
131
|
-
function getLtv({ collateral, borrowShares, }: {
|
|
132
|
-
collateral: BigNumberish;
|
|
133
|
-
borrowShares: BigNumberish;
|
|
134
|
-
}, market: {
|
|
135
|
-
totalBorrowAssets: BigNumberish;
|
|
136
|
-
totalBorrowShares: BigNumberish;
|
|
137
|
-
price: BigNumberish;
|
|
138
|
-
}): bigint | null;
|
|
139
|
-
function getBorrowCapacityUsage(position: {
|
|
140
|
-
collateral: BigNumberish;
|
|
141
|
-
borrowShares: BigNumberish;
|
|
142
|
-
}, market: {
|
|
143
|
-
totalBorrowAssets: BigNumberish;
|
|
144
|
-
totalBorrowShares: BigNumberish;
|
|
145
|
-
price: BigNumberish;
|
|
146
|
-
}, marketConfig: {
|
|
147
|
-
lltv: BigNumberish;
|
|
148
|
-
}): bigint | null;
|
|
149
|
-
function toSupplyAssets(shares: BigNumberish, market: {
|
|
150
|
-
totalSupplyAssets: BigNumberish;
|
|
151
|
-
totalSupplyShares: BigNumberish;
|
|
152
|
-
}, rounding?: RoundingDirection): bigint;
|
|
153
|
-
function toSupplyShares(assets: BigNumberish, market: {
|
|
154
|
-
totalSupplyAssets: BigNumberish;
|
|
155
|
-
totalSupplyShares: BigNumberish;
|
|
156
|
-
}, rounding?: RoundingDirection): bigint;
|
|
157
|
-
function toBorrowAssets(shares: BigNumberish, market: {
|
|
158
|
-
totalBorrowAssets: BigNumberish;
|
|
159
|
-
totalBorrowShares: BigNumberish;
|
|
160
|
-
}, rounding?: RoundingDirection): bigint;
|
|
161
|
-
function toBorrowShares(assets: BigNumberish, market: {
|
|
162
|
-
totalBorrowAssets: BigNumberish;
|
|
163
|
-
totalBorrowShares: BigNumberish;
|
|
164
|
-
}, rounding?: RoundingDirection): bigint;
|
|
165
|
-
}
|
|
@@ -1,182 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.MarketUtils = void 0;
|
|
4
|
-
const ethers_1 = require("ethers");
|
|
5
|
-
const constants_1 = require("../constants");
|
|
6
|
-
const maths_1 = require("../maths");
|
|
7
|
-
var MarketUtils;
|
|
8
|
-
(function (MarketUtils) {
|
|
9
|
-
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);
|
|
18
|
-
}
|
|
19
|
-
MarketUtils.getMarketId = getMarketId;
|
|
20
|
-
function getLiquidationIncentiveFactor({ lltv, }) {
|
|
21
|
-
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))));
|
|
23
|
-
}
|
|
24
|
-
MarketUtils.getLiquidationIncentiveFactor = getLiquidationIncentiveFactor;
|
|
25
|
-
function getUtilization({ totalSupplyAssets, totalBorrowAssets, }) {
|
|
26
|
-
totalSupplyAssets = (0, ethers_1.toBigInt)(totalSupplyAssets);
|
|
27
|
-
if (totalSupplyAssets === 0n)
|
|
28
|
-
return 0n;
|
|
29
|
-
return maths_1.MathLib.wDivDown(totalBorrowAssets, totalSupplyAssets);
|
|
30
|
-
}
|
|
31
|
-
MarketUtils.getUtilization = getUtilization;
|
|
32
|
-
function getSupplyRate(borrowRate, { utilization, fee }) {
|
|
33
|
-
const borrowRateWithoutFees = maths_1.MathLib.wMulUp(borrowRate, utilization);
|
|
34
|
-
return maths_1.MathLib.wMulUp(borrowRateWithoutFees, maths_1.MathLib.WAD - (0, ethers_1.toBigInt)(fee));
|
|
35
|
-
}
|
|
36
|
-
MarketUtils.getSupplyRate = getSupplyRate;
|
|
37
|
-
function getApy(rate) {
|
|
38
|
-
return maths_1.MathLib.wTaylorCompounded(rate, constants_1.SECONDS_PER_YEAR);
|
|
39
|
-
}
|
|
40
|
-
MarketUtils.getApy = getApy;
|
|
41
|
-
function getAccruedInterest(borrowRate, { totalSupplyAssets, totalBorrowAssets, totalSupplyShares, fee, }, elapsed = 0n) {
|
|
42
|
-
const interest = maths_1.MathLib.wMulDown(totalBorrowAssets, maths_1.MathLib.wTaylorCompounded(borrowRate, elapsed));
|
|
43
|
-
const feeAmount = maths_1.MathLib.wMulDown(interest, fee);
|
|
44
|
-
const feeShares = toSupplyShares(feeAmount, {
|
|
45
|
-
totalSupplyAssets: (0, ethers_1.toBigInt)(totalSupplyAssets) - feeAmount,
|
|
46
|
-
totalSupplyShares,
|
|
47
|
-
}, "Down");
|
|
48
|
-
return { interest, feeShares };
|
|
49
|
-
}
|
|
50
|
-
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);
|
|
55
|
-
if (utilization === 0n) {
|
|
56
|
-
if (totalBorrowAssets === 0n)
|
|
57
|
-
return totalSupplyAssets;
|
|
58
|
-
return 0n;
|
|
59
|
-
}
|
|
60
|
-
return maths_1.MathUtils.zeroFloorSub(totalSupplyAssets, maths_1.MathLib.wDivUp(totalBorrowAssets, utilization));
|
|
61
|
-
}
|
|
62
|
-
MarketUtils.getLiquidityToUtilization = getLiquidityToUtilization;
|
|
63
|
-
function getCollateralPower(collateral, { lltv }) {
|
|
64
|
-
return maths_1.MathLib.wMulDown(collateral, lltv);
|
|
65
|
-
}
|
|
66
|
-
MarketUtils.getCollateralPower = getCollateralPower;
|
|
67
|
-
function getCollateralValue(collateral, { price }) {
|
|
68
|
-
return maths_1.MathLib.mulDivDown(collateral, price, constants_1.ORACLE_PRICE_SCALE);
|
|
69
|
-
}
|
|
70
|
-
MarketUtils.getCollateralValue = getCollateralValue;
|
|
71
|
-
function getMaxBorrowAssets(collateral, market, { lltv }) {
|
|
72
|
-
return maths_1.MathLib.wMulDown(getCollateralValue(collateral, market), lltv);
|
|
73
|
-
}
|
|
74
|
-
MarketUtils.getMaxBorrowAssets = getMaxBorrowAssets;
|
|
75
|
-
function getMaxBorrowableAssets({ collateral, borrowShares, }, market, marketConfig) {
|
|
76
|
-
return maths_1.MathUtils.zeroFloorSub(getMaxBorrowAssets(collateral, market, marketConfig), toBorrowAssets(borrowShares, market));
|
|
77
|
-
}
|
|
78
|
-
MarketUtils.getMaxBorrowableAssets = getMaxBorrowableAssets;
|
|
79
|
-
function getLiquidationSeizedAssets(repaidShares, market, config) {
|
|
80
|
-
market.price = (0, ethers_1.toBigInt)(market.price);
|
|
81
|
-
if (market.price === 0n)
|
|
82
|
-
return 0n;
|
|
83
|
-
return maths_1.MathLib.mulDivDown(maths_1.MathLib.wMulDown(toBorrowAssets(repaidShares, market, "Down"), getLiquidationIncentiveFactor(config)), constants_1.ORACLE_PRICE_SCALE, market.price);
|
|
84
|
-
}
|
|
85
|
-
MarketUtils.getLiquidationSeizedAssets = getLiquidationSeizedAssets;
|
|
86
|
-
function getLiquidationRepaidShares(seizedAssets, market, config) {
|
|
87
|
-
return toBorrowShares(maths_1.MathLib.wDivUp(maths_1.MathLib.mulDivUp(seizedAssets, market.price, constants_1.ORACLE_PRICE_SCALE), getLiquidationIncentiveFactor(config)), market, "Up");
|
|
88
|
-
}
|
|
89
|
-
MarketUtils.getLiquidationRepaidShares = getLiquidationRepaidShares;
|
|
90
|
-
function getSeizableCollateral(position, market, config) {
|
|
91
|
-
market.price = (0, ethers_1.toBigInt)(market.price);
|
|
92
|
-
if (market.price === 0n || isHealthy(position, market, config))
|
|
93
|
-
return 0n;
|
|
94
|
-
return maths_1.MathLib.min(position.collateral, getLiquidationSeizedAssets(position.borrowShares, market, config));
|
|
95
|
-
}
|
|
96
|
-
MarketUtils.getSeizableCollateral = getSeizableCollateral;
|
|
97
|
-
function getWithdrawableCollateral({ collateral, borrowShares, }, market, { lltv }) {
|
|
98
|
-
market.price = (0, ethers_1.toBigInt)(market.price);
|
|
99
|
-
if (market.price === 0n)
|
|
100
|
-
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));
|
|
102
|
-
}
|
|
103
|
-
MarketUtils.getWithdrawableCollateral = getWithdrawableCollateral;
|
|
104
|
-
function isHealthy({ collateral, borrowShares, }, market, marketConfig) {
|
|
105
|
-
return (getMaxBorrowAssets(collateral, market, marketConfig) >=
|
|
106
|
-
toBorrowAssets(borrowShares, market));
|
|
107
|
-
}
|
|
108
|
-
MarketUtils.isHealthy = isHealthy;
|
|
109
|
-
/**
|
|
110
|
-
* Returns the price of the collateral quoted in the loan token (e.g. ETH/DAI)
|
|
111
|
-
* that set the user's position to be liquidatable.
|
|
112
|
-
* Returns null if the user is not a borrower
|
|
113
|
-
*/
|
|
114
|
-
function getLiquidationPrice({ collateral, borrowShares, }, market, marketConfig) {
|
|
115
|
-
borrowShares = (0, ethers_1.toBigInt)(borrowShares);
|
|
116
|
-
market.totalBorrowShares = (0, ethers_1.toBigInt)(market.totalBorrowShares);
|
|
117
|
-
if (borrowShares === 0n || market.totalBorrowShares === 0n)
|
|
118
|
-
return null;
|
|
119
|
-
const collateralPower = getCollateralPower(collateral, marketConfig);
|
|
120
|
-
if (collateralPower === 0n)
|
|
121
|
-
return ethers_1.MaxUint256;
|
|
122
|
-
const borrowAssets = toBorrowAssets(borrowShares, market);
|
|
123
|
-
return maths_1.MathLib.mulDivUp(borrowAssets, constants_1.ORACLE_PRICE_SCALE, collateralPower);
|
|
124
|
-
}
|
|
125
|
-
MarketUtils.getLiquidationPrice = getLiquidationPrice;
|
|
126
|
-
function getPriceVariationToLiquidation(position, market, marketConfig) {
|
|
127
|
-
market.price = (0, ethers_1.toBigInt)(market.price);
|
|
128
|
-
const liquidationPrice = getLiquidationPrice(position, market, marketConfig);
|
|
129
|
-
if (market.price === 0n || liquidationPrice == null)
|
|
130
|
-
return null;
|
|
131
|
-
return maths_1.MathLib.WAD - maths_1.MathLib.wDivUp(liquidationPrice, market.price);
|
|
132
|
-
}
|
|
133
|
-
MarketUtils.getPriceVariationToLiquidation = getPriceVariationToLiquidation;
|
|
134
|
-
function getHealthFactor({ collateral, borrowShares, }, market, marketConfig) {
|
|
135
|
-
borrowShares = (0, ethers_1.toBigInt)(borrowShares);
|
|
136
|
-
market.totalBorrowShares = (0, ethers_1.toBigInt)(market.totalBorrowShares);
|
|
137
|
-
if (borrowShares === 0n || market.totalBorrowShares === 0n)
|
|
138
|
-
return null;
|
|
139
|
-
const borrowAssets = toBorrowAssets(borrowShares, market);
|
|
140
|
-
if (borrowAssets === 0n)
|
|
141
|
-
return ethers_1.MaxUint256;
|
|
142
|
-
const maxBorrowAssets = getMaxBorrowAssets(collateral, market, marketConfig);
|
|
143
|
-
return maths_1.MathLib.wDivDown(maxBorrowAssets, borrowAssets);
|
|
144
|
-
}
|
|
145
|
-
MarketUtils.getHealthFactor = getHealthFactor;
|
|
146
|
-
function getLtv({ collateral, borrowShares, }, market) {
|
|
147
|
-
borrowShares = (0, ethers_1.toBigInt)(borrowShares);
|
|
148
|
-
market.totalBorrowShares = (0, ethers_1.toBigInt)(market.totalBorrowShares);
|
|
149
|
-
if (borrowShares === 0n || market.totalBorrowShares === 0n)
|
|
150
|
-
return null;
|
|
151
|
-
const collateralValue = getCollateralValue(collateral, market);
|
|
152
|
-
if (collateralValue === 0n)
|
|
153
|
-
return ethers_1.MaxUint256;
|
|
154
|
-
return maths_1.MathLib.wDivUp(toBorrowAssets(borrowShares, market), collateralValue);
|
|
155
|
-
}
|
|
156
|
-
MarketUtils.getLtv = getLtv;
|
|
157
|
-
function getBorrowCapacityUsage(position, market, marketConfig) {
|
|
158
|
-
const hf = getHealthFactor(position, market, marketConfig);
|
|
159
|
-
if (hf === null)
|
|
160
|
-
return null;
|
|
161
|
-
if (hf === 0n)
|
|
162
|
-
return ethers_1.MaxUint256;
|
|
163
|
-
return maths_1.MathLib.wDivUp(maths_1.MathLib.WAD, hf);
|
|
164
|
-
}
|
|
165
|
-
MarketUtils.getBorrowCapacityUsage = getBorrowCapacityUsage;
|
|
166
|
-
function toSupplyAssets(shares, market, rounding = "Down") {
|
|
167
|
-
return maths_1.SharesMath.toAssets(shares, market.totalSupplyAssets, market.totalSupplyShares, rounding);
|
|
168
|
-
}
|
|
169
|
-
MarketUtils.toSupplyAssets = toSupplyAssets;
|
|
170
|
-
function toSupplyShares(assets, market, rounding = "Up") {
|
|
171
|
-
return maths_1.SharesMath.toShares(assets, market.totalSupplyAssets, market.totalSupplyShares, rounding);
|
|
172
|
-
}
|
|
173
|
-
MarketUtils.toSupplyShares = toSupplyShares;
|
|
174
|
-
function toBorrowAssets(shares, market, rounding = "Up") {
|
|
175
|
-
return maths_1.SharesMath.toAssets(shares, market.totalBorrowAssets, market.totalBorrowShares, rounding);
|
|
176
|
-
}
|
|
177
|
-
MarketUtils.toBorrowAssets = toBorrowAssets;
|
|
178
|
-
function toBorrowShares(assets, market, rounding = "Down") {
|
|
179
|
-
return maths_1.SharesMath.toShares(assets, market.totalBorrowAssets, market.totalBorrowShares, rounding);
|
|
180
|
-
}
|
|
181
|
-
MarketUtils.toBorrowShares = toBorrowShares;
|
|
182
|
-
})(MarketUtils || (exports.MarketUtils = MarketUtils = {}));
|