@kamino-finance/klend-sdk 5.0.3 → 5.0.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/classes/manager.d.ts +38 -4
- package/dist/classes/manager.d.ts.map +1 -1
- package/dist/classes/manager.js +98 -4
- package/dist/classes/manager.js.map +1 -1
- package/dist/classes/market.d.ts.map +1 -1
- package/dist/classes/market.js +2 -25
- package/dist/classes/market.js.map +1 -1
- package/dist/classes/vault.d.ts +12 -8
- package/dist/classes/vault.d.ts.map +1 -1
- package/dist/classes/vault.js +94 -66
- package/dist/classes/vault.js.map +1 -1
- package/dist/client_kamino_manager.d.ts.map +1 -1
- package/dist/client_kamino_manager.js +14 -0
- package/dist/client_kamino_manager.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/accounts/Reserve.d.ts +3 -3
- package/dist/idl_codegen_kamino_vault/accounts/Reserve.d.ts.map +1 -1
- package/dist/idl_codegen_kamino_vault/accounts/Reserve.js +22 -18
- package/dist/idl_codegen_kamino_vault/accounts/Reserve.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/accounts/VaultState.d.ts +45 -15
- package/dist/idl_codegen_kamino_vault/accounts/VaultState.d.ts.map +1 -1
- package/dist/idl_codegen_kamino_vault/accounts/VaultState.js +101 -39
- package/dist/idl_codegen_kamino_vault/accounts/VaultState.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/accounts/index.d.ts +4 -4
- package/dist/idl_codegen_kamino_vault/accounts/index.d.ts.map +1 -1
- package/dist/idl_codegen_kamino_vault/accounts/index.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/errors/anchor.d.ts.map +1 -1
- package/dist/idl_codegen_kamino_vault/errors/anchor.js +162 -162
- package/dist/idl_codegen_kamino_vault/errors/anchor.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/errors/custom.d.ts +144 -32
- package/dist/idl_codegen_kamino_vault/errors/custom.d.ts.map +1 -1
- package/dist/idl_codegen_kamino_vault/errors/custom.js +305 -109
- package/dist/idl_codegen_kamino_vault/errors/custom.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/errors/index.d.ts +3 -3
- package/dist/idl_codegen_kamino_vault/errors/index.d.ts.map +1 -1
- package/dist/idl_codegen_kamino_vault/errors/index.js +7 -2
- package/dist/idl_codegen_kamino_vault/errors/index.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/instructions/deposit.d.ts +4 -2
- package/dist/idl_codegen_kamino_vault/instructions/deposit.d.ts.map +1 -1
- package/dist/idl_codegen_kamino_vault/instructions/deposit.js +3 -1
- package/dist/idl_codegen_kamino_vault/instructions/deposit.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/instructions/giveUpPendingFees.d.ts +13 -0
- package/dist/idl_codegen_kamino_vault/instructions/giveUpPendingFees.d.ts.map +1 -0
- package/dist/idl_codegen_kamino_vault/instructions/giveUpPendingFees.js +47 -0
- package/dist/idl_codegen_kamino_vault/instructions/giveUpPendingFees.js.map +1 -0
- package/dist/idl_codegen_kamino_vault/instructions/index.d.ts +18 -10
- package/dist/idl_codegen_kamino_vault/instructions/index.d.ts.map +1 -1
- package/dist/idl_codegen_kamino_vault/instructions/index.js +9 -1
- package/dist/idl_codegen_kamino_vault/instructions/index.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/instructions/initVault.d.ts +2 -1
- package/dist/idl_codegen_kamino_vault/instructions/initVault.d.ts.map +1 -1
- package/dist/idl_codegen_kamino_vault/instructions/initVault.js +1 -0
- package/dist/idl_codegen_kamino_vault/instructions/initVault.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/instructions/invest.d.ts +6 -2
- package/dist/idl_codegen_kamino_vault/instructions/invest.d.ts.map +1 -1
- package/dist/idl_codegen_kamino_vault/instructions/invest.js +9 -1
- package/dist/idl_codegen_kamino_vault/instructions/invest.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/instructions/updateAdmin.d.ts +7 -0
- package/dist/idl_codegen_kamino_vault/instructions/updateAdmin.d.ts.map +1 -0
- package/dist/idl_codegen_kamino_vault/instructions/updateAdmin.js +16 -0
- package/dist/idl_codegen_kamino_vault/instructions/updateAdmin.js.map +1 -0
- package/dist/idl_codegen_kamino_vault/instructions/updateReserveAllocation.d.ts +2 -2
- package/dist/idl_codegen_kamino_vault/instructions/updateReserveAllocation.d.ts.map +1 -1
- package/dist/idl_codegen_kamino_vault/instructions/updateReserveAllocation.js +1 -1
- package/dist/idl_codegen_kamino_vault/instructions/updateReserveAllocation.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/instructions/updateVaultConfig.d.ts +14 -0
- package/dist/idl_codegen_kamino_vault/instructions/updateVaultConfig.d.ts.map +1 -0
- package/dist/idl_codegen_kamino_vault/instructions/updateVaultConfig.js +52 -0
- package/dist/idl_codegen_kamino_vault/instructions/updateVaultConfig.js.map +1 -0
- package/dist/idl_codegen_kamino_vault/instructions/withdraw.d.ts +4 -2
- package/dist/idl_codegen_kamino_vault/instructions/withdraw.d.ts.map +1 -1
- package/dist/idl_codegen_kamino_vault/instructions/withdraw.js +8 -2
- package/dist/idl_codegen_kamino_vault/instructions/withdraw.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/instructions/withdrawPendingFees.d.ts +23 -0
- package/dist/idl_codegen_kamino_vault/instructions/withdrawPendingFees.d.ts.map +1 -0
- package/dist/idl_codegen_kamino_vault/instructions/withdrawPendingFees.js +51 -0
- package/dist/idl_codegen_kamino_vault/instructions/withdrawPendingFees.js.map +1 -0
- package/dist/idl_codegen_kamino_vault/programId.d.ts +1 -1
- package/dist/idl_codegen_kamino_vault/programId.d.ts.map +1 -1
- package/dist/idl_codegen_kamino_vault/programId.js +1 -1
- package/dist/idl_codegen_kamino_vault/programId.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/types/BigFractionBytes.d.ts +2 -2
- package/dist/idl_codegen_kamino_vault/types/BigFractionBytes.d.ts.map +1 -1
- package/dist/idl_codegen_kamino_vault/types/BigFractionBytes.js +4 -1
- package/dist/idl_codegen_kamino_vault/types/BigFractionBytes.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/types/BorrowRateCurve.d.ts +1 -1
- package/dist/idl_codegen_kamino_vault/types/BorrowRateCurve.d.ts.map +1 -1
- package/dist/idl_codegen_kamino_vault/types/BorrowRateCurve.js +1 -1
- package/dist/idl_codegen_kamino_vault/types/BorrowRateCurve.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/types/CurvePoint.d.ts +1 -1
- package/dist/idl_codegen_kamino_vault/types/CurvePoint.d.ts.map +1 -1
- package/dist/idl_codegen_kamino_vault/types/CurvePoint.js +1 -1
- package/dist/idl_codegen_kamino_vault/types/CurvePoint.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/types/LastUpdate.d.ts +2 -2
- package/dist/idl_codegen_kamino_vault/types/LastUpdate.d.ts.map +1 -1
- package/dist/idl_codegen_kamino_vault/types/LastUpdate.js +6 -1
- package/dist/idl_codegen_kamino_vault/types/LastUpdate.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/types/PriceHeuristic.d.ts +2 -2
- package/dist/idl_codegen_kamino_vault/types/PriceHeuristic.d.ts.map +1 -1
- package/dist/idl_codegen_kamino_vault/types/PriceHeuristic.js +1 -1
- package/dist/idl_codegen_kamino_vault/types/PriceHeuristic.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/types/PythConfiguration.d.ts +2 -2
- package/dist/idl_codegen_kamino_vault/types/PythConfiguration.d.ts.map +1 -1
- package/dist/idl_codegen_kamino_vault/types/PythConfiguration.js +1 -1
- package/dist/idl_codegen_kamino_vault/types/PythConfiguration.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/types/ReserveCollateral.d.ts +3 -3
- package/dist/idl_codegen_kamino_vault/types/ReserveCollateral.d.ts.map +1 -1
- package/dist/idl_codegen_kamino_vault/types/ReserveCollateral.js +5 -5
- package/dist/idl_codegen_kamino_vault/types/ReserveCollateral.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/types/ReserveConfig.d.ts +27 -24
- package/dist/idl_codegen_kamino_vault/types/ReserveConfig.d.ts.map +1 -1
- package/dist/idl_codegen_kamino_vault/types/ReserveConfig.js +59 -53
- package/dist/idl_codegen_kamino_vault/types/ReserveConfig.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/types/ReserveFees.d.ts +2 -2
- package/dist/idl_codegen_kamino_vault/types/ReserveFees.d.ts.map +1 -1
- package/dist/idl_codegen_kamino_vault/types/ReserveFees.js +5 -1
- package/dist/idl_codegen_kamino_vault/types/ReserveFees.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/types/ReserveLiquidity.d.ts +11 -3
- package/dist/idl_codegen_kamino_vault/types/ReserveLiquidity.d.ts.map +1 -1
- package/dist/idl_codegen_kamino_vault/types/ReserveLiquidity.js +25 -17
- package/dist/idl_codegen_kamino_vault/types/ReserveLiquidity.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/types/ScopeConfiguration.d.ts +2 -2
- package/dist/idl_codegen_kamino_vault/types/ScopeConfiguration.d.ts.map +1 -1
- package/dist/idl_codegen_kamino_vault/types/ScopeConfiguration.js +3 -3
- package/dist/idl_codegen_kamino_vault/types/ScopeConfiguration.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/types/SwitchboardConfiguration.d.ts +2 -2
- package/dist/idl_codegen_kamino_vault/types/SwitchboardConfiguration.d.ts.map +1 -1
- package/dist/idl_codegen_kamino_vault/types/SwitchboardConfiguration.js +1 -1
- package/dist/idl_codegen_kamino_vault/types/SwitchboardConfiguration.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/types/TokenInfo.d.ts +3 -3
- package/dist/idl_codegen_kamino_vault/types/TokenInfo.d.ts.map +1 -1
- package/dist/idl_codegen_kamino_vault/types/TokenInfo.js +11 -11
- package/dist/idl_codegen_kamino_vault/types/TokenInfo.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/types/VaultAllocation.d.ts +18 -8
- package/dist/idl_codegen_kamino_vault/types/VaultAllocation.d.ts.map +1 -1
- package/dist/idl_codegen_kamino_vault/types/VaultAllocation.js +27 -13
- package/dist/idl_codegen_kamino_vault/types/VaultAllocation.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/types/VaultConfigField.d.ts +110 -0
- package/dist/idl_codegen_kamino_vault/types/VaultConfigField.d.ts.map +1 -0
- package/dist/idl_codegen_kamino_vault/types/VaultConfigField.js +242 -0
- package/dist/idl_codegen_kamino_vault/types/VaultConfigField.js.map +1 -0
- package/dist/idl_codegen_kamino_vault/types/index.d.ts +34 -30
- package/dist/idl_codegen_kamino_vault/types/index.d.ts.map +1 -1
- package/dist/idl_codegen_kamino_vault/types/index.js +26 -1
- package/dist/idl_codegen_kamino_vault/types/index.js.map +1 -1
- package/dist/lending_operations/repay_with_collateral_operations.d.ts.map +1 -1
- package/dist/lending_operations/repay_with_collateral_operations.js +6 -0
- package/dist/lending_operations/repay_with_collateral_operations.js.map +1 -1
- package/dist/leverage/calcs.d.ts +28 -1
- package/dist/leverage/calcs.d.ts.map +1 -1
- package/dist/leverage/calcs.js +204 -8
- package/dist/leverage/calcs.js.map +1 -1
- package/dist/leverage/index.d.ts +1 -0
- package/dist/leverage/index.d.ts.map +1 -1
- package/dist/leverage/index.js +1 -0
- package/dist/leverage/index.js.map +1 -1
- package/dist/leverage/operations.d.ts +14 -241
- package/dist/leverage/operations.d.ts.map +1 -1
- package/dist/leverage/operations.js +508 -776
- package/dist/leverage/operations.js.map +1 -1
- package/dist/leverage/types.d.ts +173 -0
- package/dist/leverage/types.d.ts.map +1 -0
- package/dist/leverage/types.js +3 -0
- package/dist/leverage/types.js.map +1 -0
- package/dist/leverage/utils.d.ts +5 -5
- package/dist/leverage/utils.d.ts.map +1 -1
- package/dist/leverage/utils.js +68 -33
- package/dist/leverage/utils.js.map +1 -1
- package/dist/utils/constants.d.ts +1 -0
- package/dist/utils/constants.d.ts.map +1 -1
- package/dist/utils/constants.js +2 -1
- package/dist/utils/constants.js.map +1 -1
- package/dist/utils/fuzz.d.ts +3 -0
- package/dist/utils/fuzz.d.ts.map +1 -0
- package/dist/utils/fuzz.js +11 -0
- package/dist/utils/fuzz.js.map +1 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +1 -0
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/rpc.d.ts.map +1 -1
- package/dist/utils/rpc.js +2 -1
- package/dist/utils/rpc.js.map +1 -1
- package/package.json +2 -1
- package/src/classes/manager.ts +122 -4
- package/src/classes/market.ts +1 -1
- package/src/classes/vault.ts +123 -49
- package/src/client_kamino_manager.ts +18 -0
- package/src/idl_codegen_kamino_vault/accounts/Reserve.ts +132 -105
- package/src/idl_codegen_kamino_vault/accounts/VaultState.ts +217 -124
- package/src/idl_codegen_kamino_vault/accounts/index.ts +4 -4
- package/src/idl_codegen_kamino_vault/errors/anchor.ts +335 -326
- package/src/idl_codegen_kamino_vault/errors/custom.ts +353 -157
- package/src/idl_codegen_kamino_vault/errors/index.ts +35 -22
- package/src/idl_codegen_kamino_vault/instructions/deposit.ts +33 -25
- package/src/idl_codegen_kamino_vault/instructions/giveUpPendingFees.ts +40 -0
- package/src/idl_codegen_kamino_vault/instructions/index.ts +27 -10
- package/src/idl_codegen_kamino_vault/instructions/initVault.ts +25 -20
- package/src/idl_codegen_kamino_vault/instructions/invest.ts +40 -25
- package/src/idl_codegen_kamino_vault/instructions/updateAdmin.ts +24 -0
- package/src/idl_codegen_kamino_vault/instructions/updateReserveAllocation.ts +24 -24
- package/src/idl_codegen_kamino_vault/instructions/updateVaultConfig.ts +49 -0
- package/src/idl_codegen_kamino_vault/instructions/withdraw.ts +45 -33
- package/src/idl_codegen_kamino_vault/instructions/withdrawPendingFees.ts +75 -0
- package/src/idl_codegen_kamino_vault/programId.ts +5 -3
- package/src/idl_codegen_kamino_vault/types/BigFractionBytes.ts +24 -18
- package/src/idl_codegen_kamino_vault/types/BorrowRateCurve.ts +21 -16
- package/src/idl_codegen_kamino_vault/types/CurvePoint.ts +21 -18
- package/src/idl_codegen_kamino_vault/types/LastUpdate.ts +32 -27
- package/src/idl_codegen_kamino_vault/types/PriceHeuristic.ts +25 -22
- package/src/idl_codegen_kamino_vault/types/PythConfiguration.ts +14 -14
- package/src/idl_codegen_kamino_vault/types/ReserveCollateral.ts +35 -35
- package/src/idl_codegen_kamino_vault/types/ReserveConfig.ts +232 -191
- package/src/idl_codegen_kamino_vault/types/ReserveFees.ts +27 -23
- package/src/idl_codegen_kamino_vault/types/ReserveLiquidity.ts +116 -98
- package/src/idl_codegen_kamino_vault/types/ScopeConfiguration.ts +25 -25
- package/src/idl_codegen_kamino_vault/types/SwitchboardConfiguration.ts +21 -18
- package/src/idl_codegen_kamino_vault/types/TokenInfo.ts +92 -74
- package/src/idl_codegen_kamino_vault/types/VaultAllocation.ts +67 -49
- package/src/idl_codegen_kamino_vault/types/VaultConfigField.ts +270 -0
- package/src/idl_codegen_kamino_vault/types/index.ts +77 -30
- package/src/lending_operations/repay_with_collateral_operations.ts +2 -0
- package/src/leverage/calcs.ts +315 -8
- package/src/leverage/index.ts +1 -0
- package/src/leverage/operations.ts +1079 -1331
- package/src/leverage/types.ts +211 -0
- package/src/leverage/utils.ts +103 -64
- package/src/utils/constants.ts +2 -0
- package/src/utils/fuzz.ts +5 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/rpc.ts +2 -1
package/src/leverage/calcs.ts
CHANGED
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
import { PublicKey } from '@solana/web3.js';
|
|
2
2
|
import Decimal from 'decimal.js';
|
|
3
|
-
import { Kamino, StrategyWithAddress, TokenAmounts } from '@kamino-finance/kliquidity-sdk';
|
|
3
|
+
import { collToLamportsDecimal, Kamino, StrategyWithAddress, TokenAmounts } from '@kamino-finance/kliquidity-sdk';
|
|
4
|
+
import { KaminoMarket, KaminoObligation, KaminoReserve } from '../classes';
|
|
5
|
+
import { getExpectedTokenBalanceAfterBorrow } from './utils';
|
|
6
|
+
import {
|
|
7
|
+
AdjustLeverageCalcsResult,
|
|
8
|
+
DepositLeverageCalcsResult,
|
|
9
|
+
PriceAinBProvider,
|
|
10
|
+
WithdrawLeverageCalcsResult,
|
|
11
|
+
} from './types';
|
|
12
|
+
import { fuzzyEqual } from '../utils';
|
|
4
13
|
|
|
5
14
|
export const toJson = (object: any): string => {
|
|
6
15
|
return JSON.stringify(object, null, 2);
|
|
@@ -126,9 +135,13 @@ export async function calculateMultiplyEffects(
|
|
|
126
135
|
isClosingPosition =
|
|
127
136
|
(withdrawModeEstimatedDepositTokenWithdrawn.gte(new Decimal(deposited)) ||
|
|
128
137
|
withdrawModeEstimatedBorrowTokenWithdrawn.gte(new Decimal(borrowed)) ||
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
138
|
+
fuzzyEqual(
|
|
139
|
+
withdrawModeEstimatedDepositTokenWithdrawn,
|
|
140
|
+
new Decimal(deposited),
|
|
141
|
+
closingPositionDiffTolerance
|
|
142
|
+
) ||
|
|
143
|
+
fuzzyEqual(withdrawModeEstimatedBorrowTokenWithdrawn, new Decimal(borrowed), closingPositionDiffTolerance)) &&
|
|
144
|
+
!fuzzyEqual(withdrawModeEstimatedDepositTokenWithdrawn, new Decimal(0), closingPositionDiffTolerance);
|
|
132
145
|
|
|
133
146
|
totalDeposited = isClosingPosition ? new Decimal(0) : deposited.sub(withdrawModeEstimatedDepositTokenWithdrawn);
|
|
134
147
|
totalBorrowed = isClosingPosition ? new Decimal(0) : borrowed.sub(withdrawModeEstimatedBorrowTokenWithdrawn);
|
|
@@ -270,10 +283,6 @@ export function calcWithdrawAmounts(params: WithdrawParams): WithdrawResult {
|
|
|
270
283
|
};
|
|
271
284
|
}
|
|
272
285
|
|
|
273
|
-
export const fuzzyEq = (a: Decimal.Value, b: Decimal.Value, epsilon = 0.0001) => {
|
|
274
|
-
return new Decimal(a).sub(b).abs().lte(epsilon);
|
|
275
|
-
};
|
|
276
|
-
|
|
277
286
|
interface UseEstimateAdjustAmountsProps {
|
|
278
287
|
targetLeverage: Decimal;
|
|
279
288
|
debtTokenMint: PublicKey;
|
|
@@ -463,3 +472,301 @@ export async function simulateMintKToken(
|
|
|
463
472
|
|
|
464
473
|
return [requiredA, requiredB, actualMint];
|
|
465
474
|
}
|
|
475
|
+
|
|
476
|
+
export const depositLeverageCalcs = (props: {
|
|
477
|
+
depositAmount: Decimal;
|
|
478
|
+
depositTokenIsCollToken: boolean;
|
|
479
|
+
depositTokenIsSol: boolean;
|
|
480
|
+
priceDebtToColl: Decimal;
|
|
481
|
+
targetLeverage: Decimal;
|
|
482
|
+
slippagePct: Decimal;
|
|
483
|
+
flashLoanFee: Decimal;
|
|
484
|
+
}): DepositLeverageCalcsResult => {
|
|
485
|
+
// Initialize local variables from the props object
|
|
486
|
+
const {
|
|
487
|
+
depositAmount,
|
|
488
|
+
depositTokenIsCollToken,
|
|
489
|
+
depositTokenIsSol,
|
|
490
|
+
priceDebtToColl,
|
|
491
|
+
targetLeverage,
|
|
492
|
+
slippagePct,
|
|
493
|
+
flashLoanFee,
|
|
494
|
+
} = props;
|
|
495
|
+
const slippage = slippagePct.div('100');
|
|
496
|
+
|
|
497
|
+
const initDepositInSol = depositTokenIsSol ? depositAmount : new Decimal(0);
|
|
498
|
+
|
|
499
|
+
// Core logic
|
|
500
|
+
if (depositTokenIsCollToken) {
|
|
501
|
+
const y = targetLeverage.mul(priceDebtToColl);
|
|
502
|
+
const x = flashLoanFee.add('1').mul(slippage.add('1')).div(priceDebtToColl);
|
|
503
|
+
const finalColl = depositAmount.mul(x).div(x.sub(targetLeverage.sub('1').div(y)));
|
|
504
|
+
const debt = finalColl.sub(depositAmount).mul(x);
|
|
505
|
+
const flashBorrowColl = finalColl.sub(depositAmount).mul(flashLoanFee.add('1'));
|
|
506
|
+
|
|
507
|
+
return {
|
|
508
|
+
flashBorrowInCollToken: flashBorrowColl,
|
|
509
|
+
initDepositInSol,
|
|
510
|
+
debtTokenToBorrow: debt,
|
|
511
|
+
collTokenToDeposit: finalColl,
|
|
512
|
+
swapDebtTokenIn: debt,
|
|
513
|
+
swapCollTokenExpectedOut: finalColl.sub(depositAmount),
|
|
514
|
+
flashBorrowInDebtTokenKtokenOnly: new Decimal(0),
|
|
515
|
+
singleSidedDepositKtokenOnly: new Decimal(0),
|
|
516
|
+
requiredCollateralKtokenOnly: new Decimal(0),
|
|
517
|
+
};
|
|
518
|
+
} else {
|
|
519
|
+
const y = targetLeverage.mul(priceDebtToColl);
|
|
520
|
+
const x = flashLoanFee.add('1').mul(slippage.add('1')).div(priceDebtToColl);
|
|
521
|
+
const finalColl = depositAmount.div(x.sub(targetLeverage.sub('1').div(y)));
|
|
522
|
+
const flashBorrowColl = finalColl.mul(flashLoanFee.add('1'));
|
|
523
|
+
const debt = targetLeverage.sub('1').mul(finalColl).div(y);
|
|
524
|
+
|
|
525
|
+
return {
|
|
526
|
+
flashBorrowInCollToken: flashBorrowColl,
|
|
527
|
+
initDepositInSol,
|
|
528
|
+
debtTokenToBorrow: debt,
|
|
529
|
+
collTokenToDeposit: finalColl,
|
|
530
|
+
swapDebtTokenIn: debt.add(depositAmount),
|
|
531
|
+
swapCollTokenExpectedOut: finalColl,
|
|
532
|
+
flashBorrowInDebtTokenKtokenOnly: new Decimal(0),
|
|
533
|
+
singleSidedDepositKtokenOnly: new Decimal(0),
|
|
534
|
+
requiredCollateralKtokenOnly: new Decimal(0),
|
|
535
|
+
};
|
|
536
|
+
}
|
|
537
|
+
};
|
|
538
|
+
|
|
539
|
+
export const depositLeverageKtokenCalcs = async (props: {
|
|
540
|
+
kamino: Kamino;
|
|
541
|
+
strategy: StrategyWithAddress;
|
|
542
|
+
debtTokenMint: PublicKey;
|
|
543
|
+
depositAmount: Decimal;
|
|
544
|
+
depositTokenIsCollToken: boolean;
|
|
545
|
+
depositTokenIsSol: boolean;
|
|
546
|
+
priceDebtToColl: Decimal;
|
|
547
|
+
targetLeverage: Decimal;
|
|
548
|
+
slippagePct: Decimal;
|
|
549
|
+
flashLoanFee: Decimal;
|
|
550
|
+
priceAinB: PriceAinBProvider;
|
|
551
|
+
strategyHoldings?: TokenAmounts;
|
|
552
|
+
}): Promise<DepositLeverageCalcsResult> => {
|
|
553
|
+
const {
|
|
554
|
+
kamino,
|
|
555
|
+
strategy,
|
|
556
|
+
debtTokenMint,
|
|
557
|
+
depositAmount,
|
|
558
|
+
depositTokenIsCollToken,
|
|
559
|
+
depositTokenIsSol,
|
|
560
|
+
priceDebtToColl,
|
|
561
|
+
targetLeverage,
|
|
562
|
+
slippagePct,
|
|
563
|
+
flashLoanFee,
|
|
564
|
+
priceAinB,
|
|
565
|
+
strategyHoldings,
|
|
566
|
+
} = props;
|
|
567
|
+
const initDepositInSol = depositTokenIsSol ? depositAmount : new Decimal(0);
|
|
568
|
+
const slippage = slippagePct.div('100');
|
|
569
|
+
|
|
570
|
+
let flashBorrowInDebtToken: Decimal;
|
|
571
|
+
let collTokenToDeposit: Decimal;
|
|
572
|
+
let debtTokenToBorrow: Decimal;
|
|
573
|
+
|
|
574
|
+
if (depositTokenIsCollToken) {
|
|
575
|
+
const x = slippage.add('1').div(priceDebtToColl);
|
|
576
|
+
const y = flashLoanFee.add('1').mul(priceDebtToColl);
|
|
577
|
+
const z = targetLeverage.mul(y).div(targetLeverage.sub(1));
|
|
578
|
+
flashBorrowInDebtToken = depositAmount.div(z.minus(new Decimal(1).div(x)));
|
|
579
|
+
collTokenToDeposit = depositAmount.add(flashBorrowInDebtToken.div(x));
|
|
580
|
+
debtTokenToBorrow = flashBorrowInDebtToken.mul(new Decimal(1).add(flashLoanFee));
|
|
581
|
+
|
|
582
|
+
return {
|
|
583
|
+
flashBorrowInCollToken: new Decimal(0),
|
|
584
|
+
initDepositInSol,
|
|
585
|
+
collTokenToDeposit,
|
|
586
|
+
debtTokenToBorrow,
|
|
587
|
+
swapDebtTokenIn: new Decimal(0),
|
|
588
|
+
swapCollTokenExpectedOut: new Decimal(0),
|
|
589
|
+
flashBorrowInDebtTokenKtokenOnly: flashBorrowInDebtToken,
|
|
590
|
+
requiredCollateralKtokenOnly: collTokenToDeposit.sub(depositAmount), // Assuming netValue is requiredCollateral, adjust as needed
|
|
591
|
+
singleSidedDepositKtokenOnly: flashBorrowInDebtToken,
|
|
592
|
+
};
|
|
593
|
+
} else {
|
|
594
|
+
const y = targetLeverage.mul(priceDebtToColl);
|
|
595
|
+
// although we will only swap ~half of the debt token, we account for the slippage on the entire amount as we are working backwards from the minimum collateral and do not know the exact swap proportion in advance
|
|
596
|
+
// This also allows for some variation in the pool ratios between calculation + submitting the tx
|
|
597
|
+
const x = flashLoanFee.add('1').mul(slippage.add('1')).div(priceDebtToColl);
|
|
598
|
+
// Calculate the amount of collateral tokens we will deposit in order to achieve the desired leverage after swapping a portion of the debt token and flash loan fees
|
|
599
|
+
const finalColl = depositAmount.div(x.sub(targetLeverage.sub('1').div(y)));
|
|
600
|
+
// Calculate how many A and B tokens we will need to actually mint the desired amount of ktoken collateral
|
|
601
|
+
// The actual amount of ktokens received may be less than the finalColl due to smart proportional contract logic
|
|
602
|
+
// So we use the actualColl as the amount we will deposit
|
|
603
|
+
const [estimatedA, estimatedB, actualColl] = await simulateMintKToken(
|
|
604
|
+
kamino!,
|
|
605
|
+
strategy!,
|
|
606
|
+
finalColl,
|
|
607
|
+
strategyHoldings
|
|
608
|
+
);
|
|
609
|
+
const pxAinB = await priceAinB(strategy!.strategy.tokenAMint, strategy!.strategy.tokenBMint);
|
|
610
|
+
const isTokenADeposit = strategy.strategy.tokenAMint.equals(debtTokenMint);
|
|
611
|
+
// Calculate the amount we need to flash borrow by combining value of A and B into the debt token
|
|
612
|
+
const singleSidedDepositAmount = isTokenADeposit
|
|
613
|
+
? estimatedA.add(estimatedB.div(pxAinB))
|
|
614
|
+
: estimatedB.add(estimatedA.mul(pxAinB));
|
|
615
|
+
|
|
616
|
+
// Add slippage to the entire amount, add flash loan fee to part we will flash borrow
|
|
617
|
+
flashBorrowInDebtToken = singleSidedDepositAmount
|
|
618
|
+
.div(new Decimal('1').sub(slippage))
|
|
619
|
+
.sub(depositAmount)
|
|
620
|
+
.div(new Decimal('1').sub(flashLoanFee));
|
|
621
|
+
// Deposit the min ktoken amount we calculated at the beginning
|
|
622
|
+
// Any slippage will be left in the user's wallet as ktokens
|
|
623
|
+
collTokenToDeposit = actualColl;
|
|
624
|
+
debtTokenToBorrow = flashBorrowInDebtToken.div(new Decimal('1').sub(flashLoanFee));
|
|
625
|
+
// Add slippage to ensure we try to swap/deposit as much as possible after flash loan fees
|
|
626
|
+
const singleSidedDeposit = singleSidedDepositAmount.div(new Decimal('1').sub(slippage));
|
|
627
|
+
|
|
628
|
+
return {
|
|
629
|
+
flashBorrowInCollToken: new Decimal(0),
|
|
630
|
+
initDepositInSol,
|
|
631
|
+
collTokenToDeposit,
|
|
632
|
+
debtTokenToBorrow,
|
|
633
|
+
swapDebtTokenIn: new Decimal(0),
|
|
634
|
+
swapCollTokenExpectedOut: new Decimal(0),
|
|
635
|
+
flashBorrowInDebtTokenKtokenOnly: flashBorrowInDebtToken,
|
|
636
|
+
singleSidedDepositKtokenOnly: singleSidedDeposit,
|
|
637
|
+
requiredCollateralKtokenOnly: collTokenToDeposit, // Assuming collTokenToDeposit is requiredCollateral, adjust as needed
|
|
638
|
+
};
|
|
639
|
+
}
|
|
640
|
+
};
|
|
641
|
+
|
|
642
|
+
export function withdrawLeverageCalcs(
|
|
643
|
+
market: KaminoMarket,
|
|
644
|
+
collReserve: KaminoReserve,
|
|
645
|
+
debtReserve: KaminoReserve,
|
|
646
|
+
priceCollToDebt: Decimal,
|
|
647
|
+
withdrawAmount: Decimal,
|
|
648
|
+
deposited: Decimal,
|
|
649
|
+
borrowed: Decimal,
|
|
650
|
+
currentSlot: number,
|
|
651
|
+
isClosingPosition: boolean,
|
|
652
|
+
selectedTokenIsCollToken: boolean,
|
|
653
|
+
selectedTokenMint: PublicKey,
|
|
654
|
+
obligation: KaminoObligation,
|
|
655
|
+
flashLoanFee: Decimal,
|
|
656
|
+
slippagePct: Decimal
|
|
657
|
+
): WithdrawLeverageCalcsResult {
|
|
658
|
+
// 1. Calculate coll_amount and debt_amount to repay such that we maintain leverage and we withdraw to
|
|
659
|
+
// the wallet `amountInDepositTokenToWithdrawToWallet` amount of collateral token
|
|
660
|
+
// We need to withdraw withdrawAmountInDepositToken coll tokens
|
|
661
|
+
// and repay repayAmountInBorrowToken debt tokens
|
|
662
|
+
const { adjustDepositPosition: withdrawAmountCalculated, adjustBorrowPosition: initialRepayAmount } =
|
|
663
|
+
isClosingPosition
|
|
664
|
+
? { adjustDepositPosition: deposited, adjustBorrowPosition: borrowed }
|
|
665
|
+
: calcWithdrawAmounts({
|
|
666
|
+
collTokenMint: collReserve.getLiquidityMint(),
|
|
667
|
+
priceCollToDebt: new Decimal(priceCollToDebt),
|
|
668
|
+
currentDepositPosition: deposited,
|
|
669
|
+
currentBorrowPosition: borrowed,
|
|
670
|
+
withdrawAmount: new Decimal(withdrawAmount),
|
|
671
|
+
selectedTokenMint: selectedTokenMint,
|
|
672
|
+
});
|
|
673
|
+
|
|
674
|
+
// Add slippage for the accrued interest rate amount
|
|
675
|
+
const irSlippageBpsForDebt = obligation!
|
|
676
|
+
.estimateObligationInterestRate(market, debtReserve!, obligation?.state.borrows[0]!, currentSlot)
|
|
677
|
+
.toDecimalPlaces(debtReserve?.state.liquidity.mintDecimals.toNumber()!, Decimal.ROUND_CEIL);
|
|
678
|
+
// add 0.1 to irSlippageBpsForDebt because we don't want to estimate slightly less than SC and end up not reapying enough
|
|
679
|
+
const repayAmount = initialRepayAmount
|
|
680
|
+
.mul(irSlippageBpsForDebt.add('0.1').div('10_000').add('1'))
|
|
681
|
+
.toDecimalPlaces(debtReserve?.state.liquidity.mintDecimals.toNumber()!, Decimal.ROUND_CEIL);
|
|
682
|
+
|
|
683
|
+
// 6. Get swap ixns
|
|
684
|
+
// 5. Get swap estimations to understand how much we need to borrow from borrow reserve
|
|
685
|
+
// prevent withdrawing more then deposited if we close position
|
|
686
|
+
const depositTokenWithdrawAmount = !isClosingPosition
|
|
687
|
+
? withdrawAmountCalculated.mul(new Decimal(1).plus(flashLoanFee))
|
|
688
|
+
: withdrawAmountCalculated;
|
|
689
|
+
|
|
690
|
+
// We are swapping debt token
|
|
691
|
+
// When withdrawing coll, it means we just need to swap enough to pay for the flash borrow
|
|
692
|
+
const swapAmountIfWithdrawingColl = repayAmount
|
|
693
|
+
.mul(new Decimal(1).plus(flashLoanFee))
|
|
694
|
+
.mul(new Decimal(1).plus(slippagePct.div(100)))
|
|
695
|
+
.div(priceCollToDebt);
|
|
696
|
+
|
|
697
|
+
// When withdrawing debt, it means we need to swap just the collateral we are withdrwaing
|
|
698
|
+
// enough to cover the debt we are repaying, leaving the remaining in the wallet
|
|
699
|
+
const swapAmountIfWithdrawingDebt = withdrawAmountCalculated;
|
|
700
|
+
|
|
701
|
+
const collTokenSwapIn = selectedTokenIsCollToken ? swapAmountIfWithdrawingColl : swapAmountIfWithdrawingDebt;
|
|
702
|
+
const debtTokenExpectedSwapOut = collTokenSwapIn.mul(priceCollToDebt).div(new Decimal(1).add(slippagePct.div(100)));
|
|
703
|
+
|
|
704
|
+
return {
|
|
705
|
+
withdrawAmount: withdrawAmountCalculated,
|
|
706
|
+
repayAmount,
|
|
707
|
+
collTokenSwapIn,
|
|
708
|
+
debtTokenExpectedSwapOut,
|
|
709
|
+
depositTokenWithdrawAmount,
|
|
710
|
+
};
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
export async function adjustDepositLeverageCalcs(
|
|
714
|
+
market: KaminoMarket,
|
|
715
|
+
owner: PublicKey,
|
|
716
|
+
debtReserve: KaminoReserve,
|
|
717
|
+
adjustDepositPosition: Decimal,
|
|
718
|
+
adjustBorrowPosition: Decimal,
|
|
719
|
+
priceDebtToColl: Decimal,
|
|
720
|
+
flashLoanFee: Decimal,
|
|
721
|
+
slippagePct: Decimal,
|
|
722
|
+
collIsKtoken: boolean
|
|
723
|
+
): Promise<AdjustLeverageCalcsResult> {
|
|
724
|
+
// used if coll is Ktoken and we borrow debt token instead
|
|
725
|
+
const amountToFlashBorrowDebt = adjustDepositPosition
|
|
726
|
+
.div(priceDebtToColl)
|
|
727
|
+
.mul(new Decimal(new Decimal(1).add(slippagePct.div(100))))
|
|
728
|
+
.toDecimalPlaces(debtReserve!.stats.decimals, Decimal.ROUND_UP);
|
|
729
|
+
|
|
730
|
+
const borrowAmount = adjustDepositPosition
|
|
731
|
+
.mul(new Decimal(1).plus(flashLoanFee))
|
|
732
|
+
.mul(new Decimal(new Decimal(1).add(slippagePct.div(100))))
|
|
733
|
+
.div(priceDebtToColl);
|
|
734
|
+
|
|
735
|
+
const expectedDebtTokenAtaBalance = await getExpectedTokenBalanceAfterBorrow(
|
|
736
|
+
market.getConnection(),
|
|
737
|
+
debtReserve.getLiquidityMint(),
|
|
738
|
+
owner,
|
|
739
|
+
collToLamportsDecimal(!collIsKtoken ? borrowAmount : amountToFlashBorrowDebt, debtReserve!.stats.decimals).floor(),
|
|
740
|
+
debtReserve!.state.liquidity.mintDecimals.toNumber()
|
|
741
|
+
);
|
|
742
|
+
|
|
743
|
+
return {
|
|
744
|
+
adjustDepositPosition,
|
|
745
|
+
adjustBorrowPosition,
|
|
746
|
+
amountToFlashBorrowDebt,
|
|
747
|
+
borrowAmount,
|
|
748
|
+
expectedDebtTokenAtaBalance,
|
|
749
|
+
withdrawAmountWithSlippageAndFlashLoanFee: new Decimal(0),
|
|
750
|
+
};
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
export function adjustWithdrawLeverageCalcs(
|
|
754
|
+
adjustDepositPosition: Decimal,
|
|
755
|
+
adjustBorrowPosition: Decimal,
|
|
756
|
+
flashLoanFee: Decimal,
|
|
757
|
+
slippagePct: Decimal
|
|
758
|
+
): AdjustLeverageCalcsResult {
|
|
759
|
+
// used if coll is Ktoken and we borrow debt token instead
|
|
760
|
+
const withdrawAmountWithSlippageAndFlashLoanFee = Decimal.abs(adjustDepositPosition)
|
|
761
|
+
.mul(new Decimal(1).plus(flashLoanFee))
|
|
762
|
+
.mul(new Decimal(1).add(slippagePct.div(100)));
|
|
763
|
+
|
|
764
|
+
return {
|
|
765
|
+
adjustDepositPosition,
|
|
766
|
+
adjustBorrowPosition,
|
|
767
|
+
amountToFlashBorrowDebt: new Decimal(0),
|
|
768
|
+
borrowAmount: new Decimal(0),
|
|
769
|
+
expectedDebtTokenAtaBalance: new Decimal(0),
|
|
770
|
+
withdrawAmountWithSlippageAndFlashLoanFee,
|
|
771
|
+
};
|
|
772
|
+
}
|
package/src/leverage/index.ts
CHANGED