@carrot-protocol/clend-rpc 0.1.6 → 0.1.7-fe-math-dev-24c809a

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/math.d.ts DELETED
@@ -1,322 +0,0 @@
1
- import { BN } from "@coral-xyz/anchor";
2
- import { InterestRateConfig, BankAccruedInterest } from "./state";
3
- /**
4
- * Convert a UI number to a token amount BN
5
- * @param uiAmount Amount in UI format (e.g., 1.5 USDC)
6
- * @param decimals Token decimals
7
- * @returns BN representing the token amount in smallest units (lamports)
8
- */
9
- export declare function uiToAmount(uiAmount: number, decimals: number): BN;
10
- /**
11
- * Convert a token amount BN to a UI number
12
- * @param tokenAmount BN representing the token amount in smallest units (lamports)
13
- * @param decimals Token decimals
14
- * @returns Number in UI format (e.g., 1.5 USDC)
15
- */
16
- export declare function amountToUi(amount: BN | number, decimals: number): number;
17
- /**
18
- * Calculate the utilization rate of a bank
19
- * @param totalSupply Total supply of tokens
20
- * @param totalBorrow Total borrowed tokens
21
- * @returns Utilization rate as a decimal (0-1)
22
- */
23
- export declare function calculateUtilizationRate(totalSupply: number, totalBorrow: number): number;
24
- /**
25
- * Calculate asset quantity from asset shares
26
- * @param assetShares Asset shares amount
27
- * @param assetShareValue Value of each asset share
28
- * @returns Asset quantity
29
- */
30
- export declare function calculateAssetQuantity(assetShares: number | BN, assetShareValue: number): number;
31
- /**
32
- * Calculate liability quantity from liability shares
33
- * @param liabilityShares Liability shares amount
34
- * @param liabilityShareValue Value of each liability share
35
- * @returns Liability quantity
36
- */
37
- export declare function calculateLiabilityQuantity(liabilityShares: number | BN, liabilityShareValue: number): number;
38
- /**
39
- * Calculate total asset quantity
40
- * @param totalAssetShares Total asset shares in the bank
41
- * @param assetShareValue Value of each asset share
42
- * @returns Total asset quantity
43
- *
44
- * Note: You can get these values from a Bank object:
45
- * - totalAssetShares = bank.totalAssetShares
46
- * - assetShareValue = bank.assetShareValue
47
- */
48
- export declare function calculateTotalAssetQuantity(totalAssetShares: number, assetShareValue: number): number;
49
- /**
50
- * Calculate total liability quantity
51
- * @param totalLiabilityShares Total liability shares in the bank
52
- * @param liabilityShareValue Value of each liability share
53
- * @returns Total liability quantity
54
- *
55
- * Note: You can get these values from a Bank object:
56
- * - totalLiabilityShares = bank.totalLiabilityShares
57
- * - liabilityShareValue = bank.liabilityShareValue
58
- */
59
- export declare function calculateTotalLiabilityQuantity(totalLiabilityShares: number, liabilityShareValue: number): number;
60
- /**
61
- * Calculate the bank's utilization rate
62
- * @param totalAssetShares Total asset shares in the bank
63
- * @param assetShareValue Value of each asset share
64
- * @param totalLiabilityShares Total liability shares in the bank
65
- * @param liabilityShareValue Value of each liability share
66
- * @returns Utilization rate as a decimal (0-1)
67
- *
68
- * Note: You can get these values from a Bank object:
69
- * - totalAssetShares = bank.totalAssetShares
70
- * - assetShareValue = bank.assetShareValue
71
- * - totalLiabilityShares = bank.totalLiabilityShares
72
- * - liabilityShareValue = bank.liabilityShareValue
73
- */
74
- export declare function calculateBankUtilizationRate(totalAssetShares: number, assetShareValue: number, totalLiabilityShares: number, liabilityShareValue: number): number;
75
- /**
76
- * Calculate the base interest rate based on utilization rate
77
- * @param utilizationRate Current utilization rate (0-1)
78
- * @param optimalUtilizationRate Optimal utilization rate target
79
- * @param plateauInterestRate Interest rate at optimal utilization
80
- * @param maxInterestRate Maximum interest rate at 100% utilization
81
- * @returns Base interest rate as a decimal
82
- *
83
- * Note: You can get these values from a Bank object:
84
- * - optimalUtilizationRate = bank.config.interestRateConfig.optimalUtilizationRate
85
- * - plateauInterestRate = bank.config.interestRateConfig.plateauInterestRate
86
- * - maxInterestRate = bank.config.interestRateConfig.maxInterestRate
87
- */
88
- export declare function calculateBaseInterestRate(utilizationRate: number, optimalUtilizationRate: number, plateauInterestRate: number, maxInterestRate: number): number;
89
- /**
90
- * Calculate the supply APY
91
- * @param utilizationRate Current utilization rate (0-1)
92
- * @param baseInterestRate Base interest rate calculated from utilization
93
- * @param protocolIrFee Protocol interest rate fee
94
- * @param insuranceIrFee Insurance interest rate fee
95
- * @returns Supply APY as a decimal (e.g., 0.05 for 5%)
96
- *
97
- * Note: You can get these values from a Bank object:
98
- * - utilizationRate = calculateBankUtilizationRate(...)
99
- * - baseInterestRate = calculateBaseInterestRate(...)
100
- * - protocolIrFee = bank.config.interestRateConfig.protocolIrFee
101
- * - insuranceIrFee = bank.config.interestRateConfig.insuranceIrFee
102
- */
103
- export declare function calculateSupplyApy(utilizationRate: number, baseInterestRate: number, protocolIrFee: number, insuranceIrFee: number): number;
104
- /**
105
- * Calculate the borrow APY
106
- * @param baseInterestRate Base interest rate calculated from utilization
107
- * @param protocolFixedFeeApr Protocol fixed fee APR
108
- * @param insuranceFeeFixedApr Insurance fixed fee APR
109
- * @returns Borrow APY as a decimal (e.g., 0.08 for 8%)
110
- *
111
- * Note: You can get these values from a Bank object:
112
- * - baseInterestRate = calculateBaseInterestRate(...)
113
- * - protocolFixedFeeApr = bank.config.interestRateConfig.protocolFixedFeeApr
114
- * - insuranceFeeFixedApr = bank.config.interestRateConfig.insuranceFeeFixedApr
115
- */
116
- export declare function calculateBorrowApy(baseInterestRate: number, protocolFixedFeeApr: number, insuranceFeeFixedApr: number): number;
117
- /**
118
- * Computes amounts needed to adjust leverage
119
- * @param currentCollateral - Current collateral amount (e.g., JLP)
120
- * @param currentDebt - Current debt amount (e.g., USDC)
121
- * @param collateralPrice - Current collateral price in USD
122
- * @param debtPrice - Current debt token price in USD
123
- * @param currentLeverage - Current leverage ratio
124
- * @param targetLeverage - Desired leverage ratio
125
- * @param collateralWeight - Weight of collateral for maintenance (0-1)
126
- * @param debtWeight - Weight of debt for maintenance (1+)
127
- */
128
- export declare function computeAdjustLeverageAmounts(currentCollateral: number, currentDebt: number, collateralPrice: number, debtPrice: number, currentLeverage: number, targetLeverage: number, collateralWeight: number, debtWeight: number): {
129
- isIncrease: boolean;
130
- collateralDelta: number;
131
- debtDelta: number;
132
- };
133
- /**
134
- * Computes the maximum leverage possible given deposit and borrow asset weights
135
- * @param depositAssetWeight - Initial risk weight of deposit asset (0-1). Higher value = more trusted as collateral
136
- * @param borrowLiabilityWeight - Initial risk weight of borrow asset (0-2). Higher value = more conservative borrowing
137
- * @returns Object containing:
138
- * - maxLeverage: Maximum leverage possible through recursive borrow-deposit
139
- * - ltv: Loan-to-Value ratio, representing what portion of collateral can be borrowed
140
- */
141
- export declare function computeMaxLeverage(depositAssetWeight: number, borrowLiabilityWeight: number): {
142
- maxLeverage: number;
143
- ltv: number;
144
- };
145
- /**
146
- * Computes the borrow and deposit amounts needed to achieve target leverage, accounting for weights
147
- * @param principal - Initial deposit amount
148
- * @param targetLeverage - Desired leverage multiplier
149
- * @param depositPrice - Price of deposit/collateral asset
150
- * @param borrowPrice - Price of borrow/debt asset
151
- * @param collateralWeight - Weight of collateral asset (0-1)
152
- * @param debtWeight - Weight of debt asset (1+)
153
- * @returns Object containing:
154
- * - borrowAmount: Amount to borrow in borrow asset units
155
- * - totalDepositAmount: Total final position size in deposit asset units
156
- */
157
- export declare function computeLoopingAmounts(principal: number, targetLeverage: number, depositPrice: number, borrowPrice: number, collateralWeight: number, debtWeight: number): {
158
- borrowAmountUi: number;
159
- totalDepositAmountUi: number;
160
- };
161
- /**
162
- * Calculates the weighted value of an amount based on its price and weight
163
- * @param amount - Raw amount of the token
164
- * @param price - Price of the token
165
- * @param weight - Weight to apply (0-1 for assets, 1+ for liabilities)
166
- * @returns Weighted value in USD terms
167
- */
168
- export declare function calculateWeightedValue(amount: number, price: number, weight: number): number;
169
- /**
170
- * Calculates the weighted value of an amount based on its price and weight
171
- * @param amount - Raw amount of the token
172
- * @param price - Price of the token
173
- * @param weight - Weight to apply (0-1 for assets, 1+ for liabilities)
174
- * @returns Weighted value in USD terms
175
- */
176
- export declare function calculateWeightedAmount(amount: number, weight: number): number;
177
- /**
178
- * Calculates leverage using weighted values
179
- * @param collateralAmount - Amount of collateral
180
- * @param collateralPrice - Price of collateral
181
- * @param collateralWeight - Weight of collateral (0-1)
182
- * @param debtAmount - Amount of debt
183
- * @param debtPrice - Price of debt
184
- * @param debtWeight - Weight of debt (1+)
185
- * @returns Leverage ratio
186
- */
187
- export declare function calculateWeightedLeverage(collateralAmount: number, collateralPrice: number, collateralWeight: number, debtAmount: number, debtPrice: number, debtWeight: number): number;
188
- /**
189
- * Calculates the debt to repay when withdrawing collateral to maintain the same leverage ratio
190
- * @param currentCollateral - Current collateral amount
191
- * @param currentDebt - Current debt amount
192
- * @param withdrawAmount - Amount of collateral to withdraw
193
- * @param collateralPrice - Price of collateral token
194
- * @param debtPrice - Price of debt token
195
- * @param collateralWeight - Weight of collateral (0-1)
196
- * @param debtWeight - Weight of debt (1+)
197
- * @returns Object containing the debt to repay and new leverage values
198
- */
199
- export declare function computeWithdrawLeverageAmounts(currentCollateral: number, currentDebt: number, withdrawAmount: number, collateralPrice: number, debtPrice: number, collateralWeight: number, debtWeight: number): {
200
- debtToRepayUi: number;
201
- newCollateralUi: number;
202
- newDebtUi: number;
203
- currentLeverage: number;
204
- newLeverage: number;
205
- };
206
- /**
207
- * Calculates the health factor and leverage of a Clend account using weighted maintenance values.
208
- *
209
- * Standard Health Factor = Total Weighted Assets (Maintenance) / Total Weighted Liabilities (Maintenance)
210
- * Interpretation:
211
- * - HF > 1: Position is overcollateralized (safe).
212
- * - HF = 1: Position is at the liquidation threshold.
213
- * - HF < 1: Position is undercollateralized and liquidatable.
214
- * - HF = Infinity: Position has no debt (healthiest).
215
- *
216
- * Leverage = Total Weighted Assets (Maintenance) / (Total Weighted Assets (Maintenance) - Total Weighted Liabilities (Maintenance))
217
- *
218
- * @param weightedAssetValues - An array of the user's weighted asset values (maintenance).
219
- * @param weightedLiabilityValues - An array of the user's weighted liability values (maintenance).
220
- * @returns An object containing the healthFactor and leverage.
221
- */
222
- export declare function calculateHealthFactorAndLeverage(weightedAssetValues: number[], weightedLiabilityValues: number[]): {
223
- healthFactor: number;
224
- leverage: number;
225
- };
226
- /**
227
- * Calculates the interest that has accrued but not yet been applied to a bank
228
- * @param lastUpdate The timestamp of the last update
229
- * @param assetAmountUi The total asset amount in UI format
230
- * @param liabilityAmountUi The total liability amount in UI format
231
- * @param utilizationRate The current utilization rate
232
- * @param interestRateConfig The interest rate configuration
233
- * @param currentTimestamp Optional current timestamp (in seconds). Defaults to Math.floor(Date.now() / 1000)
234
- * @returns Object containing:
235
- * - assetInterestAccrued: Interest accrued for lenders (in token units)
236
- * - liabilityInterestAccrued: Interest accrued for borrowers (in token units)
237
- * - insuranceFeesAccrued: Insurance fees accrued (in token units)
238
- * - groupFeesAccrued: Group fees accrued (in token units)
239
- * - protocolFeesAccrued: Protocol fees accrued (in token units)
240
- */
241
- export declare function calculateAccruedInterest(lastUpdate: BN, assetAmountUi: number, liabilityAmountUi: number, utilizationRate: number, interestRateConfig: InterestRateConfig, currentTimestamp: number): BankAccruedInterest;
242
- /**
243
- * Adjusts an ideal borrow amount downwards solely to compensate for the
244
- * protocol's origination fee. This calculates the principal amount to borrow
245
- * such that after the fee is added, the liability matches the ideal amount.
246
- *
247
- * @param idealBorrowAmount - The target borrow amount before considering the fee (native units, BN).
248
- * @param originationFeeRate - The protocol origination fee rate (e.g., 0.001 for 0.1%).
249
- * @returns Adjusted borrow amount (native units, BN), slightly less than or equal to ideal.
250
- */
251
- export declare function adjustBorrowForOriginationFee(idealBorrowAmount: BN, originationFeeRate: number): BN;
252
- /**
253
- * Adjusts an amount upwards to compensate for potential swap slippage,
254
- * assuming worst-case based on slippageBps.
255
- *
256
- * @param amountToAdjust - The amount to adjust (native units, BN), typically after cost adjustments.
257
- * @param slippageBps - The maximum slippage tolerance in basis points (e.g., 100 for 1%).
258
- * @returns Adjusted amount (native units, BN), slightly more than the input amount. Returns input amount if slippageBps is zero or invalid.
259
- */
260
- export declare function adjustAmountForSlippage(amountToAdjust: BN, slippageBps: number): BN;
261
- /**
262
- * Calculates the estimated liquidation price for a collateral asset.
263
- *
264
- * Liquidation is possible when the risk-adjusted value of debt meets or exceeds
265
- * the risk-adjusted value of the collateral, based on maintenance requirements.
266
- *
267
- * Formula:
268
- * CollateralLiquidationPrice = (DebtAmount * DebtPrice * DebtMaintenanceLiabilityWeight) / (CollateralAmount * CollateralMaintenanceAssetWeight)
269
- *
270
- * @param collateralAmount The amount of the collateral asset deposited (e.g., number of JLP tokens).
271
- * @param collateralMaintenanceAssetWeight The maintenance asset weight for the collateral asset (e.g., 0.85 for 85%). Found in Marginfi protocol config/UI.
272
- * @param debtAmount The amount of the debt asset borrowed (e.g., number of USDC tokens).
273
- * @param debtMaintenanceLiabilityWeight The maintenance liability weight for the debt asset (e.g., 1.1 for 110%). Found in Marginfi protocol config/UI.
274
- * @param debtPrice The current price of the debt asset.
275
- * @returns The estimated price of the collateral asset at which liquidation may occur, or Infinity if collateral amount/weight is zero.
276
- */
277
- export declare function calculateLiquidationPrice(collateralAmountUi: number, collateralMaintenanceAssetWeight: number, debtAmountUi: number, debtMaintenanceLiabilityWeight: number, debtPrice: number): number;
278
- /**
279
- * Calculates the Loan-to-Value (LTV) ratio.
280
- * LTV = Total Value of Liabilities / Total Value of Assets
281
- *
282
- * Note: The input values can be either weighted (initial or maintenance) or unweighted USD values,
283
- * as long as the same type of value is used consistently for both assets and liabilities.
284
- * The interpretation of the LTV depends on the type of values used.
285
- *
286
- * @param {number[]} assetValues - An array of the user's asset values (e.g., USD value, weighted or unweighted).
287
- * @param {number[]} liabilityValues - An array of the user's liability values (e.g., USD value, weighted or unweighted).
288
- * @returns {number} The LTV ratio. Returns 0 if there are no liabilities. Returns `NaN` if total asset value is zero or negative.
289
- */
290
- export declare function calculateLtv(assetValues: number[], liabilityValues: number[]): number;
291
- /**
292
- * Calculates the percentage change required for the current price to reach the liquidation price.
293
- *
294
- * @param currentPrice The current price of the asset.
295
- * @param liquidationPrice The price at which the asset would be liquidated.
296
- * @returns The percentage change needed to reach the liquidation price (e.g., -10.5 for a 10.5% drop). Returns Infinity if currentPrice is 0.
297
- */
298
- export declare function calculateLiquidationPriceChangePercentage(currentPrice: number, liquidationPrice: number): number;
299
- /**
300
- * Calculates the current liability interest for a specific liability position,
301
- * accounting for interest accrued since the bank's last update.
302
- *
303
- * @param userLiabilitySharesBN - The user's liability shares for the debt asset (native units, BN).
304
- * @param bankLiabilityShareValue - The bank's liability share value as of last update (number).
305
- * @param bankBorrowApy - The current estimated borrow APY of the debt asset (e.g., 0.025 for 2.5%).
306
- * @param bankLastUpdateSeconds - The Unix timestamp (seconds) when the bank's interest was last updated on-chain.
307
- * @param currentTimeSeconds - The current Unix timestamp (seconds).
308
- * @returns The estimated current debt amount for the user (native units, BN).
309
- */
310
- export declare function calculateLiabilityInterest(userLiabilityShares: number, bankLiabilityShareValue: number, bankBorrowApy: number, bankLastUpdateSeconds: number, currentTimeSeconds: number): BN;
311
- /**
312
- * Calculates the final debt repayment target amount by adding buffers
313
- * for future interest accrual and minor discrepancies to an estimated current debt.
314
- * this is used to calculate the final debt to repay amount taking into account tx execution time from when its calculated
315
- *
316
- * @param currentDebt - The current debt amount including interest accrued up to the present moment
317
- * @param borrowApy - The current estimated borrow APY of the debt asset (e.g., 0.025 for 2.5%).
318
- * @param futureInterestBufferSec - The estimated number of seconds of future interest to buffer for transaction execution time. As in, if it takes
319
- * 10 seconds to execute the tx from when this is called, we will add 10 seconds of interest to the debt
320
- * @returns The final debt repayment target amount (native units, BN).
321
- */
322
- export declare function addInterestAccrualBuffer(currentDebt: BN, borrowApy: number, futureInterestBufferSec: number): BN;