@gearbox-protocol/sdk 13.0.0-beta.4 → 13.0.0-beta.6
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/dist/cjs/common-utils/index.js +22 -0
- package/dist/cjs/common-utils/package.json +1 -0
- package/dist/cjs/{sdk → common-utils}/utils/assetsMath.js +78 -12
- package/dist/cjs/common-utils/utils/bigintMath.js +65 -0
- package/dist/cjs/common-utils/utils/creditAccount/calcHealthFactor.js +76 -0
- package/dist/cjs/common-utils/utils/creditAccount/calcOverallAPY.js +81 -0
- package/dist/cjs/{sdk/utils/priceMath.js → common-utils/utils/creditAccount/calcQuotaBorrowRate.js} +19 -12
- package/dist/cjs/{sdk/utils/bigintMath.js → common-utils/utils/creditAccount/calcRelativeBaseBorrowRate.js} +11 -10
- package/dist/cjs/common-utils/utils/creditAccount/debt.js +64 -0
- package/dist/cjs/common-utils/utils/creditAccount/getTimeToLiquidation.js +38 -0
- package/dist/cjs/common-utils/utils/creditAccount/index.js +40 -0
- package/dist/cjs/common-utils/utils/creditAccount/liquidationPrice.js +47 -0
- package/dist/cjs/common-utils/utils/creditAccount/quotaUtils.js +149 -0
- package/dist/cjs/common-utils/utils/creditAccount/sort.js +95 -0
- package/dist/cjs/common-utils/utils/creditAccount/types.js +16 -0
- package/dist/cjs/{sdk → common-utils}/utils/endpoints.js +11 -17
- package/dist/cjs/common-utils/utils/index.js +30 -0
- package/dist/cjs/common-utils/utils/priceMath.js +66 -0
- package/dist/cjs/sdk/utils/index.js +0 -10
- package/dist/esm/common-utils/index.js +1 -0
- package/dist/esm/common-utils/package.json +1 -0
- package/dist/esm/{sdk → common-utils}/utils/assetsMath.js +78 -12
- package/dist/esm/common-utils/utils/bigintMath.js +41 -0
- package/dist/esm/common-utils/utils/creditAccount/calcHealthFactor.js +55 -0
- package/dist/esm/common-utils/utils/creditAccount/calcOverallAPY.js +60 -0
- package/dist/esm/common-utils/utils/creditAccount/calcQuotaBorrowRate.js +18 -0
- package/dist/esm/common-utils/utils/creditAccount/calcRelativeBaseBorrowRate.js +10 -0
- package/dist/esm/common-utils/utils/creditAccount/debt.js +43 -0
- package/dist/esm/common-utils/utils/creditAccount/getTimeToLiquidation.js +18 -0
- package/dist/esm/common-utils/utils/creditAccount/index.js +10 -0
- package/dist/esm/common-utils/utils/creditAccount/liquidationPrice.js +23 -0
- package/dist/esm/common-utils/utils/creditAccount/quotaUtils.js +125 -0
- package/dist/esm/common-utils/utils/creditAccount/sort.js +67 -0
- package/dist/esm/common-utils/utils/creditAccount/types.js +0 -0
- package/dist/esm/{sdk → common-utils}/utils/endpoints.js +9 -14
- package/dist/esm/common-utils/utils/index.js +5 -0
- package/dist/esm/common-utils/utils/priceMath.js +42 -0
- package/dist/esm/sdk/utils/index.js +0 -5
- package/dist/types/common-utils/index.d.ts +1 -0
- package/dist/types/common-utils/utils/assetsMath.d.ts +114 -0
- package/dist/types/common-utils/utils/bigintMath.d.ts +43 -0
- package/dist/types/common-utils/utils/creditAccount/calcHealthFactor.d.ts +25 -0
- package/dist/types/common-utils/utils/creditAccount/calcOverallAPY.d.ts +37 -0
- package/dist/types/common-utils/utils/creditAccount/calcQuotaBorrowRate.d.ts +18 -0
- package/dist/types/common-utils/utils/creditAccount/calcRelativeBaseBorrowRate.d.ts +15 -0
- package/dist/types/common-utils/utils/creditAccount/debt.d.ts +35 -0
- package/dist/types/common-utils/utils/creditAccount/getTimeToLiquidation.d.ts +16 -0
- package/dist/types/common-utils/utils/creditAccount/index.d.ts +10 -0
- package/dist/types/common-utils/utils/creditAccount/liquidationPrice.d.ts +25 -0
- package/dist/types/common-utils/utils/creditAccount/quotaUtils.d.ts +81 -0
- package/dist/types/common-utils/utils/creditAccount/sort.d.ts +55 -0
- package/dist/types/common-utils/utils/creditAccount/types.d.ts +18 -0
- package/dist/types/{sdk → common-utils}/utils/endpoints.d.ts +13 -5
- package/dist/types/common-utils/utils/index.d.ts +5 -0
- package/dist/types/common-utils/utils/priceMath.d.ts +47 -0
- package/dist/types/sdk/utils/index.d.ts +0 -5
- package/package.json +6 -1
- package/dist/cjs/sdk/utils/creditAccount.js +0 -411
- package/dist/esm/sdk/utils/bigintMath.js +0 -9
- package/dist/esm/sdk/utils/creditAccount.js +0 -396
- package/dist/esm/sdk/utils/priceMath.js +0 -11
- package/dist/types/sdk/utils/assetsMath.d.ts +0 -42
- package/dist/types/sdk/utils/bigintMath.d.ts +0 -6
- package/dist/types/sdk/utils/creditAccount.d.ts +0 -128
- package/dist/types/sdk/utils/priceMath.d.ts +0 -9
|
@@ -1,411 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
var creditAccount_exports = {};
|
|
20
|
-
__export(creditAccount_exports, {
|
|
21
|
-
CreditAccountDataUtils: () => CreditAccountDataUtils
|
|
22
|
-
});
|
|
23
|
-
module.exports = __toCommonJS(creditAccount_exports);
|
|
24
|
-
var import_constants = require("../constants/index.js");
|
|
25
|
-
var import_bigintMath = require("./bigintMath.js");
|
|
26
|
-
var import_priceMath = require("./priceMath.js");
|
|
27
|
-
const MAX_UINT16 = 65535;
|
|
28
|
-
class CreditAccountDataUtils {
|
|
29
|
-
constructor() {
|
|
30
|
-
}
|
|
31
|
-
static sortBalances(balances, prices, tokens) {
|
|
32
|
-
return Object.entries(balances).sort(
|
|
33
|
-
([addr1, amount1], [addr2, amount2]) => {
|
|
34
|
-
return CreditAccountDataUtils.assetComparator(
|
|
35
|
-
{
|
|
36
|
-
token: addr1,
|
|
37
|
-
balance: amount1
|
|
38
|
-
},
|
|
39
|
-
{
|
|
40
|
-
token: addr2,
|
|
41
|
-
balance: amount2
|
|
42
|
-
},
|
|
43
|
-
prices,
|
|
44
|
-
prices,
|
|
45
|
-
tokens,
|
|
46
|
-
tokens
|
|
47
|
-
);
|
|
48
|
-
}
|
|
49
|
-
);
|
|
50
|
-
}
|
|
51
|
-
static sortAssets(balances, prices, tokens) {
|
|
52
|
-
return [...balances].sort(
|
|
53
|
-
(t1, t2) => CreditAccountDataUtils.assetComparator(
|
|
54
|
-
t1,
|
|
55
|
-
t2,
|
|
56
|
-
prices,
|
|
57
|
-
prices,
|
|
58
|
-
tokens,
|
|
59
|
-
tokens
|
|
60
|
-
)
|
|
61
|
-
);
|
|
62
|
-
}
|
|
63
|
-
static assetComparator(t1, t2, prices1, prices2, tokens1, tokens2) {
|
|
64
|
-
const addr1Lc = t1.token.toLowerCase();
|
|
65
|
-
const addr2Lc = t2.token.toLowerCase();
|
|
66
|
-
const token1 = tokens1?.[addr1Lc];
|
|
67
|
-
const token2 = tokens2?.[addr2Lc];
|
|
68
|
-
const price1 = prices1?.[addr1Lc] || import_constants.PRICE_DECIMALS;
|
|
69
|
-
const price2 = prices2?.[addr2Lc] || import_constants.PRICE_DECIMALS;
|
|
70
|
-
const totalPrice1 = import_priceMath.PriceUtils.calcTotalPrice(
|
|
71
|
-
price1,
|
|
72
|
-
t1.balance,
|
|
73
|
-
token1?.decimals
|
|
74
|
-
);
|
|
75
|
-
const totalPrice2 = import_priceMath.PriceUtils.calcTotalPrice(
|
|
76
|
-
price2,
|
|
77
|
-
t2.balance,
|
|
78
|
-
token2?.decimals
|
|
79
|
-
);
|
|
80
|
-
if (totalPrice1 === totalPrice2) {
|
|
81
|
-
return t1.balance === t2.balance ? CreditAccountDataUtils.tokensAbcComparator(token1, token2) : CreditAccountDataUtils.amountAbcComparator(t1.balance, t2.balance);
|
|
82
|
-
}
|
|
83
|
-
return CreditAccountDataUtils.amountAbcComparator(totalPrice1, totalPrice2);
|
|
84
|
-
}
|
|
85
|
-
static tokensAbcComparator(t1, t2) {
|
|
86
|
-
const { symbol: symbol1 = "" } = t1 || {};
|
|
87
|
-
const { symbol: symbol2 = "" } = t2 || {};
|
|
88
|
-
const symbol1LC = symbol1.toLowerCase();
|
|
89
|
-
const symbol2LC = symbol2.toLowerCase();
|
|
90
|
-
if (symbol1LC === symbol2LC) return 0;
|
|
91
|
-
return symbol1LC > symbol2LC ? 1 : -1;
|
|
92
|
-
}
|
|
93
|
-
static amountAbcComparator(t1, t2) {
|
|
94
|
-
return t1 > t2 ? -1 : 1;
|
|
95
|
-
}
|
|
96
|
-
static calcMaxDebtIncrease(healthFactor, debt, underlyingLT, minHf = Number(import_constants.PERCENTAGE_FACTOR)) {
|
|
97
|
-
const result = debt * BigInt(healthFactor - minHf) / BigInt(minHf - underlyingLT);
|
|
98
|
-
return import_bigintMath.BigIntMath.max(0n, result);
|
|
99
|
-
}
|
|
100
|
-
static calcMaxLendingDebt({
|
|
101
|
-
assets,
|
|
102
|
-
liquidationThresholds,
|
|
103
|
-
underlyingToken,
|
|
104
|
-
prices,
|
|
105
|
-
tokensList,
|
|
106
|
-
targetHF = import_constants.PERCENTAGE_FACTOR
|
|
107
|
-
}) {
|
|
108
|
-
const assetsLTMoney = assets.reduce(
|
|
109
|
-
(acc, { token: tokenAddress, balance: amount }) => {
|
|
110
|
-
const tokenDecimals = tokensList[tokenAddress]?.decimals || 18;
|
|
111
|
-
const lt = liquidationThresholds[tokenAddress] || 0n;
|
|
112
|
-
const price = prices[tokenAddress] || 0n;
|
|
113
|
-
const tokenMoney = import_priceMath.PriceUtils.calcTotalPrice(
|
|
114
|
-
price,
|
|
115
|
-
amount,
|
|
116
|
-
tokenDecimals
|
|
117
|
-
);
|
|
118
|
-
const tokenLtMoney = tokenMoney * lt;
|
|
119
|
-
return acc + tokenLtMoney;
|
|
120
|
-
},
|
|
121
|
-
0n
|
|
122
|
-
);
|
|
123
|
-
const underlyingPrice = prices[underlyingToken] || 0n;
|
|
124
|
-
const underlyingDecimals = tokensList[underlyingToken]?.decimals || 18;
|
|
125
|
-
const max = underlyingPrice > 0 ? assetsLTMoney * 10n ** BigInt(underlyingDecimals) / underlyingPrice / targetHF / 10n ** BigInt(import_constants.WAD_DECIMALS_POW - import_constants.PRICE_DECIMALS_POW) : 0n;
|
|
126
|
-
return max;
|
|
127
|
-
}
|
|
128
|
-
// [
|
|
129
|
-
// Sum(amount_i * price_i * apy_i - quota_i * quotaPrice * quotaRate_i * (1 + feeInterest)) -
|
|
130
|
-
// debt * debtPrice * baseRateWithFee
|
|
131
|
-
// ] / (totalValue - debt) * debtPrice
|
|
132
|
-
static calcOverallAPY({
|
|
133
|
-
caAssets,
|
|
134
|
-
lpAPY,
|
|
135
|
-
prices,
|
|
136
|
-
quotas,
|
|
137
|
-
quotaRates,
|
|
138
|
-
feeInterest,
|
|
139
|
-
totalValue,
|
|
140
|
-
debt,
|
|
141
|
-
baseRateWithFee,
|
|
142
|
-
underlyingToken,
|
|
143
|
-
tokensList
|
|
144
|
-
}) {
|
|
145
|
-
if (!lpAPY || !totalValue || totalValue <= 0n || !debt || totalValue <= debt)
|
|
146
|
-
return void 0;
|
|
147
|
-
const underlyingTokenDecimals = tokensList[underlyingToken]?.decimals || 18;
|
|
148
|
-
const underlyingPrice = prices[underlyingToken];
|
|
149
|
-
const assetAPYMoney = caAssets.reduce(
|
|
150
|
-
(acc, { token: tokenAddress, balance: amount }) => {
|
|
151
|
-
const apy = lpAPY[tokenAddress] || 0;
|
|
152
|
-
const tokenDecimals = tokensList[tokenAddress]?.decimals || 18;
|
|
153
|
-
const price = prices[tokenAddress] || 0n;
|
|
154
|
-
const money = import_priceMath.PriceUtils.calcTotalPrice(price, amount, tokenDecimals);
|
|
155
|
-
const apyMoney = money * BigInt(apy);
|
|
156
|
-
const { rate: quotaAPY = 0n, isActive = false } = quotaRates?.[tokenAddress] || {};
|
|
157
|
-
const { balance: quotaBalance = 0n } = quotas[tokenAddress] || {};
|
|
158
|
-
const quotaAmount = isActive ? quotaBalance : 0n;
|
|
159
|
-
const quotaMoney = import_priceMath.PriceUtils.calcTotalPrice(
|
|
160
|
-
underlyingPrice || 0n,
|
|
161
|
-
quotaAmount,
|
|
162
|
-
underlyingTokenDecimals
|
|
163
|
-
);
|
|
164
|
-
const quotaRate = quotaAPY * (BigInt(feeInterest) + import_constants.PERCENTAGE_FACTOR) / import_constants.PERCENTAGE_FACTOR;
|
|
165
|
-
const quotaAPYMoney = quotaMoney * quotaRate;
|
|
166
|
-
return acc + apyMoney - quotaAPYMoney;
|
|
167
|
-
},
|
|
168
|
-
0n
|
|
169
|
-
);
|
|
170
|
-
const debtMoney = import_priceMath.PriceUtils.calcTotalPrice(
|
|
171
|
-
underlyingPrice || 0n,
|
|
172
|
-
debt,
|
|
173
|
-
underlyingTokenDecimals
|
|
174
|
-
);
|
|
175
|
-
const debtAPYMoney = debtMoney * BigInt(baseRateWithFee);
|
|
176
|
-
const yourAssetsMoney = import_priceMath.PriceUtils.calcTotalPrice(
|
|
177
|
-
underlyingPrice || import_constants.PRICE_DECIMALS,
|
|
178
|
-
totalValue - debt,
|
|
179
|
-
underlyingTokenDecimals
|
|
180
|
-
);
|
|
181
|
-
const apyInPercent = (assetAPYMoney - debtAPYMoney) / yourAssetsMoney;
|
|
182
|
-
return apyInPercent;
|
|
183
|
-
}
|
|
184
|
-
static calcHealthFactor({
|
|
185
|
-
assets,
|
|
186
|
-
quotas,
|
|
187
|
-
quotasInfo,
|
|
188
|
-
liquidationThresholds,
|
|
189
|
-
underlyingToken,
|
|
190
|
-
debt,
|
|
191
|
-
prices,
|
|
192
|
-
tokensList
|
|
193
|
-
}) {
|
|
194
|
-
if (debt === 0n) return MAX_UINT16;
|
|
195
|
-
const underlyingDecimals = tokensList[underlyingToken]?.decimals || 18;
|
|
196
|
-
const underlyingPrice = prices[underlyingToken] || 0n;
|
|
197
|
-
const assetMoney = assets.reduce(
|
|
198
|
-
(acc, { token: tokenAddress, balance: amount }) => {
|
|
199
|
-
const tokenDecimals = tokensList[tokenAddress]?.decimals || 18;
|
|
200
|
-
const lt = liquidationThresholds[tokenAddress] || 0n;
|
|
201
|
-
const price = prices[tokenAddress] || 0n;
|
|
202
|
-
const tokenMoney = import_priceMath.PriceUtils.calcTotalPrice(
|
|
203
|
-
price,
|
|
204
|
-
amount,
|
|
205
|
-
tokenDecimals
|
|
206
|
-
);
|
|
207
|
-
const tokenLtMoney = tokenMoney * lt / import_constants.PERCENTAGE_FACTOR;
|
|
208
|
-
const { isActive = false } = quotasInfo?.[tokenAddress] || {};
|
|
209
|
-
const quota = quotas[tokenAddress];
|
|
210
|
-
const quotaBalance = isActive ? quota?.balance || 0n : 0n;
|
|
211
|
-
const quotaMoney = import_priceMath.PriceUtils.calcTotalPrice(
|
|
212
|
-
underlyingPrice,
|
|
213
|
-
quotaBalance,
|
|
214
|
-
underlyingDecimals
|
|
215
|
-
);
|
|
216
|
-
const money = quota ? import_bigintMath.BigIntMath.min(quotaMoney, tokenLtMoney) : tokenLtMoney;
|
|
217
|
-
return acc + money;
|
|
218
|
-
},
|
|
219
|
-
0n
|
|
220
|
-
);
|
|
221
|
-
const borrowedMoney = import_priceMath.PriceUtils.calcTotalPrice(
|
|
222
|
-
underlyingPrice || import_constants.PRICE_DECIMALS,
|
|
223
|
-
debt,
|
|
224
|
-
underlyingDecimals
|
|
225
|
-
);
|
|
226
|
-
const hfInPercent = borrowedMoney > 0n ? assetMoney * import_constants.PERCENTAGE_FACTOR / borrowedMoney : 0n;
|
|
227
|
-
return Number(hfInPercent);
|
|
228
|
-
}
|
|
229
|
-
static roundUpQuota(quotaChange) {
|
|
230
|
-
return quotaChange !== import_constants.MIN_INT96 ? quotaChange / import_constants.PERCENTAGE_FACTOR * import_constants.PERCENTAGE_FACTOR : quotaChange;
|
|
231
|
-
}
|
|
232
|
-
static calcRecommendedQuota({
|
|
233
|
-
amount,
|
|
234
|
-
debt,
|
|
235
|
-
lt,
|
|
236
|
-
quotaReserve
|
|
237
|
-
}) {
|
|
238
|
-
const recommendedBaseQuota = import_bigintMath.BigIntMath.min(
|
|
239
|
-
debt,
|
|
240
|
-
amount * lt / import_constants.PERCENTAGE_FACTOR
|
|
241
|
-
);
|
|
242
|
-
const recommendedQuota = recommendedBaseQuota * (import_constants.PERCENTAGE_FACTOR + quotaReserve) / import_constants.PERCENTAGE_FACTOR;
|
|
243
|
-
return CreditAccountDataUtils.roundUpQuota(recommendedQuota);
|
|
244
|
-
}
|
|
245
|
-
static calcDefaultQuota({ amount, lt, quotaReserve }) {
|
|
246
|
-
const recommendedBaseQuota = amount * lt / import_constants.PERCENTAGE_FACTOR;
|
|
247
|
-
const recommendedQuota = recommendedBaseQuota * (import_constants.PERCENTAGE_FACTOR + quotaReserve) / import_constants.PERCENTAGE_FACTOR;
|
|
248
|
-
return CreditAccountDataUtils.roundUpQuota(recommendedQuota);
|
|
249
|
-
}
|
|
250
|
-
static calcQuotaUpdate(props) {
|
|
251
|
-
const { quotas, initialQuotas, maxDebt, allowedToSpend, allowedToObtain } = props;
|
|
252
|
-
const quotaDecrease = Object.keys(allowedToSpend).reduce((acc, token) => {
|
|
253
|
-
const ch = CreditAccountDataUtils.getSingleQuotaChange(
|
|
254
|
-
token,
|
|
255
|
-
0n,
|
|
256
|
-
props
|
|
257
|
-
);
|
|
258
|
-
if (ch && ch.balance < 0) acc[ch.token] = ch;
|
|
259
|
-
return acc;
|
|
260
|
-
}, {});
|
|
261
|
-
const quotaCap = CreditAccountDataUtils.roundUpQuota(maxDebt * 2n);
|
|
262
|
-
const quotaBought = Object.values(initialQuotas).reduce(
|
|
263
|
-
(sum, q) => sum + CreditAccountDataUtils.roundUpQuota(q?.quota || 0n),
|
|
264
|
-
0n
|
|
265
|
-
);
|
|
266
|
-
const quotaReduced = Object.values(quotaDecrease).reduce((sum, q) => {
|
|
267
|
-
const quotaBalance = q.balance || 0n;
|
|
268
|
-
const safeBalance = quotaBalance === import_constants.MIN_INT96 ? import_bigintMath.BigIntMath.neg(
|
|
269
|
-
CreditAccountDataUtils.roundUpQuota(
|
|
270
|
-
initialQuotas[q.token]?.quota || 0n
|
|
271
|
-
)
|
|
272
|
-
) : quotaBalance;
|
|
273
|
-
return sum + safeBalance;
|
|
274
|
-
}, 0n);
|
|
275
|
-
const maxQuotaIncrease = CreditAccountDataUtils.roundUpQuota(
|
|
276
|
-
import_bigintMath.BigIntMath.max(quotaCap - (quotaBought + quotaReduced), 0n)
|
|
277
|
-
);
|
|
278
|
-
const quotaIncrease = Object.keys(allowedToObtain).reduce((acc, token) => {
|
|
279
|
-
const ch = CreditAccountDataUtils.getSingleQuotaChange(
|
|
280
|
-
token,
|
|
281
|
-
maxQuotaIncrease,
|
|
282
|
-
props
|
|
283
|
-
);
|
|
284
|
-
if (ch && ch.balance > 0) acc[ch.token] = ch;
|
|
285
|
-
return acc;
|
|
286
|
-
}, {});
|
|
287
|
-
const quotaChange = {
|
|
288
|
-
...quotaDecrease,
|
|
289
|
-
...quotaIncrease
|
|
290
|
-
};
|
|
291
|
-
const desiredQuota = Object.values(quotas).reduce(
|
|
292
|
-
(acc, cmQuota) => {
|
|
293
|
-
const { token, isActive } = cmQuota;
|
|
294
|
-
const { quota: initialQuota = 0n } = initialQuotas[token] || {};
|
|
295
|
-
if (!isActive) {
|
|
296
|
-
acc[token] = {
|
|
297
|
-
balance: initialQuota,
|
|
298
|
-
token
|
|
299
|
-
};
|
|
300
|
-
} else {
|
|
301
|
-
const change = quotaChange[token]?.balance || 0n;
|
|
302
|
-
const quotaAfter = change === import_constants.MIN_INT96 ? 0n : initialQuota + change;
|
|
303
|
-
acc[token] = {
|
|
304
|
-
balance: quotaAfter,
|
|
305
|
-
token
|
|
306
|
-
};
|
|
307
|
-
}
|
|
308
|
-
return acc;
|
|
309
|
-
},
|
|
310
|
-
{}
|
|
311
|
-
);
|
|
312
|
-
return {
|
|
313
|
-
desiredQuota,
|
|
314
|
-
quotaDecrease: Object.values(quotaDecrease),
|
|
315
|
-
quotaIncrease: Object.values(quotaIncrease)
|
|
316
|
-
};
|
|
317
|
-
}
|
|
318
|
-
static getSingleQuotaChange(token, unsafeMaxQuotaIncrease, props) {
|
|
319
|
-
const { isActive = false } = props.quotas[token] || {};
|
|
320
|
-
const { quota: unsafeInitialQuota = 0n } = props.initialQuotas[token] || {};
|
|
321
|
-
if (!isActive) {
|
|
322
|
-
return void 0;
|
|
323
|
-
}
|
|
324
|
-
const assetAfter = props.assetsAfterUpdate[token];
|
|
325
|
-
const { amountInTarget = 0n } = assetAfter || {};
|
|
326
|
-
const lt = props.liquidationThresholds[token] || 0n;
|
|
327
|
-
const maxQuotaIncrease = CreditAccountDataUtils.roundUpQuota(
|
|
328
|
-
unsafeMaxQuotaIncrease
|
|
329
|
-
);
|
|
330
|
-
const initialQuota = CreditAccountDataUtils.roundUpQuota(unsafeInitialQuota);
|
|
331
|
-
const defaultQuota = props.calcModification?.type === "recommendedQuota" && props.calcModification.debt > 0 ? CreditAccountDataUtils.calcRecommendedQuota({
|
|
332
|
-
lt,
|
|
333
|
-
quotaReserve: props.quotaReserve,
|
|
334
|
-
amount: amountInTarget,
|
|
335
|
-
debt: props.calcModification.debt
|
|
336
|
-
}) : CreditAccountDataUtils.calcDefaultQuota({
|
|
337
|
-
lt,
|
|
338
|
-
quotaReserve: props.quotaReserve,
|
|
339
|
-
amount: amountInTarget
|
|
340
|
-
});
|
|
341
|
-
const unsafeQuotaChange = CreditAccountDataUtils.roundUpQuota(
|
|
342
|
-
defaultQuota - initialQuota
|
|
343
|
-
);
|
|
344
|
-
const quotaChange = unsafeQuotaChange > 0 ? import_bigintMath.BigIntMath.min(maxQuotaIncrease, unsafeQuotaChange) : unsafeQuotaChange < 0 && import_bigintMath.BigIntMath.abs(unsafeQuotaChange) >= initialQuota ? import_constants.MIN_INT96 : unsafeQuotaChange;
|
|
345
|
-
const correctIncrease = assetAfter && props.allowedToObtain[token] && quotaChange > 0;
|
|
346
|
-
const correctDecrease = assetAfter && props.allowedToSpend[token] && quotaChange < 0;
|
|
347
|
-
if (correctIncrease || correctDecrease) {
|
|
348
|
-
return {
|
|
349
|
-
balance: quotaChange,
|
|
350
|
-
token
|
|
351
|
-
};
|
|
352
|
-
}
|
|
353
|
-
return void 0;
|
|
354
|
-
}
|
|
355
|
-
static calcQuotaBorrowRate({ quotas, quotaRates }) {
|
|
356
|
-
const totalRateBalance = Object.values(quotas).reduce(
|
|
357
|
-
(acc, { token, balance }) => {
|
|
358
|
-
const { rate = 0, isActive = false } = quotaRates?.[token] || {};
|
|
359
|
-
const quotaBalance = isActive ? balance : 0n;
|
|
360
|
-
const rateBalance = quotaBalance * BigInt(rate);
|
|
361
|
-
return acc + rateBalance;
|
|
362
|
-
},
|
|
363
|
-
0n
|
|
364
|
-
);
|
|
365
|
-
return totalRateBalance;
|
|
366
|
-
}
|
|
367
|
-
static calcRelativeBaseBorrowRate({
|
|
368
|
-
debt,
|
|
369
|
-
baseRateWithFee,
|
|
370
|
-
assetAmountInUnderlying
|
|
371
|
-
}) {
|
|
372
|
-
return debt * BigInt(baseRateWithFee) * assetAmountInUnderlying;
|
|
373
|
-
}
|
|
374
|
-
static liquidationPrice({
|
|
375
|
-
liquidationThresholds,
|
|
376
|
-
debt,
|
|
377
|
-
underlyingToken,
|
|
378
|
-
targetToken,
|
|
379
|
-
assets,
|
|
380
|
-
tokensList
|
|
381
|
-
}) {
|
|
382
|
-
const underlyingDecimals = tokensList[underlyingToken]?.decimals || 18;
|
|
383
|
-
const { balance: underlyingBalance = 0n } = assets[underlyingToken] || {};
|
|
384
|
-
const ltUnderlying = liquidationThresholds[underlyingToken] || 0n;
|
|
385
|
-
const effectiveDebt = (debt - underlyingBalance * ltUnderlying / import_constants.PERCENTAGE_FACTOR) * import_constants.WAD / 10n ** BigInt(underlyingDecimals);
|
|
386
|
-
const targetDecimals = tokensList[targetToken]?.decimals || 18;
|
|
387
|
-
const { balance: targetBalance = 0n } = assets[targetToken] || {};
|
|
388
|
-
const effectiveTargetBalance = targetBalance * import_constants.WAD / 10n ** BigInt(targetDecimals);
|
|
389
|
-
const lpLT = liquidationThresholds[targetToken] || 0n;
|
|
390
|
-
if (targetBalance <= 0n || lpLT <= 0n) return 0n;
|
|
391
|
-
return effectiveDebt * import_constants.PRICE_DECIMALS * import_constants.PERCENTAGE_FACTOR / (effectiveTargetBalance * lpLT);
|
|
392
|
-
}
|
|
393
|
-
/**
|
|
394
|
-
* Calculates the time remaining until liquidation for a credit account.
|
|
395
|
-
* @returns The time remaining until liquidation in milliseconds.
|
|
396
|
-
*/
|
|
397
|
-
static getTimeToLiquidation({
|
|
398
|
-
healthFactor,
|
|
399
|
-
totalBorrowRate_debt
|
|
400
|
-
}) {
|
|
401
|
-
if (healthFactor <= import_constants.PERCENTAGE_FACTOR || totalBorrowRate_debt === 0n)
|
|
402
|
-
return null;
|
|
403
|
-
const HF_1 = BigInt(healthFactor) - import_constants.PERCENTAGE_FACTOR;
|
|
404
|
-
const brPerYear = BigInt(import_constants.SECONDS_PER_YEAR) * import_constants.PERCENTAGE_FACTOR * import_constants.PERCENTAGE_DECIMALS / totalBorrowRate_debt;
|
|
405
|
-
return HF_1 * brPerYear * 1000n / import_constants.PERCENTAGE_FACTOR;
|
|
406
|
-
}
|
|
407
|
-
}
|
|
408
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
409
|
-
0 && (module.exports = {
|
|
410
|
-
CreditAccountDataUtils
|
|
411
|
-
});
|