@kamino-finance/klend-sdk 5.13.0 → 5.13.2
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/action.d.ts +74 -49
- package/dist/classes/action.d.ts.map +1 -1
- package/dist/classes/action.js +167 -115
- package/dist/classes/action.js.map +1 -1
- package/dist/classes/fraction.d.ts +2 -0
- package/dist/classes/fraction.d.ts.map +1 -1
- package/dist/classes/fraction.js +6 -0
- package/dist/classes/fraction.js.map +1 -1
- package/dist/classes/index.d.ts +3 -2
- package/dist/classes/index.d.ts.map +1 -1
- package/dist/classes/index.js +3 -2
- package/dist/classes/index.js.map +1 -1
- package/dist/classes/manager.d.ts +7 -7
- package/dist/classes/manager.d.ts.map +1 -1
- package/dist/classes/manager.js +46 -46
- package/dist/classes/manager.js.map +1 -1
- package/dist/classes/obligation.d.ts.map +1 -1
- package/dist/classes/obligation.js +1 -6
- package/dist/classes/obligation.js.map +1 -1
- package/dist/classes/obligationOrder.d.ts +1 -1
- package/dist/classes/obligationOrder.d.ts.map +1 -1
- package/dist/classes/reserve.d.ts +2 -2
- package/dist/classes/reserve.d.ts.map +1 -1
- package/dist/classes/reserve.js +71 -74
- package/dist/classes/reserve.js.map +1 -1
- package/dist/classes/shared.d.ts +13 -0
- package/dist/classes/shared.d.ts.map +1 -1
- package/dist/classes/utils.d.ts +1 -9
- package/dist/classes/utils.d.ts.map +1 -1
- package/dist/classes/utils.js +4 -51
- package/dist/classes/utils.js.map +1 -1
- package/dist/classes/vault.d.ts +5 -3
- package/dist/classes/vault.d.ts.map +1 -1
- package/dist/classes/vault.js +53 -41
- package/dist/classes/vault.js.map +1 -1
- package/dist/classes/{types.d.ts → vault_types.d.ts} +2 -15
- package/dist/classes/vault_types.d.ts.map +1 -0
- package/dist/classes/{types.js → vault_types.js} +1 -1
- package/dist/classes/vault_types.js.map +1 -0
- package/dist/client_kamino_manager.d.ts.map +1 -1
- package/dist/client_kamino_manager.js +25 -26
- package/dist/client_kamino_manager.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 +8 -8
- package/dist/lending_operations/repay_with_collateral_operations.js.map +1 -1
- package/dist/lending_operations/swap_collateral_operations.d.ts +6 -6
- package/dist/lending_operations/swap_collateral_operations.d.ts.map +1 -1
- package/dist/lending_operations/swap_collateral_operations.js +63 -65
- package/dist/lending_operations/swap_collateral_operations.js.map +1 -1
- package/dist/leverage/calcs.d.ts +0 -1
- package/dist/leverage/calcs.d.ts.map +1 -1
- package/dist/leverage/calcs.js +5 -8
- package/dist/leverage/calcs.js.map +1 -1
- package/dist/leverage/operations.d.ts +5 -5
- package/dist/leverage/operations.d.ts.map +1 -1
- package/dist/leverage/operations.js +80 -87
- package/dist/leverage/operations.js.map +1 -1
- package/dist/leverage/types.d.ts +1 -1
- package/dist/leverage/types.d.ts.map +1 -1
- package/dist/leverage/utils.d.ts +2 -2
- package/dist/leverage/utils.d.ts.map +1 -1
- package/dist/leverage/utils.js +5 -5
- package/dist/leverage/utils.js.map +1 -1
- package/dist/referrals/instructions.d.ts +3 -3
- package/dist/referrals/instructions.d.ts.map +1 -1
- package/dist/referrals/instructions.js +10 -10
- package/dist/referrals/instructions.js.map +1 -1
- package/dist/referrals/operations.js +6 -6
- package/dist/referrals/operations.js.map +1 -1
- package/dist/utils/ata.d.ts +3 -3
- package/dist/utils/ata.d.ts.map +1 -1
- package/dist/utils/ata.js +22 -22
- package/dist/utils/ata.js.map +1 -1
- package/dist/utils/instruction.d.ts +2 -2
- package/dist/utils/instruction.d.ts.map +1 -1
- package/dist/utils/instruction.js +12 -12
- package/dist/utils/instruction.js.map +1 -1
- package/dist/utils/lookupTable.js +6 -6
- package/dist/utils/lookupTable.js.map +1 -1
- package/dist/utils/userMetadata.d.ts +1 -1
- package/dist/utils/userMetadata.d.ts.map +1 -1
- package/dist/utils/userMetadata.js +3 -3
- package/dist/utils/userMetadata.js.map +1 -1
- package/package.json +1 -1
- package/src/classes/action.ts +198 -150
- package/src/classes/fraction.ts +7 -0
- package/src/classes/index.ts +5 -2
- package/src/classes/manager.ts +54 -51
- package/src/classes/obligation.ts +2 -7
- package/src/classes/obligationOrder.ts +1 -1
- package/src/classes/reserve.ts +128 -137
- package/src/classes/shared.ts +15 -0
- package/src/classes/utils.ts +3 -52
- package/src/classes/vault.ts +56 -42
- package/src/classes/{types.ts → vault_types.ts} +1 -16
- package/src/client.ts +16 -16
- package/src/client_kamino_manager.ts +25 -25
- package/src/lending_operations/repay_with_collateral_operations.ts +13 -15
- package/src/lending_operations/swap_collateral_operations.ts +87 -89
- package/src/leverage/calcs.ts +2 -6
- package/src/leverage/operations.ts +91 -97
- package/src/leverage/types.ts +1 -1
- package/src/leverage/utils.ts +5 -5
- package/src/referrals/instructions.ts +6 -6
- package/src/referrals/operations.ts +9 -9
- package/src/utils/ata.ts +18 -18
- package/src/utils/instruction.ts +10 -10
- package/src/utils/lookupTable.ts +6 -6
- package/src/utils/userMetadata.ts +1 -1
- package/dist/classes/types.d.ts.map +0 -1
- package/dist/classes/types.js.map +0 -1
- package/dist/classes/types_utils.d.ts +0 -14
- package/dist/classes/types_utils.d.ts.map +0 -1
- package/dist/classes/types_utils.js +0 -50
- package/dist/classes/types_utils.js.map +0 -1
- package/src/classes/types_utils.ts +0 -49
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
lamportsToNumberDecimal as fromLamports,
|
|
9
9
|
getTokenIdsForScopeRefresh,
|
|
10
10
|
isKaminoObligation,
|
|
11
|
+
toJson,
|
|
11
12
|
} from '../classes';
|
|
12
13
|
import { getFlashLoanInstructions } from './instructions';
|
|
13
14
|
|
|
@@ -23,10 +24,10 @@ import {
|
|
|
23
24
|
U64_MAX,
|
|
24
25
|
createAtasIdempotent,
|
|
25
26
|
getAssociatedTokenAddress,
|
|
26
|
-
|
|
27
|
-
|
|
27
|
+
getComputeBudgetAndPriorityFeeIxs,
|
|
28
|
+
getTransferWsolIxs,
|
|
28
29
|
getLookupTableAccount,
|
|
29
|
-
|
|
30
|
+
removeBudgetAndAtaIxs,
|
|
30
31
|
uniqueAccountsWithProgramIds,
|
|
31
32
|
} from '../utils';
|
|
32
33
|
import {
|
|
@@ -35,7 +36,6 @@ import {
|
|
|
35
36
|
calcAdjustAmounts,
|
|
36
37
|
depositLeverageCalcs,
|
|
37
38
|
depositLeverageKtokenCalcs,
|
|
38
|
-
toJson,
|
|
39
39
|
withdrawLeverageCalcs,
|
|
40
40
|
} from './calcs';
|
|
41
41
|
import {
|
|
@@ -67,7 +67,7 @@ import {
|
|
|
67
67
|
WithdrawLeverageIxsResponse,
|
|
68
68
|
WithdrawWithLeverageProps,
|
|
69
69
|
WithdrawWithLeverageSwapInputsProps,
|
|
70
|
-
|
|
70
|
+
LeverageIxsOutput,
|
|
71
71
|
FlashLoanInfo,
|
|
72
72
|
} from './types';
|
|
73
73
|
|
|
@@ -140,7 +140,7 @@ export async function getDepositWithLeverageSwapInputs<QuoteResponse>({
|
|
|
140
140
|
}
|
|
141
141
|
|
|
142
142
|
// Build the repay & withdraw collateral tx to get the number of accounts
|
|
143
|
-
const klendIxs:
|
|
143
|
+
const klendIxs: LeverageIxsOutput = await buildDepositWithLeverageIxs(
|
|
144
144
|
kaminoMarket,
|
|
145
145
|
debtReserve,
|
|
146
146
|
collReserve,
|
|
@@ -302,7 +302,7 @@ async function getDepositWithLeverageCalcs(
|
|
|
302
302
|
return calcs;
|
|
303
303
|
}
|
|
304
304
|
|
|
305
|
-
export async function
|
|
305
|
+
export async function getDepositWithLeverageIxs<QuoteResponse>({
|
|
306
306
|
owner,
|
|
307
307
|
kaminoMarket,
|
|
308
308
|
debtTokenMint,
|
|
@@ -385,7 +385,7 @@ export async function getDepositWithLeverageIxns<QuoteResponse>({
|
|
|
385
385
|
const solTokenReserve = kaminoMarket.getReserveByMint(NATIVE_MINT);
|
|
386
386
|
const depositTokenIsSol = !solTokenReserve ? false : selectedTokenMint.equals(solTokenReserve!.getLiquidityMint());
|
|
387
387
|
|
|
388
|
-
const ixs:
|
|
388
|
+
const ixs: LeverageIxsOutput = await buildDepositWithLeverageIxs(
|
|
389
389
|
kaminoMarket,
|
|
390
390
|
debtReserve!,
|
|
391
391
|
collReserve!,
|
|
@@ -417,7 +417,7 @@ export async function getDepositWithLeverageIxns<QuoteResponse>({
|
|
|
417
417
|
};
|
|
418
418
|
}
|
|
419
419
|
|
|
420
|
-
async function
|
|
420
|
+
async function buildDepositWithLeverageIxs(
|
|
421
421
|
market: KaminoMarket,
|
|
422
422
|
debtReserve: KaminoReserve,
|
|
423
423
|
collReserve: KaminoReserve,
|
|
@@ -434,8 +434,8 @@ async function buildDepositWithLeverageIxns(
|
|
|
434
434
|
collIsKtoken: boolean,
|
|
435
435
|
useV2Ixs: boolean,
|
|
436
436
|
elevationGroupOverride?: number
|
|
437
|
-
): Promise<
|
|
438
|
-
const
|
|
437
|
+
): Promise<LeverageIxsOutput> {
|
|
438
|
+
const budgetIxs = budgetAndPriorityFeeIxs || getComputeBudgetAndPriorityFeeIxs(3000000);
|
|
439
439
|
const collTokenMint = collReserve.getLiquidityMint();
|
|
440
440
|
const debtTokenMint = debtReserve.getLiquidityMint();
|
|
441
441
|
const collTokenAta = getAssociatedTokenAddressSync(
|
|
@@ -451,7 +451,7 @@ async function buildDepositWithLeverageIxns(
|
|
|
451
451
|
debtReserve.getLiquidityTokenProgram()
|
|
452
452
|
);
|
|
453
453
|
|
|
454
|
-
// 1. Create atas & budget
|
|
454
|
+
// 1. Create atas & budget ixs
|
|
455
455
|
let mintsToCreateAtas: Array<{ mint: PublicKey; tokenProgram: PublicKey }>;
|
|
456
456
|
if (collIsKtoken) {
|
|
457
457
|
const secondTokenAta = strategy!.strategy.tokenAMint.equals(debtTokenMint)
|
|
@@ -499,12 +499,12 @@ async function buildDepositWithLeverageIxns(
|
|
|
499
499
|
];
|
|
500
500
|
}
|
|
501
501
|
|
|
502
|
-
const
|
|
502
|
+
const atasAndCreateIxs = createAtasIdempotent(owner, mintsToCreateAtas);
|
|
503
503
|
|
|
504
|
-
const
|
|
504
|
+
const fillWsolAtaIxs: TransactionInstruction[] = [];
|
|
505
505
|
if (depositTokenIsSol) {
|
|
506
|
-
|
|
507
|
-
...
|
|
506
|
+
fillWsolAtaIxs.push(
|
|
507
|
+
...getTransferWsolIxs(
|
|
508
508
|
owner,
|
|
509
509
|
getAssociatedTokenAddressSync(NATIVE_MINT, owner),
|
|
510
510
|
toLamports(calcs.initDepositInSol, SOL_DECIMALS).ceil()
|
|
@@ -518,7 +518,7 @@ async function buildDepositWithLeverageIxns(
|
|
|
518
518
|
// if user deposits coll, then we borrow the diff, else we borrow the entire amount
|
|
519
519
|
const { flashBorrowIxn, flashRepayIxn } = getFlashLoanInstructions({
|
|
520
520
|
borrowIxnIndex:
|
|
521
|
-
|
|
521
|
+
budgetIxs.length + atasAndCreateIxs.length + fillWsolAtaIxs.length + (scopeRefreshIxn.length > 0 ? 1 : 0),
|
|
522
522
|
walletPublicKey: owner,
|
|
523
523
|
lendingMarketAuthority: market.getLendingMarketAuthority(),
|
|
524
524
|
lendingMarketAddress: market.getAddress(),
|
|
@@ -552,15 +552,14 @@ async function buildDepositWithLeverageIxns(
|
|
|
552
552
|
0,
|
|
553
553
|
false,
|
|
554
554
|
elevationGroupOverride === 0 ? false : true, // emode
|
|
555
|
-
|
|
556
|
-
false, // to be checked and created in a setup tx in the UI
|
|
555
|
+
{ skipInitialization: true, skipLutCreation: true }, // to be checked and created in a setup tx in the UI
|
|
557
556
|
referrer,
|
|
558
557
|
currentSlot
|
|
559
558
|
);
|
|
560
559
|
|
|
561
560
|
// 4. Swap
|
|
562
561
|
const { swapIxs } = swapQuoteIxs;
|
|
563
|
-
const swapInstructions =
|
|
562
|
+
const swapInstructions = removeBudgetAndAtaIxs(swapIxs, []);
|
|
564
563
|
const flashBorrowReserve = !collIsKtoken ? collReserve : debtReserve;
|
|
565
564
|
const flashLoanInfo = {
|
|
566
565
|
flashBorrowReserve: flashBorrowReserve.address,
|
|
@@ -572,9 +571,9 @@ async function buildDepositWithLeverageIxns(
|
|
|
572
571
|
flashLoanInfo,
|
|
573
572
|
instructions: [
|
|
574
573
|
...scopeRefreshIxn,
|
|
575
|
-
...
|
|
576
|
-
...
|
|
577
|
-
...
|
|
574
|
+
...budgetIxs,
|
|
575
|
+
...atasAndCreateIxs.map((x) => x.createAtaIx),
|
|
576
|
+
...fillWsolAtaIxs,
|
|
578
577
|
...[flashBorrowIxn],
|
|
579
578
|
...KaminoAction.actionToIxs(kaminoDepositAndBorrowAction),
|
|
580
579
|
...swapInstructions,
|
|
@@ -586,9 +585,9 @@ async function buildDepositWithLeverageIxns(
|
|
|
586
585
|
flashLoanInfo,
|
|
587
586
|
instructions: [
|
|
588
587
|
...scopeRefreshIxn,
|
|
589
|
-
...
|
|
590
|
-
...
|
|
591
|
-
...
|
|
588
|
+
...budgetIxs,
|
|
589
|
+
...atasAndCreateIxs.map((x) => x.createAtaIx),
|
|
590
|
+
...fillWsolAtaIxs,
|
|
592
591
|
...[flashBorrowIxn],
|
|
593
592
|
...swapInstructions,
|
|
594
593
|
...kaminoDepositAndBorrowAction.setupIxs,
|
|
@@ -654,7 +653,7 @@ export async function getWithdrawWithLeverageSwapInputs<QuoteResponse>({
|
|
|
654
653
|
slippagePct
|
|
655
654
|
);
|
|
656
655
|
|
|
657
|
-
const klendIxs = await
|
|
656
|
+
const klendIxs = await buildWithdrawWithLeverageIxs(
|
|
658
657
|
kaminoMarket,
|
|
659
658
|
debtReserve!,
|
|
660
659
|
collReserve!,
|
|
@@ -736,7 +735,7 @@ export async function getWithdrawWithLeverageSwapInputs<QuoteResponse>({
|
|
|
736
735
|
};
|
|
737
736
|
}
|
|
738
737
|
|
|
739
|
-
export async function
|
|
738
|
+
export async function getWithdrawWithLeverageIxs<QuoteResponse>({
|
|
740
739
|
owner,
|
|
741
740
|
kaminoMarket,
|
|
742
741
|
debtTokenMint,
|
|
@@ -818,7 +817,7 @@ export async function getWithdrawWithLeverageIxns<QuoteResponse>({
|
|
|
818
817
|
}
|
|
819
818
|
}
|
|
820
819
|
|
|
821
|
-
const ixs:
|
|
820
|
+
const ixs: LeverageIxsOutput = await buildWithdrawWithLeverageIxs(
|
|
822
821
|
kaminoMarket,
|
|
823
822
|
debtReserve!,
|
|
824
823
|
collReserve!,
|
|
@@ -841,7 +840,7 @@ export async function getWithdrawWithLeverageIxns<QuoteResponse>({
|
|
|
841
840
|
useV2Ixs
|
|
842
841
|
);
|
|
843
842
|
|
|
844
|
-
// Send
|
|
843
|
+
// Send ixs and lookup tables
|
|
845
844
|
return {
|
|
846
845
|
ixs: ixs.instructions,
|
|
847
846
|
flashLoanInfo: ixs.flashLoanInfo,
|
|
@@ -851,7 +850,7 @@ export async function getWithdrawWithLeverageIxns<QuoteResponse>({
|
|
|
851
850
|
};
|
|
852
851
|
}
|
|
853
852
|
|
|
854
|
-
export async function
|
|
853
|
+
export async function buildWithdrawWithLeverageIxs(
|
|
855
854
|
market: KaminoMarket,
|
|
856
855
|
debtReserve: KaminoReserve,
|
|
857
856
|
collReserve: KaminoReserve,
|
|
@@ -868,7 +867,7 @@ export async function buildWithdrawWithLeverageIxns(
|
|
|
868
867
|
strategy: StrategyWithAddress | undefined,
|
|
869
868
|
collIsKtoken: boolean,
|
|
870
869
|
useV2Ixs: boolean
|
|
871
|
-
): Promise<
|
|
870
|
+
): Promise<LeverageIxsOutput> {
|
|
872
871
|
const collTokenMint = collReserve.getLiquidityMint();
|
|
873
872
|
const debtTokenMint = debtReserve.getLiquidityMint();
|
|
874
873
|
const debtTokenAta = getAssociatedTokenAddressSync(
|
|
@@ -925,24 +924,24 @@ export async function buildWithdrawWithLeverageIxns(
|
|
|
925
924
|
];
|
|
926
925
|
}
|
|
927
926
|
|
|
928
|
-
const
|
|
927
|
+
const atasAndCreateIxs = createAtasIdempotent(owner, mintsToCreateAtas);
|
|
929
928
|
|
|
930
|
-
const
|
|
929
|
+
const closeWsolAtaIxs: TransactionInstruction[] = [];
|
|
931
930
|
if (depositTokenIsSol || debtTokenMint.equals(NATIVE_MINT)) {
|
|
932
931
|
const wsolAta = getAssociatedTokenAddress(NATIVE_MINT, owner, false);
|
|
933
|
-
|
|
932
|
+
closeWsolAtaIxs.push(createCloseAccountInstruction(wsolAta, owner, owner, [], TOKEN_PROGRAM_ID));
|
|
934
933
|
}
|
|
935
934
|
|
|
936
|
-
const
|
|
935
|
+
const budgetIxs = budgetAndPriorityFeeIxs || getComputeBudgetAndPriorityFeeIxs(3000000);
|
|
937
936
|
|
|
938
937
|
// TODO: Mihai/Marius check if we can improve this logic and not convert any SOL
|
|
939
938
|
// This is here so that we have enough wsol to repay in case the kAB swapped to sol after estimates is not enough
|
|
940
|
-
const
|
|
939
|
+
const fillWsolAtaIxs: TransactionInstruction[] = [];
|
|
941
940
|
if (debtTokenMint.equals(NATIVE_MINT)) {
|
|
942
941
|
const halfSolBalance = (await market.getConnection().getBalance(owner)) / LAMPORTS_PER_SOL / 2;
|
|
943
942
|
const balanceToWrap = halfSolBalance < 0.1 ? halfSolBalance : 0.1;
|
|
944
|
-
|
|
945
|
-
...
|
|
943
|
+
fillWsolAtaIxs.push(
|
|
944
|
+
...getTransferWsolIxs(
|
|
946
945
|
owner,
|
|
947
946
|
getAssociatedTokenAddressSync(NATIVE_MINT, owner),
|
|
948
947
|
toLamports(balanceToWrap, SOL_DECIMALS).ceil()
|
|
@@ -952,12 +951,12 @@ export async function buildWithdrawWithLeverageIxns(
|
|
|
952
951
|
|
|
953
952
|
const scopeRefreshIxn = await getScopeRefreshIx(market, collReserve, debtReserve, obligation, scopeRefreshConfig);
|
|
954
953
|
|
|
955
|
-
// 2. Prepare the flash borrow and flash repay amounts and
|
|
954
|
+
// 2. Prepare the flash borrow and flash repay amounts and ixs
|
|
956
955
|
// We borrow exactly how much we need to repay
|
|
957
956
|
// and repay that + flash amount fee
|
|
958
957
|
const { flashBorrowIxn, flashRepayIxn } = getFlashLoanInstructions({
|
|
959
958
|
borrowIxnIndex:
|
|
960
|
-
|
|
959
|
+
budgetIxs.length + atasAndCreateIxs.length + fillWsolAtaIxs.length + (scopeRefreshIxn.length > 0 ? 1 : 0),
|
|
961
960
|
walletPublicKey: owner,
|
|
962
961
|
lendingMarketAuthority: market.getLendingMarketAuthority(),
|
|
963
962
|
lendingMarketAddress: market.getAddress(),
|
|
@@ -986,13 +985,12 @@ export async function buildWithdrawWithLeverageIxns(
|
|
|
986
985
|
undefined,
|
|
987
986
|
0,
|
|
988
987
|
false,
|
|
989
|
-
false,
|
|
990
|
-
|
|
991
|
-
isClosingPosition,
|
|
988
|
+
false,
|
|
989
|
+
{ skipInitialization: true, skipLutCreation: true }, // to be checked and created in a setup tx in the UI (won't be the case for withdraw anyway as this would be created in deposit)
|
|
992
990
|
referrer
|
|
993
991
|
);
|
|
994
992
|
|
|
995
|
-
const swapInstructions =
|
|
993
|
+
const swapInstructions = removeBudgetAndAtaIxs(swapQuoteIxs.swapIxs, []);
|
|
996
994
|
|
|
997
995
|
return {
|
|
998
996
|
flashLoanInfo: {
|
|
@@ -1001,14 +999,14 @@ export async function buildWithdrawWithLeverageIxns(
|
|
|
1001
999
|
},
|
|
1002
1000
|
instructions: [
|
|
1003
1001
|
...scopeRefreshIxn,
|
|
1004
|
-
...
|
|
1005
|
-
...
|
|
1006
|
-
...
|
|
1002
|
+
...budgetIxs,
|
|
1003
|
+
...atasAndCreateIxs.map((x) => x.createAtaIx),
|
|
1004
|
+
...fillWsolAtaIxs,
|
|
1007
1005
|
...[flashBorrowIxn],
|
|
1008
1006
|
...KaminoAction.actionToIxs(repayAndWithdrawAction),
|
|
1009
1007
|
...swapInstructions,
|
|
1010
1008
|
...[flashRepayIxn],
|
|
1011
|
-
...
|
|
1009
|
+
...closeWsolAtaIxs,
|
|
1012
1010
|
],
|
|
1013
1011
|
};
|
|
1014
1012
|
}
|
|
@@ -1083,7 +1081,7 @@ export async function getAdjustLeverageSwapInputs<QuoteResponse>({
|
|
|
1083
1081
|
);
|
|
1084
1082
|
|
|
1085
1083
|
// Build the repay & withdraw collateral tx to get the number of accounts
|
|
1086
|
-
const klendIxs:
|
|
1084
|
+
const klendIxs: LeverageIxsOutput = await buildIncreaseLeverageIxs(
|
|
1087
1085
|
owner,
|
|
1088
1086
|
kaminoMarket,
|
|
1089
1087
|
collTokenMint,
|
|
@@ -1188,7 +1186,7 @@ export async function getAdjustLeverageSwapInputs<QuoteResponse>({
|
|
|
1188
1186
|
} else {
|
|
1189
1187
|
const calcs = adjustWithdrawLeverageCalcs(adjustDepositPosition, adjustBorrowPosition, flashLoanFee, slippagePct);
|
|
1190
1188
|
|
|
1191
|
-
const klendIxs:
|
|
1189
|
+
const klendIxs: LeverageIxsOutput = await buildDecreaseLeverageIxs(
|
|
1192
1190
|
owner,
|
|
1193
1191
|
kaminoMarket,
|
|
1194
1192
|
collTokenMint,
|
|
@@ -1271,7 +1269,7 @@ export async function getAdjustLeverageSwapInputs<QuoteResponse>({
|
|
|
1271
1269
|
}
|
|
1272
1270
|
}
|
|
1273
1271
|
|
|
1274
|
-
export async function
|
|
1272
|
+
export async function getAdjustLeverageIxs<QuoteResponse>({
|
|
1275
1273
|
owner,
|
|
1276
1274
|
kaminoMarket,
|
|
1277
1275
|
debtTokenMint,
|
|
@@ -1346,7 +1344,7 @@ export async function getAdjustLeverageIxns<QuoteResponse>({
|
|
|
1346
1344
|
initialInputs.swapQuote
|
|
1347
1345
|
);
|
|
1348
1346
|
|
|
1349
|
-
const ixs:
|
|
1347
|
+
const ixs: LeverageIxsOutput = await buildIncreaseLeverageIxs(
|
|
1350
1348
|
owner,
|
|
1351
1349
|
kaminoMarket,
|
|
1352
1350
|
collTokenMint,
|
|
@@ -1387,14 +1385,14 @@ export async function getAdjustLeverageIxns<QuoteResponse>({
|
|
|
1387
1385
|
withdrawSwapper = swapper;
|
|
1388
1386
|
}
|
|
1389
1387
|
|
|
1390
|
-
// 5. Get swap
|
|
1388
|
+
// 5. Get swap ixs
|
|
1391
1389
|
const { swapIxs, lookupTables } = await withdrawSwapper(
|
|
1392
1390
|
swapInputs,
|
|
1393
1391
|
initialInputs.klendAccounts,
|
|
1394
1392
|
initialInputs.swapQuote
|
|
1395
1393
|
);
|
|
1396
1394
|
|
|
1397
|
-
const ixs:
|
|
1395
|
+
const ixs: LeverageIxsOutput = await buildDecreaseLeverageIxs(
|
|
1398
1396
|
owner,
|
|
1399
1397
|
kaminoMarket,
|
|
1400
1398
|
collTokenMint,
|
|
@@ -1428,7 +1426,7 @@ export async function getAdjustLeverageIxns<QuoteResponse>({
|
|
|
1428
1426
|
/**
|
|
1429
1427
|
* Deposit and borrow tokens if leverage increased
|
|
1430
1428
|
*/
|
|
1431
|
-
async function
|
|
1429
|
+
async function buildIncreaseLeverageIxs(
|
|
1432
1430
|
owner: PublicKey,
|
|
1433
1431
|
kaminoMarket: KaminoMarket,
|
|
1434
1432
|
collTokenMint: PublicKey,
|
|
@@ -1441,9 +1439,9 @@ async function buildIncreaseLeverageIxns(
|
|
|
1441
1439
|
scopeRefreshConfig: ScopePriceRefreshConfig | undefined,
|
|
1442
1440
|
collIsKtoken: boolean,
|
|
1443
1441
|
swapQuoteIxs: SwapIxs,
|
|
1444
|
-
|
|
1442
|
+
budgetAndPriorityFeeIxs: TransactionInstruction[] | undefined,
|
|
1445
1443
|
useV2Ixs: boolean
|
|
1446
|
-
): Promise<
|
|
1444
|
+
): Promise<LeverageIxsOutput> {
|
|
1447
1445
|
const collReserve = kaminoMarket.getReserveByMint(collTokenMint);
|
|
1448
1446
|
const debtReserve = kaminoMarket.getReserveByMint(debtTokenMint);
|
|
1449
1447
|
const debtTokenAta = getAssociatedTokenAddressSync(
|
|
@@ -1460,7 +1458,7 @@ async function buildIncreaseLeverageIxns(
|
|
|
1460
1458
|
);
|
|
1461
1459
|
|
|
1462
1460
|
// 1. Create atas & budget txns
|
|
1463
|
-
const
|
|
1461
|
+
const budgetIxs = budgetAndPriorityFeeIxs || getComputeBudgetAndPriorityFeeIxs(3000000);
|
|
1464
1462
|
let mintsToCreateAtas: Array<{ mint: PublicKey; tokenProgram: PublicKey }>;
|
|
1465
1463
|
if (collIsKtoken) {
|
|
1466
1464
|
const secondTokenAta = strategy!.strategy.tokenAMint.equals(debtTokenMint)
|
|
@@ -1508,7 +1506,7 @@ async function buildIncreaseLeverageIxns(
|
|
|
1508
1506
|
];
|
|
1509
1507
|
}
|
|
1510
1508
|
|
|
1511
|
-
const
|
|
1509
|
+
const atasAndCreateIxs = createAtasIdempotent(owner, mintsToCreateAtas);
|
|
1512
1510
|
|
|
1513
1511
|
const scopeRefreshIxn = await getScopeRefreshIx(
|
|
1514
1512
|
kaminoMarket,
|
|
@@ -1520,7 +1518,7 @@ async function buildIncreaseLeverageIxns(
|
|
|
1520
1518
|
|
|
1521
1519
|
// 2. Create borrow flash loan instruction
|
|
1522
1520
|
const { flashBorrowIxn, flashRepayIxn } = getFlashLoanInstructions({
|
|
1523
|
-
borrowIxnIndex:
|
|
1521
|
+
borrowIxnIndex: budgetIxs.length + atasAndCreateIxs.length + (scopeRefreshIxn.length > 0 ? 1 : 0), // TODO: how about user metadata ixs
|
|
1524
1522
|
walletPublicKey: owner,
|
|
1525
1523
|
lendingMarketAuthority: kaminoMarket.getLendingMarketAuthority(),
|
|
1526
1524
|
lendingMarketAddress: kaminoMarket.getAddress(),
|
|
@@ -1547,8 +1545,7 @@ async function buildIncreaseLeverageIxns(
|
|
|
1547
1545
|
0,
|
|
1548
1546
|
false,
|
|
1549
1547
|
false,
|
|
1550
|
-
|
|
1551
|
-
false, // to be checked and create in a setup tx in the UI (won't be the case for adjust anyway as this would be created in deposit)
|
|
1548
|
+
{ skipInitialization: true, skipLutCreation: true },
|
|
1552
1549
|
referrer,
|
|
1553
1550
|
currentSlot
|
|
1554
1551
|
);
|
|
@@ -1565,19 +1562,18 @@ async function buildIncreaseLeverageIxns(
|
|
|
1565
1562
|
0,
|
|
1566
1563
|
false,
|
|
1567
1564
|
false,
|
|
1568
|
-
|
|
1569
|
-
false, // to be checked and create in a setup tx in the UI (won't be the case for adjust anyway as this would be created in deposit)
|
|
1565
|
+
{ skipInitialization: true, skipLutCreation: true }, // to be checked and create in a setup tx in the UI (won't be the case for adjust anyway as this would be created in deposit)
|
|
1570
1566
|
referrer,
|
|
1571
1567
|
currentSlot
|
|
1572
1568
|
);
|
|
1573
1569
|
|
|
1574
|
-
const swapInstructions =
|
|
1570
|
+
const swapInstructions = removeBudgetAndAtaIxs(swapQuoteIxs.swapIxs, []);
|
|
1575
1571
|
|
|
1576
1572
|
const ixs = !collIsKtoken
|
|
1577
1573
|
? [
|
|
1578
1574
|
...scopeRefreshIxn,
|
|
1579
|
-
...
|
|
1580
|
-
...
|
|
1575
|
+
...budgetIxs,
|
|
1576
|
+
...atasAndCreateIxs.map((x) => x.createAtaIx),
|
|
1581
1577
|
...[flashBorrowIxn],
|
|
1582
1578
|
...KaminoAction.actionToIxs(depositAction),
|
|
1583
1579
|
...KaminoAction.actionToIxs(borrowAction),
|
|
@@ -1586,8 +1582,8 @@ async function buildIncreaseLeverageIxns(
|
|
|
1586
1582
|
]
|
|
1587
1583
|
: [
|
|
1588
1584
|
...scopeRefreshIxn,
|
|
1589
|
-
...
|
|
1590
|
-
...
|
|
1585
|
+
...budgetIxs,
|
|
1586
|
+
...atasAndCreateIxs.map((x) => x.createAtaIx),
|
|
1591
1587
|
...[flashBorrowIxn],
|
|
1592
1588
|
...swapInstructions,
|
|
1593
1589
|
...KaminoAction.actionToIxs(depositAction),
|
|
@@ -1596,7 +1592,7 @@ async function buildIncreaseLeverageIxns(
|
|
|
1596
1592
|
];
|
|
1597
1593
|
|
|
1598
1594
|
const flashBorrowReserve = !collIsKtoken ? collReserve! : debtReserve!;
|
|
1599
|
-
const res:
|
|
1595
|
+
const res: LeverageIxsOutput = {
|
|
1600
1596
|
flashLoanInfo: {
|
|
1601
1597
|
flashBorrowReserve: flashBorrowReserve.address,
|
|
1602
1598
|
flashLoanFee: flashBorrowReserve.getFlashLoanFee(),
|
|
@@ -1610,7 +1606,7 @@ async function buildIncreaseLeverageIxns(
|
|
|
1610
1606
|
/**
|
|
1611
1607
|
* Withdraw and repay tokens if leverage decreased
|
|
1612
1608
|
*/
|
|
1613
|
-
async function
|
|
1609
|
+
async function buildDecreaseLeverageIxs(
|
|
1614
1610
|
owner: PublicKey,
|
|
1615
1611
|
kaminoMarket: KaminoMarket,
|
|
1616
1612
|
collTokenMint: PublicKey,
|
|
@@ -1623,9 +1619,9 @@ async function buildDecreaseLeverageIxns(
|
|
|
1623
1619
|
scopeRefreshConfig: ScopePriceRefreshConfig | undefined,
|
|
1624
1620
|
collIsKtoken: boolean,
|
|
1625
1621
|
swapQuoteIxs: SwapIxs,
|
|
1626
|
-
|
|
1622
|
+
budgetAndPriorityFeeIxs: TransactionInstruction[] | undefined,
|
|
1627
1623
|
useV2Ixs: boolean
|
|
1628
|
-
): Promise<
|
|
1624
|
+
): Promise<LeverageIxsOutput> {
|
|
1629
1625
|
const collReserve = kaminoMarket.getReserveByMint(collTokenMint);
|
|
1630
1626
|
const debtReserve = kaminoMarket.getReserveByMint(debtTokenMint);
|
|
1631
1627
|
const debtTokenAta = getAssociatedTokenAddressSync(
|
|
@@ -1636,7 +1632,7 @@ async function buildDecreaseLeverageIxns(
|
|
|
1636
1632
|
);
|
|
1637
1633
|
|
|
1638
1634
|
// 1. Create atas & budget txns
|
|
1639
|
-
const
|
|
1635
|
+
const budgetIxs = budgetAndPriorityFeeIxs || getComputeBudgetAndPriorityFeeIxs(3000000);
|
|
1640
1636
|
let mintsToCreateAtas: Array<{ mint: PublicKey; tokenProgram: PublicKey }>;
|
|
1641
1637
|
if (collIsKtoken) {
|
|
1642
1638
|
const secondTokenAta = strategy!.strategy.tokenAMint.equals(debtTokenMint)
|
|
@@ -1683,21 +1679,21 @@ async function buildDecreaseLeverageIxns(
|
|
|
1683
1679
|
},
|
|
1684
1680
|
];
|
|
1685
1681
|
}
|
|
1686
|
-
const
|
|
1682
|
+
const atasAndCreateIxs = createAtasIdempotent(owner, mintsToCreateAtas);
|
|
1687
1683
|
|
|
1688
1684
|
// TODO: Mihai/Marius check if we can improve this logic and not convert any SOL
|
|
1689
1685
|
// This is here so that we have enough wsol to repay in case the kAB swapped to sol after estimates is not enough
|
|
1690
|
-
const
|
|
1691
|
-
const
|
|
1686
|
+
const closeWsolAtaIxs: TransactionInstruction[] = [];
|
|
1687
|
+
const fillWsolAtaIxs: TransactionInstruction[] = [];
|
|
1692
1688
|
if (debtTokenMint.equals(NATIVE_MINT)) {
|
|
1693
1689
|
const wsolAta = getAssociatedTokenAddress(NATIVE_MINT, owner, false);
|
|
1694
1690
|
|
|
1695
|
-
|
|
1691
|
+
closeWsolAtaIxs.push(createCloseAccountInstruction(wsolAta, owner, owner, [], TOKEN_PROGRAM_ID));
|
|
1696
1692
|
|
|
1697
1693
|
const halfSolBalance = (await kaminoMarket.getConnection().getBalance(owner)) / LAMPORTS_PER_SOL / 2;
|
|
1698
1694
|
const balanceToWrap = halfSolBalance < 0.1 ? halfSolBalance : 0.1;
|
|
1699
|
-
|
|
1700
|
-
...
|
|
1695
|
+
fillWsolAtaIxs.push(
|
|
1696
|
+
...getTransferWsolIxs(owner, wsolAta, toLamports(balanceToWrap, debtReserve!.stats.decimals).ceil())
|
|
1701
1697
|
);
|
|
1702
1698
|
}
|
|
1703
1699
|
|
|
@@ -1712,7 +1708,7 @@ async function buildDecreaseLeverageIxns(
|
|
|
1712
1708
|
// 3. Flash borrow & repay amount to repay (debt)
|
|
1713
1709
|
const { flashBorrowIxn, flashRepayIxn } = getFlashLoanInstructions({
|
|
1714
1710
|
borrowIxnIndex:
|
|
1715
|
-
|
|
1711
|
+
budgetIxs.length + atasAndCreateIxs.length + fillWsolAtaIxs.length + (scopeRefreshIxn.length > 0 ? 1 : 0),
|
|
1716
1712
|
walletPublicKey: owner,
|
|
1717
1713
|
lendingMarketAuthority: kaminoMarket.getLendingMarketAuthority(),
|
|
1718
1714
|
lendingMarketAddress: kaminoMarket.getAddress(),
|
|
@@ -1739,8 +1735,7 @@ async function buildDecreaseLeverageIxns(
|
|
|
1739
1735
|
0,
|
|
1740
1736
|
false,
|
|
1741
1737
|
false,
|
|
1742
|
-
|
|
1743
|
-
false, // to be checked and create in a setup tx in the UI (won't be the case for adjust anyway as this would be created in deposit)
|
|
1738
|
+
{ skipInitialization: true, skipLutCreation: true }, // to be checked and create in a setup tx in the UI (won't be the case for adjust anyway as this would be created in deposit)
|
|
1744
1739
|
referrer
|
|
1745
1740
|
);
|
|
1746
1741
|
|
|
@@ -1756,33 +1751,32 @@ async function buildDecreaseLeverageIxns(
|
|
|
1756
1751
|
0,
|
|
1757
1752
|
false,
|
|
1758
1753
|
false,
|
|
1759
|
-
|
|
1760
|
-
false, // to be checked and create in a setup tx in the UI (won't be the case for adjust anyway as this would be created in deposit)
|
|
1754
|
+
{ skipInitialization: true, skipLutCreation: true }, // to be checked and create in a setup tx in the UI (won't be the case for adjust anyway as this would be created in deposit)
|
|
1761
1755
|
referrer,
|
|
1762
1756
|
currentSlot
|
|
1763
1757
|
);
|
|
1764
1758
|
|
|
1765
|
-
const swapInstructions =
|
|
1759
|
+
const swapInstructions = removeBudgetAndAtaIxs(swapQuoteIxs.swapIxs, []);
|
|
1766
1760
|
|
|
1767
|
-
const
|
|
1761
|
+
const ixs = [
|
|
1768
1762
|
...scopeRefreshIxn,
|
|
1769
|
-
...
|
|
1770
|
-
...
|
|
1771
|
-
...
|
|
1763
|
+
...budgetIxs,
|
|
1764
|
+
...atasAndCreateIxs.map((x) => x.createAtaIx),
|
|
1765
|
+
...fillWsolAtaIxs,
|
|
1772
1766
|
...[flashBorrowIxn],
|
|
1773
1767
|
...KaminoAction.actionToIxs(repayAction),
|
|
1774
1768
|
...KaminoAction.actionToIxs(withdrawAction),
|
|
1775
1769
|
...swapInstructions,
|
|
1776
1770
|
...[flashRepayIxn],
|
|
1777
|
-
...
|
|
1771
|
+
...closeWsolAtaIxs,
|
|
1778
1772
|
];
|
|
1779
1773
|
|
|
1780
|
-
const res:
|
|
1774
|
+
const res: LeverageIxsOutput = {
|
|
1781
1775
|
flashLoanInfo: {
|
|
1782
1776
|
flashBorrowReserve: debtReserve!.address,
|
|
1783
1777
|
flashLoanFee: debtReserve!.getFlashLoanFee(),
|
|
1784
1778
|
},
|
|
1785
|
-
instructions:
|
|
1779
|
+
instructions: ixs,
|
|
1786
1780
|
};
|
|
1787
1781
|
|
|
1788
1782
|
return res;
|
|
@@ -1806,9 +1800,9 @@ export const getScopeRefreshIx = async (
|
|
|
1806
1800
|
: new PublicKeySet<PublicKey>([collReserve.address, debtReserve.address]).toArray();
|
|
1807
1801
|
const tokenIds = getTokenIdsForScopeRefresh(market, allReserves);
|
|
1808
1802
|
|
|
1809
|
-
const
|
|
1803
|
+
const scopeRefreshIxs: TransactionInstruction[] = [];
|
|
1810
1804
|
if (tokenIds.length > 0 && scopeRefreshConfig) {
|
|
1811
|
-
|
|
1805
|
+
scopeRefreshIxs.push(
|
|
1812
1806
|
await scopeRefreshConfig.scope.refreshPriceListIx(
|
|
1813
1807
|
{
|
|
1814
1808
|
feed: scopeRefreshConfig.scopeFeed,
|
|
@@ -1818,5 +1812,5 @@ export const getScopeRefreshIx = async (
|
|
|
1818
1812
|
);
|
|
1819
1813
|
}
|
|
1820
1814
|
|
|
1821
|
-
return
|
|
1815
|
+
return scopeRefreshIxs;
|
|
1822
1816
|
};
|
package/src/leverage/types.ts
CHANGED
package/src/leverage/utils.ts
CHANGED
|
@@ -32,7 +32,7 @@ export async function getTokenToKtokenSwapper<QuoteResponse>(
|
|
|
32
32
|
slippagePct: Decimal,
|
|
33
33
|
swapper: SwapIxsProvider<QuoteResponse>,
|
|
34
34
|
priceAinB: PriceAinBProvider,
|
|
35
|
-
|
|
35
|
+
includeAtaIxs: boolean = true
|
|
36
36
|
): Promise<SwapIxsProvider<QuoteResponse>> {
|
|
37
37
|
return async (
|
|
38
38
|
inputs: SwapInputs,
|
|
@@ -58,7 +58,7 @@ export async function getTokenToKtokenSwapper<QuoteResponse>(
|
|
|
58
58
|
inputs.amountDebtAtaBalance,
|
|
59
59
|
swapper,
|
|
60
60
|
priceAinB,
|
|
61
|
-
|
|
61
|
+
includeAtaIxs,
|
|
62
62
|
klendAccounts,
|
|
63
63
|
quote
|
|
64
64
|
))!;
|
|
@@ -84,7 +84,7 @@ export async function getKtokenDepositIxs<QuoteResponse>(
|
|
|
84
84
|
amountExpectedDepositAtaBalance: Decimal,
|
|
85
85
|
swapper: SwapIxsProvider<QuoteResponse>,
|
|
86
86
|
priceAinB: PriceAinBProvider,
|
|
87
|
-
|
|
87
|
+
includeAtaIxs: boolean = true,
|
|
88
88
|
klendAccounts: Array<PublicKey>,
|
|
89
89
|
quote: SwapQuote<QuoteResponse>
|
|
90
90
|
) {
|
|
@@ -106,7 +106,7 @@ export async function getKtokenDepositIxs<QuoteResponse>(
|
|
|
106
106
|
swapProviderToKaminoSwapProvider(swapper, klendAccounts, quote),
|
|
107
107
|
tokensBalances,
|
|
108
108
|
priceAinBDecimal,
|
|
109
|
-
|
|
109
|
+
includeAtaIxs
|
|
110
110
|
);
|
|
111
111
|
} else if (tokenBMint.equals(depositTokenMint)) {
|
|
112
112
|
const aBalance = await getTokenAccountBalanceDecimal(connection, tokenAMint, depositor);
|
|
@@ -120,7 +120,7 @@ export async function getKtokenDepositIxs<QuoteResponse>(
|
|
|
120
120
|
swapProviderToKaminoSwapProvider(swapper, klendAccounts, quote),
|
|
121
121
|
tokensBalances,
|
|
122
122
|
priceAinBDecimal,
|
|
123
|
-
|
|
123
|
+
includeAtaIxs
|
|
124
124
|
);
|
|
125
125
|
} else {
|
|
126
126
|
throw Error('Deposit token is neither A nor B in the strategy');
|
|
@@ -9,7 +9,7 @@ import {
|
|
|
9
9
|
initReferrerTokenState,
|
|
10
10
|
} from '../lib';
|
|
11
11
|
|
|
12
|
-
export const
|
|
12
|
+
export const getInitAllReferrerTokenStateIxs = async ({
|
|
13
13
|
referrer,
|
|
14
14
|
kaminoMarket,
|
|
15
15
|
payer = referrer,
|
|
@@ -24,7 +24,7 @@ export const getInitAllReferrerTokenStateIxns = async ({
|
|
|
24
24
|
|
|
25
25
|
await kaminoMarket.loadReserves();
|
|
26
26
|
|
|
27
|
-
const
|
|
27
|
+
const initReferrerTokenStateIxs: TransactionInstruction[] = [];
|
|
28
28
|
|
|
29
29
|
const tokenStatesToCreate: [PublicKey, PublicKey][] = [];
|
|
30
30
|
const reserves = kaminoMarket.getReserves();
|
|
@@ -53,13 +53,13 @@ export const getInitAllReferrerTokenStateIxns = async ({
|
|
|
53
53
|
kaminoMarket.programId
|
|
54
54
|
);
|
|
55
55
|
|
|
56
|
-
|
|
56
|
+
initReferrerTokenStateIxs.push(initReferrerTokenStateIx);
|
|
57
57
|
});
|
|
58
58
|
|
|
59
|
-
return
|
|
59
|
+
return initReferrerTokenStateIxs;
|
|
60
60
|
};
|
|
61
61
|
|
|
62
|
-
export const
|
|
62
|
+
export const getInitReferrerStateAndShortUrlIxs = ({
|
|
63
63
|
referrer,
|
|
64
64
|
shortUrl,
|
|
65
65
|
programId = PROGRAM_ID,
|
|
@@ -92,7 +92,7 @@ export const getInitReferrerStateAndShortUrlIxns = ({
|
|
|
92
92
|
};
|
|
93
93
|
|
|
94
94
|
// TODO: 1 thing left before adding program id
|
|
95
|
-
export const
|
|
95
|
+
export const getDeleteReferrerStateAndShortUrlIxs = async ({
|
|
96
96
|
referrer,
|
|
97
97
|
connection,
|
|
98
98
|
programId = PROGRAM_ID,
|