@kamino-finance/klend-sdk 5.13.1 → 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 +32 -31
- package/dist/classes/action.d.ts.map +1 -1
- package/dist/classes/action.js +126 -95
- package/dist/classes/action.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/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 +70 -70
- 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 +50 -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.js +6 -6
- 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 +61 -61
- 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 +73 -73
- 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 +143 -102
- package/src/classes/index.ts +5 -2
- package/src/classes/manager.ts +54 -51
- package/src/classes/obligationOrder.ts +1 -1
- package/src/classes/reserve.ts +126 -132
- package/src/classes/shared.ts +15 -0
- package/src/classes/utils.ts +3 -52
- package/src/classes/vault.ts +53 -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 -13
- package/src/lending_operations/swap_collateral_operations.ts +85 -85
- package/src/leverage/calcs.ts +2 -6
- package/src/leverage/operations.ts +84 -84
- 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(),
|
|
@@ -559,7 +559,7 @@ async function buildDepositWithLeverageIxns(
|
|
|
559
559
|
|
|
560
560
|
// 4. Swap
|
|
561
561
|
const { swapIxs } = swapQuoteIxs;
|
|
562
|
-
const swapInstructions =
|
|
562
|
+
const swapInstructions = removeBudgetAndAtaIxs(swapIxs, []);
|
|
563
563
|
const flashBorrowReserve = !collIsKtoken ? collReserve : debtReserve;
|
|
564
564
|
const flashLoanInfo = {
|
|
565
565
|
flashBorrowReserve: flashBorrowReserve.address,
|
|
@@ -571,9 +571,9 @@ async function buildDepositWithLeverageIxns(
|
|
|
571
571
|
flashLoanInfo,
|
|
572
572
|
instructions: [
|
|
573
573
|
...scopeRefreshIxn,
|
|
574
|
-
...
|
|
575
|
-
...
|
|
576
|
-
...
|
|
574
|
+
...budgetIxs,
|
|
575
|
+
...atasAndCreateIxs.map((x) => x.createAtaIx),
|
|
576
|
+
...fillWsolAtaIxs,
|
|
577
577
|
...[flashBorrowIxn],
|
|
578
578
|
...KaminoAction.actionToIxs(kaminoDepositAndBorrowAction),
|
|
579
579
|
...swapInstructions,
|
|
@@ -585,9 +585,9 @@ async function buildDepositWithLeverageIxns(
|
|
|
585
585
|
flashLoanInfo,
|
|
586
586
|
instructions: [
|
|
587
587
|
...scopeRefreshIxn,
|
|
588
|
-
...
|
|
589
|
-
...
|
|
590
|
-
...
|
|
588
|
+
...budgetIxs,
|
|
589
|
+
...atasAndCreateIxs.map((x) => x.createAtaIx),
|
|
590
|
+
...fillWsolAtaIxs,
|
|
591
591
|
...[flashBorrowIxn],
|
|
592
592
|
...swapInstructions,
|
|
593
593
|
...kaminoDepositAndBorrowAction.setupIxs,
|
|
@@ -653,7 +653,7 @@ export async function getWithdrawWithLeverageSwapInputs<QuoteResponse>({
|
|
|
653
653
|
slippagePct
|
|
654
654
|
);
|
|
655
655
|
|
|
656
|
-
const klendIxs = await
|
|
656
|
+
const klendIxs = await buildWithdrawWithLeverageIxs(
|
|
657
657
|
kaminoMarket,
|
|
658
658
|
debtReserve!,
|
|
659
659
|
collReserve!,
|
|
@@ -735,7 +735,7 @@ export async function getWithdrawWithLeverageSwapInputs<QuoteResponse>({
|
|
|
735
735
|
};
|
|
736
736
|
}
|
|
737
737
|
|
|
738
|
-
export async function
|
|
738
|
+
export async function getWithdrawWithLeverageIxs<QuoteResponse>({
|
|
739
739
|
owner,
|
|
740
740
|
kaminoMarket,
|
|
741
741
|
debtTokenMint,
|
|
@@ -817,7 +817,7 @@ export async function getWithdrawWithLeverageIxns<QuoteResponse>({
|
|
|
817
817
|
}
|
|
818
818
|
}
|
|
819
819
|
|
|
820
|
-
const ixs:
|
|
820
|
+
const ixs: LeverageIxsOutput = await buildWithdrawWithLeverageIxs(
|
|
821
821
|
kaminoMarket,
|
|
822
822
|
debtReserve!,
|
|
823
823
|
collReserve!,
|
|
@@ -840,7 +840,7 @@ export async function getWithdrawWithLeverageIxns<QuoteResponse>({
|
|
|
840
840
|
useV2Ixs
|
|
841
841
|
);
|
|
842
842
|
|
|
843
|
-
// Send
|
|
843
|
+
// Send ixs and lookup tables
|
|
844
844
|
return {
|
|
845
845
|
ixs: ixs.instructions,
|
|
846
846
|
flashLoanInfo: ixs.flashLoanInfo,
|
|
@@ -850,7 +850,7 @@ export async function getWithdrawWithLeverageIxns<QuoteResponse>({
|
|
|
850
850
|
};
|
|
851
851
|
}
|
|
852
852
|
|
|
853
|
-
export async function
|
|
853
|
+
export async function buildWithdrawWithLeverageIxs(
|
|
854
854
|
market: KaminoMarket,
|
|
855
855
|
debtReserve: KaminoReserve,
|
|
856
856
|
collReserve: KaminoReserve,
|
|
@@ -867,7 +867,7 @@ export async function buildWithdrawWithLeverageIxns(
|
|
|
867
867
|
strategy: StrategyWithAddress | undefined,
|
|
868
868
|
collIsKtoken: boolean,
|
|
869
869
|
useV2Ixs: boolean
|
|
870
|
-
): Promise<
|
|
870
|
+
): Promise<LeverageIxsOutput> {
|
|
871
871
|
const collTokenMint = collReserve.getLiquidityMint();
|
|
872
872
|
const debtTokenMint = debtReserve.getLiquidityMint();
|
|
873
873
|
const debtTokenAta = getAssociatedTokenAddressSync(
|
|
@@ -924,24 +924,24 @@ export async function buildWithdrawWithLeverageIxns(
|
|
|
924
924
|
];
|
|
925
925
|
}
|
|
926
926
|
|
|
927
|
-
const
|
|
927
|
+
const atasAndCreateIxs = createAtasIdempotent(owner, mintsToCreateAtas);
|
|
928
928
|
|
|
929
|
-
const
|
|
929
|
+
const closeWsolAtaIxs: TransactionInstruction[] = [];
|
|
930
930
|
if (depositTokenIsSol || debtTokenMint.equals(NATIVE_MINT)) {
|
|
931
931
|
const wsolAta = getAssociatedTokenAddress(NATIVE_MINT, owner, false);
|
|
932
|
-
|
|
932
|
+
closeWsolAtaIxs.push(createCloseAccountInstruction(wsolAta, owner, owner, [], TOKEN_PROGRAM_ID));
|
|
933
933
|
}
|
|
934
934
|
|
|
935
|
-
const
|
|
935
|
+
const budgetIxs = budgetAndPriorityFeeIxs || getComputeBudgetAndPriorityFeeIxs(3000000);
|
|
936
936
|
|
|
937
937
|
// TODO: Mihai/Marius check if we can improve this logic and not convert any SOL
|
|
938
938
|
// This is here so that we have enough wsol to repay in case the kAB swapped to sol after estimates is not enough
|
|
939
|
-
const
|
|
939
|
+
const fillWsolAtaIxs: TransactionInstruction[] = [];
|
|
940
940
|
if (debtTokenMint.equals(NATIVE_MINT)) {
|
|
941
941
|
const halfSolBalance = (await market.getConnection().getBalance(owner)) / LAMPORTS_PER_SOL / 2;
|
|
942
942
|
const balanceToWrap = halfSolBalance < 0.1 ? halfSolBalance : 0.1;
|
|
943
|
-
|
|
944
|
-
...
|
|
943
|
+
fillWsolAtaIxs.push(
|
|
944
|
+
...getTransferWsolIxs(
|
|
945
945
|
owner,
|
|
946
946
|
getAssociatedTokenAddressSync(NATIVE_MINT, owner),
|
|
947
947
|
toLamports(balanceToWrap, SOL_DECIMALS).ceil()
|
|
@@ -951,12 +951,12 @@ export async function buildWithdrawWithLeverageIxns(
|
|
|
951
951
|
|
|
952
952
|
const scopeRefreshIxn = await getScopeRefreshIx(market, collReserve, debtReserve, obligation, scopeRefreshConfig);
|
|
953
953
|
|
|
954
|
-
// 2. Prepare the flash borrow and flash repay amounts and
|
|
954
|
+
// 2. Prepare the flash borrow and flash repay amounts and ixs
|
|
955
955
|
// We borrow exactly how much we need to repay
|
|
956
956
|
// and repay that + flash amount fee
|
|
957
957
|
const { flashBorrowIxn, flashRepayIxn } = getFlashLoanInstructions({
|
|
958
958
|
borrowIxnIndex:
|
|
959
|
-
|
|
959
|
+
budgetIxs.length + atasAndCreateIxs.length + fillWsolAtaIxs.length + (scopeRefreshIxn.length > 0 ? 1 : 0),
|
|
960
960
|
walletPublicKey: owner,
|
|
961
961
|
lendingMarketAuthority: market.getLendingMarketAuthority(),
|
|
962
962
|
lendingMarketAddress: market.getAddress(),
|
|
@@ -990,7 +990,7 @@ export async function buildWithdrawWithLeverageIxns(
|
|
|
990
990
|
referrer
|
|
991
991
|
);
|
|
992
992
|
|
|
993
|
-
const swapInstructions =
|
|
993
|
+
const swapInstructions = removeBudgetAndAtaIxs(swapQuoteIxs.swapIxs, []);
|
|
994
994
|
|
|
995
995
|
return {
|
|
996
996
|
flashLoanInfo: {
|
|
@@ -999,14 +999,14 @@ export async function buildWithdrawWithLeverageIxns(
|
|
|
999
999
|
},
|
|
1000
1000
|
instructions: [
|
|
1001
1001
|
...scopeRefreshIxn,
|
|
1002
|
-
...
|
|
1003
|
-
...
|
|
1004
|
-
...
|
|
1002
|
+
...budgetIxs,
|
|
1003
|
+
...atasAndCreateIxs.map((x) => x.createAtaIx),
|
|
1004
|
+
...fillWsolAtaIxs,
|
|
1005
1005
|
...[flashBorrowIxn],
|
|
1006
1006
|
...KaminoAction.actionToIxs(repayAndWithdrawAction),
|
|
1007
1007
|
...swapInstructions,
|
|
1008
1008
|
...[flashRepayIxn],
|
|
1009
|
-
...
|
|
1009
|
+
...closeWsolAtaIxs,
|
|
1010
1010
|
],
|
|
1011
1011
|
};
|
|
1012
1012
|
}
|
|
@@ -1081,7 +1081,7 @@ export async function getAdjustLeverageSwapInputs<QuoteResponse>({
|
|
|
1081
1081
|
);
|
|
1082
1082
|
|
|
1083
1083
|
// Build the repay & withdraw collateral tx to get the number of accounts
|
|
1084
|
-
const klendIxs:
|
|
1084
|
+
const klendIxs: LeverageIxsOutput = await buildIncreaseLeverageIxs(
|
|
1085
1085
|
owner,
|
|
1086
1086
|
kaminoMarket,
|
|
1087
1087
|
collTokenMint,
|
|
@@ -1186,7 +1186,7 @@ export async function getAdjustLeverageSwapInputs<QuoteResponse>({
|
|
|
1186
1186
|
} else {
|
|
1187
1187
|
const calcs = adjustWithdrawLeverageCalcs(adjustDepositPosition, adjustBorrowPosition, flashLoanFee, slippagePct);
|
|
1188
1188
|
|
|
1189
|
-
const klendIxs:
|
|
1189
|
+
const klendIxs: LeverageIxsOutput = await buildDecreaseLeverageIxs(
|
|
1190
1190
|
owner,
|
|
1191
1191
|
kaminoMarket,
|
|
1192
1192
|
collTokenMint,
|
|
@@ -1269,7 +1269,7 @@ export async function getAdjustLeverageSwapInputs<QuoteResponse>({
|
|
|
1269
1269
|
}
|
|
1270
1270
|
}
|
|
1271
1271
|
|
|
1272
|
-
export async function
|
|
1272
|
+
export async function getAdjustLeverageIxs<QuoteResponse>({
|
|
1273
1273
|
owner,
|
|
1274
1274
|
kaminoMarket,
|
|
1275
1275
|
debtTokenMint,
|
|
@@ -1344,7 +1344,7 @@ export async function getAdjustLeverageIxns<QuoteResponse>({
|
|
|
1344
1344
|
initialInputs.swapQuote
|
|
1345
1345
|
);
|
|
1346
1346
|
|
|
1347
|
-
const ixs:
|
|
1347
|
+
const ixs: LeverageIxsOutput = await buildIncreaseLeverageIxs(
|
|
1348
1348
|
owner,
|
|
1349
1349
|
kaminoMarket,
|
|
1350
1350
|
collTokenMint,
|
|
@@ -1385,14 +1385,14 @@ export async function getAdjustLeverageIxns<QuoteResponse>({
|
|
|
1385
1385
|
withdrawSwapper = swapper;
|
|
1386
1386
|
}
|
|
1387
1387
|
|
|
1388
|
-
// 5. Get swap
|
|
1388
|
+
// 5. Get swap ixs
|
|
1389
1389
|
const { swapIxs, lookupTables } = await withdrawSwapper(
|
|
1390
1390
|
swapInputs,
|
|
1391
1391
|
initialInputs.klendAccounts,
|
|
1392
1392
|
initialInputs.swapQuote
|
|
1393
1393
|
);
|
|
1394
1394
|
|
|
1395
|
-
const ixs:
|
|
1395
|
+
const ixs: LeverageIxsOutput = await buildDecreaseLeverageIxs(
|
|
1396
1396
|
owner,
|
|
1397
1397
|
kaminoMarket,
|
|
1398
1398
|
collTokenMint,
|
|
@@ -1426,7 +1426,7 @@ export async function getAdjustLeverageIxns<QuoteResponse>({
|
|
|
1426
1426
|
/**
|
|
1427
1427
|
* Deposit and borrow tokens if leverage increased
|
|
1428
1428
|
*/
|
|
1429
|
-
async function
|
|
1429
|
+
async function buildIncreaseLeverageIxs(
|
|
1430
1430
|
owner: PublicKey,
|
|
1431
1431
|
kaminoMarket: KaminoMarket,
|
|
1432
1432
|
collTokenMint: PublicKey,
|
|
@@ -1439,9 +1439,9 @@ async function buildIncreaseLeverageIxns(
|
|
|
1439
1439
|
scopeRefreshConfig: ScopePriceRefreshConfig | undefined,
|
|
1440
1440
|
collIsKtoken: boolean,
|
|
1441
1441
|
swapQuoteIxs: SwapIxs,
|
|
1442
|
-
|
|
1442
|
+
budgetAndPriorityFeeIxs: TransactionInstruction[] | undefined,
|
|
1443
1443
|
useV2Ixs: boolean
|
|
1444
|
-
): Promise<
|
|
1444
|
+
): Promise<LeverageIxsOutput> {
|
|
1445
1445
|
const collReserve = kaminoMarket.getReserveByMint(collTokenMint);
|
|
1446
1446
|
const debtReserve = kaminoMarket.getReserveByMint(debtTokenMint);
|
|
1447
1447
|
const debtTokenAta = getAssociatedTokenAddressSync(
|
|
@@ -1458,7 +1458,7 @@ async function buildIncreaseLeverageIxns(
|
|
|
1458
1458
|
);
|
|
1459
1459
|
|
|
1460
1460
|
// 1. Create atas & budget txns
|
|
1461
|
-
const
|
|
1461
|
+
const budgetIxs = budgetAndPriorityFeeIxs || getComputeBudgetAndPriorityFeeIxs(3000000);
|
|
1462
1462
|
let mintsToCreateAtas: Array<{ mint: PublicKey; tokenProgram: PublicKey }>;
|
|
1463
1463
|
if (collIsKtoken) {
|
|
1464
1464
|
const secondTokenAta = strategy!.strategy.tokenAMint.equals(debtTokenMint)
|
|
@@ -1506,7 +1506,7 @@ async function buildIncreaseLeverageIxns(
|
|
|
1506
1506
|
];
|
|
1507
1507
|
}
|
|
1508
1508
|
|
|
1509
|
-
const
|
|
1509
|
+
const atasAndCreateIxs = createAtasIdempotent(owner, mintsToCreateAtas);
|
|
1510
1510
|
|
|
1511
1511
|
const scopeRefreshIxn = await getScopeRefreshIx(
|
|
1512
1512
|
kaminoMarket,
|
|
@@ -1518,7 +1518,7 @@ async function buildIncreaseLeverageIxns(
|
|
|
1518
1518
|
|
|
1519
1519
|
// 2. Create borrow flash loan instruction
|
|
1520
1520
|
const { flashBorrowIxn, flashRepayIxn } = getFlashLoanInstructions({
|
|
1521
|
-
borrowIxnIndex:
|
|
1521
|
+
borrowIxnIndex: budgetIxs.length + atasAndCreateIxs.length + (scopeRefreshIxn.length > 0 ? 1 : 0), // TODO: how about user metadata ixs
|
|
1522
1522
|
walletPublicKey: owner,
|
|
1523
1523
|
lendingMarketAuthority: kaminoMarket.getLendingMarketAuthority(),
|
|
1524
1524
|
lendingMarketAddress: kaminoMarket.getAddress(),
|
|
@@ -1567,13 +1567,13 @@ async function buildIncreaseLeverageIxns(
|
|
|
1567
1567
|
currentSlot
|
|
1568
1568
|
);
|
|
1569
1569
|
|
|
1570
|
-
const swapInstructions =
|
|
1570
|
+
const swapInstructions = removeBudgetAndAtaIxs(swapQuoteIxs.swapIxs, []);
|
|
1571
1571
|
|
|
1572
1572
|
const ixs = !collIsKtoken
|
|
1573
1573
|
? [
|
|
1574
1574
|
...scopeRefreshIxn,
|
|
1575
|
-
...
|
|
1576
|
-
...
|
|
1575
|
+
...budgetIxs,
|
|
1576
|
+
...atasAndCreateIxs.map((x) => x.createAtaIx),
|
|
1577
1577
|
...[flashBorrowIxn],
|
|
1578
1578
|
...KaminoAction.actionToIxs(depositAction),
|
|
1579
1579
|
...KaminoAction.actionToIxs(borrowAction),
|
|
@@ -1582,8 +1582,8 @@ async function buildIncreaseLeverageIxns(
|
|
|
1582
1582
|
]
|
|
1583
1583
|
: [
|
|
1584
1584
|
...scopeRefreshIxn,
|
|
1585
|
-
...
|
|
1586
|
-
...
|
|
1585
|
+
...budgetIxs,
|
|
1586
|
+
...atasAndCreateIxs.map((x) => x.createAtaIx),
|
|
1587
1587
|
...[flashBorrowIxn],
|
|
1588
1588
|
...swapInstructions,
|
|
1589
1589
|
...KaminoAction.actionToIxs(depositAction),
|
|
@@ -1592,7 +1592,7 @@ async function buildIncreaseLeverageIxns(
|
|
|
1592
1592
|
];
|
|
1593
1593
|
|
|
1594
1594
|
const flashBorrowReserve = !collIsKtoken ? collReserve! : debtReserve!;
|
|
1595
|
-
const res:
|
|
1595
|
+
const res: LeverageIxsOutput = {
|
|
1596
1596
|
flashLoanInfo: {
|
|
1597
1597
|
flashBorrowReserve: flashBorrowReserve.address,
|
|
1598
1598
|
flashLoanFee: flashBorrowReserve.getFlashLoanFee(),
|
|
@@ -1606,7 +1606,7 @@ async function buildIncreaseLeverageIxns(
|
|
|
1606
1606
|
/**
|
|
1607
1607
|
* Withdraw and repay tokens if leverage decreased
|
|
1608
1608
|
*/
|
|
1609
|
-
async function
|
|
1609
|
+
async function buildDecreaseLeverageIxs(
|
|
1610
1610
|
owner: PublicKey,
|
|
1611
1611
|
kaminoMarket: KaminoMarket,
|
|
1612
1612
|
collTokenMint: PublicKey,
|
|
@@ -1619,9 +1619,9 @@ async function buildDecreaseLeverageIxns(
|
|
|
1619
1619
|
scopeRefreshConfig: ScopePriceRefreshConfig | undefined,
|
|
1620
1620
|
collIsKtoken: boolean,
|
|
1621
1621
|
swapQuoteIxs: SwapIxs,
|
|
1622
|
-
|
|
1622
|
+
budgetAndPriorityFeeIxs: TransactionInstruction[] | undefined,
|
|
1623
1623
|
useV2Ixs: boolean
|
|
1624
|
-
): Promise<
|
|
1624
|
+
): Promise<LeverageIxsOutput> {
|
|
1625
1625
|
const collReserve = kaminoMarket.getReserveByMint(collTokenMint);
|
|
1626
1626
|
const debtReserve = kaminoMarket.getReserveByMint(debtTokenMint);
|
|
1627
1627
|
const debtTokenAta = getAssociatedTokenAddressSync(
|
|
@@ -1632,7 +1632,7 @@ async function buildDecreaseLeverageIxns(
|
|
|
1632
1632
|
);
|
|
1633
1633
|
|
|
1634
1634
|
// 1. Create atas & budget txns
|
|
1635
|
-
const
|
|
1635
|
+
const budgetIxs = budgetAndPriorityFeeIxs || getComputeBudgetAndPriorityFeeIxs(3000000);
|
|
1636
1636
|
let mintsToCreateAtas: Array<{ mint: PublicKey; tokenProgram: PublicKey }>;
|
|
1637
1637
|
if (collIsKtoken) {
|
|
1638
1638
|
const secondTokenAta = strategy!.strategy.tokenAMint.equals(debtTokenMint)
|
|
@@ -1679,21 +1679,21 @@ async function buildDecreaseLeverageIxns(
|
|
|
1679
1679
|
},
|
|
1680
1680
|
];
|
|
1681
1681
|
}
|
|
1682
|
-
const
|
|
1682
|
+
const atasAndCreateIxs = createAtasIdempotent(owner, mintsToCreateAtas);
|
|
1683
1683
|
|
|
1684
1684
|
// TODO: Mihai/Marius check if we can improve this logic and not convert any SOL
|
|
1685
1685
|
// This is here so that we have enough wsol to repay in case the kAB swapped to sol after estimates is not enough
|
|
1686
|
-
const
|
|
1687
|
-
const
|
|
1686
|
+
const closeWsolAtaIxs: TransactionInstruction[] = [];
|
|
1687
|
+
const fillWsolAtaIxs: TransactionInstruction[] = [];
|
|
1688
1688
|
if (debtTokenMint.equals(NATIVE_MINT)) {
|
|
1689
1689
|
const wsolAta = getAssociatedTokenAddress(NATIVE_MINT, owner, false);
|
|
1690
1690
|
|
|
1691
|
-
|
|
1691
|
+
closeWsolAtaIxs.push(createCloseAccountInstruction(wsolAta, owner, owner, [], TOKEN_PROGRAM_ID));
|
|
1692
1692
|
|
|
1693
1693
|
const halfSolBalance = (await kaminoMarket.getConnection().getBalance(owner)) / LAMPORTS_PER_SOL / 2;
|
|
1694
1694
|
const balanceToWrap = halfSolBalance < 0.1 ? halfSolBalance : 0.1;
|
|
1695
|
-
|
|
1696
|
-
...
|
|
1695
|
+
fillWsolAtaIxs.push(
|
|
1696
|
+
...getTransferWsolIxs(owner, wsolAta, toLamports(balanceToWrap, debtReserve!.stats.decimals).ceil())
|
|
1697
1697
|
);
|
|
1698
1698
|
}
|
|
1699
1699
|
|
|
@@ -1708,7 +1708,7 @@ async function buildDecreaseLeverageIxns(
|
|
|
1708
1708
|
// 3. Flash borrow & repay amount to repay (debt)
|
|
1709
1709
|
const { flashBorrowIxn, flashRepayIxn } = getFlashLoanInstructions({
|
|
1710
1710
|
borrowIxnIndex:
|
|
1711
|
-
|
|
1711
|
+
budgetIxs.length + atasAndCreateIxs.length + fillWsolAtaIxs.length + (scopeRefreshIxn.length > 0 ? 1 : 0),
|
|
1712
1712
|
walletPublicKey: owner,
|
|
1713
1713
|
lendingMarketAuthority: kaminoMarket.getLendingMarketAuthority(),
|
|
1714
1714
|
lendingMarketAddress: kaminoMarket.getAddress(),
|
|
@@ -1756,27 +1756,27 @@ async function buildDecreaseLeverageIxns(
|
|
|
1756
1756
|
currentSlot
|
|
1757
1757
|
);
|
|
1758
1758
|
|
|
1759
|
-
const swapInstructions =
|
|
1759
|
+
const swapInstructions = removeBudgetAndAtaIxs(swapQuoteIxs.swapIxs, []);
|
|
1760
1760
|
|
|
1761
|
-
const
|
|
1761
|
+
const ixs = [
|
|
1762
1762
|
...scopeRefreshIxn,
|
|
1763
|
-
...
|
|
1764
|
-
...
|
|
1765
|
-
...
|
|
1763
|
+
...budgetIxs,
|
|
1764
|
+
...atasAndCreateIxs.map((x) => x.createAtaIx),
|
|
1765
|
+
...fillWsolAtaIxs,
|
|
1766
1766
|
...[flashBorrowIxn],
|
|
1767
1767
|
...KaminoAction.actionToIxs(repayAction),
|
|
1768
1768
|
...KaminoAction.actionToIxs(withdrawAction),
|
|
1769
1769
|
...swapInstructions,
|
|
1770
1770
|
...[flashRepayIxn],
|
|
1771
|
-
...
|
|
1771
|
+
...closeWsolAtaIxs,
|
|
1772
1772
|
];
|
|
1773
1773
|
|
|
1774
|
-
const res:
|
|
1774
|
+
const res: LeverageIxsOutput = {
|
|
1775
1775
|
flashLoanInfo: {
|
|
1776
1776
|
flashBorrowReserve: debtReserve!.address,
|
|
1777
1777
|
flashLoanFee: debtReserve!.getFlashLoanFee(),
|
|
1778
1778
|
},
|
|
1779
|
-
instructions:
|
|
1779
|
+
instructions: ixs,
|
|
1780
1780
|
};
|
|
1781
1781
|
|
|
1782
1782
|
return res;
|
|
@@ -1800,9 +1800,9 @@ export const getScopeRefreshIx = async (
|
|
|
1800
1800
|
: new PublicKeySet<PublicKey>([collReserve.address, debtReserve.address]).toArray();
|
|
1801
1801
|
const tokenIds = getTokenIdsForScopeRefresh(market, allReserves);
|
|
1802
1802
|
|
|
1803
|
-
const
|
|
1803
|
+
const scopeRefreshIxs: TransactionInstruction[] = [];
|
|
1804
1804
|
if (tokenIds.length > 0 && scopeRefreshConfig) {
|
|
1805
|
-
|
|
1805
|
+
scopeRefreshIxs.push(
|
|
1806
1806
|
await scopeRefreshConfig.scope.refreshPriceListIx(
|
|
1807
1807
|
{
|
|
1808
1808
|
feed: scopeRefreshConfig.scopeFeed,
|
|
@@ -1812,5 +1812,5 @@ export const getScopeRefreshIx = async (
|
|
|
1812
1812
|
);
|
|
1813
1813
|
}
|
|
1814
1814
|
|
|
1815
|
-
return
|
|
1815
|
+
return scopeRefreshIxs;
|
|
1816
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,
|
|
@@ -8,9 +8,9 @@ import {
|
|
|
8
8
|
shortUrlPda,
|
|
9
9
|
} from '../utils';
|
|
10
10
|
import {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
getDeleteReferrerStateAndShortUrlIxs,
|
|
12
|
+
getInitAllReferrerTokenStateIxs,
|
|
13
|
+
getInitReferrerStateAndShortUrlIxs,
|
|
14
14
|
} from './instructions';
|
|
15
15
|
import { PROGRAM_ID, UserMetadata, ReferrerState, ShortUrl } from '../lib';
|
|
16
16
|
import Decimal from 'decimal.js';
|
|
@@ -27,9 +27,9 @@ export const initAllReferrerTokenStates = async ({
|
|
|
27
27
|
referrer: Keypair;
|
|
28
28
|
kaminoMarket: KaminoMarket;
|
|
29
29
|
}) => {
|
|
30
|
-
const
|
|
30
|
+
const ixs = await getInitAllReferrerTokenStateIxs({ referrer: referrer.publicKey, kaminoMarket });
|
|
31
31
|
|
|
32
|
-
const tx = await buildVersionedTransaction(kaminoMarket.getConnection(), referrer.publicKey,
|
|
32
|
+
const tx = await buildVersionedTransaction(kaminoMarket.getConnection(), referrer.publicKey, ixs);
|
|
33
33
|
|
|
34
34
|
console.log('Init Referral Token States');
|
|
35
35
|
|
|
@@ -53,9 +53,9 @@ export const createReferrerStateAndShortUrl = async ({
|
|
|
53
53
|
shortUrl: string;
|
|
54
54
|
programId?: PublicKey;
|
|
55
55
|
}) => {
|
|
56
|
-
const
|
|
56
|
+
const ix = getInitReferrerStateAndShortUrlIxs({ referrer: referrer.publicKey, shortUrl, programId });
|
|
57
57
|
|
|
58
|
-
const tx = await buildVersionedTransaction(connection, referrer.publicKey, [
|
|
58
|
+
const tx = await buildVersionedTransaction(connection, referrer.publicKey, [ix]);
|
|
59
59
|
|
|
60
60
|
console.log('Init ReferrerState for referrer ' + referrer.publicKey.toBase58() + ' and shortUrl ' + shortUrl);
|
|
61
61
|
|
|
@@ -79,13 +79,13 @@ export const updateReferrerStateAndShortUrl = async ({
|
|
|
79
79
|
newShortUrl: string;
|
|
80
80
|
programId?: PublicKey;
|
|
81
81
|
}) => {
|
|
82
|
-
const deleteIxn = await
|
|
82
|
+
const deleteIxn = await getDeleteReferrerStateAndShortUrlIxs({
|
|
83
83
|
referrer: referrer.publicKey,
|
|
84
84
|
connection,
|
|
85
85
|
programId,
|
|
86
86
|
});
|
|
87
87
|
|
|
88
|
-
const initIxn =
|
|
88
|
+
const initIxn = getInitReferrerStateAndShortUrlIxs({
|
|
89
89
|
referrer: referrer.publicKey,
|
|
90
90
|
shortUrl: newShortUrl,
|
|
91
91
|
programId,
|