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