@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
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import { AddressLookupTableAccount, PublicKey, TransactionInstruction } from '@solana/web3.js';
|
|
2
|
+
import Decimal from 'decimal.js';
|
|
3
|
+
import { KaminoMarket, KaminoObligation } from '../classes';
|
|
4
|
+
import { InstructionsWithLookupTables, Kamino, StrategyWithAddress } from '@kamino-finance/kliquidity-sdk';
|
|
5
|
+
import { ObligationType, ObligationTypeTag } from '../utils';
|
|
6
|
+
|
|
7
|
+
export type SwapQuoteProvider<QuoteResponse> = (
|
|
8
|
+
inputs: SwapInputs,
|
|
9
|
+
klendAccounts: Array<PublicKey>
|
|
10
|
+
) => Promise<SwapQuote<QuoteResponse>>;
|
|
11
|
+
|
|
12
|
+
export type SwapQuoteIxsProvider<QuoteResponse> = (
|
|
13
|
+
inputs: SwapInputs,
|
|
14
|
+
klendAccounts: Array<PublicKey>,
|
|
15
|
+
quote: SwapQuote<QuoteResponse>
|
|
16
|
+
) => Promise<SwapQuoteIxs>;
|
|
17
|
+
|
|
18
|
+
export type SwapQuote<QuoteResponse> = {
|
|
19
|
+
priceAInB: Decimal;
|
|
20
|
+
quoteResponse?: QuoteResponse;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export type SwapQuoteIxs = {
|
|
24
|
+
preActionIxs: TransactionInstruction[];
|
|
25
|
+
swapIxs: TransactionInstruction[];
|
|
26
|
+
lookupTables: AddressLookupTableAccount[];
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export type PriceAinBProvider = (mintA: PublicKey, mintB: PublicKey) => Promise<Decimal>;
|
|
30
|
+
|
|
31
|
+
export type IsKtokenProvider = (token: PublicKey | string) => Promise<boolean>;
|
|
32
|
+
|
|
33
|
+
export type SwapInputs = {
|
|
34
|
+
inputAmountLamports: Decimal;
|
|
35
|
+
inputMint: PublicKey;
|
|
36
|
+
outputMint: PublicKey;
|
|
37
|
+
amountDebtAtaBalance: Decimal | undefined;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export type KaminoDepositSwapOverride = (
|
|
41
|
+
kaminoMarket: KaminoMarket,
|
|
42
|
+
kamino: Kamino,
|
|
43
|
+
depositor: PublicKey,
|
|
44
|
+
amountInMint: PublicKey,
|
|
45
|
+
amountOutMint: PublicKey,
|
|
46
|
+
amountIn: Decimal,
|
|
47
|
+
slippageFactor: Decimal,
|
|
48
|
+
amountDebtAtaBalance: Decimal
|
|
49
|
+
) => Promise<InstructionsWithLookupTables>;
|
|
50
|
+
|
|
51
|
+
export type DepsoitLeverageIxsResponse<QuoteResponse> = {
|
|
52
|
+
ixs: TransactionInstruction[];
|
|
53
|
+
lookupTables: AddressLookupTableAccount[];
|
|
54
|
+
swapInputs: SwapInputs;
|
|
55
|
+
initialInputs: DepositLeverageInitialInputs<QuoteResponse>;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export type DepositLeverageInitialInputs<QuoteResponse> = {
|
|
59
|
+
calcs: DepositLeverageCalcsResult;
|
|
60
|
+
swapQuote: SwapQuote<QuoteResponse>;
|
|
61
|
+
currentSlot: number;
|
|
62
|
+
klendAccounts: Array<PublicKey>;
|
|
63
|
+
collIsKtoken: boolean;
|
|
64
|
+
obligation: KaminoObligation | ObligationType | undefined;
|
|
65
|
+
strategy: StrategyWithAddress | undefined;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
export interface DepositWithLeverageSwapInputsProps<QuoteResponse> {
|
|
69
|
+
owner: PublicKey;
|
|
70
|
+
kaminoMarket: KaminoMarket;
|
|
71
|
+
debtTokenMint: PublicKey;
|
|
72
|
+
collTokenMint: PublicKey;
|
|
73
|
+
obligation: KaminoObligation | null;
|
|
74
|
+
obligationTypeTagOverride: ObligationTypeTag;
|
|
75
|
+
referrer: PublicKey;
|
|
76
|
+
currentSlot: number;
|
|
77
|
+
depositAmount: Decimal;
|
|
78
|
+
priceDebtToColl: Decimal;
|
|
79
|
+
slippagePct: Decimal;
|
|
80
|
+
targetLeverage: Decimal;
|
|
81
|
+
selectedTokenMint: PublicKey;
|
|
82
|
+
budgetAndPriorityFeeIxs?: TransactionInstruction[];
|
|
83
|
+
kamino: Kamino | undefined;
|
|
84
|
+
scopeFeed: string | undefined;
|
|
85
|
+
quoteBufferBps: Decimal;
|
|
86
|
+
priceAinB: PriceAinBProvider;
|
|
87
|
+
isKtoken: IsKtokenProvider;
|
|
88
|
+
quoter: SwapQuoteProvider<QuoteResponse>;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export interface DepositWithLeverageProps<QuoteResponse> extends DepositWithLeverageSwapInputsProps<QuoteResponse> {
|
|
92
|
+
swapper: SwapQuoteIxsProvider<QuoteResponse>;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export type DepositLeverageCalcsResult = {
|
|
96
|
+
flashBorrowInCollToken: Decimal;
|
|
97
|
+
initDepositInSol: Decimal;
|
|
98
|
+
debtTokenToBorrow: Decimal;
|
|
99
|
+
collTokenToDeposit: Decimal;
|
|
100
|
+
swapDebtTokenIn: Decimal;
|
|
101
|
+
swapCollTokenExpectedOut: Decimal;
|
|
102
|
+
flashBorrowInDebtTokenKtokenOnly: Decimal;
|
|
103
|
+
singleSidedDepositKtokenOnly: Decimal;
|
|
104
|
+
requiredCollateralKtokenOnly: Decimal;
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
export type WithdrawLeverageIxsResponse<QuoteResponse> = {
|
|
108
|
+
ixs: TransactionInstruction[];
|
|
109
|
+
lookupTables: AddressLookupTableAccount[];
|
|
110
|
+
swapInputs: SwapInputs;
|
|
111
|
+
initialInputs: WithdrawLeverageInitialInputs<QuoteResponse>;
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
export type WithdrawLeverageInitialInputs<QuoteResponse> = {
|
|
115
|
+
calcs: WithdrawLeverageCalcsResult;
|
|
116
|
+
swapQuote: SwapQuote<QuoteResponse>;
|
|
117
|
+
currentSlot: number;
|
|
118
|
+
klendAccounts: Array<PublicKey>;
|
|
119
|
+
collIsKtoken: boolean;
|
|
120
|
+
obligation: KaminoObligation | ObligationType | undefined;
|
|
121
|
+
strategy: StrategyWithAddress | undefined;
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
export interface WithdrawWithLeverageSwapInputsProps<QuoteResponse> {
|
|
125
|
+
owner: PublicKey;
|
|
126
|
+
kaminoMarket: KaminoMarket;
|
|
127
|
+
debtTokenMint: PublicKey;
|
|
128
|
+
collTokenMint: PublicKey;
|
|
129
|
+
obligation: KaminoObligation;
|
|
130
|
+
deposited: Decimal;
|
|
131
|
+
borrowed: Decimal;
|
|
132
|
+
referrer: PublicKey;
|
|
133
|
+
currentSlot: number;
|
|
134
|
+
withdrawAmount: Decimal;
|
|
135
|
+
priceCollToDebt: Decimal;
|
|
136
|
+
slippagePct: Decimal;
|
|
137
|
+
isClosingPosition: boolean;
|
|
138
|
+
selectedTokenMint: PublicKey;
|
|
139
|
+
budgetAndPriorityFeeIxs?: TransactionInstruction[];
|
|
140
|
+
kamino: Kamino | undefined;
|
|
141
|
+
scopeFeed: string | undefined;
|
|
142
|
+
quoteBufferBps: Decimal;
|
|
143
|
+
isKtoken: IsKtokenProvider;
|
|
144
|
+
quoter: SwapQuoteProvider<QuoteResponse>;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export interface WithdrawWithLeverageProps<QuoteResponse> extends WithdrawWithLeverageSwapInputsProps<QuoteResponse> {
|
|
148
|
+
swapper: SwapQuoteIxsProvider<QuoteResponse>;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export type WithdrawLeverageCalcsResult = {
|
|
152
|
+
withdrawAmount: Decimal;
|
|
153
|
+
repayAmount: Decimal;
|
|
154
|
+
collTokenSwapIn: Decimal;
|
|
155
|
+
depositTokenWithdrawAmount: Decimal;
|
|
156
|
+
debtTokenExpectedSwapOut: Decimal;
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
export type AdjustLeverageIxsResponse<QuoteResponse> = {
|
|
160
|
+
ixs: TransactionInstruction[];
|
|
161
|
+
lookupTables: AddressLookupTableAccount[];
|
|
162
|
+
swapInputs: SwapInputs;
|
|
163
|
+
initialInputs: AdjustLeverageInitialInputs<QuoteResponse>;
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
export type AdjustLeverageInitialInputs<QuoteResponse> = {
|
|
167
|
+
calcs: AdjustLeverageCalcsResult;
|
|
168
|
+
swapQuote: SwapQuote<QuoteResponse>;
|
|
169
|
+
currentSlot: number;
|
|
170
|
+
klendAccounts: Array<PublicKey>;
|
|
171
|
+
isDeposit: boolean;
|
|
172
|
+
collIsKtoken: boolean;
|
|
173
|
+
obligation: KaminoObligation | ObligationType | undefined;
|
|
174
|
+
strategy: StrategyWithAddress | undefined;
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
export interface AdjustLeverageSwapInputsProps<QuoteResponse> {
|
|
178
|
+
owner: PublicKey;
|
|
179
|
+
kaminoMarket: KaminoMarket;
|
|
180
|
+
debtTokenMint: PublicKey;
|
|
181
|
+
collTokenMint: PublicKey;
|
|
182
|
+
obligation: KaminoObligation;
|
|
183
|
+
depositedLamports: Decimal;
|
|
184
|
+
borrowedLamports: Decimal;
|
|
185
|
+
referrer: PublicKey;
|
|
186
|
+
currentSlot: number;
|
|
187
|
+
targetLeverage: Decimal;
|
|
188
|
+
priceCollToDebt: Decimal;
|
|
189
|
+
priceDebtToColl: Decimal;
|
|
190
|
+
slippagePct: Decimal;
|
|
191
|
+
budgetAndPriorityFeeIxs?: TransactionInstruction[];
|
|
192
|
+
kamino: Kamino | undefined;
|
|
193
|
+
scopeFeed: string | undefined;
|
|
194
|
+
quoteBufferBps: Decimal;
|
|
195
|
+
priceAinB: PriceAinBProvider;
|
|
196
|
+
isKtoken: IsKtokenProvider;
|
|
197
|
+
quoter: SwapQuoteProvider<QuoteResponse>;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export interface AdjustLeverageProps<QuoteResponse> extends AdjustLeverageSwapInputsProps<QuoteResponse> {
|
|
201
|
+
swapper: SwapQuoteIxsProvider<QuoteResponse>;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export type AdjustLeverageCalcsResult = {
|
|
205
|
+
adjustDepositPosition: Decimal;
|
|
206
|
+
adjustBorrowPosition: Decimal;
|
|
207
|
+
amountToFlashBorrowDebt: Decimal;
|
|
208
|
+
borrowAmount: Decimal;
|
|
209
|
+
expectedDebtTokenAtaBalance: Decimal;
|
|
210
|
+
withdrawAmountWithSlippageAndFlashLoanFee: Decimal;
|
|
211
|
+
};
|
package/src/leverage/utils.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { Kamino, StrategyWithAddress } from '@kamino-finance/kliquidity-sdk';
|
|
2
2
|
import { KaminoMarket, KaminoReserve, lamportsToNumberDecimal } from '../classes';
|
|
3
3
|
import { Connection, PublicKey, TransactionInstruction } from '@solana/web3.js';
|
|
4
|
-
import { PriceAinBProvider, SwapIxnsProvider } from './operations';
|
|
5
4
|
import Decimal from 'decimal.js';
|
|
6
|
-
import { getTokenAccountBalanceDecimal } from '../utils';
|
|
5
|
+
import { getLookupTableAccounts, getTokenAccountBalanceDecimal } from '../utils';
|
|
7
6
|
import { numberToLamportsDecimal } from '../classes/utils';
|
|
8
7
|
import BN from 'bn.js';
|
|
8
|
+
import { PriceAinBProvider, SwapInputs, SwapQuote, SwapQuoteIxs, SwapQuoteIxsProvider } from './types';
|
|
9
9
|
|
|
10
10
|
export interface KaminoSwapperIxBuilder {
|
|
11
11
|
(
|
|
@@ -25,48 +25,55 @@ export interface DepositAmountsForSwap {
|
|
|
25
25
|
tokenBToSwapAmount: Decimal;
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
export
|
|
29
|
-
connection: Connection,
|
|
28
|
+
export async function getTokenToKtokenSwapper<QuoteResponse>(
|
|
30
29
|
kaminoMarket: KaminoMarket,
|
|
31
30
|
kamino: Kamino,
|
|
32
31
|
depositor: PublicKey,
|
|
33
|
-
|
|
32
|
+
slippagePct: Decimal,
|
|
33
|
+
swapper: SwapQuoteIxsProvider<QuoteResponse>,
|
|
34
34
|
priceAinB: PriceAinBProvider,
|
|
35
35
|
includeAtaIxns: boolean = true
|
|
36
|
-
): Promise<
|
|
36
|
+
): Promise<SwapQuoteIxsProvider<QuoteResponse>> {
|
|
37
37
|
return async (
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
console.debug('Depositing token', amountInMint.toString(), ' for ', amountOutMint.toString(), 'ktoken');
|
|
48
|
-
if (amountDebtAtaBalance === undefined) {
|
|
38
|
+
inputs: SwapInputs,
|
|
39
|
+
klendAccounts: Array<PublicKey>,
|
|
40
|
+
quote: SwapQuote<QuoteResponse>
|
|
41
|
+
): Promise<SwapQuoteIxs> => {
|
|
42
|
+
const slippageBps = new Decimal(slippagePct).mul('100');
|
|
43
|
+
const mintInDecimals = kaminoMarket.getReserveByMint(inputs.inputMint)!.state.liquidity.mintDecimals.toNumber();
|
|
44
|
+
const amountIn = lamportsToNumberDecimal(inputs.inputAmountLamports, mintInDecimals);
|
|
45
|
+
console.debug('Depositing token', inputs.inputMint.toString(), ' for ', inputs.outputMint.toString(), 'ktoken');
|
|
46
|
+
if (inputs.amountDebtAtaBalance === undefined) {
|
|
49
47
|
throw Error('Amount in debt ATA balance is undefined for leverage ktoken deposit');
|
|
50
48
|
}
|
|
51
49
|
|
|
52
50
|
const ixWithLookup = (await getKtokenDepositIxs(
|
|
53
|
-
|
|
51
|
+
kaminoMarket.getConnection(),
|
|
54
52
|
kamino,
|
|
55
53
|
depositor,
|
|
56
|
-
|
|
57
|
-
|
|
54
|
+
inputs.inputMint,
|
|
55
|
+
inputs.outputMint,
|
|
58
56
|
amountIn,
|
|
59
57
|
slippageBps,
|
|
60
|
-
amountDebtAtaBalance,
|
|
58
|
+
inputs.amountDebtAtaBalance,
|
|
61
59
|
swapper,
|
|
62
60
|
priceAinB,
|
|
63
|
-
includeAtaIxns
|
|
61
|
+
includeAtaIxns,
|
|
62
|
+
klendAccounts,
|
|
63
|
+
quote
|
|
64
64
|
))!;
|
|
65
|
-
|
|
65
|
+
|
|
66
|
+
const luts = await getLookupTableAccounts(kaminoMarket.getConnection(), ixWithLookup.lookupTablesAddresses);
|
|
67
|
+
|
|
68
|
+
return {
|
|
69
|
+
preActionIxs: [],
|
|
70
|
+
swapIxs: ixWithLookup.instructions,
|
|
71
|
+
lookupTables: luts,
|
|
72
|
+
};
|
|
66
73
|
};
|
|
67
|
-
}
|
|
74
|
+
}
|
|
68
75
|
|
|
69
|
-
export async function getKtokenDepositIxs(
|
|
76
|
+
export async function getKtokenDepositIxs<QuoteResponse>(
|
|
70
77
|
connection: Connection,
|
|
71
78
|
kamino: Kamino,
|
|
72
79
|
depositor: PublicKey,
|
|
@@ -75,9 +82,11 @@ export async function getKtokenDepositIxs(
|
|
|
75
82
|
amountToDeposit: Decimal,
|
|
76
83
|
slippageBps: Decimal,
|
|
77
84
|
amountExpectedDepositAtaBalance: Decimal,
|
|
78
|
-
swapper:
|
|
85
|
+
swapper: SwapQuoteIxsProvider<QuoteResponse>,
|
|
79
86
|
priceAinB: PriceAinBProvider,
|
|
80
|
-
includeAtaIxns: boolean = true
|
|
87
|
+
includeAtaIxns: boolean = true,
|
|
88
|
+
klendAccounts: Array<PublicKey>,
|
|
89
|
+
quote: SwapQuote<QuoteResponse>
|
|
81
90
|
) {
|
|
82
91
|
const kaminoStrategy = await kamino.getStrategyByKTokenMint(ktokenMint);
|
|
83
92
|
const tokenAMint = kaminoStrategy?.strategy.tokenAMint!;
|
|
@@ -94,7 +103,7 @@ export async function getKtokenDepositIxs(
|
|
|
94
103
|
depositor,
|
|
95
104
|
slippageBps,
|
|
96
105
|
undefined,
|
|
97
|
-
swapProviderToKaminoSwapProvider(swapper),
|
|
106
|
+
swapProviderToKaminoSwapProvider(swapper, klendAccounts, quote),
|
|
98
107
|
tokensBalances,
|
|
99
108
|
priceAinBDecimal,
|
|
100
109
|
includeAtaIxns
|
|
@@ -108,7 +117,7 @@ export async function getKtokenDepositIxs(
|
|
|
108
117
|
depositor,
|
|
109
118
|
slippageBps,
|
|
110
119
|
undefined,
|
|
111
|
-
swapProviderToKaminoSwapProvider(swapper),
|
|
120
|
+
swapProviderToKaminoSwapProvider(swapper, klendAccounts, quote),
|
|
112
121
|
tokensBalances,
|
|
113
122
|
priceAinBDecimal,
|
|
114
123
|
includeAtaIxns
|
|
@@ -118,18 +127,18 @@ export async function getKtokenDepositIxs(
|
|
|
118
127
|
}
|
|
119
128
|
}
|
|
120
129
|
|
|
121
|
-
export
|
|
130
|
+
export async function getKtokenToTokenSwapper<QuoteResponse>(
|
|
122
131
|
kaminoMarket: KaminoMarket,
|
|
123
132
|
kamino: Kamino,
|
|
124
133
|
depositor: PublicKey,
|
|
125
|
-
swapper:
|
|
126
|
-
): Promise<
|
|
127
|
-
return async (
|
|
128
|
-
const amountInDecimals = kaminoMarket.getReserveByMint(
|
|
129
|
-
const amountToWithdraw = lamportsToNumberDecimal(
|
|
130
|
-
const kaminoStrategy = await kamino.getStrategyByKTokenMint(
|
|
134
|
+
swapper: SwapQuoteIxsProvider<QuoteResponse>
|
|
135
|
+
): Promise<SwapQuoteIxsProvider<QuoteResponse>> {
|
|
136
|
+
return async (inputs: SwapInputs, klendAccounts: Array<PublicKey>, quote: SwapQuote<QuoteResponse>) => {
|
|
137
|
+
const amountInDecimals = kaminoMarket.getReserveByMint(inputs.inputMint)!.state.liquidity.mintDecimals.toNumber();
|
|
138
|
+
const amountToWithdraw = lamportsToNumberDecimal(inputs.inputAmountLamports, amountInDecimals);
|
|
139
|
+
const kaminoStrategy = await kamino.getStrategyByKTokenMint(inputs.inputMint);
|
|
131
140
|
|
|
132
|
-
console.log('Withdrawing ktoken',
|
|
141
|
+
console.log('Withdrawing ktoken', inputs.inputMint.toString(), ' for ', inputs.outputMint.toString(), 'token');
|
|
133
142
|
|
|
134
143
|
const ixWithdraw = (await getKtokenWithdrawIxs(kamino, depositor, kaminoStrategy!, amountToWithdraw))!;
|
|
135
144
|
|
|
@@ -139,29 +148,45 @@ export const getKtokenToTokenSwapper = async (
|
|
|
139
148
|
amountToWithdraw
|
|
140
149
|
);
|
|
141
150
|
|
|
142
|
-
if (
|
|
143
|
-
const
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
151
|
+
if (inputs.outputMint.equals(kaminoStrategy!.strategy.tokenAMint!)) {
|
|
152
|
+
const { swapIxs, lookupTables } = await swapper(
|
|
153
|
+
{
|
|
154
|
+
inputAmountLamports: estimatedBOut,
|
|
155
|
+
inputMint: kaminoStrategy!.strategy.tokenBMint!,
|
|
156
|
+
outputMint: kaminoStrategy!.strategy.tokenAMint!,
|
|
157
|
+
amountDebtAtaBalance: new Decimal(0),
|
|
158
|
+
},
|
|
159
|
+
klendAccounts,
|
|
160
|
+
quote
|
|
148
161
|
);
|
|
149
162
|
|
|
150
|
-
return
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
163
|
+
return {
|
|
164
|
+
preActionIxs: [],
|
|
165
|
+
swapIxs: [...ixWithdraw.prerequisiteIxs, ixWithdraw.withdrawIx, ...swapIxs],
|
|
166
|
+
lookupTables,
|
|
167
|
+
};
|
|
168
|
+
} else if (inputs.outputMint.equals(kaminoStrategy!.strategy.tokenBMint!)) {
|
|
169
|
+
const { swapIxs, lookupTables } = await swapper(
|
|
170
|
+
{
|
|
171
|
+
inputAmountLamports: estimatedAOut,
|
|
172
|
+
inputMint: kaminoStrategy!.strategy.tokenAMint!,
|
|
173
|
+
outputMint: kaminoStrategy!.strategy.tokenBMint!,
|
|
174
|
+
amountDebtAtaBalance: new Decimal(0),
|
|
175
|
+
},
|
|
176
|
+
klendAccounts,
|
|
177
|
+
quote
|
|
157
178
|
);
|
|
158
179
|
|
|
159
|
-
return
|
|
180
|
+
return {
|
|
181
|
+
preActionIxs: [],
|
|
182
|
+
swapIxs: [...ixWithdraw.prerequisiteIxs, ixWithdraw.withdrawIx, ...swapIxs],
|
|
183
|
+
lookupTables,
|
|
184
|
+
};
|
|
160
185
|
} else {
|
|
161
186
|
throw Error('Deposit token is neither A nor B in the strategy');
|
|
162
187
|
}
|
|
163
188
|
};
|
|
164
|
-
}
|
|
189
|
+
}
|
|
165
190
|
|
|
166
191
|
export async function getKtokenWithdrawIxs(
|
|
167
192
|
kamino: Kamino,
|
|
@@ -225,29 +250,43 @@ export async function getKtokenWithdrawEstimatesAndPrice(
|
|
|
225
250
|
return [estimatedAOutDecimal, estimatedBOutDecimal];
|
|
226
251
|
}
|
|
227
252
|
|
|
228
|
-
export function swapProviderToKaminoSwapProvider(
|
|
253
|
+
export function swapProviderToKaminoSwapProvider<QuoteResponse>(
|
|
254
|
+
swapper: SwapQuoteIxsProvider<QuoteResponse>,
|
|
255
|
+
klendAccounts: Array<PublicKey>,
|
|
256
|
+
swapQuote: SwapQuote<QuoteResponse>
|
|
257
|
+
): KaminoSwapperIxBuilder {
|
|
229
258
|
return async (
|
|
230
259
|
input: DepositAmountsForSwap,
|
|
231
260
|
tokenAMint: PublicKey,
|
|
232
261
|
tokenBMint: PublicKey,
|
|
233
262
|
_owner: PublicKey,
|
|
234
|
-
|
|
263
|
+
_slippage: Decimal,
|
|
235
264
|
_allKeys: PublicKey[]
|
|
236
265
|
): Promise<[TransactionInstruction[], PublicKey[]]> => {
|
|
237
266
|
if (input.tokenBToSwapAmount.lt(0)) {
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
267
|
+
const swapperIxs = await swapper(
|
|
268
|
+
{
|
|
269
|
+
inputAmountLamports: input.tokenBToSwapAmount.abs(),
|
|
270
|
+
inputMint: tokenBMint,
|
|
271
|
+
outputMint: tokenAMint,
|
|
272
|
+
amountDebtAtaBalance: undefined,
|
|
273
|
+
},
|
|
274
|
+
klendAccounts,
|
|
275
|
+
swapQuote
|
|
243
276
|
);
|
|
277
|
+
return [swapperIxs.swapIxs, swapperIxs.lookupTables.map((lt) => lt.key)];
|
|
244
278
|
} else if (input.tokenAToSwapAmount.lt(0)) {
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
279
|
+
const swapperIxs = await swapper(
|
|
280
|
+
{
|
|
281
|
+
inputAmountLamports: input.tokenAToSwapAmount.abs(),
|
|
282
|
+
inputMint: tokenAMint,
|
|
283
|
+
outputMint: tokenBMint,
|
|
284
|
+
amountDebtAtaBalance: undefined,
|
|
285
|
+
},
|
|
286
|
+
klendAccounts,
|
|
287
|
+
swapQuote
|
|
250
288
|
);
|
|
289
|
+
return [swapperIxs.swapIxs, swapperIxs.lookupTables.map((lt) => lt.key)];
|
|
251
290
|
} else {
|
|
252
291
|
throw Error('Nothing to swap');
|
|
253
292
|
}
|
package/src/utils/constants.ts
CHANGED
package/src/utils/index.ts
CHANGED
package/src/utils/rpc.ts
CHANGED
|
@@ -10,6 +10,7 @@ import {
|
|
|
10
10
|
import { Buffer } from 'buffer';
|
|
11
11
|
import axios from 'axios';
|
|
12
12
|
import { init, decompress } from '@bokuweb/zstd-wasm';
|
|
13
|
+
import { randomUUID } from 'crypto';
|
|
13
14
|
|
|
14
15
|
(async () => {
|
|
15
16
|
await init();
|
|
@@ -45,7 +46,7 @@ export async function getProgramAccounts(
|
|
|
45
46
|
...configWithoutEncoding,
|
|
46
47
|
},
|
|
47
48
|
],
|
|
48
|
-
id:
|
|
49
|
+
id: randomUUID(),
|
|
49
50
|
},
|
|
50
51
|
{
|
|
51
52
|
headers: {
|